OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 65 | Go to most recent revision | 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.ui;
15
 
16
import org.openconcerto.erp.config.ServerFinderConfig;
17
import org.openconcerto.erp.config.ServerFinderPanel;
18
import org.openconcerto.erp.core.sales.pos.Caisse;
19
import org.openconcerto.sql.model.DBRoot;
20
import org.openconcerto.sql.model.DBSystemRoot;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowListRSH;
23
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.SQLServer;
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;
32
import java.awt.event.ComponentEvent;
33
import java.awt.event.ComponentListener;
34
import java.awt.event.ItemEvent;
35
import java.awt.event.ItemListener;
36
import java.util.List;
37
import java.util.Vector;
38
 
39
import javax.swing.ComboBoxModel;
40
import javax.swing.DefaultComboBoxModel;
41
import javax.swing.DefaultListCellRenderer;
42
import javax.swing.JCheckBox;
43
import javax.swing.JComboBox;
44
import javax.swing.JLabel;
45
import javax.swing.JList;
46
import javax.swing.JOptionPane;
47
import javax.swing.JPanel;
48
import javax.swing.JTextField;
49
import javax.swing.SwingConstants;
50
import javax.swing.SwingUtilities;
51
 
52
public class ConfigCaissePanel extends JPanel {
53
    private final JComboBox comboType;
54
    private final JTextField textPrintWidth;
55
    private final JTextField textPort;
56
    private final JTextField textLibJPOS;
57
    private final JCheckBox checkboxDouble;
58
    private int userId;
59
    private int societeId;
60
    private int caisseId;
61
    private final ServerFinderPanel serverFinderPanel;
62
    private final JComboBox comboSociete;
63
    private final JComboBox comboCaisse;
64
    private final JComboBox comboUtilisateur;
65
    private final TicketLineTable headerTable;
66
    private final TicketLineTable footerTable;
67
 
68
    // Selecteur de societe
69
    // Selecteur d'utilisateur
70
    // Selecteur de numero de caisse
71
    // Headers
72
    // Footers
73
    // JPOS
74
    // ESCP
75
    public ConfigCaissePanel(final ServerFinderPanel serverFinderPanel) {
76
        this.serverFinderPanel = serverFinderPanel;
77
        setOpaque(false);
78
 
79
        setLayout(new GridBagLayout());
80
        final GridBagConstraints c = new DefaultGridBagConstraints();
81
        c.weighty = 0;
25 ilm 82
        // Fichier
83
        c.weightx = 0;
84
        this.add(new JLabel("Fichier de configuration", SwingConstants.RIGHT), c);
85
        c.gridx++;
86
        c.gridwidth = 3;
87
        c.weightx = 1;
88
        JTextField textConfigurationFile = new JTextField("");
89
        if (Caisse.getConfigFile() != null) {
90
            textConfigurationFile.setText(Caisse.getConfigFile().getAbsolutePath());
91
        }
92
        textConfigurationFile.setEditable(false);
93
        this.add(textConfigurationFile, c);
94
 
18 ilm 95
        // Connexion
25 ilm 96
        c.gridy++;
97
        c.gridx = 0;
18 ilm 98
        final JLabelBold titleConnexion = new JLabelBold("Connexion");
99
        c.gridwidth = 2;
100
        this.add(titleConnexion, c);
101
        c.gridy++;
102
        c.gridwidth = 1;
103
        c.weightx = 0;
104
        this.add(new JLabel("Société", SwingConstants.RIGHT), c);
105
        c.gridx++;
106
        c.weightx = 1;
107
        this.comboSociete = new JComboBox();
108
        this.comboSociete.setRenderer(new DefaultListCellRenderer() {
109
            @Override
110
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
111
                final SQLRow r = (SQLRow) value;
112
                String label = "";
113
                if (r != null) {
114
                    label = r.getString("NOM") + " (" + r.getString("ID") + ")";
115
                }
116
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
117
            }
118
        });
119
        this.add(this.comboSociete, c);
120
        c.gridx = 0;
121
        c.gridy++;
122
        c.weightx = 0;
123
        this.add(new JLabel("Caisse", SwingConstants.RIGHT), c);
124
        c.gridx++;
125
        this.comboCaisse = new JComboBox();
126
        this.comboCaisse.setRenderer(new DefaultListCellRenderer() {
127
            @Override
128
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
129
                final SQLRow r = (SQLRow) value;
130
                String label = "";
131
                if (r != null) {
132
                    label = r.getString("NOM") + " (" + r.getString("ID") + ")";
133
                }
134
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
135
            }
136
        });
