OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.pos;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
67 ilm 17
import org.openconcerto.erp.config.MainFrame;
18
import org.openconcerto.erp.core.common.ui.TotalCalculator;
18 ilm 19
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
21 ilm 20
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
18 ilm 21
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
67 ilm 22
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
18 ilm 23
import org.openconcerto.erp.core.sales.pos.io.ESCSerialPrinter;
24
import org.openconcerto.erp.core.sales.pos.io.JPOSTicketPrinter;
25
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
26
import org.openconcerto.erp.core.sales.pos.model.Article;
27
import org.openconcerto.erp.core.sales.pos.model.Paiement;
80 ilm 28
import org.openconcerto.erp.core.sales.pos.model.ReceiptCode;
18 ilm 29
import org.openconcerto.erp.core.sales.pos.model.Ticket;
30
import org.openconcerto.erp.core.sales.pos.model.TicketLine;
83 ilm 31
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
32
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.Type;
61 ilm 33
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
18 ilm 34
import org.openconcerto.erp.generationEcritures.GenerationMvtTicketCaisse;
21 ilm 35
import org.openconcerto.erp.generationEcritures.GenerationMvtVirement;
18 ilm 36
import org.openconcerto.erp.generationEcritures.GenerationReglementVenteNG;
37
import org.openconcerto.erp.model.PrixTTC;
73 ilm 38
import org.openconcerto.erp.preferences.DefaultNXProps;
18 ilm 39
import org.openconcerto.sql.Configuration;
40
import org.openconcerto.sql.element.SQLElement;
41
import org.openconcerto.sql.model.SQLBase;
42
import org.openconcerto.sql.model.SQLRow;
83 ilm 43
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 44
import org.openconcerto.sql.model.SQLRowListRSH;
45
import org.openconcerto.sql.model.SQLRowValues;
46
import org.openconcerto.sql.model.SQLSelect;
47
import org.openconcerto.sql.model.SQLTable;
48
import org.openconcerto.sql.model.Where;
49
import org.openconcerto.sql.users.UserManager;
25 ilm 50
import org.openconcerto.sql.utils.SQLUtils;
19 ilm 51
import org.openconcerto.utils.DesktopEnvironment;
52
import org.openconcerto.utils.ExceptionHandler;
18 ilm 53
import org.openconcerto.utils.Pair;
67 ilm 54
import org.openconcerto.utils.i18n.TranslationManager;
18 ilm 55
 
56
import java.io.File;
57
import java.io.FileOutputStream;
80 ilm 58
import java.io.IOException;
67 ilm 59
import java.math.BigDecimal;
60
import java.math.MathContext;
61
import java.math.RoundingMode;
18 ilm 62
import java.sql.SQLException;
63
import java.util.ArrayList;
21 ilm 64
import java.util.Date;
18 ilm 65
import java.util.List;
67 ilm 66
import java.util.Locale;
18 ilm 67
 
68
import javax.swing.JFrame;
69
import javax.swing.JOptionPane;
25 ilm 70
import javax.swing.SwingUtilities;
18 ilm 71
 
72
import org.jdom.Document;
73
import org.jdom.Element;
74
import org.jdom.input.SAXBuilder;
75
import org.jdom.output.Format;
76
import org.jdom.output.XMLOutputter;
77
 
