I have been given task to create an API (unbound code unit) that will create and post an invoice. I have a bit of experience NAV over the years but not much code development and am new to AL.
For the most part it works, however I assumed it would populate a lot of the posting and tax fields and calculate totals based on customer passed in.
I noticed there are a lot of functions such as CalcLineAmount and CalcFields that can help with it but I am looking for an existing more complete example - have searched for AL and tried to find older C/AL examples since they seem relevant.
What would be really great is to find the source for the Application Test Toolkit , it seems to have a lot of routines in LibrarySales to do this such as CreateSalesDocument and CreateSalesLine etc.
Below is a snippet of the main section to create the invoice - a customer is retrieved based on a Customer Business Relation linked to a particular contact and an invoice is built:
Cust.RESET();
Cust.SETRANGE("No.", CBR."No.");
Cust.FindFirst();
SalesHeader.Init();
SalesHeader."Sell-to Customer No." := CBR."No.";
SalesHeader."Bill-to Customer No." := CBR."No.";
SalesHeader."Document Date" := Today();
SalesHeader."Due Date" := Today();
SalesHeader."Document Type" := SalesHeader."Document Type"::Invoice;
SalesHeader."External Document No." := referenceNo;
//
SalesHeader."Gen. Bus. Posting Group" := Cust."Gen. Bus. Posting Group";
SalesHeader."Customer Posting Group" := Cust."Customer Posting Group";
SalesHeader.Insert(True);
SalesLine.Init();
SalesLine."Document Type" := SalesLine."Document Type"::Invoice;
SalesLine."Document No." := SalesHeader."No.";
//set GL account
SalesLine.Type := SalesLine.Type::"G/L Account";
SalesLine."No." := '1001';
SalesLine."Line No." := 10000;
SalesLine."Unit of Measure Code" := 'PCS';
SalesLine.Quantity := 1;
SalesLine."Qty. to Ship" := SalesLine.Quantity;
SalesLine."Qty. to Invoice" := SalesLine.Quantity;
SalesLine."Qty. Shipped Not Invoiced" := 0;
SalesLine."Unit Price" := amount;
//
SalesLine."Gen. Bus. Posting Group" := Cust."Gen. Bus. Posting Group";
SalesLine."Gen. Prod. Posting Group" := 'SERVICES'; //Cust."Customer Posting Group"; //'SERVICES';
SalesLine."VAT Bus. Posting Group" := Cust."VAT Bus. Posting Group";
SalesLine."VAT Prod. Posting Group" := 'GST10';
SalesLine."Line Amount" := SalesLine.CalcLineAmount();
SalesLine.Insert(True);
SalesHeader.CalcFields(Amount, "Amount Including VAT");
Codeunit.Run(Codeunit::"Sales-Post", SalesHeader);