project QadFinancials > class BPaymentCondition > method CalculateDueDiscountDateNoRead


Parameters


icPaymentConditionPeriodTypeinputcharacter
itPaymentConditionBaseDateinputdate
iiPaymentConditionDaysMonthsinputinteger
iiPaymentConditionSupplDaysinputinteger
iiPaymentConditionBaseDaysinputinteger
iiPaymentConditionDueDaysinputinteger
itInvoiceDateinputdate
iiPaymentConditionDayMthDiscinputinteger
iiPaymentConditionSupDayDiscinputinteger
icPaymentConditionpDiscTypeinputcharacter
otDueDateoutputdate
otDiscountDateoutputdate
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.CreateCInvoicesPayment
method BCInvoice.DefaultValuesPaymentCondition
method BDInvoice.DefaultDuedatesForDeduction
method BDInvoice.DefaultValuesDueDates
method BDInvoice.DefaultValuesPaymentCondition
method BPaymentCondition.CalculateDueDiscountDate

QadFitnesse
method BCInvoice.ApiGetStagedPaymentConditions


program code (program3/bpaymentcondition.p)

/* ================================ */
    /* Check for the correct input type */
    /* ================================ */
    if icPaymentConditionPeriodType <> {&PAYMENTCONDITIONPERIODTYPE-DAYS}      and
       icPaymentConditionPeriodType <> {&PAYMENTCONDITIONPERIODTYPE-MONTHS}    and
       icPaymentConditionPeriodType <> {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT} and
       icPaymentConditionPeriodType <> {&PAYMENTCONDITIONPERIODTYPE-WEEK}
    then do:
        <M-6 run SetMessage
           (input  trim(#T-13'The credit terms period type $1 is invalid.':250(65283)t-13#) (icMessage), 
            input  icPaymentConditionPeriodType (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-6984':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>  
        assign otDueDate      = 12/31/9999
               otDiscountDate = 12/31/9999
               oiReturnStatus = -1.
        return.
    end. /* if icPaymentConditionPeriodType <> {&PAYMENTCONDITIONPERIODTYPE-DAYS}      and */
        
    /* ===================================== */
    /* Set the StartDate to calculation from */
    /* ===================================== */
    assign vtStartDate = itInvoiceDate.
    if itInvoiceDate              <> ? and
       itPaymentConditionBaseDate <> ?
    then do:
        if itInvoiceDate < itPaymentConditionBaseDate
        then assign vtStartDate = itPaymentConditionBaseDate.
    end. /* if itInvoiceDate              <> ? and */
    else if itPaymentConditionBaseDate <> ? and
            itInvoiceDate               = ?
         then assign vtStartDate = itPaymentConditionBaseDate.
    if vtStartDate = ?
    then do:
        assign oiReturnStatus  = -1.
        return.
    end. /* if vtStartDate = ? */
        
    /* ================ */
    /* Set the Due Date */
    /* ================ */
    if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS}
    then do :
        if itPaymentConditionBaseDate   <> ? and 
           iiPaymentConditionDaysMonths = 0  and 
           iiPaymentConditionSupplDays  = 0 and 
           iiPaymentConditionBaseDays   = 0
        then assign vtStartDate = itPaymentConditionBaseDate 
                    otDueDate   = itPaymentConditionBaseDate.
        /* Set the start date to the end of the month */
        else assign vtStartDate = date((iiPaymentConditionDaysMonths + month(vtStartDate)) modulo 12 + 1,
                                      1,
                                      int(year(vtStartDate) + (iiPaymentConditionDaysMonths + month(vtStartDate) - (iiPaymentConditionDaysMonths + month(vtStartDate)) modulo 12) / 12))
                    vtStartDate = vtStartDate - 1
                    otDueDate   = vtStartDate + iiPaymentConditionSupplDays + iiPaymentConditionBaseDays.
    end. /* if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS} */
    else do :
        if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT}
        then do:
            if itPaymentConditionBaseDate   <> ? and 
               iiPaymentConditionDaysMonths = 0  and 
               iiPaymentConditionSupplDays  = 0 and 
               iiPaymentConditionBaseDays   = 0
            then assign vtStartDate = itPaymentConditionBaseDate
                        otDueDate   = itPaymentConditionBaseDate.
            /* If due date start at the end of the fortnight, the system uses the 15th of the  month */
            /* to calculate the due date when the transaction date is between days 1 and 15 of the   */
            /* month. Otherwise the system uses the end of the month.                                */
            else do :
                if day(vtStartDate) <= 15
                then /* Due date is the 15th */
                     assign vtStartDate = date(month(vtStartDate), 15, year(vtStartDate)).
                else /* Set to end date of the month (first of next month  - 1) */
                     assign vtStartDate = date((month(vtStartDate) modulo 12) + 1, 1, year(vtStartDate) + if month(vtStartDate) = 12 then 1 else 0)
                            vtStartDate = vtStartDate - 1.
                assign otDueDate = vtStartDate + (iiPaymentConditionDaysMonths * 14) + iiPaymentConditionBaseDays.
            end. /* Not if itPaymentConditionBaseDate   <> ? and */
        end. /* if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT} */
        else if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-WEEK}
             then do :
                if itPaymentConditionBaseDate   <> ? and 
                   iiPaymentConditionDaysMonths = 0  and 
                   iiPaymentConditionSupplDays  = 0 and 
                   iiPaymentConditionBaseDays   = 0
                then assign vtStartDate = itPaymentConditionBaseDate
                            otDueDate   = itPaymentConditionBaseDate.
                /* Set the due date to the next Saturday */
                else assign vtStartDate = vtStartDate + 7 - weekday(vtStartDate)
                            otDueDate   = vtStartDate + iiPaymentConditionDaysMonths * 7 + iiPaymentConditionBaseDays.
             end. /* if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-WEEK} */
             else /* PaymentConditionPaymentTyp = {&PAYMENTCONDITIONPERIODTYPE-DAYS} */
                  if itPaymentConditionBaseDate   <> ? and 
                     iiPaymentConditionDaysMonths = 0  and 
                     iiPaymentConditionSupplDays  = 0 and 
                     iiPaymentConditionBaseDays   = 0
                  then assign vtStartDate = itPaymentConditionBaseDate
                              otDueDate = itPaymentConditionBaseDate.
                  /* Start date is the later of invoice date and base date           */
                  else assign otDueDate = vtStartDate + iiPaymentConditionDaysMonths + iiPaymentConditionBaseDays.
    end. /* Not if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS} */

    /* =========================================================================================================== */
    /* Take the Minimum # days between invoice date and due date into account and increase the DueDate when needed */
    /* =========================================================================================================== */
    if itPaymentConditionBaseDate   <> ? and 
       iiPaymentConditionDaysMonths = 0  and 
       iiPaymentConditionSupplDays  = 0 and 
       iiPaymentConditionBaseDays   = 0
    then assign otDueDate = otDueDate.
    else do : 
        assign viCurrentDay = day(otDueDate).    
        if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS}    or
           icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT} or
           icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-WEEK}
        then do while (otDuedate - itinvoicedate) < iiPaymentConditionDueDays:
            if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS}
            then assign otDueDate = otDueDate - iiPaymentConditionSupplDays - iiPaymentConditionBaseDays + 1
                        /* Add a month */
                        otDueDate = date(month(otDueDate) modulo 12 + 1,
                                         1,
                                         year(otDueDate) + if month(otDueDate) = 12 then 1 else 0)
                        otDueDate = otDueDate  - 1
                        otDueDate = otDueDate + iiPaymentConditionSupplDays + iiPaymentConditionBaseDays.
            else
            if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT}
            then do:
                /* Add a fortnight */
                assign otDueDate = otDueDate - iiPaymentConditionDaysMonths - iiPaymentConditionBaseDays.
                if day(otDueDate) = 15
                then /* Set due date to the end of the month */
                     assign otDueDate = date(month(otDueDate) modulo 12 + 1,
                                             1, 
                                             year(otDueDate) + if month(otDueDate) = 12 then 1 else 0)
                            otDueDate = otDueDate - 1.
                else /* Due date is end of the month           */
                     /* Set due date to the 15th of next month */
                     assign otDueDate = date(month(otDueDate) modulo 12 + 1,
                                             15,
                                             year(otDueDate) + if month(otDueDate) = 12 then 1 else 0).
                assign otDueDate = otDueDate + iiPaymentConditionDaysMonths + iiPaymentConditionBaseDays.
            end. /* if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT} */
            else /* Add a week */
                 assign otDueDate = otDueDate + 7.
        end. /* if icPaymentConditionPeriodType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS}    or */
        else if otDuedate - itInvoiceDate < iiPaymentConditionDueDays
             then assign otDueDate = itInvoiceDate + iiPaymentConditionDueDays.
    end. /* Not if itPaymentConditionBaseDate   <> ? and  */
    
    /* ========================= */
    /* Set the Discount Due Date */
    /* ========================= */
    assign vtStartDate = if itPaymentConditionBaseDate = ?
                         then itInvoiceDate
                         else itPaymentConditionBaseDate.
    if vtStartDate = ?
    then do:
        <M-9 run SetMessage
           (input  trim(#T-11'Invalid Invoice Date':20(57418)T-11#) (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-6985':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>
        assign otDiscountDate = 12/31/9999
               oiReturnStatus = -1.
        return.
    end. /* if vtStartDate = ? */
    if icPaymentConditionpDiscType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS}
    then do :
        if itPaymentConditionBaseDate   <> ? and 
           iiPaymentConditionDayMthDisc  = 0 and 
           iiPaymentConditionSupDayDisc  = 0 and 
           iiPaymentConditionBaseDays    = 0
        then do :
            assign vtStartDate    = itPaymentConditionBaseDate 
                   otDiscountDate = itPaymentConditionBaseDate.
        end. /* if itPaymentConditionBaseDate   <> ? and */
        else do :
            /* Set the start date to the end of the month */
            assign vtStartDate    = date((iiPaymentConditionDayMthDisc + month(vtStartDate)) modulo 12 + 1,  
                                         1,
                                         int(year(vtStartDate) + (iiPaymentConditionDayMthDisc + month(vtStartDate) - (iiPaymentConditionDayMthDisc + month(vtStartDate)) modulo 12) / 12))
                   vtStartDate    = vtStartDate - 1
                   otDiscountDate = vtStartDate + iiPaymentConditionSupDayDisc + iiPaymentConditionBaseDays.
        end. /* Not if itPaymentConditionBaseDate   <> ? and */
    end. /* if icPaymentConditionpDiscType = {&PAYMENTCONDITIONPERIODTYPE-MONTHS} */
    else if icPaymentConditionpDiscType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT}
         then do:
             /* If discount date start at the end of the fortnight, the system uses the 15th of the  month */
             /* to calculate the discount date when the transaction date is between days 1 and 15 of the   */
             /* month. Otherwise the system uses the end of the month.                                     */
             if day(vtStartDate) <= 15
             then /* Discount date is the 15th */
                  assign vtStartDate = date(month(vtStartDate), 15, year(vtStartDate)).
             else /* Set to end date of the month */
                  assign vtStartDate = date((month(vtStartDate) modulo 12) + 1,
                                            1,
                                            year(vtStartDate) + if month(vtStartDate) = 12 then 1 else 0)
                         vtStartDate = vtStartDate - 1.
             assign otDiscountDate = vtStartDate + (iiPaymentConditionDayMthDisc  * 14) + iiPaymentConditionBaseDays.
         end. /* if icPaymentConditionpDiscType = {&PAYMENTCONDITIONPERIODTYPE-FORTNIGHT} */
         else if icPaymentConditionpDiscType = {&PAYMENTCONDITIONPERIODTYPE-WEEK}
              then assign vtStartDate    = vtStartDate + 7 - weekday(vtStartDate)
                          otDiscountDate = vtStartDate + (iiPaymentConditionDayMthDisc  * 7) + iiPaymentConditionBaseDays.
              else /* PaymentConditionPaymentTyp = {&PAYMENTCONDITIONPERIODTYPE-DAYS} */
                  /* Start date is invoice date or base date                         */
                   assign otDiscountDate = vtStartDate + iiPaymentConditionDayMthDisc + iiPaymentConditionBaseDays.