78
public class Caisse {
19 ilm 79
    private static final String POS_CONFIGURATION_FILENAME = "pos.xml";
18 ilm 80
    private static Document document;
81
 
19 ilm 82
    public static File getConfigFile(final String appName, final File wd) {
83
        final File wdFile = new File(wd + "/Configuration", POS_CONFIGURATION_FILENAME);
84
        final File confFile;
85
        if (wdFile.isFile()) {
86
            confFile = wdFile;
87
        } else {
88
            final File preferencesFolder = DesktopEnvironment.getDE().getPreferencesFolder(appName);
89
            if (!preferencesFolder.exists()) {
90
                preferencesFolder.mkdir();
91
            }
92
            confFile = new File(preferencesFolder, POS_CONFIGURATION_FILENAME);
93
        }
94
        return confFile;
18 ilm 95
    }
96
 
97
    private static Document getDocument() {
98
        if (document != null) {
99
            return document;
100
        }
101
 
102
        final SAXBuilder constructeur = new SAXBuilder();
103
        // lecture du contenu d'un fichier XML avec JDOM
104
        File file = getConfigFile();
19 ilm 105
        document = new Document();
106
 
18 ilm 107
        if (!file.exists()) {
19 ilm 108
            System.err.println("Erreur le fichier " + file.getAbsolutePath() + " n'existe pas!");
109
            document.setRootElement(new Element("config"));
110
            return document;
18 ilm 111
        }
112
 
113
        try {
114
            System.out.println("Loading:" + file.getAbsolutePath());
115
            document = constructeur.build(file);
116
        } catch (Exception e) {
19 ilm 117
            document.setRootElement(new Element("config"));
18 ilm 118
        }
119
        return document;
120
    }
121
 
25 ilm 122
    public static File getConfigFile() {
19 ilm 123
        return getConfigFile(ComptaPropsConfiguration.APP_NAME, new File("."));
124
    }
125
 
18 ilm 126
    public static void createConnexion() {
127
        final ComptaPropsConfiguration conf = ComptaPropsConfiguration.create();
67 ilm 128
        TranslationManager.getInstance().addTranslationStreamFromClass(MainFrame.class);
129
        TranslationManager.getInstance().setLocale(Locale.getDefault());
130
 
18 ilm 131
        Configuration.setInstance(conf);
132
        try {
133
            conf.getBase();
134
        } catch (Exception e) {
135
            e.printStackTrace();
136
        }
137
 
138
        try {
67 ilm 139
            UserManager.getInstance().setCurrentUser(getUserID());
18 ilm 140
            final ComptaPropsConfiguration comptaPropsConfiguration = ((ComptaPropsConfiguration) Configuration.getInstance());
141
            comptaPropsConfiguration.setUpSocieteDataBaseConnexion(getSocieteID());
142
        } catch (Exception e) {
143
            JOptionPane.showMessageDialog(new JFrame(), "Impossible de configurer la connexion à la base de donnée.\n ID société: " + getSocieteID() + " \n ID utilisateur: " + getUserID());
144
            e.printStackTrace();
61 ilm 145
            System.exit(2);
18 ilm 146
        }
147
    }
148
 
25 ilm 149
    public static void commitAll(final List<Ticket> tickets) {
18 ilm 150
        // createConnexion();
25 ilm 151
        try {
152
            SQLUtils.executeAtomic(Configuration.getInstance().getSystemRoot().getDataSource(), new SQLUtils.SQLFactory<Object>() {
153
                @Override
154
                public Object create() throws SQLException {
155
                    SQLElement elt = Configuration.getInstance().getDirectory().getElement("TICKET_CAISSE");
156
                    SQLElement eltFact = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE_ELEMENT");
157
                    SQLElement eltEnc = Configuration.getInstance().getDirectory().getElement("ENCAISSER_MONTANT");
158
                    SQLElement eltMode = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
67 ilm 159
                    SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement("ARTICLE");
80 ilm 160
                    int imported = 0;
25 ilm 161
                    for (Ticket ticket : tickets) {
162
                        SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
163
                        sel.addSelect(elt.getTable().getField("NUMERO"));
164
                        sel.setWhere(new Where(elt.getTable().getField("NUMERO"), "=", ticket.getCode()));
165
                        List<?> l = Configuration.getInstance().getBase().getDataSource().executeCol(sel.asString());
166
                        if (l != null && l.size() == 0) {
18 ilm 167
 
25 ilm 168
                            SQLRowValues rowVals = new SQLRowValues(elt.getTable());
169
                            rowVals.put("NUMERO", ticket.getCode());
170
                            rowVals.put("DATE", ticket.getCreationDate());
171
                            rowVals.put("ID_CAISSE", getID());
18 ilm 172
 
67 ilm 173
                            TotalCalculator calc = new TotalCalculator("T_PA_HT", "T_PV_HT", null);
174
 
73 ilm 175
                            String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
176
                            Boolean bServiceActive = Boolean.valueOf(val);
177
                            calc.setServiceActive(bServiceActive != null && bServiceActive);
178
 
25 ilm 179
                            // Articles
180
                            for (Pair<Article, Integer> item : ticket.getArticles()) {
181
                                SQLRowValues rowValsElt = new SQLRowValues(eltFact.getTable());
61 ilm 182
                                final Article article = item.getFirst();
183
                                final Integer nb = item.getSecond();
184
                                rowValsElt.put("QTE", nb);
67 ilm 185
                                rowValsElt.put("PV_HT", article.getPriceHTInCents());
186
                                Float tauxFromId = TaxeCache.getCache().getTauxFromId(article.getIdTaxe());
187
                                BigDecimal tauxTVA = new BigDecimal(tauxFromId).movePointLeft(2).add(BigDecimal.ONE);
188
 
189
                                final BigDecimal valueHT = article.getPriceHTInCents().multiply(new BigDecimal(nb), MathContext.DECIMAL128);
73 ilm 190
 
25 ilm 191
                                rowValsElt.put("T_PV_HT", valueHT);
67 ilm 192
                                rowValsElt.put("T_PV_TTC", valueHT.multiply(tauxTVA, MathContext.DECIMAL128));
61 ilm 193
                                rowValsElt.put("ID_TAXE", article.getIdTaxe());
194
                                rowValsElt.put("CODE", article.getCode());
195
                                rowValsElt.put("NOM", article.getName());
25 ilm 196
                                rowValsElt.put("ID_TICKET_CAISSE", rowVals);
61 ilm 197
                                rowValsElt.put("ID_ARTICLE", article.getId());
67 ilm 198
                                calc.addLine(rowValsElt, eltArticle.getTable().getRow(article.getId()), 0, false);
25 ilm 199
                            }
67 ilm 200
                            calc.checkResult();
201
                            long longValueTotalHT = calc.getTotalHT().movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
202
                            rowVals.put("TOTAL_HT", longValueTotalHT);
18 ilm 203
 
67 ilm 204
                            long longValueTotal = calc.getTotalTTC().movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
205
                            rowVals.put("TOTAL_TTC", longValueTotal);
206
                            long longValueTotalTVA = calc.getTotalTVA().movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
207
                            rowVals.put("TOTAL_TVA", longValueTotalTVA);
18 ilm 208
 
25 ilm 209
                            // Paiements
210
                            for (Paiement paiement : ticket.getPaiements()) {
211
                                if (paiement.getMontantInCents() > 0) {
212
                                    SQLRowValues rowValsElt = new SQLRowValues(eltEnc.getTable());
213
                                    SQLRowValues rowValsEltMode = new SQLRowValues(eltMode.getTable());
214
                                    if (paiement.getType() == Paiement.CB) {
215
                                        rowValsEltMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.CB);
216
                                    } else if (paiement.getType() == Paiement.CHEQUE) {
217
                                        rowValsEltMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.CHEQUE);
218
                                    } else if (paiement.getType() == Paiement.ESPECES) {
219
                                        rowValsEltMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.ESPECE);
220
                                    }
18 ilm 221
 
25 ilm 222
                                    rowValsElt.put("ID_MODE_REGLEMENT", rowValsEltMode);
223
                                    try {
224
                                        rowValsElt.put("ID_CLIENT", getClientCaisse().getID());
225
                                    } catch (SQLException e) {
226
                                        e.printStackTrace();
227
                                    }
228
                                    long montant = Long.valueOf(paiement.getMontantInCents());
229
                                    if (ticket.getPaiements().size() == 1 && paiement.getType() == Paiement.ESPECES) {
67 ilm 230
                                        montant = longValueTotal;
25 ilm 231
                                    }
232
                                    rowValsElt.put("MONTANT", montant);
233
                                    rowValsElt.put("NOM", "Ticket " + ticket.getCode());
234
                                    rowValsElt.put("DATE", ticket.getCreationDate());
235
                                    rowValsElt.put("ID_TICKET_CAISSE", rowVals);
236
                                }
237
                            }
238
 
239
                            SQLRow rowFinal = rowVals.insert();
80 ilm 240
                            imported++;
61 ilm 241
                            GenerationMvtTicketCaisse mvt = new GenerationMvtTicketCaisse(rowFinal);
242
                            final Integer idMvt;
25 ilm 243
                            try {
61 ilm 244
                                idMvt = mvt.genereMouvement().call();
73 ilm 245
 
246
                                SQLRowValues valTicket = rowFinal.asRowValues();
247
                                valTicket.put("ID_MOUVEMENT", Integer.valueOf(idMvt));
248
                                rowFinal = valTicket.update();
249
 
250
                                // msie à jour du mouvement
251
                                List<SQLRow> rowsEnc = rowFinal.getReferentRows(eltEnc.getTable());
252
                                long totalEnc = 0;
253
                                for (SQLRow sqlRow : rowsEnc) {
254
                                    long montant = sqlRow.getLong("MONTANT");
255
                                    PrixTTC ttc = new PrixTTC(montant);
256
                                    totalEnc += montant;
257
                                    new GenerationReglementVenteNG("Règlement " + sqlRow.getForeignRow("ID_MODE_REGLEMENT").getForeignRow("ID_TYPE_REGLEMENT").getString("NOM") + " Ticket "
258
                                            + rowFinal.getString("NUMERO"), getClientCaisse(), ttc, sqlRow.getDate("DATE").getTime(), sqlRow.getForeignRow("ID_MODE_REGLEMENT"), rowFinal, rowFinal
259
                                            .getForeignRow("ID_MOUVEMENT"), false);
260
                                }
261
                                if (totalEnc > longValueTotal) {
262
                                    final SQLTable table = Configuration.getInstance().getDirectory().getElement("TYPE_REGLEMENT").getTable();
263
                                    int idComptePceCaisse = table.getRow(TypeReglementSQLElement.ESPECE).getInt("ID_COMPTE_PCE_CLIENT");
264
                                    if (idComptePceCaisse == table.getUndefinedID()) {
265
                                        idComptePceCaisse = ComptePCESQLElement.getId(ComptePCESQLElement.getComptePceDefault("VenteEspece"));
266
                                    }
267
                                    new GenerationMvtVirement(idComptePceCaisse, getClientCaisse().getInt("ID_COMPTE_PCE"), 0, totalEnc - longValueTotal, "Rendu sur règlement " + " Ticket "
268
                                            + rowFinal.getString("NUMERO"), new Date(), JournalSQLElement.CAISSES, " Ticket " + rowFinal.getString("NUMERO")).genereMouvement();
269
                                }
61 ilm 270
                            } catch (Exception exn) {
25 ilm 271
                                exn.printStackTrace();
61 ilm 272
                                throw new SQLException(exn);
25 ilm 273
                            }
274
                            updateStock(rowFinal.getID());
275
 
18 ilm 276
                        }
277
                    }
80 ilm 278
                    // mark imported
279
                    for (Ticket ticket : tickets) {
280
                        final ReceiptCode code = ticket.getReceiptCode();
281
                        try {
282
                            // it's OK if some files cannot be moved, the next call will try again
283
                            // (the above code doesn't import duplicates)
284
                            code.markImported();
285
                        } catch (IOException e) {
286
                            e.printStackTrace();
287
                        }
288
                    }
289
                    // archive to avoid parsing more and more receipts
290
                    try {
291
                        // it's OK if some files cannot be moved, the next call will try again
292
                        ReceiptCode.archiveCompletelyImported();
293
                    } catch (IOException e) {
294
                        e.printStackTrace();
295
                    }
296
                    final String count = imported + "/" + tickets.size();
25 ilm 297
                    SwingUtilities.invokeLater(new Runnable() {
298
 
299
                        @Override
300
                        public void run() {
80 ilm 301
                            JOptionPane.showMessageDialog(null, count + " ticket(s) importé(s). Clôture de la caisse terminée.");
21 ilm 302
                        }
25 ilm 303
                    });
304
                    return null;
18 ilm 305
                }
25 ilm 306
            });
