project QadFinancials > class BCInvoice > method CreateCInvoicesPayment

Description

This method calculates all related values based on Payment Condition code, also Payment stages are created here.


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.CreateCInvoices


program code (program3/bcinvoice.p)

/* ====================================================================================== *
 * Method       : CreateCInvoicesPayment                                                  *
 * Description  : This method updates supplier invoice about all payment details, also    *
 *                CInvoiceStage records are created here                                  *
 *                Child method of CreateCInvoices, have to be in the same segment         *
 * -------------------------------------------------------------------------------------- *
 * Parameters   :                                                                         *
 * ====================================================================================== */
 
assign oiReturnStatus = -98
       viLocalReturn  = 0.

MAIN_BLOCK:
do on error undo, return:
    /* Default output parameters */

    /* Pre-validation block */
    if not available tCInvoice
    then do:
        assign vcMessage = #T-1'Available Supplier invoice record (available(tCInvoice) = &1)':255(591256444)T-1#
               vcMessage = substitute(vcMessage, available tCInvoice).
        <M-2 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-9732':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>

        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not available */

    /* If payment condition is not defined -> do nothing */
    if (tCInvoice.NormalPaymentCondition_ID    = 0   or
        tCInvoice.NormalPaymentCondition_ID    = ?)     and
       (tCInvoice.tcNormalPaymentConditionCode = ?   or
        tCInvoice.tcNormalPaymentConditionCode = "":U)
    then leave MAIN_BLOCK.
    
    /* Get payment definition */
    <Q-3 run PaymentConditionByIdCode (all) (Read) (NoCache)
       (input tCInvoice.NormalPaymentCondition_ID, (PaymentConditionId)
        input tCInvoice.tcNormalPaymentConditionCode, (PaymentConditionCode)
        output dataset tqPaymentConditionByIdCode) in BPaymentCondition >
    
    find first tqPaymentConditionByIdCode no-error.
    if not available tqPaymentConditionByIdCode
    then do:
        assign vcMessage = #T-5'Invalid payment condition code &1':255(332037144)T-5#
               vcMessage = substitute(vcMessage, tCInvoice.tcNormalPaymentConditionCode)
               vcContext = "NormalPaymentCondition_ID=&1|NormalPaymentConditionCode=&2":U
               vcContext = replace(vcContext, "|":U, chr(2))
               vcContext = substitute(vcContext, tCInvoice.NormalPaymentCondition_ID, tCInvoice.tcNormalPaymentConditionCode). 
        
        <M-4 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-9733':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>

        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not available tqPaymentConditionByIdCode */

    /* Assign payment condition values to invoice definition */
    assign tCInvoice.NormalPaymentCondition_ID    = tqPaymentConditionByIdCode.tiPaymentCondition_ID
           tCInvoice.tcNormalPaymentConditionCode = tqPaymentConditionByIdCode.tcPaymentConditionCode
           tCInvoice.tcNormalPaymentConditionType = tqPaymentConditionByIdCode.tcPaymentConditionPaymentTyp.

    /* If Payment condition is staged, calculate also stages */
    if tCInvoice.tcNormalPaymentConditionType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
    then do:
         /* Get all stages */
         <Q-6 run PaymentConditionStagedByPC (all) (Read) (NoCache)
           (input tCInvoice.NormalPaymentCondition_ID, (ParentPaymentConditionID)
            output dataset tqPaymentConditionStagedByPC) in BPaymentCondition >

        /* Start instance of payment condition component for calculation */
        if can-find(first tqPaymentConditionStagedByPC where
                          tqPaymentConditionStagedByPC.tiParentPaymentCondition_ID = tCInvoice.NormalPaymentCondition_ID)
        then do:
            if viBPaymentConditionCIID <> 0 and
               viBPaymentConditionCIID <> ?
            then do:
                <I-7 {bFcOpenInstance
                     &CLASS           = "BPaymentCondition"}>
            end.
            else do:
                <I-8 {bFcStartAndOpenInstance
                     &ADD-TO-TRANSACTION   = "false"
                     &CLASS                = "BPaymentCondition"}>
                assign vlBPaymentConditionStartedHere = true.
            end.
        end.

        /* Do real creation of stages */
        assign vdTotalInvoiceOriginalTC = tCInvoice.tdCInvoiceOriginalTC.

        for each tqPaymentConditionStagedByPC where
                 tqPaymentConditionStagedByPC.tiParentPaymentCondition_ID = tCInvoice.NormalPaymentCondition_ID
            break by tqPaymentConditionStagedByPC.tiParentPaymentCondition_ID:

            /* Calculate stage amount */
            if last-of(tqPaymentConditionStagedByPC.tiParentPaymentCondition_ID)
            then assign vdStageAmountTC = vdTotalInvoiceOriginalTC.
            else assign vdStageAmountTC = <M-11 RoundAmount
                                             (input  tCInvoice.tdCInvoiceOriginalTC * tqPaymentConditionStagedByPC.tdPaymentConditionStagedPerc / 100 (idUnroundedAmount), 
                                              input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyID), 
                                              input  tCInvoice.tcCurrencyCode (icCurrencyCode)) in BCInvoice>.
            assign vdTotalInvoiceOriginalTC = vdTotalInvoiceOriginalTC - vdStageAmountTC.

            /* Create new CInvoiceStage record and assign all values */
            <M-10 run AddDetailLine
               (input  'CInvoiceStage':U (icTable), 
                input  tCInvoice.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 then viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper < 0  then leave MAIN_BLOCK.

            assign tCInvoiceStage.CInvoiceStageAmountTC        = vdStageAmountTC
                   tCInvoiceStage.CInvoiceStagePercent         = tqPaymentConditionStagedByPC.tdPaymentConditionStagedPerc
                   tCInvoiceStage.PaymentConditionStaged_ID    = tqPaymentConditionStagedByPC.tiPaymentConditionStaged_ID
                   tCInvoiceStage.tcPaymentConditionCode       = tqPaymentConditionStagedByPC.tcPaymentConditionCode
                   tCInvoiceStage.tcPaymentConditionDescript   = tqPaymentConditionStagedByPC.tcPaymentConditionDescript
                   tCInvoiceStage.tdPaymentConditionPercentage = tqPaymentConditionStagedByPC.tdPaymentConditionPercentage.

            /* Calculate DueDate and DiscountDueDate for staged payment */
            <M-12 run CalculateDueDiscountDateNoRead
               (input  tqPaymentConditionStagedByPC.tcPaymentConditionPeriodType (icPaymentConditionPeriodType), 
                input  tqPaymentConditionStagedByPC.ttPaymentConditionBaseDate (itPaymentConditionBaseDate), 
                input  tqPaymentConditionStagedByPC.tiPaymentConditionDaysMonths (iiPaymentConditionDaysMonths), 
                input  tqPaymentConditionStagedByPC.tiPaymentConditionSupplDays (iiPaymentConditionSupplDays), 
                input  tqPaymentConditionStagedByPC.tiPaymentConditionBaseDays (iiPaymentConditionBaseDays), 
                input  tqPaymentConditionStagedByPC.tiPaymentConditionDueDays (iiPaymentConditionDueDays), 
                input  tCInvoice.CInvoiceDate (itInvoiceDate), 
                input  tqPaymentConditionStagedByPC.tiPaymentConditionDayMthDisc (iiPaymentConditionDayMthDisc), 
                input  tqPaymentConditionStagedByPC.tiPaymentConditionSupDayDisc (iiPaymentConditionSupDayDisc), 
                input  tqPaymentConditionStagedByPC.tcPaymentConditionPdDiscType (icPaymentConditionpDiscType), 
                output tCInvoiceStage.CInvoiceStageDueDate (otDueDate), 
                output tCInvoiceStage.CInvoiceStageDiscDate (otDiscountDate), 
                output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>
            if viFcReturnSuper <> 0 then viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper < 0  then leave MAIN_BLOCK.
        end. /* Do real creation of stages */
    end. /* if tCInvoice.tcNormalPaymentConditionType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} */
end. /* MAIN_BLOCK */

/* Close BPayment component */
if valid-handle(vhBPaymentConditionCIInst)
then do:
     if vlBPaymentConditionStartedHere
     then do:
         <I-13 {bFcCloseAndStopInstance
              &CLASS           = "BPaymentCondition"}>
     end.
     else do:
        <I-9 {bFcCloseInstance
             &CLASS           = "BPaymentCondition"}>
     end.
end.

/* Error handling */
assign oiReturnStatus = viLocalReturn.