Search code examples
axaptadynamics-ax-2009invoice

AX 2009: Need to print ProForma Invoice before the invoice has been posted


So my company has tasked me with creating a batch job that will print out Proforma sales invoices for all of the sales orders that have had the packing slip posted that day, but have not had the invoices posted yet. They are asking for this because they wish to review the invoices through PDF files before actually posting them. Most likely so that they can post in all of the approved invoices in one process.

So far all of the examples of how to create this batch report run (that I can find through Google searches) uses the custInvoiceJour entries for the sales order in order to generate the SalesInvoice report, however, since the invoices have not actually been posted yet, there is no corresponding CustInvoiceJour entry to use in this case

How would I go about creating the SalesInvoice, without posting (ProForma), since these journal entries have not been created? I know that this can be done, since you can do it through the UI.

Here is the code that I have so far for the run method of the batch job, and when it runs it errors out about not being able to find a custInvoiceJour entry.

public void run()
{
// Arguments for function.
   Args args;
   ParmId parmId;
   ReportRun reportRun;
   SalesTable salesTable;
   PrintJobSettings printJobSettings;
   SalesFormLetter formLetter;

   select salesTable where salesTable.DocumentStatus == DocumentStatus::PackingSlip && salesTable.SalesStatus == SalesStatus::Delivered;

   printJobSettings = new PrintJobSettings();
   printJobSettings.setTarget(PrintMedium::File);
   printJobSettings.format(PrintFormat::PDF);
   printJobSettings.fileName("C:\temp\proforma_invoice.pdf");

   formLetter = SalesFormLetter::construct(DocumentStatus::Invoice);

   formLetter.proforma(true);
   formLetter.printFormLetter(true);
   formLetter.updatePrinterSettingsFormLetter(printJobSettings.packPrintJobSettings());

   args = new Args(ReportStr(SalesInvoice));
   args.caller(formLetter);
   args.parmEnum(PrintCopyOriginal::Original);
   args.parmEnumType(enumnum(PrintCopyOriginal));
   args.record(salesTable);

   reportRun = new ReportRun(args);
   reportRun.printJobSettings().setTarget(PrintMedium::File);
   reportRun.printJobSettings().format(PrintFormat::PDF);
   reportRun.printJobSettings().fileName("C:\temp\proforma_invoice.pdf");
   reportRun.prompt();
   reportRun.run();

}


Solution

  • You can use the code below to save the proforma to file

    public void run()
    {
        SalesTable          salesTable;
        PrintJobSettings    printJobSettings;
        SalesFormLetter     salesFormLetter;
        ;
    
        select firstOnly salesTable
            where salesTable.DocumentStatus == DocumentStatus::PackingSlip
               && salesTable.SalesStatus    == SalesStatus::Delivered;
    
        printJobSettings = new PrintJobSettings();
        printJobSettings.setTarget(PrintMedium::File);
        printJobSettings.format(PrintFormat::PDF);
        printJobSettings.fileName(@'C:\Temp\proforma_invoice.pdf');
    
        salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice);
        salesFormLetter.updatePrinterSettingsFormLetter(printJobSettings.packPrintJobSettings());
        salesFormLetter.update(salesTable, 
                               systemDateGet(), 
                               SalesUpdate::PackingSlip, 
                               AccountOrder::None, 
                               NoYes::Yes, 
                               NoYes::Yes);
    }