307
        } catch (Exception exn) {
308
            ExceptionHandler.handle("Une erreur est survenue pendant la clôture.", exn);
18 ilm 309
        }
25 ilm 310
 
18 ilm 311
    }
312
 
313
    private static SQLRow rowClient = null;
314
 
315
    private static SQLRow getClientCaisse() throws SQLException {
316
        if (rowClient == null) {
317
            SQLElement elt = Configuration.getInstance().getDirectory().getElement("CLIENT");
73 ilm 318
            SQLSelect sel = new SQLSelect();
18 ilm 319
            sel.addSelectStar(elt.getTable());
320
            sel.setWhere(new Where(elt.getTable().getField("NOM"), "=", "Caisse OpenConcerto"));
321
            List<SQLRow> l = (List<SQLRow>) elt.getTable().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
322
            if (l.size() > 0) {
323
                rowClient = l.get(0);
324
            } else {
325
                SQLRowValues rowValues = new SQLRowValues(elt.getTable());
326
                rowValues.put("NOM", "Caisse OpenConcerto");
327
                SQLRowValues rowValuesMode = new SQLRowValues(elt.getTable().getTable("MODE_REGLEMENT"));
328
                rowValuesMode.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.CB);
329
                rowValues.put("ID_MODE_REGLEMENT", rowValuesMode);
330
 
331
                // Select Compte client par defaut
332
                final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
333
                final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
334
                final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
335
 
336
                int idDefaultCompteClient = rowPrefsCompte.getInt("ID_COMPTE_PCE_CLIENT");
337
                if (idDefaultCompteClient <= 1) {
338
                    try {
339
                        idDefaultCompteClient = ComptePCESQLElement.getIdComptePceDefault("Clients");
340
                    } catch (Exception e) {
341
                        e.printStackTrace();
342
                    }
343
                }
344
 
345
                rowValues.put("ID_COMPTE_PCE", idDefaultCompteClient);
346
                rowClient = rowValues.insert();
347
            }
348
        }
