Hi Everyone,
Need urgent help on below issue,
I am trying to apply prepayment on Purchase Order during invoice posting.
Below code I have found on one of the blogs.
However it is not working in my case. Didn't get any error but the prepayment amount to be applied has not been applied when I run the code.
Please suggest some solution. Thank you in advance.
Link which I referred is,
http://axtechsolutions.blogspot.com/2018/11/job-to-apply-vendor-prepayment-on-po.html
public static void applyPOInvPrepayment(VendInvoiceInfoTable _vendInvoiceInfoTable, Amount _amountToApply)
{
VendAdvanceInvoice vendAdvanceInvoice;
VendInvoiceJour localVendInvoiceJour;
tmpVendStandardInvoice tmpVendStandardInvoice;
tmpVendAdvanceInvoice tmpVendAdvanceInvoice;
PurchTotals localPurchTotals;
VendTrans vendTrans, vendTransOffset;
PurchPrepayTable purchPrepayTable;
VendInvoiceInfoSubTable vendInvoiceInfoSubTable;
VendInvoiceInfoTable vendInvoiceInfoTable;
select firstonly forupdate * from vendInvoiceInfoTable
where vendInvoiceInfoTable.RecId == _vendInvoiceInfoTable.RecId;
try
{
ttsbegin;
vendAdvanceInvoice = VendAdvanceInvoice::construct(true);
vendAdvanceInvoice.parmCallerTable(vendInvoiceInfoTable);
vendAdvanceInvoice.parmIsInvoicePosted(false);
vendAdvanceInvoice.init();
if(vendInvoiceInfoTable.InvoiceType == PurchInvoiceType::Standard)
{
localPurchTotals = PurchTotals::newParmTable(vendInvoiceInfoTable, PurchUpdate::ReceiveNow,vendInvoiceInfoTable.ParmId, '',DocumentStatus::Invoice);
localPurchTotals.calc(true);
tmpVendStandardInvoice.clear();
while select vendInvoiceInfoSubTable
where vendInvoiceInfoSubTable.ParmId == vendInvoiceInfoTable.ParmId
&& vendInvoiceInfoSubTable.TableRefId == vendInvoiceInfoTable.TableRefId
&& vendInvoiceInfoSubTable.OrigPurchId == vendInvoiceInfoTable.PurchId
{
tmpVendStandardInvoice.clear();
tmpVendStandardInvoice.PurchId = vendInvoiceInfoSubTable.OrigPurchId;
tmpVendStandardInvoice.InvoiceNum = vendInvoiceInfoTable.Num;
tmpVendStandardInvoice.CurrencyCode = vendInvoiceInfoTable.CurrencyCode;
tmpVendStandardInvoice.InvoiceDate = vendInvoiceInfoTable.TransDate;
tmpVendStandardInvoice.InvoiceAccount = vendInvoiceInfoTable.InvoiceAccount;
tmpVendStandardInvoice.InvoiceAmount = localPurchTotals.purchTotalAmount();
tmpVendStandardInvoice.insert();
}
}
while select localVendInvoiceJour
where localVendInvoiceJour.InvoiceType == PurchInvoiceType::VendorAdvance
&& localVendInvoiceJour.InvoiceAccount == tmpVendStandardInvoice.InvoiceAccount
&& localVendInvoiceJour.PurchId == tmpVendStandardInvoice.PurchId
&& localVendInvoiceJour.CurrencyCode == tmpVendStandardInvoice.CurrencyCode
join vendTrans
where vendTrans.Voucher == localVendInvoiceJour.LedgerVoucher
&& vendTrans.AccountNum == localVendInvoiceJour.InvoiceAccount
&& vendTrans.TransDate == localVendInvoiceJour.InvoiceDate
join purchPrepayTable
where purchPrepayTable.PurchTable == localVendInvoiceJour.PurchId
&& purchPrepayTable.PrepayApplicationRemaining > 0
exists join vendTransOffset
where vendTransOffset.RecId == vendTrans.OffsetRecid
&& vendTransOffset.TransType == LedgerTransType::Payment
{
if(abs(vendTrans.AmountCur) == abs(vendTrans.SettleAmountCur))
{
tmpVendAdvanceInvoice.clear();
tmpVendAdvanceInvoice.PurchId = localVendInvoiceJour.PurchId;
tmpVendAdvanceInvoice.InvoiceNum = localVendInvoiceJour.InvoiceId;
tmpVendAdvanceInvoice.CurrencyCode = localVendInvoiceJour.CurrencyCode;
tmpVendAdvanceInvoice.InvoiceDate = localVendInvoiceJour.InvoiceDate;
tmpVendAdvanceInvoice.LedgerVoucher = localVendInvoiceJour.LedgerVoucher;
tmpVendAdvanceInvoice.AdvanceAmount = localVendInvoiceJour.InvoiceAmount;
tmpVendAdvanceInvoice.InvoiceRecId = localVendInvoiceJour.RecId;
tmpVendAdvanceInvoice.AdvanceRemaining = abs(vendTrans.SettleAmountCur);
if(tmpVendAdvanceInvoice.AdvanceRemaining > 0)
{
tmpVendAdvanceInvoice.AppliedToPendingAmount = abs(VendAdvanceInvoice_Application::totalUnpostedAdvanceAppliedAmountForPrepayInvoice(localVendInvoiceJour.RecId));
tmpVendAdvanceInvoice.AdvanceRemaining = tmpVendAdvanceInvoice.AppliedToPendingAmount;
tmpVendAdvanceInvoice.insert();
}
}
}
vendAdvanceInvoice.updateTransMarked(tmpVendStandardInvoice.InvoiceRecId, tmpVendAdvanceInvoice.InvoiceRecId, NoYes::Yes, tmpVendStandardInvoice.SettlementPostingDate);
vendAdvanceInvoice.updateAdvanceApplicationAmount(tmpVendStandardInvoice.InvoiceRecId, tmpVendAdvanceInvoice.InvoiceRecId, -(_amountToApply));
vendInvoiceInfoTable.updateExpectedValuesAndMatchStatus();
ttscommit;
}
catch
{
ttsabort;
}
}