project QadFinancials > class BOpenItemAdjustment > method OIAdjApplyFilterDebtor

Description

submethod of OIAdjApplyFilter; customer part


Parameters


iiCompanyIdinputintegerEntity ID
itPostingDateinputdatePosting Date
icCredDebEmplCodeinputcharacterDebtorCreditorEmployeeCode
Code of the debtor/creditor/employee
icBusinessRelationCodeinputcharacterBusiness Relation Code
icInvoiceNumberReferenceinputcharacterReference of the debtor/creditor/employee invoice
idMinimumAmountinputdecimalMinimum Amount that should fit the Invoice/Collection/Document
idMaximumAmountinputdecimalMaximum Amount that should fit the Invoice/Collection/Document
icCurrencyCodeinputcharacterCurrencyCode that should fit the Invoice/Collection/Document. This is not used in case iiCurrencyID is passed
ilIncludeOverAllocatedinputlogicalIncludeOverAllocated Items
ilIncludeAllCompaniesinputlogicalIncludeAllCompanies
ilIncludeClosedInvoicesinputlogical
iiPeriodYearMinimuminputinteger
iiPeriodYearinputinteger
icJournalCodeinputcharacterDaybook Code
iiVoucherinputintegerVoucher
icCorporateGroupCodeinputcharacter
iiBillNumberinputinteger
iiBillYearinputinteger
icShipperOrGTCodeinputcharacterPass the Shipper ID or Golden Tax doc number to the query.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BOpenItemAdjustment.OIAdjApplyFilter


program code (program7/bopenitemadjustment.p)

/* State the DebtorID when there is only one debtor with that Code */
assign viDebtorID = ?.
if icCredDebEmplCode <> '':U and 
   icCredDebEmplCode <> ?
then do :
    <Q-26 run DebtorPrim (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input icCredDebEmplCode, (DebtorCode)
        input ?, (Debtor_ID)
        output dataset tqDebtorPrim) in BDebtor>
    find tqDebtorPrim where /* DON'T USE THE FIRST OPTION!!!!! */
    	 tqDebtorPrim.tcDebtorCode = icCredDebEmplCode 
    	 no-error.
    if available tqDebtorPrim
    then  assign viDebtorID = tqDebtorPrim.tiDebtor_ID.
end. /* if icCredDebEmplCode <> ? and  */

/* Launch query on DI */
<Q-97 run DInvoiceByAllocationInfo (all) (Read) (NoCache)
   (input if ilIncludeAllCompanies then ? else iiCompanyID, (CompanyId)
    input  icBusinessRelationCode, (BusinessRelationCode)
    input ?, (DInvoiceID)
    input ?, (DInvoiceDescription)
    input  icCredDebEmplCode, (DebtorCode)
    input  icCurrencyCode, (CurrencyCode)
    input (if ilIncludeClosedInvoices = true then ? else true), (DInvoiceIsOpen)
    input idMinimumAmount, (DInvoiceAmountTCMinimum)
    input idMaximumAmount, (DInvoiceAmountTCMaximum)
    input iiPeriodYearMinimum, (PeriodYearMinimum)
    input icJournalCode, (JournalCode)
    input iiPeriodYear, (PeriodYear)
    input iiVoucher, (Voucher)
    input ?, (DInvoiceTSMNumber)
    input icShipperOrGTCode, (ShipperCode)
    input icCorporateGroupCode, (CorporateGroupCode)
    input if ilIncludeAllCompanies then true else ?, (CompIsOICrossCy)
    input viDebtorID, (Debtor_ID)
    input false, (DInvoiceIsSelected)
    input iiBillNumber, (BillNumber)
    input iiBillYear, (BillYear)
    output dataset tqDInvoiceByAllocationInfo) in BDInvoice>

