Description
Submethod of OIAdjCommitSubDInvoice 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-21 {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-23 run DInvoiceBankForDiscountGL (all) (Read) (NoCache)
(input tOIAdj.tiInvoiceID, (DInvoiceID)
input tOIAdj.tiCompanyId, (CompanyId)
output dataset tqDInvoiceBankForDiscountGL) in BDInvoice >
Find first tqDInvoiceBankForDiscountGL where
tqDInvoiceBankForDiscountGL.tiDInvoice_ID = tOIAdj.tiInvoiceID and
tqDInvoiceBankForDiscountGL.tcBankARDiscountGLCode <> "":U and
tqDInvoiceBankForDiscountGL.tcBankARDiscountGLCode <> ?
no-lock no-error.
if not available tqDInvoiceBankForDiscountGL
then do :
assign vcMsgOIAdj = trim(substitute(#T-22'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-22#,tOIAdj.tcInvoiceNumberReference))
viLocalReturnStatus = -3.
<M-15 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-8172':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
Leave CIINVOICEDISCOUNTBLOCK.
end. /* if not available tqDInvoiceBankForDiscountGL */
/* ======================================================================================== */
/* 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-24 run DInvoiceVatByAllocationInfo (all) (Read) (NoCache)
(input tOIAdj.tiInvoiceID, (DInvoiceID)
input vcDomainCode, (DomainCode)
input true, (DiscountTaxAtPayment)
output dataset tqDInvoiceVatByAllocationInfo) in BDInvoice >
/* Read all posting-vat of the CI-posting of the invoice */
<Q-19 run PostingVatByPostingID (all) (Read) (NoCache)
(input tOIAdj.tiCompanyId, (CompanyId)
input tOIAdj.tiInvoicePostingID, (PostingID)
output dataset tqPostingVatByPostingID) in BPosting >
/* Go through all DInvoiceVAT and their associated postingvat-records */
for each tqDInvoiceVatByAllocationInfo where
tqDInvoiceVatByAllocationInfo.tiDInvoice_ID = tOIAdj.tiInvoiceID and
tqDInvoiceVatByAllocationInfo.tltx2_pmt_disc = true and
tqDInvoiceVatByAllocationInfo.tlDInvoiceVatIsTaxable = true
no-lock,
each tqPostingVatByPostingID where
tqPostingVatByPostingID.tiVat_ID = tqDInvoiceVatByAllocationInfo.tiVat_ID and
tqPostingVatByPostingID.tcTxenvTaxEnv = tqDInvoiceVatByAllocationInfo.tcTxenvTaxEnv and
tqPostingVatByPostingID.tcTxuTaxUsage = tqDInvoiceVatByAllocationInfo.tcTxuTaxUsage and
tqPostingVatByPostingID.tcTxclTaxCls = tqDInvoiceVatByAllocationInfo.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 */
/* discount */
/** ((Invoice amount - Tax)? Non-discountable amount ) * DiscountPercentage/100 **/
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-25 RoundAmount
(input vdDiscountOnTaxAccountTC * tqPostingVatByPostingID.tdPostingLineExchangeRate * tqPostingVatByPostingID.tdPostingLineRateScale (idUnroundedAmount),
input viCompanyLCId (iiCurrencyID),
input ? (icCurrencyCode)) in BOpenItemAdjustment>
vdDiscountOnTaxAccountTC = <M-26 RoundAmount
(input vdDiscountOnTaxAccountTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>
vdBaseTC = <M-17 RoundAmount
(input (tqPostingVatByPostingID.tdPostingVatBaseDebitTC - tqPostingVatByPostingID.tdPostingVatBaseCreditTC) * tOIAdj.tdAdjustmentDiscountAmountTC / tOIAdj.tdInvoiceOriginalBalanceTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>.
vdDiscTaxDebitTC = <M-89 RoundAmount
(input vdDiscTaxDebitTC (idUnroundedAmount),
input ? (iiCurrencyID),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode)) in BOpenItemAdjustment>.
vdDiscTaxCreditTC = <M-2 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-40 run GetExchangeRateDateToUse
(input true (ilisCustomer),
input tOIAdjust.OIAdjustPostingDate (itPostingDate),
input tOIAdj.ttInvoiceDate (itInvoiceDate),
input tOIAdj.tcInvoiceType (icInvoiceType),
output vtExchangeRateDateToUse (otExchangeRateDate),
output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
<M-13 run AddPostingLineTax
(input vcMainPostingTcRowid (icPostingTcRowid),
input tqPostingVatByPostingID.ttPostingVatTaxPointDate (itTaxPointDate),
input tqPostingVatByPostingID.tcPostingLineText (icPostingLineText),
input tOIAdj.tcInvoiceCurrencyCode (icCurrencyCode),
input tqPostingVatByPostingID.tcPostingVatInOut (icPostingVatInOut),
input tqDInvoiceVatByAllocationInfo.tcVatCode (icPostingVatCode),
input tqDInvoiceVatByAllocationInfo.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 tqDInvoiceVatByAllocationInfo.tlDInvoiceVatIsTaxable (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 tqDInvoiceVatByAllocationInfo where */
end. /* if tOIAdj.tlIsDiscountTaxAtPayment = true */
/* ======================================================================================== */
/* 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 - vdDiscountTotalOnTaxAccountTC)
else (tOIAdj.tdAdjustmentDiscountAmountTC - abs(vdDiscountTotalOnTaxAccountTC)).
else assign vdDiscountOnDiscountAccountTC = tOIAdj.tdAdjustmentDiscountAmountTC - vdDiscountTotalOnTaxAccountTC.
if vdDiscountOnDiscountAccountTC = 0
then next.
<M-18 run AddStandardPosting
(input vcMainPostingTcRowid (icPostingtcRowid),
input tqDInvoiceBankForDiscountGL.tcBankARDiscountGLCode (icGLCode),
input (if tqDInvoiceBankForDiscountGL.tlGLIsDivisionAccount = true then tqDInvoiceBankForDiscountGL.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-12 {bFcCloseInstance
&CLASS = "BJournalEntry"}>
assign oiReturnStatus = viLocalReturnStatus.