OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.ui;
15
 
80 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18 ilm 17
import org.openconcerto.erp.config.ServerFinderConfig;
18
import org.openconcerto.erp.config.ServerFinderPanel;
132 ilm 19
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
20
import org.openconcerto.erp.core.sales.pos.io.ESCSerialPrinter;
18 ilm 21
import org.openconcerto.sql.model.DBRoot;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowListRSH;
24
import org.openconcerto.sql.model.SQLSelect;
25
import org.openconcerto.ui.DefaultGridBagConstraints;
26
import org.openconcerto.ui.JLabelBold;
27
 
28
import java.awt.Component;
29
import java.awt.Dimension;
30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
132 ilm 32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
18 ilm 34
import java.awt.event.ComponentEvent;
35
import java.awt.event.ComponentListener;
36
import java.awt.event.ItemEvent;
37
import java.awt.event.ItemListener;
132 ilm 38
import java.util.ArrayList;
18 ilm 39
import java.util.List;
40
import java.util.Vector;
41
 
142 ilm 42
import javax.print.PrintService;
43
import javax.print.PrintServiceLookup;
18 ilm 44
import javax.swing.ComboBoxModel;
45
import javax.swing.DefaultComboBoxModel;
46
import javax.swing.DefaultListCellRenderer;
132 ilm 47
import javax.swing.JButton;
18 ilm 48
import javax.swing.JComboBox;
49
import javax.swing.JLabel;
50
import javax.swing.JList;
51
import javax.swing.JOptionPane;
52
import javax.swing.JPanel;
132 ilm 53
import javax.swing.JTabbedPane;
18 ilm 54
import javax.swing.JTextField;
55
import javax.swing.SwingConstants;
56
import javax.swing.SwingUtilities;
57
 
58
public class ConfigCaissePanel extends JPanel {
132 ilm 59
 
156 ilm 60
    private final POSConfiguration configuration;
18 ilm 61
    private int userId;
62
    private int societeId;
63
    private int caisseId;
64
    private final ServerFinderPanel serverFinderPanel;
65
    private final JComboBox comboSociete;
66
    private final JComboBox comboCaisse;
67
    private final JComboBox comboUtilisateur;
182 ilm 68
    private final JComboBox comboDepot;
18 ilm 69
    private final TicketLineTable headerTable;
70
    private final TicketLineTable footerTable;
83 ilm 71
    private JTextField textTerminalCB;
18 ilm 72
 
132 ilm 73
    // Ticket printers
74
    private final TicketPrinterConfigPanel ticketPanel1;
75
    private final TicketPrinterConfigPanel ticketPanel2;
76
    // LCD
142 ilm 77
    private JComboBox comboLCDType;
78
    private JTextField textLCDPort;
132 ilm 79
    private JTextField textLCDLine1;
80
    private JTextField textLCDLine2;
182 ilm 81
    protected int depotStockId = 2;
132 ilm 82
 
18 ilm 83
    public ConfigCaissePanel(final ServerFinderPanel serverFinderPanel) {
84
        this.serverFinderPanel = serverFinderPanel;
156 ilm 85
        try {
86
            this.configuration = POSConfiguration.setInstance();
87
        } catch (Exception e) {
88
            throw new IllegalStateException("Couldn't initialise POSConfiguration", e);
89
        }
132 ilm 90
 
18 ilm 91
        setOpaque(false);
92
 
93
        setLayout(new GridBagLayout());
94
        final GridBagConstraints c = new DefaultGridBagConstraints();
95
        c.weighty = 0;
25 ilm 96
        // Fichier
97
        c.weightx = 0;
98
        this.add(new JLabel("Fichier de configuration", SwingConstants.RIGHT), c);
99
        c.gridx++;
100
        c.gridwidth = 3;
101
        c.weightx = 1;
102
        JTextField textConfigurationFile = new JTextField("");
142 ilm 103
        if (this.configuration.getConfigFile() != null) {
104
            textConfigurationFile.setText(this.configuration.getConfigFile().getAbsolutePath());
25 ilm 105
        }
106
        textConfigurationFile.setEditable(false);
107
        this.add(textConfigurationFile, c);
108
 
18 ilm 109
        // Connexion
25 ilm 110
        c.gridy++;
111
        c.gridx = 0;
18 ilm 112
        final JLabelBold titleConnexion = new JLabelBold("Connexion");
113
        c.gridwidth = 2;
114
        this.add(titleConnexion, c);
115
        c.gridy++;
116
        c.gridwidth = 1;
117
        c.weightx = 0;
118
        this.add(new JLabel("Société", SwingConstants.RIGHT), c);
119
        c.gridx++;
120
        c.weightx = 1;
121
        this.comboSociete = new JComboBox();
122
        this.comboSociete.setRenderer(new DefaultListCellRenderer() {
123
            @Override
124
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
125
                final SQLRow r = (SQLRow) value;
126
                String label = "";
127
                if (r != null) {
128
                    label = r.getString("NOM") + " (" + r.getString("ID") + ")";
129
                }
130
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
131
            }
132
        });
133
        this.add(this.comboSociete, c);
134
        c.gridx = 0;
135
        c.gridy++;
136
        c.weightx = 0;
137
        this.add(new JLabel("Caisse", SwingConstants.RIGHT), c);
138
        c.gridx++;
139
        this.comboCaisse = new JComboBox();
140
        this.comboCaisse.setRenderer(new DefaultListCellRenderer() {
141
            @Override
142
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
143
                final SQLRow r = (SQLRow) value;
144
                String label = "";
145
                if (r != null) {
146
                    label = r.getString("NOM") + " (" + r.getString("ID") + ")";
147
                }
148
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
149
            }
150
        });