349
        return rowClient;
350
 
351
    }
352
 
73 ilm 353
    private static void updateStock(int id) throws SQLException {
18 ilm 354
 
83 ilm 355
        SQLRow row = getClientCaisse().getTable().getTable("TICKET_CAISSE").getRow(id);
356
        StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
61 ilm 357
            @Override
83 ilm 358
            public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
61 ilm 359
                return "Ticket N°" + rowOrigin.getString("NUMERO");
18 ilm 360
            }
83 ilm 361
        }, row, row.getReferentRows(getClientCaisse().getTable().getTable("SAISIE_VENTE_FACTURE_ELEMENT")), Type.REAL_DELIVER);
362
 
363
        stockUpdater.update();
364
 
18 ilm 365
    }
366
 
367
    public static int getID() {
368
        final Document d = getDocument();
83 ilm 369
        return Integer.valueOf(d.getRootElement().getAttributeValue("caisseID", "2"));
18 ilm 370
    }
371
 
372
    public static void setID(int caisseId) {
373
        final Document d = getDocument();
374
        d.getRootElement().setAttribute("caisseID", String.valueOf(caisseId));
375
    }
376
 
377
    public static List<Ticket> allTickets() {
378
        final List<Ticket> l = new ArrayList<Ticket>();
80 ilm 379
        for (final File f : ReceiptCode.getReceiptsToImport(Caisse.getID())) {
380
            final Ticket ticket = Ticket.parseFile(f);
65 ilm 381
            if (ticket != null) {
382
                l.add(ticket);
383
            }
18 ilm 384
        }
385
        return l;
386
    }
