project QadFinancials > class BCInvoiceJournalEntry > method ApiStdMaintainTTValidateExchangeRate

Description

This method validates that the exchange rates between the invoice tables and posting tables are the same


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoiceJournalEntry.ApiStdMaintainTTValidationsAndDef


program code (program9/bcinvoicejournalentry.p)

/*  ExchangeRate Validation                                                 */
/*     - TC currency code should be the same in invoice and posting tables  */  
/*     - Calculate invoice exchange rate   (TC and CC if applicable         */
/*         - if rate set in tPostingLine different from invoice - error     */
/*         - if no rate in tPostingLine - copy across                       */
/* ======================================================================== */
assign oiReturnStatus = -98
       viLocalReturnStatus = 0.
/*
Assumption:  All the posting line records apply to the single
invoice, so we can just iterate through all the postinglines.
*/

/* posting exchange rate cannot be set if invoice exchange rate is not set */
assign vlthrowError = false.

CALCAMOUNTSBLOCK:
DO:
/* Default the Scale values */    
if t_sApiCInvoiceCIJE.CInvoiceRateScale = 0 or  t_sApiCInvoiceCIJE.CInvoiceRateScale = ?
    then assign  t_sApiCInvoiceCIJE.CInvoiceRateScale = 1.
if t_sApiCInvoiceCIJE.CInvoiceCCScale = 0 or  t_sApiCInvoiceCIJE.CInvoiceCCScale = ?
    then assign  t_sApiCInvoiceCIJE.CInvoiceCCScale = 1.

/* Calculation for LC exchange rate */
if t_sApiCInvoiceCIJE.CInvoiceCurrency_ID = viCompanyLCId or
   t_sApiCInvoiceCIJE.tcCurrencyCode = vcCompanyLC  
then assign t_sApiCInvoiceCIJE.CInvoiceExchangeRate     = 1
            t_sApiCInvoiceCIJE.CinvoiceRateScale        = 1.
else 
if (t_sApiCInvoiceCIJE.CInvoiceOriginalDebitLC = 0 or t_sApiCInvoiceCIJE.CInvoiceOriginalDebitLC = ?) and
   (t_sApiCInvoiceCIJE.CInvoiceOriginalCreditLC = 0 or t_sApiCInvoiceCIJE.CInvoiceOriginalCreditLC = ?)
then do:
    /* check to see if the rate is set */
    if (t_sApiCInvoiceCIJE.CInvoiceExchangeRate = 0 or t_sApiCInvoiceCIJE.CInvoiceExchangeRate = ?)
    then do:            
         <M-78 run GetExRateByEntityInvoiceFlag
            (input  t_sApiCInvoiceCIJE.Company_ID (iiCompanyID), 
             input  t_sApiCInvoiceCIJE.CInvoiceDate (itCInvoiceDate), 
             input  t_sApiCInvoiceCIJE.CInvoicePostingDate (itCInvoicePostingDate), 
             input  t_sApiCInvoiceCIJE.CInvoiceCurrency_ID (iiFromCurrencyId), 
             input  if t_sApiCInvoiceCIJE.CInvoiceCurrency_ID = 0 or t_sApiCInvoiceCIJE.CInvoiceCurrency_ID = ? then t_sApiCInvoiceCIJE.tcCurrencyCode else '':U (icFromCurrencyCode), 
             input  viCompanyLCid (iiToCurrencyId), 
             input  ? (icToCurrencyCode), 
             input  0 (iiExchangeRateTypeId), 
             input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
             output t_sApiCInvoiceCIJE.CInvoiceExchangeRate (odCInvoiceExchangeRate), 
             output t_sApiCInvoiceCIJE.CInvoiceRateScale (odCInvoiceRateScale), 
             output viFcReturnSuper (oiReturnStatus)) in BCInvoiceJournalEntry>
                
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.    
        if viFcReturnSuper < 0
        then leave CALCAMOUNTSBLOCK.
    end. /* if (tApiCInvoice.CInvoiceExchangeRate = 0 */
end.
else do:
    if ((t_sApiCInvoiceCIJE.CInvoiceOriginalDebitLC  <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalDebitLC  <> ?) or
        (t_sApiCInvoiceCIJE.CInvoiceOriginalCreditLC <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalCreditLC <> ?)) and
       ((t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC  <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC  <> ?) or 
        (t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC <> ?))
    then do:
        /*   If LC and exchange rate is set - use exchange rate from xml otherwise calculate it  */
        if (t_sApiCInvoiceCIJE.CInvoiceExchangeRate = 0 or t_sApiCInvoiceCIJE.CInvoiceExchangeRate = ?)
        then do:
           assign t_sApiCInvoiceCIJE.CInvoiceExchangeRate =                 
            ((if t_sApiCInvoiceCIJE.CInvoiceOriginalDebitLC  = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalDebitLC) - 
             (if t_sApiCInvoiceCIJE.CInvoiceOriginalCreditLC = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalCreditLC)) 
             /
            ((if t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC  = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC) - 
             (if t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC)) no-error.
        end.
    end.