151
        this.add(this.comboCaisse, c);
182 ilm 152
        // Utilisateur
18 ilm 153
        c.gridx = 0;
154
        c.gridy++;
155
        c.weightx = 0;
156
        this.add(new JLabel("Utilisateur", SwingConstants.RIGHT), c);
157
        c.gridx++;
158
        this.comboUtilisateur = new JComboBox();
159
        this.comboUtilisateur.setRenderer(new DefaultListCellRenderer() {
160
            @Override
161
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
162
                final SQLRow r = (SQLRow) value;
163
                String label = "";
164
                if (r != null) {
165
                    label = r.getString("NOM") + " " + r.getString("PRENOM") + " (" + r.getString("ID") + ")";
166
                }
167
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
168
            }
169
        });
170
        this.add(this.comboUtilisateur, c);
182 ilm 171
        // Depots
172
        c.gridx = 0;
173
        c.gridy++;
174
        c.weightx = 0;
175
        this.add(new JLabel("Dépôt de stock", SwingConstants.RIGHT), c);
176
        c.gridx++;
177
        this.comboDepot = new JComboBox();
178
        this.comboDepot.setRenderer(new DefaultListCellRenderer() {
179
            @Override
180
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
181
                final SQLRow r = (SQLRow) value;
182
                String label = "";
183
                if (r != null) {
184
                    label = r.getString("CODE") + "  " + r.getString("NOM") + " (" + r.getID() + ")".trim();
185
                }
186
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
187
            }
188
        });
189
        this.add(this.comboDepot, c);
18 ilm 190
 
191
        // Ticket
192
        final JLabelBold titleTicket = new JLabelBold("Ticket");
193
        c.gridwidth = 2;
194
        c.gridx = 0;
195
        c.gridy++;
196
        c.weightx = 0;
197
        this.add(titleTicket, c);
132 ilm 198
 
199
        final JTabbedPane tabsTicket = new JTabbedPane();
18 ilm 200
        c.gridy++;
132 ilm 201
        c.gridwidth = 2;
18 ilm 202
        c.weightx = 0;
132 ilm 203
        c.fill = GridBagConstraints.BOTH;
18 ilm 204
        c.anchor = GridBagConstraints.NORTHWEST;
205
        this.headerTable = new TicketLineTable();
132 ilm 206
        tabsTicket.addTab("Entête", this.headerTable);
207
        this.footerTable = new TicketLineTable();
208
        tabsTicket.addTab("Pied de page", this.footerTable);
209
        this.add(tabsTicket, c);
18 ilm 210
 
132 ilm 211
        // Périphérique
212
        final JLabelBold titleImprimante = new JLabelBold("Périphériques");
18 ilm 213
        c.fill = GridBagConstraints.HORIZONTAL;
214
        c.anchor = GridBagConstraints.WEST;
215
        c.gridwidth = 2;
216
        c.gridx = 0;
217
        c.gridy++;
218
        c.weightx = 0;
219
        this.add(titleImprimante, c);
220
        c.gridy++;
