project QadFinancials > class BAPMatching > method CalculatePost


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.Calculate


program code (program7/bapmatching.p)

define buffer bBusinessRelation for BusinessRelation.
define buffer bAddressType for AddressType.
define buffer bAddress for Address.
define buffer bCountry for Country.
define buffer bPaymentCondition for PaymentCondition.


/* ================================================================================ */
/* Method that will be executed each time instances are loaded                      */
/* Note that this method is in Segment-7 as one of the methods that are called      */
/* from here (not needed for record availability, just for performant segmentation  */
/* ================================================================================ */

/* ======================== */
/* Set default ReturnStatus */
/* Start Queries            */
/* ======================== */
assign vlJournalAccessAllowed = true.
    
<Q-49 run CompanyPropertyBySuspDelTax (all) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input {&SUSPDELAYTAX-NOTAPPLICABLE}, (DelTax)
    input ?, (SuspTax)
    output dataset tqCompanyPropertyBySuspDelTax) in BCompanyProperty >
find first tqCompanyPropertyBySuspDelTax where
           tqCompanyPropertyBySuspDelTax.tiCompany_ID = viCompanyId and
           tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax <> {&SUSPDELAYTAX-NOTAPPLICABLE}
           no-error.
assign vlUseDelTax = if available tqCompanyPropertyBySuspDelTax
                     then true
                     else false.
                     