137
        this.add(this.comboCaisse, c);
138
        c.gridx = 0;
139
        c.gridy++;
140
        c.weightx = 0;
141
        this.add(new JLabel("Utilisateur", SwingConstants.RIGHT), c);
142
        c.gridx++;
143
        this.comboUtilisateur = new JComboBox();
144
        this.comboUtilisateur.setRenderer(new DefaultListCellRenderer() {
145
            @Override
146
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
147
                final SQLRow r = (SQLRow) value;
148
                String label = "";
149
                if (r != null) {
150
                    label = r.getString("NOM") + " " + r.getString("PRENOM") + " (" + r.getString("ID") + ")";
151
                }
152
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
153
            }
154
        });
155
        this.add(this.comboUtilisateur, c);
156
 
157
        // Ticket
158
        final JLabelBold titleTicket = new JLabelBold("Ticket");
159
        c.gridwidth = 2;
160
        c.gridx = 0;
161
        c.gridy++;
162
        c.weightx = 0;
163
        this.add(titleTicket, c);
164
        c.gridy++;
165
        c.gridwidth = 1;
166
        c.weightx = 0;
167
        c.fill = GridBagConstraints.HORIZONTAL;
168
        c.anchor = GridBagConstraints.NORTHWEST;
169
        this.add(new JLabel("Entête", SwingConstants.RIGHT), c);
170
        c.gridx++;
171
        c.weightx = 1;
172
 
173
        c.fill = GridBagConstraints.BOTH;
174
        this.headerTable = new TicketLineTable();
175
        this.add(this.headerTable, c);
176
        c.gridx = 0;
177
        c.gridy++;
178
 
179
        c.weightx = 0;
180
        c.fill = GridBagConstraints.HORIZONTAL;
181
        this.add(new JLabel("Pied de page", SwingConstants.RIGHT), c);
182
        c.gridx++;
183
        c.weightx = 1;
184
 
185
        c.fill = GridBagConstraints.BOTH;
186
        this.footerTable = new TicketLineTable();
187
        this.add(this.footerTable, c);
188
        c.fill = GridBagConstraints.HORIZONTAL;
189
 
190
        this.checkboxDouble = new JCheckBox("imprimer le ticket en double exemplaire");
191
        this.checkboxDouble.setOpaque(false);
192
        c.gridx = 1;
193
        c.gridy++;
194
        c.weightx = 0;
195
        c.gridwidth = 1;
196
        this.add(this.checkboxDouble, c);
197
 
198
        // Imprimante
199
        final JLabelBold titleImprimante = new JLabelBold("Imprimante ticket");
200
        c.anchor = GridBagConstraints.WEST;
201
        c.gridwidth = 2;
202
        c.gridx = 0;
203
        c.gridy++;
204
        c.weightx = 0;
205
        this.add(titleImprimante, c);
206
        c.gridx = 0;
207
        c.gridy++;
208
        c.weightx = 0;
209
        c.gridwidth = 1;
210
        c.fill = GridBagConstraints.NONE;
211
        this.add(new JLabel("Largeur (en caractères)", SwingConstants.RIGHT), c);
212
        c.gridx++;
213
        this.textPrintWidth = new JTextField(8);
214
        this.add(this.textPrintWidth, c);
215
        c.gridy++;
216
        c.gridx = 0;
217
        c.weightx = 0;
218
        c.fill = GridBagConstraints.HORIZONTAL;
219
        this.add(new JLabel("Type", SwingConstants.RIGHT), c);
220
        c.gridx++;
221
        c.weightx = 1;
222
        c.fill = GridBagConstraints.NONE;
223
        this.comboType = new JComboBox(new String[] { "jpos", "escp" });
224
        this.add(this.comboType, c);
225
        c.gridx = 0;
226
        c.gridy++;
227
        c.weightx = 0;
228
        c.fill = GridBagConstraints.HORIZONTAL;
