project QadFinancials > class BPaymentSelection > method AdditionalUpdatesRegister

Description

Register Payment Selection


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPaymentSelection.AdditionalUpdates


program code (program5/bpaymentselection.p)

/* Hold translated values of CREDIT/DEBIT in the class data item (for performance). */
if vcCreditTR = "":U then assign vcCreditTR = {&CREDITDEBITABBREVIATION-CREDIT-TR}.

empty temp-table tCDocumentPaySel.
empty temp-table tCDocumentInvoiceXRefPaySel.
empty temp-table tOpenBalancePrepayment.

assign viCDocSeq = 0
       viCDocXRefSeq = 0
       vlStart1 = false
       vlStart2 = false
       vlStart3 = false
       vlStart4 = false
       vlStart5 = false
       vtCDocumentDueDate = ?
       vtRequestedDate = today
       vlSkipDueDtVal = false
       viPaySelLineExecuteNbr = 0.

for each tPaySel where
         tPaySel.tc_Status <> "D":U:  
      <M-45 run GetPaymentInstrument
            (input  tPaySel.BankPayFormat_ID (iiBankPayFormatID), 
             output vcPaymentInstrument (ocPaymentInstrument), 
             output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
        
    /* Assign due date. */
    assign vtCDocumentDueDate   = if vtCDocumentDueDate = ? then tPaySel.PaySelDate
                                      else max(vtCDocumentDueDate, tPaySel.PaySelDate).
                                 
    /* Update PaySelstatus for last group. */
    if viLastInvGroupNbr = 1 then do:
    
        assign tPaySel.PaySelStatus = if vcPaymentInstrument = {&DOCUMENTTYPECREDITOR-ELECTRONIC}
                                      then {&PAYMENTSELECTIONSTATUS-REGISTERED}
                                      else {&PAYMENTSELECTIONSTATUS-TRANSFERRED}.
    end.
    /* Update PaySelstatus as 'PARTIALY CONFIRMED' after first group updating. */
    else do:
        assign tPaySel.PaySelStatus = {&PAYMENTSELECTIONSTATUS-PARTIALLY}.
    end.

    if tPaySel.tc_Status = "":U then assign tPaySel.tc_Status = "C":U.
    for each tPaySelLine where
             tPaySelLine.tc_ParentRowid = tPaySel.tc_Rowid and
             tPaySelLine.tc_Status     <> "D":U:
             
        /* Skip the PaySelLine which is not belong to current invoice group. */
        <M-54 run IsValidPaySelIDInGroup
           (input  tPaySelLine.PaySelLine_ID (iiCurrentPaySelLineID), 
            output vlIsInCurrentInvoiceBatch (olIsValidPaySelLine), 
            input  no (ilChangeStatus), 
            output oiReturnStatus (oiReturnStatus)) in BPaymentSelection>             
                       
        if vlIsInCurrentInvoiceBatch = no then
        do:
          next.
        end.

        if tPaySelLine.tc_status = "":U  then assign tPaySelLine.tc_Status = "C":U.
        assign tPaySelLine.PaySelLineIsConfirmed = yes
               viPaySelLineExecuteNbr = viPaySelLineExecuteNbr  + 1.
    end.
end.


/* ========================================================= */
/* Find out if there are mutliple lines for the same invocie */
/* ========================================================= */
for each btPaySelLine where 
         btPaySelLine.tc_Status <> "D":U
     group by btPaySelLine.PaySelLineParentObject_ID:
     
    if first-of(btPaySelLine.PaySelLineParentObject_ID)
    then assign vdCumulatedInvoiceMovement = 0.
    
    assign btPaySelLine.tdOpenInvoiceAmountTC = btPaySelLine.tdOpenInvoiceAmountTC
                                              - vdCumulatedInvoiceMovement 
           vdCumulatedInvoiceMovement         = vdCumulatedInvoiceMovement 
                                              + btPaySelLine.PaySelLineAmountTC 
                                              + btPaySelLine.PaySelLineDiscountTC
                                              + btPaySelLine.PaySelLineWHTAmtTC.
end. /* for each btPaySelLine where */



<M-46 run ValidateCDocumentDueDate
   (input  vtCDocumentDueDate (itCDocumentDueDate), 
    output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>

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

if oiReturnStatus < 0
then return.

for each tPaysel where tPaySel.tc_Status <> "D":U:
    if tPaySel.PaySelDate < vtRequestedDate
    then assign vtRequestedDate = tPaySel.PaySelDate.
    
    <M-34 run GetInvWithCommonPayAttributes
       (input  tPaySel.tc_Rowid (icPaySelRowID), 
        output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
    then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then return.

    for each tPaySelLine where
             tPaySelLine.tc_ParentRowid = tPaySel.tc_Rowid and tPaySelLine.tc_Status <> "D":U
             break by tPaySelLine.tcCreditorCode
                   by tPaySelLine.tcInvoiceCurrencyCode
                   by tPaySelLine.BankNumber_ID
                   by tPaySelLine.PayFormatTypeCode:
  
        /* Skip PaySelLine which is not belong to current invoice group. */
        <M-23 run IsValidPaySelIDInGroup
           (input  tPaySelLine.PaySelLine_ID (iiCurrentPaySelLineID), 
            output vlIsInCurrentInvoiceBatch (olIsValidPaySelLine), 
            input  yes (ilChangeStatus), 
            output oiReturnStatus (oiReturnStatus)) in BPaymentSelection>             
       
        if vlIsInCurrentInvoiceBatch = no then
        do:
            next.
        end.
        
        /* If it is the first payment selection line of the batch, set vlFirstPaySelLineInBatch to 'true'. */
        if viPaymentSelLineNum = 0 then
        do:
            viPaymentSelLineNum = viPaymentSelLineNum + 1.
            vlFirstPaySelLineInBatch = true.
        end.
        else
            vlFirstPaySelLineInBatch = false.
                       
        if first-of(tPaySelLine.tcCreditorCode)
        then do:
            assign vcNormalPaymentConditionCode = "":U
                   vcDivisionCode               = "":U
                   vcCostCentreCode             = "":U
                   vcProjectCode                = "":U.

            if can-find(first bPaySelLine where
                              bPaySelLine.tc_ParentRowid       = tPaySel.tc_Rowid                   and
                              bPaySelLine.tcCreditorCode       = tPaySelLine.tcCreditorcode         and
                              bPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-PREPAYMENT} and
                              bPaySelLine.tc_Status           <> "D":U)
            then do:
                if not vlStart1
                then do:
                     <Q-47 run CreditorByCreditorCodeID  (Start) in BCreditor >
                    assign vlStart1 = true.
                end.

                <Q-48 run CreditorByCreditorCodeID (all) (Read) (NoCache)
                   (input ?, (CreditorId)
                    input tPaySel.Company_ID, (CompanyId)
                    input tPaySelLine.tcCreditorCode, (CreditorCode)
                    output dataset tqCreditorByCreditorCodeID) in BCreditor >

                for first tqCreditorByCreditorCodeID where tqCreditorByCreditorCodeID.tcCreditorCode = tPaySelLine.tccreditorcode:
                    assign vcNormalPaymentConditionCode = tqCreditorByCreditorCodeID.tcPaymentConditionCode.

                    /* Tempotary solution.  Division_ID, CostCentre_ID, and Project_ID needs to be
                     * added to table PaySelPre to properly store these info for prepayment. */
                    assign vhFcComponent = ?.

                    <M-49 run GetDefaultCCProjDivForControlGL
                       (input  tqCreditorByCreditorCodeID.tiPrePayControlGLProfile_ID (iiControlGLProfileId), 
                        input  tqCreditorByCreditorCodeID.tiDivisionProfile_ID (iiDivisionProfileId), 
                        output vcGLCode (ocGLCode), 
                        output vcCostCentreCode (ocCostCentreCode), 
                        output vcProjectCode (ocProjectCode), 
                        output vcDivisionCode (ocDivisionCode), 
                        output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                end.
            end.
        end.

        find first tMatchingInvoiceGroup no-error.
        if available tMatchingInvoiceGroup 
        then do:
            find first tCInvoicePaymentAttributes 
                where tCInvoicePaymentAttributes.tiPaymentSelectionLineID = tPaySelLine.PaySelLine_ID no-error.
            if available  tCInvoicePaymentAttributes
            then do:
                assign vlCDocCreatedForInvGroup = no.
            end.
            else do:
                find first tMatchingInvoices 
                    where tMatchingInvoices.tiGroupSeq = tMatchingInvoiceGroup.tiGroupSeq  
                      and tMatchingInvoices.tiPaySelLineID = tPaySelLine.PaySelLine_ID no-error.
                if available  tMatchingInvoices 
                then assign vlCDocCreatedForInvGroup = no.
                else assign vlCDocCreatedForInvGroup = yes.
            end.
        end.
        else do:
            assign vlCDocCreatedForInvGroup = yes.
        end.
              
        for first tMatchingInvoices 
            where tMatchingInvoices.tiPaySelLineID = tPaySelLine.PaySelLine_ID,
            first tMatchingInvoiceGroup 
            where tMatchingInvoiceGroup.tiGroupSeq = tMatchingInvoices.tiGroupSeq
              and tMatchingInvoiceGroup.tlCDocCreated = yes:                                  
            
            assign vlCDocCreatedForInvGroup = yes.
            find tCDocumentPaySel where tCDocumentPaySel.tc_Rowid = tMatchingInvoiceGroup.tcCDocRowID no-error.
        end.

        /* The variable vlCreateCDocumentPaySel is to decide whether to create a document for payment selection line. */
        vlCreateCDocumentPaySel = first-of(tPaySelLine.PayFormatTypeCode) or vlFirstPaySelLineInBatch
                                  or vlCDocCreatedForInvGroup = no or vlMultiInvoiceBank. 
            
        if not tPaySel.tlDocumentPerDueDate
        then do:
            if vlCreateCDocumentPaySel
            then do:            
                create tCDocumentPaySel.
                assign viCDocSeq                                    = viCDocSeq + 1
                       tCDocumentPaySel.CCollection_ID              = 0
                       tCDocumentPaySel.CDocumentCreationDate       = today
                       tCDocumentPaySel.CDocumentDueDate            = vtCDocumentDueDate
                       tCDocumentPaySel.CDocumentNumber             = 0
                       tCDocumentPaySel.CDocumentReference          = "":U
                       tCDocumentPaySel.CDocumentStatus             = if tPaySel.tlCreditDirectlyOnBank then {&DOCUMENTSTATUS-PAID}
                                                                      else {&DOCUMENTSTATUS-INCASSO}
                       tCDocumentPaySel.CDocumentSubType            = (if vcPaymentInstrument = {&PAYFORMATPAYINSTRUMENT-ELECTRONIC} 
                                                                       then {&DOCUMENTSUBTYPE-AUTO} 
                                                                       else {&DOCUMENTSUBTYPE-MAN})
                       tCDocumentPaySel.CDocumentType               = vcPaymentInstrument
                       tCDocumentPaySel.CDocumentYear               = year(today)
                       tCDocumentPaySel.CDocument_ID                = 0
                       tCDocumentPaySel.Company_ID                  = tPaySel.Company_ID
                       tCDocumentPaySel.PaySel_ID                   = tPaySel.PaySel_ID
                       tCDocumentPaySel.tcCreditorCode              = tPaySelLine.tcCreditorCode
                       tCDocumentPaySel.tcCurrencyCode              = tPaySelLine.tcInvoiceCurrencyCode
                       tCDocumentPaySel.tcGLCode                    = tPaySel.tcGLCode
                       tCDocumentPaySel.tcPaySelCode                = tPaySel.PaySelCode
                       tCDocumentPaySel.tc_Rowid                    = string(viCDocSeq)
                       tCDocumentPaySel.tc_Status                   = "N":U
                       tCDocumentPaySel.ttPostingDate               = if tPaySel.ttPostingDate = ?
                                                                      then tPaySel.PaySelDate
                                                                      else tPaySel.ttPostingDate
                       tCDocumentPaySel.BankNumber_ID               = tPaySelLine.BankNumber_ID
                       tCDocumentPaySel.tlOwnBankIsBankChargeEnable = tPaySel.tlBankNumberIsBankCharge
                       tCDocumentPaySel.tiOwnBankNumberId           = tPaySel.tiOwnBankNumberId
                       tCDocumentPaySel.tcPayFormatTypeCode         = tPaySel.tcPayFormatTypeCode
                       tCDocumentPaySel.ttBankChargePostingDate     = (if vcPaymentInstrument = {&PAYFORMATPAYINSTRUMENT-ELECTRONIC} 
                                                                       then tPaySel.PaySelDate
                                                                       else tCDocumentPaySel.ttPostingDate).
            end.   
        end.
        else do:       
          assign vlSkipDueDtVal = tPaySel.tlDocumentPerDueDate.
             <M-51 run AdditionalUpdatesRegister1
                (input  tPaySelLine.ttDueDate (itCDocumentDueDate), 
                 input  vcPaymentInstrument (icPaymentInstrument), 
                 input  vlCDocCreatedForInvGroup (ilCDocCreated), 
                 input-output viCDocSeq (biCDocSeq), 
                 input  vlCreateCDocumentPaySel (ilCreateCDocumentPaySel), 
                 output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
        end.     
        
        for first tMatchingInvoices 
            where tMatchingInvoices.tiPaySelLineID = tPaySelLine.PaySelLine_ID,
            first tMatchingInvoiceGroup 
            where tMatchingInvoiceGroup.tiGroupSeq = tMatchingInvoices.tiGroupSeq
              and tMatchingInvoiceGroup.tlCDocCreated = no:     
              
             assign 
                tMatchingInvoiceGroup.tlCDocCreated = yes
                tMatchingInvoiceGroup.tcCDocRowID = tCDocumentPaySel.tc_Rowid.

        end.  
        
        create tCDocumentInvoiceXRefPaySel.

        assign tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTAmtTC     = <M-53 RoundAmount
                                                                                 (input  tPaySelLine.PaySelLineWHTAmtTC (idUnroundedAmount), 
                                                                                  input  ? (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTAmtLC     = <M-74 RoundAmount
                                                                                 (input  tPaySelLine.PaySelLineWHTAmtLC (idUnroundedAmount), 
                                                                                  input  ? (iiCurrencyID), 
                                                                                  input  vcCompanyLC (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTAmtCC     = <M-44 RoundAmount
                                                                                 (input  tPaySelLine.PaySelLineWHTAmtCC (idUnroundedAmount), 
                                                                                  input  ? (iiCurrencyID), 
                                                                                  input  vcCompanyCC (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTTxFeeTC   = <M-70 RoundAmount
                                                                                 (input  tPaySelLine.tdWHTTaxableFeeTC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTPerc      = tPaySelLine.tdWHTPerc                                                                                     
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTCCRate    = tPaySelLine.tdWHTTCCCExchangeRate
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTCCScale   = tPaySelLine.tdWHTTCCCRateScale   
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTLCRate    = tPaySelLine.tdWHTTCLCExchangeRate
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefWHTCCScale   = tPaySelLine.tdWHTTCLCRateScale   
               tCDocumentInvoiceXRefPaySel.tdCInvoiceOriginalTC             = <M-634 RoundAmount
                                                                                 (input  tPaySelLine.tdCInvoiceOriginalTC (idUnroundedAmount), 
                                                                                  input  viCompanyId (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.tdCInvoiceWHTUnpaidAmtTC         = <M-6 RoundAmount
                                                                                 (input  tPaySelLine.tdCInvoiceWHTUnpaidAmtTC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.tdCInvoiceTotWHTTC               = <M-97 RoundAmount
                                                                                 (input  tPaySelLine.tdCInvoiceTotWHTTC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.tdCInvoiceWHTAmtTC               = <M-21 RoundAmount
                                                                                 (input  tPaySelLine.tdCInvoiceWHTAmtTC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.tdTotWHTAmtLC                    = <M-63 RoundAmount
                                                                                 (input  tPaySelLine.tdTotWHTAmtLC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.tdTotWHTAmtTC                    = <M-10 RoundAmount
                                                                                 (input  tPaySelLine.tdTotWHTAmtTC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.tdCInvoiceTotWHTBaseTC           = <M-64 RoundAmount
                                                                                 (input  tPaySelLine.tdCInvoiceTotWHTBaseTC (idUnroundedAmount), 
                                                                                  input  0 (iiCurrencyID), 
                                                                                  input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>.        



        /* Invoices and invoice corrections reacts in the same way, because we don't show negative amounts. */
               assign viCDocXRefSeq                                      = viCDocXRefSeq + 1
               tCDocumentInvoiceXrefPaySel.CDocumentInvoiceXrefAlloTC    = tPaySelLine.PaySelLineAmountTC
                                                                         + tPaySelLine.PaySelLineDiscountTC
                                                                         + tPaySelLine.PaySelLineWHTAmtTC
                                                                         - tPaySelLine.PaySelLineIntAmtTC
		 tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXRefPaidAmtTC = tPaySelLine.PaySelLineAmountTC
               tCDocumentInvoiceXrefPaySel.CDocumentInvoiceXrefDiscTC    = tPaySelLine.PaySelLineDiscountTC
               tCDocumentInvoiceXrefPaySel.CDocumentInvoiceXrefIntTC     = <M-17 RoundAmount
                                                                              (input  tPaySelLine.PaySelLineIntAmtTC (idUnroundedAmount), 
                                                                               input  0 (iiCurrencyID), 
                                                                               input  tPaySelLine.tcInvoiceCurrencyCode (icCurrencyCode)) in BPaymentSelection>
               tCDocumentInvoiceXRefPaySel.CInvoice_ID                 = if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-DEBTORCREDITNOTE} or
                                                                            tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-DEBTORCNCORR}
                                                                         then ?
                                                                         else tPaySelLine.PaySelLineParentObject_ID
               tCDocumentInvoiceXRefPaySel.DInvoice_ID                 = if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-DEBTORCREDITNOTE} or
                                                                            tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-DEBTORCNCORR}
                                                                         then tPaySelLine.PaySelLineParentObject_ID
                                                                         else ?
               tCDocumentInvoiceXRefPaySel.tdCInvoiceBalance           = tPaySelLine.tdOpenInvoiceAmountTC
               tCDocumentInvoiceXrefPaySel.PaySelLine_ID               = tPaySelLine.PaySelLine_ID
               tCDocumentInvoiceXRefPaySel.tcAllocAmountCrDt           = tPaySelLine.tcPaymentCrDt
               tCDocumentInvoiceXRefPaySel.tcCInvoiceBalanceCrDt       = tPaySelLine.tcInvoiceCrDt
               tCDocumentInvoiceXrefPaySel.tcCurrencyCode              = tPaySelLine.tcInvoiceCurrencyCode
               tCDocumentInvoiceXRefPaySel.tcDivisionCode              = if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-PREPAYMENT}
                                                                         then vcDivisionCode
                                                                         else tPaySelLine.tcDivisionCode
               tCDocumentInvoiceXRefPaySel.tc_ParentRowid              = tCDocumentPaySel.tc_Rowid
               tCDocumentInvoiceXrefPaySel.tc_Rowid                    = string(viCDocXRefSeq)
               tCDocumentInvoiceXrefPaySel.tc_Status                   = 'N':U
               tCDocumentInvoiceXRefPaySel.tcCInvoiceType              = if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-INVOICE}
                                                                         then {&INVOICETYPE-INVOICE}
                                                                         else if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-INVOICECORR}
                                                                              then {&INVOICETYPE-INVOICECORRECTION}
                                                                              else if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-CREDITNOTE}
                                                                                   then {&INVOICETYPE-CREDITNOTE}
                                                                                   else if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-CREDITNOTECORR}
                                                                                        then {&INVOICETYPE-CREDITNOTECORRECTION}
                                                                                        else if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-ADJUSTMENT}
                                                                                             then {&INVOICETYPE-ADJUSTMENT}
                                                                                             else {&INVOICETYPE-PREPAYMENT}
               tCDocumentInvoiceXRefPaySel.ttCInvoiceDate              = tPaySelLine.ttInvoiceDate
               tCDocumentPaySel.CDocumentOriginalCreditTC              = tCDocumentPaySel.CDocumentOriginalCreditTC
                                                                       + (if tPaySelLine.tcPaymentCrDt = vcCreditTR
                                                                          then - tPaySelLine.PaySelLineAmountTC
                                                                          else tPaySelLine.PaySelLineAmountTC)
               tCDocumentInvoiceXRefPaySel.tiBankNumber_ID             = tPaySelLine.BankNumber_ID
               tCDocumentInvoiceXRefPaySel.CDocumentInvoiceXrefObjectType = if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-PREPAYMENT}
                                                                            then {&CDOCUMENTTYPE-NEWPREPAYMENT}
                                                                            else tCDocumentInvoiceXRefPaySel.tcCInvoiceType.
                                                                         

        assign viCount = 0.  

        for each tPaySelLineStage where tPaySelLineStage.PaySelLine_ID = tPaySelLine.PaySelLine_ID:
          
          
            create tCDocumentXrefPaySelStage.
                                    
            assign
                viCount = viCount + 1
                tCDocumentXrefPaySelStage.tc_ParentRowid                 = tCDocumentInvoiceXrefPaySel.tc_Rowid
                tCDocumentXrefPaySelStage.tc_Rowid                       = tCDocumentXrefPaySelStage.tc_ParentRowid + "-":U + string(viCount)
                tCDocumentXrefPaySelStage.CInvoiceStage_ID               = tPaySelLineStage.CInvoiceStage_ID
                tCDocumentXrefPaySelStage.CDocInvoiceXrefStageAlloTC     = (tPaySelLineStage.PaySelLineStagePaymAmntTC
                                                                            - tPaySelLineStage.PaySelLineStageIntAmtTC )
                                                                           + tPaySelLineStage.PaySelLineStageDiscAmntTC
                                                                           + tPaySelLineStage.PaySelLineStageWHTAmtTC
                tCDocumentXrefPaySelStage.CDocInvoiceXrefStagePaidAmtTC  = tPaySelLineStage.PaySelLineStagePaymAmntTC 
                tCDocumentXrefPaySelStage.CDocInvoiceXrefStageDiscTC     = tPaySelLineStage.PaySelLineStageDiscAmntTC
                tCDocumentXrefPaySelStage.CDocInvoiceXrefStageIntTC      = tPaySelLineStage.PaySelLineStageIntAmtTC
                tCDocumentXrefPaySelStage.tdDefaultPaymentAmountTC       = tPaySelLineStage.PaySelLineStagePaymAmntTC
                tCDocumentXrefPaySelStage.tdDefaultDiscountAmountTC      = tPaySelLineStage.PaySelLineStageDiscAmntTC
                tCDocumentXrefPaySelStage.tiCInvoice_ID                  = tPaySelLine.PaySelLineParentObject_ID.                            
        end.                     

        if not vlStart3
        then do:
            <Q-38 run CInvoicePostingByCInvoiceId
               (Start) in BCInvoice >
            assign vlStart3 = true.
        end.
        
        <Q-35 run CInvoicePostingByCInvoiceId (all) (Read) (NoCache)
           (input tPaySelLine.PaySelLineParentObject_ID, (CInvoiceId)
            input  {&INVOICEPOSTINGTYPE-INITIAL}, (PostingType)
            output dataset tqCInvoicePostingByCInvoiceId) in BCInvoice >
        find first tqCInvoicePostingByCInvoiceId where
                   tqCInvoicePostingByCInvoiceId.tiCInvoice_ID         = tPaySelLine.PaySelLineParentObject_ID and
                   tqCInvoicePostingByCInvoiceId.tcCInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL}
                   no-error.
        if available tqCInvoicePostingByCInvoiceId
        then assign tCDocumentInvoiceXRefPaySel.tiCInvoicePostingId = tqCInvoicePostingByCInvoiceId.tiPosting_ID.
        
        assign tCDocumentInvoiceXRefPaySel.tlDiscountTaxAtPayment = false.
        if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-DEBTORCREDITNOTE} or
            tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-DEBTORCNCORR}
        then do:
            if not vlStart4 then do:
                <Q-40 run DInvoiceVatByDInvoiceId  (Start) in BDInvoice >
                assign vlStart4 = true.
            end.
            
            <Q-32 run DInvoiceVatByDInvoiceId (all) (Read) (NoCache)
               (input tPaySelLine.PaySelLineParentObject_ID, (DInvoiceId)
                output dataset tqDInvoiceVatByDInvoiceId) in BDInvoice >
            for each tqDInvoiceVatByDInvoiceId where
                     tqDInvoiceVatByDInvoiceId.tiDInvoice_ID = tPaySelLine.PaySelLineParentObject_ID :
                 <Q-33 assign vlFcQueryRecordsAvailable = MfgTaxCodeByTaxCode (NoCache)
                    (input tqDInvoiceVatByDInvoiceId.tcVatCode, (TaxCode)
                     input tqDInvoiceVatByDInvoiceId.tcDomainCode, (DomainCode)
                     input true, (DiscountAtPayment)
                     input ?, (IsTaxByLine)) in BMfgTaxCode >
                 if vlFcQueryRecordsAvailable = yes or vlFcQueryRecordsAvailable = ?
                 then do:
                    assign tCDocumentInvoiceXRefPaySel.tlDiscountTaxAtPayment = true.
                    leave.
                 end.
            end.
        end.
        else do:
            if not vlStart5 then do:
                <Q-41 run CInvoiceVatByCInvoiceId  (Start) in BCInvoice >
                assign vlStart5 = true.
            end.
            
            <Q-30 run CInvoiceVatByCInvoiceId (all) (Read) (NoCache)
               (input tPaySelLine.PaySelLineParentObject_ID, (CInvoiceId)
                output dataset tqCInvoiceVatByCInvoiceId) in BCInvoice >
            for each tqCInvoiceVatByCInvoiceId where
                     tqCInvoiceVatByCInvoiceId.tiCInvoice_ID = tPaySelLine.PaySelLineParentObject_ID :
                 <Q-31 assign vlFcQueryRecordsAvailable = MfgTaxCodeByTaxCode (NoCache)
                    (input tqCInvoiceVatByCInvoiceId.tcVatCode, (TaxCode)
                     input tqCInvoiceVatByCInvoiceId.tcDomainCode, (DomainCode)
                     input true, (DiscountAtPayment)
                     input ?, (IsTaxByLine)) in BMfgTaxCode >
                 if vlFcQueryRecordsAvailable = yes or vlFcQueryRecordsAvailable = ?
                 then do:
                    assign tCDocumentInvoiceXRefPaySel.tlDiscountTaxAtPayment = true.
                    leave.
                 end.
            end.
        end.
        
        if tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-PREPAYMENT}
        then do:
            <M-52 run AdditionalUpdatesRegisterPrePayment
               (input  vcDivisionCode (icDivisionCode), 
                input  vcCostCentreCode (icCostCentreCode), 
                input  vcProjectCode (icProjectCode), 
                input  vcNormalPaymentConditionCode (icNormalPaymentConditionCode), 
                input-output vlStart2 (blStart2), 
                output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
             if viFcReturnSuper < 0 or
               oiReturnStatus  = 0
             then assign oiReturnStatus = viFcReturnSuper.

             if oiReturnStatus < 0
             then return.
        end.
    end. /* For each tPaySelLine */
end. /* For each tPaySel */
   
if vlStart1
then do:
    <Q-50 run CreditorByCreditorCodeID  (Stop) in BCreditor >
end.

if vlStart2
then do:
    <Q-26 run BankNumberByID
       (Stop) in BBankNumber >
end.

if vlStart3
then do:
    <Q-39 run CInvoicePostingByCInvoiceId
       (Stop) in BCInvoice >
end.

if vlStart4
then do:
    <Q-42 run DInvoiceVatByDInvoiceId
       (Stop) in BDInvoice >
end.

if vlStart5
then do:
    <Q-43 run CInvoiceVatByCInvoiceId
       (Stop) in BCInvoice >
end.

if can-find(first tCDocumentPaySel)
then do:
    if viBCDocumentPaySelID = 0 or
       viBCDocumentPaySelID = ?
    then do:
        <I-7 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "true"
             &CLASS                = "BCDocument"}>

        assign vlBCDocIsStartedFromPaySel = true.
    end.
    else do:
        <I-8 {bFcOpenInstance
            &CLASS           = "BCDocument"}>
    end.
    
    <M-9 run CreateCDocuments
       (input  tCDocumentPaySel (tCDocumentsToCreate), 
        input  tCDocumentInvoiceXRefPaySel (tCDocumentInvoiceXrefsToCreate), 
        input  tCDocumentXrefPaySelStage (tCDocInvoiceXrefStageToCreate), 
        input  tOpenBalancePrepayment (t_sOpenBalancesToCreate), 
        input  vlBCDocIsStartedFromPaySel (ilClearData), 
        input  vtRequestedDate (itRequestedDate), 
        input  viBCInvoicePaySelID (iiBCInvoiceId), 
        input  viBDInvoicePaySelID (iiBDInvoiceId), 
        input  vlSkipDueDtVal (ilSkipDueDtVal), 
        output viReturn (oiReturnStatus)) in BCDocument>
    if viReturn < 0 or oiReturnStatus = 0
    then assign oiReturnStatus = viReturn.

    if viReturn >= 0 and
       vlBCDocIsStartedFromPaySel
    then do:
        <M-27 run ValidateBCAndAdditionalUpdates
           (output vcJustCreatedCInvoices (ocCreatedPrePaymentForPaySel), 
            output viReturn (oiReturnStatus)) in BCDocument>
    end.

    <I-15 {bFcCloseInstance
            &CLASS           = "BCDocument"}>

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

    if oiReturnStatus < 0
    then return.

    do viA = num-entries(vcJustCreatedCInvoices, "|":U) to 1 by -1:
        find first tPaySelLine where
                   tPaySelLine.PaySelLine_ID = int(entry(1, entry(viA, vcJustCreatedCInvoices, "|":U)))
                   no-error.

        if available tPaySelLine
        then assign tPaySelLine.PaySelLineParentObject_ID = int(entry(2, entry(viA, vcJustCreatedCInvoices, "|":U))).
    end.

    for each tPaySel where
             tPaySel.tc_Status <> "D":U,
        each tPaySelLine where
             tPaySelLine.tc_ParentRowid       = tPaySel.tc_Rowid                   and
             tPaySelLine.PaySelLineObjectType = {&PAYMENTSELECTIONTYPE-PREPAYMENT} and
             tPaySelLine.tc_Status           <> "D":U:
        find first tPaySelPrepayRefPS where
                   tPaySelPrepayRefPS.PaySelPrepay_ID = tPaySelLine.PaySelLineParentObject_ID
                   no-error.

        if not available tPaySelPrepayRefPS
        then do:
            assign oiReturnStatus = -1.
            leave.
        end.
    end.
end.