387
 
388
    public static int getUserID() {
389
        final Document d = getDocument();
83 ilm 390
        return Integer.valueOf(d.getRootElement().getAttributeValue("userID", "2"));
18 ilm 391
    }
392
 
393
    public static void setUserID(int userId) {
394
        final Document d = getDocument();
395
        d.getRootElement().setAttribute("userID", String.valueOf(userId));
396
    }
397
 
398
    public static int getSocieteID() {
399
        final Document d = getDocument();
83 ilm 400
        return Integer.valueOf(d.getRootElement().getAttributeValue("societeID", "42"));
18 ilm 401
    }
402
 
403
    public static void setSocieteID(int societeId) {
404
        final Document d = getDocument();
405
        d.getRootElement().setAttribute("societeID", String.valueOf(societeId));
406
    }
407
 
83 ilm 408
    public static int getScanDelay() {
409
        final Document d = getDocument();
410
        return Integer.valueOf(d.getRootElement().getAttributeValue("scanDelay", "80"));
411
    }
412
 
413
    public static void setScanDelay(int scanDelay) {
414
        final Document d = getDocument();
415
        d.getRootElement().setAttribute("scanDelay", String.valueOf(scanDelay));
416
    }
417
 
18 ilm 418
    public static boolean isCopyActive() {
419
        final Document d = getDocument();
19 ilm 420
        return Boolean.valueOf(d.getRootElement().getAttributeValue("copyTicket", "true"));
18 ilm 421
    }
