project QadFinancials > class BDInvoice > method AdditionalUpdatesAllDebtorUpdates

Description

This submethod of AdditionalUpdatesAll handles all actions on the update fo the debtor-data based upon the invoices.


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.AdditionalUpdatesAll


program code (program8/bdinvoice.p)

/* ===================================================================================== */
/* General info: this method will check whether some fields in debtor need to be updated */
/* The communicationto BDebtor is done via a call to method ApiUpdateDebtorPaymentSales  */
/* in BDebtor - input for the method is a temp-table                                     */
/* - The LastPaymentDate of the Debtor needs to be updated only when there is new        */
/*   movement that refers to a posting in journal-type 'BankEntry' or 'DebtorPayment'    */
/* - The DaysLate and NbrOfInvoicesPais of the Debtor needs to be updated only in case   */
/*   the LastPaymentDate gets updated and the Invoice-status is set from open to closed  */
/* - The LastSaleDate of the Debtor needs to be updated only when there is new invoice   */
/*   that has at least one DInvocieOptInfo of type 'SalesOrder'                          */
/* ===================================================================================== */

/* ========================== */
/* Set default return-status  */
/* ========================== */
assign oiReturnStatus = -98.

/* ======================================================================================= */
/* Empty temp-table that is the base for the communication to BDebtor                      */
/* ======================================================================================= */
empty temp-table tUpdateDebtorPaymentSalesDI.

/* ======================================================================================= */
/* 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-20'Internal error: unexpected situation-no communication to the postings available':255(17891)t-20#) + chr(10) +
                            trim(substitute(#T-21'Instance ID = &1.':255(17892)T-21#,string(viBJournalEntryDIID)))                                  + chr(10) +
                            trim(substitute(#T-22'Valid handle = &1.':255(17893)T-22#,string(valid-handle(vhBJournalEntryDIInst) = TRUE)))          + chr(10) +
                            trim(substitute(#T-23'Handle value = &1.':255(17894)T-23#,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-6068':U (icFcMsgNumber), 
                 input  '':U (icFcExplanation), 
                 input  '':U (icFcIdentification), 
                 input  '':U (icFcContext), 
                 output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    Return.
end. /* if */

