Prepay posting vendor invoice with amounts
class BPContractAddToPendingPrePayVendorInvoice
{
PurchTable purchTable;
VendInvoiceInfoTable vendInvoiceInfoTable;
VendInvoiceInfoLine vendInvoiceInfoLine,vendInvoiceInfoLineCount;
VendInvoiceInfoSubTable vendInvoiceInfoSubTable;
BPInvoiceProposalTable proposalTable;
PurchLine purchLine;
PurchParmUpdate parmupdate;
PurchFormLetter purchFormletter;
PurchPrepayTable prepayTable;
Num parmId;
VendInvoiceJour invoiceJour;
LineAmount applyPrepayAmt;
public void runOperation(BPInvoiceProposalTable _proposalTable,LineAmount _amountPrepay = 0)
{
proposalTable = _proposalTable;
purchTable = proposalTable.bpProjContractTable().purchTable();
select firstonly prepayTable where prepayTable.PurchTable == purchTable.PurchId;
applyPrepayAmt = _amountPrepay;
if(!_amountPrepay)
{
applyPrepayAmt = prepayTable.PrepayAvailable ;
}
ttsbegin;
this.createPendingInvoiceHeader();
if(vendInvoiceInfoTable)
{
this.createParmUpdate();
this.createSubTable();
this.createPedingInvoiceLines();
}
if(vendInvoiceInfoLine.RecId)
{
this.postPendingVendorInvoice();
}
else
{
this.deletePendingVendorInvoice();
throw error("@BPP:BPP0224") ;
}
ttscommit;
}
public static void createPendingVendorInvoice(container _callerparameters)
{
BPInvoiceProposalTable propTable =conPeek(_callerparameters, 1);
LineAmount applyPrpeayAmt = conPeek(_callerparameters, 2);
BPContractAddToPendingPrePayVendorInvoice pendingInv = new BPContractAddToPendingPrePayVendorInvoice();
pendingInv.runOperation(propTable,applyPrpeayAmt);
}
public void createPendingInvoiceHeader()
{
vendInvoiceInfoTable.clear();
vendInvoiceInfoTable.initValue();
vendInvoiceInfoTable.initFromPurchTable(purchTable);
vendInvoiceInfoTable.InvoiceType = PurchInvoiceType::VendorAdvance;
vendInvoiceInfoTable.DocumentOrigin = DocumentOrigin::Manual;
vendInvoiceInfoTable.CurrencyCode = purchTable.CurrencyCode;
vendInvoiceInfoTable.DeliveryName = purchTable.DeliveryName;
vendInvoiceInfoTable.InvoiceAccount = purchTable.InvoiceAccount ;
vendInvoiceInfoTable.OrderAccount = purchTable.OrderAccount;
vendInvoiceInfoTable.Num = 'Pre-'+purchTable.PurchId ; //add invoice number in here
vendInvoiceInfoTable.Description = prepayTable.Description;
vendInvoiceInfoTable.PurchName = purchTable.PurchName;
vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;
vendInvoiceInfoTable.DocumentDate = systemDateGet();
vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::Unknown;
vendInvoiceInfoTable.ParmJobStatus = ParmJobStatus::Waiting;
vendInvoiceInfoTable.Approved = NoYes::Yes;
vendInvoiceInfoTable.Approver = HcmWorkerLookup::currentWorker();
vendInvoiceInfoTable.DeliveryPostalAddress= purchTable.DeliveryPostalAddress;
vendInvoiceInfoTable.FixedDueDate = systemDateGet();
vendInvoiceInfoTable.AddressRefRecId = purchTable.AddressRefRecId;
vendInvoiceInfoTable.DefaultDimension = vendInvoiceInfoTable.copyDimension(purchTable.DefaultDimension);
//l̥vendInvoiceInfoTable.ReceivedDate = systemDateGet();
vendInvoiceInfoTable.ReportingCurrencyExchangeRate = 100;
purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
parmid=purchFormLetter.parmId(NumberSeq::newGetNum(CompanyInfo::numRefParmId()).num());
parmId= purchFormLetter.parmId();
vendInvoiceInfoTable.ParmId = parmId;
vendInvoiceInfoTable.defaultField(fieldNum(VendInvoiceInfoTable,RemittanceLocation),purchTable,null);
vendInvoiceInfoTable.defaultField(fieldNum(VendInvoiceInfoTable,FixedDueDate),purchTable,null);
vendInvoiceInfoTable.defaultField(fieldNum(VendInvoiceInfoTable,ExchRate),purchTable,null);
vendInvoiceInfoTable.defaultField(fieldNum(VendInvoiceInfoTable,TransDate),purchTable,null);
vendInvoiceInfoTable.defaultField(fieldNum(VendInvoiceInfoTable,PaymMode),purchTable,null);
vendInvoiceInfoTable.insert();
}
public void createParmUpdate()
{
parmupdate.ParmId = vendInvoiceInfoTable.ParmId;
parmupdate.SpecQty = PurchUpdate::All;
parmupdate.DocumentStatus = DocumentStatus::Invoice;
parmupdate.SumIncludePending =NoYes::Yes;
parmupdate.insert() ;
}
public void createPedingInvoiceLines()
{
//Generate Pending Invoice Line
vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initValue();
//vendInvoiceInfoLine.defaultRow(purchLine);
//vendInvoiceInfoLine.initFromPurchLine(purchLine);
vendInvoiceInfoLine.DeliveryName = vendInvoiceInfoTable.DeliveryName;
vendInvoiceInfoLine.ParmId = vendInvoiceInfoTable.ParmId;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.currencyCode = vendInvoiceInfoTable.CurrencyCode;
// vendInvoiceInfoLine.LineNum = 1;
vendInvoiceInfoLine.InvoiceAccount = vendInvoiceInfoTable.InvoiceAccount;
vendInvoiceInfoLine.InventDimId = InventDim::findOrCreateBlank().inventDimId;
vendInvoiceInfoLine.OrderAccount = vendInvoiceInfoTable.OrderAccount;
vendInvoiceInfoLine.OrigPurchId = vendInvoiceInfoTable.PurchId;
vendInvoiceInfoLine.ProcurementCategory = prepayTable.PrepayCategoryId;
// vendInvoiceInfoLine.PurchLineRecId = purchLine.RecId;
// vendInvoiceInfoLine.InventTransId = purchLine.InventTransId;
vendInvoiceInfoLine.DocumentOrigin = DocumentOrigin::Manual;
vendInvoiceInfoLine.LineType = PurchInvoiceLineType::Advance;
vendInvoiceInfoLine.ReceiveNow =1;
vendInvoiceInfoLine.RemainBefore = 0;
vendInvoiceInfoLine.RemainAfter = -1;
vendInvoiceInfoLine.PurchPrice =applyPrepayAmt;
vendInvoiceInfoLine.LineAmount = applyPrepayAmt;
// vendInvoiceInfoLine.DefaultDimension = purchLine.DefaultDimension;
vendInvoiceInfoLine.insert();
}
public Void createSubTable()
{
AccountingEvent accountingEvent;
AccountingDistribution accountingDistribution;
select firstonly RecId from accountingEvent
exists join accountingDistribution
where accountingEvent.RecId == accountingDistribution.AccountingEvent
&& accountingEvent.SourceDocumentHeader == purchTable.SourceDocumentHeader;
vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.initValue();
vendInvoiceInfoSubTable.defaultRow();
vendInvoiceInfoSubTable.ParmId = vendInvoiceInfoTable.ParmId;
vendInvoiceInfoSubTable.OrigPurchId = vendInvoiceInfoTable.PurchId;
vendInvoiceInfoSubTable.PurchName = vendInvoiceInfoTable.PurchName;
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.SourceAccountingEvent = accountingEvent.RecId;
vendInvoiceInfoSubTable.insert();
}
public void postPendingVendorInvoice()
{
VendInvoiceInfoTable localVendInvoiceInfoTableForPosting = VendInvoiceInfoTable::find(VendInvoiceInfoTable.ParmId, VendInvoiceInfoTable.TableRefId);
ttsbegin;
localVendInvoiceInfoTableForPosting.selectForUpdate(true);
localVendInvoiceInfoTableForPosting.updateExpectedValuesAndMatchStatus();
ttscommit;
purchFormletter = PurchFormletter_Invoice::newFromSavedInvoice(localVendInvoiceInfoTableForPosting);
purchFormletter.parmInvoiceType(PurchInvoiceType::VendorAdvance);
purchFormLetter.purchParmUpdate(parmupdate);
purchFormLetter.parmId(parmId);
purchFormLetter.proforma(false);
purchFormLetter.reArrangeNow(false);
purchFormLetter.printFormLetter(false);
purchFormLetter.printout(Printout::Current);
purchFormLetter.usePrintManagement(false);
purchFormLetter.parmParmTableNum (localVendInvoiceInfoTableForPosting.Num);
purchFormLetter.specQty ( PurchUpdate::All);
purchFormLetter.parmNumber(parmupdate.SumNum);
// purchFormLetter.chooseLines(true,localVendInvoiceInfoTableForPosting,true);
purchFormLetter.run();
invoiceJour = purchFormletter.getOutputContract().parmJournal() as VendInvoiceJour;
if(invoiceJour )
{
this.endPostingUpdate();
info(strfmt("@BPP:BPP0223", invoiceJour.InvoiceId));
}
else
{
this.deletePendingVendorInvoice();
throw error ("Posting cancelled");
}
}
public Void deletePendingVendorInvoice()
{
if(vendInvoiceInfoTable.RecId)
{
ttsbegin;
vendInvoiceInfoTable.delete();
ttscommit;
}
}
public void endPostingUpdate()
{
ttsbegin;
invoiceJour.selectForUpdate(true);
invoiceJour.BPInvoiceProposald = proposalTable.BPInvoiceProposalD;
invoiceJour.BPContractNumber = proposalTable.BPContractNumber;
invoiceJour.doUpdate();
ttscommit;
}
}
// Prepay amounts appling
class BPContractApplyPrepaymentsToVendInvoice
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
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.InvoiceRecId = vendInvoiceInfoTable.RecId;
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;
}
}
}
Comments
Post a Comment