Description
Submethod of OIAdjCommitSubCInvoice that will handle the actions for the discount
Parameters
oiReturnStatus | output | integer | Return status of the method. |
Internal usage
QadFinancials
program code (program8/bopenitemadjustment.p)
/* ================================== */
/* Prerequisite: tOIAdj is available */
/* Exception handling */
/* ================================== */
assign oiReturnStatus = -98
viLocalReturnStatus = 0.
/* ==================================================== */
/* Return in case no actions needed - clear temp-tables */
/* ==================================================== */
if tOIAdj.tdAdjustmentDiscountAmountTC = 0 or
tOIAdj.tdAdjustmentDiscountAmountTC = ?
then do :
assign oiReturnStatus = 0.
Return.
end. /* if tOIAdj.tdAdjustmentDiscountAmountTC = 0 */
/* ==================================== */
/* Open BJournalEntry and start a block */
/* ==================================== */
<I-7 {bFcOpenInstance
&CLASS = "BJournalEntry"}>
CIINVOICEDISCOUNTBLOCK : DO :
/* ===================================================================================================== */
/* Get the account to be used for the discount that does not go into taxes */
/* We will look for the banking of the invoice and then take the discount-account from that bank-account */
/* ===================================================================================================== */
<Q-1 run CInvoiceBankForDiscountGL (all) (Read) (NoCache)
(input tOIAdj.tiInvoiceID, (CInvoiceID)
input tOIAdj.tiCompanyId, (CompanyId)
output dataset tqCInvoiceBankForDiscountGL) in BCInvoice >
Find first tqCInvoiceBankForDiscountGL where
tqCInvoiceBankForDiscountGL.tiCInvoice_ID = tOIAdj.tiInvoiceID and
tqCInvoiceBankForDiscountGL.tcBankAPDiscountGLCode <> "":U and
tqCInvoiceBankForDiscountGL.tcBankAPDiscountGLCode <> ?
no-lock no-error.
if not available tqCInvoiceBankForDiscountGL
then do :
assign vcMsgOIAdj = trim(substitute(#T-3'The system cannot determine the sub-account based upon the bank information of the invoice (&1). Check the bank number definition and its bank account.':255(69576)T-3#,tOIAdj.tcInvoiceNumberReference))
viLocalReturnStatus = -3.
<M-2 run SetMessage
(input vcMsgOIAdj (icMessage),
input '':U (icArguments),
input '':U (icFieldName),
input '':U (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input '':U (icRowid),
input 'QadFin-8160':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
Leave CIINVOICEDISCOUNTBLOCK.
end. /* if not available tqCInvoiceBankForDiscountGL */
/* ======================================================================================== */
/* If it concerns DiscountTaxAtPayment, then part of the discount is posted on tax-accounts */
/* ======================================================================================== */
if tOIAdj.tlIsDiscountTaxAtPayment = true
then do :
/* Read all tax-details of the invoice that use DiscountTaxAtPayment */
<Q-4 run CInvoiceVatByAllocationInfo (all) (Read) (NoCache)
(input tOIAdj.tiInvoiceID, (CInvoiceID)
input vcDomainCode, (DomainCode)
input true, (DiscountTaxAtPayment)
output dataset tqCInvoiceVatByAllocationInfo) in BCInvoice >
/* Read all posting-vat of the CI-posting of the invoice */
<Q-5 run PostingVatByPostingID (all) (Read) (NoCache)
(input tOIAdj.tiCompanyId, (CompanyId)
input tOIAdj.tiInvoicePostingID, (PostingID)
output dataset tqPostingVatByPostingID) in BPosting >
/* Go through all CInvoiceVAT and their associated postingvat-records */
for each tqCInvoiceVatByAllocationInfo where
tqCInvoiceVatByAllocationInfo.tiCInvoice_ID = tOIAdj.tiInvoiceID and
tqCInvoiceVatByAllocationInfo.tltx2_pmt_disc = true and
tqCInvoiceVatByAllocationInfo.tlCInvoiceVatIsTaxable = true
no-lock,
each tqPostingVatByPostingID where
tqPostingVatByPostingID.tiVat_ID = tqCInvoiceVatByAllocationInfo.tiVat_ID and
tqPostingVatByPostingID.tcTxenvTaxEnv = tqCInvoiceVatByAllocationInfo.tcTxenvTaxEnv and
tqPostingVatByPostingID.tcTxuTaxUsage = tqCInvoiceVatByAllocationInfo.tcTxuTaxUsage and
tqPostingVatByPostingID.tcTxclTaxCls = tqCInvoiceVatByAllocationInfo.tcTxclTaxCls
no-lock :
/** ==============================================================================================================================================**/
/** Calculate the amount to be posted on the tax-account: value of the postingvat, multiplied with the DiscountAmount, divided by the invoice-amount **/
/* Pro-rate this for the tax-base as well */
/** ==============================================================================================================================================**/
assign vdDiscountAmount = tOIAdj.tdAdjustmentDiscountAmountTC
vdDiscTaxDebitTC = tqPostingVatByPostingID.tdPostingVatTaxDebitTC * (tOIAdj.tdAdjustmentDiscountAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC)
vdDiscTaxCreditTC = tqPostingVatByPostingID.tdPostingVatTaxCreditTC * (tOIAdj.tdAdjustmentDiscountAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC)
vdDiscountOnTaxAccountTC = if tqPostingVatByPostingID.tdPostingVatTaxDebitTC <> 0
then vdDiscTaxDebitTC
else vdDiscTaxCreditTC.
if tOIAdj.tdInvoiceBalanceTC < tOIAdj.tdNewBalanceTC and
tOIAdj.tdNewBalanceTC > 0
then assign vdDiscountOnTaxAccountTC = - vdDiscountOnTaxAccountTC.
assign vdDiscountOnTaxAccountLC = <M-14 RoundAmount
(input vdDiscountOnTaxAccountTC * tqPostingVatByPostingID.tdPostingLineExchangeRate * tqPostingVatByPostingID.tdPostingLineRateScale (idUnroundedAmount),
input viCompanyLCId (iiCurrencyID),
input ? (icCurrencyCode)) in BOpenItemAdjustment>
vdDiscountOnTaxAccountTC = <M-13 RoundAmount
(input vdDiscountOnTaxAccountTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
vdBaseTC = <M-10 RoundAmount
(input (tqPostingVatByPostingID.tdPostingVatBaseDebitTC - tqPostingVatByPostingID.tdPostingVatBaseCreditTC) * tOIAdj.tdAdjustmentDiscountAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
vdDiscTaxDebitTC = <M-47 RoundAmount
(input vdDiscTaxDebitTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
vdDiscTaxCreditTC = <M-12 RoundAmount
(input vdDiscTaxCreditTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>.
/* Get the correct exchange date to use for the exchange rate conversions */
<M-39 run GetExchangeRateDateToUse
(input false (ilisCustomer),
input tOIAdjust.OIAdjustPostingDate (itPostingDate),
input tOIAdj.ttInvoiceDate (itInvoiceDate),
input tOIAdj.tcInvoiceType (icInvoiceType),
output vtExchangeRateDateToUse (otExchangeRateDate),
output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
<M-9 run AddPostingLineTax
(input vcMainPostingTcRowid (icPostingTcRowid),
input tqPostingVatByPostingID.ttPostingVatTaxPointDate (itTaxPointDate),
input tqPostingVatByPostingID.tcPostingLineText (icPostingLineText),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode),
input tqPostingVatByPostingID.tcPostingVatInOut (icPostingVatInOut),
input tqCInvoiceVatByAllocationInfo.tcVatCode (icPostingVatCode),
input tqCInvoiceVatByAllocationInfo.tctx2_domain (icPostingVatDomain),
input tqPostingVatByPostingID.tdPostingLineExchangeRate (idExchangeRate),
input tqPostingVatByPostingID.tdPostingLineRateScale (idExchangeRateScale),
input {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType),
input ? (idCCExchangeRate),
input ? (idCCExchangeRateScale),
input tqPostingVatByPostingID.tcPostingVatTransType (icTransactionType),
input tqPostingVatByPostingID.tcPostingVatTaxTransType (icTaxTransactionType),
input (if tqPostingVatByPostingID.tdPostingVatBaseDebitTC = 0 or tqPostingVatByPostingID.tdPostingVatBaseDebitTC = ? then absolute(vdBaseTC) else 0) (idBaseDebitTC),
input (if tqPostingVatByPostingID.tdPostingVatBaseCreditTC = 0 or tqPostingVatByPostingID.tdPostingVatBaseCreditTC = ? then absolute(vdBaseTC) else 0) (idBaseCreditTC),
input ? (idBaseDebitLC),
input ? (idBaseCreditLC),
input ? (idBaseDebitCC),
input ? (idBaseCreditCC),
input (if tqPostingVatByPostingID.tdPostingVatTaxDebitTC = 0 or tqPostingVatByPostingID.tdPostingVatTaxDebitTC = ? then vdDiscountOnTaxAccountTC else 0) (idVatDebitTC),
input (if tqPostingVatByPostingID.tdPostingVatTaxCreditTC = 0 or tqPostingVatByPostingID.tdPostingVatTaxCreditTC = ? then vdDiscountOnTaxAccountTC else 0) (idVatCreditTC),
input (if tqPostingVatByPostingID.tdPostingVatTaxDebitTC = 0 or tqPostingVatByPostingID.tdPostingVatTaxDebitTC = ? then vdDiscountOnTaxAccountLC else 0) (idVatDebitLC),
input (if tqPostingVatByPostingID.tdPostingVatTaxCreditTC = 0 or tqPostingVatByPostingID.tdPostingVatTaxCreditTC = ? then vdDiscountOnTaxAccountLC else 0) (idVatCreditLC),
input ? (idVatDebitCC),
input ? (idVatCreditCC),
input tqPostingVatByPostingID.tcGLCode (icNormalTaxGLCode),
input tqPostingVatByPostingID.tcDivisionCode (icNormalTaxDivisionCode),
input tqPostingVatByPostingID.tlPostingVatIsAbsRet (ilNormalTaxIsAlreadyAbsRet),
input '':U (icAbsRetTaxGLCode),
input '':U (icAbsRetTaxDivisionCode),
input tqPostingVatByPostingID.tcFromTxzTaxZone (icFromTaxZone),
input tqPostingVatByPostingID.tcToTxzTaxZone (icToTaxZone),
input tqPostingVatByPostingID.tcTxenvTaxEnv (icTaxEnvrionment),
input tqCInvoiceVatByAllocationInfo.tlCInvoiceVatIsTaxable (ilIsTaxable),
input tqPostingVatByPostingID.tcTxclTaxCls (icTaxClass),
input tqPostingVatByPostingID.tcTxuTaxUsage (icTaxUsage),
input tqPostingVatByPostingID.tcTxtyTaxType (icTaxType),
input tqPostingVatByPostingID.tlPostingVatIsReverseCharge (ilTaxIsReverseChargeDomestic),
input ? (iiTaxAddressID),
input tqPostingVatByPostingID.tcPostingVatTaxIDFeder (icTaxIDFeder),
input tqPostingVatByPostingID.tcPostingVatTaxIDState (icTaxIDState),
input tqPostingVatByPostingID.tcPostingVatTaxIDMisc1 (icTaxIDMisc1),
input tqPostingVatByPostingID.tcPostingVatTaxIDMisc2 (icTaxIDMisc2),
input tqPostingVatByPostingID.tcPostingVatTaxIDMisc3 (icTaxIDMisc3),
input ? (iiOwnAddressID),
input tqPostingVatByPostingID.tcPostingVatOwnTaxIDFeder (icOwnTaxIDFeder),
input tqPostingVatByPostingID.tcPostingVatOwnTaxIDState (icOwnTaxIDState),
input tqPostingVatByPostingID.tcPostingVatOwnTaxIDMisc1 (icOwnTaxIDMisc1),
input tqPostingVatByPostingID.tcPostingVatOwnTaxIDMisc2 (icOwnTaxIDMisc2),
input tqPostingVatByPostingID.tcPostingVatOwnTaxIDMisc3 (icOwnTaxIDMisc3),
input tqPostingVatByPostingID.tiPostingVatOwnTaxDeclarat (iiOwnTaxIDDeclarat),
input tqPostingVatByPostingID.tlPostingVatIsSuspDel (ilIsSuspDel),
output viNewPostingLineID (oiNormalTaxPostingLineID),
output vcDummy (ocNormalTaxPostingLineTcRowid),
output viDummy (oiAbsRetTaxPostingLineID),
output vcDummy (ocAbsRetTaxPostingLineTcRowid),
output viDummy (oiNormalTaxPostingVatID),
input false (ilLinkedCrCyDaemonReqExists),
input no (ilCalledFromMoveSuspDelTax),
input vtExchangeRateDateToUse (itInvoiceExchangeRateDate),
output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
/* Hold the PostingLine_ID of the new postingline in field in tOIAdj: this is done to be able to remove the postingelines in case an error occurs later on */
if viNewPostingLineID <> 0 and
viNewPostingLineID <> ?
then assign tOIAdj.tcCreatedDiscountPostingLines = tOIAdj.tcCreatedDiscountPostingLines + ",":U + string(viNewPostingLineID).
if viFcReturnSuper <> 0
then assign viLocalReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Leave CIINVOICEDISCOUNTBLOCK.
/* Hold the total of the values posted on a tax-account */
if tOIAdj.tcAdjustmentCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
then assign vdDiscountTotalOnTaxAccountTC = vdDiscountTotalOnTaxAccountTC - vdDiscTaxCreditTC + vdDiscTaxDebitTC.
else assign vdDiscountTotalOnTaxAccountTC = vdDiscountTotalOnTaxAccountTC + vdDiscTaxCreditTC - vdDiscTaxDebitTC.
assign vdDiscountTotalOnTaxAccountLC = vdDiscountTotalOnTaxAccountLC - vdDiscountOnTaxAccountLC.
end. /* for each tqCInvoiceVatByAllocationInfo where */
end. /* if tOIAdj.tlIsDiscountTaxAtPayment = true */
/* ======================================================================================== */
/* Assign the TaxDiscountAmount-fields in tOIAdj as they will be used later on */
/* Post the discount - that is not yet posted to the taxes - to the discount account */
/* ======================================================================================== */
assign tOIAdj.tdTaxDiscountAmountTC = vdDiscountTotalOnTaxAccountTC
tOIAdj.tdTaxDiscountAmountLC = vdDiscountTotalOnTaxAccountLC.
if tOIAdj.tdInvoiceBalanceTC < tOIAdj.tdNewBalanceTC and tOIAdj.tdNewBalanceTC > 0
then assign vdDiscountOnDiscountAccountTC = if tOIAdj.tcInvoiceType = {&INVOICETYPE-INVOICE}
then (tOIAdj.tdAdjustmentDiscountAmountTC - abs(vdDiscountTotalOnTaxAccountTC))
else (- tOIAdj.tdAdjustmentDiscountAmountTC - vdDiscountTotalOnTaxAccountTC).
else vdDiscountOnDiscountAccountTC = tOIAdj.tdAdjustmentDiscountAmountTC - vdDiscountTotalOnTaxAccountTC.
if vdDiscountOnDiscountAccountTC = 0
then next.
<M-11 run AddStandardPosting
(input vcMainPostingTcRowid (icPostingtcRowid),
input tqCInvoiceBankForDiscountGL.tcBankAPDiscountGLCode (icGLCode),
input (if tqCInvoiceBankForDiscountGL.tlGLIsDivisionAccount = true then tqCInvoiceBankForDiscountGL.tcDivisionCode else '':U) (icDivisionCode),
input '':U (icCostCentreCode),
input '':U (icCostCentreText),
input '':U (icProjectCode),
input '':U (icProjectText),
input '':U (icIntercoBusinessRelationCode),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode),
input (if tOIAdj.tcInvoiceBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT} then vdDiscountOnDiscountAccountTC else 0) (idDebitTC),
input ? (idDebitLC),
input ? (idDebitCC),
input ? (idDebitPC),
input (if tOIAdj.tcInvoiceBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT} then 0 else vdDiscountOnDiscountAccountTC) (idCreditTC),
input ? (idCreditLC),
input ? (idCreditCC),
input ? (idCreditPC),
input 0 (idQty),
input string(viMainPostingYear,'9999':U) + '/':U + string(viMainPostingPeriod,'99':U) + '/ ':U + trim(vcMainPostingJournalCode) + '/':U + string(viMainPostingVoucher,'999999999':U) (icLineText),
input '':U (icSafText),
input tDefaultSafsFromOIAdj (tDefaultSafs),
input {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType),
input tOIAdj.tdAdjustmentExchangeRate (idExchangeRate),
input tOIAdj.tdAdjustmentExchangeRateScale (idExchangeRateScale),
output viNewPostingLineID (oiPostingLineId),
input 0 (iiSafStructureId),
input '':U (icSafStructureCode),
input '':U (icAllocationKey),
output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
/* Hold the PostingLine_ID of the new postingline in field in tOIAdj: this is done to be able to remove the postingelines in case an error occurs later on */
if viNewPostingLineID <> 0 and
viNewPostingLineID <> ?
then assign tOIAdj.tcCreatedDiscountPostingLines = tOIAdj.tcCreatedDiscountPostingLines + ",":U + string(viNewPostingLineID).
if viFcReturnSuper <> 0
then assign viLocalReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Leave CIINVOICEDISCOUNTBLOCK.
END. /* CIINVOICEDISCOUNTBLOCK */
/* ================== */
/* Close BJournaEntry */
/* Exception handling */
/* ================== */
<I-8 {bFcCloseInstance
&CLASS = "BJournalEntry"}>
assign oiReturnStatus = viLocalReturnStatus.