project QadFinancials > class BCInvoice > method AdditionalUpdatesAllCreateCA

Description

This method is a submethod of AdditionalUpdates to Create automatically a 'CA' posting for the linking between the credit note and the invoice


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.AdditionalUpdatesAll


program code (program8/bcinvoice.p)

/* ============================================================================== */
/* General note: BJournalEntry is already started and opened by the caller-method */
/* ============================================================================== */

/* ================================================================================================================================= */
/* Exception Handling                                                                                                                */
/* Reset a class data-item that holds the comma-separated list with all the posting-ids of the CA-postings created from method       */
/* AdditionalUpdatesAllCreateCA. This data-itam will be used in StopExternalInstances to remove these postings from the BJE-instance */
/* ================================================================================================================================= */
assign oiReturnStatus                  = -98
       vcListCAPostingsCreatedInAddUpd = "":U.

/* =================================================================================== */
/* Validate that the communication to BJE is already open in case therre is a movement */
/* =================================================================================== */
if can-find(first tCInvoiceMovement where
                  tCInvoiceMovement.tc_Status <> "":U) and
  (viBJournalEntryCIID = 0                             or
   viBJournalEntryCIID = ?                             or
   not valid-handle(vhBJournalEntryCIInst))
then do:
    assign vcMessage      = trim(#T-66'Internal error: unexpected situation-no communication to the postings available':255(17891)T-66#) + chr(10) +
                            trim(subst(#T-67'Instance ID = &1.':255(17892)T-67#,string(viBJournalEntryCIID)))                              + chr(10) +
                            trim(subst(#T-68'Valid handle = &1.':255(17893)T-68#,string(valid-handle(vhBJournalEntryCIInst) = TRUE)))      + chr(10) +
                            trim(subst(#T-69'Handle value = &1.':255(17894)T-69#,string(vhBJournalEntryCIInst)))
           oiReturnStatus = -3.
    <M-65 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-4496':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
end. /* if can-find(first tCInvoiceMovement where */

/* ================================================================================================================ */
/* Validation :                                                                                                     */
/* The system should not allow the automatic linking of invoice and credit note in Supplier invoice create          */
/* when the exchange rates are different, because the system does not automatically create realised exchange        */
/* gains and losses as part of that process. An error message should be shown, and the user must link the invoice   */
/* and credit notes separately in Open Item Adjustments.                                                            */
/* This also includes validations on the WHT-values against the invoice-totals.                                     */
/* ================================================================================================================ */
<M-80 run AdditionalUpdatesAllCreateCAVal
   (output vlSwitchDebitCredit (olSwitchDebitCredit), 
    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0 then Return.

/* ===================================================================================== */
/* Make sure we do not allow multiple tCInvoices with tCInvoice.LinkedCInvoice_ID <> 0   */
/* ===================================================================================== */
find tCInvoice where
     tCInvoice.tc_status          = "N":U and
     tCInvoice.LinkedCInvoice_ID <> 0     and
     tCInvoice.LinkedCInvoice_ID <> ?
     no-error.
if ambiguous tCInvoice
then do:
    assign vcMessage      = trim(#T-77'The system cannot hanlde multiple supplier-invoices with a supplier-adjustment link in a single transaction.':255(309501929)T-77#)
           oiReturnStatus = -1.
    <M-76 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-4502':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    return.
end. /* if ambiguous tCInvoice */
if not available tCInvoice
then do:
    assign oiReturnStatus = 0.
    return.
end. /* if not available tCInvoice */

/* ============================================================ */
/* Read the linked CInvoice as we need some info of it later on */
/* ============================================================ */
<Q-85 run CInvoiceForCreateCA (all) (Read) (NoCache)
   (input ?, (CompanyId)
    input tCInvoice.LinkedCInvoice_ID, (CInvoiceId)
    output dataset tqCInvoiceForCreateCA) in BCInvoice>
find tqCInvoiceForCreateCA where
     tqCInvoiceForCreateCA.tiCInvoice_ID = tCInvoice.LinkedCInvoice_ID
     no-error.
if not available tqCInvoiceForCreateCA
then do :
    assign vcMessage      = trim(#T-51'The system cannot create a supplier adjustment because the linked invoice is not available.':255(685647295)T-51#)
           oiReturnStatus = -1.
    <M-29 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-147603':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    return.
end. /* if not available tqCInvoiceForCreateCA */

/* ===================================================================================================================== */
/* Get the BusinessRelation and IntercoCode of the Creditor as we need this for creating the Supplier-Adjustment Posting */
/* ===================================================================================================================== */
<Q-79 run SupplierForSICACreate (all) (Read) (NoCache)
   (input ?, (CompanyId)
    input tCInvoice.Creditor_ID, (CreditorId)
    input tCInvoice.tcCreditorCode, (CreditorCode)
    output dataset tqSupplierForSICACreate) in BCreditor >
find first tqSupplierForSICACreate where
           tqSupplierForSICACreate.tiCreditor_ID = tCInvoice.Creditor_ID
           no-error.
assign vcBusinessRelationCode = if available tqSupplierForSICACreate
                                then tqSupplierForSICACreate.tcBusinessRelationCode
                                else "":U
       vcCAPostingInvoiceReferenceText = string(tCInvoice.CInvoicePostingYear, "9999":U)  + "/":U +
                                         string(tCInvoice.CInvoicePostingPeriod ,"99":U)  + " ":U +
                                         tCInvoice.tcCAJournalCode                        + " ":U +
                                         string(tCInvoice.CInvoiceVoucher, "999999999":U) + " ":U +
                                         trim(tCInvoice.CInvoiceReference).

/* ================================================================ */
/* Create posting header record for the Supplier-Adjustment Posting */
/* ================================================================ */
<M-3 run AddPostingHeader
   (input  tCInvoice.Company_ID (iiCompanyId), 
    input  tCInvoice.CInvoicePostingYear (iiPeriodYear), 
    input  tCInvoice.CInvoicePostingPeriod (iiPeriodPeriod), 
    input  tCInvoice.tcCAJournalCode (icJournalCode), 
    input  '' (icReportingJournalCode), 
    input  tCInvoice.tiCAVoucher (iiVoucher), 
    input  tCInvoice.CInvoicePostingDate (itPostingDate), 
    input  ? (itValueDate), 
    input  tCInvoice.CInvoiceDescription (icPostingText), 
    input  vcBusinessRelationCode (icPostingBusinessRelationText), 
    input  vcCAPostingInvoiceReferenceText (icPostingInvoiceReferenceText), 
    input  tCInvoice.CInvoiceDescription (icPostingParentText), 
    input  ? (iiBPeriodId), 
    input  tCInvoice.tcCreditorCode (icPostingOriginAddressCode), 
    input  string(tCInvoice.CInvoicePostingYear,'9999':U) + '/':U + trim(tCInvoice.tcJournalCode) + string(tCInvoice.CInvoiceVoucher,'999999999':U) (icPostingOriginDocument), 
    input  ? (icPostingOriginDocumentType), 
    input  '':U (icBatchNumber), 
    input  ? (icBankImpLineRef), 
    output viCAPostingID (oiPostingId), 
    output vcCAPostingTcRowid (ocRowid), 
    output tCInvoice.tiCAVoucher (oiPostingVoucher), 
    output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* =================================================================================================== */
/* Set a class data-item that holds the posting-id of the CA-posting created from method               */
/* This data-itam will be used in StopExternalInstances to remove these postings from the BJE-instance */
/* =================================================================================================== */
assign vcListCAPostingsCreatedInAddUpd = string(viCAPostingId).

/* ===================================================================================================================================== */
/* Empty temp-table tMovement and tCInvoiceStageUpdates (that is used later on in this method to call BCInvoice:CreateCInvoiceMovements) */
/* Empty temp-table tInvoiceWHTPaymentForCI (that is used later on in this method to call BWithholdingTax:CreateAndOrUpdateWHT           */
/* ===================================================================================================================================== */
Empty temp-table tMovement.
Empty temp-table tCInvoiceStageUpdates.
Empty temp-table tInvoiceWHTPaymentForCI.
Empty temp-table tInvoiceWHTPaymentCIWHTForCI.

/* ============================================================================================================================= */
/* Calculate the amounts for the posting-line on the control-account of the CInvoice identified by tCInvoice.CInvoice_ID         */
/* The posted amount in here should be the same amount as the entered value for the CInvoice identified by tCInvoice.CInvoice_ID */ 
/* decreased with its WHT-amount and the amount was a debit then it is should here be a credit and vice-versa                    */
/* ============================================================================================================================= */
/* This is how the data looks for the different invoice-type                                                                     */
/*                          OriginalDebitTC   OriginalCreditTC  WHTDebitTC      WHTCreditTC   MovementAmtDeb  MovementAmtCred    */
/*    INVOICE                  0                 1000                0               150          850                            */
/*    CREDITNOTE              1000                0                 150               0                             850          */
/*    INVOICECORRECTION        0                 -1000               0               -150         -850                           */
/*    CREDITNOTECORRECTION    -1000               0                 -150              0                             -850         */
/* ============================================================================================================================= */
assign vdAmountTC = Absolute((tCInvoice.CInvoiceOriginalDebitTC - tCInvoice.CInvoiceOriginalCreditTC) - (tCInvoice.CInvoiceWHTTotAmtDebitTC - tCInvoice.CInvoiceWHTTotAmtCreditTC))
       vdAmountLC = Absolute((tCInvoice.CInvoiceOriginalDebitLC - tCInvoice.CInvoiceOriginalCreditLC) - (tCInvoice.CInvoiceWHTTotAmtDebitLC - tCInvoice.CInvoiceWHTTotAmtCreditLC))
       vdAmountCC = Absolute((tCInvoice.CInvoiceOriginalDebitCC - tCInvoice.CInvoiceOriginalCreditCC) - (tCInvoice.CInvoiceWHTTotAmtDebitCC - tCInvoice.CInvoiceWHTTotAmtCreditCC)).
/* Negative amount needed for corrections */
if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} or 
   tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
then assign vdAmountTC = vdAmountTC * -1
            vdAmountLC = vdAmountLC * -1 
            vdAmountCC = vdAmountCC * -1.
/* Movement should be on debit for Invoice and for invoice-correction */
/* Movement should be on credit for Credit-note and for Credit-note-correction */
if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
   tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
then assign vdMainControlDebitTC  = vdAmountTC
            vdMainControlDebitLC  = vdAmountLC
            vdMainControlDebitCC  = vdAmountCC
            vdMainControlCreditTC = 0
            vdMainControlCreditLC = 0 
            vdMainControlCreditCC = 0.
else assign vdMainControlDebitTC  = 0
            vdMainControlDebitLC  = 0
            vdMainControlDebitCC  = 0
            vdMainControlCreditTC = vdAmountTC
            vdMainControlCreditLC = vdAmountLC
            vdMainControlCreditCC = vdAmountCC.

/* ==================================================================================================================== */
/* Create the main movement record for the control account of the invoice identified by tCInvoice.CInvoice_ID           */
/* Next to it, create the simalar on for the invoice identified by tCInvoice.LinkedCInvoice_ID (=tqCInvoiceForCreateCA) */
/* ==================================================================================================================== */
create tMovement.
assign tMovement.tdAmountDebitTC            = vdMainControlDebitTC
       tMovement.tdAmountCreditTC           = vdMainControlCreditTC 
       tMovement.tdAmountDebitLC            = vdMainControlDebitLC
       tMovement.tdAmountCreditLC           = vdMainControlCreditLC
       tMovement.tdAmountDebitCC            = vdMainControlDebitCC
       tMovement.tdAmountCreditCC           = vdMainControlCreditCC
       tMovement.tdRateTCLC                 = tCInvoice.CInvoiceExchangeRate
       tMovement.tdScaleTCLC                = tCInvoice.CinvoiceRateScale
       tMovement.tdRateTCCC                 = tCInvoice.CInvoiceCCRate
       tMovement.tdScaleTCCC                = tCInvoice.CInvoiceCCScale
       tMovement.tiCInvoiceId               = tCInvoice.CInvoice_ID
       tMovement.tcGLAccountDivisionCode    = tCInvoice.tcDivisionCode
       tMovement.tiPostingLine_ID           = 0 
       tMovement.tcPostingRowId             = vcCAPostingTcRowid
       tMovement.tcPostingText              = tCInvoice.CInvoiceDescription
       tMovement.tdCInvoiceHoldAmountTC     = ? /* Set to unknown-value to make sure this field in the invoice is not updated */
       tMovement.tdMovementDiscountTC       = 0
       tMovement.tlIsUndoPayment            = false
       tMovement.tlMovementIsAboutWHT       = false
       tMovement.ttPaymentDate              = tCInvoice.CInvoicePostingDate
       tMovement.tlIsDelTaxAtPartialPayment = false.
Create btMovement.
Buffer-copy tMovement 
    except tMovement.tiCInvoiceId
           tMovement.tcGLAccountDivisionCode
    to btMovement
    assign btMovement.tiCInvoiceId            = tqCInvoiceForCreateCA.tiCInvoice_ID
           btMovement.tcGLAccountDivisionCode = tqCInvoiceForCreateCA.tcDivisionCode.
if vlSwitchDebitCredit = true
then assign vdTemp                      = btMovement.tdAmountDebitTC
            btMovement.tdAmountDebitTC  = btMovement.tdAmountCreditTC
            btMovement.tdAmountCreditTC = vdTemp
            vdTemp                      = btMovement.tdAmountDebitLC
            btMovement.tdAmountDebitLC  = btMovement.tdAmountCreditLC
            btMovement.tdAmountCreditLC = vdTemp
            vdTemp                      = btMovement.tdAmountDebitCC
            btMovement.tdAmountDebitCC  = btMovement.tdAmountCreditCC
            btMovement.tdAmountCreditCC = vdTemp.
else assign btMovement.tdAmountDebitTC  = btMovement.tdAmountDebitTC * -1
            btMovement.tdAmountCreditTC = btMovement.tdAmountCreditTC * -1
            btMovement.tdAmountDebitLC  = btMovement.tdAmountDebitLC * -1
            btMovement.tdAmountCreditLC = btMovement.tdAmountCreditLC * -1
            btMovement.tdAmountDebitCC  = btMovement.tdAmountDebitCC * -1
            btMovement.tdAmountCreditCC = btMovement.tdAmountCreditCC * -1.

/* ======================================================================================================== */
/* In case the CInvoice identified by tCInvoice.CInvoice_ID holds WHT then we will create another tMovement */
/* record for the control-account and a second one for the WHT                                              */
/* The caluclation of the amount for the control-account will follow the same logic as the creation of the  */
/* previous tMovement right above and the amount for the WHT will be the oposite (debit-credit) of this     */
/* Call a single method that will do all WHT-related actions for the Invoice and the Linked-Invoice         */
/* ======================================================================================================== */
if (tCInvoice.CInvoiceWHTTotAmtDebitTC <> 0 and 
    tCInvoice.CInvoiceWHTTotAmtDebitTC <> ?) or  
   (tCInvoice.CInvoiceWHTTotAmtCreditTC <> 0 and 
    tCInvoice.CInvoiceWHTTotAmtCreditTC <> ?)
then do :
    <M-83 run AdditionalUpdatesAllCreateCAWHT
       (input  vcCAPostingTcRowid (icCAPostingTcRowid), 
        input  vdMainControlDebitTC (idMainControlDebitTC), 
        input  vdMainControlCreditTC (idMainControlCreditTC), 
        input  vlSwitchDebitCredit (ilSwitchDebitCredit), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then return.
end. /* if (tCInvoice.CInvoiceWHTTotAmtDebitTC <> 0 and  */

/* =============================================================================== */
/* Call the internal method that will do the required actions based upon tMovement */
/* =============================================================================== */
if can-find (first tMovement)
then do :
    <M-2 run CreateCInvoiceMovements
       (input-output tMovement (tMovement), 
        input  tCInvoiceStageUpdates (tCInvoiceStageUpdates), 
        input-output viBJournalEntryCIID (biBJournalEntryId), 
        input  false (ilClearData), 
        input  ? (itPaymentTaxPointDate), 
        input  true (ilIsCallFromBankingEntryOrOIAdj), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then return.
end. /* if can-find (first tMovement) */

/* ============================================================================================================= */
/* Call the method in BWithholdingTax that will create the actual WHT records based upon tInvoiceWHTPaymentForCI */
/* ============================================================================================================= */
if can-find (first tInvoiceWHTPaymentForCI) and
   vcActivityCode <> "Reverse":U
then do :
    if viBWithholdingTaxBCIID = 0 or 
       viBWithholdingTaxBCIID = ?
    then do:
        <I-81 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "true"
             &CLASS                = "BWithholdingTax"}>
    end. /* if viBWithholdingTaxBCIID = 0 or */
    else do:
        <I-18 {bFcOpenInstance
             &CLASS           = "BWithholdingTax"}>
    end. /* else do */
    assign viExternalReturnStatus = 0.
    BWITHHOLDINGTAXBLOCK: DO : 
        <M-6 run CreateAndOrUpdateWHT
           (input  tInvoiceWHTPaymentForCI (tInvoiceWHTPayment), 
            input  tInvoiceWHTPaymentCIWHTForCI (tInvoiceWHTPaymentCIWHT), 
            output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
        if viFcReturnSuper < 0 or 
           (viFcReturnSuper > 0 and viExternalReturnStatus = 0)
        then assign viExternalReturnStatus = viFcReturnSuper.
        if viExternalReturnStatus < 0 
        then Leave BWITHHOLDINGTAXBLOCK. 
        <M-36 run ValidateBCAndAdditionalUpdates  (output viExternalReturnStatus (oiReturnStatus)) in BWithholdingTax>    
        if viFcReturnSuper < 0 or 
           (viFcReturnSuper > 0 and viExternalReturnStatus = 0)
        then assign viExternalReturnStatus = viFcReturnSuper.
        if viExternalReturnStatus < 0 
        then Leave BWITHHOLDINGTAXBLOCK.    
    END. /* BWITHHOLDINGTAXBLOCK */
    if viBWithholdingTaxBCIID <> 0 and
       viBWithholdingTaxBCIID <> ?
    then.   /* ========================================================================================================= */
            /* Normally we would here close BWithholdingTax we will not do it for performance                            */
            /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
            /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
            /* appserver that is executed after each call to the appserver                                               */
            /* ========================================================================================================= */
    if viExternalReturnStatus <> 0 then assign oiReturnStatus = viExternalReturnStatus.
    if viExternalReturnStatus < 0 then return.
end. /* if can-find (first tInvoiceWHTPaymentForCI) and */

/* ================== */
/* Exception handling */
/* ================== */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.