132 ilm 221
        //
222
        JTabbedPane tabs = new JTabbedPane();
223
        ticketPanel1 = new TicketPrinterConfigPanel();
224
        ticketPanel1.setOpaque(false);
225
        ticketPanel2 = new TicketPrinterConfigPanel();
226
        ticketPanel2.setOpaque(false);
227
        tabs.addTab("Imprimante ticket principale", ticketPanel1);
228
        tabs.addTab("Imprimante ticket secondaire", ticketPanel2);
229
        tabs.addTab("Terminal CB", createCreditCardPanel());
230
        tabs.addTab("Afficheur LCD", createLCDPanel());
231
        this.add(tabs, c);
232
        //
18 ilm 233
 
234
        // Spacer
235
        c.gridx = 0;
236
        c.gridy++;
237
        c.weighty = 1;
238
        c.fill = GridBagConstraints.BOTH;
239
        final JPanel spacer = new JPanel();
240
        spacer.setOpaque(false);
241
        spacer.setPreferredSize(new Dimension(10, 10));
242
 
243
        add(spacer, c);
244
 
245
        // Listeners
246
        this.comboSociete.addItemListener(new ItemListener() {
247
 
248
            @Override
249
            public void itemStateChanged(final ItemEvent e) {
250
                if (e.getStateChange() == ItemEvent.SELECTED) {
251
                    ConfigCaissePanel.this.societeId = ((SQLRow) e.getItem()).getID();
252
                    reloadCaisses();
253
                }
254
            }
255
        });
256
        this.comboCaisse.addItemListener(new ItemListener() {
257
            @Override
258
            public void itemStateChanged(final ItemEvent e) {
259
                if (e.getStateChange() == ItemEvent.SELECTED) {
260
                    ConfigCaissePanel.this.caisseId = ((SQLRow) e.getItem()).getID();
261
                }
262
            }
263
        });
264
        this.comboUtilisateur.addItemListener(new ItemListener() {
265
            @Override
266
            public void itemStateChanged(final ItemEvent e) {
267
                if (e.getStateChange() == ItemEvent.SELECTED) {
268
                    ConfigCaissePanel.this.userId = ((SQLRow) e.getItem()).getID();
269
                }
270
            }
271
        });
182 ilm 272
        this.comboDepot.addItemListener(new ItemListener() {
273
            @Override
274
            public void itemStateChanged(final ItemEvent e) {
275
                if (e.getStateChange() == ItemEvent.SELECTED) {
276
                    ConfigCaissePanel.this.depotStockId = ((SQLRow) e.getItem()).getID();
277
                }
278
            }
279
        });
18 ilm 280
    }
281
 
