project QadFinancials > class BCInvoice > method ApiStdMaintainTTCalcAmounts

Description

This method is a submethod of ApiStdMaintainTT.
This method calculate lc, cc and balance amounts


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ApiStdMaintainTTV01


program code (program9/bcinvoice.p)

/* ==================================================================================================*/
/*  Integration - valid for following situations                                                     */
/*    1.  TC entered only - then get exchange rates (LC & CC), and calculate LC/CC amounts           */
/*    2.  TC + LC exchange rate - get CC rate and calculate LC/CC amounts                            */
/*    3.  TC + CC exchange rate - get LC rate and calculate LC/CC amounts                            */
/*    4.  TC + LC amounts - calculate LC rate from amounts - get CC rate and calculate CC amounts    */
/*    5.  TC + LC + CC amounts - calculate LC and CC rates.                                          */
/*  Validation added to ensure exchange rate calculations are correct for TC/LC and TC/CC            */
/*        normal situation: TC * Rate * Scale = LC (test incorporates tolerance                      */
/* ==================================================================================================*/
assign oiReturnStatus = -98
       viLocalReturnStatus = 0.

CALCAMOUNTSBLOCK:
DO:
    /* check that the tcCurrencyCode is set - if not no need to do these calculations */
    if (tApiCInvoice.CInvoiceCurrency_ID = 0 or tApiCInvoice.CInvoiceCurrency_ID = ?)  and
        (tApiCInvoice.tcCurrencyCode = "" or tApiCInvoice.tcCurrencyCode = ?)
        then leave CALCAMOUNTSBLOCK.

    /* check that the postingdate is set - if not no need to do these calculations */
    if tApiCInvoice.CInvoicePostingDate = ?
        then leave CALCAMOUNTSBLOCK.

    /* ensure that if the TC value is 0, then the other values are also 0 */
    if (((if tApiCInvoice.CInvoiceOriginalDebitTC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitTC) - 
                 (if tApiCInvoice.CInvoiceOriginalCreditTC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditTC)) = 0)  and
            ((((if tApiCInvoice.CInvoiceOriginalDebitLC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitLC) - 
                 (if tApiCInvoice.CInvoiceOriginalCreditLC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditLC)) <> 0) or
                (((if tApiCInvoice.CInvoiceOriginalDebitCC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitCC) - 
                 (if tApiCInvoice.CInvoiceOriginalCreditCC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditCC)) <> 0) )
    then do:
         assign vcMessage  = trim(substitute(#T-73'When TC amounts total 0, then BC and CC amounts must also total 0.':255(748249262)T-73#)) 
                viLocalReturnStatus = -1.

        <M-7 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  'tApiCInvoice.CInvoiceOriginalCreditTC':U (icFieldName), 
            input  string(tApiCInvoice.CInvoiceOriginalCreditTC) (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-422950':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        leave CALCAMOUNTSBLOCK.
    end. /*if*/

    /* check to see if the scale is set for LC and CC exchange rates
        Note:  this will get overridden if APIGetExchangeRate is used to retrieve
        either exchange rates
    */    
    if tApiCInvoice.CInvoiceRateScale = 0 or tApiCInvoice.CInvoiceRateScale = ? 
        then assign tApiCInvoice.CInvoiceRateScale = 1.
    if tApiCInvoice.CInvoiceCCScale = 0 or tApiCInvoice.CInvoiceCCScale = ? 
        then assign tApiCInvoice.CInvoiceCCScale = 1.
        
    /* Calculation for LC */
    if tApiCInvoice.CInvoiceCurrency_ID = viCompanyLCId or
       tApiCInvoice.tcCurrencyCode = vcCompanyLC  
    then assign tApiCInvoice.CInvoiceExchangeRate     = 1
                tApiCInvoice.CinvoiceRateScale        = 1
                tApiCInvoice.CInvoiceOriginalDebitLC  = tApiCInvoice.CInvoiceOriginalDebitTC
                tApiCInvoice.CInvoiceOriginalCreditLC = tApiCInvoice.CInvoiceOriginalCreditTC.
    else 
    if (tApiCInvoice.CInvoiceOriginalDebitLC = 0 or tApiCInvoice.CInvoiceOriginalDebitLC = ?) and
       (tApiCInvoice.CInvoiceOriginalCreditLC = 0 or tApiCInvoice.CInvoiceOriginalCreditLC = ?)
    then do:
        /* check to see if the rate is set */
         if (tApiCInvoice.CInvoiceExchangeRate = 0 or tApiCInvoice.CInvoiceExchangeRate = ?)
        then do:            
            
            <M-21 run GetExRateByEntityInvoiceFlag
               (input  tApiCInvoice.CInvoiceDate (itCInvoiceDate), 
                input  tApiCInvoice.CInvoicePostingDate (itCInvoicePostingDate), 
                input  tApiCInvoice.CInvoiceCurrency_ID (iiFromCurrencyId), 
                input  if tApiCInvoice.CInvoiceCurrency_ID = 0 or tApiCInvoice.CInvoiceCurrency_ID = ? then tApiCInvoice.tcCurrencyCode else '':U (icFromCurrencyCode), 
                input  viCompanyLCId (iiToCurrencyId), 
                input  ? (icToCurrencyCode), 
                input  0 (iiExchangeRateTypeId), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                output tApiCInvoice.CInvoiceExchangeRate (odCInvoiceExchangeRate), 
                output tApiCInvoice.CInvoiceRateScale (odCInvoiceRateScale), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                
            if viFcReturnSuper <> 0
            then assign viLocalReturnStatus = viFcReturnSuper.    
            if viFcReturnSuper < 0
            then leave CALCAMOUNTSBLOCK.
        end. /* if (tApiCInvoice.CInvoiceExchangeRate = 0 */
        
        /* calculate the LC amounts */
        assign tApiCInvoice.CInvoiceOriginalDebitLC  = <M-3 RoundAmount
                                                          (input  tApiCInvoice.CInvoiceOriginalDebitTC * tApiCInvoice.CInvoiceExchangeRate * tApiCInvoice.CinvoiceRateScale (idUnroundedAmount), 
                                                           input  viCompanyLCId (iiCurrencyID), 
                                                           input  vcCompanyLC (icCurrencyCode)) in business>       
               tApiCInvoice.CInvoiceOriginalCreditLC = <M-4 RoundAmount
                                                          (input  tApiCInvoice.CInvoiceOriginalCreditTC * tApiCInvoice.CInvoiceExchangeRate * tApiCInvoice.CinvoiceRateScale (idUnroundedAmount), 
                                                           input  viCompanyLCId (iiCurrencyID), 
                                                           input  vcCompanyLC (icCurrencyCode)) in business>.               
    end. /* if (tApiCInvoice.CInvoiceOriginalDebitLC */
    else do:
        if ((tApiCInvoice.CInvoiceOriginalDebitLC  <> 0 and tApiCInvoice.CInvoiceOriginalDebitLC  <> ?) or
            (tApiCInvoice.CInvoiceOriginalCreditLC <> 0 and tApiCInvoice.CInvoiceOriginalCreditLC <> ?)) and
           ((tApiCInvoice.CInvoiceOriginalDebitTC  <> 0 and tApiCInvoice.CInvoiceOriginalDebitTC  <> ?) or 
            (tApiCInvoice.CInvoiceOriginalCreditTC <> 0 and tApiCInvoice.CInvoiceOriginalCreditTC <> ?))
        then do:
            /*
               If LC and exchange rate is set - use exchange rate from xml otherwise calculate it
            */
            if (tApiCInvoice.CInvoiceExchangeRate = 0 or tApiCInvoice.CInvoiceExchangeRate = ?)
            then do:
               assign tApiCInvoice.CInvoiceExchangeRate =                 
                ((if tApiCInvoice.CInvoiceOriginalDebitLC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitLC) - 
                 (if tApiCInvoice.CInvoiceOriginalCreditLC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditLC)) 
                 /
                ((if tApiCInvoice.CInvoiceOriginalDebitTC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitTC) - 
                 (if tApiCInvoice.CInvoiceOriginalCreditTC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditTC)) no-error.
                /* check the error status */
                if error-status:error 
                then do:
                     
                    assign vcMessage  = trim(substitute(#T-18'Error calculating exchange rate from BC and TC amounts':255(88263408)T-18#))  + chr(10) +
                                    trim(substitute(#T-94'TC Credit Amount in invoice: &1.':255(210144214)T-94#, trim(string(tApiCInvoice.CInvoiceOriginalCreditTC)))) + chr(10) + 
                                    trim(substitute(#T-16'TC Debit Amount in invoice: &1.':255(602977159)T-16#, trim(string(tApiCInvoice.CInvoiceOriginalDebitTC)))) + chr(10) + 
                                    trim(substitute(#T-12'BC Credit Amount in invoice: &1.':255(920191623)T-12#, trim(string(tApiCInvoice.CInvoiceOriginalCreditLC)))) + chr(10) + 
                                    trim(substitute(#T-31'BC Debit Amount in invoice: &1.':255(956484826)T-31#, trim(string(tApiCInvoice.CInvoiceOriginalDebitLC)))).
                    
                    /* assign vcMessage = "error happened". */
                    viLocalReturnStatus = -1.
                   <M-45 run SetMessage
                      (input  vcMessage (icMessage), 
                       input  '':U (icArguments), 
                       input  'tApiCInvoice.CInvoiceExchangeRate':U (icFieldName), 
                       input  string(tApiCInvoice.CInvoiceExchangeRate) (icFieldValue), 
                       input  'E':U (icType), 
                       input  3 (iiSeverity), 
                       input  '':U (icRowid), 
                       input  'qadfin-754036':U (icFcMsgNumber), 
                       input  '' (icFcExplanation), 
                       input  '' (icFcIdentification), 
                       input  '' (icFcContext), 
                       output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                    leave CALCAMOUNTSBLOCK.
                 end.  /* if error-status:error */ 
            end. /* if (tApiCInvoice.CInvoiceExchangeRate */
        end. /* if ((tApiCInvoice.CInvoiceOriginalDebitLC  <> 0 */
    end. /* else do */
    if tApiCInvoice.CinvoiceRateScale =  0 
        then tApiCInvoice.CinvoiceRateScale = 1.
    /* Calculation for CC */
    if viCompanyCCId <> 0 and viCompanyCCId <> ?
    then do:
        if tApiCInvoice.tcCurrencyCode = vcCompanyCC
        then assign tApiCInvoice.CInvoiceOriginalDebitCC  = tApiCInvoice.CInvoiceOriginalDebitTC
                    tApiCInvoice.CInvoiceOriginalCreditCC = tApiCInvoice.CInvoiceOriginalCreditTC.
        else if vlDomainIsStatutory = false
        then assign tApiCInvoice.CInvoiceOriginalDebitCC  = tApiCInvoice.CInvoiceOriginalDebitLC
                    tApiCInvoice.CInvoiceOriginalCreditCC = tApiCInvoice.CInvoiceOriginalCreditLC.
        else do:
        /* if CC is not set then calculate it by getting the exchange rate */
            if (tApiCInvoice.CInvoiceOriginalDebitCC = 0 or tApiCInvoice.CInvoiceOriginalDebitCC = ?) and
               (tApiCInvoice.CInvoiceOriginalCreditCC = 0 or tApiCInvoice.CInvoiceOriginalCreditCC = ?)
            then do:
            /*
               If CC exchange rate is set - use exchange rate from xml otherwise calculate it
            */                
                if (tApiCInvoice.CInvoiceCCRate = 0 or tApiCInvoice.CInvoiceCCRate = ?)
                then do:
                    
                    <M-61 run GetExRateByEntityInvoiceFlag
                       (input  tApiCInvoice.CInvoiceDate (itCInvoiceDate), 
                        input  tApiCInvoice.CInvoicePostingDate (itCInvoicePostingDate), 
                        input  0 (iiFromCurrencyId), 
                        input  tApiCInvoice.tcCurrencyCode (icFromCurrencyCode), 
                        input  viCompanyCCId (iiToCurrencyId), 
                        input  ? (icToCurrencyCode), 
                        input  0 (iiExchangeRateTypeId), 
                        input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                        output tApiCInvoice.CInvoiceCCRate (odCInvoiceExchangeRate), 
                        output tApiCInvoice.CInvoiceCCScale (odCInvoiceRateScale), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                        
                    if viFcReturnSuper <> 0 
                    then assign viLocalReturnStatus = viFcReturnSuper.    
                    if viFcReturnSuper < 0
                    then leave CALCAMOUNTSBLOCK.
                    
                    <M-29 run CheckLinkedInvoiceApiCC
                       (input  tApiCInvoice.tc_Rowid (icApiInvoiceRowid), 
                        input-output tApiCInvoice.CInvoiceCCRate (bdExchangeRate), 
                        input-output tApiCInvoice.CInvoiceCCScale (bdRateScale), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                        
                    if viFcReturnSuper <> 0 
                    then assign viLocalReturnStatus = viFcReturnSuper.    
                    if viFcReturnSuper < 0
                    then leave CALCAMOUNTSBLOCK.
                    
                end. /* if (tApiCInvoice.CInvoiceCCRate = 0 */
            
                assign 
                        tApiCInvoice.CInvoiceOriginalDebitCC  = <M-5 RoundAmount
                                                              (input  tApiCInvoice.CInvoiceOriginalDebitTC * tApiCinvoice.CInvoiceCCRate * tApiCinvoice.CInvoiceCCScale (idUnroundedAmount), 
                                                               input  viCompanyCCId (iiCurrencyID), 
                                                               input  vcCompanyCC (icCurrencyCode)) in business>
                        tApiCInvoice.CInvoiceOriginalCreditCC = <M-6 RoundAmount
                                                              (input  tApiCInvoice.CInvoiceOriginalCreditTC * tApiCinvoice.CInvoiceCCRate * tApiCinvoice.CInvoiceCCScale (idUnroundedAmount), 
                                                               input  viCompanyCCId (iiCurrencyID), 
                                                               input  vcCompanyCC (icCurrencyCode)) in business>.                       
            end. /* if (tApiCInvoice.CInvoiceOriginalDebitCC = 0 */                                                              
            else do: 
                if ((tApiCInvoice.CInvoiceOriginalDebitCC  <> 0 and tApiCInvoice.CInvoiceOriginalDebitCC  <> ?) or
                    (tApiCInvoice.CInvoiceOriginalCreditCC <> 0 and tApiCInvoice.CInvoiceOriginalCreditCC <> ?)) and
                   ((tApiCInvoice.CInvoiceOriginalDebitTC  <> 0 and tApiCInvoice.CInvoiceOriginalDebitTC  <> ?) or 
                    (tApiCInvoice.CInvoiceOriginalCreditTC <> 0 and tApiCInvoice.CInvoiceOriginalCreditTC <> ?))
                then do:
                    /*  
                       If CC and exchange rate is set - use exchange rate from xml
                       otherwise calculate it
                    */
                    if (tApiCInvoice.CInvoiceCCRate = 0 or tApiCInvoice.CInvoiceCCRate = ?)
                    then do:
                       assign tApiCInvoice.CInvoiceCCRate =                 
                        ((if tApiCInvoice.CInvoiceOriginalDebitCC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitCC) - 
                         (if tApiCInvoice.CInvoiceOriginalCreditCC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditCC)) 
                         /
                        ((if tApiCInvoice.CInvoiceOriginalDebitTC  = ? then 0 else tApiCInvoice.CInvoiceOriginalDebitTC) - 
                         (if tApiCInvoice.CInvoiceOriginalCreditTC = ? then 0 else tApiCInvoice.CInvoiceOriginalCreditTC)) no-error.
                         
                       /* check the error status */
                        if error-status:error 
                        then do:
                             
                            assign vcMessage  = trim(substitute(#T-85'Error calculating exchange rate from TC and SC amounts':255(30662463)T-85#))  + chr(10) +
                                            trim(substitute(#T-83'TC Credit Amount in invoice: &1.':255(210144214)T-83#, trim(string(tApiCInvoice.CInvoiceOriginalCreditTC)))) + chr(10) + 
                                            trim(substitute(#T-81'TC Debit Amount in invoice: &1.':255(602977159)T-81#, trim(string(tApiCInvoice.CInvoiceOriginalDebitTC)))) + chr(10) + 
                                            trim(substitute(#T-89'SC Credit Amount in invoice: &1.':255(93914103)T-89#, trim(string(tApiCInvoice.CInvoiceOriginalCreditCC)))) + chr(10) + 
                                            trim(substitute(#T-639'SC Debit Amount in invoice: &1.':255(127077815)T-639#, trim(string(tApiCInvoice.CInvoiceOriginalDebitCC)))).
                            
                            /* assign vcMessage = "error happened". */
                            viLocalReturnStatus = -1.
                           <M-40 run SetMessage
                              (input  vcMessage (icMessage), 
                               input  '':U (icArguments), 
                               input  'tApiCInvoice.CInvoiceCCRate':U (icFieldName), 
                               input  string(tApiCInvoice.CInvoiceCCRate) (icFieldValue), 
                               input  'E':U (icType), 
                               input  3 (iiSeverity), 
                               input  '':U (icRowid), 
                               input  'qadfin-594690':U (icFcMsgNumber), 
                               input  '' (icFcExplanation), 
                               input  '' (icFcIdentification), 
                               input  '' (icFcContext), 
                               output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                            leave CALCAMOUNTSBLOCK.
                         end.  /* if error-status:error */ 
                     end. /* if (tApiCInvoice.CInvoiceCCRate = 0 */
                end. /* if ((tApiCInvoice.CInvoiceOriginalDebitCC  <> 0 */      
           end. /* else do: */                                                        
        end. /* else do: */
    end. /* if viCompanyCCId <> 0 and viCompanyCCId <> ? */

   /* ============================================================================================================ */
    /* New extention only applicable for the interfaces:                                                           */
    /*  - Note: normal situation: TC * Rate * Scale = LC                                                           */
    /* ExchangeRate and ScaleFactor and TC and LC are filled at this state - check here if                         */
    /* - LC (rounded by viCompanyLCDec)                                                                            */
    /* - TC * Rate * Scale (rounded by viCompanyLCDec)                                                             */
    /* Are the same with an exception on the last significant digit                                                */
    /* =========================================================================================================== */
    if ((tApiCInvoice.CInvoiceOriginalCreditLC <> 0 and
         tApiCInvoice.CInvoiceOriginalCreditLC <> ?) or
        (tApiCInvoice.CInvoiceOriginalDebitLC  <> 0 and
         tApiCInvoice.CInvoiceOriginalDebitLC  <> ?))     and 
       ((tApiCInvoice.CInvoiceOriginalCreditTC <> 0 and
         tApiCInvoice.CInvoiceOriginalCreditTC <> ?) or
        (tApiCInvoice.CInvoiceOriginalDebitTC  <> 0 and
         tApiCInvoice.CInvoiceOriginalDebitTC  <> ?))     and 
       tApiCInvoice.CInvoiceExchangeRate <> 0     and
       tApiCInvoice.CInvoiceExchangeRate <> ?     and
       tApiCInvoice.CinvoiceRateScale    <> 0     and
       tApiCInvoice.CinvoiceRateScale    <> ?            
    then do :
    
        /* Check if the passed LC is acceptable */
        assign vdCInvoiceAmountInputLC = tApiCInvoice.CInvoiceOriginalCreditLC - tApiCInvoice.CInvoiceOriginalDebitLC
               vdCinvoiceAmountCalcLC = <M-64 RoundAmount
                                          (input  (tApiCInvoice.CInvoiceOriginalCreditTC - tApiCInvoice.CInvoiceOriginalDebitTC) * tApiCInvoice.CInvoiceExchangeRate * tApiCInvoice.CInvoiceRateScale (idUnroundedAmount), 
                                           input  viCompanyLCId (iiCurrencyID), 
                                           input  vcCompanyLC (icCurrencyCode)) in business>.                                                              
               
        if viCompanyLCDec = 0 or viCompanyLCDec = ?
        then assign vdTolerance = 1.
        else assign vdTolerance = 1 / (10 * viCompanyLCDec).
        if ABS (vdCInvoiceAmountInputLC - vdCInvoiceAmountCalcLC) > vdTolerance
        then do :
                assign vcMessage  = trim(substitute(#T-79'The rounded values for the BC Amount and the TC Amount (multiplied by the Exchange Rate and the Rate Scale) should be the same (except the last significant character)':255(73470646)T-79#)) + chr(10) +
                                    trim(substitute(#T-24'When you pass amounts and exchange rates for invoices in foreign currency, the allowed differences between the TC amount (plus rates) and BC amount is limited':255(384053095)T-24#)) + chr(10) +
                                    trim(substitute(#T-92'TC Credit Amount in invoice: &1.':255(210144214)T-92#, trim(string(tApiCInvoice.CInvoiceOriginalCreditTC)))) + chr(10) + 
                                    trim(substitute(#T-48'TC Debit Amount in invoice: &1.':255(602977159)T-48#, trim(string(tApiCInvoice.CInvoiceOriginalDebitTC)))) + chr(10) + 
                                    trim(substitute(#T-41'BC Credit Amount in invoice: &1.':255(920191623)T-41#, trim(string(tApiCInvoice.CInvoiceOriginalCreditLC)))) + chr(10) + 
                                    trim(substitute(#T-84'BC Debit Amount in invoice: &1.':255(956484826)T-84#, trim(string(tApiCInvoice.CInvoiceOriginalDebitLC)))) + chr(10) + 
                                    trim(substitute(#T-30'BC Amount in invoice: &1':255(696778381)T-30#,trim(string(abs (tApiCInvoice.CInvoiceOriginalCreditLC - tApiCInvoice.CInvoiceOriginalDebitLC))))) + chr(10) +                                                                         
                                    trim(substitute(#T-49'Tolerance Value: &1':255(73460411)T-49#,string (vdTolerance))) +                                                                     
                                    trim(substitute(#T-32'Exchange Rate in invoice: &1.':255(644609108)T-32#, trim(string(tApiCInvoice.CInvoiceExchangeRate)))) + chr(10) + 
                                    trim(substitute(#T-65'Rate Scale in invoice : &1.':255(120858645)T-65#, trim(string(tApiCInvoice.CInvoiceRateScale)))) + chr(10) + 
                                    trim(substitute(#T-23'Number of decimals for BC amounts: &1.':255(31354)T-23#, trim(string(viCompanyLCDec))))
                   viLocalReturnStatus = -1.
                  
            <M-39 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'tApiCInvoice.CInvoiceOriginalCreditTC':U (icFieldName), 
                input  string(tApiCInvoice.CInvoiceOriginalCreditTC) (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-170657':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            leave CALCAMOUNTSBLOCK.
        end. /* if vdCInvoiceAmountTC <> vdCInvoiceAmountLC */
    end. /* if */
   /* =========================================================================================================== */
   /* Repeat test for CC fields   - if statutory currency is used                                                 */
   /* =========================================================================================================== */
    if (vlDomainIsStatutory = true and
        (tApiCInvoice.CInvoiceOriginalCreditCC <> 0 and
         tApiCInvoice.CInvoiceOriginalCreditCC <> ?) or
        (tApiCInvoice.CInvoiceOriginalDebitCC  <> 0 and
         tApiCInvoice.CInvoiceOriginalDebitCC  <> ?))     and 
       ((tApiCInvoice.CInvoiceOriginalCreditTC <> 0 and
         tApiCInvoice.CInvoiceOriginalCreditTC <> ?) or
        (tApiCInvoice.CInvoiceOriginalDebitTC  <> 0 and
         tApiCInvoice.CInvoiceOriginalDebitTC  <> ?))     and 
       tApiCInvoice.CInvoiceCCRate <> 0     and
       tApiCInvoice.CInvoiceCCRate <> ?     and
       tApiCInvoice.CInvoiceCCScale    <> 0     and
       tApiCInvoice.CInvoiceCCScale    <> ?          
    then do :
    
        /* Check if the passed CC is acceptable */
        assign vdCInvoiceAmountInputLC = tApiCInvoice.CInvoiceOriginalCreditCC - tApiCInvoice.CInvoiceOriginalDebitCC
               vdCinvoiceAmountCalcLC = <M-52 RoundAmount
                                           (input  (tApiCInvoice.CInvoiceOriginalCreditTC - tApiCInvoice.CInvoiceOriginalDebitTC) * tApiCInvoice.CInvoiceCCRate * tApiCInvoice.CInvoiceCCScale (idUnroundedAmount), 
                                            input  viCompanyCCId (iiCurrencyID), 
                                            input  vcCompanyCC (icCurrencyCode)) in business>. 
                                                                                                       
               
        if viCompanyCCDec = 0 or viCompanyCCDec = ?
        then assign vdTolerance = 1.
        else assign vdTolerance = 1 / (10 * viCompanyCCDec).
        if ABS (vdCInvoiceAmountInputLC - vdCInvoiceAmountCalcLC) > vdTolerance
        then do :
                assign vcMessage  = trim(substitute(#T-22'The rounded values for the SC Amount and the TC Amount (multiplied by the Exchange Rate and the Rate Scale) should be the same (except the last significant character)':255(556570023)T-22#)) + chr(10) +
                                    trim(substitute(#T-88'When you pass amounts and exchange rates for invoices in foreign currency, the allowed differences between the TC amount (plus rates) and SC amount is limited':255(371672440)T-88#)) + chr(10) +
                                    trim(substitute(#T-50'TC Credit Amount in invoice: &1.':255(210144214)T-50#, trim(string(tApiCInvoice.CInvoiceOriginalCreditTC)))) + chr(10) + 
                                    trim(substitute(#T-9'TC Debit Amount in invoice: &1.':255(602977159)T-9#, trim(string(tApiCInvoice.CInvoiceOriginalDebitTC)))) + chr(10)  + 
                                    trim(substitute(#T-70'SC Credit Amount in invoice: &1.':255(93914103)T-70#, trim(string(tApiCInvoice.CInvoiceOriginalCreditCC)))) + chr(10) + 
                                    trim(substitute(#T-17'SC Debit Amount in invoice: &1.':255(127077815)T-17#, trim(string(tApiCInvoice.CInvoiceOriginalDebitCC)))) + chr(10) + 
                                    trim(substitute(#T-91'SC Amount in invoice: &1':255(385461765)T-91#,trim(string(abs (tApiCInvoice.CInvoiceOriginalCreditCC - tApiCInvoice.CInvoiceOriginalDebitCC))))) + chr(10) +                                                                         
                                    trim(substitute(#T-27'Tolerance Value: &1':255(73460411)T-27#,string (vdTolerance))) +                                                                     
                                    trim(substitute(#T-34'Exchange Rate in invoice: &1.':255(644609108)T-34#, trim(string(tApiCInvoice.CInvoiceCCRate)))) + chr(10) + 
                                    trim(substitute(#T-58'Rate Scale in invoice : &1.':255(120858645)T-58#, trim(string(tApiCInvoice.CInvoiceCCScale)))) + chr(10) + 
                                    trim(substitute(#T-25'Number of decimals for SC amounts: &1.':255(33985321)T-25#, trim(string(viCompanyCCDec))))
                   viLocalReturnStatus = -1.
                  
            <M-11 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'tApiCInvoice.CInvoiceOriginalCreditTC':U (icFieldName), 
                input  string(tApiCInvoice.CInvoiceOriginalCreditTC) (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-393538':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            leave CALCAMOUNTSBLOCK.
        end. /* if vdCInvoiceAmountTC <> vdCInvoiceAmountCC */
   end. /* if */

    /* fill balance fields */
    
    assign tApiCInvoice.CInvoiceBalanceDebitTC = tApiCInvoice.CInvoiceBalanceDebitTC + tApiCInvoice.CInvoiceOriginalDebitTC
           tApiCInvoice.CInvoiceBalanceDebitLC = tApiCInvoice.CInvoiceBalanceDebitLC + tApiCInvoice.CInvoiceOriginalDebitLC
           tApiCInvoice.CInvoiceBalanceDebitCC = tApiCInvoice.CInvoiceBalanceDebitCC + tApiCInvoice.CInvoiceOriginalDebitCC
           tApiCInvoice.CInvoiceBalanceCreditTC = tApiCInvoice.CInvoiceBalanceCreditTC + tApiCInvoice.CInvoiceOriginalCreditTC
           tApiCInvoice.CInvoiceBalanceCreditLC = tApiCInvoice.CInvoiceBalanceCreditLC + tApiCInvoice.CInvoiceOriginalCreditLC
           tApiCInvoice.CInvoiceBalanceCreditCC = tApiCInvoice.CInvoiceBalanceCreditCC + tApiCInvoice.CInvoiceOriginalCreditCC.

    assign tApiCInvoice.CInvoiceBalanceTC = tApiCInvoice.CInvoiceBalanceDebitTC - tApiCInvoice.CInvoiceBalanceCreditTC
           tApiCInvoice.CInvoiceBalanceLC = tApiCInvoice.CInvoiceBalanceDebitLC - tApiCInvoice.CInvoiceBalanceCreditLC
           tApiCInvoice.CInvoiceBalanceCC = tApiCInvoice.CInvoiceBalanceDebitCC - tApiCInvoice.CInvoiceBalanceCreditCC
           tApiCInvoice.CInvoiceIsOpen    = (tApiCInvoice.CInvoiceBalanceTC <> 0 or tApiCInvoice.CInvoiceBalanceLC <> 0 or tApiCInvoice.CInvoiceBalanceCC <> 0)
           tApiCInvoice.tc_status         = (if tApiCInvoice.tc_Status = "":U then "C":U else tApiCInvoice.tc_Status).

                                                                                              
 END. /* CALCAMOUNTSBLOCK */

 assign oiReturnStatus = viLocalReturnStatus.