project QadFinancials > class BPosting > method AddPostingLineExchRateGainLoss

Description

This method does creation of the exchange gain/lose posting both for the LC and/or CC. Method can generate both realized and unrealized gain/lose transacctions.


Parameters


icPostingtcRowIdinputcharacterRow Id of the posting header
icExchangeRateGainLoseTypeinputcharacterThis parameter defines, whether system will create posting to the realized exchange rate gain/lose account ({&EXCHANGERATETYPE_REALIZED}) or to unrealized exchange rate gain/lose account ({&EXCHANGERATETYPE_UNREALIZED})
idDeltaDebitLCinputdecimal
idDeltaCreditLCinputdecimal
idDeltaDebitCCinputdecimal
idDeltaCreditCCinputdecimal
idLCExchangeRateinputdecimal
idLCExchangeRateScaleinputdecimal
idCCExchangeRateinputdecimal
idCCExchangeRateScaleinputdecimal
icPostingLineTextPreinputcharacter
itExchangeRateDateinputdateExchange rate date
icCurrencyCodeinputcharacterCode of the transactional currency, from which the calculation of exchange rate gain/loss was done.
oiLCPostingLine_IDoutputintegerID of the PostingLine created for LC exchange rate gain or lose posting
oiCCPostingLine_IDoutputintegerID of the PostingLine for the CC exchange rate gain/lose posting
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AdditionalUpdatesInvCIMovements
method BCInvoice.UpdateCInvoiceVatDelay
method BPosting.AddPostingLineTaxForSuspDel


program code (program6/bposting.p)

