project QadFinancials > class BDInvoice > method AdditionalUpdatesAllCreateDALinkedDI

Description

This submethod of AdditionalUpdatesAllCreateDA will create the postinglines and movement-records for the linked DI-record


Parameters


icPostingTcRowidinputcharacterTC Row ID of the Posting
blStartDIByIDForBusinessRelinput-outputlogicalIs query tqDInvoiceByODForBusinessRel already started or not ?
blStartDIMovByDInvoiceIDinput-outputlogicalIs query DInvoiceMovementByDInvoiceID already started or not ?
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.AdditionalUpdatesAllCreateDA


program code (program8/bdinvoice.p)

    
    /* =========================================================================================== */
    /* Pre- and post-requisitions:                                                                 */
    /* - tCInvoice points to the originating CI                                                    */
    /* =========================================================================================== */
    
    /* ========================== */
    /* Set default return-status  */
    /* ========================== */
    assign oiReturnStatus = -98.
    
    /* ======================================================================================= */
    /* 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-17'Internal error: unexpected situation-no communication to the postings available':255(17891)T-17#) + chr(10) +
                                trim(substitute(#T-18'Instance ID = &1.':255(17892)T-18#,string(viBJournalEntryDIID)))                                  + chr(10) +
                                trim(substitute(#T-19'Valid handle = &1.':255(17893)T-19#,string(valid-handle(vhBJournalEntryDIInst) = TRUE)))          + chr(10) +
                                trim(substitute(#T-20'Handle value = &1.':255(17894)T-20#,string(vhBJournalEntryDIInst)))
               oiReturnStatus = -3.
        <M-16 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-4524':U (icFcMsgNumber), 
                     input  '':U (icFcExplanation), 
                     input  '':U (icFcIdentification), 
                     input  '':U (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        Return.
    end. /* if */
    
    /* ========================================================================= */
    /* Get the BusinessRelation and IntercoCode of the Debtor of the Linked DI   */
    /* ========================================================================= */
    if not blStartDIByIDForBusinessRel
    then do:
        <Q-37 run DinvoiceByIDForBusinessRel  (Start) in BDInvoice >
        assign blStartDIByIDForBusinessRel = true.
    end.

    <Q-33 run DinvoiceByIDForBusinessRel (all) (Read) (NoCache)
       (input viCompanyID, (CompanyId)
        input tDInvoice.LinkedDInvoice_ID, (DinvoiceID)
        output dataset tqDinvoiceByIDForBusinessRel) in BDInvoice >
       
    find first tqDinvoiceByIDForBusinessRel 
        where tqDinvoiceByIDForBusinessRel.tiDinvoice_ID = tDInvoice.LinkedDInvoice_ID no-error.
    if not available tqDinvoiceByIDForBusinessRel
    then do:
        assign vcMessage      =      #T-36'The system encountered an error when retrieving the linked invoice.':255(65735)T-36#
               oiReturnStatus = -1.
        <M-34 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-7443':U (icFcMsgNumber),
                         input  '' (icFcExplanation),
                         input  '' (icFcIdentification),
                         input  '' (icFcContext),
                         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        Return.
    end.
   
    assign vcDBusinessRelationInterCoCode = tqDinvoiceByIDForBusinessRel.tcBusinessRelationICCode.
    
    if not blStartDIMovByDInvoiceID
    then do:
        <Q-38 run DInvoiceMovementByDInvoiceID  (Start) in BDInvoice >
        assign blStartDIMovByDInvoiceID = true.
    end.

    <Q-30 run DInvoiceMovementByDInvoiceID (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input tqDinvoiceByIDForBusinessRel.tiDInvoice_ID, (DInvoiceId)
        output dataset tqDInvoiceMovementByDInvoiceID) in BDInvoice >
    find first tqDInvoiceMovementByDInvoiceID where
               tqDInvoiceMovementByDInvoiceID.tiDInvoice_ID          = tqDinvoiceByIDForBusinessRel.tiDInvoice_ID and
               tqDInvoiceMovementByDInvoiceID.tcDInvoiceMovementType = {&MOVEMENTTYPE-INITIAL}
               no-error.
    if not available tqDInvoiceMovementByDInvoiceID
    then do:
        assign vcMessage      =  trim(#T-32'The customer control account for the linked invoice is not defined in the system.':150(49392)T-32#)
               oiReturnStatus = -1.
        <M-31 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-6422':U (icFcMsgNumber),
                         input  '' (icFcExplanation),
                         input  '' (icFcIdentification),
                         input  '' (icFcContext),
                         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        Return.
    end.

    /* ============================================================================================== */
    /* PostingLine based on the Linked CI                                                             */
    /* Posting on creditor control account or credit note / first search the creditor control account */
    /* Add control posting for the creditor control account of the credit note/invoice                */
    /* ============================================================================================== */
    if tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTE}
    then assign vdCreditTC = tDInvoice.DInvoiceOriginalCreditTC
                vdCreditLC = tDInvoice.DInvoiceOriginalCreditLC
                vdCreditCC = tDInvoice.DInvoiceOriginalCreditCC
                vdDebitTC  = 0
                vdDebitLC  = 0
                vdDebitCC  = 0.
    else if tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
         then assign vdCreditTC = tDInvoice.DInvoiceOriginalDebitTC + tDInvoice.DInvoiceOriginalCreditTC /* amount is negative */
                     vdCreditLC = tDInvoice.DInvoiceOriginalDebitLC + tDInvoice.DInvoiceOriginalCreditLC
                     vdCreditCC = tDInvoice.DInvoiceOriginalDebitCC + tDInvoice.DInvoiceOriginalCreditCC
                     vdDebitTC  = 0
                     vdDebitLC  = 0
                     vdDebitCC  = 0.
         else assign vdCreditTC = 0
                     vdCreditLC = 0
                     vdCreditCC = 0
                     vdDebitTC  = tDInvoice.DInvoiceOriginalDebitTC - tDInvoice.DInvoiceOriginalCreditTC
                     vdDebitLC  = tDInvoice.DInvoiceOriginalDebitLC - tDInvoice.DInvoiceOriginalCreditLC
                     vdDebitCC  = tDInvoice.DInvoiceOriginalDebitCC - tDInvoice.DInvoiceOriginalCreditCC.   

    empty temp-table tDefaultSafsDI.
    <M-5 run AddControlPosting
       (input  vcPostingHeaderRowId (icPostingtcRowid), 
        input  tqDInvoiceMovementByDInvoiceID.tcGLCode (icGLCode), 
        input  tqDinvoiceByIDForBusinessRel.tcDivisionCode (icDivisionCode), 
        input  vcDBusinessRelationInterCoCode (icIntercoBusinessRelationCode), 
        input  tqDinvoiceByIDForBusinessRel.tcCurrencyCode (icCurrencyCode), 
        input  vdDebitTC (idDebitTC), 
        input  vdDebitLC (idDebitLC), 
        input  vdDebitCC (idDebitCC), 
        input  vdCreditTC (idCreditTC), 
        input  vdCreditLC (idCreditLC), 
        input  vdCreditCC (idCreditCC), 
        input  tqDinvoiceByIDForBusinessRel.tcDInvoiceDIText (icText), 
        input  tqDinvoiceByIDForBusinessRel.tiDebtor_ID (iiMasterId), 
        input  tqDinvoiceByIDForBusinessRel.tiDInvoice_ID (iiTransactionId), 
        input  tDInvoice.DInvoiceExchangeRate (idExchangeRate), 
        input  tDInvoice.DInvoiceRateScale (idExchangeRateScale), 
        input  tDInvoice.DInvoiceCCRate (idPostingLineCCRate), 
        input  tDInvoice.DInvoiceCCScale (idPostingLineCCScale), 
        input  '':U (icAllocationKey), 
        input  tqDinvoiceByIDForBusinessRel.tcCostCentreCode (icCostCentreCode), 
        input  tqDinvoiceByIDForBusinessRel.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 Linked DI and update the balance of the DI */
    /* ======================================================================== */
    if not can-find(first tDInvoiceMovement where
                          tDInvoiceMovement.DInvoice_ID    = tqDinvoiceByIDForBusinessRel.tiDInvoice_ID and
                          tDInvoiceMovement.PostingLine_ID = viCreditNoteCntrlPostingLineID)
    then do :
        /* ======================================================================================== */
        /* Update balance fields; if the originalbalance-Credit was filled then all updates of      */
        /* the balance will be on the credit fields allthough the CInvoice record can have linked   */ 
        /* posting-lines with an amount filled on the debit-side of the posting.                    */
        /* ======================================================================================== */
        find first tDInvoice where 
                   tDInvoice.DInvoice_ID = tqDinvoiceByIDForBusinessRel.tiDInvoice_ID
                   no-error.
        if not available tDInvoice
        then do :
            /* If we do a DataLoad here then vlFcDataValidated will be set to false again. */
            /* As we only want to create a movement on the linked invoice and update its balance and amount to pay and as all info for doing so has already been validated, we consider it to be okay to savethe linked invoice without validating that invoice */
            /* To avoid we get error 'BLF-308: Data to save () has not been validated", we will manually assign vlFcDataValidated to true after the DataLoad if it was already true before the DataLoad */
            assign vlPreviousFcDataValidated = vlFcDataValidated.
            <M-21 run DataLoad
               (input  '':U (icRowids), 
                input  string(tqDinvoiceByIDForBusinessRel.tiDInvoice_ID) (icPkeys), 
                input  '':U (icObjectIds), 
                input  '' (icFreeform), 
                input  yes (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if vlPreviousFcDataValidated = true
            then assign vlFcDataValidated = true.
            if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then do:
                assign vcMessage = trim(substitute(#T-26'An internal error occurred on the customer adjustments. The data load method failed. The customer invoice (customer invoice ID = &1) cannot be modified.':255(17928)T-26#,string(tqDinvoiceByIDForBusinessRel.tiDInvoice_ID))).
                <M-22 run SetMessage (input  vcMessage (icMessage), 
                      input  '':U (icArguments), 
                      input  '':U (icFieldName), 
                      input  '':U (icFieldValue), 
                      input  'S':U (icType), 
                      input  2 (iiSeverity), 
                      input  '':U (icRowid), 
                      input  'QADFIN-4526':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                assign oiReturnStatus = viFcReturnSuper.
                return.
            end. /* if viFcReturnSuper < 0 */
        end. /* if not available tDInvoice */
        find first tDInvoice where tDInvoice.DInvoice_ID = tqDinvoiceByIDForBusinessRel.tiDInvoice_ID no-error.
        if not available tDInvoice
        then do:
            assign vcMessage = trim(substitute(#T-27'An internal error occurred on the customer adjustments. The data load method did not load the expected invoice. The customer invoice (customer invoice ID = &1) cannot be modified.':255(17929)T-27#,string(tqDinvoiceByIDForBusinessRel.tiDInvoice_ID))).
            <M-23 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-4527':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            assign oiReturnStatus = -1.
            return.
        end. /* if not available tDInvoice */
        /* ======================================================================== */
        /* Create a DInvoiceMovement for Linked DI and update the balance of the DI */
        /* ======================================================================== */
        <M-28 run AddDetailLine (input  'DInvoiceMovement':U (icTable), 
                             input  tDInvoice.tc_Rowid (icParentRowid), 
                             output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper <> 0 
        then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then Return.
        assign tDInvoiceMovement.DInvoice_ID          = tqDinvoiceByIDForBusinessRel.tiDInvoice_ID
               tDInvoiceMovement.PostingLine_Id       = viCreditNoteCntrlPostingLineID
               tDInvoiceMovement.DInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT}.

        <M-29 run UpdateDInvoiceBankToPayTC (input  tDInvoice.DInvoice_ID (iiDInvoiceId), 
                                     input  tDInvoice.DInvoiceBalanceDebitTC (idOldBalanceDebitTC), 
                                     input  tDInvoice.DInvoiceBalanceCreditTC (idOldBalanceCreditTC), 
                                     input  vdDebitTC (idMovementAmountDebitTC), 
                                     input  vdCreditTC (idMovementAmountCreditTC), 
                                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then Return.

        /* ======================================================================================== */
        /* 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 - vdCreditTC + vdDebitTC
                    tDInvoice.DInvoiceBalanceDebitLC = tDInvoice.DInvoiceBalanceDebitLC - vdCreditLC + vdDebitLC
                    tDInvoice.DInvoiceBalanceDebitCC = tDInvoice.DInvoiceBalanceDebitCC - vdCreditCC + vdDebitCC.
        if tDInvoice.DInvoiceBalanceCreditTC <> ? and tDInvoice.DInvoiceBalanceCreditTC <> 0
        then assign tDInvoice.DInvoiceBalanceCreditTC = tDInvoice.DInvoiceBalanceCreditTC + vdCreditTC - vdDebitTC
                    tDInvoice.DInvoiceBalanceCreditLC = tDInvoice.DInvoiceBalanceCreditLC + vdCreditLC - vdDebitLC
                    tDInvoice.DInvoiceBalanceCreditCC = tDInvoice.DInvoiceBalanceCreditCC + vdCreditCC - vdDebitCC.
        assign tDInvoice.DInvoiceBalanceCC = tDInvoice.DInvoiceBalanceDebitCC - tDInvoice.DInvoiceBalanceCreditCC
               tDInvoice.DInvoiceBalanceLC = tDInvoice.DInvoiceBalanceDebitLC - tDInvoice.DInvoiceBalanceCreditLC
               tDInvoice.DInvoiceBalanceTC = tDInvoice.DInvoiceBalanceDebitTC - tDInvoice.DInvoiceBalanceCreditTC
               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(first tDInvoiceMovement where */
    
    /* ========================== */
    /* Set return-status = OK     */
    /* ========================== */
    if oiReturnStatus = -98
    then assign oiReturnStatus = 0.