project QadFinancials > class BCInvoice > method ValidateComponentPre

Description

This method includes validations that are done before the ancestor code of ValidateComponent.
Give some fields of the credittor invoice and his sub-tables the right value.


Parameters


blStartVal1input-outputlogicalIs query tqCreditorForCInvoiceVal already started or not ?
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ValidateComponent


program code (program5/bcinvoice.p)

assign vlStart1 = false
       vlStart2 = false
       vlStart3 = false
       vlStart4 = false.

empty temp-table tApproveWarningInvLst.

for each t_sCInvoice where
         t_sCInvoice.tc_Status <> "D":U:
         
    <Q-31 run ReasonByIdCode (all) (Read) (Cache)
       (input ?, (ReasonId)
        input t_sCInvoice.tcReasonCode, (ReasonCode)
        output dataset tqReasonByIdCode) in BReason>
    
    find first tqReasonByIdCode where tqReasonByIdCode.tcReasonCode = t_sCInvoice.tcReasonCode no-error.        
        if available tqReasonByIdCode
        then do:
            if  (   vcActivityCode = "Approve":U  and  tqReasonByIdCode.tlReasonIsInvoiceApproved = no )  
            then do: 
                    assign oiReturnStatus = 1.
                    <M-62 run SetMessage
                       (input  trim(#T-66'Invoice will be saved with a Status Code that hasn't the Approve flag set.':255(315466208)T-66#) (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'W':U (icType), 
                        input  3 (iiSeverity), 
                        input  '':U (icRowid), 
                        input  'qadfin-672165':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice> 
                        
                        create tApproveWarningInvLst.
                        assign tApproveWarningInvLst.tiInvoiceId = t_sCInvoice.CInvoice_ID.
                        
              end.
            if( vcActivityCode = "Release For Payment":U  and tqReasonByIdCode.tlReasonIsLockPayment = yes)
            then do: 
                assign oiReturnStatus = 1.
                <M-38 run SetMessage
                   (input  trim(#T-44'Invoice will be saved with a Status Code that has the Lock Payment flag set.':255(677540431)T-44#) (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'W':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-648465':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice> 
              end.
        /* Reset CInvoiceCIRECClosingDate to 12/31/9999 when modify a supplier invoice from transient allocation to no allocation status */
        if  t_sCInvoice.tc_Status = "C":U and tqReasonByIdCode.tcReasonAllocationStatus = {&ALLOCSTATUS-NOALLOC} and t_sCInvoice.CInvoiceCirecClosingDate <> 12/31/9999
        then do:
            find first t_iCInvoice where t_iCInvoice.CInvoice_ID = t_sCInvoice.CInvoice_ID no-error.
            if available t_iCInvoice
            then do:
                <Q-828 run ReasonByIdCode (all) (Read) (Cache)
                   (input t_iCInvoice.Reason_ID, (ReasonId)
                    input t_iCInvoice.tcReasonCode, (ReasonCode)
                    output dataset tqReasonByIdCode) in BReason>
                find first tqReasonByIdCode no-error.
                if available tqReasonByIdCode
                then do:
                    if tqReasonByIdCode.tcReasonAllocationStatus = {&ALLOCSTATUS-TRANSALLOC}
                    then assign t_sCInvoice.CInvoiceCirecClosingDate = 12/31/9999.    
                end.
            end. /* if available t_iCInvoice */ 
        end. /* if  t_sCInvoice.tc_Status = "C":U */                      
    end.  /* if available tqReasonByIdCode */  

    if t_sCInvoice.tc_Status <> "N":U
    then do:
        find t_iCInvoice where
             t_iCInvoice.tc_Rowid = t_sCInvoice.tc_Rowid
             no-error.
        if not available t_iCInvoice
        then do:
            <M-1 run SetMessage
               (input  trim(#T-53'Could not find initial values for supplier invoice $1 $2 $3.':255(999890511)T-53#) (icMessage), 
                input  string(t_sCInvoice.CInvoicePostingYear) + chr(2) + t_sCInvoice.tcJournalCode + chr(2) + string(t_sCInvoice.CInvoiceVoucher) (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  t_sCInvoice.tc_Rowid (icRowid), 
                input  'QadFin-7078':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            assign oiReturnStatus = -1.
            next.
        end. /* if not available t_iCInvoice */
        <M-59 run ValidateComponentPreCI  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0 and 
           oiReturnStatus  >= 0 
        then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 
        then next.
    end. /* if t_sCInvoice.tc_Status <> "N":U */

    assign vdCInvoiceExchangeRate = ?
           vdCInvoiceRateScale    = ?.
           
    if (t_sCInvoice.CInvoiceCCRate <> 0 and t_sCInvoice.CInvoiceCCScale <> ?)
    then do:
        assign vdCInvoiceExchangeRate = t_sCInvoice.CInvoiceCCRate
               vdCInvoiceRateScale    = t_sCInvoice.CInvoiceCCScale.
    end.
    
  
    if t_sCInvoice.CInvoiceIsInitial    = false and
     ((t_sCinvoice.CInvoiceVoucher     <> 0 and
       t_sCinvoice.CInvoiceVoucher     <> ? and
       t_sCInvoice.tc_Status            = "N":U) or 
      (t_sCInvoice.tc_Status            = "C":U and
      (t_sCInvoice.CInvoiceVoucher     <> t_iCInvoice.CInvoiceVoucher or
       t_sCInvoice.Company_ID          <> t_iCInvoice.Company_ID or
       t_sCInvoice.CInvoicePostingYear <> t_iCInvoice.CInvoicePostingYear or
       t_sCInvoice.tcJournalCode       <> t_iCInvoice.tcJournalCode)))
    then do:
        /* check that there's only one record with these values */
        /* first check the instance */
        find first bCInvoice where
                   bCInvoice.Company_ID          = t_sCInvoice.Company_ID          and
                   bCInvoice.CInvoicePostingYear = t_sCInvoice.CInvoicePostingYear and
                   bCInvoice.CInvoiceVoucher     = t_sCInvoice.CInvoiceVoucher     and
                   bCInvoice.tcJournalCode       = t_sCInvoice.tcJournalCode       and
                   bCInvoice.tc_Rowid           <> t_sCInvoice.tc_Rowid
                   no-lock no-error.
        if available bCInvoice
        then do:
            assign oiReturnStatus = -1.
            <M-40 run SetMessage
               (input  #T-54'Duplicate Invoice $1 $2 $3':50(69153)T-54# (icMessage), 
                input  string(t_sCInvoice.CInvoicePostingYear) + chr(2) + t_sCInvoice.tcJournalCode + chr(2) + string(t_sCInvoice.CInvoiceVoucher) (icArguments), 
                input  ' ':U (icFieldName), 
                input  ' ':U (icFieldValue), 
                input  ' E':U (icType), 
                input  3 (iiSeverity), 
                input  t_sCInvoice.tc_Rowid (icRowid), 
                input  'QadFin-7958':U (icFcMsgNumber), 
                input  ' ':U (icFcExplanation), 
                input  ' ':U (icFcIdentification), 
                input  ' ':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            return.
        end.
        else do:
            /* check in database */
            <Q-36 assign vlFcQueryRecordsAvailable = CInvoiceByCyIDYearJrnlVchr (NoCache)
               (input viCompanyId, (CompanyId)
                input 0, (CInvoiceId)
                input t_sCInvoice.CInvoicePostingYear, (PeriodYear)
                input 0, (JournalId)
                input t_sCInvoice.tcJournalCode, (JournalCode)
                input t_sCInvoice.CInvoiceVoucher, (Voucher)) in BCInvoice >
            if vlFcQueryRecordsAvailable <> false 
            then do:
                assign oiReturnStatus = -1.
                <M-39 run SetMessage
                   (input  #T-55'Duplicate Invoice $1 $2 $3':50(69153)T-55# (icMessage), 
                    input  string(t_sCInvoice.CInvoicePostingYear) + chr(2) + t_sCInvoice.tcJournalCode + chr(2) + string(t_sCInvoice.CInvoiceVoucher) (icArguments), 
                    input  ' ':U (icFieldName), 
                    input  ' ':U (icFieldValue), 
                    input  ' E':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sCInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-7957':U (icFcMsgNumber), 
                    input  ' ':U (icFcExplanation), 
                    input  ' ':U (icFcIdentification), 
                    input  ' ':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                return.
            end.
        end.
    end.
    
    /* Assign Creditor_ID */
    if t_sCInvoice.tc_Status       = "N":U or
       t_sCInvoice.tc_Status       = "C":U and
       t_sCInvoice.tcCreditorCode <> t_iCInvoice.tcCreditorCode
    then do:
        if t_sCInvoice.tcCreditorCode = "":U or
           t_sCInvoice.tcCreditorCode = ?
        then assign t_sCInvoice.Creditor_ID = 0.
        else do:
            if not blStartVal1
            then do:
                <Q-9 run CreditorForCInvoiceVal  (Start) in BCreditor >
                assign blStartVal1 = true.
            end.

            <Q-10 run CreditorForCInvoiceVal (all) (Read) (Cache)
               (input t_sCInvoice.Company_ID, (CompanyId)
                input t_sCInvoice.tcCreditorCode, (CreditorCode)
                output dataset tqCreditorForCInvoiceVal) in BCreditor >

            find first tqCreditorForCInvoiceVal where
                       tqCreditorForCInvoiceVal.tcCreditorCode = t_sCInvoice.tcCreditorCode
                       no-error.

            assign t_sCInvoice.Creditor_ID = if available tqCreditorForCInvoiceVal
                                             then tqCreditorForCInvoiceVal.tiCreditor_ID
                                             else 0.
        end.
    end.

    /* Assign Period_ID */
    if t_sCInvoice.tc_Status              = "N":U                           or
       t_sCInvoice.tc_Status              = "C":U                           and
      (t_sCInvoice.CInvoicePostingYear   <> t_iCInvoice.CInvoicePostingYear or
       t_sCInvoice.CInvoicePostingPeriod <> t_iCInvoice.CInvoicePostingPeriod)
    then do:
        if not vlStart1
        then do:
            <Q-3 run PeriodPrim  (Start) in BPeriod >
            assign vlStart1 = true.
        end.

        <Q-4 run PeriodPrim (all) (Read) (Cache)
           (input t_sCInvoice.Company_ID, (CompanyId)
            input ?, (PeriodId)
            input t_sCInvoice.CInvoicePostingYear, (PeriodYear)
            input t_sCInvoice.CInvoicePostingPeriod, (PeriodPeriod)
            output dataset tqPeriodPrim) in BPeriod >

        find first tqPeriodPrim where
                   tqPeriodPrim.tiCompany_Id   = t_sCInvoice.Company_ID          and
                   tqPeriodPrim.tiPeriodYear   = t_sCInvoice.CInvoicePostingYear and
                   tqPeriodPrim.tiPeriodPeriod = t_sCInvoice.CInvoicePostingPeriod
                   no-error.

        if available tqPeriodPrim
        then assign t_sCInvoice.Period_ID                 = tqPeriodPrim.tiPeriod_ID
                    t_sCInvoice.CInvoicePostingYearPeriod = tqPeriodPrim.tiPeriodYear * 100 + tqPeriodPrim.tiPeriodPeriod.
    end.

    /* Assign tcNormalPaymentConditionType if empty */
    if (t_sCInvoice.tc_Status                     = "N":U                                     or
        t_sCInvoice.tc_Status                     = "C":U                                     and
        t_sCInvoice.tcNormalPaymentConditionCode <> t_iCInvoice.tcNormalPaymentConditionCode) and
        t_sCInvoice.tcNormalPaymentConditionCode <> "":U                                      and
        t_sCInvoice.tcNormalPaymentConditionCode <> ?                                         and
       (t_sCInvoice.tcNormalPaymentConditionType  = "":U                                      or
        t_sCInvoice.tcNormalPaymentConditionType  = ?)
    then do:
        if not vlStart2
        then do:
            <Q-5 run PaymentConditionByIdCode  (Start) in BPaymentCondition >
            assign vlStart2 = true.
        end.

        <Q-6 run PaymentConditionByIdCode (all) (Read) (Cache)
           (input ?, (PaymentConditionId)
            input t_sCInvoice.tcNormalPaymentConditionCode, (PaymentConditionCode)
            output dataset tqPaymentConditionByIdCode) in BPaymentCondition >

        find first tqPaymentConditionByIdCode where
                   tqPaymentConditionByIdCode.tcPaymentConditionCode = t_sCInvoice.tcNormalPaymentConditionCode
                   no-error.

        if available tqPaymentConditionByIdCode
        then assign t_sCInvoice.tcNormalPaymentConditionType = tqPaymentConditionByIdCode.tcPaymentConditionPaymentTyp.
    end.

    /* Fill CInvoiceDueDate if payment condition is staged */
    if t_sCInvoice.tcNormalPaymentConditionType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
    then do:
        for each t_sCInvoiceStage where
                 t_sCInvoiceStage.tc_ParentRowid = t_sCInvoice.tc_Rowid and
                 t_sCInvoiceStage.tc_Status     <> "D":U
                 by t_sCInvoiceStage.CInvoiceStageDueDate:
            if t_sCInvoice.CInvoiceDueDate <> t_sCInvoiceStage.CInvoiceStageDueDate
            then do:
                if t_sCInvoice.CInvoiceType <> {&INVOICETYPE-PREPAYMENT}
                then assign t_sCInvoice.CInvoiceDueDate = t_sCInvoiceStage.CInvoiceStageDueDate.

                if t_sCInvoice.tc_Status = "":U
                then assign t_sCInvoice.tc_Status = "C":U.
            end.

            leave.
        end.

        if t_sCInvoice.CInvoiceDiscountDueDate <> ?
        then do:
            assign t_sCInvoice.CInvoiceDiscountDueDate = ?.

            if t_sCInvoice.tc_Status = "":U
            then assign t_sCInvoice.tc_Status = "C":U.
        end.
    end.
    /* Remove CInvoiceStage records if payment condition isn't staged */
    else for each t_sCInvoiceStage where
                  t_sCInvoiceStage.tc_ParentRowid = t_sCInvoice.tc_Rowid:
        assign t_sCInvoiceStage.tc_Status = "D":U.
    end.

    /* Remove CInvoicePO records if receiver matching option is false and remove the empty records */
    if t_sCInvoice.CInvoiceIsLogisticMatching  = false       and
       vcActivityCode                         <> "Reverse":U
    then do:
        for each t_sCInvoicePO where
                 t_sCInvoicePO.tc_ParentRowid = t_sCInvoice.tc_Rowid:
            assign t_sCInvoicePO.tc_Status = "D":U.
        end. /* for each t_sCInvoicePO where */
    end. /*if t_sCInvoice.CInvoiceIsLogisticMatching = false and*/

    for each t_sCInvoicePO where
             t_sCInvoicePO.tc_ParentRowid   = t_sCInvoice.tc_Rowid and 
             t_sCInvoicePO.tc_Status        <> "D":U               and 
             (t_sCInvoicePO.CInvoicePOPoNbr  = "":U or 
              t_sCInvoicePO.CInvoicePOPoNbr  = "":U) :
        assign t_sCInvoicePO.tc_Status = "D":U.
    end. /* for each t_sCInvoicePO where */

    /* Assign CInvoiceIsWithBank */
    if t_sCInvoice.tc_Status = "N":U or
       t_sCInvoice.tc_Status = "C":U
    then assign t_sCInvoice.CInvoiceIsWithBank = can-find(first t_sCInvoiceBank where
                                                                t_sCInvoiceBank.tc_ParentRowId = t_sCInvoice.tc_RowID and
                                                                t_sCInvoiceBank.tc_Status     <> "D":U).


    /* Handle CInvoiceVat */
    <M-18 run ValidateComponentPreVat
       (input  vdCInvoiceExchangeRate (idCInvoiceExchangeRate), 
        input  vdCInvoiceRateScale (idCInvoiceRateScale), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>

    if viFcReturnSuper <> 0 and
       oiReturnStatus  >= 0
    then assign oiReturnStatus = viFcReturnSuper.                                                                
                                                                

    /* Fill some DB fields based on calculated fields */
    if (t_sCInvoice.tc_Status              = "N":U                            and
      (t_sCInvoice.CInvoiceType            = {&INVOICETYPE-INVOICE}           or
       t_sCInvoice.CInvoiceType            = {&INVOICETYPE-CREDITNOTE}        or
       t_sCInvoice.CInvoiceType            = {&INVOICETYPE-INVOICECORRECTION} or
       t_sCInvoice.CInvoiceType            = {&INVOICETYPE-CREDITNOTECORRECTION})) or
      (t_sCInvoice.tc_Status               = "C":U and
      (t_sCInvoice.CInvoiceIsInitialStatus = true or
       t_iCInvoice.CInvoiceIsInitialStatus = true) and
      (t_sCInvoice.CInvoiceType            = {&INVOICETYPE-INVOICE}           or
       t_sCInvoice.CInvoiceType            = {&INVOICETYPE-CREDITNOTE}        or
       t_sCInvoice.CInvoiceType            = {&INVOICETYPE-INVOICECORRECTION} or
       t_sCInvoice.CInvoiceType            = {&INVOICETYPE-CREDITNOTECORRECTION}))
    then do :
        
        if t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or
           t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
        then do:
            if t_sCInvoice.CInvoiceIsTaxExcluded = false
            then assign t_sCInvoice.CInvoiceOriginalCreditTC = t_sCInvoice.tdCInvoiceOriginalTC
                        t_sCInvoice.CInvoiceOriginalDebitTC  = 0
                        t_sCInvoice.CInvoiceOriginalCreditLC = t_sCInvoice.tdCInvoiceOriginalLC
                        t_sCInvoice.CInvoiceOriginalDebitLC  = 0.
            else assign t_sCInvoice.CInvoiceOriginalCreditTC = t_sCInvoice.tdCInvoiceOriginalTC + t_sCInvoice.CInvoiceVatDebitTC - t_sCInvoice.CInvoiceVatCreditTC
                        t_sCInvoice.CInvoiceOriginalDebitTC  = 0
                        t_sCInvoice.CInvoiceOriginalCreditLC = t_sCInvoice.tdCInvoiceOriginalLC + t_sCInvoice.CInvoiceVatDebitLC - t_sCInvoice.CInvoiceVatCreditLC
                        t_sCInvoice.CInvoiceOriginalDebitLC  = 0.
        end.
        else do:
            if t_sCInvoice.CInvoiceIsTaxExcluded = false
            then assign t_sCInvoice.CInvoiceOriginalCreditTC = 0
                        t_sCInvoice.CInvoiceOriginalDebitTC  = t_sCInvoice.tdCInvoiceOriginalTC
                        t_sCInvoice.CInvoiceOriginalCreditLC = 0
                        t_sCInvoice.CInvoiceOriginalDebitLC  = t_sCInvoice.tdCInvoiceOriginalLC.
            else assign t_sCInvoice.CInvoiceOriginalCreditTC = 0
                        t_sCInvoice.CInvoiceOriginalDebitTC  = t_sCInvoice.tdCInvoiceOriginalTC + t_sCInvoice.CInvoiceVatCreditTC - t_sCInvoice.CInvoiceVatDebitTC
                        t_sCInvoice.CInvoiceOriginalCreditLC = 0
                        t_sCInvoice.CInvoiceOriginalDebitLC  = t_sCInvoice.tdCInvoiceOriginalLC + t_sCInvoice.CInvoiceVatCreditLC - t_sCInvoice.CInvoiceVatDebitLC.
        end.
        
        /* Set all balance fields based upon the original invoice-values - needed for inital invoices and in case of replacements */ 
        if t_sCInvoice.CInvoiceIsInitialStatus = true or 
           (t_sCInvoice.tc_Status = "N":U and 
            vcActivityCode        = "Replace":U)
        then assign t_sCInvoice.CInvoiceBalanceCreditTC = t_sCInvoice.CInvoiceOriginalCreditTC
                    t_sCInvoice.CInvoiceBalanceDebitTC  = t_sCInvoice.CInvoiceOriginalDebitTC
                    t_sCInvoice.CInvoiceBalanceCreditLC = t_sCInvoice.CInvoiceOriginalCreditLC
                    t_sCInvoice.CInvoiceBalanceDebitLC  = t_sCInvoice.CInvoiceOriginalDebitLC
                    t_sCInvoice.CInvoiceBalanceCreditCC = t_sCInvoice.CInvoiceOriginalCreditCC
                    t_sCInvoice.CInvoiceBalanceDebitCC  = t_sCInvoice.CInvoiceOriginalDebitCC
                    t_sCInvoice.CInvoiceBalanceTC       = t_sCInvoice.CInvoiceBalanceDebitTC - t_sCInvoice.CInvoiceBalanceCreditTC
                    t_sCInvoice.CInvoiceBalanceLC       = t_sCInvoice.CInvoiceBalanceDebitLC - t_sCInvoice.CInvoiceBalanceCreditLC
                    t_sCInvoice.CInvoiceBalanceCC       = t_sCInvoice.CInvoiceBalanceDebitCC - t_sCInvoice.CInvoiceBalanceCreditCC.

        /* Update cc amounts and Rate */
        if viCompanyCCId <> 0 and
           viCompanyCCId <> ?
        then do :
            if t_sCInvoice.tcCurrencyCode = vcCompanyCC
            then assign t_sCInvoice.CInvoiceOriginalDebitCC  = t_sCInvoice.CInvoiceOriginalDebitTC
                        t_sCInvoice.CInvoiceOriginalCreditCC = t_sCInvoice.CInvoiceOriginalCreditTC
                        t_sCInvoice.CInvoiceCCRate           = 1
                        t_sCInvoice.CInvoiceCCScale          = 1.
            else if vlDomainIsStatutory = false
                 then assign t_sCInvoice.CInvoiceOriginalDebitCC  = t_sCInvoice.CInvoiceOriginalDebitLC
                             t_sCInvoice.CInvoiceOriginalCreditCC = t_sCInvoice.CInvoiceOriginalCreditLC
                             t_sCInvoice.CInvoiceCCRate           = t_sCInvoice.CInvoiceExchangeRate
                             t_sCInvoice.CInvoiceCCScale          = t_sCInvoice.CInvoiceRateScale.            
                 else do:
                    if vdCInvoiceExchangeRate = ?
                    then do:
                         if (t_sCInvoice.CInvoiceCCRate <> 0 and t_sCInvoice.CInvoiceCCScale <> ?)
                         then do:
                             assign vdCInvoiceExchangeRate = t_sCInvoice.CInvoiceCCRate
                                    vdCInvoiceRateScale    = t_sCInvoice.CInvoiceCCScale.
                         end.
                         else do:
                            <M-770 run GetExRateByEntityInvoiceFlag
                               (input  t_sCInvoice.CInvoiceDate (itCInvoiceDate), 
                                input  t_sCInvoice.CInvoicePostingDate (itCInvoicePostingDate), 
                                input  0 (iiFromCurrencyId), 
                                input  t_sCInvoice.tcCurrencyCode (icFromCurrencyCode), 
                                input  viCompanyCCId (iiToCurrencyId), 
                                input  vcCompanyCC (icToCurrencyCode), 
                                input  0 (iiExchangeRateTypeId), 
                                input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                                output vdCInvoiceExchangeRate (odCInvoiceExchangeRate), 
                                output vdCInvoiceRateScale (odCInvoiceRateScale), 
                                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                            if viFcReturnSuper <> 0 and oiReturnStatus  >= 0
                            then assign oiReturnStatus = viFcReturnSuper.
                            <M-88 run CheckLinkedInvoiceCC
                               (input  t_sCInvoice.tc_Rowid (icInvoiceRowid), 
                                input-output vdCInvoiceExchangeRate (bdExchangeRate), 
                                input-output vdCInvoiceRateScale (bdRateScale), 
                                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                            if viFcReturnSuper <> 0 and oiReturnStatus  >= 0
                            then assign oiReturnStatus = viFcReturnSuper.
                        end. /* else do */
                    end. /* if vdCInvoiceExchangeRate = ? */
                    assign t_sCInvoice.CInvoiceOriginalDebitCC  = <M-14 RoundAmount
                                                                 (input  t_sCInvoice.CInvoiceOriginalDebitTC * vdCInvoiceExchangeRate * vdCInvoiceRateScale (idUnroundedAmount), 
                                                                  input  viCompanyCCId (iiCurrencyID), 
                                                                  input  vcCompanyCC (icCurrencyCode)) in business>
                           t_sCInvoice.CInvoiceOriginalCreditCC = <M-13 RoundAmount
                                                                     (input  t_sCInvoice.CInvoiceOriginalCreditTC * vdCInvoiceExchangeRate * vdCInvoiceRateScale (idUnroundedAmount), 
                                                                      input  viCompanyCCId (iiCurrencyID), 
                                                                      input  vcCompanyCC (icCurrencyCode)) in business>
                           t_sCInvoice.CInvoiceCCRate           = vdCInvoiceExchangeRate
                           t_sCInvoice.CInvoiceCCScale          = vdCInvoiceRateScale.
                end. /* else - not if vlDomainIsStatutory = false */
            assign t_sCInvoice.CInvoiceBalanceCreditCC = t_sCInvoice.CInvoiceOriginalCreditCC
                   t_sCInvoice.CInvoiceBalanceDebitCC  = t_sCInvoice.CInvoiceOriginalDebitCC
                   t_sCInvoice.CInvoiceBalanceCC       = t_sCInvoice.CInvoiceOriginalDebitCC - t_sCInvoice.CInvoiceOriginalCreditCC.
                   t_sCInvoice.CInvoiceIsOpen          = if t_sCInvoice.CInvoiceIsInitial = true
                                                         then t_sCInvoice.CInvoiceBalanceTC <> 0 or t_sCInvoice.CInvoiceBalanceLC <> 0 or t_sCInvoice.CInvoiceBalanceCC <> 0
                                                         else t_sCInvoice.CInvoiceIsOpen.
        end. /* if viCompanyCCId <> 0 and */
        
    end. /* if (t_sCInvoice.tc_Status              = "N":U                            and  */


    /* Update values of some calcualted fields to have total invoice amount */
    if t_sCInvoice.CInvoiceIsTaxExcluded
    then do:
        if t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or
           t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
        then assign t_sCInvoice.tdCInvoiceOriginalTC = t_sCInvoice.CInvoiceOriginalCreditTC
                    t_sCInvoice.tdCInvoiceOriginalLC = t_sCInvoice.CInvoiceOriginalCreditLC.
        else if t_sCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or
                t_sCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
        then assign t_sCInvoice.tdCInvoiceOriginalTC = t_sCInvoice.CInvoiceOriginalDebitTC
                    t_sCInvoice.tdCInvoiceOriginalLC = t_sCInvoice.CInvoiceOriginalDebitLC.
    end.

    /* Assign CInvoiceNonDiscAmountLC */
    if t_sCInvoice.tc_Status             = "N":U or
       t_sCInvoice.tc_Status             = "C":U 
    then do:
        if t_sCInvoice.tcCurrencyCode = vcCompanyLC
        then assign t_sCInvoice.CInvoiceNonDiscAmtLC = t_sCInvoice.CInvoiceNonDiscAmtTC.
        else assign t_sCInvoice.CInvoiceNonDiscAmtLC = <M-19 RoundAmount
                                                          (input  t_sCInvoice.CInvoiceNonDiscAmtTC * t_sCInvoice.CInvoiceExchangeRate * t_sCInvoice.CInvoiceRateScale (idUnroundedAmount), 
                                                           input  viCompanyLCId (iiCurrencyID), 
                                                           input  vcCompanyLC (icCurrencyCode)) in business>.
        if viCompanyCCId <> 0 and
           viCompanyCCId <> ?
        then do:
            if t_sCInvoice.tcCurrencyCode = vcCompanyCC
            then assign t_sCInvoice.CInvoiceNonDiscAmtCC = t_sCInvoice.CInvoiceNonDiscAmtTC.
            else if vlDomainIsStatutory = false
            then assign t_sCInvoice.CInvoiceNonDiscAmtCC = t_sCInvoice.CInvoiceNonDiscAmtLC.
            else do:
                if vdCInvoiceExchangeRate = ?
                then do:
                     if (t_sCInvoice.CInvoiceCCRate <> 0 and t_sCInvoice.CInvoiceCCScale <> ?)
                     then do:
                         assign vdCInvoiceExchangeRate = t_sCInvoice.CInvoiceCCRate
                                vdCInvoiceRateScale    = t_sCInvoice.CInvoiceCCScale.
                    end.
                    else do:
                        <M-81 run GetExRateByEntityInvoiceFlag
                           (input  t_sCInvoice.CInvoiceDate (itCInvoiceDate), 
                            input  t_sCInvoice.CInvoicePostingDate (itCInvoicePostingDate), 
                            input  0 (iiFromCurrencyId), 
                            input  t_sCInvoice.tcCurrencyCode (icFromCurrencyCode), 
                            input  viCompanyCCId (iiToCurrencyId), 
                            input  vcCompanyCC (icToCurrencyCode), 
                            input  0 (iiExchangeRateTypeId), 
                            input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                            output vdCInvoiceExchangeRate (odCInvoiceExchangeRate), 
                            output vdCInvoiceRateScale (odCInvoiceRateScale), 
                            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                        if viFcReturnSuper <> 0 and oiReturnStatus  >= 0
                        then assign oiReturnStatus = viFcReturnSuper.
                    end. /* else do */
                end. /* if vdCInvoiceExchangeRate = ? */

                assign t_sCInvoice.CInvoiceNonDiscAmtCC = <M-21 RoundAmount
                                                             (input  t_sCInvoice.CInvoiceNonDiscAmtTC * vdCInvoiceExchangeRate * vdCInvoiceRateScale (idUnroundedAmount), 
                                                              input  viCompanyCCId (iiCurrencyID), 
                                                              input  vcCompanyCC (icCurrencyCode)) in business>.
            end. /* else do: */
        end. /* if viCompanyCCId <> 0 and */
    end. /* if t_sCInvoice.tc_Status             = "N":U or */


    /* Handle CInvoiceBank */
    <M-17 run ValidateComponentPreBank
       (input-output vlStart3 (blStart3), 
        input-output vlStart4 (blStart4), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>

    if viFcReturnSuper <> 0 and
       oiReturnStatus  >= 0
    then assign oiReturnStatus = viFcReturnSuper.


    /* Handle CInvoicePO */
    <M-42 run ValidateComponentPrePO
       (input  t_sCInvoice.tc_Rowid (icCInvoiceRowId), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0 and
       oiReturnStatus  >= 0
    then assign oiReturnStatus = viFcReturnSuper.

    /* Handle CInvoiceWHT */
    <M-43 run ValidateComponentPreWHT
       (input  vdCInvoiceExchangeRate (idCInvoiceExchangeRate), 
        input  vdCInvoiceRateScale (idCInvoiceRateScale), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0 and
       oiReturnStatus  >= 0
    then assign oiReturnStatus = viFcReturnSuper.
    
    /* Get control account if creating initial invoice*/
    if t_sCInvoice.CInvoiceIsInitial = true 
    then do:
        if not blStartVal1
        then do:
            <Q-27 run CreditorForCInvoiceVal  (Start) in BCreditor >
            assign blStartVal1 = true.
        end.
    
        <Q-28 run CreditorForCInvoiceVal (all) (Read) (Cache)
           (input t_sCInvoice.Company_ID, (CompanyId)
            input t_sCInvoice.tcCreditorCode, (CreditorCode)
            output dataset tqCreditorForCInvoiceVal) in BCreditor >
        find first tqCreditorForCInvoiceVal where
                   tqCreditorForCInvoiceVal.tcCreditorCode = t_sCInvoice.tcCreditorCode
                   no-error.
        if available tqCreditorForCInvoiceVal 
        then assign viControlGLProfileId = if t_sCInvoice.CInvoiceType = {&INVOICETYPE-PREPAYMENT}
                                           then tqCreditorForCInvoiceVal.tiPrePayControlGLProfile_ID
                                           else if t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or
                                                   t_sCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                then tqCreditorForCInvoiceVal.tiInvControlGLProfile_ID
                                                else tqCreditorForCInvoiceVal.tiCnControlGLProfile_ID.
    
        if viControlGLProfileID <> 0 and viControlGLProfileID <> ?
        then do:
            <Q-24 run ProfileLinkByGL (all) (Read) (NoCache)
               (input viControlGLProfileId, (GlProfileId)
                input viCompanyId, (CompanyId)
                output dataset tqProfileLinkByGL) in BProfile >
            find first tqProfileLinkByGL where 
                       tqProfileLinkByGL.tiProfile_ID = viControlGLProfileId
                       no-error.
            if not available tqProfileLinkByGL
            then do:
            <M-80 run SetMessage
               (input  trim(#T-74'Supplier control account not found.':150(999890512)T-74#) (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  t_sCInvoice.tc_Rowid (icRowid), 
                input  'qadfin-126490':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>                  
                assign oiReturnStatus = -1.
                return.
            end.
            assign t_sCInvoice.tcControlGLCode = tqProfileLinkByGL.tcGLCode.
        end.
    end.
    if t_sCInvoice.tc_Status = "C":U
    then do:          
        find first tqReasonByIdCode where tqReasonByIdCode.tcReasonCode = t_sCInvoice.tcReasonCode no-error.        
        if available tqReasonByIdCode
        then do:
            if (t_iCInvoice.CInvoiceIsInitialStatus = false and tqReasonByIdCode.tlReasonIsInitialStatus = true)
            then do:          
                <M-51 run SetMessage
                   (input  #T-57'Changing the Invoice Status code to an Initial type is not allowed for posted invoices':90(999890454)T-57# (icMessage), 
                    input  ' ':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  ' ':U (icRowid), 
                    input  'QadFin-9103':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        
                assign oiReturnStatus = -1.
               return.
            end.
        end.
    end.    
end.

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

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

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

if vlStart4
then do:
    <Q-23 run PayFormatCodebyGroupID  (Stop) in BPaymentFormat >
end.

for each t_sCInvoice where
         t_sCInvoice.tc_Status = "D":U and 
         t_sCInvoice.CInvoiceIsInitialStatus <> true:
    <M-30 run SetMessage
       (input  #T-58'The invoice you selected does not have a status of Initial and cannot be deleted.':100(999890513)T-58# (icMessage), 
        input  ' ':U (icArguments), 
        input  'tcInvoice.CInvoiceIsInitialStatus':U (icFieldName), 
        input  t_sCInvoice.CInvoiceIsInitialStatus (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  t_sCInvoice.tc_Rowid (icRowid), 
        input  'QadFin-7857':U (icFcMsgNumber), 
        input  ' ':U (icFcExplanation), 
        input  ' ':U (icFcIdentification), 
        input  ' ':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    assign oiReturnStatus = -1.
end.