MAIN_BLOCK:
do on error undo, throw:

    /* =========================================================================================== *
     * Validate input parameters                                                                   *
     * =========================================================================================== */
    if icPostingtcRowId           = ? or icPostingtcRowId           = "":U or
       icExchangeRateGainLoseType = ? or icExchangeRateGainLoseType = "":U or
       (icExchangeRateGainLoseType <> {&EXCHANGERATETYPE_REALIZED} and icExchangeRateGainLoseType <> {&EXCHANGERATETYPE_UNREALIZED}) or
       icCurrencyCode             = ? or icCurrencyCode             = "":U
    then do:
        assign vcMessage =  #T-72'Not all mandatory values are populated to this method.':255(65737)T-72#
               vcContext = "icPostingtcRowId=&1|icExchangeRateGainLoseType=&2|icCurrencyCode=&3":U
               vcContext = replace(vcContext, "|":U, chr(2))
               vcContext = substitute(vcContext, icPostingtcRowId, icExchangeRateGainLoseType, icCurrencyCode).
        <M-82 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-872700':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        assign oiReturnStatus = -1.
        leave MAIN_BLOCK.
    end.
       
    /* =========================================================================================== *
     * When there is not any exchange rate gain/lose amount, then do nothing                       *
     * =========================================================================================== */
    if idDeltaCreditCC = 0 and
       idDeltaCreditLC = 0 and
       idDeltaDebitCC  = 0 and
       idDeltaDebitLC  = 0
    then leave MAIN_BLOCK.
    
    /* =========================================================================================== *
     * Find the posting for which this Exchange rate profit/lose are created                       *
     * =========================================================================================== */
    find tPosting where
         tPosting.tc_Rowid = icPostingtcRowId
         no-error.
    if not available tPosting
    then do:
        assign vcMessage =  #T-25'Cannot find posting with tcRowId = &1':255(586780156)T-25#
               vcMessage = substitute(vcMessage, icPostingtcRowId).
        <M-84 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-47900':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        assign oiReturnStatus = -1.
        leave MAIN_BLOCK.
    end.     
        
    /* =========================================================================================== *
     * Normalize input parameters                                                                  *
     * =========================================================================================== */
    if itExchangeRateDate = ? then assign itExchangeRateDate = tPosting.PostingDate. 
     
    
    /* =========================================================================================== *
     * Get GL accounts for the gain/lose                                                           *
     * =========================================================================================== */
    assign vcGainGLCode = "":U
           vcLossGLCode = "":U.
           
     
    assign vcGLSystemType = if icExchangeRateGainLoseType = {&EXCHANGERATETYPE_REALIZED}
                            then {&GLSYSTEMTYPE-EXCHANGEREALPROFIT} 
                            else {&GLSYSTEMTYPE-EXCHANGEUNREALPROFIT}.
                            
    <Q-89 run GLByGLTypeGLSystemType (all) (Read) (NoCache)
       (input tPosting.Company_ID, (CompanyId)
        input {&GLTYPECODE-SYST}, (GLType)
        input vcGLSystemType, (GLSystemType)
        output dataset tqGLByGLTypeGLSystemType) in BGL>  
    for first tqGLByGLTypeGLSystemType:
        assign vcGainGLCode = tqGLByGLTypeGLSystemType.tcGLCode.
    end.
    
    
    assign vcGLSystemType = if icExchangeRateGainLoseType = {&EXCHANGERATETYPE_REALIZED}
                            then {&GLSYSTEMTYPE-EXCHANGEREALLOSS} 
                            else {&GLSYSTEMTYPE-EXCHANGEUNREALLOSS}.
    /* Get GL Loss System Account */        
    <Q-90 run GLByGLTypeGLSystemType (all) (Read) (NoCache)
       (input tPosting.Company_ID, (CompanyId)
        input {&GLTYPECODE-SYST}, (GLType)
        input vcGLSystemType, (GLSystemType)
        output dataset tqGLByGLTypeGLSystemType) in BGL>  
    for first tqGLByGLTypeGLSystemType:
        assign vcLossGLCode = tqGLByGLTypeGLSystemType.tcGLCode.
    end.

    if vcGainGLCode = "":U or vcGainGLCode = ? or
       vcLossGLCode = "":U or vcLossGLCode = ?
    then do:
        assign vcMessage =  if icExchangeRateGainLoseType = {&EXCHANGERATETYPE_REALIZED}
                            then #T-4'The GL account for realized exchange rate gain or for realized exchange rate loss are not defined in the system.':255(238488635)T-4#
                            else #T-78'The GL account for un-realized exchange rate gain or for un-realized exchange rate loss are not defined in the system.':255(983466801)T-78#
               vcContext = "Exchange rate gain GL account=&1|Exchange rate loss GL account=&2":U
               vcContext = replace(vcContext, "|":U, chr(2))
               vcContext = substitute(vcContext, vcGainGLCode, vcLossGLCode).
        <M-92 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-784247':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        assign oiReturnStatus = -1.
        leave MAIN_BLOCK.    
    end.


    /* =============================================================================================== */
    /* All values for gain/lose have to be positive                                                    */
    /* =============================================================================================== */
    if idDeltaDebitLC < 0 
    then assign idDeltaCreditLC = - idDeltaDebitLC
                idDeltaDebitLC  = 0.
    if idDeltaCreditLC < 0 
    then assign idDeltaDebitLC  = - idDeltaCreditLC
                idDeltaCreditLC = 0.
    if idDeltaDebitCC < 0 
    then assign idDeltaCreditCC = - idDeltaDebitCC
                idDeltaDebitCC  = 0.
    if idDeltaCreditCC < 0 
    then assign idDeltaDebitCC  = - idDeltaCreditCC
                idDeltaCreditCC = 0.
       
            
    
    /* =============================================================================================== */
    /* Create postings to correct accounts                                                             */
    /* =============================================================================================== */    

    /* if both lc and cc have a balance on the same side then one posting line can be created */
    if (idDeltaDebitLC  <> 0 and idDeltaDebitCC  <> 0) or
       (idDeltaCreditLC <> 0 and idDeltaCreditCC <> 0)
    then do:
        if idDeltaDebitLC <> 0 and idDeltaDebitCC <> 0
        then assign vcGLCode          = vcLossGLCode
                    vcPostingLineText = icPostingLineTextPre + ' Loss':U.
        else assign vcGLCode          = vcGainGLCode
                    vcPostingLineText = icPostingLineTextPre + ' Gain':U.

        
        <M-87 run AddStandardPosting
           (input  tPosting.tc_Rowid (icPostingtcRowid), 
            input  vcGLCode (icGLCode), 
            input  '':U (icDivisionCode), 
            input  '':U (icCostCentreCode), 
            input  '':U (icCostCentreText), 
            input  '':U (icProjectCode), 
            input  '':U (icProjectText), 
            input  '':U (icIntercoBusinessRelationCode), 
            input  icCurrencyCode (icCurrencyCode), 
            input  0 (idDebitTC), 
            input  idDeltaDebitLC (idDebitLC), 
            input  idDeltaDebitCC (idDebitCC), 
            input  0 (idDebitPC), 
            input  0 (idCreditTC), 
            input  idDeltaCreditLC (idCreditLC), 
            input  idDeltaCreditCC (idCreditCC), 
            input  0 (idCreditPC), 
            input  0 (idQty), 
            input  vcPostingLineText (icLineText), 
            input  '':U (icSafText), 
            input  tDefaultSafs (tDefaultSafs), 
            input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
            input  idLCExchangeRate (idExchangeRate), 
            input  idLCExchangeRateScale (idExchangeRateScale), 
            input  idCCExchangeRate (idPostingLineCCRate), 
            input  idCCExchangeRateScale (idPostingLineCCScale), 
            output viPostingLineId (oiPostingLineId), 
            input  0 (iiSafStructureId), 
            input  '':U (icSafStructureCode), 
            input  '':U (icAllocationKey), 
            input  false (ilLinkedCrCyDaemonReqExists), 
            input  itExchangeRateDate (itExchangeRateDate), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave MAIN_BLOCK.
        
        assign oiLCPostingLine_ID = viPostingLineId
               oiCCPostingLine_ID = viPostingLineId.
               
    end. /* if both lc and cc have a balance on the same side then one posting line can be created */
    
    else do:
        /* There is gain/lose only on LC part of LC is gain and CC is lose or vice versa */ 
        if idDeltaDebitLC  <> 0 or 
           idDeltaCreditLC <> 0
        then do:
            
            if idDeltaDebitLC <> 0
            then assign vcGLCode          = vcLossGLCode
                        vcPostingLineText = icPostingLineTextPre + ' Loss':U.
            else assign vcGLCode          = vcGainGLCode
                        vcPostingLineText = icPostingLineTextPre + ' Gain':U.
            
            <M-71 run AddStandardPosting
               (input  tPosting.tc_Rowid (icPostingtcRowid), 
                input  vcGLCode (icGLCode), 
                input  '':U (icDivisionCode), 
                input  '':U (icCostCentreCode), 
                input  '':U (icCostCentreText), 
                input  '':U (icProjectCode), 
                input  '':U (icProjectText), 
                input  '':U (icIntercoBusinessRelationCode), 
                input  icCurrencyCode (icCurrencyCode), 
                input  0 (idDebitTC), 
                input  idDeltaDebitLC (idDebitLC), 
                input  0 (idDebitCC), 
                input  0 (idDebitPC), 
                input  0 (idCreditTC), 
                input  idDeltaCreditLC (idCreditLC), 
                input  0 (idCreditCC), 
                input  0 (idCreditPC), 
                input  0 (idQty), 
                input  vcPostingLineText (icLineText), 
                input  '':U (icSafText), 
                input  tDefaultSafs (tDefaultSafs), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                input  idLCExchangeRate (idExchangeRate), 
                input  idLCExchangeRateScale (idExchangeRateScale), 
                input  0 (idPostingLineCCRate), 
                input  0 (idPostingLineCCScale), 
                output oiLCPostingLine_ID (oiPostingLineId), 
                input  0 (iiSafStructureId), 
                input  '':U (icSafStructureCode), 
                input  '':U (icAllocationKey), 
                input  false (ilLinkedCrCyDaemonReqExists), 
                input  itExchangeRateDate (itExchangeRateDate), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave MAIN_BLOCK.
        end. /* else if idDeltaDebitLC <> 0 or  */
        
        
        /* There is gain/lose only on CC part of LC is gain and CC is lose or vice versa */
        if idDeltaDebitCC  <> 0 or 
           idDeltaCreditCC <> 0
        then do:
            
            if idDeltaDebitCC <> 0
            then assign vcGLCode         = vcLossGLCode
                        vcPostingLineText = icPostingLineTextPre + ' Loss':U.
            else assign vcGLCode          = vcGainGLCode
                        vcPostingLineText = icPostingLineTextPre + ' Gain':U.
            
            <M-18 run AddStandardPosting
               (input  tPosting.tc_Rowid (icPostingtcRowid), 
                input  vcGLCode (icGLCode), 
                input  '':U (icDivisionCode), 
                input  '':U (icCostCentreCode), 
                input  '':U (icCostCentreText), 
                input  '':U (icProjectCode), 
                input  '':U (icProjectText), 
                input  '':U (icIntercoBusinessRelationCode), 
                input  icCurrencyCode (icCurrencyCode), 
                input  0 (idDebitTC), 
                input  0 (idDebitLC), 
                input  idDeltaDebitCC (idDebitCC), 
                input  0 (idDebitPC), 
                input  0 (idCreditTC), 
                input  0 (idCreditLC), 
                input  idDeltaCreditCC (idCreditCC), 
                input  0 (idCreditPC), 
                input  0 (idQty), 
                input  vcPostingLineText (icLineText), 
                input  '':U (icSafText), 
                input  tDefaultSafs (tDefaultSafs), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                input  0 (idExchangeRate), 
                input  0 (idExchangeRateScale), 
                input  idCCExchangeRate (idPostingLineCCRate), 
                input  idCCExchangeRateScale (idPostingLineCCScale), 
                output oiCCPostingLine_ID (oiPostingLineId), 
                input  0 (iiSafStructureId), 
                input  '':U (icSafStructureCode), 
                input  '':U (icAllocationKey), 
                input  false (ilLinkedCrCyDaemonReqExists), 
                input  itExchangeRateDate (itExchangeRateDate), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave MAIN_BLOCK.
        end. /* if idDeltaDebitCC  <> 0 or  */
    end. /* else do: if (idDeltaDebitLC  <> 0 and idDeltaDebitCC  <> 0) or */
end. /* MAIN_BLOCK */