132 ilm 282
    private Component createLCDPanel() {
283
        final JPanel p = new JPanel();
284
        p.setOpaque(false);
285
 
286
        GridBagConstraints c = new DefaultGridBagConstraints();
287
        p.setLayout(new GridBagLayout());
288
        c.gridx = 0;
289
        c.gridy++;
290
        c.weightx = 0;
291
        c.gridwidth = 1;
292
        c.anchor = GridBagConstraints.EAST;
293
        c.fill = GridBagConstraints.NONE;
142 ilm 294
        p.add(new JLabel("Type", SwingConstants.RIGHT), c);
132 ilm 295
        c.gridx++;
142 ilm 296
        c.weightx = 0;
297
        c.fill = GridBagConstraints.NONE;
298
        c.anchor = GridBagConstraints.WEST;
299
        comboLCDType = new JComboBox(new String[] { "Port série", "Emulation imprimante" });
300
 
301
        p.add(comboLCDType, c);
302
        c.gridy++;
303
        c.gridx = 0;
304
        c.anchor = GridBagConstraints.EAST;
305
        final JLabel labelType = new JLabel("Port", SwingConstants.RIGHT);
306
        p.add(labelType, c);
307
        c.gridx++;
132 ilm 308
        c.weightx = 1;
309
        c.fill = GridBagConstraints.HORIZONTAL;
310
 
142 ilm 311
        this.textLCDPort = new JTextField(20);
312
        p.add(this.textLCDPort, c);
132 ilm 313
        c.weightx = 0;
314
        c.gridx++;
315
        final JButton selectPortButton = new JButton("Sélectionner");
316
        selectPortButton.addActionListener(new ActionListener() {
317
 
318
            @Override
319
            public void actionPerformed(ActionEvent e) {
320
                List<String> choices = new ArrayList<String>();
321
                final String title;
322
                final String message;
142 ilm 323
                if (comboLCDType.getSelectedIndex() == 1) {
324
                    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
325
                    for (PrintService printer : printServices) {
326
                        choices.add(printer.getName());
327
                    }
328
                    title = "Afficheur LCD";
329
                    message = "Choisissez l'imprimante correspondante à l'afficheur LCD";
330
                } else if (comboLCDType.getSelectedIndex() == 0) {
331
                    choices.addAll(ESCSerialPrinter.getSerialPortNames());
332
                    title = "Port série";
333
                    message = "Choisissez le port série lié à l'afficheur LCD";
334
                } else {
335
                    return;
336
                }
132 ilm 337
                if (choices.isEmpty()) {
338
                    return;
339
                }
340
                String s = (String) JOptionPane.showInputDialog(p, message, title, JOptionPane.PLAIN_MESSAGE, null, choices.toArray(), choices.get(0));
142 ilm 341
 
132 ilm 342
                // If a string was returned
343
                if ((s != null) && (s.length() > 0)) {
142 ilm 344
                    textLCDPort.setText(s);
132 ilm 345
                }
142 ilm 346
 
132 ilm 347
            }
348
        });
349
        p.add(selectPortButton, c);
350
 
351
        c.gridx = 0;
352
        c.gridy++;
353
        c.gridwidth = 3;
354
        p.add(new JLabel("Message d'accueil de l'afficheur"), c);
355
        //
356
        c.gridy++;
357
        c.gridwidth = 1;
358
        p.add(new JLabel("Ligne 1", SwingConstants.RIGHT), c);
359
        c.gridx++;
360
        this.textLCDLine1 = new JTextField(20);
361
        p.add(textLCDLine1, c);
362
 
363
        c.gridx = 0;
364
        c.gridy++;
365
        p.add(new JLabel("Ligne 2", SwingConstants.RIGHT), c);
366
        c.gridx++;
367
        this.textLCDLine2 = new JTextField(20);
368
        p.add(textLCDLine2, c);
369
 
370
        final JPanel spacer = new JPanel();
371
        spacer.setOpaque(false);
372
        c.gridy++;
373
        c.weighty = 1;
374
        p.add(spacer, c);
375
        return p;
376
    }
377
 
378
    private JPanel createCreditCardPanel() {
379
        final JPanel p = new JPanel();
380
        p.setOpaque(false);
381
        p.setLayout(new GridBagLayout());
382
        GridBagConstraints c = new DefaultGridBagConstraints();
383
 
384
        c.gridx = 0;
385
        c.gridy++;
386
        c.weightx = 0;
387
        c.gridwidth = 1;
388
        c.anchor = GridBagConstraints.EAST;
389
        c.fill = GridBagConstraints.NONE;
390
        p.add(new JLabel("Port série", SwingConstants.RIGHT), c);
391
        c.gridx++;
392
        c.weightx = 1;
393
        c.fill = GridBagConstraints.HORIZONTAL;
394
 
395
        this.textTerminalCB = new JTextField(20);
396
        p.add(this.textTerminalCB, c);
397
        c.weightx = 0;
398
        c.gridx++;
399
        final JButton selectPortButton = new JButton("Sélectionner");
400
        selectPortButton.addActionListener(new ActionListener() {
401
 
402
            @Override
403
            public void actionPerformed(ActionEvent e) {
404
                List<String> choices = new ArrayList<String>();
405
                final String title;
406
                final String message;
407
 
408
                choices.addAll(ESCSerialPrinter.getSerialPortNames());
409
                title = "Port série";
410
                message = "Choisissez le port série lié au terminal CB";
411
                if (choices.isEmpty()) {
412
                    return;
413
                }
414
                String s = (String) JOptionPane.showInputDialog(p, message, title, JOptionPane.PLAIN_MESSAGE, null, choices.toArray(), choices.get(0));
415
                // If a string was returned
416
                if ((s != null) && (s.length() > 0)) {
417
                    textTerminalCB.setText(s);
418
                }
419
            }
420
        });
421
        p.add(selectPortButton, c);
422
 
423
        final JPanel spacer = new JPanel();
424
        spacer.setOpaque(false);
425
        c.gridy++;
426
        c.weighty = 1;
427
        p.add(spacer, c);
428
 
429
        return p;
430
    }