end.
/* Same calculation for CC */
if viCompanyCCId <> 0 and viCompanyCCId <> ?
then do:
    if t_sApiCInvoiceCIJE.tcCurrencyCode = vcCompanyCC
    then assign t_sApiCInvoiceCIJE.CInvoiceCCRate    = 1
                t_sApiCInvoiceCIJE.CInvoiceCCScale   = 1.
    else if vlDomainIsStatutory <> false
    then do: 
        if (t_sApiCInvoiceCIJE.CInvoiceOriginalDebitCC = 0 or t_sApiCInvoiceCIJE.CInvoiceOriginalDebitCC = ?) and
           (t_sApiCInvoiceCIJE.CInvoiceOriginalCreditCC = 0 or t_sApiCInvoiceCIJE.CInvoiceOriginalCreditCC = ?)
        then do:
        /*  If CC exchange rate is set - use exchange rate from xml otherwise calculate it  */            
            if (t_sApiCInvoiceCIJE.CInvoiceCCRate = 0 or t_sApiCInvoiceCIJE.CInvoiceCCRate = ?)
            then do:
                <M-41 run GetExRateByEntityInvoiceFlag
                   (input  t_sApiCInvoiceCIJE.Company_ID (iiCompanyID), 
                    input  t_sApiCInvoiceCIJE.CInvoiceDate (itCInvoiceDate), 
                    input  t_sApiCInvoiceCIJE.CInvoicePostingDate (itCInvoicePostingDate), 
                    input  0 (iiFromCurrencyId), 
                    input  t_sApiCInvoiceCIJE.tcCurrencyCode (icFromCurrencyCode), 
                    input  viCompanyCCId (iiToCurrencyId), 
                    input  vcCompanyCC (icToCurrencyCode), 
                    input  0 (iiExchangeRateTypeId), 
                    input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                    output t_sApiCInvoiceCIJE.CInvoiceCCRate (odCInvoiceExchangeRate), 
                    output t_sApiCInvoiceCIJE.CInvoiceCCScale (odCInvoiceRateScale), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoiceJournalEntry>
             
                if viFcReturnSuper <> 0 
                then assign viLocalReturnStatus = viFcReturnSuper.    
                if viFcReturnSuper < 0
                then leave CALCAMOUNTSBLOCK.
            end.
        end.                                                               
        else do:
            if ((t_sApiCInvoiceCIJE.CInvoiceOriginalDebitCC  <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalDebitCC  <> ?) or
                (t_sApiCInvoiceCIJE.CInvoiceOriginalCreditCC <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalCreditCC <> ?)) and
               ((t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC  <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC  <> ?) or 
                (t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC <> 0 and t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC <> ?))
            then do:
                /*  
                   If CC and exchange rate is set - use exchange rate from xml otherwise calculate it
                */
                assign t_sApiCInvoiceCIJE.CInvoiceCCScale    = 1.
                if (t_sApiCInvoiceCIJE.CInvoiceCCRate = 0 or t_sApiCInvoiceCIJE.CInvoiceCCRate = ?)
                then do:
                   assign t_sApiCInvoiceCIJE.CInvoiceCCRate =                 
                    ((if t_sApiCInvoiceCIJE.CInvoiceOriginalDebitCC  = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalDebitCC) - 
                     (if t_sApiCInvoiceCIJE.CInvoiceOriginalCreditCC = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalCreditCC)) 
                     /
                    ((if t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC  = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalDebitTC) - 
                     (if t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC = ? then 0 else t_sApiCInvoiceCIJE.CInvoiceOriginalCreditTC)) no-error.
                 end.
            end.       
       end. /* else do: */  
       
       /* Check if the invoice is linked to an invoice and then check the statutory currency rate used on the linked invoice */ 
       if not available t_sApiCInvoiceCIJE
       then do:       
           find first t_sApiCInvoiceCIJE where
                      t_sApiCInvoiceCIJE.CInvoicePostingYear      = t_sApiPostingCIJE.PostingYear    and
                      t_sApiCInvoiceCIJE.CInvoiceVoucher          = t_sApiPostingCIJE.PostingVoucher and
                      t_sApiCInvoiceCIJE.tcJournalCode            = t_sApiPostingCIJE.tcJournalCode  
           exclusive-lock no-error.
       end.
       if available t_sApiCInvoiceCIJE                                   and
                    t_sApiCInvoiceCIJE.tiLinkedCInvoiceVoucher <> 0      and
                    t_sApiCInvoiceCIJE.tiLinkedCInvoiceVoucher <> ?      and
                    t_sApiCInvoiceCIJE.tiLinkedPeriodYear      <> 0      and
                    t_sApiCInvoiceCIJE.tiLinkedPeriodYear      <> ?      and
                    t_sApiCInvoiceCIJE.tcLinkedJournalCode     <> '':U   and
                    t_sApiCInvoiceCIJE.tcLinkedJournalCode     <> ?       
       then do:
           <Q-23 run CInvoiceByInvoiceNumber (all) (Read) (NoCache)
              (input viCompanyId, (CompanyId)
               input t_sApiCInvoiceCIJE.tiLinkedPeriodYear, (CInvoicePostingYear)
               input t_sApiCInvoiceCIJE.tiLinkedCInvoiceVoucher, (CInvoiceVoucher)
               input t_sApiCInvoiceCIJE.tcLinkedJournalCode, (JournalCode)
               output dataset tqCInvoiceByInvoiceNumber) in BCInvoice >
                 
           find first tqCInvoiceByInvoiceNumber no-lock no-error.
           if available tqCInvoiceByInvoiceNumber
           then do:
                if tqCInvoiceByInvoiceNumber.tdCInvoiceCCRate    <> t_sApiCInvoiceCIJE.CInvoiceCCRate      or
                   tqCInvoiceByInvoiceNumber.tdCInvoiceCCScale   <> t_sApiCInvoiceCIJE.CInvoiceCCScale
                then assign t_sApiCInvoiceCIJE.CInvoiceCCRate  = tqCInvoiceByInvoiceNumber.tdCInvoiceCCRate
                            t_sApiCInvoiceCIJE.CInvoiceCCScale = tqCInvoiceByInvoiceNumber.tdCInvoiceRateScale. 
           end. /* if available tqCInvoiceByInvoiceNumber */
       end. /* if available t_sApiCInvoiceCIJE */
    end. /* else do: */
end. /* if viCompanyCCId <> 0 and viCompanyCCId <> ? */

/* TC should be the same in invoice and posting tables */
find first t_sApiPostingLineCIJE where t_sApiPostingLineCIJE.tcCurrencyCode <>
    t_sApiCInvoiceCIJE.tcCurrencyCode no-error.
if available t_sApiPostingLineCIJE
then do:
    assign vcMsgApi = trim(substitute(#T-20'Posting line transaction currency (&1) must be the same as invoice currency code (&2) (tcCurrencyCode) .':150(865480580)T-20#,
                          trim(string(t_sApiPostingLineCIJE.tcCurrencyCode)),
                          trim(string(t_sApiCInvoiceCIJE.tcCurrencyCode)))).
   <M-84 run SetMessage
      (input  vcMsgApi (icMessage), 
       input  '':U (icArguments), 
       input  '':U (icFieldName), 
       input  '':U (icFieldValue), 
       input  'E':U (icType), 
       input  1 (iiSeverity), 
       input  t_sApiPostingLineCIJE.tc_Rowid (icRowid), 
       input  'QadFin-563209':U (icFcMsgNumber), 
       input  '' (icFcExplanation), 
       input  '' (icFcIdentification), 
       input  '' (icFcContext), 
       output viFcReturnSuper (oiReturnStatus)) in BCInvoiceJournalEntry>
    assign viLocalReturnStatus = -1.               
    leave CALCAMOUNTSBLOCK.
end. /* if if available t_sApiPostingLineCIJE */                      


/* set the rates for the posting lines if not set */
for each t_sApiPostingLineCIJE:
/* if Scale/Rates are not set or if they are not the same */
    if ((t_sApiCInvoiceCIJE.CInvoiceExchangeRate = 0 or t_sApiCInvoiceCIJE.CInvoiceExchangeRate = ?) and
         (t_sApiPostingLineCIJE.PostingLineExchangeRate <> 0 and  t_sApiPostingLineCIJE.PostingLineExchangeRate <> ?)) or
         (t_sApiCInvoiceCIJE.CInvoiceExchangeRate <> t_sApiPostingLineCIJE.PostingLineExchangeRate and
             t_sApiPostingLineCIJE.PostingLineExchangeRate <> 0 and t_sApiPostingLineCIJE.PostingLineExchangeRate <> ?)
         then assign vlthrowError = true.       
    else if ((t_sApiCInvoiceCIJE.CInvoiceRateScale = 0 or t_sApiCInvoiceCIJE.CInvoiceRateScale = ?) and
             (t_sApiPostingLineCIJE.PostingLineRateScale <> 0 and  t_sApiPostingLineCIJE.PostingLineRateScale <> ?)) or
            (t_sApiCInvoiceCIJE.CInvoiceRateScale <>  t_sApiPostingLineCIJE.PostingLineRateScale and
             t_sApiPostingLineCIJE.PostingLineRateScale <> 0 and t_sApiPostingLineCIJE.PostingLineRateScale <> ?)
         then assign vlthrowError = true.             
    else if vlDomainIsStatutory = true    
    then do:    
        if ((t_sApiCInvoiceCIJE.CInvoiceCCRate = 0 or t_sApiCInvoiceCIJE.CInvoiceCCRate = ?) and
            (t_sApiPostingLineCIJE.PostingLineCCRate <> 0 and  t_sApiPostingLineCIJE.PostingLineCCRate <> ?)) or
            (t_sApiCInvoiceCIJE.CInvoiceCCRate <> t_sApiPostingLineCIJE.PostingLineCCRate and
            t_sApiPostingLineCIJE.PostingLineCCRate <> 0 and t_sApiPostingLineCIJE.PostingLineCCRate <> ?)
        then do:            
            /* The exchange rates for statutory currency can be different as the Tax amounts can have
             different exchange rates */
             <Q-74 run GLForGLTypeInfo (all) (Read) (NoCache)
                (input viCompanyId, (CompanyId)
                 input t_sApiPostingLineCIJE.tcGLCode, (GLCode)
                 output dataset tqGLForGLTypeInfo) in BGL>
                 
              find first tqGLForGLTypeInfo where tqGLForGLTypeInfo.tcGLCode = t_sApiPostingLineCIJE.tcGLCode.
              if available tqGLForGLTypeInfo 
              then do:                
                    if tqGLForGLTypeInfo.tcGLCode = t_sApiPostingLineCIJE.tcGLCode
                            and tqGLForGLTypeInfo.tcGLTypeCode <> {&GLTYPECODE-VAT} 
                    then assign vlthrowError = true.       
                    else next.
              end.              
         end.
         else if ((t_sApiCInvoiceCIJE.CInvoiceCCScale = 0 or t_sApiCInvoiceCIJE.CInvoiceCCScale = ?) and
             (t_sApiPostingLineCIJE.PostingLineCCScale <> 0 and  t_sApiPostingLineCIJE.PostingLineCCScale <> ?)) or
             (t_sApiCInvoiceCIJE.CInvoiceCCScale <>  t_sApiPostingLineCIJE.PostingLineCCScale and
              t_sApiPostingLineCIJE.PostingLineCCScale <> 0 and t_sApiPostingLineCIJE.PostingLineCCScale <> ?)
         then assign vlthrowError = true.                                         
    end. /*if vlDomainIsStatutory */
       
    if (vlThrowError = true) then do:
        assign vcMsgApi = trim(substitute(#T-14'Posting line exchange rate/scale (&1/&2 (LC) &3/&4 (CC)) must be the same as exchange rate/scales on the invoice (&5/&6 (LC) &7/&8 (CC)) .':150(126252338)T-14#,
                              trim(string(t_sApiPostingLineCIJE.PostingLineExchangeRate)),
                              trim(string(t_sApiPostingLineCIJE.PostingLineRateScale)),
                              trim(string(t_sApiPostingLineCIJE.PostingLineCCRate)),
                              trim(string(t_sApiPostingLineCIJE.PostingLineCCScale)),
                              trim(string(t_sApiCInvoiceCIJE.CInvoiceExchangeRate)),
                              trim(string(t_sApiCInvoiceCIJE.CInvoiceRateScale)),
                              trim(string(t_sApiCInvoiceCIJE.CInvoiceCCRate)),
                              trim(string(t_sApiCInvoiceCIJE.CInvoiceCCScale)))).                                              
          
       <M-89 run SetMessage
          (input  vcMsgApi (icMessage), 
           input  '':U (icArguments), 
           input  '':U (icFieldName), 
           input  '':U (icFieldValue), 
           input  'E':U (icType), 
           input  1 (iiSeverity), 
           input  t_sApiPostingLineCIJE.tc_Rowid (icRowid), 
           input  'QadFin-805543':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in BCInvoiceJournalEntry>
           
           assign viLocalReturnStatus = -1.               
           leave CALCAMOUNTSBLOCK.

    end. /* if (vlThrowError) */                      

    /* assign the rate/scales to the line */
    assign
           t_sApiPostingLineCIJE.PostingLineExchangeRate =  t_sApiCInvoiceCIJE.CInvoiceExchangeRate
           t_sApiPostingLineCIJE.PostingLineRateScale    =  t_sApiCInvoiceCIJE.CInvoiceRateScale
           t_sApiPostingLineCIJE.PostingLineCCRate       =  t_sApiCInvoiceCIJE.CInvoiceCCRate
           t_sApiPostingLineCIJE.PostingLineCCScale      =  t_sApiCInvoiceCIJE.CInvoiceCCScale.        
end. /* for each */
end. /* CALCAMOUNTSBLOCK */

assign oiReturnStatus = viLocalReturnStatus.