for each tqDInvoiceByAllocationInfo no-lock :


    assign vdBalanceTC      = tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitTC - tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditTC
           vlOverAllocation = (tqDInvoiceByAllocationInfo.tdDInvoiceOriginalDebitTC  >  0  AND  vdBalanceTC  < 0) OR
                              (tqDInvoiceByAllocationInfo.tdDInvoiceOriginalDebitTC  <  0  AND  vdBalanceTC  > 0) OR
                              (tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC >  0  AND  vdBalanceTC  > 0)  OR
                              (tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC <  0  AND  vdBalanceTC  < 0).
    if ilIncludeOverAllocated = false or
       (ilIncludeOverAllocated  = true and 
        vlOverAllocation        = true )
    then do:

        create  tOIAdj.
        assign  viRowidOIAdj                         = viRowidOIAdj + 1
                tOIAdj.tc_Rowid                      = string(viRowidOIAdj)
                tOIAdj.tiCompanyID                   = tqDInvoiceByAllocationInfo.tiCompany_ID
                tOIAdj.tiInvoiceID                   = tqDInvoiceByAllocationInfo.tiDInvoice_ID
                tOIAdj.tcReasonCode                  = tqDInvoiceByAllocationInfo.tcReasonCode
                tOIAdj.tcBusinessRelationCode        = tqDInvoiceByAllocationInfo.tcBusinessRelationCode
                tOIAdj.tcCredDebEmplCode             = tqDInvoiceByAllocationInfo.tcDebtorCode
                tOIAdj.tcInvoiceCurrencyCode         = tqDInvoiceByAllocationInfo.tcCurrencyCode
                tOIAdj.tcInvoiceNumberReference      = trim(string(tqDInvoiceByAllocationInfo.tiPeriodYear)) + "/":U + trim(tqDInvoiceByAllocationInfo.tcJournalCode) + "/":U + string(tqDInvoiceByAllocationInfo.tiDInvoiceVoucher,"999999999":U)
                tOIAdj.tcInvoiceOrigin               = {&INVOICEORIGIN-DEBTOR}
                tOIAdj.tcInvoiceType                 = tqDInvoiceByAllocationInfo.tcDInvoiceType
                tOIAdj.ttInvoiceNormalDueDate        = tqDInvoiceByAllocationInfo.ttDInvoiceDueDate 
                tOIAdj.ttInvoiceDiscountDueDate      = tqDInvoiceByAllocationInfo.ttDInvoiceDiscountDueDate
                tOIAdj.ttInvoiceDate                 = tqDInvoiceByAllocationInfo.ttDInvoiceDate 
                tOIAdj.tdInvoiceExchangeRate         = tqDInvoiceByAllocationInfo.tdDInvoiceExchangeRate
                tOIAdj.tcCorporateGroupCode          = tqDInvoiceByAllocationInfo.tcCorporateGroupCode
                tOIAdj.tdInvoiceExchangeRateScale    = tqDInvoiceByAllocationInfo.tdDInvoiceRateScale
                tOIAdj.tcInvoiceBalanceCrDt          = if tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0 and
                                                          tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditTC <> ?
                                                       then {&CREDITDEBITABBREVIATION-CREDIT}
                                                       else 
                                                       if tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitTC <> 0 and
                                                          tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitTC <> ?
                                                       then {&CREDITDEBITABBREVIATION-DEBIT}
                                                       else
                                                       if tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC <> 0 and
                                                       tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC <> ? 
                                                       then {&CREDITDEBITABBREVIATION-CREDIT}
                                                       else {&CREDITDEBITABBREVIATION-DEBIT}
                tOIAdj.tdInvoiceBalanceTC            = if tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditTC <> 0 and   
                                                          tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditTC <> ? 
                                                       then tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditTC 
                                                       else 
                                                       if tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitTC <> 0 and   
                                                          tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitTC <> ? 
                                                       then tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitTC
                                                       else 0
                tOIAdj.tlIsNewOpenItem               = false
                tOIAdj.tlIsOverAllocated             = vlOverAllocation 
                tOIAdj.tdAdjustmentAmountLC          = 0
                tOIAdj.tdAdjustmentAmountTC          = 0 
                tOIAdj.tcAdjustmentCrDt              = if tOIAdj.tdInvoiceBalanceTC = 0
                                                       then tOIAdj.tcInvoiceBalanceCrDt
                                                       else
                                                       if tOIAdj.tcInvoiceBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                       then {&CREDITDEBITABBREVIATION-CREDIT}
                                                       else {&CREDITDEBITABBREVIATION-DEBIT}               
                tOIAdj.tcNewBalanceCrDt              = if tqDInvoiceByAllocationInfo.tlDInvoiceIsOpen
                                                       then tOIAdj.tcInvoiceBalanceCrDt
                                                       else if tqDInvoiceByAllocationInfo.tdDInvoiceOriginalDebitTC <> 0
                                                            then {&CREDITDEBITABBREVIATION-DEBIT} 
                                                            else {&CREDITDEBITABBREVIATION-CREDIT}
                tOIAdj.tdNewBalanceTC                = tOIAdj.tdInvoiceBalanceTC
                tOIAdj.tdAdjustmentExchangeRate      = tqDInvoiceByAllocationInfo.tdDInvoiceExchangeRate
                tOIAdj.tdAdjustmentExchangeRateScale = tqDInvoiceByAllocationInfo.tdDInvoiceRateScale
                tOIAdj.tlIsDiscountTaxAtPayment      = false
                tOIAdj.tlIsStagedPaymentCondition    = if tqDInvoiceByAllocationInfo.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
                                                       then true
                                                       else false
                tOIAdj.tdInvoiceDiscountPercentage   = (if tqDInvoiceByAllocationInfo.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} or
                                                           tqDInvoiceByAllocationInfo.tdPaymentConditionPercentage = ? 
                                                        then 0 
                                                        else tqDInvoiceByAllocationInfo.tdPaymentConditionPercentage)
                tOIAdj.ttInvoiceDiscountDueDate      = tqDInvoiceByAllocationInfo.ttDInvoiceDiscountDueDate
                tOIAdj.tdInvoiceOriginalBalanceTC    = (if tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC <> 0 and tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC <> ? then tqDInvoiceByAllocationInfo.tdDInvoiceOriginalCreditTC else tqDInvoiceByAllocationInfo.tdDInvoiceOriginalDebitTC)
                tOIAdj.tiInvoicePostingID            = tqDInvoiceByAllocationInfo.tiPosting_ID
                tOIAdj.tdAdjustmentDiscountAmountTC  = 0
                tOIAdj.tdAdjustmentDiscountAmountLC  = 0
                tOIAdj.tdInvoiceBalanceLC            = (if tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditLC <> 0 and tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditLC <> ? then tqDInvoiceByAllocationInfo.tdDInvoiceBalanceCreditLC else tqDInvoiceByAllocationInfo.tdDInvoiceBalanceDebitLC)
                tOIAdj.tcInvoiceControlGLCode        = tqDInvoiceByAllocationInfo.tcGLCode
                tOIAdj.tcCompanyCode                 = tqDInvoiceByAllocationInfo.tcCompanyCode
                tOIAdj.tcCInvoiceReference           = "":U
                tOIAdj.tiInvMovementPostingLineID    = 0
                tOIAdj.tdVatTC                       = if tqDInvoiceByAllocationInfo.tdDInvoiceVatDebitTC <> 0 and tqDInvoiceByAllocationInfo.tdDInvoiceVatDebitTC <> ? then tqDInvoiceByAllocationInfo.tdDInvoiceVatDebitTC else tqDInvoiceByAllocationInfo.tdDInvoiceVatCreditTC 
                tOIAdj.tdNonDiscountableAmountTC     = tqDInvoiceByAllocationInfo.tdDInvoiceNonDiscAmtTC
                tOIAdj.tcInvoiceDescription          = tqDInvoiceByAllocationInfo.tcDInvoiceDescription
                tOIAdj.tlBusinessRelationIsCompens   = tqDInvoiceByAllocationInfo.tlBusinessRelationIsCompens
                tOIAdj.tlCompIsCompens               = tqDInvoiceByAllocationInfo.tlCompanyPropertyIsCompens
                tOIAdj.tcCompOINetRest               = tqDInvoiceByAllocationInfo.tcCompanyPropertyOINetRestr
                tOIAdj.tdAdjustmentCCExchangeRate    = tqDInvoiceByAllocationInfo.tdDInvoiceCCRate
                tOIAdj.tdAdjustmentCCExchangeScale   = tqDInvoiceByAllocationInfo.tdDInvoiceCCScale
                tOIAdj.tiBillNumberOIA               = if tqDInvoiceByAllocationInfo.tiBillNumber <> 0
                                                       then tqDInvoiceByAllocationInfo.tiBillNumber
                                                       else ?
                tOIAdj.tiBillYearOIA                 = if tqDInvoiceByAllocationInfo.tiBillYear <> 0
                                                       then tqDInvoiceByAllocationInfo.tiBillYear
                                                       else ?
                tOIAdj.tcBillStatusOIA               = tqDInvoiceByAllocationInfo.tcBillStatus
                tOIAdj.tcDInvoiceDIText              = tqDInvoiceByAllocationInfo.tcDInvoiceDIText
                tOIAdj.tcShipperAndGTInfo            = tqDInvoiceByAllocationInfo.tcDInvoiceOpInfoCode
                tOIAdj.tcInvoiceOpInfoType           = tqDInvoiceByAllocationInfo.tcDInvoiceOpInfoType.

        /* State if tax-at-payment is applicable for this invoice - pass the domaincode due to Fin-Operation jion in the query that is not complete */
        <Q-80 assign vlFcQueryRecordsAvailable = DInvoiceVatByAllocationInfo (NoCache)
           (input tqDInvoiceByAllocationInfo.tiDInvoice_ID, (DInvoiceID)
            input vcDomainCode, (DomainCode)
            input true, (DiscountTaxAtPayment)) in BDInvoice >
        if vlFcQueryRecordsAvailable <> false
        then assign tlIsDiscountTaxAtPayment = true.
        
        /* Create detail records for staged payment conditions */
        if tqDInvoiceByAllocationInfo.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} and
           tOIAdj.tcInvoiceType                                    <> {&INVOICETYPE-PREPAYMENT}        /* if this is prepayment with the staged payment condition, then staged payment records are not created */
        then do:
            /* Calculate discountable amount of whole invoice */
            if tOIAdj.tlIsDiscountTaxAtPayment
            then assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceOriginalBalanceTC - tOIAdj.tdNonDiscountableAmountTC.
            else assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceOriginalBalanceTC - tOIAdj.tdNonDiscountableAmountTC - tOIAdj.tdVatTC.

            assign viRowidOIAdj                   = 0
                   vdTotalDefaultPaymentAmountTC  = 0
                   vdTotalDefaultDiscountAmountTC = 0.

            <Q-30 run DInvoiceStageByDueDate (all) (Read) (NoCache)
               (input tqDInvoiceByAllocationInfo.tiDInvoice_ID, (DInvoiceID)
                input ?, (DueDate)
                output dataset tqDInvoiceStageByDueDate) in BDInvoice>

            for each tqDInvoiceStageByDueDate where
                     tqDInvoiceStageByDueDate.tiDInvoice_ID = tqDInvoiceByAllocationInfo.tiDInvoice_ID
                     break by tqDInvoiceStageByDueDate.tiDInvoice_ID
                           by tqDInvoiceStageByDueDate.ttDInvoiceStageDueDate:
                       
                create tOIAdjStage.
                assign viRowidOIAdj                             = viRowidOIAdj + 1
                       tOIAdjStage.tc_Rowid                     = string(tqDInvoiceStageByDueDate.tc_rowid,"X(20)":U) + string(viRowidOIAdj)
                       tOIAdjStage.tc_ParentRowid               = tOIAdj.tc_Rowid
                       tOIAdjStage.tiInvoiceStageID             = tqDInvoiceStageByDueDate.tiDInvoiceStage_ID
                       tOIAdjStage.tiInvoiceID                  = tqDInvoiceStageByDueDate.tiDInvoice_ID
                       tOIAdjStage.ttDueDate                    = tqDInvoiceStageByDueDate.ttDInvoiceStageDueDate
                       tOIAdjStage.ttDiscountDueDate            = tqDInvoiceStageByDueDate.ttDInvoiceStageDiscDate
                       tOIAdjStage.tdDiscountPercentage         = tqDInvoiceStageByDueDate.tdDInvoiceStagePercentage
                       vdDefaultAllocAmountTC                   = tqDInvoiceStageByDueDate.tdDInvoiceStageAmountTC - tqDInvoiceStageByDueDate.tdDInvoiceStageAmtAppliedTC.
                
                if itPostingDate        <= tqDInvoiceStageByDueDate.ttDInvoiceStageDiscDate and
                   tOIAdj.tcInvoiceType <> {&INVOICETYPE-PREPAYMENT}
                then assign tOIAdjStage.tdDefaultDiscountAmountTC = vdDiscountableInvoiceTC 
                                                                  * (vdDefaultAllocAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC)
                                                                  * (tqDInvoiceStageByDueDate.tdDInvoiceStageDiscPerc / 100)
                            tOIAdjStage.tdDefaultDiscountAmountTC = <M-18 RoundAmount
                                                                       (input  tOIAdjStage.tdDefaultDiscountAmountTC (idUnroundedAmount), 
                                                                        input  tqDInvoiceByAllocationInfo.tiDInvoiceCurrency_ID (iiCurrencyID), 
                                                                        input  tqDInvoiceByAllocationInfo.tcCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
                            tOIAdjStage.tdDefaultPaymentAmountTC  = vdDefaultAllocAmountTC - tOIAdjStage.tdDefaultDiscountAmountTC.
                else assign tOIAdjStage.tdDefaultDiscountAmountTC = 0
                            tOIAdjStage.tdDefaultPaymentAmountTC  = vdDefaultAllocAmountTC.                                                                                             
                                
                assign vdTotalDefaultPaymentAmountTC        = vdTotalDefaultPaymentAmountTC  + tOIAdjStage.tdDefaultPaymentAmountTC
                       vdTotalDefaultDiscountAmountTC       = vdTotalDefaultDiscountAmountTC + tOIAdjStage.tdDefaultDiscountAmountTC.
            end. /* for each tqDInvoiceStageByDueDate */
            
            assign tOIAdj.tdDefaultPaymentAmountTC  = vdTotalDefaultPaymentAmountTC
                   tOIAdj.tdDefaultDiscountAmountTC = vdTotalDefaultDiscountAmountTC.

        end. /* if tqDInvoiceByAllocationInfo.tcPaymentConditionPaymentTyp = staged */
        else do:
            /* ======================================================================================================================================== */
            /*  Calculation of Discount Amount                                                                                                          */
            /* ======================================================================================================================================== */
            /* 1) In case of Discount Tax at Payment = YES                                                                                              */
            /*    Discount Amount =  Invoice amount with Tax *                                                                                          */
            /*                       ((Invoice amount without Tax ?- Non-discountable amount) / Invoice amount without Tax) *                           */
            /*                         Discount %  *                                                                                                    */
            /*                        (Invoice open amount / Invoice amount with Tax).                                                                  */
            /* 2) In case of Discount Tax at Payment = NO                                                                                               */
            /*    Discount Amount = (Invoice amount without Tax  - Non-discountable amount) *  */
            /*                      Discount % *                                                                                                        */
            /*                     (Invoice open amount / Invoice amount with Tax ).           */
            /* ======================================================================================================================================== */
            if itPostingDate        <= tqDInvoiceByAllocationInfo.ttDInvoiceDiscountDueDate and
               tOIAdj.tcInvoiceType <> {&INVOICETYPE-PREPAYMENT}
            then do:
                /* Calculate discountable amount of whole invoice */
                if tOIAdj.tlIsDiscountTaxAtPayment
                then assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceOriginalBalanceTC - tOIAdj.tdNonDiscountableAmountTC.
                else assign vdDiscountableInvoiceTC = tOIAdj.tdInvoiceOriginalBalanceTC - tOIAdj.tdNonDiscountableAmountTC - tOIAdj.tdVatTC.
                
                assign tOIAdj.tdDefaultDiscountAmountTC = vdDiscountableInvoiceTC 
                                                        * (tOIAdj.tdInvoiceBalanceTC / tOIAdj.tdInvoiceOriginalBalanceTC)
                                                        * (tqDInvoiceByAllocationInfo.tdPaymentConditionPercentage / 100)
                       tOIAdj.tdDefaultDiscountAmountTC = <M-62 RoundAmount
                                                             (input  tOIAdj.tdDefaultDiscountAmountTC (idUnroundedAmount), 
                                                              input  0 (iiCurrencyID), 
                                                              input  tqDInvoiceByAllocationInfo.tcCurrencyCode (icCurrencyCode)) in BApplicationProperty>
                       tOIAdj.tdDefaultPaymentAmountTC  = tOIAdj.tdInvoiceBalanceTC
                                                        - tOIAdj.tdDefaultDiscountAmountTC.
            end.                                                               
            else assign tOIAdj.tdDefaultPaymentAmountTC  = tOIAdj.tdInvoiceBalanceTC
                        tOIAdj.tdDefaultDiscountAmountTC = 0.
        end. /* if tqCInvoiceByAllocationInfoNew.tcPaymentConditionPaymentTyp <> staged */        
    end.  /* if  IncludeOverAllocated */
end. /* for each tqDInvoiceByAllocationInfo no-lock */