229
        this.add(new JLabel("Port/nom", SwingConstants.RIGHT), c);
230
        c.gridx++;
231
        this.textPort = new JTextField();
232
        this.add(this.textPort, c);
233
        c.gridx = 0;
234
        c.gridy++;
235
        c.weightx = 0;
236
        this.add(new JLabel("Dossier lib JPOS", SwingConstants.RIGHT), c);
237
        c.gridx++;
238
        this.textLibJPOS = new JTextField();
239
        this.add(this.textLibJPOS, c);
240
 
241
        // Spacer
242
        c.gridx = 0;
243
        c.gridy++;
244
        c.weighty = 1;
245
        c.fill = GridBagConstraints.BOTH;
246
        final JPanel spacer = new JPanel();
247
        spacer.setOpaque(false);
248
        spacer.setPreferredSize(new Dimension(10, 10));
249
 
250
        add(spacer, c);
251
 
252
        // Listeners
253
        this.comboSociete.addItemListener(new ItemListener() {
254
 
255
            @Override
256
            public void itemStateChanged(final ItemEvent e) {
257
                if (e.getStateChange() == ItemEvent.SELECTED) {
258
                    ConfigCaissePanel.this.societeId = ((SQLRow) e.getItem()).getID();
259
                    reloadCaisses();
260
                }
261
            }
262
        });
263
        this.comboCaisse.addItemListener(new ItemListener() {
264
            @Override
265
            public void itemStateChanged(final ItemEvent e) {
266
                if (e.getStateChange() == ItemEvent.SELECTED) {
267
                    ConfigCaissePanel.this.caisseId = ((SQLRow) e.getItem()).getID();
268
                }
269
            }
270
        });
271
        this.comboUtilisateur.addItemListener(new ItemListener() {
272
            @Override
273
            public void itemStateChanged(final ItemEvent e) {
274
                if (e.getStateChange() == ItemEvent.SELECTED) {
275
                    ConfigCaissePanel.this.userId = ((SQLRow) e.getItem()).getID();
276
                }
277
            }
278
        });
279
    }
280
 
281
    protected void reloadCaisses() {
282
        this.comboCaisse.setEnabled(false);
283
        final int id = this.societeId;
284
        final ServerFinderConfig config = ConfigCaissePanel.this.serverFinderPanel.createServerFinderConfig();
285
        if (!config.isOnline()) {
286
            JOptionPane.showMessageDialog(ConfigCaissePanel.this, "Impossible de se connecter au serveur");
287
            return;
288
        }
289
 
290
        try {
19 ilm 291
            System.out.println("Reloading POS information from: " + config);
18 ilm 292
            SQLServer server = config.createServer("Common");
25 ilm 293
            DBSystemRoot r = server.getSystemRoot(config.getSystemRoot());
18 ilm 294
            DBRoot root = r.getRoot("Common");
295
            // Sociétés
296
            SQLSelect sel = new SQLSelect(root.getBase());
297
            sel.addSelectStar(root.getTable("SOCIETE_COMMON"));
298
            sel.setWhere("SOCIETE_COMMON.ID", "=", id);
299
 
300
            final List<SQLRow> societes = SQLRowListRSH.execute(sel);
301
            server.destroy();
302
            if (societes.size() > 0) {
303
                final String name = societes.get(0).getString("DATABASE_NAME");
304
                server = config.createServer(name);
25 ilm 305
                r = server.getSystemRoot(config.getSystemRoot());
18 ilm 306
                root = r.getRoot(name);
307
                // Caisses
308
                sel = new SQLSelect(root.getBase());
309
                sel.addSelectStar(root.getTable("CAISSE"));
310
                final List<SQLRow> caisses = SQLRowListRSH.execute(sel);
311
                server.destroy();
25 ilm 312
                // Stock l'id de la caisse pour que la reslectionne soit correcte
313
                final int idCaisseToSelect = this.caisseId;
18 ilm 314
                SwingUtilities.invokeLater(new Runnable() {
315
                    @Override
316
                    public void run() {
25 ilm 317
                        ConfigCaissePanel.this.caisseId = idCaisseToSelect;
18 ilm 318
                        if (caisses.isEmpty()) {
319
                            JOptionPane.showMessageDialog(ConfigCaissePanel.this, "Pas de caisses définies dans la société " + name);
320
                        }
321
                        ConfigCaissePanel.this.comboCaisse.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(caisses)));
322
                        ConfigCaissePanel.this.comboUtilisateur.setEnabled(true);
323
                        ConfigCaissePanel.this.comboCaisse.setEnabled(true);
324
                        final ComboBoxModel model = ConfigCaissePanel.this.comboCaisse.getModel();
325
                        final int stop = model.getSize();
326
                        if (stop > 0) {
327
                            // Force la reselection si la valeur n'existe plus,
328
                            // nécessité de recuperer l'id
329
                            ConfigCaissePanel.this.caisseId = ((SQLRow) model.getElementAt(0)).getID();
330
                        }
331
                        for (int i = 0; i < stop; i++) {
332
                            final SQLRow r = (SQLRow) model.getElementAt(i);
25 ilm 333
                            if (r.getID() == idCaisseToSelect) {
18 ilm 334
                                ConfigCaissePanel.this.comboCaisse.setSelectedItem(r);
335
                                break;
336
                            }
337
                        }
338
                    }
339
 
340
                });
341
            } else {
342
                JOptionPane.showMessageDialog(this, "Impossible de trouver la société d'ID " + id);
343
            }
