project QadFinancials > class BDInvoice > method ValidateComponentPre

Description

This method includes validations that are done before the ancestor code of ValidateComponent.
Give some fields of the debtor invoice and his sub-tables the right value.
For example : Give DInvoiceOriginalDebitTC, DInvoiceOriginalCreditTC, ... the right value based on the Excluded tax flag.


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.ValidateComponent


program code (program5/bdinvoice.p)

/* ======================================================================================================================= */
/* Backwards-compatibility: Make sure some new mandatory fields get a default value in case they are still unknown         */
/* This is needed as older callers may still use and interface that does not hold these fields although they are mandatory */
/* This is about these fields: tDInvoice.DInvoiceIsWithDeduction                                                           */
/* ======================================================================================================================= */
for each t_sDInvoice where 
         t_sDInvoice.tc_Status = "N":U or 
         t_sDInvoice.tc_Status = "C":U : 
    if t_sDInvoice.DInvoiceIsWithDeduction = ? 
    then assign t_sDInvoice.DInvoiceIsWithDeduction = false.
end. /* for each t_sQCrossCyPosting where */

/* assign t_sDInvoice.Period_ID */
for each t_sDInvoice where 
         t_sDInvoice.tc_Status <> "D":U :                         
    
    if tc_status = "C":U 
    then do:
        find first t_iDInvoice where
             t_iDInvoice.tc_Rowid = t_sDInvoice.tc_Rowid 
             no-lock no-error.
         if not available t_iDinvoice
         then do:
             assign 
                vcMessage =       #T-71'The system cannot find the original customer invoice record.':100(65734)T-71#
               oiReturnStatus = -1.
               <M-72 run SetMessage
                  (input  vcMessageText (icMessage), 
                   input  '':U (icArguments), 
                   input  '' (icFieldName), 
                   input  '' (icFieldValue), 
                   input  'E':U (icType), 
                   input  3 (iiSeverity), 
                   input  '' (icRowid), 
                   input  'QadFin-7435':U (icFcMsgNumber), 
                   input  '' (icFcExplanation), 
                   input  '' (icFcIdentification), 
                   input  '' (icFcContext), 
                   output viFcReturnSuper (oiReturnStatus)) in BDInvoice>         
         end.   
         
                
         /* Check to see if the invoice exists on a payment selection - this flag will only be set for an invoice that has not
         been paid */
         if t_sDInvoice.DInvoiceIsSelected and vcActivityCode  <> "ModifyDueDate":U
         then do:             
              <Q-92 run DPaySelLineByParentObjectID (all) (Read) (NoCache)
                 (input t_sDInvoice.Dinvoice_ID, (ParentObjectID)
                  input {&PAYMENTSELECTIONTYPE-INVOICE}, (ObjectType)
                  input t_sDInvoice.Company_ID, (CompanyId)
                  input 'CANCELLED', (LineStatus)
                  output dataset tqDPaySelLineByParentObjectID) in BDPaymentSelection>
              
              for first tqDPaySelLineByParentObjectID 
                  where tqDPaySelLineByParentObjectID.tiDPaySelLineParentObject_ID = t_sDInvoice.Dinvoice_ID
                    and tqDPaySelLineByParentObjectID.tcDPaySelLineObjectType = {&PAYMENTSELECTIONTYPE-INVOICE}:
                  assign vcPaySelCode =  tqDPaySelLineByParentObjectID.tcDPaySelCode.     
              end.
              
              assign 
               vcMessageText =    #T-84'Invoice $2/$3/$4 cannot be modified - exists on Payment Selection $1':255(286148885)T-84#
               oiReturnStatus = -1.
                
              <M-40 run SetMessage
                 (input  vcMessageText (icMessage), 
                  input  vcPaySelCode + chr(2) + string(t_sDINvoice.DInvoicePostingYear) + chr(2) + t_sDInvoice.tcjournalCode + chr(2) + string(t_sDInvoice.DInvoicevoucher,'999999999') (icArguments), 
                  input  '' (icFieldName), 
                  input  '' (icFieldValue), 
                  input  'E' (icType), 
                  input  3 (iiSeverity), 
                  input  t_sDInvoice.tc_Rowid (icRowid), 
                  input  'qadfin-702736':U (icFcMsgNumber), 
                  input  '' (icFcExplanation), 
                  input  '' (icFcIdentification), 
                  input  '' (icFcContext), 
                  output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
              return.
         end.   /* end of DInvoiceIsSelected */  
         
         /* Check to see if any stages being modified are on a payment selection */    
         for each t_sDInvoiceStage 
            where t_sDInvoiceStage.tc_parentrowid = t_sDInvoice.tc_rowid
              and t_sDInvoiceStage.tc_status = "C"   
              and t_sDInvoiceStage.DInvoiceStageIsSelected and vcActivityCode  <> "ModifyDueDate":U:
              
              <Q-60 run DPaySelLineStageByDInvoiceStage (all) (Read) (NoCache)
                 (input t_sDInvoice.Company_ID, (CompanyId)
                  input t_sDInvoiceStage.DInvoiceStage_ID, (DInvoiceStageID)
                  input 'CANCELLED', (LineStageStatus)
                  output dataset tqDPaySelLineStageByDInvoiceStg) in BDPaymentSelection>
                  
              for first tqDPaySelLineStageByDInvoiceStg
                  where tqDPaySelLineStageByDInvoiceStg.tiDInvoiceStage_ID = t_sDInvoiceStage.DInvoiceStage_ID:
                  assign vcPaySelCode = tqDPaySelLineStageByDInvoiceStg.tcDPaySelCode.
              end.   
              
              assign 
                vcMessageText =  #T-41'Stage with due date $5 on Invoice $2/$3/$4 cannot be modified - exists on Payment Selection $1':255(579042565)T-41#
                oiReturnStatus = -1.
                
              <M-37 run SetMessage
                 (input  vcMessageText (icMessage), 
                  input  vcPaySelCode + chr(2) + string(t_sDINvoice.DInvoicePostingYear) + chr(2) + t_sDInvoice.tcjournalCode + chr(2) + string(t_sDInvoice.DInvoicevoucher,'999999999') + chr(2) + string(t_sDInvoiceStage.DInvoiceStageDueDate) (icArguments), 
                  input  '' (icFieldName), 
                  input  '' (icFieldValue), 
                  input  'E' (icType), 
                  input  3 (iiSeverity), 
                  input  t_sDInvoiceStage.tc_Rowid (icRowid), 
                  input  'qadfin-358952':U (icFcMsgNumber), 
                  input  '' (icFcExplanation), 
                  input  '' (icFcIdentification), 
                  input  '' (icFcContext), 
                  output viFcReturnSuper (oiReturnStatus)) in BDInvoice>              
         end.      
         
         if oiReturnStatus < 0
         then return.
    end.
         
     /* Assign Debtor_ID */
    if t_sDInvoice.tc_Status       = "N":U or
       t_sDInvoice.tc_Status       = "C":U and
       t_sDInvoice.tcDebtorCode <> t_iDInvoice.tcDebtorCode
    then do:
        if t_sDInvoice.tcDebtorCode = "":U or
           t_sDInvoice.tcDebtorCode = ?
        then assign t_sDInvoice.Debtor_ID = 0.
        else do:
            if not vlStartDebtorPrim
            then do:
               <Q-68 run DebtorPrim  (Start) in BDebtor > 
                assign vlStartDebtorPrim = true.
            end.
            <Q-69 run DebtorPrim (all) (Read) (NoCache)
               (input t_sDinvoice.Company_ID, (CompanyId)
                input t_sDinvoice.tcDebtorCode, (DebtorCode)
                input ?, (Debtor_ID)
                output dataset tqDebtorPrim) in BDebtor > 
           
            find first tqDebtorPrim where
                       tqDebtorPrim.tcDebtorCode = t_sDInvoice.tcDebtorCode
                       no-error.

            assign t_sDInvoice.Debtor_ID = if available tqDebtorPrim
                                             then tqDebtorPrim.tiDebtor_ID
                                             else 0.
        end.
    end.     
         
    if t_sDInvoice.tc_Status = 'N':U or
      (available t_iDInvoice and
       (t_iDInvoice.DInvoicePostingYear   <> t_sDInvoice.DInvoicePostingYear  or
        t_iDInvoice.DInvoicePostingPeriod <> t_sDInvoice.DInvoicePostingPeriod  ))
    then do:
        if vlStart1 = false
        then do:
            <Q-14 run PeriodPrim  (Start) in BPeriod >
            assign vlStart1 = true.
        end.
        <Q-1 run PeriodPrim (all) (Read) (Cache)
           (input t_sDInvoice.Company_ID, (CompanyId)
            input ?, (PeriodId)
            input t_sDInvoice.DInvoicePostingYear, (PeriodYear)
            input t_sDInvoice.DInvoicePostingPeriod, (PeriodPeriod)
            output dataset tqPeriodPrim) in BPeriod >
        find tqPeriodPrim where
             tqPeriodPrim.tiPeriodYear   = t_sDInvoice.DInvoicePostingYear   and
             tqPeriodPrim.tiPeriodPeriod = t_sDInvoice.DInvoicePostingPeriod and
             tqPeriodPrim.tiCompany_Id   = t_sDInvoice.Company_ID
             no-lock no-error.
        if available tqPeriodPrim
        Then assign t_sDInvoice.Period_ID                 = tqPeriodPrim.tiPeriod_ID
                    t_sDInvoice.DInvoicePostingYearPeriod = tqPeriodPrim.tiPeriodYear * 100 + tqPeriodPrim.tiPeriodPeriod.
    end. /* if t_sDInvoice.tc_Status = 'N':U or */

    /* Delete the DInvoiceStage lines in case the PC type is not staged */
    if t_sDInvoice.tcNormalPaymentConditionCode <> ? and
       t_sDInvoice.tcNormalPaymentConditionCode <> "":U and
       t_sDInvoice.tcNormalPaymentConditionType = "":U and
       t_sDinvoice.tcNormalPaymentConditionType <> ? 
    then do:
        if vlStart2 = false
        then do:
            <Q-17 run PaymentConditionByIdCode (Start) in BPaymentCondition >
            assign vlStart2 = true.
        end.
        <Q-9 run PaymentConditionByIdCode (all) (Read) (NoCache)
          (input ?, (PaymentConditionId)
           input t_sDInvoice.tcNormalPaymentConditionCode, (PaymentConditionCode)
           output dataset tqPaymentConditionByIdCode) in BPaymentCondition >
        find first tqPaymentConditionByIdCode no-error.
        if available tqPaymentConditionByIdCode
        then assign t_sDInvoice.tcNormalPaymentConditionType = tqPaymentConditionByIdCode.tcPaymentConditionPaymentTyp.
    end.

    if t_sDInvoice.tcNormalPaymentConditionCode = ?    or
       t_sDInvoice.tcNormalPaymentConditionCode = "":U or
       t_sDInvoice.tcNormalPaymentConditionType <> {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
    then do:
        for each t_sDInvoiceStage where 
                 t_sDInvoiceStage.tc_ParentRowid = t_sDInvoice.tc_Rowid:
            assign t_sDInvoiceStage.tc_Status = "D":U.
        end.
    end.
    else do:        
        /* Set the duedate of the invoice */
        assign vtDueDate = 12/12/9999.
        for each t_sDInvoiceStage where 
                 t_sDInvoiceStage.tc_ParentRowid = t_sDInvoice.tc_Rowid and
                 t_sDInvoiceStage.tc_Status <> "D":U:
            if t_sDInvoiceStage.DInvoiceStageDueDate <> ?
            then assign vtDueDate = minimum(vtDueDate, t_sDInvoiceStage.DInvoiceStageDueDate).
        end.
        if t_sDInvoice.DInvoiceType    <> {&INVOICETYPE-PREPAYMENT} and
           t_sDInvoice.DInvoiceType    <> {&INVOICETYPE-DEDUCTION} and /* FOR DEDUCTION, WE DO NOT NEED TO CREATE STAGE RECORD.*/
           t_sDInvoice.DInvoiceDueDate <> vtDueDate
        then assign t_sDInvoice.DInvoiceDueDate = vtDueDate
                    t_sDInvoice.tc_status = if t_sDInvoice.tc_status = "":U 
                                            then "C":U 
                                            else t_sDInvoice.tc_status.
    end.

    /* ============================================================== */
    /* give some db fields the right value based on calculated fields */
    /* ============================================================== */
    if t_sDInvoice.tc_Status = "C":U or
       t_sDInvoice.tc_Status = "N":U 
    then do:
       
        if t_sDInvoice.DinvoiceType = {&INVOICETYPE-INVOICE} or
           t_sDInvoice.DinvoiceType = {&INVOICETYPE-FINANCECHARGE} or
           t_sDInvoice.DinvoiceType = {&INVOICETYPE-CREDITNOTE} or
           t_sDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or
           t_sDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
        then do:
            if t_sDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or t_sDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE} or t_sDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
            then do:
                if t_sDInvoice.DInvoiceIsTaxExcluded = false
                then assign t_sDInvoice.DInvoiceOriginalCreditTC = 0
                            t_sDInvoice.DInvoiceOriginalDebitTC  = t_sDInvoice.tdDInvoiceOriginalTC
                            t_sDInvoice.DInvoiceOriginalCreditLC = 0
                            t_sDInvoice.DInvoiceOriginalDebitLC  = t_sDInvoice.tdDInvoiceOriginalLC
                            /* base amounts will be filled in ValidateComponentPreValidateVat */
                            t_sDInvoice.DInvoiceVatBaseCreditTC  = 0
                            t_sDInvoice.DInvoiceVatBaseDebitTC   = 0
                            t_sDInvoice.DInvoiceVatBaseCreditLC  = 0
                            t_sDInvoice.DInvoiceVatBaseDebitLC   = 0.
                else assign t_sDInvoice.DInvoiceVatBaseCreditTC  = t_sDInvoice.tdDInvoiceOriginalTC
                            t_sDInvoice.DInvoiceVatBaseDebitTC   = 0
                            t_sDInvoice.DInvoiceVatBaseCreditLC  = t_sDInvoice.tdDInvoiceOriginalLC
                            t_sDInvoice.DInvoiceVatBaseDebitLC   = 0.
                            /* original amounts will be filled in ValidateComponentPreValidateVat */
                            /*t_sDInvoice.DInvoiceOriginalCreditTC = 0
                            t_sDInvoice.DInvoiceOriginalDebitTC  = 0
                            t_sDInvoice.DInvoiceOriginalCreditLC = 0
                            t_sDInvoice.DInvoiceOriginalDebitLC  = 0.*/
            end.
            else do:
                if t_sDInvoice.DInvoiceIsTaxExcluded = false
                then assign t_sDInvoice.DInvoiceOriginalCreditTC = t_sDInvoice.tdDInvoiceOriginalTC
                            t_sDInvoice.DInvoiceOriginalDebitTC  = 0
                            t_sDInvoice.DInvoiceOriginalCreditLC = t_sDInvoice.tdDInvoiceOriginalLC
                            t_sDInvoice.DInvoiceOriginalDebitLC  = 0
                            /* base amounts will be filled in ValidateComponentPreValidateVat */
                            t_sDInvoice.DInvoiceVatBaseCreditTC  = 0
                            t_sDInvoice.DInvoiceVatBaseDebitTC   = 0
                            t_sDInvoice.DInvoiceVatBaseCreditLC  = 0
                            t_sDInvoice.DInvoiceVatBaseDebitLC   = 0.
                else assign t_sDInvoice.DInvoiceVatBaseCreditTC  = 0
                            t_sDInvoice.DInvoiceVatBaseDebitTC   = t_sDInvoice.tdDInvoiceOriginalTC
                            t_sDInvoice.DInvoiceVatBaseCreditLC  = 0
                            t_sDInvoice.DInvoiceVatBaseDebitLC   = t_sDInvoice.tdDInvoiceOriginalLC.
                            /* original amounts will be filled in ValidateComponentPreValidateVat */
                            /*t_sDInvoice.DInvoiceOriginalCreditTC = 0
                            t_sDInvoice.DInvoiceOriginalDebitTC  = 0
                            t_sDInvoice.DInvoiceOriginalCreditLC = 0
                            t_sDInvoice.DInvoiceOriginalDebitLC  = 0.*/
            end.

            if not(t_sDInvoice.DInvoiceIsExternal)
            then do:
                assign t_sDInvoice.DInvoiceDescription      = if t_sDInvoice.DInvoiceDescription = "":U and
                                                             t_sDInvoice.tcBusinessRelationName1 <> "":U
                                                          then t_sDInvoice.tcBusinessRelationName1
                                                          else t_sDInvoice.DInvoiceDescription.
            end.

            /* update cc amounts and rate */
            if viCompanyCCId                        <> 0 and viCompanyCCId                        <> ? and
               t_sDInvoice.DInvoiceDate             <> ?                                               and 
               t_sDInvoice.tc_Status                = 'N':U                                            and
               (((t_sDInvoice.DInvoiceOriginalDebitCC  = 0 or t_sDInvoice.DInvoiceOriginalDebitCC  = ?) and
                 (t_sDInvoice.DInvoiceOriginalCreditCC = 0 or t_sDInvoice.DInvoiceOriginalCreditCC = ?)) or
                (t_sDInvoice.DInvoiceCCRate           = 0 or t_sDInvoice.DInvoiceCCRate           = ?)   or
                (t_sDInvoice.DInvoiceCCScale          = 0 or t_sDInvoice.DInvoiceCCScale          = ?))
            then do:

                if t_sDInvoice.tcCurrencyCode = vcCompanyCC
                then assign t_sDInvoice.DInvoiceOriginalDebitCC  = t_sDInvoice.DInvoiceOriginalDebitTC
                            t_sDInvoice.DInvoiceOriginalCreditCC = t_sDInvoice.DInvoiceOriginalCreditTC
                            t_sDInvoice.DInvoiceCCRate           = 1
                            t_sDInvoice.DInvoiceCCScale          = 1.
                else if vlDomainIsStatutory = false
                then assign t_sDInvoice.DInvoiceOriginalDebitCC  = t_sDInvoice.DInvoiceOriginalDebitLC
                            t_sDInvoice.DInvoiceOriginalCreditCC = t_sDInvoice.DInvoiceOriginalCreditLC
                            t_sDInvoice.DInvoiceCCRate           = t_sDInvoice.DInvoiceExchangeRate
                            t_sDInvoice.DInvoiceCCScale          = t_sDInvoice.DInvoiceRateScale.
                else do:                                        
                    <M-12 run GetExRateByEntityInvoiceFlag
                       (input  t_sDInvoice.DInvoicePostingDate (itDInvoicePostingDate), 
                        input  t_sDInvoice.DInvoiceTaxPointDate (itDInvoiceTaxPointDate), 
                        input  0 (iiFromCurrencyId), 
                        input  t_sDInvoice.tcCurrencyCode (icFromCurrencyCode), 
                        input  viCompanyCCId (iiToCurrencyId), 
                        input  vcCompanyCC (icToCurrencyCode), 
                        input  0 (iiExchangeRateTypeId), 
                        input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                        output vdDInvoiceExchangeRate (odDInvoiceExchangeRate), 
                        output vdDInvoiceExchangeRateScale (odDInvoiceRateScale), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>                  
                    
                    if viFcReturnSuper < 0 or 
                      (viFcReturnSuper > 0 and 
                       oiReturnStatus = 0)
                    then assign oiReturnStatus = viFcReturnSuper. 
                           
                  /* value of vdDInvoiceExchangeRate is set to the value entered by the user while coming from customer opening balance create */
                  /* APIGetExchangeRate gets the exchange rate default exchange rate and not the rate entered by the user */
                    if (vdDInvoiceExchangeRate <> 0 and vdDInvoiceExchangeRate <> ?)         and
                       (t_sDInvoice.DInvoiceCCRate <> 0 and t_sDInvoice.DInvoiceCCRate <> ?) and
                        vdDInvoiceExchangeRate <> t_sDInvoice.DInvoiceCCRate 
                    then assign vdDInvoiceExchangeRate = t_sDInvoice.DInvoiceCCRate.
                    
                    if (t_sDInvoice.DInvoiceCCRate = 0 or t_sDInvoice.DInvoiceCCRate = ?) and
                       (t_sDInvoice.DInvoiceOriginalDebitCC <> 0 and t_sDInvoice.DInvoiceOriginalDebitCC <> ?)
                    then assign vdDInvoiceExchangeRate  = absolute(round((t_sDInvoice.DInvoiceOriginalDebitCC - 
                                t_sDInvoice.DInvoiceOriginalCreditCC) / (t_sDInvoice.DInvoiceOriginalDebitTC - 
                                t_sDInvoice.DInvoiceOriginalCreditTC), 10)).    
                                 
                    <M-25 run CheckExternalInvoiceCC
                       (input  t_sDInvoice.tc_Rowid (icInvoiceRowid), 
                        input-output vdDInvoiceExchangeRate (bdExchangeRate), 
                        input-output vdDInvoiceExchangeRateScale (bdRateScale), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                        
                    if viFcReturnSuper < 0 or 
                      (viFcReturnSuper > 0 and 
                       oiReturnStatus = 0)
                    then assign oiReturnStatus = viFcReturnSuper. 
                    
                    
                    
                    assign t_sDInvoice.DInvoiceOriginalDebitCC  = <M-55 RoundAmount
                                                                     (input  t_sDInvoice.DInvoiceOriginalDebitTC * vdDInvoiceExchangeRate * vdDInvoiceExchangeRateScale (idUnroundedAmount), 
                                                                      input  viCompanyCCId (iiCurrencyID), 
                                                                      input  vcCompanyCC (icCurrencyCode)) in business>
                           t_sDInvoice.DInvoiceOriginalCreditCC = <M-56 RoundAmount
                                                                     (input  t_sDInvoice.DInvoiceOriginalCreditTC * vdDInvoiceExchangeRate * vdDInvoiceExchangeRateScale (idUnroundedAmount), 
                                                                      input  viCompanyCCId (iiCurrencyID), 
                                                                      input  vcCompanyCC (icCurrencyCode)) in business>
                           t_sDInvoice.DInvoiceCCRate           = vdDInvoiceExchangeRate
                           t_sDInvoice.DInvoiceCCScale          = vdDInvoiceExchangeRateScale.
                end.
            end.
        end. /* end = {&INVOICETYPE-INVOICE} or {&INVOICETYPE-CREDITNOTE} */
        
    /* ================================================== */
    /* Fill in the CC and LC amounts on the Stage-records */
    /* ================================================== */
    for each t_sDInvoiceStage where 
             t_sDInvoiceStage.tc_ParentRowid = t_sDInvoice.tc_Rowid and 
             (t_sDInvoiceStage.tc_Status = "N":U or
              t_sDInvoiceStage.tc_Status = "C":U) :
                                                   
        if t_sDInvoice.tcCurrencyCode = vcCompanyLC
        then assign t_sDInvoiceStage.DInvoiceStageAmountLC = t_sDInvoiceStage.DInvoiceStageAmountTC.
        else do:
            assign vhFcComponent = ?.
            
            <M-8 run GetExRateByEntityInvoiceFlag
               (input  t_sDInvoice.DInvoicePostingDate (itDInvoicePostingDate), 
                input  t_sDInvoice.DInvoiceTaxPointDate (itDInvoiceTaxPointDate), 
                input  ? (iiFromCurrencyId), 
                input  t_sDInvoice.tcCurrencyCode (icFromCurrencyCode), 
                input  viCompanyLCId (iiToCurrencyId), 
                input  vcCompanyCode (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeId), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                output vdExchangeRate (odDInvoiceExchangeRate), 
                output vdExchangeRateScale (odDInvoiceRateScale), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>                  
            
            if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then Leave.
            assign t_sDInvoiceStage.DInvoiceStageAmountLC 
                = <M-74 RoundAmount
          (input  t_sDinvoiceStage.DinvoiceStageAmountT * vdExchangeRate * vdExchangeRateScale (idUnroundedAmount), 
           input  viCompanyLCID (iiCurrencyID), 
           input  vcCompanyLC (icCurrencyCode)) in business>.
               
        end. /* if t_sDInvoice.tcCurrencyCode = vcCompanyLC */
        if vcCompanyCC = t_sDInvoice.tcCurrencyCode
        then assign t_sDInvoiceStage.DInvoiceStageAmountCC = t_sDInvoiceStage.DInvoiceStageAmountTC.
        else do:
            if vlDomainIsStatutory = false
            then assign t_sDInvoiceStage.DInvoiceStageAmountCC = t_sDInvoiceStage.DInvoiceStageAmountLC.
            else do:
                assign vhFcComponent = ?.
                
                <M-86 run GetExRateByEntityInvoiceFlag
                   (input  t_sDInvoice.DInvoicePostingDate (itDInvoicePostingDate), 
                    input  t_sDInvoice.DInvoiceTaxPointDate (itDInvoiceTaxPointDate), 
                    input  ? (iiFromCurrencyId), 
                    input  t_sDInvoice.tcCurrencyCode (icFromCurrencyCode), 
                    input  viCompanyCCId (iiToCurrencyId), 
                    input  vcCompanyCC (icToCurrencyCode), 
                    input  ? (iiExchangeRateTypeId), 
                    input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                    output vdExchangeRate (odDInvoiceExchangeRate), 
                    output vdExchangeRateScale (odDInvoiceRateScale), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>    
                                        
                if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Leave.
                
                <M-19 run CheckExternalInvoiceCC
                   (input  t_sDInvoiceStage.tc_ParentRowid (icInvoiceRowid), 
                    input-output vdDInvoiceExchangeRate (bdExchangeRate), 
                    input-output vdDInvoiceExchangeRateScale (bdRateScale), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                    
                if viFcReturnSuper < 0 or 
                  (viFcReturnSuper > 0 and 
                   oiReturnStatus = 0)
                then assign oiReturnStatus = viFcReturnSuper. 
                
                assign t_sDInvoiceStage.DInvoiceStageAmountCC 
                    = <M-76 RoundAmount
                         (input  t_sDinvoiceStage.DinvoiceStageAmountTC * vdExchangeRate * vdExchangeRateScale (idUnroundedAmount), 
                          input  viCompanyCCID (iiCurrencyID), 
                          input  vcCompanyCC (icCurrencyCode)) in business>.                   
            end. /* Not if vcCompanyCC = vcCompanyLC */
        end. /* Not if vcCompanyCC = t_sDInvoice.tcCurrencyCode */
    end.         

    end. /* tc_status = "N" or "C" */

    if can-find (first t_sDInvoiceVat where
                       t_sDInvoiceVat.tc_ParentRowid = t_sDInvoice.tc_Rowid and
                       t_sDInvoiceVat.tc_Status <> "D":U )
    then do:
        <M-24 run ValidateComponentPreValidateVat
           (input-output vlStartCurrencyPrim (blStartCurrencyPrim), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
        then assign oiReturnStatus = viFcReturnSuper. 
    end.

    /* must after validatecomponentprevalidatevat so that the original values are already correctly filled */
    /*TPM: since the deduction invoice will be created in the customer payment and reduced at the creation, it
    should not run this assignment. */
    if t_sDInvoice.tc_Status = "N":U and t_sDInvoice.DInvoiceType <> {&INVOICETYPE-DEDUCTION}
    then /* set the balance fields equal to the original fields, necessary for tax excluded flag */
         /*if t_sDInvoice.DInvoiceIsTaxExcluded
         then */assign t_sDInvoice.DInvoiceBalanceDebitTC  = t_sDInvoice.DInvoiceOriginalDebitTC
                     t_sDInvoice.DInvoiceBalanceCreditTC = t_sDInvoice.DInvoiceOriginalCreditTC
                     t_sDInvoice.DInvoiceBalanceDebitLC  = t_sDInvoice.DInvoiceOriginalDebitLC
                     t_sDInvoice.DInvoiceBalanceCreditLC = t_sDInvoice.DInvoiceOriginalCreditLC
                     t_sDInvoice.DInvoiceBalanceDebitCC  = t_sDInvoice.DInvoiceOriginalDebitCC
                     t_sDInvoice.DInvoiceBalanceCreditCC = t_sDInvoice.DInvoiceOriginalCreditCC
                     t_sDInvoice.DInvoiceBalanceTC       = t_sDInvoice.DInvoiceBalanceDebitTC - t_sDInvoice.DInvoiceBalanceCreditTC
                     t_sDInvoice.DInvoiceBalanceLC       = t_sDInvoice.DInvoiceBalanceDebitLC - t_sDInvoice.DInvoiceBalanceCreditLC
                     t_sDInvoice.DInvoiceBalanceCC       = t_sDInvoice.DInvoiceBalanceDebitCC - t_sDInvoice.DInvoiceBalanceCreditCC
                     t_sDInvoice.DInvoiceIsOpen          = t_sDInvoice.DInvoiceBalanceTC <> 0 or
                                                           t_sDInvoice.DInvoiceBalanceLC <> 0 or
                                                           t_sDInvoice.DInvoiceBalanceCC <> 0.

    /*CA676097*/   
    if t_sDInvoice.tc_Status = "N":U or
       t_sDInvoice.tc_Status = "C":U 
    then do:
                    
        if t_sDInvoice.tcCurrencyCode = vcCompanyLC
        then assign t_sDInvoice.DInvoiceNonDiscAmtLC = t_sDInvoice.DInvoiceNonDiscAmtTC.
        else assign t_sDInvoice.DInvoiceNonDiscAmtLC = <M-79 RoundAmount
                                                         (input  t_sDInvoice.DInvoiceNonDiscAmtTC * t_sDInvoice.DInvoiceExchangeRate * t_sDInvoice.DInvoiceRateScale (idUnroundedAmount), 
                                                          input  viCompanyLCId (iiCurrencyID), 
                                                          input  vcCompanyLC (icCurrencyCode)) in BDInvoice>.
                                                          
        if t_sDInvoice.tcCurrencyCode = vcCompanyCC
        then assign t_sDInvoice.DInvoiceNonDiscAmtCC = t_sDInvoice.DInvoiceNonDiscAmtTC.
        else if vlDomainIsStatutory = false
        then assign t_sDInvoice.DInvoiceNonDiscAmtCC = t_sDInvoice.DInvoiceNonDiscAmtLC.
        else if t_sDInvoice.DInvoiceCCRate <> 0 and t_sDInvoice.DInvoiceCCScale <> 0
        then assign t_sDInvoice.DInvoiceNonDiscAmtCC = <M-33 RoundAmount
                                                          (input  t_sDInvoice.DinvoiceNonDiscAmtTC * t_sDInvoice.DInvoiceCCRate * t_sDInvoice.DInvoiceCCScale (idUnroundedAmount), 
                                                           input  viCompanyCCId (iiCurrencyID), 
                                                           input  vcCompanyCC (icCurrencyCode)) in BDInvoice>. 
        else do: 
            assign vhFcComponent = ?. 
            <M-472 run GetExRateByEntityInvoiceFlag
               (input  t_sDInvoice.DInvoicePostingDate (itDInvoicePostingDate), 
                input  t_sDInvoice.DInvoiceTaxPointDate (itDInvoiceTaxPointDate), 
                input  ? (iiFromCurrencyId), 
                input  t_sDInvoice.tcCurrencyCode (icFromCurrencyCode), 
                input  ? (iiToCurrencyId), 
                input  vcCompanyCC (icToCurrencyCode), 
                input  0 (iiExchangeRateTypeId), 
                input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                output vdDInvoiceExchangeRate (odDInvoiceExchangeRate), 
                output vdDInvoiceExchangeRateScale (odDInvoiceRateScale), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>    
                         
            if viFcReturnSuper < 0 or 
              (viFcReturnSuper > 0 and 
               oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.  
            
            <M-96 run CheckExternalInvoiceCC
               (input  t_sDInvoice.tc_Rowid (icInvoiceRowid), 
                input-output vdDInvoiceExchangeRate (bdExchangeRate), 
                input-output vdDInvoiceExchangeRateScale (bdRateScale), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                
            if viFcReturnSuper < 0 or 
              (viFcReturnSuper > 0 and 
               oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper. 
            
            assign t_sDInvoice.DInvoiceNonDiscAmtCC = <M-81 RoundAmount
                                                         (input  t_sDInvoice.DinvoiceNonDiscAmtTC * vdDInvoiceExchangeRate * vdDInvoiceExchangeRateScale (idUnroundedAmount), 
                                                          input  viCompanyCCId (iiCurrencyID), 
                                                          input  vcCompanyCC (icCurrencyCode)) in BDInvoice>.                    
        end. /* else do: */           
    end. /* if t_sDInvoice.tc_Status = "N":U or */
    /*CA676097*/                                                                                 
     
    if t_sDInvoice.tc_Status = 'N':U  Or
       t_sDInvoice.tc_Status = 'C':U
    then do:
        
        assign
            t_sDInvoice.DInvoiceIsWithBank = 
                can-find(first t_sDInvoiceBank where
                    t_sDInvoiceBank.tc_ParentRowId = t_sDInvoice.tc_RowID and
                    t_sDInvoiceBank.tc_Status <> 'D':U).
    End.

    <M-51 run ValidateComponentPreValidateBank
       (input-output vlStartPayFormatTypeByType (blStartPayFormatTypeByType), 
        input-output vlStartBankNumberPrim (blStartBankNumberPrim), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
    then assign oiReturnStatus = viFcReturnSuper.
end. /* for each */

if vlStart1 = true
then do:
     <Q-15 run PeriodPrim  (Stop) in BPeriod >
end.

if vlStart2 = true
then do:
    <Q-16 run PaymentConditionByIdCode (Stop) in BPaymentCondition >
end.

if vlStartDebtorPrim
then do:
   <Q-70 run DebtorPrim  (Stop) in BDebtor > 
end.

if vlStartBankNumberPrim
then do:
    <Q-58 run BankNumberPrim  (Stop) in BBankNumber >
end.

if vlStartPayFormatTypeByType
then do:
    <Q-77 run PayFormatTypeByType  (Stop) in BPaymentFormat >
end.
if vlStartCurrencyPrim
then do:
    <Q-78 run CurrencyPrim  (Stop) in BCurrency >
end.