View source | Discuss this page | Page history | Printable version   
Toolbox
Main Page
Upload file
What links here
Recent changes
Help

PDF Books
Add page
Show collection (0 pages)
Collections help

Search

Openbravo POS Scripting Tutorial

Contents

Introduction

This guide refers to the Openbravo POS 2.10 version and next versions. In this version Openbravo POS has extended the scripting capabilities available in previous versions to give developers more power to create rules and to adapt Openbravo POS to their business rules and country regulations.

The scripting capabilities this release offers has the following benefits:


Execution of scripts

There are two different moments where script code is executed: Buttons of the sales panel and events of the sales panel. All the execution moments are defined in the resource Ticket.Buttons.


Buttons

Scripting buttons appear the sales panel top right and can execute a template or a script when the user press the button.

Openbravo POS script buttons.

A button is defined as a new node of type button created in the resource Ticket.Buttons. This is an example of two buttons:

<button key="button.print" name="button.print" template="Printer.TicketPreview"/>
<button key="button.discount" name="button.discount" code="Script.Discount"/>

The attributes of a button are:


Events

Events are scripts that are executed when an action is executed in the sales panel.

An event is defined as a new node of type button created in the resource Ticket.Buttons. This is an example of one event executed when a new receipt line is added to the receipt:

<event key="ticket.addline" code="event.addline"/>

The event attributes are:

The following events are published:

The object model

There are five objects passed to the scripts: ticket that contains the data of the current ticket, taxes that contains the list of available taxes record, place that in restaurant mode contains the table object, in other modes contains null, user that contains the user object and sales that contains utility methods of the sales panel.


Utility methods

The sales object available in scripts button and events contains a list of utility methods of the sales panel.

Ticketline Info

There are number of function calls available for working with a single ticketline:

this list isn't complete and all public functions can be found in:

src-pos/com/openbravo/pos/ticket/TicketLineInfo.java

Attributes

Receipts, receipt lines and products have a list of properties called attributes. These attributes can be used for any purpose the developer needs. When adding a new receipt line Openbravo POS copies the attributes of the product to the line, Openbravo POS does not manage in any other way the content and values of these attributes, and is the developer of script buttons and events the responsible of managing its values.

These attributes are stored in the database and can be edited in the products panel as an XML text file. This is an example of a product attributes record:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>Asian salad attributes</comment>
    <entry key="notes">Only available in spring.</entry>
    <entry key="ingredients">Lettuce, and other vegetables</entry>
</properties>

These properties can be accessed when printing a receipt (eg in the resource Printer.Ticket) by using the expression:

 $ticketline.getProperty("ingredients", "")

where $ticketline is derived from the Velocity expression:

 #foreach ($ticketline in $ticket.getLines())
 ...
 #end

The second parameter for getProperty() is optional and is a default value to be returned if the <entry> does not exist.

Examples

This is a list of functionality that can be implemented with scripts.


Add notes to receipt lines

With this functionality you create a button that when selected a line in the receipt it allows to edit notes associated to this line. The script code of the button is:

index = sales.getSelectedIndex(); 
if (index >= 0) {   
    line = ticket.getLine(index);   
    value = javax.swing.JOptionPane.showInputDialog("Line notes", line.getProperty("notes")); 
    if (value != null) { 
        // the user pressed OK 
        line.setProperty("notes", value); 
    } 
}

Add a discount to the selected line

The following code when associated to a button you will be able to add a discount to the selected line. The discount amount is calculate as the percentage typed in the on screen keyboard.

// % discount line

import com.openbravo.format.Formats;
import com.openbravo.pos.ticket.TicketLineInfo;
import com.openbravo.pos.ticket.TicketProductInfo;

discountrate = sales.getInputValue() / 100.0;  

index = sales.getSelectedIndex();
if (index >= 0) {
    line = ticket.getLine(index);
    if (line.getPrice() > 0.0 && discountrate > 0.0) { 
 
        sdiscount = Formats.PERCENT.formatValue(discountrate);  
        ticket.insertLine(index + 1,
            new TicketLineInfo(
                    "Discount " + sdiscount,
                    line.getProductTaxCategoryID(),
                    line.getMultiply(), 
                    -line.getPrice () * discountrate,
                    line.getTaxInfo()));  
        sales.setSelectedIndex(index + 1);
    } else {  
         java.awt.Toolkit.getDefaultToolkit().beep();  
    }
} else {
    java.awt.Toolkit.getDefaultToolkit().beep();  
}

