Tuesday, 7 May 2019

Download SalesInvoice(Delivery Note) using Code X++

//Single Report only we can Print for mutiple Report Code will not Execute),
//Find Bottom Code for multiple Reports.
static void SOI_SaveReportToPDFFromController(Args _args)
{

    SalesInvoiceController  salesInvoiceController;
    SalesInvoiceContract    salesInvoiceContract;
    Args                    args = new Args();
    SrsReportRunImpl        srsReportRun;
    CustInvoiceJour         custInvoiceJour;
    ReportName              reportName = "SalesInvoice.ReportMPL_PL";
    ;

    select firstOnly custInvoiceJour  where custInvoiceJour.InvoiceId == "PL/SI/17/013762";

    args.record(custInvoiceJour);

    salesInvoiceController = new SalesInvoiceController();

    salesInvoiceController.parmReportName(reportName);

    salesInvoiceContract = salesInvoiceController.parmReportContract().parmRdpContract();

    salesInvoiceContract.parmRecordId(custInvoiceJour.RecId); // Record id must be passed otherwise the report will be empty

    salesInvoiceContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo()); // comment this code if tested in pre release

    salesInvoiceController.parmArgs(args);

    srsReportRun = salesInvoiceController.parmReportRun() as SrsReportRunImpl;

    salesInvoiceController.parmReportRun(srsReportRun);

    salesInvoiceController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);

    salesInvoiceController.parmReportContract().parmPrintSettings().overwriteFile(true);

    salesInvoiceController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);

    salesInvoiceController.parmReportContract().parmPrintSettings().fileName("c:\\SR_SalesInvoice.pdf");
    salesInvoiceController.parmReportContract().parmReportExecutionInfo(new SrsPrintMgmtExecutionInfo()); //If getting Error like "Parameter executionInfo cannot be null or empty"
    salesInvoiceController.runReport(); 

}

//Multiple Reports Print
static void TestSOPrint(Args _args)
{
    CustInvoiceJour     custInvoiceJour;
    smmParametersTable  parametersTable = smmParametersTable::find();
    str ext;
    str filepath;
    PrintMgmtReportFormatName printMgmtReportFormatName;
    SalesInvoiceContract    SalesInvoiceContract;
    SrsReportRunController  srsReportRunController;
    SRSPrintDestinationSettings printerSettings;
    SalesTable              salestable;

    while select SalesId, SHP_ConsignmentNumber from salestable
            where salestable.SHP_ConsignmentNumber ==  'PL-000023'
        join InvoiceId from custInvoiceJour
            where custInvoiceJour.SalesId == salestable.SalesId

    {
        if (custInvoiceJour)
        {
           
            ext = parametersTable.SHP_InboundFilePath + "\\" + strRem(custInvoiceJour.InvoiceId, '/') + "_" + salestable.SHP_ConsignmentNumber + '.pdf';
            //ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
            printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat();
            info(strFmt(custInvoiceJour.InvoiceId));
            salesInvoiceContract = new SalesInvoiceContract();
            salesInvoiceContract.parmRecordId(custInvoiceJour.RecId);

            srsReportRunController = new SrsReportRunController();
            srsReportRunController.parmReportName(printMgmtReportFormatName);
            srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            srsReportRunController.parmShowDialog(false);
            srsReportRunController.parmReportContract().parmRdpContract(salesInvoiceContract);

            printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
            printerSettings.printMediumType(SRSPrintMediumType::File);
            printerSettings.fileFormat(SRSReportFileFormat::PDF);
            //printerSettings.parmFileName(custInvoiceJour.InvoiceId + filepath);
            printerSettings.parmFileName(ext);
            printerSettings.overwriteFile(true);

            srsReportRunController.runReport();
        }
    }

}

No comments:

Post a Comment