Posting Purchase Order Invoices via X++

In the scenario where you need to post purchase order invoices on a per packing slip basis, you will want to make use of the VendInvoiceInfo series of tables: VendInvoiceInfoTable, VendInvoiceInfoSubTable, VendInvoiceInfoLine, and VendInvoiceInfoSubLine.  A few notes about using these tables from code.  Since there will be no form front end, you won't specify a ParmID in your code.  Also there can only be one invoice line per PO line in each invoice, so if you have multiple receipts to invoice for one PO line, each different packing slip goes in the subLine table.  Also, if you need to use charge codes to apply to the invoice, MarkupTrans table and MarkupAllocation class are your tools.  Below are some code snippets to refer to - they are not intended to be copy/pasted and that's it.  They are just some examples of initializing the tables.  in the code below, psJour is VendPackingSlipJour and psTrans is VendPackingSlipTrans.  You will use class PurchFormLetter_Invoice to do the posting.  One other note - you may need to update fields in VendTrans after posting, such as the Approved field, for everything to work properly.

        table.initValue();
        table.initFromPurchTable(PurchTable::find(purchId));
        table.DocumentOrigin = DocumentOrigin::Manual;
        table.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;
        table.LastMatchVariance = LastMatchVarianceOptions::OK;
        table.ParmJobStatus = ParmJobStatus::Waiting;
        table.TransDate = today();

                    subTable.initValue();
                    subTable.defaultRow();
                    subTable.ParmId = table.ParmId;
                    subTable.OrigPurchId = table.PurchId;
                    subTable.PurchName = table.PurchName;
                    subTable.TableRefId = table.TableRefId;

                        select psJour where psJour.PurchId == purchId && psJour.PackingSlipId ==                                        packingSlipId;
                        select psTrans where psTrans.PackingSlipId == packingSlipId
                                          && psTrans.VendPackingSlipJour == psJour.RecId;
                        purchLine = psTrans.purchLine();
                        select forUpdate line where line.PurchLineRecId == purchLine.RecId;
                        if (line)
                        {
                            line.ReceiveNow += psTrans.Qty;
                            line.RemainBefore += psTrans.Qty;
                            line.RemainBeforeInvent += psTrans.Qty;
                            line.InventNow += psTrans.Qty;
                            line.LineAmount = line.calcLineAmount(line.ReceiveNow, purchLine);
                            line.update();
                            subLine.initValue();
                            subLine.defaultRow();
                            subLine.ParmId = table.ParmId;
                            subLine.LineRefRecId = line.RecId;
                            subLine.ReceiveNow = psTrans.Qty;
                            subLine.InventNow = psTrans.Qty;
                            subLine.JournalRefRecId = psTrans.RecId;
                            subLine.JournalRefTableId = psTrans.TableId;
                            subLine.DocumentId = psTrans.PackingSlipId;
                            subLine.insert();
                            line.clear();
                            subLine.clear();
                        }
                        else
                        {
                            line.initValue();
                            line.defaultRow(null, purchLine);
                            line.initFromPurchLine(purchLine);
                            line.PurchLineRecId = purchLine.RecId;
                            line.ParmId = table.ParmId;
                            line.TableRefId = table.TableRefId;
                            line.LineNum = any2real(line.PurchaseLineLineNumber);
                            line.InventDimId = psTrans.InventDimId;
                            line.InventTransId = psTrans.InventTransId;
                            line.DocumentOrigin = DocumentOrigin::Manual;
                            line.ReceiveNow = psTrans.Qty;
                            line.RemainBefore = psTrans.Qty;
                            line.RemainBeforeInvent = psTrans.Qty;
                            line.InventNow = psTrans.Qty;
                            line.DefaultDimension = purchLine.DefaultDimension;
                            line.PriceUnit = purchLine.PriceUnit;
                            line.PurchPrice = purchLine.PurchPrice;
                            line.LineAmount = line.calcLineAmount(psTrans.Qty, purchLine);
                            line.insert();
                            subLine.initValue();
                            subLine.defaultRow();
                            subLine.ParmId = table.ParmId;
                            subLine.LineRefRecId = line.RecId;
                            subLine.ReceiveNow = psTrans.Qty;
                            subLine.InventNow = psTrans.Qty;
                            subLine.JournalRefRecId = psTrans.RecId;
                            subLine.JournalRefTableId = psTrans.TableId;
                            subLine.DocumentId = psTrans.PackingSlipId;
                            subLine.insert();
                            line.clear();
                            subLine.clear();
                        }

letter = PurchFormLetter_Invoice::newFromSavedInvoice(table);
letter.update(table.purchTable(), table.Num);

Comments