project QadFinancials > class BDInvoice > method CreateDInvoiceMovementsCrossCy

Description

CreateDInvoiceMovementsCrossCy; sub -method of CreateDInvoiceMovements that is called in case the control-posting-line of the posting should be replaced by a cross-company account.


Parameters


icPostingRowIdinputcharacterPostingRowId; RowId of Posting
idMovementAmountCreditTCinputdecimalAmountCreditTC
idMovementAmountDebitTCinputdecimalAmountDebitTC
iiMovementDInvoiceIDinputintegerMovementDInvoiceID
icMovemenGLAccountDivisionCodeinputcharacterMovemenGLAccountDivisionCode
iiDDocInvoiceXrefIDinputinteger
idMovementDiscountAmountTCinputdecimalMovement Descount Amount Tc
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.CreateDInvoiceMovements


program code (program3/bdinvoice.p)

/* ========================================================================================= *
 * Method:      CreateDInvoiceMovementsCrossCy                                               *
 * ========================================================================================= */
assign oiReturnStatus = -98
       viLocalReturn  = 0.

MAIN_BLOCK:
do on error undo, leave:    
    /* ====================================== */
    /* Find the DInvoice and it currency-code */
    /* ====================================== */
    <Q-76 run DInvoiceForPaymentExt (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input iiMovementDInvoiceID, (DInvoiceId)
        output dataset tqDInvoiceForPaymentExt) in BDInvoice>
    find first tqDInvoiceForPaymentExt where     
               tqDInvoiceForPaymentExt.tiDInvoice_ID = iiMovementDInvoiceID
               no-lock no-error.
    if not available tqDInvoiceForPaymentExt
    then do :
        assign vcMessage      = trim(substitute(#T-39'A cross-company error occurred. The system was unable to locate the customer invoice based on its ID (&1).':255(1775)T-39#,string(iiMovementDInvoiceID))).
        <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-1087':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not available tqDInvoiceDebtStatusCurr */
        
    if tqDInvoiceForPaymentExt.tiCompany_ID = viCompanyId
    then do :
        assign vcMessage      = trim(substitute(#T-42'A cross-company error occurred. You do not need to create a cross-company posting because the customer invoice is in the current entity (invoice: &1).':255(1777)T-42#,string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))).
        <M-15 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-1080':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if tqCompanyPrim.tiCompany_ID = viCompanyId */
    
    /* ================================================================================= */
    /* Get the JournalCode of the current posting. This JournalCode should also exist in */
    /* the company of the DInvoice and the type of these 2 Journals should be the same   */
    /* ================================================================================= */
    <M-60 run CreateDInvoiceMovementsCrossCyJrnl
       (input  icPostingRowId (icPostingRowId), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAIN_BLOCK.
    
    /* ===================================================== */
    /* Get the businessRelation out of the company of the DI */
    /* ===================================================== */
    <Q-18 run CompanyPropertyByBusRelSimple (all) (Read) (Cache)
       (input tqDInvoiceForPaymentExt.tiCompany_ID, (CompanyId)
        input ?, (CompanyCode)
        input ?, (BusinessRelationCode)
        output dataset tqCompanyPropertyByBusRelSimple) in BCompanyProperty>
    find first tqCompanyPropertyByBusRelSimple where 
               tqCompanyPropertyByBusRelSimple.tiCompany_ID = tqDInvoiceForPaymentExt.tiCompany_ID
               no-lock no-error.
    if not available tqCompanyPropertyByBusRelSimple
    then do :
        assign vcMessage = trim(substitute(#T-43'A cross-company error occurred. The system was unable to find the business relation information for entity '&1' (customer invoice: &2).':255(1778)T-43#, tqDInvoiceForPaymentExt.tcCompanyCode, string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))).
        <M-16 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-1081':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not tqCompanyPropertyByBusRelSimple. tqCompanyPropertyByBusRelSimple */
    else if tqCompanyPropertyByBusRelSimple.tcBusinessRelationCode = ? or 
            tqCompanyPropertyByBusRelSimple.tcBusinessRelationCode = "":U
         then do :
            assign vcMessage = trim(substitute(#T-44'A cross-company error occurred. No business relation was defined for entity '&1' (customer invoice: &2).':255(1779)T-44#, tqDInvoiceForPaymentExt.tcCompanyCode, string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))).
            <M-17 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-1082':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
         end. /* if tqCompanyPropertyByBusRelSimple.tcBusinessRelationCode = ? or */

    /* ============================================================================================ */
    /* Get a GL that will be used for the posting in the current company with following properties; */
    /* Get the A/R G/L account that is specified in the domain of the current company               */
    /* - GL.GLType should be CrossCompany                                                           */
    /* - GL.GLSystemType should be empty                                                            */
    /* - GL.GLIsIntercompany should be true                                                         */
    /* ============================================================================================ */
    <Q-65 run DomainForInterCompany (all) (Read) (Cache)
       (input viDomainID, (DomainID)
        input ?, (DomainCode)
        input ?, (CompanyID)
        output dataset tqDomainForInterCompany) in BDomain>
    find first tqDomainForInterCompany no-lock no-error.
    if not available tqDomainForInterCompany
    then do :
        assign vcMessage = trim(substitute(#T-45'A cross-company error occurred. No cross-company account information was defined for the current domain (&1 / &2')':255(59432)T-45#,vcDomainCode,viDomainID)) + chr(10) + 
                           trim(substitute(#T-46'Customer invoice: '&1'.':255(49403)T-46#, tqDInvoiceForPaymentExt.tcCompanyCode, string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))) + chr(10) + 
                           trim(substitute(#T-47'Entity: &1.':255(999890713)T-47#,string(viCompanyId))).
        <M-23 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-1083':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.        
    end. /* if not available tqDomainForInterCompany */
    
    assign vcMessage = "":U.
    if tqDomainForInterCompany.tcIntercoARGLCode = "":U and 
       tqDomainForInterCompany.tcIntercoARGLCode = ?
    then assign vcMessage = vcMessage + chr(10) + trim(substitute(#T-68'AR cross-company accounts are not defined for the domain (&1).':255(59436)T-68#,vcDomainCode)).
    if tqDomainForInterCompany.tlIntercoARGLIsIntercoAccount <> true
    then assign vcMessage = vcMessage + chr(10) + trim(#T-48'The GL account must be an intercompany account.':255(997)T-48#).
    if tqDomainForInterCompany.tlIntercoARGLIsCostCentreAccount and 
       (tqDomainForInterCompany.tiIntercoARCostCentreProfile_ID = 0 or
        tqDomainForInterCompany.tiIntercoARCostCentreProfile_ID = ?)
    then assign vcMessage = vcMessage + chr(10) + trim(#T-49'No cost center was specified, and the GL account is defined with cost center analysis. A default cost center is mandatory for automatic postings.':255(998)T-49#).
    if tqDomainForInterCompany.tlIntercoARGLIsProjectAccount and 
       (tqDomainForInterCompany.tiIntercoARProjectProfile_ID = 0 or
        tqDomainForInterCompany.tiIntercoARProjectProfile_ID = ?)
    then assign vcMessage = vcMessage + chr(10) + trim(#T-50'No project was specified, and the GL account is defined with project analysis. A default project is mandatory for automatic postings.':255(999)T-50#).
    if tqDomainForInterCompany.tlIntercoARGLIsDivisionAccount and 
       (tqDomainForInterCompany.tiIntercoARDivisionProfile_ID  = 0 or 
        tqDomainForInterCompany.tiIntercoARDivisionProfile_ID = ?)
    then assign vcMessage = vcMessage + chr(10) + trim(#T-51'No sub-account was specified, and the GL account is defined with sub-account analysis. A default sub-account is mandatory for automatic postings.':255(1000)T-51#).
    if vcMessage <> "":U and length(vcMessage,"CHARACTER":U) > 1
    then do :
        assign vcMessage = trim(substitute(#T-52'A cross-company error occurred. The specified GL account '&1' of domain '&2' is not defined in the system or is invalid.':255(59440)T-52#,tqDomainForInterCompany.tcIntercoARGLCode,vcDomainCode)) + chr(10) + 
                           trim(substitute(#T-53'Customer invoice: '&1'.':255(49403)T-53#, string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))) + chr(10) + 
                           substring(vcMessage,2,-1,"CHARACTER":U).
        <M-24 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-1084':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.         
    end. /* if vcMessage <> "":U and LENGTH(vcMessage,"CHARACTER":U) > 1 */

    /* =========================== */
    /* Get the division of the G/L */
    /* =========================== */
    if tqDomainForInterCompany.tiIntercoARDivisionProfile_ID <> 0 and 
       tqDomainForInterCompany.tiIntercoARDivisionProfile_ID <> ?
    then do :
        <Q-27 run GetDivisionFromProfile (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input tqDomainForInterCompany.tiIntercoARDivisionProfile_ID, (DivisionProfileId)
           output dataset tqDivisionFromProfile) in BProfile >
        
        find first tqDivisionFromProfile where
            tqDivisionFromProfile.tiProfile_ID =  tqDomainForInterCompany.tiIntercoARDivisionProfile_ID no-error.  
            
        if not available tqDivisionFromProfile
        then do:
            assign vcMessage = trim(substitute(#T-54'A cross-company error occurred. The system is unable to find the sub-account of the GL account (&1) based on its profile.':255(1101)T-54#,tqDomainForInterCompany.tcIntercoARGLCode)) + chr(10) + 
                               trim(substitute(#T-55'Customer invoice: '&1'.':255(49403)T-55#, string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))) + chr(10) + 
                               trim(substitute(#T-56'Profile ID: &1.':255(1102)T-56#,string(tqDomainForInterCompany.tiIntercoARDivisionProfile_ID))).
            <M-28 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-1085':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end. /* if not avail */
    end. /* if tqDomainForInterCompany.tiIntercoARDivisionProfile_ID <> 0 */
    
    /* ================================================================================================ *
     * Calculate LC exchange rate which will be used for allocation posting                             *
     * ================================================================================================ */
    /* If transaction currency of the invoice is the same as local currency of the payment */
    if tqDInvoiceForPaymentExt.tiDInvoiceCurrency_ID = viCompanyLCId
    then assign vdTCLCExchangeRate      = 1
                vdTCLCExchangeRateScale = 1.
    /* If local currency of the invoice are the same as local currency of the payment */
    else if tqDInvoiceForPaymentExt.tiLCCurrency_ID = viCompanyLCId
    then do:
        /* If this is the last payment, calculate exchange rate from the balance - to prevent *
         * rounding issues with multiple payments                                             */
        if abs(tqDInvoiceForPaymentExt.tdDInvoiceBalanceTC) = abs(idMovementAmountCreditTC + idMovementAmountDebitTC)
        then assign vdTCLCExchangeRate      = tqDInvoiceForPaymentExt.tdDInvoiceBalanceLC / tqDInvoiceForPaymentExt.tdDInvoiceBalanceTC
                    vdTCLCExchangeRateScale = 1.
        else assign vdTCLCExchangeRate      = tqDInvoiceForPaymentExt.tdDInvoiceExchangeRate
                    vdTCLCExchangeRateScale = tqDInvoiceForPaymentExt.tdDInvoiceRateScale.
    end.
    /* If local currencies are different, take current exchange rate between TC and LC */
    else do :
        <M-72 run GetExchangeRate
           (input  viCompanyId (iiCompanyID), 
            input  tqDInvoiceForPaymentExt.tiDInvoiceCurrency_ID (iiFromCurrencyID), 
            input  ? (icFromCurrencyCode), 
            input  viCompanyLCId (iiToCurrencyID), 
            input  ? (icToCurrencyCode), 
            input  ? (iiExchangeRateTypeID), 
            input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
            input  tqDInvoiceForPaymentExt.ttDInvoicePostingDate (itValidityDate), 
            output vdTCLCExchangeRate (odExchangeRate), 
            output vdTCLCExchangeRateScale (odExchangeScaleFactor), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave MAIN_BLOCK.
    end.
    
    /* ================================================================================================ *
     * Calculate LC amounts                                                                             *
     * ================================================================================================ */
    assign vdMovementAmountCreditLC   = <M-70 RoundAmount
                                           (input  idMovementAmountCreditTC * vdTCLCExchangeRate * vdTCLCExchangeRateScale (idUnroundedAmount), 
                                            input  0 (iiCurrencyID), 
                                            input  vcCompanyLC (icCurrencyCode)) in BDInvoice>
           vdMovementAmountDebitLC    = <M-83 RoundAmount
                                           (input  idMovementAmountDebitTC * vdTCLCExchangeRate * vdTCLCExchangeRateScale (idUnroundedAmount), 
                                            input  0 (iiCurrencyID), 
                                            input  vcCompanyLC (icCurrencyCode)) in BDInvoice>.


    /* ================================================================================================ *
     * Calculate CC exchange rate which will be used for allocation posting                             *
     * ================================================================================================ */
    /* If transaction currency of the invoice is the same as transactional currency of the payment */
    if tqDInvoiceForPaymentExt.tiDInvoiceCurrency_ID = viCompanyCCId
    then assign vdTCCCExchangeRate      = 1
                vdTCCCExchangeRateScale = 1.
    /* If statutory currencies are different, take current exchange rate between TC and LC */
    else if not vlDomainIsStatutory 
    then assign vdTCCCExchangeRate      = vdTCLCExchangeRate
                vdTCCCExchangeRateScale = vdTCLCExchangeRateScale.
    /* If statutory currency of the invoice are the same as statutory currency of the payment */
    else if tqDInvoiceForPaymentExt.tiStatutoryCurrency_ID = viCompanyCCId
    then do:
        /* If this is the last payment, calculate exchange rate from the balance - to prevent *
         * rounding issues with multiple payments                                             */
        if abs(tqDInvoiceForPaymentExt.tdDInvoiceBalanceTC) = abs(idMovementAmountCreditTC + idMovementAmountDebitTC)
        then assign vdTCCCExchangeRate      = tqDInvoiceForPaymentExt.tdDInvoiceBalanceCC / tqDInvoiceForPaymentExt.tdDInvoiceBalanceTC
                    vdTCCCExchangeRateScale = 1.
        else assign vdTCCCExchangeRate      = tqDInvoiceForPaymentExt.tdDInvoiceCCRate
                    vdTCCCExchangeRateScale = tqDInvoiceForPaymentExt.tdDInvoiceCCScale.
    end.

    else do:
        <M-19 run GetExchangeRate
           (input  viCompanyId (iiCompanyID), 
            input  tqDInvoiceForPaymentExt.tiDInvoiceCurrency_ID (iiFromCurrencyID), 
            input  ? (icFromCurrencyCode), 
            input  viCompanyCCId (iiToCurrencyID), 
            input  ? (icToCurrencyCode), 
            input  ? (iiExchangeRateTypeID), 
            input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
            input  tqDInvoiceForPaymentExt.ttDInvoicePostingDate (itValidityDate), 
            output vdTCCCExchangeRate (odExchangeRate), 
            output vdTCCCExchangeRateScale (odExchangeScaleFactor), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave MAIN_BLOCK.
    end.
    
    /* ================================================================================================ *
     * Calculate CC amounts                                                                             *
     * ================================================================================================ */
    assign vdMovementAmountCreditCC   = <M-11 RoundAmount
                                           (input  idMovementAmountCreditTC * vdTCCCExchangeRate * vdTCCCExchangeRateScale (idUnroundedAmount), 
                                            input  0 (iiCurrencyID), 
                                            input  vcCompanyCC (icCurrencyCode)) in BDInvoice>
           vdMovementAmountDebitCC    = <M-93 RoundAmount
                                           (input  idMovementAmountDebitTC * vdTCCCExchangeRate * vdTCCCExchangeRateScale (idUnroundedAmount), 
                                            input  0 (iiCurrencyID), 
                                            input  vcCompanyCC (icCurrencyCode)) in BDInvoice>.

    
    /* ================================================================================================================= */
    /* Add standard-posting as replacement for the control posting for the retrieved G/L account in the current domain   */
    /* Do not fill the businessrelation for the interco as this will be is by BJournalEntry based on the cross-cy fields */
    /* ================================================================================================================= */
    <M-67 run AddStandardPosting
       (input  icPostingRowId (icPostingtcRowid), 
        input  tqDomainForInterCompany.tcIntercoARGLCode (icGLCode), 
        input  (if available tqDivisionFromProfile then tqDivisionFromProfile.tcDivisionCode else '':U) (icDivisionCode), 
        input  tqDInvoiceForPaymentExt.tcCostCentreCode (icCostCentreCode), 
        input  '':U (icCostCentreText), 
        input  tqDInvoiceForPaymentExt.tcProjectCode (icProjectCode), 
        input  '':U (icProjectText), 
        input  tqCompanyPropertyByBusRelSimple.tcBusinessRelationICCode (icIntercoBusinessRelationCode), 
        input  tqDInvoiceForPaymentExt.tcCurrencyCode (icCurrencyCode), 
        input  idMovementAmountDebitTC (idDebitTC), 
         input  vdMovementAmountDebitLC (idDebitLC), 
         input  vdMovementAmountDebitCC (idDebitCC), 
         input  ? (idDebitPC), 
         input  idMovementAmountCreditTC (idCreditTC), 
         input  vdMovementAmountCreditLC (idCreditLC), 
         input  vdMovementAmountCreditCC (idCreditCC), 
         input  ? (idCreditPC), 
         input  ? (idQty), 
        input  #T-67'Cross-Company Posting':40(59433)T-67# (icLineText), 
        input  '':U (icSafText), 
        input  tDefaultSafsDI (tDefaultSafs), 
        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
        input  vdTCLCExchangeRate (idExchangeRate), 
        input  vdTCLCExchangeRateScale (idExchangeRateScale), 
        input  vdTCCCExchangeRate (idPostingLineCCRate), 
        input  vdTCCCExchangeRateScale (idPostingLineCCScale), 
        output viControlPostingLineId (oiPostingLineId), 
        input  0 (iiSafStructureId), 
        input  '':U (icSafStructureCode), 
        input  '':U (icAllocationKey), 
        input  true (ilLinkedCrCyDaemonReqExists), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viFcReturnSuper < 0
    then do :
        assign vcMessage = trim(#T-58'A cross-company error occurred  while creating the control posting. See other messages for further details.':255(1104)T-58#) + chr(10) + 
                           trim(substitute(#T-59'Customer invoice: '&1'.':255(49403)T-59#, string(tqDInvoiceForPaymentExt.tcJournalCode) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoicePostingYear) + "/":U + string(tqDInvoiceForPaymentExt.tiDInvoiceVoucher, "999999999":U))).
        <M-36 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-1086':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>        
        leave MAIN_BLOCK.
    end. /* if oiReturnStatus < 0 */
    
    /* ================================================================== */
    /* Call a method that will create a QCrossCompany-record that will be */
    /* picked up by a daemon to create a posting in the compnay of the DI */
    /* First convert icPostingRowId into the PostingID                    */
    /* ================================================================== */
    <M-37 run GetPostingIDBasedOnPostingRowid
       (input  icPostingRowId (icPostingRowid), 
        output viPostingId (oiPostingID), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAIN_BLOCK.
    
    <M-20 run CreateDInvoiceMovementsCrossCyVal
       (input  viControlPostingLineId (iiPostingLine_ID), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAIN_BLOCK.

    <M-38 run CreateDInvoiceMovementsCrossCyExt
       (input  viPostingId (iiOriginatorPostingID), 
        input  viControlPostingLineId (iiOriginatorPostingLineId), 
        input  idMovementAmountCreditTC (idMovementAmountCreditTC), 
        input  idMovementAmountDebitTC (idMovementAmountDebitTC), 
        input  tqDInvoiceForPaymentExt.tiDInvoice_ID (iiDInvoiceID), 
        input  tqDInvoiceForPaymentExt.tiCompany_ID (iiDInvoiceCompanyID), 
        input  iiDDocInvoiceXrefID (iiDDocumentInvoiceXrefID), 
        input  idMovementDiscountAmountTC (idMovementDiscountAmountTC), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAIN_BLOCK.
end. /* of MAIN_BLOCK */

/* Error handling */
assign oiReturnStatus = viLocalReturn.