344
 
345
        } catch (final Exception e) {
346
            e.printStackTrace();
347
        }
348
    }
349
 
350
    public void loadConfiguration() {
351
        if (Caisse.isUsingJPos()) {
352
            this.comboType.setSelectedIndex(0);
353
            this.textPort.setText(Caisse.getJPosPrinter());
354
        } else {
355
            this.comboType.setSelectedIndex(1);
356
            this.textPort.setText(Caisse.getESCPPort());
357
        }
358
        this.textPrintWidth.setText(String.valueOf(Caisse.getTicketWidth()));
359
        this.textLibJPOS.setText(Caisse.getJPosDirectory());
360
        this.checkboxDouble.setSelected(Caisse.isCopyActive());
361
        this.userId = Caisse.getUserID();
362
        this.societeId = Caisse.getSocieteID();
363
        this.caisseId = Caisse.getID();
364
        this.headerTable.fillFrom(Caisse.getHeaders());
365
        this.footerTable.fillFrom(Caisse.getFooters());
366
 
367
        addComponentListener(new ComponentListener() {
368
            @Override
369
            public void componentHidden(final ComponentEvent event) {
370
            }
371
 
372
            @Override
373
            public void componentMoved(final ComponentEvent event) {
374
            }
375
 
376
            @Override
377
            public void componentResized(final ComponentEvent event) {
378
            }
379
 
380
            @Override
381
            public void componentShown(final ComponentEvent event) {
382
                final Thread t = new Thread(new Runnable() {
383
 
384
                    @Override
385
                    public void run() {
386
                        ConfigCaissePanel.this.comboSociete.setEnabled(false);
387
                        ConfigCaissePanel.this.comboUtilisateur.setEnabled(false);
388
                        ConfigCaissePanel.this.comboCaisse.setEnabled(false);
389
                        final ServerFinderConfig config = ConfigCaissePanel.this.serverFinderPanel.createServerFinderConfig();
390
                        if (!config.isOnline()) {
391
                            JOptionPane.showMessageDialog(ConfigCaissePanel.this, "Impossible de se connecter au serveur");
392
                            return;
393
                        }
394
                        try {
395
 
396
                            final SQLServer server = config.createServer("Common");
25 ilm 397
                            final DBSystemRoot r = server.getSystemRoot(config.getSystemRoot());
18 ilm 398
                            final DBRoot root = r.getRoot("Common");
399
                            // Sociétés
400
                            SQLSelect sel = new SQLSelect(root.getBase());
401
                            sel.addSelectStar(root.getTable("SOCIETE_COMMON"));
402
                            final List<SQLRow> societes = SQLRowListRSH.execute(sel);
403
 
404
                            // Utilisateurs
405
                            sel = new SQLSelect(root.getBase());
406
                            sel.addSelectStar(root.getTable("USER_COMMON"));
407
                            final List<SQLRow> utilisateurs = SQLRowListRSH.execute(sel);
408
 
409
                            server.destroy();
410
 
411
                            SwingUtilities.invokeLater(new Runnable() {
412
                                @Override
413
                                public void run() {
414
                                    ConfigCaissePanel.this.comboSociete.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(societes)));
415
                                    ConfigCaissePanel.this.comboUtilisateur.setModel(new DefaultComboBoxModel(new Vector<SQLRow>(utilisateurs)));
416
 
417
                                    ConfigCaissePanel.this.comboSociete.setEnabled(true);
418
                                    ConfigCaissePanel.this.comboUtilisateur.setEnabled(true);
419
 
25 ilm 420
                                    // Societe
18 ilm 421
                                    ComboBoxModel model = ConfigCaissePanel.this.comboSociete.getModel();
422
                                    int stop = model.getSize();
25 ilm 423
                                    boolean societeFound = false;
18 ilm 424
                                    for (int i = 0; i < stop; i++) {
425
                                        final SQLRow r = (SQLRow) model.getElementAt(i);
426
                                        if (r.getID() == ConfigCaissePanel.this.societeId) {
427
                                            ConfigCaissePanel.this.comboSociete.setSelectedItem(r);
25 ilm 428
                                            ConfigCaissePanel.this.societeId = r.getID();
429
                                            societeFound = true;
18 ilm 430
                                            break;
431
                                        }
432
                                    }
25 ilm 433
 
434
                                    if (!societeFound && stop > 0) {
435
                                        ConfigCaissePanel.this.comboSociete.setSelectedItem(model.getElementAt(0));
436
                                        ConfigCaissePanel.this.societeId = ((SQLRow) model.getElementAt(0)).getID();
437
                                    }
438
                                    // Utilisateur
18 ilm 439
                                    model = ConfigCaissePanel.this.comboUtilisateur.getModel();
440
                                    stop = model.getSize();
25 ilm 441
                                    boolean utilisateurFound = false;
18 ilm 442
                                    for (int i = 0; i < stop; i++) {
443
                                        final SQLRow r = (SQLRow) model.getElementAt(i);
444
                                        if (r.getID() == ConfigCaissePanel.this.userId) {
445
                                            ConfigCaissePanel.this.comboUtilisateur.setSelectedItem(r);
25 ilm 446
                                            ConfigCaissePanel.this.userId = r.getID();
447
                                            utilisateurFound = true;
18 ilm 448
                                            break;
449
                                        }
450
                                    }
25 ilm 451
 
452
                                    if (!utilisateurFound && stop > 0) {
453
                                        ConfigCaissePanel.this.comboUtilisateur.setSelectedItem(model.getElementAt(0));
454
                                        ConfigCaissePanel.this.userId = ((SQLRow) model.getElementAt(0)).getID();
455
                                    }
456
 
457
                                    reloadCaisses();
18 ilm 458
                                }
459
 
460
                            });
461
 
462
                        } catch (final Exception e) {
463
                            e.printStackTrace();
464
                        }
465
 
466
                    }
467
                });
468
                t.setDaemon(true);
469
                t.start();
470
            }
471
        });
472
    }
473
 
25 ilm 474
    public void dumpConfiguration() {
475
        System.out.println("Societe: id:" + this.societeId);
476
        System.out.println("Caisse: id:" + this.caisseId);
477
        System.out.println("Utilisateur:  id:" + this.userId);
478
    }
479
 
18 ilm 480
    public void saveConfiguration() {
481
        Caisse.setPrinterType(this.comboType.getSelectedItem().toString());
482
        if (this.comboType.getSelectedIndex() == 0) {
483
            // JPOS
484
            Caisse.setJPosPrinter(this.textPort.getText());
485
        } else {
486
            Caisse.setESCPPort(this.textPort.getText());
487
        }
488
        Caisse.setTicketWidth(this.textPrintWidth.getText());
489
        Caisse.setJPosDirectory(this.textLibJPOS.getText());
490
        Caisse.setCopyActive(this.checkboxDouble.isSelected());
491
 
492
        Caisse.setUserID(this.userId);
493
        Caisse.setSocieteID(this.societeId);
494
        Caisse.setID(this.caisseId);
495
 
496
        Caisse.setHeaders(this.headerTable.getLines());
497
        Caisse.setFooters(this.footerTable.getLines());
498
        Caisse.saveConfiguration();
499
    }
500
}