/* =========== */
/* Start block */
/* =========== */
CALCULATEBLOCK: DO :
                                                                                    
    /* ========================== */
    /* Go through all APMatchings */
    /* ========================== */
    for each tAPMatching,
        each t_iAPMatching where 
             t_iAPMatching.tc_Rowid = tAPMatching.tc_Rowid on error undo, throw:
        
        /* ================================================================= */
        /* BLF-3552                                                          */
        /* These calculated fields were previously defined as read-only      */
        /* related business fields on query CInvoiceForAPMSelection.         */
        /* Because the cinvoice data object does not include these           */
        /* properties they must be calculated here.                          */
        /* ================================================================= */
        if tAPMatching.tiBusinessRelationID <> 0
        then do:
            for each bBusinessRelation fields (BusinessRelation_ID BusinessRelationCode BusinessRelationICCode) no-lock where
                     bBusinessRelation.BusinessRelation_ID = tAPMatching.tiBusinessRelationID on error undo, throw:
                assign tAPMatching.tcBusinessRelationCode = bBusinessRelation.BusinessRelationCode
                       tAPMatching.tcBusinessRelationICCode = bBusinessRelation.BusinessRelationICCode.
            end.
            
            for first bAddressType fields (AddressTypeCode AddressType_ID) no-lock where
                      bAddressType.AddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE},
                first bAddress fields (BusinessRelation_ID AddressType_ID Address_ID Country_ID AddressTaxIDFederal AddressTaxIDMisc1 AddressTaxIDMisc2 AddressTaxIDMisc3 AddressTaxIdState AddressIsTaxInCity) no-lock where
                      bAddress.BusinessRelation_ID = tAPMatching.tiBusinessRelationID and
                      bAddress.AddressType_ID = bAddressType.AddressType_ID on error undo, throw:
                assign tAPMatching.tcCreditorAddrTaxIDFederal = bAddress.AddressTaxIDFederal
                       tAPMatching.tcCreditorAddrTaxIDMisc1   = bAddress.AddressTaxIDMisc1
                       tAPMatching.tcCreditorAddrTaxIDMisc2   = bAddress.AddressTaxIDMisc2
                       tAPMatching.tcCreditorAddrTaxIDMisc3   = bAddress.AddressTaxIDMisc3
                       tAPMatching.tcCreditorAddrTaxIDState   = bAddress.AddressTaxIdState
                       tAPMatching.tlCreditorAddrIsTaxInCity  = bAddress.AddressIsTaxInCity
                       tAPMatching.tiCreditorAddr_ID          = bAddress.Address_ID.
                if bAddress.Country_ID <> 0
                then for first bCountry fields (Country_ID CountryCode CountryIsEUCountry) no-lock where
                               bCountry.Country_ID = bAddress.Country_ID on error undo, throw:
                    assign tAPMatching.tcCreditorAddrCountryCode  = bCountry.CountryCode
                           tAPMatching.tlCreditorAddrIsEUCountry  = bCountry.CountryIsEUCountry.
                end.
            end.
        end.
        
        if tAPMatching.tiNormalPaymentCondition_ID <> 0
        then for each bPaymentCondition fields (PaymentCondition_ID PaymentConditionCode) no-lock where
                      bPaymentCondition.PaymentCondition_ID = tAPMatching.tiNormalPaymentCondition_ID on error undo, throw:
            assign tAPMatching.tcCInvoicePaymentConditionCode = bPaymentCondition.PaymentConditionCode.
        end.
        
        if tAPMatching.tiShipToAddress_ID <> 0
        then for each bAddress fields (Address_ID Country_ID AddressTaxDeclaration AddressTaxIDFederal AddressTaxIDMisc1 AddressTaxIDMisc2 AddressTaxIDMisc3 AddressTaxIdState AddressIsTaxInCity) no-lock where
                      bAddress.Address_ID = tAPMatching.tiShipToAddress_ID on error undo, throw:
            assign tAPMatching.tiShipToAddrTaxDeclaration = bAddress.AddressTaxDeclaration
                   tAPMatching.tcShipToAddrTaxIDFederal   = bAddress.AddressTaxIDFederal
                   tAPMatching.tcShipToAddrTaxIDMisc1     = bAddress.AddressTaxIDMisc1
                   tAPMatching.tcShipToAddrTaxIDMisc2     = bAddress.AddressTaxIDMisc2
                   tAPMatching.tcShipToAddrTaxIDMisc3     = bAddress.AddressTaxIDMisc3
                   tAPMatching.tcShipToAddrTaxIDState     = bAddress.AddressTaxIdState
                   tAPMatching.tlShipToAddrIsTaxInCity    = bAddress.AddressIsTaxInCity.                        
            if bAddress.Country_ID <> 0
            then for first bCountry fields (Country_ID CountryCode CountryIsEUCountry) no-lock where
                           bCountry.Country_ID = bAddress.Country_ID on error undo, throw:
                assign tAPMatching.tcShipToAddrCountryCode  = bCountry.CountryCode
                       tAPMatching.tlShipToAddrIsEUCountry  = bCountry.CountryIsEUCountry.
            end.
        end.
        
        /* ======================================================================= */
        /* Go through all APMatchingLns: Mark every exising line as being selected */
        /* ======================================================================= */
        for each tAPMatchingLn where
                 tAPMatchingLn.tc_ParentRowid = tAPMatching.tc_Rowid on error undo, throw:
            assign tAPMatchingLn.tlIsSelected = true.
        end. /* for each tAPMatching where */
        
        /* ========================================================================================================================= */
        /* Fields tAPMatching.tdCInvoiceLCCCRate and tAPMatching.tdCInvoiceLCCCScale represent the rate and scale between LC and SC  */
        /* at the moment of the invoice (type: Statutory - optional: accounting) but these fields are not stored on the CInvoice as  */
        /* we do not need that information on the invoice itself. BUT; the pvod use this value to assign the CC-amounts based upon   */
        /* the LC-amounts in the PO-receipt-posting and we store that rate here to be able to compare the values.                    */
        /* In fact, we do not use these values for making calculations: they are information-only                                    */  
        /* ========================================================================================================================= */
        if viCompanyCCId = viCompanyLCId
        then assign tAPMatching.tdCInvoiceLCCCRate  = 1
                    tAPMatching.tdCInvoiceLCCCScale = 1.
        else do :
            <M-20 run GetExchangeRate
               (input  tAPMatching.Company_ID (iiCompanyID), 
                input  viCompanyLCId (iiFromCurrencyID), 
                input  ? (icFromCurrencyCode), 
                input  viCompanyCCId (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                input  tAPMatching.ttCInvoicePostingDate (itValidityDate), 
                output tAPMatching.tdCInvoiceLCCCRate (odExchangeRate), 
                output tAPMatching.tdCInvoiceLCCCScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            if viFcReturnSuper <> 0 then assign viBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then Return.
        end. /* not if viCompanyCCId = viCompanyLCId */
        
        /* =============================================================== */
        /* Check the access is allowed for this user and this journal      */
        /* =============================================================== */
        if tAPMatching.APMatchingStatus = {&APMATCHINGSTATUS-FINISHED}
        then do:
            assign  vhFcComponent = ?.
            <M-79 run CheckJournalRole
               (input  viUsrId (iiUserID), 
                input  ? (icUserCode), 
                input  ? (iiJournalID), 
                input  tAPMatching.tcJournalCode (icJournalCode), 
                input  tAPMatching.Company_ID (iiCompanyID), 
                input  ? (icCompanyCode), 
                input  viSessionID (iiSessionID), 
                output vcMessage (ocErrorMessage), 
                output vlJournalAccessAllowed (olJournalAccessAllowed), 
                output viFcReturnSuper (oiReturnStatus)) in TJournalRole>
            if viFcReturnSuper <> 0
            then assign viBlockReturnStatus = viFcReturnSuper.
    
            run gipr_DeleteProcedure in vhFcComponent.
            delete procedure vhFcComponent.        
            
            if viBlockReturnStatus <> 0
            then do:    
                <M-88 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tAPMatching.tcJournalCode':U (icFieldName), 
                    input  tAPMatching.tcJournalCode (icFieldValue), 
                    input  'E':U (icType), 
                    input  1 (iiSeverity), 
                    input  tAPMatching.tc_Rowid (icRowid), 
                    input  'qadfin-912467':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            end.
            
            assign tAPMatching.tlJournalAccessAllowed = vlJournalAccessAllowed.        
    
            if tAPMatching.tlJournalAccessAllowed  <> true
            then do:
                assign vcMessage           = #T-87'You are not authorized to modify or delete records in daybook &1':100(251571043)T-87#
                       vcMessage           = substitute(vcMessage, tAPMatching.tcJournalCode)                               
                       viBlockReturnStatus = 1.                
                <M-86 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tAPMatching.tcJournalCode':U (icFieldName), 
                    input  tAPMatching.tcJournalCode (icFieldValue), 
                    input  'W':U (icType), 
                    input  3 (iiSeverity), 
                    input  tAPMatching.tc_Rowid (icRowid), 
                    input  'qadfin-553105':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            end.  
        end. /* if tAPMatching.APMatchingStatus = {&APMATCHINGSTATUS-FINISHED} */    
        else assign tAPMatching.tlJournalAccessAllowed = true.                        

        /* ========================================================================== */
        /* All following actions are only needed for initial matchings                */
        /* Finshed ones cannot be updated anymore so all values in the db are correct */
        /* ========================================================================== */
        if tAPMatching.APMatchingStatus <> {&APMATCHINGSTATUS-INITIAL}
        then next.
        
        /* ===================================================== */
        /* Call a method that returns the amount to be matched   */
        /* Note: this method is also called from another place   */
        /* This method uses query APMatchingByCInvoice           */
        /* ===================================================== */
        <M-2 run DefaultValuesInvoiceAmount
           (input  tAPMatching.CInvoice_ID (iiCInvoiceID), 
            input  tAPMatching.APMatching_ID (iiAPMatchingID), 
            input  ? (iiOldReasonID), 
            input-output tAPMatching.Reason_ID (biNewReasonID), 
            input-output tAPMatching.tcReasonCode (bcNewReasonCode), 
            output vcDummy (ocCurrency), 
            output tAPMatching.APMatchingTotalAmountTC (odAmountToMatchTC), 
            output tAPMatching.APMatchingTotalAmountLC (odAmountToMatchLC), 
            output tAPMatching.APMatchingARRecTaxTC (odTaxAcrueReceiptRecovTC), 
            output tAPMatching.APMatchingARNRecTaxTC (odTaxAcrueReceiptNonRecovTC), 
            output tAPMatching.APMatchingAIRecTaxTC (odTaxAcrueInvoiceRecovTC), 
            output tAPMatching.APMatchingAINRecTaxTC (odTaxAcrueInvoiceNonRecovTC), 
            output tAPMatching.APMatchingCInvoiceDiscPct (odInvoiceDiscountPercentage), 
            input  false (ilBCInvoiceIsAlreadyOpen), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        if viFcReturnSuper     <> 0 then assign viBlockReturnStatus = viFcReturnSuper.
        if viBlockReturnStatus <  0 then leave CALCULATEBLOCK.

        /* ================================================================================= */
        /* Check if some main fields have been chnaged and if so, mark the record as changed */
        /* ================================================================================= */
        if t_iAPMatching.APMatchingTotalAmountTC   <> tAPMatching.APMatchingTotalAmountTC   or
           t_iAPMatching.APMatchingTotalAmountLC   <> tAPMatching.APMatchingTotalAmountLC   or
           t_iAPMatching.APMatchingARRecTaxTC      <> tAPMatching.APMatchingARRecTaxTC      or
           t_iAPMatching.APMatchingARNRecTaxTC     <> tAPMatching.APMatchingARNRecTaxTC     or
           t_iAPMatching.APMatchingAIRecTaxTC      <> tAPMatching.APMatchingAIRecTaxTC      or
           t_iAPMatching.APMatchingAINRecTaxTC     <> tAPMatching.APMatchingAINRecTaxTC     or
           t_iAPMatching.APMatchingCInvoiceDiscPct <> tAPMatching.APMatchingCInvoiceDiscPct or
           t_iAPMatching.tdCInvoiceLCCCRate        <> tAPMatching.tdCInvoiceLCCCRate        or
           t_iAPMatching.tdCInvoiceLCCCScale       <> tAPMatching.tdCInvoiceLCCCScale   
        then assign tAPMatching.tc_Status = "C":U.
        
        /* ===================================================================================================== */
        /* Call a submethod in the same segment that will handle the matching-lines and the underlying data      */
        /* ===================================================================================================== */
        <M-51 run CalculatePostAPMLn
           (input  vlUseDelTax (ilUseDelTax), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        if viFcReturnSuper <> 0 then assign viBlockReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then Return.
        
    end. /* for each tAPMatching */
    
    /* ==================================================================== */
    /* Go through all APMatchings: Mark every exising line as being selected */
    /* ==================================================================== */
    for each tAPMatching, 
        each tAPMatchingLn where
             tAPMatchingLn.tc_ParentRowid = tAPMatching.tc_Rowid on error undo, throw:
        assign tAPMatchingLn.tlIsSelected = true.
    end. /* for each tAPMatching where */
    
END. /* CALCULATEBLOCK */

/* ============================= */
/* Check for errors in the block */
/* ============================= */
if viBlockReturnStatus <> 0 
then assign oiReturnStatus = viBlockReturnStatus.
if viBlockReturnStatus < 0
then Return.