Schedule a report to PDF  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Schedule a report to PDF

Postby DannyC » Fri Jun 22, 2018 1:04 pm

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.
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Schedule a report to PDF  Topic is solved

Postby Mike.Sheen » Fri Jun 22, 2018 3:55 pm

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);
   }
}
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Schedule a report to PDF

Postby DannyC » Mon Jun 25, 2018 3:37 pm

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);
         }
      }
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Schedule a report to PDF

Postby Mike.Sheen » Mon Jun 25, 2018 7:31 pm

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
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Schedule a report to PDF

Postby DannyC » Tue Jun 26, 2018 10:14 am

That's awesome.
Got it working!
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 20 guests

cron