422
 
423
    public static void setCopyActive(boolean b) {
424
        final Document d = getDocument();
425
        d.getRootElement().setAttribute("copyTicket", b ? "true" : "false");
426
    }
427
 
428
    public static TicketPrinter getTicketPrinter() {
429
        if (isUsingJPos()) {
430
            JPOSTicketPrinter prt = new JPOSTicketPrinter(getJPosPrinter());
431
            return prt;
432
        }
433
        return new ESCSerialPrinter(getESCPPort());
434
    }
435
 
436
    public static boolean isUsingJPos() {
437
        final Document d = getDocument();
19 ilm 438
        Element child = d.getRootElement().getChild("printer");
18 ilm 439
        if (child == null) {
19 ilm 440
            child = new Element("printer");
441
            d.getRootElement().addContent(child);
18 ilm 442
            return false;
443
        }
444
        final String type = child.getAttributeValue("type");
445
        return (type != null && type.equalsIgnoreCase("jpos"));
446
    }
447
 
448
    public static void setPrinterType(String type) {
449
        final Document d = getDocument();
19 ilm 450
        Element e = d.getRootElement().getChild("printer");
451
        if (e == null) {
452
            e = new Element("printer");
453
            d.getRootElement().addContent(e);
454
        }
455
        e.setAttribute("type", type);
18 ilm 456
    }
457
 
458
    public static String getJPosPrinter() {
459
        final Document d = getDocument();
460
        final List<Element> children = d.getRootElement().getChildren("jpos");
19 ilm 461
        if (children != null) {
462
            for (Element e : children) {
463
                if (e.getAttribute("printer") != null) {
464
                    return e.getAttributeValue("printer");
465
                }
18 ilm 466
            }
467
        }
468
        return "";
469
    }
470
 
471
    public static void setJPosPrinter(String printer) {
472
        final Document d = getDocument();
19 ilm 473
        Element e = d.getRootElement().getChild("jpos");
474
        if (e == null) {
475
            e = new Element("jpos");
476
            d.getRootElement().addContent(e);
477
        }
18 ilm 478
        e.setAttribute("printer", printer);
479
    }
480
 
481
    public static String getESCPPort() {
482
        final Document d = getDocument();
483
        final List<Element> children = d.getRootElement().getChildren("escp");
19 ilm 484
        if (children != null) {
485
            for (Element e : children) {
486
                if (e.getAttribute("port") != null) {
487
                    return e.getAttributeValue("port", "COM1:");
488
                }
18 ilm 489
            }
490
        }
491
        return "COM1:";
492
    }
493
 
494
    public static void setESCPPort(String port) {
495
        final Document d = getDocument();
19 ilm 496
        Element e = d.getRootElement().getChild("escp");
497
        if (e == null) {
498
            e = new Element("escp");
499
            d.getRootElement().addContent(e);
500
        }
18 ilm 501
        e.setAttribute("port", port);
502
    }
503
 
83 ilm 504
    public static String getCardPort() {
505
        final Document d = getDocument();
506
        final List<Element> children = d.getRootElement().getChildren("card");
507
        if (children != null) {
508
            for (Element e : children) {
509
                if (e.getAttribute("port") != null) {
510
                    return e.getAttributeValue("port", "COM1:");
511
                }
512
            }
513
        }
514
        return "";
515
    }
516
 
517
    public static void setCardPort(String port) {
518
        final Document d = getDocument();
519
        Element e = d.getRootElement().getChild("card");
520
        if (e == null) {
521
            e = new Element("card");
522
            d.getRootElement().addContent(e);
523
        }
524
        e.setAttribute("port", port);
525
    }
526
 
18 ilm 527
    public static String getJPosDirectory() {
528
        final Document d = getDocument();
529
        final List<Element> children = d.getRootElement().getChildren("jpos");
19 ilm 530
        if (children != null) {
531
            for (Element e : children) {
532
                if (e.getValue() != null) {
533
                    return e.getValue();
534
                }
18 ilm 535
            }
536
        }
537
        return "";
538
    }
539
 
540
    public static void setJPosDirectory(String dir) {
541
        final Document d = getDocument();
19 ilm 542
        Element e = d.getRootElement().getChild("jpos");
543
        if (e == null) {
544
            e = new Element("jpos");
545
            d.getRootElement().addContent(e);
546
        }
18 ilm 547
        e.setText(dir);
548
    }
