Page 1 of 1

Schedule a report to PDF

PostPosted: Fri Jun 22, 2018 1:04 pm
by DannyC
Hi,
I want to write a plugin (v 157, 175 and 7.1 - hopefully the code wont be different for each version)

It will, nightly, export a report to PDF. This might be say, the BackOrders and Outstanding Orders report, or perhaps the Profit & Loss report.
For this example I want to make it easy and not have any parameters to deal with so I'm happy to start my development with the Inventory Reconciliation report.

I assume the code needs to go in here
Code: Select all
#region "ScheduledExecutionPlugin"
public class ScheduledExecutionPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaScheduledExecutionPlugin
{
    public void Execute(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, JiwaFinancials.Jiwa.JiwaApplication.Schedule.Schedule Schedule)
    {  /*  code goes in here.
        JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport
      
      */   
    }

    public void OnServiceStart(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
    {
    }

    public void OnServiceStopping(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
    {
    }
}
#endregion

but I can't work out how to specify which report to print. And how I get it to export to PDF.
And later on I want to work out how to specify parameter values.

If I could just have an example of one report I should be able to work out other reports.

Re: Schedule a report to PDF  Topic is solved

PostPosted: Fri Jun 22, 2018 3:55 pm
by Mike.Sheen
Hi Danny,

If you look at the REST API plugin, we have a service there to download a PDF generated from a report - it shows how to use our JiwaPrintReport class to generate a report (with parameters) and save to PDF - you can just pick out the relevant bits:

Code: Select all
public class ReportsServices : Service
{
   [Authenticate]
   public ServiceStack.Web.IHttpResult Get(ReportsPDFGETRequest request)
   {
      JiwaApplication.Manager manager = this.SessionAs<JiwaAuthUserSession>().Manager;

      JiwaApplication.Report.Configuration.ReportCollection m_Reports = manager.BusinessLogicFactory.CreateBusinessLogic<JiwaApplication.Report.Configuration.ReportCollection>(null);
      m_Reports.Read();

      // find report with request.ReportID
      JiwaApplication.Report.Configuration.Report report = m_Reports[request.ReportID];

      if (report == null)
         throw new JiwaApplication.Exceptions.RecordNotFoundException("Report not found");

      JiwaApplication.JiwaPrintReport.JiwaPrintReport printReportObject = manager.BusinessLogicFactory.CreateBusinessLogic<JiwaApplication.JiwaPrintReport.JiwaPrintReport>(null);
      printReportObject.Setup();

      printReportObject.LoadReport(report);

      if (request.ReportParameters != null)
      {
         foreach (ReportParameter reportParameter in request.ReportParameters)
         {
            foreach (JiwaApplication.JiwaPrintReport.JiwaReportRange jiwaReportRange in printReportObject.JiwaReportRangeCollection)
            {
               if (printReportObject.JiwaPrintFormulaCollection[jiwaReportRange.FormulaKey].Name.Replace("{", "").Replace("}", "").Replace("@", "").Trim().ToUpper() == reportParameter.Name.Replace("{", "").Replace("}", "").Replace("@", "").Trim().ToUpper())
               {
                  jiwaReportRange.Value = reportParameter.Value;
                  break;
               }
            }
         }
      }

      printReportObject.UpdateReport();

      // export report to PDF
      string tempFolderPath = System.IO.Path.GetTempPath();
      string fileName = report.Title;

      // Make filename safe
      foreach (char c in System.IO.Path.GetInvalidFileNameChars())
      {
         fileName = fileName.Replace(c.ToString(), "");
      }

      string fullFileName = System.IO.Path.Combine(tempFolderPath, fileName) + ".pdf";
      printReportObject.CrystalReportObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, fullFileName);

      // Return the file
      System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullFileName);
      return new HttpResult(fileInfo, asAttachment: request.AsAttachment);
   }
}

Re: Schedule a report to PDF

PostPosted: Mon Jun 25, 2018 3:37 pm
by DannyC
That helps Mike.
I'm writing this for 7.0.175 so have adjusted the code a bit where relevant.
I am just getting stuck on the line
Code: Select all
JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport printReportObject = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport>(null);

Compiler error says The type JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport cannot be used as a type paramter 'BusinesLogicType' in the generic type or method JiwaFinancials.Jiwa.JiwaAPplication.BusinessLogicFactory.CreateBusinessLogic

The full routine is
Code: Select all
   public void DannyPrintTest
   {
      JiwaFinancials.Jiwa.JiwaApplication.Report.Configuration.ReportCollection m_Reports = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaApplication.Report.Configuration.ReportCollection>(null);
         m_Reports.Read();
      foreach (JiwaFinancials.Jiwa.JiwaApplication.Report.Configuration.Report JiwaReport in m_Reports)
      {   
         //MessageBox.Show(JiwaReport.FileName);
         if (JiwaReport.FileName == "ININF055 - Inventory Reconciliation.rpt")
         {
            JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport printReportObject = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport>(null);
            printReportObject.Setup();
            printReportObject.LoadReport(JiwaReport);
            printReportObject.UpdateReport();
            //string MyFolderPath = "D:\Danny\";
            string FileName = "D:\\Danny\\" + JiwaReport.FileName + "_" + System.DateTime.Today.ToString("d MMM yyyy") + ".pdf";
            printReportObject.CrystalReportObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, FileName);
         }
      }

Re: Schedule a report to PDF

PostPosted: Mon Jun 25, 2018 7:31 pm
by Mike.Sheen
Hi Danny,

Ah - in 07.01 we smacked JiwaApplication.JiwaPrintReport.JiwaPrintReport around a bit and made it be a proper business logic object, and changed all code instantiating it to use our Business Logic factory.

What this means is in versions prior to 07.01 (such as 07.00.175) instead of this:

Code: Select all
JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport printReportObject = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport>(null);


You just do this instead:

Code: Select all
JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport printReportObject = new JiwaFinancials.Jiwa.JiwaApplication.JiwaPrintReport.JiwaPrintReport();
printReportObject.Setup();


You might also want to put a break; in your if condition of the for loop - no point in continuing to iterate the report collection once you've found the report you were looking for and have generated it!

Mike

Re: Schedule a report to PDF

PostPosted: Tue Jun 26, 2018 10:14 am
by DannyC
That's awesome.
Got it working!