431
 
18 ilm 432
    protected void reloadCaisses() {
433
        this.comboCaisse.setEnabled(false);
434
        final int id = this.societeId;
435
        final ServerFinderConfig config = ConfigCaissePanel.this.serverFinderPanel.createServerFinderConfig();
436
        if (!config.isOnline()) {
437
            JOptionPane.showMessageDialog(ConfigCaissePanel.this, "Impossible de se connecter au serveur");
438
            return;
439
        }
440
 
80 ilm 441
        System.out.println("Reloading POS information from: " + config);
442
        ComptaPropsConfiguration conf = config.createConf();
18 ilm 443
        try {
444
            // Sociétés
80 ilm 445
            conf.setUpSocieteStructure(id);
446
            final SQLRow societe = conf.getRowSociete();
65 ilm 447
            if (societe != null) {
448
                final String name = societe.getString("DATABASE_NAME");
18 ilm 449
                // Caisses
80 ilm 450
                final SQLSelect sel = new SQLSelect();
451
                sel.addSelectStar(conf.getRootSociete().getTable("CAISSE"));
18 ilm 452
                final List<SQLRow> caisses = SQLRowListRSH.execute(sel);
182 ilm 453
                // Depots
454
                final SQLSelect sel2 = new SQLSelect();
455
                sel2.addSelectStar(conf.getRootSociete().getTable("DEPOT_STOCK"));
456
                final List<SQLRow> depots = SQLRowListRSH.execute(sel2);
457
 
25 ilm 458
                // Stock l'id de la caisse pour que la reslectionne soit correcte
459
                final int idCaisseToSelect = this.caisseId;
182 ilm 460
                final int idDepotToSelect = this.depotStockId;
461
 
18 ilm 462
                SwingUtilities.invokeLater(new Runnable() {
463
                    @Override
464
                    public void run() {
25 ilm 465
                        ConfigCaissePanel.this.caisseId = idCaisseToSelect;
182 ilm 466
                        ConfigCaissePanel.this.depotStockId = idDepotToSelect;
18 ilm 467
                        if (caisses.isEmpty()) {
468
                            JOptionPane.showMessageDialog(ConfigCaissePanel.this, "Pas de caisses définies dans la société " + name);
469
                        }
470
                        ConfigCaissePanel.this.comboCaisse.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(caisses)));
182 ilm 471
                        ConfigCaissePanel.this.comboDepot.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(depots)));
18 ilm 472
                        ConfigCaissePanel.this.comboUtilisateur.setEnabled(true);
473
                        ConfigCaissePanel.this.comboCaisse.setEnabled(true);
182 ilm 474
                        ConfigCaissePanel.this.comboDepot.setEnabled(true);
475
                        {
476
                            // Selection caisse
477
                            final ComboBoxModel model = ConfigCaissePanel.this.comboCaisse.getModel();
478
                            final int stop = model.getSize();
479
                            if (stop > 0) {
480
                                // Force la reselection si la valeur n'existe plus,
481
                                // nécessité de recuperer l'id
482
                                ConfigCaissePanel.this.caisseId = ((SQLRow) model.getElementAt(0)).getID();
483
                            }
484
                            for (int i = 0; i < stop; i++) {
485
                                final SQLRow r = (SQLRow) model.getElementAt(i);
486
                                if (r.getID() == idCaisseToSelect) {
487
                                    ConfigCaissePanel.this.comboCaisse.setSelectedItem(r);
488
                                    break;
489
                                }
490
                            }
18 ilm 491
                        }
182 ilm 492
                        {
493
                            // Selection depot
494
                            final ComboBoxModel model2 = ConfigCaissePanel.this.comboDepot.getModel();
495
                            final int stop2 = model2.getSize();
496
                            if (stop2 > 0) {
497
                                // Force la reselection si la valeur n'existe plus,
498
                                // nécessité de recuperer l'id
499
                                ConfigCaissePanel.this.depotStockId = ((SQLRow) model2.getElementAt(0)).getID();
18 ilm 500
                            }
182 ilm 501
                            for (int i = 0; i < stop2; i++) {
502
                                final SQLRow r = (SQLRow) model2.getElementAt(i);
503
                                if (r.getID() == idDepotToSelect) {
504
                                    ConfigCaissePanel.this.comboDepot.setSelectedItem(r);
505
                                    break;
506
                                }
507
                            }
18 ilm 508
                        }
509
                    }
510
 
511
                });
