project QadFinancials > class BPosting > method CreateRoundingPosting
Description
This method can be used for automatic creation of rounding posting, which will balance a posting.
Parameters
icPostingRowID | input | character | |
oiPostingLineID | output | integer | |
oiReturnStatus | output | integer | Return status of the method. |
Internal usage
QadFinancials
program code (program6/bposting.p)
/* =================================================================================================== */
/* Method : CreateRoundingPosting */
/* Desc : This method can be used for automatic creation of rounding posting, which will */
/* balance a posting. */
/* --------------------------------------------------------------------------------------------------- */
/* Params: (I) PostingRowId RowId of the posting to be balanced by rounding */
/* =================================================================================================== */
assign oiReturnStatus = -98
viLocalReturn = 0.
/* =================================================================================================== */
/* Normalize input parameters */
/* =================================================================================================== */
if icPostingRowId = "":U then assign icPostingRowId = ?.
MAIN_BLOCK:
do on error undo, return:
/* =================================================================================================== */
/* Validate input parameters */
/* =================================================================================================== */
if icPostingRowId = ?
then do:
assign vcMessage = #T-53'Posting Row Id parameter is mandatory.':255(253407076)T-53#
vcContext = 'icPostingRowId=&1':U
vcContext = substitute(vcContext, icPostingRowId).
<M-59 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-756898':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input vcContext (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
assign viLocalReturn = -1.
leave MAIN_BLOCK.
end.
/* Get posting */
find tPosting where
tPosting.tc_Rowid = icPostingRowId
no-error.
if not available tPosting
then do:
assign vcMessage = #T-30'Cannot find posting with tcRowId = &1':255(586780156)T-30#
vcMessage = substitute(vcMessage, icPostingRowId)
vcContext = 'icPostingRowId=&1':U
vcContext = substitute(vcContext, icPostingRowId).
<M-90 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-785024':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input vcContext (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
assign viLocalReturn = -1.
leave MAIN_BLOCK.
end.
/* =================================================================================================== */
/* Calculate balance of the posting */
/* =================================================================================================== */
assign vdBalanceLC = 0
vdBalanceCC = 0
vdBalanceTC = 0
viNumbrOfCurrenciesInPost = 0
viNumbrOfPostingLines = 0.
for each tPostingLine where
tPostingLine.tc_ParentRowid = tPosting.tc_Rowid and
tPostingLine.tc_status <> "D":U
break by tPostingLine.tcCurrencyCode:
/* Reset some currency-related fields */
if first-of(tPostingLine.tcCurrencyCode)
then assign viNumbrOfCurrenciesInPost = viNumbrOfCurrenciesInPost + 1.
/* Calculate balance LC and CC */
assign vdBalanceLC = vdBalanceLC + tPostingLine.PostingLineDebitLC - tPostingLine.PostingLineCreditLC
vdBalanceCC = vdBalanceCC + tPostingLine.PostingLineDebitCC - tPostingLine.PostingLineCreditCC
vdBalanceTC = vdBalanceTC + tPostingLine.PostingLineDebitTC - tPostingLine.PostingLineCreditTC
viNumbrOfPostingLines = viNumbrOfPostingLines + 1.
end.
/* =================================================================================================== */
/* If the posting contains only one transactional currency, then we can generate the rounding posting */
/* =================================================================================================== */
if viNumbrOfCurrenciesInPost > 1 or
(vdBalanceLC = 0 and
vdBalanceCC = 0)
then leave MAIN_BLOCK.
/* =================================================================================================== */
/* Pickup rounding account */
/* =================================================================================================== */
<Q-20 run GLBySystemTypeAndType (all) (Read) (NoCache)
(input ?, (GLCode)
input {&GLSYSTEMTYPE-ROUND}, (GLSystemTypeCode)
input ?, (GLId)
input {&GLTYPECODE-SYST}, (GLTypeCode)
input viCompanyId, (CompanyId)
input ?, (GlIsActive)
output dataset tqGLBySystemTypeAndType) in BGL>
find tqGLBySystemTypeAndType where
tqGLBySystemTypeAndType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-ROUND} and
tqGLBySystemTypeAndType.tcGLTypeCode = {&GLTYPECODE-SYST}
no-error.
if not available tqGLBySystemTypeAndType
then do :
assign vcMessage = #T-83'The system account for rounding differences has not been defined.':255(733756123)T-83#.
<M-69 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-539885':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
assign viLocalReturn = -1.
leave MAIN_BLOCK.
end. /* if not available tqGLBySystemTypeAndType */
/* =================================================================================================== */
/* Create LC Rounding Posting */
/* =================================================================================================== */
if vdBalanceLC <> 0
then do:
/* The system contains definition of rounding method, which should be used for *
* rounding of the currency */
if not vlIsStartedQCurrencyByCurrRound
then do:
<Q-47 run CurrencyByCurrRoundingMethod (Start) in BCurrency >
assign vlIsStartedQCurrencyByCurrRound = true.
end.
<Q-89 run CurrencyByCurrRoundingMethod (all) (Read) (Cache)
(input viCompanyLCId, (CurrencyID)
input vcCompanyLC, (CurrencyCode)
input ?, (RoundingMethodCode)
output dataset tqCurrencyByCurrRoundingMethod) in BCurrency >
find tqCurrencyByCurrRoundingMethod where
tqCurrencyByCurrRoundingMethod.tiCurrency_ID = viCompanyLCId
no-error.
if not available tqCurrencyByCurrRoundingMethod
then do:
assign vcMessage = #T-3'Setup-error: unable to find the rounding-method for the base-currency (&1).':255(916117471)T-3#
vcMessage = substitute(vcMessage, vcCompanyLC).
<M-72 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-451130':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
assign viLocalReturn = -1.
leave MAIN_BLOCK.
end.
/* Check if the differnect is in the allowed range */
if absolute(vdBalanceLC) <= viNumbrOfPostingLines * tqCurrencyByCurrRoundingMethod.tdRoundingMethodUnit
then do:
/* Create LC rounding posting */
<M-95 run AddDetailLine
(input 'PostingLine':U (icTable),
input tPosting.tc_Rowid (icParentRowid),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
if viFcReturnSuper < 0 then leave MAIN_BLOCK.
assign tPostingLine.PostingLineDebitLC = if vdBalanceLC > 0 then 0 else - vdBalanceLC
tPostingLine.PostingLineCreditLC = if vdBalanceLC > 0 then vdBalanceLC else 0
tPostingLine.PostingLineExchangeRate = 1
tPostingLine.PostingLineRateScale = 1
tPostingLine.Period_ID = tPosting.Period_ID
tPostingLine.Company_ID = tPosting.Company_ID
tPostingLine.PostingDate = tPosting.PostingDate
tPostingLine.PostingYearPeriod = tPosting.PostingYearPeriod
tPostingLine.Currency_ID = viCompanyLCID
tPostingLine.tcCurrencyCode = vcCompanyLC
tPostingLine.GL_ID = tqGLBySystemTypeAndType.tiGL_ID
tPostingLine.tcGLCode = tqGLBySystemTypeAndType.tcGLCode
tPostingLine.tcGLDescription = tqGLBySystemTypeAndType.tcGLDescription
tPostingLine.tlGLIsAutomaticAccount = tqGLBySystemTypeAndType.tlGLIsAutomaticAccount
tPostingLine.tlGLIsDebitAccount = tqGLBySystemTypeAndType.tlGLIsDebitAccount
tPostingLine.tcGLTypeCode = tqGLBySystemTypeAndType.tcGLTypeCode
tPostingLine.tcUnitCode = tqGLBySystemTypeAndType.tcUnitCode
tPostingLine.CustomShort8 = 'False':U.
oiPostingLineID = tPostingLine.PostingLine_ID.
end.
end. /* if vdBalanceLC <> 0 */
/* =================================================================================================== */
/* Create CC Rounding Posting */
/* =================================================================================================== */
if vdBalanceCC <> 0
then do:
/* The system contains definition of rounding method, which should be used for *
* rounding of the currency */
if not vlIsStartedQCurrencyByCurrRound
then do:
<Q-52 run CurrencyByCurrRoundingMethod (Start) in BCurrency >
assign vlIsStartedQCurrencyByCurrRound = true.
end.
<Q-44 run CurrencyByCurrRoundingMethod (all) (Read) (Cache)
(input viCompanyCCId, (CurrencyID)
input vcCompanyCC, (CurrencyCode)
input ?, (RoundingMethodCode)
output dataset tqCurrencyByCurrRoundingMethod) in BCurrency >
find tqCurrencyByCurrRoundingMethod where
tqCurrencyByCurrRoundingMethod.tiCurrency_ID = viCompanyCCId
no-error.
if not available tqCurrencyByCurrRoundingMethod
then do:
assign vcMessage = #T-4'Setup-error: unable to find the rounding-method for the management currency (&1).':255(177623382)T-4#
vcMessage = substitute(vcMessage, vcCompanyCC).
<M-23 run SetMessage
(input vcMessage (icMessage),
input '':U (icArguments),
input '':U (icFieldName),
input '':U (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input '':U (icRowid),
input 'qadfin-331885':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
assign viLocalReturn = -1.
leave MAIN_BLOCK.
end.
/* Check if the differnect is in the allowed range */
if absolute(vdBalanceCC) <= viNumbrOfPostingLines * tqCurrencyByCurrRoundingMethod.tdRoundingMethodUnit
then do:
/* Create CC rounding posting */
<M-96 run AddDetailLine
(input 'PostingLine':U (icTable),
input tPosting.tc_Rowid (icParentRowid),
output viFcReturnSuper (oiReturnStatus)) in BPosting>
if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
if viFcReturnSuper < 0 then leave MAIN_BLOCK.
assign tPostingLine.PostingLineDebitCC = if vdBalanceCC > 0 then 0 else - vdBalanceCC
tPostingLine.PostingLineCreditCC = if vdBalanceCC > 0 then vdBalanceCC else 0
tPostingLine.PostingLineExchangeRate = 1
tPostingLine.PostingLineRateScale = 1
tPostingLine.Period_ID = tPosting.Period_ID
tPostingLine.Company_ID = tPosting.Company_ID
tPostingLine.PostingDate = tPosting.PostingDate
tPostingLine.PostingYearPeriod = tPosting.PostingYearPeriod
tPostingLine.Currency_ID = viCompanyCCID
tPostingLine.tcCurrencyCode = vcCompanyCC
tPostingLine.GL_ID = tqGLBySystemTypeAndType.tiGL_ID
tPostingLine.tcGLCode = tqGLBySystemTypeAndType.tcGLCode
tPostingLine.tcGLDescription = tqGLBySystemTypeAndType.tcGLDescription
tPostingLine.tlGLIsAutomaticAccount = tqGLBySystemTypeAndType.tlGLIsAutomaticAccount
tPostingLine.tlGLIsDebitAccount = tqGLBySystemTypeAndType.tlGLIsDebitAccount
tPostingLine.tcGLTypeCode = tqGLBySystemTypeAndType.tcGLTypeCode
tPostingLine.tcUnitCode = tqGLBySystemTypeAndType.tcUnitCode.
oiPostingLineID = tPostingLine.PostingLine_ID.
end.
end. /* if vdBalanceCC <> 0 */
end. /* MAIN_BLOCK */
/* =================================================================================================== */
/* Stop queries */
/* =================================================================================================== */
if vlIsStartedQCurrencyByCurrRound
then do:
<Q-65 run CurrencyByCurrRoundingMethod (Stop) in BCurrency >
end.
/* =================================================================================================== */
/* Return */
/* =================================================================================================== */
assign oiReturnStatus = viLocalReturn.