/* ===================================================================================== */  
/* Go through the instance and evaluate the conditions that trigger an update to BDebtor */
/* - The LastPaymentDate of the Debtor needs to be updated only when there is new        */
/*   movement that refers to a posting in journal-type 'BankEntry' or 'DebtorPayment'    */
/* - The DaysLate and NbrOfInvoicesPais of the Debtor needs to be updated only in case   */
/*   the LastPaymentDate gets updated and the Invoice-status is set from open to closed  */
/* - The LastSaleDate of the Debtor needs to be updated only when there is new invoice   */
/*   that has at least one DInvocieOptInfo of type 'SalesOrder'                          */
/* ===================================================================================== */
/* The fields in the temp-table that are unknown will not be taken into account to       */
/* update the debtor-data. In other words, the fields in the temp-table are only filled  */
/* in case they realy need to be updated in the debtor-table                             */
/* ===================================================================================== */
for each tDInvoice where 
         tDInvoice.tc_Status <> "D":U and
         tDInvoice.Debtor_ID <> ? and
         tDInvoice.Debtor_ID <> 0
         no-lock :

    /* =================================================================================================== */
    /* Get the Debtor to know the debtor currency                                                          */
    /* =================================================================================================== */  
    <Q-18 run DebtorForUpdate (all) (Read) (Cache)
       (input viCompanyId, (CompanyId)
        input tDInvoice.Debtor_ID, (DebtorID)
        input '':U, (DebtorCode)
        output dataset tqDebtorForUpdate) in BDebtor>                      
        
    find first tqDebtorForUpdate where
               tqDebtorForUpdate.tiDebtor_ID = tDInvoice.Debtor_ID no-error. 
    if not available tqDebtorForUpdate
    then next.

    /* ======================================================================================== */
    /* 1- Code-block that triggers the update of the DebtorLastSale and DebtorSalesOrderBalance */
    /* ======================================================================================== */
    if tDInvoice.tc_Status = "N":U and
       can-find (first tDInvoiceOpInfo where 
                       tDInvoiceOpInfo.tc_Status          <> "D":U              and 
                       tDInvoiceOpInfo.tc_ParentRowid      = tDInvoice.tc_Rowid and 
                       tDInvoiceOpInfo.DInvoiceOpInfoType  = "SALESORDER":U) /* hard-coded value as this information comes from MfgPro */
    then do :
                            
        /* =================================================================================================== */
        /* Get exhange rate between debtor and sales order                                                     */
        /* =================================================================================================== */                       
        if tDInvoice.tcCurrencyCode = tqDebtorForUpdate.tcCurrencyCode                
        then assign vdRate      = 1
                    vdRateScale = 1.
        else do :
            <M-62 run GetExchangeRate
               (input  viCompanyId (iiCompanyID), 
                input  ? (iiFromCurrencyID), 
                input  tDInvoice.tcCurrencyCode (icFromCurrencyCode), 
                input  tqDebtorForUpdate.tiCurrency_ID (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                input  today (itValidityDate), 
                output vdRate (odExchangeRate), 
                output vdRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper <  0 then return.
        end.
        
        /* ============================================================================================================ */
        /* Create a tUpdateDebtorPaymentSalesDI when it not yet exists and set the LastSale field in it                 */
        /* ============================================================================================================ */
        find tUpdateDebtorPaymentSalesDI where 
             tUpdateDebtorPaymentSalesDI.tiDebtorID = tDInvoice.Debtor_ID
             no-lock no-error.
             
        if not available tUpdateDebtorPaymentSalesDI
        then do : 
        
            create tUpdateDebtorPaymentSalesDI.
            assign tUpdateDebtorPaymentSalesDI.tiDebtorID            = tDInvoice.Debtor_ID
                   tUpdateDebtorPaymentSalesDI.tiDebtorDaysLate      = ?
                   tUpdateDebtorPaymentSalesDI.tiDebtorNbrOfInvoices = ?
                   tUpdateDebtorPaymentSalesDI.ttDebtorLastPayment   = ?
                   tUpdateDebtorPaymentSalesDI.tcInvoiceCurrency     = ?                           
                   tUpdateDebtorPaymentSalesDI.ttDebtorLastSale      = ?
                   tUpdateDebtorPaymentSalesDI.tdInvoicetotal        = ?.
                   
        end. /* if not available tUpdateDebtorPaymentSalesDI */
        
        if (tDInvoice.DInvoiceDate > tqDebtorForUpdate.ttDebtorLastSale or tqDebtorForUpdate.ttDebtorLastSale = ?) and
           (tDInvoice.DInvoiceDate > tUpdateDebtorPaymentSalesDI.ttDebtorLastSale or tUpdateDebtorPaymentSalesDI.ttDebtorLastSale = ?)
        then assign tUpdateDebtorPaymentSalesDI.ttDebtorLastSale = tDInvoice.DInvoiceDate.
        
        /* If Customer Currency = Base Currency then pick exchange rate from Invoice */
        if tqDebtorforUpdate.tcCurrencyCode = vcCompanyLC
        then assign vdRate      = tDInvoice.DInvoiceExchangeRate
                    vdRateScale = tDInvoice.DInvoiceRateScale.
        
        assign tUpdateDebtorPaymentSalesDI.tdInvoicetotal = 
                       (tDInvoice.DInvoiceOriginalDebitTC - tDInvoice.DInvoiceOriginalCreditTC) * vdRate * vdRateScale.
                       
        if vdRate <> 1 or vdRateScale <> 1
        then do :
            assign tUpdateDebtorPaymentSalesDI.tdInvoicetotal =
                       <M-29 RoundAmount
                          (input  tUpdateDebtorPaymentSalesDI.tdInvoicetotal (idUnroundedAmount), 
                           input  ? (iiCurrencyID), 
                           input  tqDebtorForUpdate.tcCurrencyCode (icCurrencyCode)) in BDInvoice>.               
        end.                                                              
                                          
    end. /* if tDInvoice.tc_Status = "N":U and  */
    
    /* ============================================================================================= */
    /* 2- Code-block that triggers the update of the DebtorLastPayment NumberOfInvoicesPaid DaysLate */
    /* ============================================================================================= */
    if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or
       tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT}
    then do :
                                                        
        /* ========================================== */
        /* Go through all new movement of the invoice */
        /* ========================================== */
        for each tDInvoiceMovement where 
                 tDInvoiceMovement.tc_Status      = "N":U and 
                 tDInvoiceMovement.tc_ParentRowid = tDInvoice.tc_Rowid 
                 no-lock :
                 
            /* ============================================================= */
            /* Check the JournalType of the posting that holds this movement */
            /* temp-table filled in AdditionalUpdatesAllAssignments          */
            /* ============================================================= */
            find first tPostingPostingLineInfoDIJE where
                       tPostingPostingLineInfoDIJE.tiPostingLine_ID = tDInvoiceMovement.PostingLine_ID
                       no-error.
            if not available tPostingPostingLineInfoDIJE
            then do:
                assign vcMessage      = trim(#T-44'Internal error: unexpected situation - no posting line information available':255(999890251)T-44#) + chr(10) +
                                        trim(substitute(#T-14'Posting Line ID = &1.':255(999890249)T-14#,string(tDInvoiceMovement.PostingLine_ID)))
                       oiReturnStatus = -3.
                <M-24 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-375002':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                next.
            end.

            if tPostingPostingLineInfoDIJE.tcJournalTypeCode = {&JOURNALTYPE-BANKINGENTRY} or 
               tPostingPostingLineInfoDIJE.tcJournalTypeCode = {&JOURNALTYPE-DEBTORPAYMENT}
            then do :
            
                /* =============================================================================================== */
                /* Create a tUpdateDebtorPaymentSalesDI when it not yet exists and set the LastPayment field in it */
                /* =============================================================================================== */
                find tUpdateDebtorPaymentSalesDI where 
                     tUpdateDebtorPaymentSalesDI.tiDebtorID = tDInvoice.Debtor_ID
                     no-lock no-error.
                     
                if not available tUpdateDebtorPaymentSalesDI
                then do : 
                    create tUpdateDebtorPaymentSalesDI.
                    assign tUpdateDebtorPaymentSalesDI.tiDebtorID            = tDInvoice.Debtor_ID
                           tUpdateDebtorPaymentSalesDI.tiDebtorDaysLate      = ?
                           tUpdateDebtorPaymentSalesDI.tiDebtorNbrOfInvoices = ?
                           tUpdateDebtorPaymentSalesDI.ttDebtorLastPayment   = ?
                           tUpdateDebtorPaymentSalesDI.tcInvoiceCurrency     = ?                           
                           tUpdateDebtorPaymentSalesDI.ttDebtorLastSale      = ?
                           tUpdateDebtorPaymentSalesDI.tdInvoicetotal        = ?.
                           
                end. /* if not available tUpdateDebtorPaymentSalesDI */
            
                if tPostingPostingLineInfoDIJE.ttPostingDate <> ? and 
                   (tPostingPostingLineInfoDIJE.ttPostingDate > tqDebtorForUpdate.ttDebtorLastPayment or tqDebtorForUpdate.ttDebtorLastPayment = ?) and
                   (tUpdateDebtorPaymentSalesDI.ttDebtorLastPayment = ? or 
                    tUpdateDebtorPaymentSalesDI.ttDebtorLastPayment < tPostingPostingLineInfoDIJE.ttPostingDate)
                then assign tUpdateDebtorPaymentSalesDI.ttDebtorLastPayment = tPostingPostingLineInfoDIJE.ttPostingDate. 
                                
                /* ============================================================================================================ */
                /* Update the NumberOfInvoicesPaid and the DaysLate in case the Invoice is finished and was not finished before */
                /* ============================================================================================================ */
                if tDInvoice.DInvoiceIsOpen = false and 
                   (tDInvoice.tc_Status = "N":U or 
                    (tDInvoice.tc_Status = "C":U and 
                     can-find (t_iDInvoice where 
                               t_iDInvoice.tc_Rowid       = tDInvoice.tc_Rowid and 
                               t_iDInvoice.DInvoiceIsOpen = true)))
                then do :
                    assign tUpdateDebtorPaymentSalesDI.tiDebtorNbrOfInvoices = (if tUpdateDebtorPaymentSalesDI.tiDebtorNbrOfInvoices = ?
                                                                                then 1
                                                                                else tUpdateDebtorPaymentSalesDI.tiDebtorNbrOfInvoices + 1).
                                                                                
                    if tPostingPostingLineInfoDIJE.ttPostingDate <> ? and 
                       tPostingPostingLineInfoDIJE.ttPostingDate > tDInvoice.DInvoiceDueDate
                    then assign tUpdateDebtorPaymentSalesDI.tiDebtorDaysLate = 
                                                  (if tUpdateDebtorPaymentSalesDI.tiDebtorDaysLate = ?
                                                   then 0 
                                                   else tUpdateDebtorPaymentSalesDI.tiDebtorDaysLate) + 
                                                  tPostingPostingLineInfoDIJE.ttPostingDate - tDInvoice.DInvoiceDueDate.
                                                  
                end. /* if tDInvoice.DInvoiceIsOpen = false and  */
                
            end. /* if tPostingPostingLineInfoDIJE.tcJournalTypeCode = {&JOURNALTYPE-BANKINGENTRY} or  */ 
            
        end. /* for each tDInvoiceMovement where */
        
    end. /* if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} */
    
end. /* for each tDInvoice where tDInvoice.tc_Status <> "D":U, each tDInvoiceMovement where */

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