549
 
550
    public static List<TicketLine> getHeaders() {
551
        final List<TicketLine> l = new ArrayList<TicketLine>();
552
        final Document d = getDocument();
553
        final List<Element> list = d.getRootElement().getChildren("header");
19 ilm 554
        if (list != null) {
555
            for (Element element : list) {
556
                l.add(new TicketLine(element.getValue(), element.getAttributeValue("style")));
557
            }
18 ilm 558
        }
559
        return l;
560
    }
561
 
562
    public static void setLines(String type, List<TicketLine> lines) {
563
        final Document d = getDocument();
564
        final Element rootElement = d.getRootElement();
565
        rootElement.removeChildren(type);
566
        for (TicketLine ticketLine : lines) {
567
            Element e = new Element(type);
568
            final String style = ticketLine.getStyle();
569
            if (style != null && !style.isEmpty()) {
570
                e.setAttribute("style", style);
571
            }
572
            e.setText(ticketLine.getText());
573
            rootElement.addContent(e);
574
        }
575
 
576
    }
577
 
578
    public static List<TicketLine> getFooters() {
579
        final List<TicketLine> l = new ArrayList<TicketLine>();
580
        final Document d = getDocument();
581
        final List<Element> list = d.getRootElement().getChildren("footer");
19 ilm 582
        if (list != null) {
583
            for (Element element : list) {
584
                l.add(new TicketLine(element.getValue(), element.getAttributeValue("style")));
585
            }
18 ilm 586
        }
587
        return l;
588
    }
589
 
590
    public static void setHeaders(List<TicketLine> lines) {
591
        setLines("header", lines);
592
    }
593
 
594
    public static void setFooters(List<TicketLine> lines) {
595
        setLines("footer", lines);
596
    }
597
 
598
    public static int getTicketWidth() {
599
        final Document d = getDocument();
600
        final List<Element> children = d.getRootElement().getChildren("printer");
19 ilm 601
        if (children != null) {
602
            for (Element e : children) {
603
                if (e.getAttribute("printWidth") != null) {
604
                    final String attributeValue = e.getAttributeValue("printWidth");
605
                    return Integer.parseInt(attributeValue);
606
                }
18 ilm 607
            }
608
        }
609
        return 20;
610
    }
611
 
612
    public static void setTicketWidth(String w) {
613
        final Document d = getDocument();
19 ilm 614
        Element e = d.getRootElement().getChild("printer");
615
        if (e == null) {
616
            e = new Element("printer");
617
            d.getRootElement().addContent(e);
618
        }
18 ilm 619
        e.setAttribute("printWidth", w);
620
    }
621
 
83 ilm 622
    public static int getScreenWidth() {
623
        final Document d = getDocument();
624
        final List<Element> children = d.getRootElement().getChildren("screen");
625
        if (children != null) {
626
            for (Element e : children) {
627
                if (e.getAttribute("width") != null) {
628
                    return Integer.valueOf(e.getAttributeValue("width", "0"));
629
                }
630
            }
631
        }
632
        return 0;
633
    }
634
 
635
    public static int getScreenHeight() {
636
        final Document d = getDocument();
637
        final List<Element> children = d.getRootElement().getChildren("screen");
638
        if (children != null) {
639
            for (Element e : children) {
640
                if (e.getAttribute("height") != null) {
641
                    return Integer.valueOf(e.getAttributeValue("height", "0"));
642
                }
643
            }
644
        }
645
        return 0;
646
    }
647
 
18 ilm 648
    public static void saveConfiguration() {
649
        final File file = getConfigFile();
650
        final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
651
        try {
652
            System.out.println("Saving:" + file.getAbsolutePath());
653
            final FileOutputStream fileOutputStream = new FileOutputStream(file);
654
            outputter.output(getDocument(), fileOutputStream);
655
            fileOutputStream.close();
656
        } catch (Exception e) {
657
            e.printStackTrace();
19 ilm 658
            ExceptionHandler.handle("Erreur lors de la sauvegarde de la configuration de la caisse.\n" + file.getAbsolutePath());
18 ilm 659
        }
660
 
661
    }
662
 
663
}