project QadFinancials > class BDDocument > method AdditionalUpdatesPosting


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDDocument.AdditionalUpdates


program code (program5/bddocument.p)

assign vlStart1  = false
       vlStart2  = false
       vlStart3  = false
       vlStart4  = false
       vlStart6  = false
       vlStart7  = false
       vlStart8  = false
       vlStart9  = false.

empty temp-table tApiDPayDDoc.
empty temp-table tDCollectionDDoc.
empty temp-table tDIMovementDDoc.
empty temp-table tMoveSuspTaxDDoc.

for each tDDocument where
         tDDocument.tc_Status <> "D":U:

    empty temp-table tDivisionDDoc.

    /* New, initial documents need no extra updates */
    if tDDocument.tc_Status = "N":U and 
       tDDocument.DDocumentStatus = {&DOCUMENTSTATUS-INIT}
    then next.

    /* Changed, but still initial documents need no extra updates */
    if tDDocument.tc_Status = "C":U and
       tDDocument.DDocumentStatus = {&DOCUMENTSTATUS-INIT} and
       can-find(first t_iDDocument where
                      t_iDDocument.tc_Rowid        = tDDocument.tc_Rowid and
                      t_iDDocument.DDocumentStatus = {&DOCUMENTSTATUS-INIT})
    then next.

    /* Unchanged documents without changed allocations need no extra updates */
    if tDDocument.tc_Status = "":U and
       not can-find(first tDDocumentInvoiceXref where
                          tDDocumentInvoiceXref.tc_ParentRowid = tDDocument.tc_Rowid and
                          tDDocumentInvoiceXref.tc_Status     <> "":U)
    then next.

    /* Changed documents with unchanged status and without changed allocations need no extra updates */
    if tDDocument.tc_Status = "C":U                                              and
       can-find(first t_iDDocument where
                      t_iDDocument.tc_Rowid        = tDDocument.tc_Rowid and
                      t_iDDocument.DDocumentStatus = tDDocument.DDocumentStatus and
                      t_iDDocument.BankNumber_ID   = tDDocument.BankNumber_ID ) and
       not can-find(first tDDocumentInvoiceXref where
                          tDDocumentInvoiceXref.tc_ParentRowid = tDDocument.tc_Rowid and
                          tDDocumentInvoiceXref.tc_Status     <> "":U)
    then next.

    /* Get Initial Values */
    if tDDocument.tc_Status <> "N":U
    then do:
        find t_iDDocument where
             t_iDDocument.tc_Rowid = tDDocument.tc_Rowid
             no-error.

        if not available t_iDDocument
        then do:
            <M-2 run SetMessage
               (input  trim(#T-51'Initial values not found for customer payment with row ID $1.':250(1653)T-51#) (icMessage), 
                input  tDDocument.tc_Rowid (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QADFIN-2588':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>

            assign oiReturnStatus = -1.
            leave.
        end.
    end.

    /* Get a valid period for the Document */
    if not vlStart3
    then do:
        <Q-3 run PeriodByStartEndDate (Start) in BPeriod >
        assign vlStart3 = true.
    end.

    <Q-4 run PeriodByStartEndDate (all) (Read) (NoCache)
          (input tDDocument.Company_ID, (CompanyId)
           input tDDocument.ttPostingDate, (Date)
           output dataset tqPeriodByStartEndDate) in BPeriod >

    find first tqPeriodByStartEndDate where
               tqPeriodByStartEndDate.ttPeriodStartDate         <= tDDocument.ttPostingDate and
               tqPeriodByStartEndDate.ttPeriodEndDate           >= tDDocument.ttPostingDate and
               tqPeriodByStartEndDate.tcPeriodStatus             = {&PERIODSTATUS-OPEN}     and
               tqPeriodByStartEndDate.tlPeriodIsPostingGLAllowed = true
               no-error.

    if not available tqPeriodByStartEndDate
    then do:
        <M-5 run SetMessage
           (input  trim(#T-52'No GL period is defined for date $1.':250(933)T-52#) (icMessage), 
            input  string(tDDocument.ttPostingDate) (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tDDocument.tc_Rowid (icRowid), 
            input  'QADFIN-2589':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>

        assign oiReturnStatus = -1.
        leave.
    end.

    /* Get Debtor Info */
    <M-6 run AdditionalUpdatesGetDebtorInfo
       (input-output vlStart4 (blStart4), 
        output viFcReturnSuper (oiReturnStatus)) in BDDocument>
    if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then leave.

    /* Start Journal Entry component */
    if not vlStart1
    then do:
        if viBJournalEntryDDocID = 0 or
           viBJournalEntryDDocID = ?
        then do:
            <I-7 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "yes"
            &CLASS              = "BJournalEntry"}>

            assign vlBJournalEntryDDocWasStartedHe = true.
        end.
        else do:
            <I-8 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>
        end.

        assign vlStart1 = true.
    end.

    /* Construct text which will be used for all postings */
    if tDDocument.tcPostingText = "":U or
       tDDocument.tcPostingText = ?
    then assign tDDocument.tcPostingText = 
                    substitute("&1 &2/&3":U, <M-73 GetDDocumentTypeTranslation  (input  tDDocument.DDocumentType (icType)) in BDDocument>,
                                             string(tDDocument.DDocumentYear, "9999":U),
                                             string(tDDocument.DDocumentNumber, "999999999":U)).

    /* Add Posting Header if PostingRowid is still empty (which means we didn't come from BankEntry) */
    if tDDocument.tcPostingRowId = "":U
    then do:
        /* Get Journal for the posting */
        if tDDocument.DDocumentStatus = {&DOCUMENTSTATUS-INIT} or
           tDDocument.DDocumentStatus = {&DOCUMENTSTATUS-BOUNCED}
        then do:
            <M-9 run AdditionalUpdatesGetStatusInfo
               (input  t_iDDocument.DDocumentType (icDDocumentType), 
                input  t_iDDocument.DDocumentStatus (icDDocumentStatus), 
                input  t_iDDocument.tcGLCode (icBankGLCode), 
                input-output vlStart2 (blStart2), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        end.
        else do:
            <M-10 run AdditionalUpdatesGetStatusInfo
               (input  tDDocument.DDocumentType (icDDocumentType), 
                input  tDDocument.DDocumentStatus (icDDocumentStatus), 
                input  tDDocument.tcGLCode (icBankGLCode), 
                input-output vlStart2 (blStart2), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        end.

        if viFcReturnSuper < 0 or
           oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.

        if oiReturnStatus < 0
        then leave.

        assign vcJournalCode = tDDocumentStatusInfo.tcJournalCode.
        <M-12 run AddPostingHeader
           (input  tDDocument.Company_ID (iiCompanyId), 
            input  tqPeriodByStartEndDate.tiPeriodYear (iiPeriodYear), 
            input  tqPeriodByStartEndDate.tiPeriodPeriod (iiPeriodPeriod), 
            input  vcJournalCode (icJournalCode), 
            input  '' (icReportingJournalCode), 
            input  0 (iiVoucher), 
            input  tDDocument.ttPostingDate (itPostingDate), 
            input  ? (itValueDate), 
            input  tDDocument.tcPostingText (icPostingText), 
            input  '':U (icPostingBusinessRelationText), 
            input  '':U (icPostingInvoiceReferenceText), 
            input  '':U (icPostingParentText), 
            input  0 (iiBPeriodId), 
            input  tDDocument.tcDebtorCode (icPostingOriginAddressCode), 
            input  string(tDDocument.DDocumentYear,'9999':U) + '/':U + string(tDDocument.DDocumentNumber, '999999999':U) (icPostingOriginDocument), 
            input  ? (icPostingOriginDocumentType), 
            input  '':U (icBatchNumber), 
            input  tDDocument.DDocumentBankImpRef (icBankImpLineRef), 
            output viPostingId (oiPostingId), 
            output tDDocument.tcPostingRowId (ocRowid), 
            output viPostingVoucher (oiPostingVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.

        assign vcPaymentReferenceDDoc = trim(string(tqPeriodByStartEndDate.tiPeriodYear)) + "/":U + trim(vcJournalCode) + "/":U + string(viPostingVoucher,"999999999":U).
        
        /* necessary for paysel, when there is only one posting */
        
        for each BDDocument where
                 BDDocument.tcPostingRowId = "":U and BDDocument.DPaySel_ID > 0:
            assign BDDocument.tcPostingRowId = tDDocument.tcPostingRowId.
        end.
        
        if oiReturnStatus < 0
        then leave.
    end.

    /* Create Movements on Invoices and create Postings on Discount Accounts */
    /* Create PrePayments                                                    */
    <M-64 run AdditionalUpdatesPostingXref
       (input  tDDocument.tcPostingRowId (icPostingRowid), 
        input  tqPeriodByStartEndDate.tiPeriodYear (iiPeriodYear), 
        input  tqPeriodByStartEndDate.tiPeriodPeriod (iiPeriodPeriod), 
        input  vcJournalCode (icJournalCode), 
        input  viPostingId (iiPostingId), 
        input-output vlStart6 (blStart6), 
        input-output vlStart7 (blStart7), 
        input-output vlStart8 (blStart8), 
        input  viPostingVoucher (iiPostingVoucher), 
        output viFcReturnSuper (oiReturnStatus)) in BDDocument>
    if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then leave.

    /* Calculate Payment document Exchange Rate */
    <M-17 run AdditionalUpdatesPostingDocExchangeRate  (output viFcReturnSuper (oiReturnStatus)) in BDDocument>
    if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then leave.

    /* Create Postings on Document Control accounts */
    if tDDocument.tc_Status        = "N":U or
       tDDocument.DDocumentStatus <> t_iDDocument.DDocumentStatus or
       tDDocument.BankNumber_ID   <> t_iDDocument.BankNumber_ID
    then do:
        <M-50 run AdditionalUpdatesPostingDocCtrl
           (input  tDDocument.tcPostingRowId (icPostingRowid), 
            input-output vlStart2 (blStart2), 
            input-output vlStart7 (blStart7), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then leave.
    end.
      
    /* Make necessary updates to DCollection */
    <M-15 run AdditionalUpdatesDCollection
       (input-output vlStart6 (blStart6), 
        input-output vlStart9 (blStart9), 
        output viFcReturnSuper (oiReturnStatus)) in BDDocument>

    if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then leave.
end.

for each tDDocument where
         tDDocument.tc_Status <> "D":U :
    
    /* IF PAYMENT SELECTION CODE HAS BEEN CHANGED */
    if can-find(first t_iDDocument where
                      t_iDDocument.tc_Rowid                    = tDDocument.tc_Rowid and
                      t_iDDocument.DDocumentIncassoSelectCode  <> tDDocument.DDocumentIncassoSelectCode)
    then do:
        if tDDocument.tc_Status <> "N":U
        then do:
            find t_iDDocument where
                 t_iDDocument.tc_Rowid = tDDocument.tc_Rowid
                 no-error.
    
            if not available t_iDDocument
            then do:
                assign vcMessage = trim(#T-85'Initial values not found for customer payment with row ID $1.':250(1653)T-85#).
                <M-59 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  tDDocument.tc_Rowid (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-780268':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDDocument>
    
                assign oiReturnStatus = -1.
                leave.
            end.
        end.

        <M-65 run AdditionalUpdatesDCollectionCopy
           (input-output vlStart6 (blStart6), 
            input-output vlStart9 (blStart9), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then leave.
    end.
end.

/* close the instance outside the loop otherwise it will cause issue when handle more than 1 payment */
if viBJournalEntryDDocID <> 0 and
   viBJournalEntryDDocID <> ?
then.   /* ========================================================================================================= */
        /* Normally we would here close BJournalEntry but we will not do it for performance                          */
        /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
        /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
        /* appserver that is executed after each call to the appserver                                               */
        /* ========================================================================================================= */

/* Create/Update Collections */
if oiReturnStatus >= 0 and
   can-find(first tDCollectionDDoc)
then DCOLLECTION-BLOCK: do:
    if viBDCollectionDDocID = 0 or
       viBDCollectionDDocID = ?
    then do:
        <I-27 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "yes"
            &CLASS              = "BDCollection"}>

        assign vlBDCollectionDDocWasStartedHer = true.
    end.
    else do:
        <I-28 {bFcOpenInstance
            &CLASS           = "BDCollection"}>
    end.

    <M-29 run UpdateDCollection
       (input-output tDCollectionDDoc (tDCollectionsToUpdate), 
        input  vlBDCollectionDDocWasStartedHer (ilClearData), 
        output viLocalReturn (oiReturnStatus)) in BDCollection>

    if viLocalReturn >= 0 and
       vlBDCollectionDDocWasStartedHer
    then do:
        <M-30 run ValidateBCAndAdditionalUpdates (output viLocalReturn (oiReturnStatus)) in BDCollection>
    end.
    
    /* ========================================================================================================= */
    /* Normally we would here close BDCollection but we will not do it for performance                           */
    /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
    /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
    /* appserver that is executed after each call to the appserver                                               */
    /* ========================================================================================================= */
    
    if viLocalReturn  < 0 or oiReturnStatus = 0 then assign oiReturnStatus = viLocalReturn.
    if oiReturnStatus < 0 then leave DCOLLECTION-BLOCK.

    /* Add necessary documents to the newly created collections */
    for each tDCollectionDDoc:
        for each tDDocument where
                 tDDocument.DCollection_ID   = 0                                              and
                (tDDocument.DDocumentStatus  = {&DOCUMENTSTATUS-INCASSO}                      or
                 tDDocument.DDocumentStatus  = {&DOCUMENTSTATUS-DISCONT})                     and
                 tDDocument.DDocumentType    = entry(1, tDCollectionDDoc.tcReference, chr(2)) and
                 tDDocument.DDocumentSubType = entry(2, tDCollectionDDoc.tcReference, chr(2)) and
                 tDDocument.tcGLCode         = tDCollectionDDoc.tcGLCode                      and
                 tDDocument.tc_Status       <> "D":U:
            assign tDDocument.DCollection_ID = tDCollectionDDoc.DCollection_ID.
        end.
    end.
end.

/* Create DInvoiceMovements */
if oiReturnStatus >= 0 and
   (can-find(first tDIMovementDDoc) or can-find(first tMoveSuspTaxDDoc))
then DMOVEMENT-BLOCK: do:
    if not vlStart8
    then do:
        if viBDInvoiceDDocID = 0 or
           viBDInvoiceDDocID = ?
        then do:
            <I-32 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "yes"
            &CLASS              = "BDInvoice"}>

            assign vlBDInvoiceDDocWasStartedHere = true.
        end.
        else do:
            <I-33 {bFcOpenInstance
            &CLASS           = "BDInvoice"}>
        end.

        assign vlStart8 = true.
    end.
    
    
    find first tDDocument no-error.
    if can-find(first tDIMovementDDoc)
    then do:

        <M-34 run CreateDInvoiceMovements
           (input-output tDIMovementDDoc (tDIMovement), 
            input  tDDocInvoiceXrefStage (tDInvoiceStageUpdates), 
            input-output viBJournalEntryDDocID (biBJournalEntryId), 
            input  false (ilClearData), 
            input  if available tDDocument then tDDocument.ttPostingDate else ? (itPaymentTaxPointDate), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then leave DMOVEMENT-BLOCK.
    end.

    if can-find(first tMoveSuspTaxDDoc)
    then do:
        <M-53 run MoveSuspendedTax
           (input  viBJournalEntryDDocID (iiBJournalEntryID), 
            input  tMoveSuspTaxDDoc (tMoveSuspendedTax), 
            input  false (ilInstanceBJEAlreadyOpened), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then leave DMOVEMENT-BLOCK.
    end.
end.

/* Create Exchange Rate rounding posting line if needed */
if vlStart1                        = true and
   vlBJournalEntryDDocWasStartedHe = true and
   oiReturnStatus >=0
then do:
    if viBJournalEntryDDocID <> 0 and viBJournalEntryDDocID <> ?
    then do:
         <I-60 {bFcOpenInstance
             &CLASS           = "BJournalEntry"}>
    end.
    for each tDDocument where
             tDDocument.tc_Status <> 'D':U and
             tDDocument.tcPostingRowId <> '':U:
        <M-62 run AdditionalUpdatesPostingRounding
           (input  tDDocument.tcPostingRowId (icPostingRowId), 
            output viLocalReturn (oiReturnStatus)) in BDDocument>
        if viLocalReturn < 0 or oiReturnStatus = 0 then assign oiReturnStatus = viLocalReturn.
        if oiReturnStatus < 0 then leave.
    end.
    /* ========================================================================================================= */
    /* Normally we would here close BJournalEntry but we will not do it for performance                          */
    /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
    /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
    /* appserver that is executed after each call to the appserver                                               */
    /* ========================================================================================================= */    
end.

/* Create DPay */
if oiReturnStatus >= 0 and
   can-find(first tApiDPayDDoc)
then DPAY-BLOCK: do:
    if viBDPayDDocID = 0 or
       viBDPayDDocID = ?
    then do:
        <I-35 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "yes"
            &CLASS              = "BDPay"}>

        assign vlBDPayDDocWasStartedHere = true.
    end.
    else do:
        <I-36 {bFcOpenInstance
            &CLASS           = "BDPay"}>
    end.

    <M-37 run ApiCreateDPayRecords
       (input  tApiDPayDDoc (tAPIDPay), 
        input  vlBDPayDDocWasStartedHere (ilClearData), 
        output viLocalReturn (oiReturnStatus)) in BDPay>
    
    /* ========================================================================================================= */
    /* Normally we would here close BDPay but we will not do it for performance                                  */
    /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
    /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
    /* appserver that is executed after each call to the appserver                                               */
    /* ========================================================================================================= */
        
    if viLocalReturn  < 0 or oiReturnStatus = 0 then assign oiReturnStatus = viLocalReturn.
    if oiReturnStatus < 0 then leave DPAY-BLOCK.
end.

/* Close started components/queries */
if vlStart1
then do:
    if viBJournalEntryDDocID <> 0 and
           viBJournalEntryDDocID <> ?
    then do:
        <I-55 {bFcOpenInstance
             &CLASS           = "BJournalEntry"}>
    end.
    if oiReturnStatus >= 0 and
       vlBJournalEntryDDocWasStartedHe
    then do:
        <M-39 run ValidateBCAndAdditionalUpdates (output viLocalReturn (oiReturnStatus)) in BJournalEntry>
        if viLocalReturn  < 0 or oiReturnStatus = 0 then assign oiReturnStatus = viLocalReturn.
    end.
    /* ========================================================================================================= */
    /* Normally we would here close BJournalEntry but we will not do it for performance                          */
    /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
    /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
    /* appserver that is executed after each call to the appserver                                               */
    /* ========================================================================================================= */    
end.

if vlStart2
then do:
    <Q-49 run DDocumentStatusByTypeStatusGL (Stop) in BDDocumentStatus >
end.

if vlStart3
then do:
    <Q-42 run PeriodByStartEndDate (Stop) in BPeriod >
end.

if vlStart4
then do:
    <Q-43 run DebtorForDivision
       (Stop) in BDebtor >
end.

if vlStart6
then do:
    <Q-44 run GLByBankInfo (Stop) in BGL >
end.

if vlStart7
then do:
    <Q-45 run DInvoiceByID (Stop) in BDInvoice >
end.

if vlStart8
then do:
    if oiReturnStatus >= 0 and
       vlBDInvoiceDDocWasStartedHere
    then do:
        <M-46 run ValidateBCAndAdditionalUpdates (output viLocalReturn (oiReturnStatus)) in BDInvoice>
        if viLocalReturn  < 0 or oiReturnStatus = 0 then assign oiReturnStatus = viLocalReturn.
    end.
    /* ========================================================================================================= */
    /* Normally we would here close BDInvoice but we will not do it for performance                              */
    /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
    /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
    /* appserver that is executed after each call to the appserver                                               */
    /* ========================================================================================================= */
end.