project QadFinancials > class BCInvoice > method CreateCInvoiceMovementsCrossCy

Description

CreateCInvoiceMovementsCrossCy; sub -mehtod of CreateCInvoiceMovements 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
idMovementAmountCreditLCinputdecimal
idMovementAmountDebitLCinputdecimal
idMovementAmountCreditCCinputdecimal
idMovementAmountDebitCCinputdecimal
iiMovementCInvoiceIDinputintegerMovementCInvoiceID
icMovemenGLAccountDivisionCodeinputcharacterMovemenGLAccountDivisionCode
iiCDocInvoiceXrefIDinputintegerID of the Supplier payment allocation line which triggered creation of the Supplier invoice movement
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.CreateCInvoiceMovementsIDS


program code (program7/bcinvoice.p)

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

MAIN_BLOCK:
do on error undo, leave:
    /* ====================================== */
    /* Find the CInvoice and it currency-code */
    /* ====================================== */
    <Q-22 run CInvoiceForPayment (all) (Read) (Cache)
       (input ?, (CompanyId)
        input iiMovementCInvoiceID, (CInvoiceId)
        output dataset tqCInvoiceForPayment) in BCInvoice>
    
    find first tqCInvoiceForPayment where     
               tqCInvoiceForPayment.tiCInvoice_ID = iiMovementCInvoiceID
               no-lock no-error.
    if not available tqCInvoiceForPayment
    then do :
        <M-2 run SetMessage
           (input  trim(subst(#T-33'A cross-company error occurred. The system was unable to locate the supplier invoice based on its ID (&1).':255(1097)T-33#,string(iiMovementCInvoiceID))) (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QADFIN-779':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not available tqCInvoiceByCyIDYearJrnlVchr */
    
    if tqCInvoiceForPayment.tiCompany_ID = viCompanyId
    then do :
        assign vcMessage      = trim(subst(#T-36'A cross-company error occurred. Cross-company transactions are not required because the supplier invoice references the current entity (invoice: &2).':255(1100)T-36#,string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))).
        <M-5 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
            input  'QADFIN-781':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if tqCInvoiceForPayment.tiCompany_ID = viCompanyId */
    
    /* ================================================================================= */
    /* Get the JournalCode of the current posting. This JournalCode should also exist in */
    /* the company of the CInvoice and the type of these 2 Journals should be the same   */
    /* ================================================================================= */
    <M-57 run CreateCInvoiceMovementsCrossCyJrnl
       (input  icPostingRowId (icPostingRowId), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    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 CI */
    /* ===================================================== */
    <Q-6 run CompanyPropertyByBusRelSimple (all) (Read) (Cache)
       (input tqCInvoiceForPayment.tiCompany_ID, (CompanyId)
        input ?, (CompanyCode)
        input ?, (BusinessRelationCode)
        output dataset tqCompanyPropertyByBusRelSimple) in BCompanyProperty>
    find first tqCompanyPropertyByBusRelSimple where 
               tqCompanyPropertyByBusRelSimple.tiCompany_ID = tqCInvoiceForPayment.tiCompany_ID
               no-lock no-error.
    if not available tqCompanyPropertyByBusRelSimple
    then do :
        assign vcMessage = trim(subst(#T-69'A cross-company error occurred. The system cannot find the business relation information for entity &1 (supplier invoice: &2).':255(992)T-69#,tqCInvoiceForPayment.tcCompanyCode,string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))).
        <M-7 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
            input  'QADFIN-783':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not tqCompanyPropertyByBusRelSimple. tqCompanyPropertyByBusRelSimple */
    else if tqCompanyPropertyByBusRelSimple.tcBusinessRelationCode = ? or 
            tqCompanyPropertyByBusRelSimple.tcBusinessRelationCode = "":U
    then do :
        assign vcMessage      = trim(subst(#T-38'A cross-company error occurred. No business relation was defined for entity &1 (supplier invoice: &2).':255(993)T-38#, tqCInvoiceForPayment.tcCompanyCode, string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))).
        <M-8 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
            input  'QADFIN-784':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        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/P 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-61 run DomainForInterCompany (all) (Read) (Cache)
       (input viDomainID, (DomainID)
        input ?, (DomainCode)
        input ?, (CompanyID)
        output dataset tqDomainForInterCompany) in BDomain>
    find first tqDomainForInterCompany no-error.
    if not available tqDomainForInterCompany
    then do :
        assign vcMessage      = trim(substitute(#T-39'A cross-company error occurred. No cross-company account information is defined for the current domain (&1 / &2).':255(824450204)T-39#,vcDomainCode,viDomainID)) + chr(10) + 
                                trim(substitute(#T-40'Supplier invoice: &1.':255(995)T-40#,string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))) + chr(10) + 
                                trim(substitute(#T-41'Current Entity: &1.':255(846639773)T-41#,string(viCompanyId))).
        <M-10 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
            input  'QADFIN-785':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not available tqDomainForInterCompany */
    
    assign vcMessage = "":U.
    if tqDomainForInterCompany.tcIntercoAPGLCode = "":U and 
       tqDomainForInterCompany.tcIntercoAPGLCode = ?
    then assign vcMessage = vcMessage + chr(10) + trim(substitute(#T-63'No AP cross-company control account was defined for domain (&1).':255(38099086)T-63#,vcDomainCode)).
    if tqDomainForInterCompany.tlIntercoAPGLIsIntercoAccount <> true
    then assign vcMessage = vcMessage + chr(10) + trim(#T-42'The GL account must be an intercompany account.':255(997)T-42#).
    if tqDomainForInterCompany.tlIntercoAPGLIsCostCentreAccount and 
       (tqDomainForInterCompany.tiIntercoAPCostCentreProfile_ID = 0 or
        tqDomainForInterCompany.tiIntercoAPCostCentreProfile_ID = ?)
    then assign vcMessage = vcMessage + chr(10) + trim(#T-43'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-43#).
    if tqDomainForInterCompany.tlIntercoAPGLIsProjectAccount and 
       (tqDomainForInterCompany.tiIntercoAPProjectProfile_ID = 0 or
        tqDomainForInterCompany.tiIntercoAPProjectProfile_ID = ?)
    then assign vcMessage = vcMessage + chr(10) + trim(#T-44'No project was specified, and the GL account is defined with project analysis. A default project is mandatory for automatic postings.':255(999)T-44#).
    if tqDomainForInterCompany.tlIntercoAPGLIsDivisionAccount and 
       (tqDomainForInterCompany.tiIntercoAPDivisionProfile_ID  = 0 or 
        tqDomainForInterCompany.tiIntercoAPDivisionProfile_ID = ?)
    then assign vcMessage = vcMessage + chr(10) + trim(#T-45'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-45#).
    if vcMessage <> "":U and LENGTH(vcMessage,"CHARACTER":U) > 1
    then do :
        assign vcMessage = trim(substitute(#T-46'A cross-company error occurred. GL account &1 is not defined in the system or is invalid.':255(284235593)T-46#,tqDomainForInterCompany.tcIntercoAPGLCode)) + chr(10) + 
                           trim(substitute(#T-47'Supplier invoice: &1.':255(995)T-47#,string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))) + chr(10) + 
                           substring(vcMessage,2,-1,"CHARACTER":U).
        <M-20 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
            input  'QADFIN-850':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        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.tiIntercoAPDivisionProfile_ID <> 0 and
       tqDomainForInterCompany.tiIntercoAPDivisionProfile_ID <> ? 
    then do :
        <Q-18 run GetDivisionFromProfile (all) (Read) (Cache)
           (input viCompanyId, (CompanyId)
            input tqDomainForInterCompany.tiIntercoAPDivisionProfile_ID, (DivisionProfileId)
            output dataset tqDivisionFromProfile) in BProfile>
           
        find first tqDivisionFromProfile where tqDivisionFromProfile.tiProfile_ID =  tqDomainForInterCompany.tiIntercoAPDivisionProfile_ID
            no-error.
        if not available tqDivisionFromProfile
        then do:
            assign vcMessage = trim(substitute(#T-48'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-48#,tqDomainForInterCompany.tcIntercoAPGLCode)) + chr(10) + 
                               trim(substitute(#T-49'Supplier invoice: &1.':255(995)T-49#,string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))) + chr(10) + 
                               trim(substitute(#T-50'Profile ID: &1.':255(1102)T-50#,string(tqDomainForInterCompany.tiIntercoAPDivisionProfile_ID))).
            <M-19 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
                input  'QADFIN-849':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end. /* if not avail */
    end. /* if ttqDomainForInterCompany.tiIntercoAPDivisionProfile_ID <> 0 */
    
    /* ============================== */
    /* Retrieve Invoice for CC & Proj */
    /* ============================== */
    <Q-58 run CInvoiceByID (all) (Read) (Cache)
       (input 0, (CompanyId)
        input iiMovementCInvoiceID, (CInvoiceID)
        output dataset tqCInvoiceByID) in BCInvoice>
    find tqCInvoiceByID where
         tqCInvoiceByID.tiCInvoice_ID = iiMovementCInvoiceID
         no-lock no-error.
    if not available tqCInvoiceByID
    then do :
        <M-59 run SetMessage
           (input  trim(subst(#T-60'A cross-company error occurred. The system was unable to locate the supplier invoice based on its ID (&1).':255(1097)T-60#,string(iiMovementCInvoiceID))) (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-5950':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. /* if not available tqCInvoiceByID */
        
    /* =========================================================================================================================== */
    /* Check, if this is the last payment of the invoice, in this case complete LC and CC balance has to be taken for the movement */
    /* =========================================================================================================================== */
    assign vlIsLastPayment = (tqCInvoiceForPayment.tdCInvoiceBalanceCreditTC = tMovement.tdAmountDebitTC  and tMovement.tdAmountDebitTC <> 0 or
                              tqCInvoiceForPayment.tdCInvoiceBalanceDebitTC  = tMovement.tdAmountCreditTC and tMovement.tdAmountCreditTC <> 0).
    
    /* =============================== */    
    /* Calculate the LC and CC amounts */
    /* =============================== */
    if tMovement.tlMovementIsAboutWHT <> true
    then do :        
        /* =========================================================================================================================== */    
        /* When dealing with Cross Company postings, it may be that the Base Currency for both companies are not the same.             */
        /* In this case, we need to make sure we don't take the BC Amounts or Rates from the Invoice, but we have to calculate the     */
        /* rate between TC (of the payment) and BC (of the payment company) on the invoice date. + Same story for CC                   */
        /* =========================================================================================================================== */
        if tqCInvoiceForPayment.tiLCCurrency_ID = viCompanyLCId
        then do:
            assign vdInvoiceExchangeRate    = tqCInvoiceForPayment.tdCInvoiceExchangeRate
                   vdInvoiceRateScale       = tqCInvoiceForPayment.tdCInvoiceRateScale
                   vlBaseCurrencyDifferent  = false.               
        end. /* if tqCInvoiceForPayment.tiLCCurrency_ID = viCompanyLCId */
        else do:
            <M-70 run GetExchangeRate
               (input  viCompanyId (iiCompanyID), 
                input  tqCInvoiceForPayment.tiCInvoiceCurrency_ID (iiFromCurrencyID), 
                input  ? (icFromCurrencyCode), 
                input  viCompanyLCId (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                input  tqCInvoiceForPayment.ttCInvoicePostingDate (itValidityDate), 
                output vdInvoiceExchangeRate (odExchangeRate), 
                output vdInvoiceRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 and viLocalReturn >= 0
            then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn < 0
            then leave MAIN_BLOCK.
            
            assign vlBaseCurrencyDifferent = true.
        end. /* else do: */
                    
        if tqCInvoiceForPayment.tiStatutoryCurrency_ID = viCompanyCCId
        then do:
            assign vdInvoiceCCRate          = tqCInvoiceForPayment.tdCInvoiceCCRate
                   vdInvoiceCCScale         = tqCInvoiceForPayment.tdCInvoiceCCScale
                   vlCCCurrencyDifferent    = false.    
        end. /* if tqCInvoiceForPayment.tiStatutoryCurrency_ID = viCompanyCCId */
        else do:
            <M-51 run GetExchangeRate
               (input  viCompanyId (iiCompanyID), 
                input  tqCInvoiceForPayment.tiCInvoiceCurrency_ID (iiFromCurrencyID), 
                input  ? (icFromCurrencyCode), 
                input  viCompanyCCId (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                input  tqCInvoiceForPayment.ttCInvoicePostingDate (itValidityDate), 
                output vdInvoiceCCRate (odExchangeRate), 
                output vdInvoiceCCScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 and viLocalReturn >= 0
            then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn < 0
            then leave MAIN_BLOCK.
            
            assign vlCCCurrencyDifferent = true.    
        end. /* else do */    
    
        /* ========================= */
        /* Fill in LC and CC Amounts */
        /* ========================= */ 
        if tqCInvoiceForPayment.tiCInvoiceCurrency_ID  = viCompanyLCId or 
           tqCInvoiceForPayment.tcCInvoiceCurrencyCode = vcCompanyLC  
        then assign vdMovementAmountDebitLC   = tMovement.tdAmountDebitTC
                    vdMovementAmountCreditLC  = tMovement.tdAmountCreditTC
                    vdTCLCExchangeRate        = 1
                    vdTCLCExchangeRateScale   = 1.
        else if vlIsLastPayment and not vlBaseCurrencyDifferent /* Last payment of the invoice and base currencies equal */
             then assign vdMovementAmountDebitLC  = tqCInvoiceForPayment.tdCInvoiceBalanceCreditLC
                         vdMovementAmountCreditLC = tqCInvoiceForPayment.tdCInvoiceBalanceDebitLC
                         vdTCLCExchangeRate       = Absolute(vdMovementAmountDebitLC - vdMovementAmountCreditLC) / absolute(idMovementAmountDebitTC - idMovementAmountCreditTC)
                         vdTCLCExchangeRateScale  = 1.
             else /* Special exchange rate is to be used - e. g. Expense Notes  */
                  if (tMovement.tdAmountCreditLC <> 0 and
                      tMovement.tdAmountCreditLC <> ?) or
                     (tMovement.tdAmountDebitLC  <> 0 and
                      tMovement.tdAmountDebitLC  <> ?)
                  then assign vdMovementAmountDebitLC = <M-34 RoundAmount
                                                           (input  tMovement.tdAmountDebitLC (idUnroundedAmount), 
                                                            input  viCompanyLCId (iiCurrencyID), 
                                                            input  ? (icCurrencyCode)) in BApplicationProperty>
                              vdMovementAmountCreditLC = <M-89 RoundAmount
                                                            (input  tMovement.tdAmountCreditLC (idUnroundedAmount), 
                                                             input  viCompanyLCId (iiCurrencyID), 
                                                             input  ? (icCurrencyCode)) in BApplicationProperty>
                              vdTCLCExchangeRate       = Absolute(vdMovementAmountDebitLC - vdMovementAmountCreditLC) / absolute(idMovementAmountDebitTC - idMovementAmountCreditTC)
                              vdTCLCExchangeRateScale  = 1.                              
                  else assign vdMovementAmountDebitLC = <M-170 RoundAmount
                                                           (input  tMovement.tdAmountDebitTC * vdInvoiceExchangeRate * vdInvoiceRateScale (idUnroundedAmount), 
                                                            input  viCompanyLCId (iiCurrencyID), 
                                                            input  ? (icCurrencyCode)) in BApplicationProperty>
                              vdMovementAmountCreditLC = <M-86 RoundAmount
                                                            (input  tMovement.tdAmountCreditTC * vdInvoiceExchangeRate * vdInvoiceRateScale (idUnroundedAmount), 
                                                             input  viCompanyLCId (iiCurrencyID), 
                                                             input  ? (icCurrencyCode)) in BApplicationProperty>
                              vdTCLCExchangeRate        = vdInvoiceExchangeRate
                              vdTCLCExchangeRateScale   = vdInvoiceRateScale.
        if tqCInvoiceForPayment.tiCInvoiceCurrency_ID  = viCompanyCCId or
           tqCInvoiceForPayment.tcCInvoiceCurrencyCode = vcCompanyCC 
        then assign vdMovementAmountDebitCC  = tMovement.tdAmountDebitTC
                    vdMovementAmountCreditCC = tMovement.tdAmountCreditTC
                    vdTCCCExchangeRate        = 1
                    vdTCCCExchangeRateScale   = 1.
        else if vlDomainIsStatutory = false
              then assign vdMovementAmountDebitCC  = vdMovementAmountDebitLC
                          vdMovementAmountCreditCC = vdMovementAmountCreditLC
                          vdTCCCExchangeRate       = vdTCLCExchangeRate
                          vdTCCCExchangeRateScale  = vdTCLCExchangeRateScale.
              else if vlIsLastPayment and not vlCCCurrencyDifferent /* Last payment of the invoice and base currencies equal */
                   then assign vdMovementAmountDebitCC  = tqCInvoiceForPayment.tdCInvoiceBalanceCreditCC
                               vdMovementAmountCreditCC = tqCInvoiceForPayment.tdCInvoiceBalanceDebitCC
                               vdTCCCExchangeRate       = Absolute(vdMovementAmountDebitCC - vdMovementAmountCreditCC) / absolute(idMovementAmountDebitTC - idMovementAmountCreditTC)
                               vdTCCCExchangeRateScale  = 1.
                   else /* Special exchange rate is to be used - e. g. Expense Notes  */ 
                        if (tMovement.tdAmountCreditCC <> 0 and
                            tMovement.tdAmountCreditCC <> ?) or
                           (tMovement.tdAmountDebitCC  <> 0 and
                            tMovement.tdAmountDebitCC  <> ?)
                        then assign vdMovementAmountDebitCC = <M-87 RoundAmount
                                                                 (input  tMovement.tdAmountDebitCC (idUnroundedAmount), 
                                                                  input  viCompanyCCId (iiCurrencyID), 
                                                                  input  ? (icCurrencyCode)) in BApplicationProperty>
                                    vdMovementAmountCreditCC = <M-14 RoundAmount
                                                                  (input  tMovement.tdAmountCreditCC (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  ? (icCurrencyCode)) in BApplicationProperty>
                                    vdTCCCExchangeRate       = Absolute(vdMovementAmountDebitCC - vdMovementAmountCreditCC) / absolute(idMovementAmountDebitTC - idMovementAmountCreditTC)
                                    vdTCCCExchangeRateScale  = 1. 
                        else assign vdMovementAmountDebitCC  = <M-97 RoundAmount
                                                                  (input  tMovement.tdAmountDebitTC * vdInvoiceCCRate * vdInvoiceCCScale (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  ? (icCurrencyCode)) in BApplicationProperty>
                                    vdMovementAmountCreditCC = <M-23 RoundAmount
                                                                  (input  tMovement.tdAmountCreditTC * vdInvoiceCCRate * vdInvoiceCCScale (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  ? (icCurrencyCode)) in BApplicationProperty>
                                    vdTCCCExchangeRate       = vdInvoiceCCRate
                                    vdTCCCExchangeRateScale  = vdInvoiceCCScale.
    end. /* if tMovement.tlMovementIsAboutWHT <> true */
    else do:
        /* ================================= */
        /* Fill in LC and CC Amounts for WHT */
        /* ================================= */
        if tqCInvoiceForPayment.tiCInvoiceCurrency_ID  = viCompanyLCId or 
           tqCInvoiceForPayment.tcCInvoiceCurrencyCode = vcCompanyLC  
        then assign vdMovementAmountDebitLC   = tMovement.tdAmountDebitTC
                    vdMovementAmountCreditLC  = tMovement.tdAmountCreditTC
                    vdTCLCExchangeRate        = 1
                    vdTCLCExchangeRateScale   = 1.
        else if (tMovement.tdAmountCreditLC <> 0 and
                 tMovement.tdAmountCreditLC <> ?) or
                (tMovement.tdAmountDebitLC  <> 0 and
                 tMovement.tdAmountDebitLC  <> ?)
             then assign vdMovementAmountDebitLC  = <M-68 RoundAmount
                                                       (input  tMovement.tdAmountDebitLC (idUnroundedAmount), 
                                                        input  viCompanyLCId (iiCurrencyID), 
                                                        input  ? (icCurrencyCode)) in BCInvoice>
                         vdMovementAmountCreditLC = <M-65 RoundAmount
                                                       (input  tMovement.tdAmountCreditLC (idUnroundedAmount), 
                                                        input  viCompanyLCId (iiCurrencyID), 
                                                        input  ? (icCurrencyCode)) in BCInvoice>
                         vdTCLCExchangeRate       = Absolute(vdMovementAmountDebitLC - vdMovementAmountCreditLC) / absolute(idMovementAmountDebitTC - idMovementAmountCreditTC)
                         vdTCLCExchangeRateScale  = 1.
             else do :
                 if tMovement.tdRateTCLC  = 0 or
                    tMovement.tdScaleTCLC = ? or
                    tMovement.tdRateTCLC  = 0 or
                    tMovement.tdScaleTCLC = ?
                 then do :
                     assign oiReturnStatus = -1
                            vcmessage      = trim(#T-66'Internal error: The input-record for the invoice-movement does not hold a proper rate fot the base-currency.':255(570834916)T-66#).
                     <M-67 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-213654':U (icFcMsgNumber), 
                         input  '':U (icFcExplanation), 
                         input  '':U (icFcIdentification), 
                         input  '':U (icFcContext), 
                         output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                     Leave MAIN_BLOCK.
                 end. /* if tMovement.tdRateTCLC  = 0 or */
                 assign vdMovementAmountDebitLC  = <M-92 RoundAmount
                                                      (input  tMovement.tdAmountDebitTC * tMovement.tdRateTCLC * tMovement.tdScaleTCLC (idUnroundedAmount), 
                                                       input  viCompanyLCId (iiCurrencyID), 
                                                       input  ? (icCurrencyCode)) in BCInvoice>  
                        vdMovementAmountCreditLC = <M-55 RoundAmount
                                                      (input  tMovement.tdAmountCreditTC * tMovement.tdRateTCLC * tMovement.tdScaleTCLC (idUnroundedAmount), 
                                                       input  viCompanyLCId (iiCurrencyID), 
                                                       input  ? (icCurrencyCode)) in BCInvoice>
                        vdTCLCExchangeRate       = tMovement.tdRateTCLC
                        vdTCLCExchangeRateScale  = tMovement.tdScaleTCLC.
             end. /* else do */  
        if tqCInvoiceForPayment.tiCInvoiceCurrency_ID  = viCompanyCCId or 
           tqCInvoiceForPayment.tcCInvoiceCurrencyCode = vcCompanyCC  
        then assign vdMovementAmountDebitCC   = tMovement.tdAmountDebitTC
                    vdMovementAmountCreditCC  = tMovement.tdAmountCreditTC
                    vdTCCCExchangeRate        = 1
                    vdTCCCExchangeRateScale   = 1.
        else if (tMovement.tdAmountCreditCC <> 0 and
                 tMovement.tdAmountCreditCC <> ?) or
                (tMovement.tdAmountDebitCC  <> 0 and
                 tMovement.tdAmountDebitCC  <> ?)
             then assign vdMovementAmountDebitCC  = <M-84 RoundAmount
                                                       (input  tMovement.tdAmountDebitCC (idUnroundedAmount), 
                                                        input  viCompanyCCId (iiCurrencyID), 
                                                        input  ? (icCurrencyCode)) in BCInvoice>
                         vdMovementAmountCreditCC = <M-81 RoundAmount
                                                       (input  tMovement.tdAmountCreditCC (idUnroundedAmount), 
                                                        input  viCompanyCCId (iiCurrencyID), 
                                                        input  ? (icCurrencyCode)) in BCInvoice>
                         vdTCCCExchangeRate       = Absolute(vdMovementAmountDebitLC - vdMovementAmountCreditLC) / absolute(idMovementAmountDebitTC - idMovementAmountCreditTC)
                         vdTCCCExchangeRateScale  = 1.
             else do :
                 if tMovement.tdRateTCCC  = 0 or
                    tMovement.tdScaleTCCC = ? or
                    tMovement.tdRateTCCC  = 0 or
                    tMovement.tdScaleTCCC = ?
                 then do :
                     assign oiReturnStatus = -1
                            vcmessage      = trim(#T-90'Internal error: The input-record for the invoice-movement does not hold a proper rate fot the statutory-currency.':255(198000521)T-90#).
                     <M-581 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-816923':U (icFcMsgNumber), 
                         input  '':U (icFcExplanation), 
                         input  '':U (icFcIdentification), 
                         input  '':U (icFcContext), 
                         output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                     Leave MAIN_BLOCK.
                 end. /* if tMovement.tdRateTCCC  = 0 or */
                 assign vdMovementAmountDebitCC  = <M-79 RoundAmount
                                                      (input  tMovement.tdAmountDebitTC * tMovement.tdRateTCCC * tMovement.tdScaleTCCC (idUnroundedAmount), 
                                                       input  viCompanyCCId (iiCurrencyID), 
                                                       input  ? (icCurrencyCode)) in BCInvoice>  
                        vdMovementAmountCreditCC = <M-30 RoundAmount
                                                      (input  tMovement.tdAmountCreditTC * tMovement.tdRateTCCC * tMovement.tdScaleTCCC (idUnroundedAmount), 
                                                       input  viCompanyCCId (iiCurrencyID), 
                                                       input  ? (icCurrencyCode)) in BCInvoice>
                        vdTCCCExchangeRate       = tMovement.tdRateTCCC
                        vdTCCCExchangeRateScale  = tMovement.tdScaleTCCC.
            end. /* else do : */
    end. /* else do: */
    
    /* ================================================================================================================= */
    /* 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-62 run AddStandardPosting
        (input  icPostingRowId (icPostingtcRowid), 
         input  tqDomainForInterCompany.tcIntercoAPGLCode (icGLCode), 
         input  (if available tqDivisionFromProfile then tqDivisionFromProfile.tcDivisionCode else '':U) (icDivisionCode), 
         input  tqCInvoiceByID.tcCostCentreCode (icCostCentreCode), 
         input  '':U (icCostCentreText), 
         input  tqCInvoiceByID.tcProjectCode (icProjectCode), 
         input  '':U (icProjectText), 
         input  tqCompanyPropertyByBusRelSimple.tcBusinessRelationICCode (icIntercoBusinessRelationCode), 
         input  tqCInvoiceForPayment.tcCInvoiceCurrencyCode (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-61'Cross-Company Posting':40(59434)T-61# (icLineText), 
         input  '':U (icSafText), 
         input  tDefaultSafsCI (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-52'A cross-company error occurred  while creating the control posting. See other messages for further details.':255(1104)T-52#) + chr(10) + 
                           trim(substitute(#T-53'Supplier invoice: &1.':255(995)T-53#,string(tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear) + "/":U + string(tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher,"999999999":U))).
        <M-26 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tqCInvoiceByCyIDYearJrnlVchr.tc_rowid (icRowid), 
            input  'QADFIN-851':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        leave MAIN_BLOCK.
    end. /* if oiReturnStatus < 0 */
    
    else do:
        if viControlPostingLineId <> 0 and
           viControlPostingLineId <> ?
        then do:
            if not can-find(first tPostingLinesForQCrossCompany where
                                  tPostingLinesForQCrossCompany.tiPostingLineID = viControlPostingLineId)
            then do:
                create tPostingLinesForQCrossCompany.
                assign tPostingLinesForQCrossCompany.tiPostingLineID = viControlPostingLineId.
            end.
        end.
    end. /* else do: */                                          
   
    
    /* ================================================================== */
    /* 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 CI */
    /* First convert icPostingRowId into the PostingID                    */
    /* ================================================================== */
    <M-31 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.
    
    /* do some important validations before cross cy daemon request is created */
   
    <M-95 run ValidateCInvoiceMovementCrossCy
       (input  viControlPostingLineId (iiPostingLine_ID), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper < 0 or viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAIN_BLOCK.
    
   
    <M-32 run CreateCInvoiceMovementsCrossCyExt
       (input  viPostingId (iiOriginatorPostingID), 
        input  viControlPostingLineId (iiOriginatorPostingLineId), 
        input  idMovementAmountCreditTC (idMovementAmountCreditTC), 
        input  idMovementAmountDebitTC (idMovementAmountDebitTC), 
        input  idMovementAmountCreditLC (idMovementAmountCreditLC), 
        input  idMovementAmountDebitLC (idMovementAmountDebitLC), 
        input  idMovementAmountCreditCC (idMovementAmountCreditCC), 
        input  idMovementAmountDebitCC (idMovementAmountDebitCC), 
        input  tqCInvoiceByCyIDYearJrnlVchr.tiCInvoice_ID (iiCInvoiceID), 
        input  tqCInvoiceByCyIDYearJrnlVchr.tiCompany_ID (iiCInvoiceCompanyID), 
        input  iiCDocInvoiceXrefID (iiCDocumentInvoiceXrefID), 
        input  tMovement.tlMovementIsAboutWHT (ilMovementIsAboutWHT), 
        input  tMovement.ttPaymentDate (itPaymentDate), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    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.