project QadFinancials > class BCInvoice > method Calculate

Description

Add code here to initialize the calculated fields of the class temp-tables after loading existing records from the application database.


Parameters


oiReturnStatusoutputinteger


Internal usage


unused


program code (program/bcinvoice.p)

<ANCESTOR-CODE>

/* Go through the invoices that have not yet been handled by this method (applicable when doing multiple DataLoads): that's why we have this where-clause here */
for each tCInvoice where
         (tCInvoice.tcLocalCurrencyCode = "":U or 
          tCInvoice.tcLocalCurrencyCode = ?) and 
         tCInvoice.tc_Status = "":U:
    
    if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or
       tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
    then do:
        if tCInvoice.CInvoiceIsTaxable and tCInvoice.CInvoiceIsTaxExcluded
        then assign tCInvoice.tdCInvoiceOriginalTC = tCInvoice.CInvoiceVatBaseDebitTC
                    tCInvoice.tdCInvoiceOriginalLC = tCInvoice.CInvoiceVatBaseDebitLC
                    tCInvoice.tdCInvoiceOriginalCC = tCInvoice.tdCInvoiceOriginalTC * tCInvoice.CInvoiceCCRate * tCInvoice.CInvoiceRateScale.
        else assign tCInvoice.tdCInvoiceOriginalTC = tCInvoice.CInvoiceOriginalCreditTC
                    tCInvoice.tdCInvoiceOriginalLC = tCInvoice.CInvoiceOriginalCreditLC
                    tCInvoice.tdCInvoiceOriginalCC = tCInvoice.CInvoiceOriginalCreditCC.
    end.
    else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or
            tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
         then do:
            if tCInvoice.CInvoiceIsTaxable and tCInvoice.CInvoiceIsTaxExcluded
            then assign tCInvoice.tdCInvoiceOriginalTC = tCInvoice.CInvoiceVatBaseCreditTC
                        tCInvoice.tdCInvoiceOriginalLC = tCInvoice.CInvoiceVatBaseCreditLC
                        tCInvoice.tdCInvoiceOriginalCC = tCInvoice.tdCInvoiceOriginalTC * tCInvoice.CInvoiceCCRate * tCInvoice.CInvoiceRateScale.
            else assign tCInvoice.tdCInvoiceOriginalTC = tCInvoice.CInvoiceOriginalDebitTC
                        tCInvoice.tdCInvoiceOriginalLC = tCInvoice.CInvoiceOriginalDebitLC
                        tCInvoice.tdCInvoiceOriginalCC = tCInvoice.CInvoiceOriginalDebitCC.
         end. /* if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or */
         else do:
            if tCInvoice.CInvoiceIsTaxable and tCInvoice.CInvoiceIsTaxExcluded
            then if tCInvoice.CInvoiceVatBaseDebitTC <> 0
                 then assign tCInvoice.tdCInvoiceOriginalTC  = tCInvoice.CInvoiceVatBaseDebitTC
                             tCInvoice.tdCInvoiceOriginalLC  = tCInvoice.CInvoiceVatBaseDebitLC
                             tCInvoice.tdCInvoiceOriginalCC  = tCInvoice.tdCInvoiceOriginalTC * tCInvoice.CInvoiceCCRate * tCInvoice.CInvoiceRateScale.
                 else assign tCInvoice.tdCInvoiceOriginalTC  = tCInvoice.CInvoiceVatBaseCreditTC
                             tCInvoice.tdCInvoiceOriginalLC  = tCInvoice.CInvoiceVatBaseCreditLC
                             tCInvoice.tdCInvoiceOriginalCC  = tCInvoice.tdCInvoiceOriginalTC * tCInvoice.CInvoiceCCRate * tCInvoice.CInvoiceRateScale.
            else if tCInvoice.CInvoiceOriginalCreditTC <> 0
                 then assign tCInvoice.tdCInvoiceOriginalTC  = tCInvoice.CInvoiceOriginalCreditTC
                             tCInvoice.tdCInvoiceOriginalLC  = tCInvoice.CInvoiceOriginalCreditLC
                             tCInvoice.tdCInvoiceOriginalCC  = tCInvoice.CInvoiceOriginalCreditCC.
                 else assign tCInvoice.tdCInvoiceOriginalTC  = tCInvoice.CInvoiceOriginalDebitTC
                             tCInvoice.tdCInvoiceOriginalLC  = tCInvoice.CInvoiceOriginalDebitLC
                             tCInvoice.tdCInvoiceOriginalCC  = tCInvoice.CInvoiceOriginalDebitCC.
         end. /* not if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or */
        
    assign tCInvoice.tcLocalCurrencyCode        = vcCompanyLC
           tCInvoice.tcStatutoryCurrencyCode    = vcCompanyCC
           tCInvoice.tdTotalAmountDebitTC       = tCInvoice.CInvoiceVatBaseDebitTC + tCInvoice.CInvoiceVatDebitTC
           tCInvoice.tdTotalAmountCreditTC      = tCInvoice.CInvoiceVatBaseCreditTC + tCInvoice.CInvoiceVatCreditTC
           tCInvoice.tcCInvoiceNumber           = string(tCInvoice.CInvoicePostingYear) + "/":U + tCInvoice.tcJournalCode + string(tCInvoice.CInvoiceVoucher, "999999999":U)
           tCInvoice.tdCInvoiceOriginalAmount   = tCInvoice.CInvoiceOriginalDebitTC - tCInvoice.CInvoiceOriginalCreditTC
           tCInvoice.tdCInvoiceOriginalAmountLC = tCInvoice.CInvoiceOriginalDebitLC - tCInvoice.CInvoiceOriginalCreditLC
           tCInvoice.tdCInvoiceOpenAmount       = tCInvoice.CInvoiceBalanceDebitTC - tCInvoice.CInvoiceBalanceCreditTC
           tCInvoice.tdCInvoiceOpenAmountLC     = tCInvoice.CInvoiceBalanceDebitLC - tCInvoice.CInvoiceBalanceCreditLC.
                                         
    if tCInvoice.CInvoiceClosingDate <> ?
    then assign tCInvoice.tiOverdueDays = max(0, tCInvoice.CInvoiceClosingDate - tCInvoice.CInvoiceDueDate).

    /* Reason Allocation Status */
    <Q-56 run ReasonByIdCode (all) (Read) (Cache)
       (input tCInvoice.Reason_ID, (ReasonId)
        input ?, (ReasonCode)
        output dataset tqReasonByIdCode) in BReason >
    find first tqReasonByIdCode where
               tqReasonByIdCode.tiReason_ID = tCInvoice.Reason_ID
               no-error.

    assign tCInvoice.tcReasonAllocationStatus = if available tqReasonByIdCode
                                                then tqReasonByIdCode.tcReasonAllocationStatus
                                                else "":U.
    /* shx For the Supplier invoice reversal CA Journal & Voucher must be active not only for Creditnote invoice correction Creditonote correction, but also for other invoice types */
    /* CA Journal & Voucher */
    if tCInvoice.LinkedCInvoice_ID <> 0                                   and
       tCInvoice.LinkedCInvoice_ID <> ?                                   and
      (tCInvoice.CInvoiceType       = {&INVOICETYPE-CREDITNOTE}           or
       tcinvoice.cinvoicetype       = {&INVOICETYPE-CREDITNOTECORRECTION} or
       tCInvoice.CInvoiceType       = {&INVOICETYPE-INVOICECORRECTION}    or
       tCInvoice.CInvoiceType       = {&INVOICETYPE-INVOICE}) 
    then 
        for each tCInvoiceMovement where
                 tCInvoiceMovement.tc_ParentRowid = tCInvoice.tc_Rowid:
            <Q-3 run PostingLineByPostingLineId (all) (Read) (NoCache)
               (input tCInvoice.Company_ID, (CompanyId)
                input tCInvoiceMovement.PostingLine_ID, (PostingLineId)
                input {&JOURNALTYPE-CREDITORADJUSTMENT}, (JournalTypeCode)
                output dataset tqPostingLineByPostingLineId) in BPosting>
            find first tqPostingLineByPostingLineId where
                       tqPostingLineByPostingLineId.tiPostingLine_ID  = tCInvoiceMovement.PostingLine_ID and
                       tqPostingLineByPostingLineId.tcJournalTypeCode = {&JOURNALTYPE-CREDITORADJUSTMENT}
                       no-error.
            if available tqPostingLineByPostingLineId
            then assign tCInvoice.tcCAJournalCode = tqPostingLineByPostingLineId.tcJournalCode
                        tCInvoice.tiCAVoucher     = tqPostingLineByPostingLineId.tiPostingVoucher.
        end.

    /* Creditor Tax stuff */
    <Q-26 run CreditorAddressInfoByCreditor (first) (Read) (Cache)
       (input tCInvoice.Creditor_ID, (CreditorId)
        input ?, (CompanyId)
        input ?, (CreditorCode)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
        output dataset tqCreditorAddressInfoByCreditor) in BCreditor >
    find first tqCreditorAddressInfoByCreditor where
               tqCreditorAddressInfoByCreditor.tiCreditor_ID     = tCInvoice.Creditor_ID and
               tqCreditorAddressInfoByCreditor.tcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}
               no-error.
    if available tqCreditorAddressInfoByCreditor
    then assign tCInvoice.tlCreditorIsTaxReport        = tqCreditorAddressInfoByCreditor.tlCreditorIsTaxReport
                tCInvoice.tcCredVatNumberCountryCode   = tqCreditorAddressInfoByCreditor.tcCountryCode
                tCInvoice.tcShipFromTaxClass           = tqCreditorAddressInfoByCreditor.tcTxclTaxCls
                tCInvoice.tcShipFromTaxUsage           = tqCreditorAddressInfoByCreditor.tcTxuTaxUsage
                tCInvoice.tcShipFromTaxZone            = tqCreditorAddressInfoByCreditor.tcTxzTaxZone
                tCInvoice.tcShipFromCountryCode        = tqCreditorAddressInfoByCreditor.tcCountryCode
                tCInvoice.tlShipFromCountryIsEUCountry = tqCreditorAddressInfoByCreditor.tlCountryIsEUCountry
                tCInvoice.tiShipFromAddressID                 = tqCreditorAddressInfoByCreditor.tiAddress_ID.
                 
    /* vlCIREC */
    if not can-find(first bCInvoice where
                          rowid(bCInvoice) <> rowid(tCInvoice))
    then do:
        find first tCInvoicePosting where
                   tCInvoicePosting.CInvoice_ID         = tCInvoice.CInvoice_ID and
                   tCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL}
                   no-error.
        if available tCInvoicePosting
        then do:            
            <Q-7 assign vlFcQueryRecordsAvailable = PostingLineByPostingId (NoCache)
               (input tCInvoice.Company_ID, (CompanyId)
                input tCInvoicePosting.Posting_ID, (PostingId)
                input {&GLSYSTEMTYPE-CIREC}, (GLSystemTypeCode)
                input {&GLTYPECODE-SYST}, (GLTypeCode)) in BPosting>
            assign vlCIREC = vlFcQueryRecordsAvailable <> false.
        end.
    end.

    /* Payment Condition Payment Type */
    <Q-20 run PaymentConditionByIdCode (all) (Read) (Cache)
       (input tCInvoice.NormalPaymentCondition_ID, (PaymentConditionId)
        input ?, (PaymentConditionCode)
        output dataset tqPaymentConditionByIdCode) in BPaymentCondition >

    find first tqPaymentConditionByIdCode where
               tqPaymentConditionByIdCode.tiPaymentCondition_ID = tCInvoice.NormalPaymentCondition_ID
               no-error.

    if available tqPaymentConditionByIdCode
    then assign tCInvoice.tcNormalPaymentConditionType = tqPaymentConditionByIdCode.tcPaymentConditionPaymentTyp.

    /* Own VAT Number fields */
    if tCInvoice.ShipToAddress_ID <> ? and
       tCInvoice.ShipToAddress_ID <> 0
    then do:
        <Q-25 run AddressByIdByCode (all) (Read) (Cache)
           (input ?, (AddressCity)
            input ?, (AddressStreet1)
            input ?, (AddressStreet2)
            input ?, (AddressZip)
            input ?, (AddressTypeCode)
            input tCInvoice.ShipToAddress_ID, (AddressID)
            input ?, (BusinessRelationID)
            input ?, (BusinessRelationCode)
            input ?, (AddressTypeID)
            input ?, (AddressStreet3)
            output dataset tqAddressByIdByCode) in BBusinessRelation>
        find first tqAddressByIdByCode where
                   tqAddressByIdByCode.tiAddress_ID = tCInvoice.ShipToAddress_ID
                   no-error.
        if available tqAddressByIdByCode
        then do :
            assign tCInvoice.tcOwnVatNumberCountryCode    = tqAddressByIdByCode.tcCountryCode
                   tCInvoice.tcShipToTaxZone              = tqAddressByIdByCode.tcTxzTaxZone
                   tCInvoice.tcShipToBusinessRelationCode = tqAddressByIdByCode.tcBusinessRelationCode
                   tCInvoice.tcShipToAddressTypeCode      = tqAddressByIdByCode.tcAddressTypeCode
                   tCInvoice.tcShipToAddressStreet1       = tqAddressByIdByCode.tcAddressStreet1
                   tCInvoice.tcShipToAddressStreet2       = tqAddressByIdByCode.tcAddressStreet2
                   tCInvoice.tcShipToAddressStreet3       = tqAddressByIdByCode.tcAddressStreet3 
                   tCInvoice.tcShipToAddressZip           = tqAddressByIdByCode.tcAddressZip 
                   tCInvoice.tcShipToAddressCity          = tqAddressByIdByCode.tcAddressCity
                   tCInvoice.tcShipToAddressState         = tqAddressByIdByCode.tcStateCode
                   tCInvoice.tcShipToCountryCode          = tqAddressByIdByCode.tcCountryCode
                   tCInvoice.tlShipToAddressIsTaxInCity   = tqAddressByIdByCode.tlAddressIsTaxInCity
                   tCInvoice.tlShipToCountryIsEUCountry   = tqAddressByIdByCode.tlCountryIsEUCountry
                   tCInvoice.tiShipToAddressId            = tqAddressByIdByCode.tiAddress_ID.
        end. /* if available tqAddressByIdByCode */
    end. /* if tCInvoice.ShipToAddress_ID        <> ? and */
    
    /* Control G/L Account info */
    if tCInvoice.ControlGL_ID <> ? and
       tCInvoice.ControlGL_ID <> 0
    then do:
        <Q-50 run GLByGLType (all) (Read) (Cache)
           (input viCompanyId, (CompanyId)
            input tCInvoice.ControlGL_ID, (GLId)
            input ?, (GLCode)
            input ?, (GLTypeCode)
            input ?, (GLIsDivisionAccount)
            output dataset tqGLByGLType) in BGL >
        find first tqGLByGLType where
                   tqGLByGLType.tiGL_ID = tCInvoice.ControlGL_ID
                   no-error.
        if available tqGLByGLType
        then assign tCInvoice.tlControlGLIsDivisionAccount = tqGLByGLType.tlGLIsDivisionAccount.
    end.

    /* Determine if the invoice is used in payment */
    if tCInvoice.CInvoiceIsSelected
    then assign tCInvoice.tlCInvoiceIsReferenced = true.
    else do:        
        <Q-28 assign vlFcQueryRecordsAvailable = CDocumentInvoiceXrefByInvId (NoCache)
           (input tCInvoice.CInvoice_ID, (CInvoiceId)
            input ?, (DInvoiceId)) in BCDocument>
        assign tCInvoice.tlCInvoiceIsReferenced = vlFcQueryRecordsAvailable <> false.
    end.

    /* Make sure t_iCInvoice is also up-to-date */
    find t_iCInvoice where
         t_iCInvoice.tc_Rowid = tCInvoice.tc_Rowid
         no-error.
         
    if available t_iCInvoice
    then raw-transfer tCInvoice to t_iCInvoice.

    /* Assign a Database field after the raw-transfer to avoid error*/                                               
    if tCInvoice.ShipToAddress_ID <> ?          and
       tCInvoice.ShipToAddress_ID <> 0          and
       available tqAddressByIdByCode            and
       tqAddressByIdByCode.tlCountryIsEUCountry and
       tCInvoice.CInvoiceIsInitialStatus
    then
       assign tCInvoice.CInvoiceOwnVatNumber = tqAddressByIdByCode.tcAddressTaxIDState. 

           
    /* Calculate values of CInvoiceVat */

    assign viVatCurrencyId   = tCInvoice.VatCurrency_ID
           vdVatExchangeRate = tCInvoice.CInvoiceVatExchangeRate
           vdVatRateScale    = tCInvoice.CInvoiceVatRateScale.
           
    /* If VAT curency is not defined for invoice, it is the same as transactional */
    if viVatCurrencyId = ? or
       viVatCurrencyId = 0
    then assign viVatCurrencyId   = tCInvoice.CInvoiceCurrency_ID
                vdVatExchangeRate = 1
                vdVatRateScale    = 1.
                
    for each tCInvoiceVat where
             tCInvoiceVat.tc_ParentRowid = tCInvoice.tc_Rowid and
             tCInvoiceVat.tc_Status      = "":U:

        /* Calculate values of vat amounts in vat currency */
        assign tCInvoiceVat.tdCInvoiceVatVatBaseDebitVC  = <M-45 RoundAmount
                                                              (input  tCInvoiceVat.CInvoiceVatVatBaseDebitTC * vdVatExchangeRate * vdVatRateScale (idUnroundedAmount), 
                                                               input  viVatCurrencyId (iiCurrencyID), 
                                                               input  '':U (icCurrencyCode)) in business>
               tCInvoiceVat.tdCInvoiceVatVatBaseCreditVC = <M-46 RoundAmount
                                                              (input  tCInvoiceVat.CInvoiceVatVatBaseCreditTC * vdVatExchangeRate * vdVatRateScale (idUnroundedAmount), 
                                                               input  viVatCurrencyId (iiCurrencyID), 
                                                               input  '':U (icCurrencyCode)) in business>
               tCInvoiceVat.tdCInvoiceVatVatDebitVC      = <M-47 RoundAmount
                                                              (input  tCInvoiceVat.CInvoiceVatVatDebitTC * vdVatExchangeRate * vdVatRateScale (idUnroundedAmount), 
                                                               input  viVatCurrencyId (iiCurrencyID), 
                                                               input  '':U (icCurrencyCode)) in business>
               tCInvoiceVat.tdCInvoiceVatVatCreditVC     = <M-48 RoundAmount
                                                              (input  tCInvoiceVat.CInvoiceVatVatCreditTC * vdVatExchangeRate * vdVatRateScale (idUnroundedAmount), 
                                                               input  viVatCurrencyId (iiCurrencyID), 
                                                               input  '':U (icCurrencyCode)) in business>.
                                                                                                                           
            if tCInvoiceVat.CInvoiceVatVatCreditTC <> 0 and tCInvoiceVat.CInvoiceVatVatCreditCC <> 0
                then assign tCInvoiceVat.tdCInvoiceVatExchangeRateCC = tCInvoiceVat.CInvoiceVatVatCreditCC / tCInvoiceVat.CInvoiceVatVatCreditTC.
            else if tCInvoiceVat.CInvoiceVatVatDebitTC <> 0 and tCInvoiceVat.CInvoiceVatVatDebitCC <> 0
                then assign tCInvoiceVat.tdCInvoiceVatExchangeRateCC = tCInvoiceVat.CInvoiceVatVatDebitCC / tCInvoiceVat.CInvoiceVatVatDebitTC.                                                                                      

        /* Make sure t_iCInvoiceVat is up-to-date */
        find t_iCInvoiceVat where
             t_iCInvoiceVat.tc_Rowid = tCInvoiceVat.tc_Rowid
             no-error.

        if available t_iCInvoiceVat
        then raw-transfer tCInvoiceVat to t_iCInvoiceVat.
    end.
    
    assign tCInvoice.tlDaybookOnInvoice = if vlPOCAllowUpdateDaybookOnInv = TRUE 
                                          then TRUE
                                          else FALSE.                             
    
    /* I19 requirement for Brazil ================================================= */
    /* If Fiscal receiving  is activated in the domain and the invoice was created  */
    /* through ERS processor, then it has to be invoice for legal document          */
    /* ============================================================================ */
    assign tCInvoice.tlCInvoiceIsForLegalDoc = false.

    if vlDomainIsFiscalConfirmBCI and
       tCInvoice.CInvoiceIsERS
    then tCInvoice.tlCInvoiceIsForLegalDoc = true.
    
end. /* for each tCInvoice where */

/* Bank */
<M-30 run CalculateCInvoiceBank  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>

if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
then assign oiReturnStatus = viFcReturnSuper.