project QadFinancials > class BDInvoice > method AdditionalUpdatesAllCreateDA

Description

This submethod of AdditionalUpdatesAll holds all program-code that will create the DA posting and update the balance of the DI


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.AdditionalUpdatesAll


program code (program8/bdinvoice.p)

/* ========================== */
    /* Set default return-status  */
    /* ========================== */
    assign oiReturnStatus = -98.
    
    /* ================================================================================================================================= */
    /* Reset a class data-item that holds the comma-separated list with all the posting-ids of the CA-postings created from method       */
    /* AdditionalUpdatesAllCreateCA. This data-itam will be used in StopExternalInstances to remove these postings from the BJE-instance */
    /* ================================================================================================================================= */
    assign vcListDAPostingsCreatedInAddUpd = "":U.

    /* ======================================================================================= */
    /* General note: BJournalEntry is already started and opened by the caller-method          */
    /* ======================================================================================= */
    if can-find (first tDInvoiceMovement where 
                       tDInvoiceMovement.tc_Status <> "":U) and 
       (viBJournalEntryDIID                  = 0 or 
        viBJournalEntryDIID                  = ? or
        valid-handle(vhBJournalEntryDIInst) <> true)
    then do :
        assign vcMessage      = trim(#T-2'Internal error: unexpected situation-no communication to the postings available':255(17891)T-2#) + chr(10) +
                                trim(substitute(#T-3'Instance ID = &1.':255(17892)T-3#,string(viBJournalEntryDIID)))                                  + chr(10) +
                                trim(substitute(#T-4'Valid handle = &1.':255(17893)T-4#,string(valid-handle(vhBJournalEntryDIInst) = TRUE)))          + chr(10) +
                                trim(substitute(#T-5'Handle value = &1.':255(17894)T-5#,string(vhBJournalEntryDIInst)))
               oiReturnStatus = -3.
        <M-1 run SetMessage (input  vcMessage (icMessage), 
                     input  '':U (icArguments), 
                     input  '':U (icFieldName), 
                     input  '':U (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  '':U (icRowid), 
                     input  'QADFIN-4513':U (icFcMsgNumber), 
                     input  '':U (icFcExplanation), 
                     input  '':U (icFcIdentification), 
                     input  '':U (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        Return.
    end. /* if */
    
    /* ================================================================================================================ */
    /* validation :                                                                                                     */
    /* The system should not allow the automatic linking of invoice and credit note in Supplier invoice create          */
    /* when the exchange rates are different, because the system does not automatically create realised exchange        */
    /* gains and losses as part of that process. An error message should be shown, and the user must link the invoice   */
    /* and credit notes separately in Open Item Adjustments.                                                            */
    /* ================================================================================================================ */
    <M-28 run AdditionalUpdatesAllCreateDAVal  (output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then Return.

    /* ===================================================== */
    /* Go through all Invoices that are changed or new       */
    /* ===================================================== */
    for each tDInvoice WHERE
             (tDInvoice.tc_status          = "N":U or tDInvoice.tc_status = "C":U) and
             tDInvoice.LinkedDInvoice_ID <> 0     and 
             tDInvoice.LinkedDInvoice_ID <> ?     and
             (tDInvoice.DInvoiceType       = {&INVOICETYPE-CREDITNOTE} or
              tDInvoice.DInvoiceType       = {&INVOICETYPE-INVOICECORRECTION} or
              tDInvoice.DInvoiceType       = {&INVOICETYPE-CREDITNOTECORRECTION})
 :
        /*IF Linked Invoice is not changed then do not create DA*/
        if  tDInvoice.tc_status = "C":U
        then do:
            find t_iDInvoice where t_iDInvoice.tc_Rowid = tDInvoice.tc_Rowid no-error.
            if available t_iDInvoice and 
               tDInvoice.LinkedDInvoice_ID = t_iDInvoice.LinkedDInvoice_ID
            then next.
        end. /*If  tDInvoice.tc_status = "C":U*/  
      /* =============================================================================================================== */
        /* As a DataLoad is done in submethod AdditionalUpdatesAllCreateCA and as a DataLoad accesses table tDInvoice and  */
        /* as we are within a Loop on tDInvoice, we cannot allow multiple tDInvoices with tDInvoice.LinkedCInvoice_ID <> 0 */
        /* =============================================================================================================== */
        if vcListDAPostingsCreatedInAddUpd <> "":U or not available tDinvoice
        then do :
            assign vcMessage      = trim(#T-7'Internal error. The system cannot create a customer adjustment because the originating invoice is not available':255(17917)T-7#)
                   oiReturnStatus = -1.
            <M-8 run SetMessage (input  vcMessage (icMessage), 
                     input  '':U (icArguments), 
                     input  '':U (icFieldName), 
                     input  '':U (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  '':U (icRowid), 
                     input  'QADFIN-4520':U (icFcMsgNumber), 
                     input  '':U (icFcExplanation), 
                     input  '':U (icFcIdentification), 
                     input  '':U (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            Return.
        end. /* if vcListDAPostingsCreatedInAddUpd <> "":U */
        
        /* =================================================================== */
        /* Get the BusinessRelation and IntercoCode of the Debtor            */
        /* Query-result-set tqDebtorForReasonBusRel is still used further on      */
        /* =================================================================== */
        if not vlStartDebtorForReasonBusRel
        then do:
            <Q-24 run DebtorForReasonBusRel  (Start) in BDebtor >
            assign vlStartDebtorForReasonBusRel = true.
        end.
        <Q-23 run DebtorForReasonBusRel (all) (Read) (NoCache)
           (input ?, (CompanyId)
            input ?, (DebtorCode)
            input tDInvoice.Debtor_ID, (DebtorID)
            output dataset tqDebtorForReasonBusRel) in BDebtor >
        find first tqDebtorForReasonBusRel NO-error.
        assign vcDBusinessRelationInterCoCode  = if available tqDebtorForReasonBusRel
                                                 then tqDebtorForReasonBusRel.tcBusinessRelationICCode
                                                 else "":U
               vcBusinessRelationCode          = if available tqDebtorForReasonBusRel
                                                 then tqDebtorForReasonBusRel.tcBusinessRelationCode
                                                 else "":U.
        
        /* ======================================= */
        /* Create posting header record for the CA */
        /* ======================================= */
        assign vcPostingInvoiceReferenceText = string(tDInvoice.DInvoicePostingYear, "9999":U) + "/":U + 
                                               string(tDInvoice.DInvoicePostingPeriod,"99":U) +  " ":U + 
                                               tDInvoice.tcDAJournalCode + " ":U +
                                               string(tDInvoice.DInvoiceVoucher, "999999999":U).
        <M-10 run AddPostingHeader
           (input  tDInvoice.Company_ID (iiCompanyId), 
            input  tDInvoice.DInvoicePostingYear (iiPeriodYear), 
            input  tDInvoice.DInvoicePostingPeriod (iiPeriodPeriod), 
            input  tDInvoice.tcDAJournalCode (icJournalCode), 
            input  '' (icReportingJournalCode), 
            input  tDInvoice.tiDAVoucher (iiVoucher), 
            input  tDInvoice.DInvoicePostingDate (itPostingDate), 
            input  ? (itValueDate), 
            input  tDInvoice.DInvoiceDIText (icPostingText), 
            input  vcBusinessRelationCode (icPostingBusinessRelationText), 
            input  vcPostingInvoiceReferenceText (icPostingInvoiceReferenceText), 
            input  tDInvoice.DInvoiceDescription (icPostingParentText), 
            input  ? (iiBPeriodId), 
            input  tDInvoice.tcDebtorCode (icPostingOriginAddressCode), 
            input  string(tDInvoice.DInvoicePostingYear, '9999':U) + '/':U + trim(tdinvoice.tcdajournalCode) + string(tDInvoice.DInvoiceVoucher, '999999999':U) (icPostingOriginDocument), 
            input  ? (icPostingOriginDocumentType), 
            input  '':U (icBatchNumber), 
            input  ? (icBankImpLineRef), 
            output viPostingId (oiPostingId), 
            output vcPostingHeaderRowId (ocRowid), 
            output tdinvoice.tiDAVoucher (oiPostingVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then Return.
        
        /* ================================================================================================================================= */
        /* Set a class data-item that holds the comma-separated list with all the posting-ids of the DA-postings created from method         */
        /* AdditionalUpdatesAllCreateCA. This data-itam will be used in StopExternalInstances to remove these postings from the BJE-instance */
        /* ================================================================================================================================= */
        assign vcListDAPostingsCreatedInAddUpd = string(viPostingId).
                
        if tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTE}
        then assign vdDebitTC  = tDInvoice.DInvoiceOriginalCreditTC
                    vdDebitLC  = tDInvoice.DInvoiceOriginalCreditLC
                    vdDebitCC  = tDInvoice.DInvoiceOriginalCreditCC
                    vdCreditTC = 0
                    vdCreditLC = 0
                    vdCreditCC = 0.
        else if tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
             then assign vdDebitTC  = tDInvoice.DInvoiceBalanceDebitTC + tDInvoice.DInvoiceBalanceCreditTC /* amount is negative */
                         vdDebitLC  = tDInvoice.DInvoiceBalanceDebitLC + tDInvoice.DInvoiceBalanceCreditLC
                         vdDebitCC  = tDInvoice.DInvoiceBalanceDebitCC + tDInvoice.DInvoiceBalanceCreditCC
                         vdCreditTC = 0
                         vdCreditLC = 0
                         vdCreditCC = 0.
             else assign vdDebitTC  = 0
                         vdDebitLC  = 0
                         vdDebitCC  = 0
                         vdCreditTC = tDInvoice.DInvoiceBalanceDebitTC - tDInvoice.DInvoiceBalanceCreditTC
                         vdCreditLC = tDInvoice.DInvoiceBalanceDebitLC - tDInvoice.DInvoiceBalanceCreditLC
                         vdCreditCC = tDInvoice.DInvoiceBalanceDebitCC - tDInvoice.DInvoiceBalanceCreditCC.
              
        empty temp-table tDefaultSafsDI.
        <M-13 run AddControlPosting
           (input  vcPostingHeaderRowId (icPostingtcRowid), 
            input  tDInvoice.tcControlGLCode (icGLCode), 
            input  tDInvoice.tcDivisionCode (icDivisionCode), 
            input  vcDBusinessRelationInterCoCode (icIntercoBusinessRelationCode), 
            input  tDInvoice.tcCurrencyCode (icCurrencyCode), 
            input  vdDebitTC (idDebitTC), 
            input  vdDebitLC (idDebitLC), 
            input  vdDebitCC (idDebitCC), 
            input  vdCreditTC (idCreditTC), 
            input  vdCreditLC (idCreditLC), 
            input  vdCreditCC (idCreditCC), 
            input  tDInvoice.DInvoiceDIText (icText), 
            input  tDInvoice.Debtor_ID (iiMasterId), 
            input  tDInvoice.DInvoice_ID (iiTransactionId), 
            input  tDInvoice.DInvoiceExchangeRate (idExchangeRate), 
            input  tDInvoice.DInvoiceRateScale (idExchangeRateScale), 
            input  tDInvoice.DInvoiceCCRate (idPostingLineCCRate), 
            input  tDInvoice.DInvoiceCCScale (idPostingLineCCScale), 
            input  '':U (icAllocationKey), 
            input  tDInvoice.tcCostCentreCode (icCostCentreCode), 
            input  tDInvoice.tcProjectCode (icProjectCode), 
            input  tDefaultSafsDI (tDefaultSafs), 
            output viCreditNoteCntrlPostingLineID (oiPostingLineId), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then Return.
        
        /* ==================================================================== */
        /* Create a DInvoiceMovement for Credit Note (creditor control account) */
        /* and update the balance of the DI                                     */
        /* ==================================================================== */
        if not can-find(first tDInvoiceMovement where
                              tDInvoiceMovement.tc_ParentRowid = tDInvoice.tc_Rowid and
                              tDInvoiceMovement.PostingLine_ID = viCreditNoteCntrlPostingLineID)
        then do:
            <M-14 run AddDetailLine (input  'DInvoiceMovement':U (icTable), 
                                    input  tDInvoice.tc_Rowid (icParentRowid), 
                                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.
            assign tDInvoiceMovement.DInvoice_ID          = tDInvoice.DInvoice_ID
                   tDInvoiceMovement.PostingLine_Id       = viCreditNoteCntrlPostingLineID
                   tDInvoiceMovement.DInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT}.
            /* ======================================================================================== */
            /* Update balance fields; if the originalbalance-Credit was filled then all updates of      */
            /* the balance will be on the credit fields allthough the DInvoice record can have linked   */ 
            /* posting-lines with an amount filled on the debit-side of the posting.                    */
            /* CuccBal  NewBal  Posting (=tDIMov)   BalanceChange                                       */
            /* 100Cr    80Cr    20Db                -20Cr                                               */
            /* 100Cr    110Cr   10Cr                10Cr                                                */
            /* 100Db    70Db    30Cr                -30Db                                               */
            /* 100Db    140Db   40Db                40Db                                                */
            /* ======================================================================================== */

            if tDInvoice.DInvoiceBalanceDebitTC <> ? and tDInvoice.DInvoiceBalanceDebitTC <> 0 
            then assign tDInvoice.DInvoiceBalanceDebitTC = tDInvoice.DInvoiceBalanceDebitTC + vdDebitTC - vdCreditTC
                        tDInvoice.DInvoiceBalanceDebitLC = tDInvoice.DInvoiceBalanceDebitLC + vdDebitLC - vdCreditLC
                        tDInvoice.DInvoiceBalanceDebitCC = tDInvoice.DInvoiceBalanceDebitCC + vdDebitCC - vdCreditCC.
            if tDInvoice.DInvoiceBalanceCreditTC <> ? and tDInvoice.DInvoiceBalanceCreditTC <> 0
            then assign tDInvoice.DInvoiceBalanceCreditTC = tDInvoice.DInvoiceBalanceCreditTC - vdDebitTC + vdCreditTC
                        tDInvoice.DInvoiceBalanceCreditLC = tDInvoice.DInvoiceBalanceCreditLC - vdDebitLC + vdCreditLC
                        tDInvoice.DInvoiceBalanceCreditCC = tDInvoice.DInvoiceBalanceCreditCC - vdDebitCC + vdCreditCC.
            assign tDInvoice.DInvoiceBalanceTC = tDInvoice.DInvoiceBalanceDebitTC - tDInvoice.DInvoiceBalanceCreditTC
                   tDInvoice.DInvoiceBalanceLC = tDInvoice.DInvoiceBalanceDebitLC - tDInvoice.DInvoiceBalanceCreditLC
                   tDInvoice.DInvoiceBalanceCC = tDInvoice.DInvoiceBalanceDebitCC - tDInvoice.DInvoiceBalanceCreditCC
                   tDInvoice.DInvoiceIsOpen    = (tDInvoice.DInvoiceBalanceTC <> 0 or tDInvoice.DInvoiceBalanceLC <> 0 or tDInvoice.DInvoiceBalanceCC <> 0)
                   tDInvoice.tc_Status         = (if tDInvoice.tc_Status = "":U then "C":U else tDInvoice.tc_Status).
        end. /* if not can-find */


        /* ======================================================== */
        /* Call a submethod that will                               */
        /* - do a DataLoad for the Linked DI                        */
        /* - make a tDInvoiceMovement for the Linked DI             */
        /* - make the corresponding posting-line for the Linked DI  */
        /* ======================================================== */
        <M-18 run AdditionalUpdatesAllCreateDALinkedDI
           (input  vcPostingHeaderRowId (icPostingTcRowid), 
            input-output vlStartDInvoiceByIDForBusRel (blStartDIByIDForBusinessRel), 
            input-output vlStartDIMovByDInvoiceId (blStartDIMovByDInvoiceID), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper <> 0 
        then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 
        then Return.
        
    end. /* for each tDInvoice where */

    if vlStartDebtorForReasonBusRel
    then do:
        <Q-25 run DebtorForReasonBusRel  (Stop) in BDebtor >
    end.
    if vlStartDInvoiceByIDForBusRel
    then do:
        <Q-26 run DinvoiceByIDForBusinessRel  (Stop) in BDInvoice >
    end.
    if vlStartDIMovByDInvoiceID
    then do:
        <Q-27 run DInvoiceMovementByDInvoiceID  (Stop) in BDInvoice >
    end.

    /* ===================== */        
    /* Set ReturnStatus = OK */
    /* ===================== */
    if oiReturnStatus = -98
    then assign oiReturnStatus = 0.