POS - Call methods of your owns objects from a scriptlet

Contents

Code snippet

Sometimes you need call methods of objects that have not been published in the call to scriptlet. So if you are modifying a printer ticket resource, you can get ticket and ticketlines objects, but you can't access to a product object.

With these modifications, you can make a class and publish it to all scriptlets like an utility class. This is the main idea.

So, somewhere in the sources, you'll create a package containing the classes to be published in the scriptlets.

After that you must only indicate to scripting engine that must publish that object (instantiated) to all new scripts created.

Implementation

Firstly, make a class for you utilities purposes. With idea of to be separated openbravo native source from your personal source, make a directory in your openbravo_pos proyect called src-util, and there make a package called com.openbravo.util.pos.

This is going to be your util package. So you must make in that package a non static class called ScriptletUtil.

You can make all methods you need, taking in consideration that calls made from a scriptlet can have at most one parameter, and this parameter can be an object of the scriptlet.

Edit com.openbravo.pos.scripting.ScriptEngineVelocity class (of openbravo_pos sources), in src-pos directory, and in the last line of the constrctor class (around line 68), you must add this code:

$scriptletObject is a scriptlet object like tickets, ticketlines, payments, etc (depending of the scriptlet).

Example

Perhaps this is not the best example, but at least is illustrative.
I need a method to be used on Printer.CloseCash resource, that give me special information about tickets/receipts of the closed period.

Then I make printGananciasCaja(PaymentsModel p) method, that return a String object and have a PaymentsModel parameter object. This parameter would be the payments object of the Printer.CloseCash scriplet. This method make a sql statement and returns some values.

<?xml version="1.0"encoding="UTF-8"?><!--
Openbravo POS is a point of sales application designed for touch screens.
Copyright (C) 2007-2009 Openbravo, S.L.U.
http://sourceforge.net/projects/openbravopos
This file is part of Openbravo POS.
Openbravo POS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Openbravo POS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Openbravo POS. If not, see <http://www.gnu.org/licenses/>.
-->
<output><ticket><image>Printer.Ticket.Logo</image><line></line><linesize="1"><text align ="center"length="40"bold="true">Informe cierre caja</text></line><line></line><line><textbold="true">Informe de pagos</text></line><line><text align ="right"length="40">Total</text></line><line><text>----------------------------------------</text></line>
#foreach ($line in $payments.getPaymentLines())
<line><text align ="left"length="30">${line.printType()}</text><text align ="right"length="10">${line.printValue()}</text></line>
#end
<line><text>----------------------------------------</text></line><line><text align ="left"length="30">Pagos:</text><text align ="right"length="10">${payments.printPayments()}</text></line><line></line><linesize="1"><text align ="left"length="30"bold="true">Total</text><text align ="right"length="10"bold="true">${payments.printPaymentsTotal()}</text></line><line></line><line><textbold="true">Informe de impuestos</text></line><line><text align ="right"length="40">Total</text></line><line><text>----------------------------------------</text></line>
#foreach ($line in $payments.getSaleLines())
<line><text align ="left"length="30">${line.printTaxName()}</text><text align ="right"length="10">${line.printTaxes()}</text></line>
#end
<line><text>----------------------------------------</text></line><line><text align ="left"length="30">Rececibo:</text><text align ="right"length="10">${payments.printSales()}</text></line><line></line><linesize="1"><text align ="left"length="30"bold="true">Subtotal</text><text align ="right"length="10"bold="true">${payments.printSalesBase()}</text></line><linesize="1"><text align ="left"length="20"bold="true">Total</text><text align ="right"length="10"bold="true">${payments.printSalesTaxes()}</text><text align ="right"length="10"bold="true">${payments.printSalesTotal()}</text></line><line></line><line><textalign="left">Ganancias: $scriptletutil.printGananciasCaja($payments)</text></line><line></line><line><textlength="16">TPV:</text><text>${payments.printHost()}</text></line><line><textlength="16">Seceuncia:</text><textlength="24"align="right">${payments.printSequence()}</text></line><line><textlength="16">Fecha inicio:</text><textlength="24"align="right">${payments.printDateStart()}</text></line><line><textlength="16">Fecha fin:</text><textlength="24"align="right">${payments.printDateEnd()}</text></line><line></line><line></line></ticket></output>