Add a discount to the total of the receipt

Important: This script only works release 2.30 and higher. It does not work in earlier versions (1.0 - 2.20).

The following code when associated to a button adds a 15% discount based on the total of the receipt. If the receipt includes items with different taxes it will add one discount line for each group of taxes. The discount amount is calculate as the percentage typed in the on screen keyboard.

// % Discount for the total of the receipt  

import com.openbravo.format.Formats;
import com.openbravo.pos.ticket.TicketLineInfo;
import com.openbravo.pos.ticket.TicketProductInfo; 
import java.util.Properties;

discountrate = 0.15;  

total = ticket.getTotal();  
if (total > 0.0) {  
    sdiscount = Formats.PERCENT.formatValue(discountrate);  
 
    taxes = ticket.getTaxLines();  
    for (int i = 0; i < taxes.length; i++) {  
        taxline = taxes[i];  
        ticket.insertLine(ticket.getLinesCount(),
            new TicketLineInfo(
                "Discount " + sdiscount + " of " + taxline.printSubTotal(),   
                taxline.getTaxInfo().getTaxCategoryID(),          
                1.0, 
                -taxline.getSubTotal() * discountrate,
                taxline.getTaxInfo()));  
    }  
    sales.setSelectedIndex(ticket.getLinesCount() - 1);
} else {  
    java.awt.Toolkit.getDefaultToolkit().beep();  
}

NOTE: Once the button is created and the script is associated permission to use the button must be granted. See User Roles for details.

Display prompt with change value of the sale

The following code is associated to a ticket.close event and will show the change value of the sale.

import com.openbravo.pos.payment.PaymentInfo;
import javax.swing.JOptionPane;

boolean isCash = false;
String change = "";
PaymentInfo p = ticket.payments.getFirst();

if ("cash".equals(p.getName())) {
    isCash = true;
    change = p.printChange();
}
if(isCash) {
    JOptionPane.showMessageDialog(null, "Return " + change, "Change", JOptionPane.PLAIN_MESSAGE);
}

To implement the show change ticket.close script do the following:

Step 1) Set Up the resource
Step 2) Set the event to happen

<event key="ticket.close" code="ticket.close"/>

Step 3) restart OpenbravoPOS to make the changes permanant

Success: you now see a pop up with the amount of change to be given.

Create a report to print an invoice at the end of a sale

If you need to create invoices for your customers and the receipt printer is not the solution to print the invoice you can create a complex report using the receipt data to fill in the report and print it to a regular printer. This example explains how to create a basic report using the receipt data using iReport and print it at end of every sale.

First you need to execute the script that prints the report at the end of every sale. Edit the resource Ticket.Buttons and add the following line:

<event key="ticket.close" code="code.printreport"/> 

This line creates an event that executes the script code.printreport at the end of every sale.

Then you have to create the script that prints the report. To do this create a new report with the name code.printreport and add the following line:

sales.printReport("/com/openbravo/reports/ticketsample");

This line of code executed the report ticketsample.jrxml that is in the reports folder.

Then in the folder reports/com/openbravo/reports/ of the installation of Openbravo POS create the following files:

The file ticketsample.jrxml is the report that prints the general data of the receipt and ticket_sample_lines.jrxml is the subreport that prints the data of the receipt lines. You can modify these files using the graphical tool iReport and adapt the layout of the report to your business requirements.

Add a logo image to reports/com/openbravo/reports/logo.png

To finish, in Openbravo POS, in the configuration panel select the reports printer you want to use to print the invoices report. Restart Openbravo POS and test the new report performing a sale.

This is a sample invoice using the sample report files provided.

Invoice sample.

Retrieved from "http://wiki.openbravo.com/wiki/Openbravo_POS_Scripting_Tutorial"

This page has been accessed 140,459 times. This page was last modified on 17 September 2012, at 15:06. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.