Projects:PDF Printing Improvements/Print in Backend
Contents |
Introduction
Web POS is capable to print receipts using the Receipt and Customer display document schema. But this schema is oriented to receipt printers and has limited layout capabilities. In the case it is required to print documents with a richer layout, the Jasper Reports engine can be used to print documents through the Hardware Manager. This capability is available only for printers using the Printer mode. See Hardware and Peripherals Installation.
This document will explain step by step how to create a new module that includes a Jasper Reports document to print invoices from Web POS. This module has been published in mode Test and can be installed from the Module Management window. If you want to inspect the source code or modify it for your own project, the source code is available from the Retail Sales Receipt Report project page.
It possible to use the same template the backend uses. This way a document with a richer layout is used with no extra development.
Is it also possible to define a receipt template that depending for example on a receipt property to print a receipt report or a regular receipt template. This functionality is explained in the Javascript template engine for receipt printers and customer displays document.
Create a new module
Before creating the report definition you need to create a new module. This module will contain the report declaration and the report files. It is required that you know about the Openbravo modularity concepts. You can learn about Openbravo Modularity in the document How_To_Create_and_Package_a_Module.
As System Administrator go to the window Module and create a new record. Fill in all the required fields and save. Go to the the tab Dependency and add dependencies to the module Web POS and Web POS Hardware Manager. This the definition of the module example.
Create a new jasper report
This next section includes the specific steps to declare and design the new template.
Declare the invoice template
The next step is to declare the invoice template. In this example we are going to create a new report for invoices created from the Web POS.
As System Administrator go to the window POS Print Templates and create a new record. The fields to fill in are the following:
- Name: The name that identifies this template.
- Template Type: Defines the type of the template. The template types that can be used to print a report are: Sales Report, Return, Invoice, Return Invoice, Layaway, Print Ticket, Closed Receipt, and Cashup Report. In our example: Invoice.
- Print Type: We are going to declare a report, to use this kind of printing Report Printing should be selected.
- Template Path: The relative path to the main Jasper Reports file definition. This is a relative path from within the web/org.openbravo.retail.posterminal module. Therefore the example in the screenshot below uses the .. to go one folder up.
- Printer. The number of the configured printer in the Hardware Manager we want to use to print the report.
If the report has subreports, that in general it will, you have to go to the tab Subreports and create as many records as subreports has. The fields to fill in are:
- Name. The name that identifies the subreport. This is the value that will be used in the main report file to reference the subreport.
- Template Path: The relative path to the subreport file. This is a relative path from within the web/org.openbravo.retail.posterminal module. Therefore the example in the screenshot below uses the .. to go one folder up.
Design the invoice
The tool used to design the reports is iReport Designer. To know how to use this tool you can go to the Jaspersoft documentation
The parameters received by the report are:
- REPORT_IMG_FOLDER: The folder that contains the Hardware Manager images. This parameter is useful to include images in the report.
- SUBREPORTS_FOLDER: The folder where subreports are located. When added a subreport for the receipt lines or taxes lines you have to define a new subreport witn the expresion $P{SUBREPORTS_FOLDER} + "Subreport_Name.jasper", where Subreport_Name is the name defined in the tab Subreports in the previous section.
Fields of the report depends on the type of the report and are translated directly from the JSON that contains the document data. In this example the following fields of the invoice are used:
- DOCUMENTNO: The document number.
- ORDERDATE: The order date.
- ORDERTYPE: The order type.
- CURRENCY_IDENTIFIER. The identifier of the order currency.
- GROSS: The total gross of the invoice.
- NET: The total net of the invoice.
- SALESREPRESENTATIVE_IDENTIFIER: The identifier of the sales representative.
- BP._IDENTIFIER: The identifier of the customer.
- BP.LOCNAME: The customer address.
- BP.CITYNAME: The city name of the customer.
- BP.COUNTRYNAME: The country name of the customer.
- BP.PHONE: The phone number of the customer.
- BP.TAXID: The tax ID of the customer.
Invoice lines data is received as a report parameter:
- SUBREP_LINES
And the fields used in each line are:
- LINES.PRODUCT.SEARCHKEY: The search key of the product.
- LINES.PRODUCT._IDENTIFIER: The identifier of the product.
- LINES.QTY: The products quantity.
- LINES.UNITPRICE: The net price per unit.
- LINES.GROSSUNITPRICE: The gross price per unit.
- LINES.NET: The total net of the line.
- LINES.GROSS: The total gross of the line.
Tax lines data is received as a report parameter too:
- SUBREP_TAXES
And the fields used in each tax line are:
- TAXES.NAME. The tax name.
- TAXES.NET. The net used to calculate the tax amount.
- TAXES.AMOUNT. The tax amount.
Place the report file in the correct location in the module
The report file should be placed inside the module in the web directory of the module and within it again in a directory which has the same name as the module. The OB convention is to use then a subfolder called res.
See here how this is placed within the org.openbravo.retail.posterminal module:
Use the backend report
This next section includes the specific steps to declare a report to be generated in the backend.
Declare the invoice template
The next step is to declare the invoice template. In this example we are going to create a new report for invoices created from the Web POS.
As System Administrator go to the window POS Print Templates and create a new record. The fields to fill in are the following:
- Name: The name that identifies this template.
- Template Type: Defines the type of the template. The template types that can be used to print a report are: Sales Report, Return, Invoice, Return Invoice, Layaway, Print Ticket, Closed Receipt, and Cashup Report. In our example: Invoice.
- Print Type: We are going to declare a template to be printed on the backend, to acomplish this Print in Backend should be selected.
- Printer. The number of the configured printer in the Hardware Manager we want to use to print the report.
Install and configure the printer
The only requirement to print reports is to configure the printer in the Hardware Manager in printer mode. This mode is printers installed as a printer in the operating system using the system driver provided by the manufacturer of the printer. In the document Hardware and Peripherals Installation it is explained how to configure a printer.
If instead of printing the report you want to generate a PDF file you can configure a Virtual PDF printer. There are several options depending on the operating system you use.
The following example configures in the Hardware Manager the printer named My_Printer" as printer number 1:
## Receipt printers ## machine.printer = printer\:My_Printer,standard
Configure the store
To configure your store to use the report created, as Client Administrator go to the Organization window, and in the Web POS section select the template to use. The following fields can select a template: Print Ticket Template, Cash Up Report Template, Closed Receipt Template, Invoice Template, Return Template, Return Invoice Template, Layaway Template, Sales Report Template and Print Ticket Template for Training Mode. In our case Invoice Template.
Print the invoice
Now all the work is done. To test everything works open a Web POS terminal as create a new invoice. Invoices are created selecting in the menu Invoice this receipt. Also you can only create a receipt for customers that have a valid Tax ID.
And this is how the invoice created looks like in a virtual PDF printer.
Javac and Groovy Class Not Found Exception
![]() | Note: to have Web POS use jasper reports through the hardware manager you need to make sure the hardware manager can run the 'javac' program. Javac is delivered as part of the java JDK distributions. |
If you get this exception when printing a ticket:
[java] java.lang.ClassNotFoundException: org.codehaus.groovy.control.CompilationFailedException [java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366) [java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355) [java] at java.security.AccessController.doPrivileged(Native Method) [java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354) [java] at java.lang.ClassLoader.loadClass(ClassLoader.java:425) [java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) [java] at java.lang.ClassLoader.loadClass(ClassLoader.java:358) [java] at java.lang.Class.forName0(Native Method) [java] at java.lang.Class.forName(Class.java:270) [java] at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157) [java] at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115) [java] at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:511) [java] at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215) [java] at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:201) [java] at com.openbravo.poshw.TicketPdfServlet.doProcess(TicketPdfServlet.java:165) [java] at com.openbravo.poshw.TicketPdfServlet.doPost(TicketPdfServlet.java:87)
Then you need to set the correct language in the jasper template. Open the template and right click on the root node in the report tree and then set Java as the language.