512
            } else {
513
                JOptionPane.showMessageDialog(this, "Impossible de trouver la société d'ID " + id);
514
            }
515
        } catch (final Exception e) {
516
            e.printStackTrace();
80 ilm 517
        } finally {
518
            conf.destroy();
18 ilm 519
        }
520
    }
521
 
522
    public void loadConfiguration() {
523
 
142 ilm 524
        final POSConfiguration configuration = this.configuration;
132 ilm 525
        // Terminal CB
526
        this.textTerminalCB.setText(configuration.getCreditCardPort());
527
        // Afficheur LCD
142 ilm 528
        final String lcdType = configuration.getLCDType();
529
        if (lcdType.equals("serial")) {
530
            this.comboLCDType.setSelectedIndex(0);
531
        } else {
532
            this.comboLCDType.setSelectedIndex(1);
533
        }
534
 
535
        this.textLCDPort.setText(configuration.getLCDPort());
132 ilm 536
        this.textLCDLine1.setText(configuration.getLCDLine1());
537
        this.textLCDLine2.setText(configuration.getLCDLine2());
538
 
539
        this.userId = configuration.getUserID();
540
        this.societeId = configuration.getCompanyID();
541
        this.caisseId = configuration.getPosID();
182 ilm 542
        this.depotStockId = configuration.getDepotID();
132 ilm 543
        this.headerTable.fillFrom(configuration.getHeaderLines());
544
        this.footerTable.fillFrom(configuration.getFooterLines());
545
        this.ticketPanel1.setConfiguration(configuration.getTicketPrinterConfiguration1());
546
        this.ticketPanel2.setConfiguration(configuration.getTicketPrinterConfiguration2());
547
 
18 ilm 548
        addComponentListener(new ComponentListener() {
549
            @Override
550
            public void componentHidden(final ComponentEvent event) {
551
            }
552
 
553
            @Override
554
            public void componentMoved(final ComponentEvent event) {
555
            }
556
 
557
            @Override
558
            public void componentResized(final ComponentEvent event) {
559
            }
560
 
561
            @Override
562
            public void componentShown(final ComponentEvent event) {
563
                final Thread t = new Thread(new Runnable() {
564
 
565
                    @Override
566
                    public void run() {
567
                        ConfigCaissePanel.this.comboSociete.setEnabled(false);
568
                        ConfigCaissePanel.this.comboUtilisateur.setEnabled(false);
569
                        ConfigCaissePanel.this.comboCaisse.setEnabled(false);
182 ilm 570
                        ConfigCaissePanel.this.comboDepot.setEnabled(false);
18 ilm 571
                        final ServerFinderConfig config = ConfigCaissePanel.this.serverFinderPanel.createServerFinderConfig();
572
                        if (!config.isOnline()) {
573
                            JOptionPane.showMessageDialog(ConfigCaissePanel.this, "Impossible de se connecter au serveur");
574
                            return;
575
                        }
80 ilm 576
                        final ComptaPropsConfiguration server = config.createConf();
18 ilm 577
                        try {
80 ilm 578
                            final DBRoot root = server.getRoot();
18 ilm 579
 
580
                            // Sociétés
80 ilm 581
                            SQLSelect sel = new SQLSelect();
582
                            sel.addSelectStar(root.findTable("SOCIETE_COMMON"));
18 ilm 583
                            final List<SQLRow> societes = SQLRowListRSH.execute(sel);
584
 
585
                            // Utilisateurs
80 ilm 586
                            sel = new SQLSelect();
587
                            sel.addSelectStar(root.findTable("USER_COMMON"));
18 ilm 588
                            final List<SQLRow> utilisateurs = SQLRowListRSH.execute(sel);
589
 
590
                            SwingUtilities.invokeLater(new Runnable() {
591
                                @Override
592
                                public void run() {
593
                                    ConfigCaissePanel.this.comboSociete.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(societes)));
594
                                    ConfigCaissePanel.this.comboUtilisateur.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(utilisateurs)));
595
 
596
                                    ConfigCaissePanel.this.comboSociete.setEnabled(true);
597
                                    ConfigCaissePanel.this.comboUtilisateur.setEnabled(true);
598
 
25 ilm 599
                                    // Societe
18 ilm 600
                                    ComboBoxModel model = ConfigCaissePanel.this.comboSociete.getModel();
601
                                    int stop = model.getSize();
25 ilm 602
                                    boolean societeFound = false;
18 ilm 603
                                    for (int i = 0; i < stop; i++) {
604
                                        final SQLRow r = (SQLRow) model.getElementAt(i);
605
                                        if (r.getID() == ConfigCaissePanel.this.societeId) {
606
                                            ConfigCaissePanel.this.comboSociete.setSelectedItem(r);
25 ilm 607
                                            ConfigCaissePanel.this.societeId = r.getID();
608
                                            societeFound = true;
18 ilm 609
                                            break;
610
                                        }
611
                                    }
25 ilm 612
 
613
                                    if (!societeFound && stop > 0) {
614
                                        ConfigCaissePanel.this.comboSociete.setSelectedItem(model.getElementAt(0));
615
                                        ConfigCaissePanel.this.societeId = ((SQLRow) model.getElementAt(0)).getID();
616
                                    }
617
                                    // Utilisateur
18 ilm 618
                                    model = ConfigCaissePanel.this.comboUtilisateur.getModel();
619
                                    stop = model.getSize();
25 ilm 620
                                    boolean utilisateurFound = false;
18 ilm 621
                                    for (int i = 0; i < stop; i++) {
622
                                        final SQLRow r = (SQLRow) model.getElementAt(i);
623
                                        if (r.getID() == ConfigCaissePanel.this.userId) {
624
                                            ConfigCaissePanel.this.comboUtilisateur.setSelectedItem(r);
25 ilm 625
                                            ConfigCaissePanel.this.userId = r.getID();
626
                                            utilisateurFound = true;
18 ilm 627
                                            break;
628
                                        }
629
                                    }
25 ilm 630
 
631
                                    if (!utilisateurFound && stop > 0) {
632
                                        ConfigCaissePanel.this.comboUtilisateur.setSelectedItem(model.getElementAt(0));
633
                                        ConfigCaissePanel.this.userId = ((SQLRow) model.getElementAt(0)).getID();
634
                                    }
132 ilm 635
                                    final Thread t = new Thread() {
636
                                        public void run() {
637
                                            reloadCaisses();
638
                                        };
639
                                    };
640
                                    t.start();
25 ilm 641
 
18 ilm 642
                                }
643
 
644
                            });
645
 
646
                        } catch (final Exception e) {
647
                            e.printStackTrace();
80 ilm 648
                        } finally {
649
                            server.destroy();
156 ilm 650
                            server.tearDownLogging(true);
18 ilm 651
                        }
652
                    }
653
                });
