POS - Call methods of your owns objects from a scriptlet
Code snippetName: POS - Call methods of your owns objects from a scriptlet
|
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:
this.put("scriptletutil", new ScriptletUtil());
- Then, you must recompile all sources, and try this function.
How to use
In the scriptlet you need, put something like this:
<line><text align="left">My Special datas: $scriptletutil.mySpecialMethod($scriptletObject)</text></line>
where:
- $scriptletutil is your scriptlet util object.
- mySpecialMethod is your method of ScriptletUtil you have created.
- $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.
The call to the method from the scriptlet is easy:
<line><text align="left">Ganancias: $scriptletutil.printGananciasCaja($payments)</text></line>
- ScriptletUtil class:
package com.openbravo.util.pos; import com.openbravo.data.loader.BaseSentence; import com.openbravo.data.loader.DataResultSet; import com.openbravo.data.loader.SerializerReadInteger; import com.openbravo.data.loader.Session; import com.openbravo.data.loader.StaticSentence; import com.openbravo.pos.forms.AppConfig; import com.openbravo.pos.panels.PaymentsModel; import com.openbravo.pos.util.AltEncrypter; import java.util.Date; import java.sql.SQLException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Andy Orencio (lodopidolo) http://mocosete.es */ public class ScriptletUtil { private Session session; public String printGananciasCaja(Date datestart, Date dateend) { SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String sentSql = "select avg(ve.to) as media_total_ticket, " + " avg(ve.pb) as media_total_precio_compra_producto, " + " avg(ve.ps) as media_total_precio_venta_producto, " + " avg(case ve.pb when null then null when 0 then null else ((ve.to / ve.pb) * 100) end) as media_ganancia_del_ticket " + "from ( " + " select ti.id, ti.ticketid, " + " (sum(tl.price * tl.units) * 1.16) as to, " + " (sum(pr.pricebuy * tl.units) * 1.20) as pb, " + " (sum(pricesell * tl.units) * 1.16) as ps " + " from tickets ti " + " inner join ticketlines tl on tl.ticket = ti.id " + " left outer join products pr on tl.product = pr.id " + " group by ti.id, ti.ticketid " + " ) ve " + "inner join receipts re on re.id = ve.id " + "where ve.to > 0 and " + " re.datenew between to_timestamp('" + df.format(datestart) + "', 'YYYYMMDDHH24MISSMS') and to_timestamp('" + df.format(dateend) + "', 'YYYYMMDDHH24MISSMS')"; BaseSentence sent = new StaticSentence(getSession(), sentSql, null, SerializerReadInteger.INSTANCE); DataResultSet drs; String res = ""; try { drs = sent.openExec(null); if (drs.next()) res = (new DecimalFormat("#,##0.00")).format(drs.getDouble(4)) + " %, " + (new DecimalFormat("#,##0.00")).format(drs.getDouble(1) - drs.getDouble(2)) + " €"; drs.close(); } catch (Exception ex) { Logger.getLogger(ScriptletUtil.class.getName()).log(Level.SEVERE, null, ex); } return res; } public String printGanaciasCaja(PaymentsModel p) { return p == null ? "" : printGanaciasCaja(p.getDateStart(), p.getDateEnd()); } public Session getSession() { if (this.session == null) { AppConfig config = new AppConfig(new String[] { }); config.load(); String sDBURL = config.getProperty("db.URL"); String sDBUser = config.getProperty("db.user"); String sDBPassword = config.getProperty("db.password"); if (sDBUser != null && sDBPassword != null && sDBPassword.startsWith("crypt:")) { // the password is encrypted AltEncrypter cypher = new AltEncrypter("cypherkey" + sDBUser); sDBPassword = cypher.decrypt(sDBPassword.substring(6)); } try { this.session = new Session(sDBURL, sDBUser, sDBPassword); } catch (SQLException ex) { Logger.getLogger(ScriptletUtil.class.getName()).log(Level.SEVERE, null, ex); } } return this.session; } }
- Printer.CloseCash scriptlet (Maintenance -> Resources). This script print the closed cash results:
<?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> <line size="1"> <text align ="center" length="40" bold="true">Informe cierre caja</text> </line> <line></line> <line> <text bold="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> <line size="1"> <text align ="left" length="30" bold="true">Total</text> <text align ="right" length="10" bold="true">${payments.printPaymentsTotal()}</text> </line> <line></line> <line> <text bold="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> <line size="1"> <text align ="left" length="30" bold="true">Subtotal</text> <text align ="right" length="10" bold="true">${payments.printSalesBase()}</text> </line> <line size="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><text align="left">Ganancias: $scriptletutil.printGananciasCaja($payments)</text></line> <line></line> <line> <text length="16">TPV:</text> <text>${payments.printHost()}</text> </line> <line> <text length="16">Seceuncia:</text> <text length="24" align="right">${payments.printSequence()}</text> </line> <line> <text length="16">Fecha inicio:</text> <text length="24" align="right">${payments.printDateStart()}</text> </line> <line> <text length="16">Fecha fin:</text> <text length="24" align="right">${payments.printDateEnd()}</text> </line> <line></line><line></line> </ticket> </output>
References
- Openbravo POS Scripting Tutorial. This tutorial explains what is a scriptlet, and how to manages them.