Importing AR Invoices into AccPac using HuLib
Dumping some sample HuLib code for creating AR Invoices in Sage. This is copied from one of our projects, so it will have to be modified for whatever project you want to use the code for.
public void ImportInvoices(IEnumerable<InvoiceLine> lines)
{
using (HuView ARIBC = Connection.GetView("AR0031"))
using (HuView ARIBH = Connection.GetView("AR0032"))
using (HuView ARIBD = Connection.GetView("AR0033"))
using (HuView ARIBS = Connection.GetView("AR0034"))
using (HuView ARIBHO = Connection.GetView("AR0402"))
using (HuView ARIBDO = Connection.GetView("AR0401"))
{
ARIBC.Compose(ARIBH);
ARIBH.Compose(ARIBC, ARIBD, ARIBS, ARIBHO, null);
ARIBD.Compose(ARIBH, ARIBC, ARIBDO);
ARIBS.Compose(ARIBH);
ARIBHO.Compose(ARIBH);
ARIBDO.Compose(ARIBD);
ARIBC.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))");
using (HuView ARIVPT = Connection.GetView("AR0048"))
{
ARIBC.RecordCreate(1);
ARIBC["PROCESSCMD"] = "1"; // Process Command
ARIBC.Process();
ARIBC.Read();
foreach (IGrouping<string, InvoiceLine> invoiceLine in lines.GroupBy(line => line.Invoice))
{
ARIBH.RecordCreate(2);
ARIBD.Cancel();
ARIBH["IDCUST"] = invoiceLine.First().BID; // Customer Number
ARIBH["PROCESSCMD"] = "4"; // Process Command
ARIBH["INVCTYPE"] = "2"; // Invoice Type
ARIBH.Process();
ARIBH["IDINVC"] = invoiceLine.First().Invoice + "-" + invoiceLine.First().Store.PadLeft(4, '0'); // Document Number
ARIBH["DATEINVC"] = invoiceLine.First().InvDate;
ARIBD.RecordClear();
ARIBD.RecordCreate(0);
ARIBD["PROCESSCMD"] = "0"; // Process Command Code
ARIBD.Process();
foreach (InvoiceLine line in invoiceLine)
{
ARIBD.RecordClear();
ARIBD.RecordCreate(0);
ARIBD["PROCESSCMD"] = "0"; // Process Command Code
ARIBD.Process();
ARIBD["IDDIST"] = GetDistributionCode(); // Distribution Code
ARIBD["AMTEXTN"] = line.grosssales - line.promototal; // Extended Amount w/ TIP
ARIBD.Insert();
}
ARIBH.Insert();
}
}
}
}
Explanation
Say we are given a collection of invoice detail lines as input.
ARIBC is the batch view. We want to create a new batch to put all the invoices in.
ARIBH is the invoice view. We want to group all of the detail lines by the invoice number and create a new invoice for each.
ARIBD is the invoice detail line view. We want a new detail line for each line in the data that we are importing.