Posting vendor invoice with amounts
class BPContractAddToPendingVendorInvoice
{
BPInvoiceProposalTable proposalTable;
BPInvoiceProposalLine proposalLine,proposalLineCount;
PurchTable purchTable;
VendInvoiceInfoTable vendInvoiceInfoTable;
VendInvoiceInfoLine vendInvoiceInfoLine,vendInvoiceInfoLineCount;
VendInvoiceInfoSubTable vendInvoiceInfoSubTable;
PurchLine purchLine;
PurchParmUpdate parmupdate;
PurchFormLetter purchFormletter;
BPInvoiceProposalD proposalId;
Num parmId;
VendInvoiceJour invoiceJour;
LineAmount applyprePayAmt ;
public static void createPendingVendorInvoice(container _callerparameters)
{
BPInvoiceProposalTable propTable =conPeek(_callerparameters, 1);
LineAmount applyPrpeayAmt = conPeek(_callerparameters, 2);
BPContractAddToPendingVendorInvoice pendingInv = new BPContractAddToPendingVendorInvoice();
pendingInv.runOperation(propTable,applyPrpeayAmt);
}
public void runOperation(BPInvoiceProposalTable _invoiceProposalTable,LineAmount _applyPreapyAmt)
{
applyprePayAmt = _applyPreapyAmt;
proposalTable = _invoiceProposalTable;
select purchTable
where purchTable.BPContractNumber == _invoiceProposalTable.BPContractNumber;
ttsbegin;
this.createPendingInvoiceHeader();
if(vendInvoiceInfoTable)
{
this.createParmUpdate();
this.createSubTable();
this.createPedingInvoiceLines();
}
ttscommit;
proposalId = _invoiceProposalTable.BPInvoiceProposalD;
select count(RecId) from proposalLineCount
where proposalLineCount.BPInvoiceProposalD == proposalTable.BPInvoiceProposalD;
select count(RecId) from vendInvoiceInfoLineCount
where vendInvoiceInfoLineCount.ParmId == vendInvoiceInfoTable.ParmId
&& vendInvoiceInfoLineCount.TableRefId == vendInvoiceInfoTable.TableRefId;
if(proposalLineCount.RecId == vendInvoiceInfoLineCount.RecId)
{
this.postPendingVendorInvoice();
}
else
{
this.deletePendingVendorInvoice();
throw error("@BPP:BPP0224") ;
}
}
public void createPendingInvoiceHeader()
{
vendInvoiceInfoTable.clear();
vendInvoiceInfoTable.initValue();
vendInvoiceInfoTable.initFromPurchTable(purchTable);
vendInvoiceInfoTable.DocumentOrigin = DocumentOrigin::Manual;
vendInvoiceInfoTable.CurrencyCode = purchTable.CurrencyCode;
vendInvoiceInfoTable.DeliveryName = purchTable.DeliveryName;
vendInvoiceInfoTable.InvoiceAccount = purchTable.InvoiceAccount ;
vendInvoiceInfoTable.OrderAccount = purchTable.OrderAccount;
vendInvoiceInfoTable.Num = proposalTable.BPInvoiceProposalD ; //add invoice number in here
vendInvoiceInfoTable.Description = proposalTable.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()
{
while select proposalLine
where proposalLine.BPInvoiceProposalD == proposalTable.BPInvoiceProposalD
join purchLine
where purchLine.BPHCOLineDetailsRecId == proposalLine.HCOLineDetailsRecId
&& purchLine.PurchId == purchTable.PurchId
{
//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 = any2int(purchLine.LineNumber);
vendInvoiceInfoLine.InvoiceAccount = vendInvoiceInfoTable.InvoiceAccount;
vendInvoiceInfoLine.InventDimId = purchLine.InventDimId;
vendInvoiceInfoLine.OrderAccount = vendInvoiceInfoTable.OrderAccount;
vendInvoiceInfoLine.ProcurementCategory = purchLine.ProcurementCategory;
vendInvoiceInfoLine.PurchLineRecId = purchLine.RecId;
vendInvoiceInfoLine.InventTransId = purchLine.InventTransId;
vendInvoiceInfoLine.DocumentOrigin = DocumentOrigin::Manual;
vendInvoiceInfoLine.ReceiveNow = proposalLine.PurchQty;
vendInvoiceInfoLine.RemainBefore = purchLine.RemainPurchPhysical;
vendInvoiceInfoLine.RemainAfter = (purchLine.RemainPurchPhysical - proposalLine.PurchQty );
vendInvoiceInfoLine.PurchPrice = purchLine.PurchPrice;
vendInvoiceInfoLine.LineAmount = proposalLine.InvoiceNowAmount;
vendInvoiceInfoLine.PSAReleaseAmount = proposalLine.PSAReleaseAmount ;
vendInvoiceInfoLine.PSAIsFinal = proposalLine.PSAIsFinal;
vendInvoiceInfoLine.PSAReleasePercent = proposalLine.PSAReleasePercent ;
vendInvoiceInfoLine.PSARetainageAmount = proposalLine.PSARetainageAmount ;
vendInvoiceInfoLine.PSARetainageBalance = proposalLine.PSARetainageBalance ;
vendInvoiceInfoLine.PSARetainagePercent = proposalLine.PSARetainagePercent ;
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;
if(applyprePayAmt)
{
BPContractApplyPrepaymentsToVendInvoice::applyPOInvPrepayment(localVendInvoiceInfoTableForPosting, applyprePayAmt);
}
purchFormletter = PurchFormletter_Invoice::newFromSavedInvoice(localVendInvoiceInfoTableForPosting);
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();
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()
{
BPHCOLineDetailsTable lineDetailsTable;
BPInvoiceProposalLine proposalLineUpdate;
//Re-read buffer
proposalTable = BPInvoiceProposalTable::find(proposalId,true);
ttsbegin;
proposalTable.selectForUpdate(true);
proposalTable.InvoiceAmount= invoiceJour.InvoiceAmount;
proposalTable.BPDocumentStatus = BPDocumentStatus::Invoiced;
proposalTable.doUpdate();
ttscommit;
ttsbegin;
invoiceJour.selectForUpdate(true);
invoiceJour.BPInvoiceProposald = proposalTable.BPInvoiceProposalD;
invoiceJour.BPContractNumber = proposalTable.BPContractNumber;
invoiceJour.doUpdate();
ttscommit;
ttsbegin;
while select forupdate lineDetailsTable
exists join proposalLineUpdate
where proposalLineUpdate.HCOLineDetailsRecId == lineDetailsTable.RecId
&& proposalLineUpdate.BPInvoiceProposald == proposalTable.BPInvoiceProposalD
{
if(lineDetailsTable.LineAmount == lineDetailsTable.invoicedAmt())
{
lineDetailsTable.BPHCOLineDetailsInvoiceStatus = BPHCOLineDetailsInvoiceStatus::FullyInvoiced;
}
else
{
lineDetailsTable.BPHCOLineDetailsInvoiceStatus = BPHCOLineDetailsInvoiceStatus::PartiallyInvoiced;
}
lineDetailsTable.doUpdate();
}
ttscommit;
BPProjContractTable::updateContractOrderStatus(proposalTable.bpProjContractTable());
}
}
// Prepay amounts appply if
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