654
                t.setDaemon(true);
655
                t.start();
656
            }
657
        });
658
    }
659
 
25 ilm 660
    public void dumpConfiguration() {
661
        System.out.println("Societe: id:" + this.societeId);
662
        System.out.println("Caisse: id:" + this.caisseId);
663
        System.out.println("Utilisateur:  id:" + this.userId);
182 ilm 664
        System.out.println("Depot:  id:" + this.depotStockId);
25 ilm 665
    }
666
 
18 ilm 667
    public void saveConfiguration() {
142 ilm 668
        final POSConfiguration configuration = this.configuration;
132 ilm 669
        this.ticketPanel1.commitValues();
670
        this.ticketPanel2.commitValues();
671
        configuration.setUserID(this.userId);
672
        configuration.setCompanyID(this.societeId);
673
        configuration.setPosID(this.caisseId);
182 ilm 674
        configuration.setDepotID(this.depotStockId);
132 ilm 675
        configuration.setHeaderLines(this.headerTable.getLines());
676
        configuration.setFooterLines(this.footerTable.getLines());
677
        // Terminal CB
678
        configuration.setCreditCardPort(this.textTerminalCB.getText());
679
        // LCD
142 ilm 680
        final int selectedIndex = this.comboLCDType.getSelectedIndex();
681
        if (selectedIndex == 0) {
682
            configuration.setLCDType("serial");
683
        } else {
684
            configuration.setLCDType("printer");
685
        }
686
        configuration.setLCDPort(this.textLCDPort.getText());
132 ilm 687
        configuration.setLCDLine1(this.textLCDLine1.getText());
688
        configuration.setLCDLine2(this.textLCDLine2.getText());
689
        // Save
690
        configuration.saveConfiguration();
18 ilm 691
    }
692
}