OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 181 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 181 Rev 183
Line 11... Line 11...
11
import java.io.File;
11
import java.io.File;
12
import java.io.FileInputStream;
12
import java.io.FileInputStream;
13
import java.io.FileOutputStream;
13
import java.io.FileOutputStream;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.StringReader;
15
import java.io.StringReader;
-
 
16
import java.math.BigDecimal;
16
import java.net.Socket;
17
import java.net.Socket;
17
import java.nio.charset.StandardCharsets;
18
import java.nio.charset.StandardCharsets;
18
import java.text.DecimalFormat;
19
import java.text.DecimalFormat;
19
import java.util.ArrayList;
20
import java.util.ArrayList;
20
import java.util.HashMap;
21
import java.util.HashMap;
Line 87... Line 88...
87
    }
88
    }
88
 
89
 
89
    public ZPLPrinterPanel(String zpl) {
90
    public ZPLPrinterPanel(String zpl) {
90
        this.zpl = zpl;
91
        this.zpl = zpl;
91
        this.variables = getVariables(zpl);
92
        this.variables = getVariables(zpl);
92
        knownVariables.add("product.code");
93
        this.knownVariables.add("product.code");
93
        knownVariables.add("product.name");
94
        this.knownVariables.add("product.name");
94
        knownVariables.add("product.material");
95
        this.knownVariables.add("product.material");
95
 
-
 
-
 
96
        this.knownVariables.add("product.color");
-
 
97
        this.knownVariables.add("product.size");
96
        knownVariables.add("product.ean13");
98
        this.knownVariables.add("product.ean13");
97
        knownVariables.add("product.price");
99
        this.knownVariables.add("product.price");
98
        knownVariables.add("product.pricewithtax");
100
        this.knownVariables.add("product.pricewithtax");
99
        //
101
        //
100
        mapName.put("product.name", "Nom");
102
        this.mapName.put("product.name", "Nom");
101
        mapName.put("product.code", "Code");
103
        this.mapName.put("product.code", "Code");
102
        mapName.put("product.ean13", "Code à barres");
104
        this.mapName.put("product.ean13", "Code à barres");
103
        mapName.put("product.price", "Prix HT");
105
        this.mapName.put("product.price", "Prix HT");
104
        mapName.put("product.pricewithtax", "Prix TTC");
106
        this.mapName.put("product.pricewithtax", "Prix TTC");
105
        mapName.put("product.treatment", "Traitement");
107
        this.mapName.put("product.treatment", "Traitement");
106
        mapName.put("product.origin", "Origine");
108
        this.mapName.put("product.origin", "Origine");
107
        mapName.put("product.batch", "Lot");
109
        this.mapName.put("product.batch", "Lot");
108
        mapName.put("product.size", "Taille");
110
        this.mapName.put("product.size", "Taille");
109
        mapName.put("product.color", "Couleur");
111
        this.mapName.put("product.color", "Couleur");
110
        mapName.put("product.material", "Matière");
112
        this.mapName.put("product.material", "Matière");
111
    }
113
    }
112
 
114
 
113
    private final File getPrefFile() {
115
    private final File getPrefFile() {
114
        final File prefsFolder = BaseDirs.create(ProductInfo.getInstance()).getPreferencesFolder();
116
        final File prefsFolder = BaseDirs.create(ProductInfo.getInstance()).getPreferencesFolder();
115
        if (!prefsFolder.exists()) {
117
        if (!prefsFolder.exists()) {
Line 126... Line 128...
126
 
128
 
127
        final File file = getPrefFile();
129
        final File file = getPrefFile();
128
        System.out.println(file.getAbsolutePath());
130
        System.out.println(file.getAbsolutePath());
129
        if (file.exists()) {
131
        if (file.exists()) {
130
            try {
132
            try {
131
                properties.load(new FileInputStream(file));
133
                this.properties.load(new FileInputStream(file));
132
            } catch (IOException e) {
134
            } catch (IOException e) {
133
                e.printStackTrace();
135
                e.printStackTrace();
134
            }
136
            }
135
        }
137
        }
136
 
138
 
Line 141... Line 143...
141
 
143
 
142
        // Fields
144
        // Fields
143
 
145
 
144
        Set<String> added = new HashSet<>();
146
        Set<String> added = new HashSet<>();
145
        for (String v : this.knownVariables) {
147
        for (String v : this.knownVariables) {
146
            if (variables.contains(v)) {
148
            if (this.variables.contains(v)) {
147
                // Non editable
149
                // Non editable
148
                String label = getName(v);
150
                String label = getName(v);
149
                c.gridx = 0;
151
                c.gridx = 0;
150
                c.weightx = 0;
152
                c.weightx = 0;
151
                this.add(new JLabel(label, SwingConstants.RIGHT), c);
153
                this.add(new JLabel(label, SwingConstants.RIGHT), c);
Line 156... Line 158...
156
                JTextField txt = new JTextField(20);
158
                JTextField txt = new JTextField(20);
157
                if (value != null) {
159
                if (value != null) {
158
                    txt.setText(value);
160
                    txt.setText(value);
159
                    txt.setEditable(false);
161
                    txt.setEditable(false);
160
                }
162
                }
161
                editorMap.put(v, txt);
163
                this.editorMap.put(v, txt);
162
                this.add(txt, c);
164
                this.add(txt, c);
163
                added.add(v);
165
                added.add(v);
164
                c.gridy++;
166
                c.gridy++;
165
            }
167
            }
166
        }
168
        }
Line 172... Line 174...
172
                c.weightx = 0;
174
                c.weightx = 0;
173
                this.add(new JLabel(label, SwingConstants.RIGHT), c);
175
                this.add(new JLabel(label, SwingConstants.RIGHT), c);
174
                c.gridx++;
176
                c.gridx++;
175
                c.weightx = 1;
177
                c.weightx = 1;
176
                JTextField txt = new JTextField(20);
178
                JTextField txt = new JTextField(20);
177
                editorMap.put(v, txt);
179
                this.editorMap.put(v, txt);
178
                this.add(txt, c);
180
                this.add(txt, c);
179
                added.add(v);
181
                added.add(v);
180
                c.gridy++;
182
                c.gridy++;
181
            }
183
            }
182
 
184
 
Line 231... Line 233...
231
        c.gridx = 1;
233
        c.gridx = 1;
232
        c.fill = GridBagConstraints.NONE;
234
        c.fill = GridBagConstraints.NONE;
233
        c.anchor = GridBagConstraints.SOUTHEAST;
235
        c.anchor = GridBagConstraints.SOUTHEAST;
234
 
236
 
235
        // Restore state from properties
237
        // Restore state from properties
236
        if (properties.getOrDefault("printerType", "local").equals("local")) {
238
        if (this.properties.getOrDefault("printerType", "local").equals("local")) {
237
            radioLocalPrinter.setSelected(true);
239
            radioLocalPrinter.setSelected(true);
238
        } else {
240
        } else {
239
            radioNetworkPrinter.setSelected(true);
241
            radioNetworkPrinter.setSelected(true);
240
        }
242
        }
241
        textPrinterIP.setText(properties.getOrDefault("printerIp", "").toString());
243
        textPrinterIP.setText(this.properties.getOrDefault("printerIp", "").toString());
242
        portZPL.setValue(Long.parseLong(properties.getOrDefault("printerPort", "9100").toString()));
244
        portZPL.setValue(Long.parseLong(this.properties.getOrDefault("printerPort", "9100").toString()));
243
        nbLabels.setValue(Long.parseLong(properties.getOrDefault("nbLabels", "1").toString()));
245
        nbLabels.setValue(Long.parseLong(this.properties.getOrDefault("nbLabels", "1").toString()));
244
        delayLabels.setValue(Long.parseLong(properties.getOrDefault("delay", "800").toString()));
246
        delayLabels.setValue(Long.parseLong(this.properties.getOrDefault("delay", "800").toString()));
245
        // Print
247
        // Print
246
 
248
 
247
        JButton printButton = new JButton("Imprimer");
249
        JButton printButton = new JButton("Imprimer");
248
        this.add(printButton, c);
250
        this.add(printButton, c);
249
        printButton.addActionListener(new ActionListener() {
251
        printButton.addActionListener(new ActionListener() {
250
 
252
 
251
            @Override
253
            @Override
252
            public void actionPerformed(ActionEvent e) {
254
            public void actionPerformed(ActionEvent e) {
253
                try {
255
                try {
254
                    if (radioLocalPrinter.isSelected()) {
256
                    if (radioLocalPrinter.isSelected()) {
255
                        properties.put("printerType", "local");
257
                        ZPLPrinterPanel.this.properties.put("printerType", "local");
256
                    } else {
258
                    } else {
257
                        properties.put("printerType", "network");
259
                        ZPLPrinterPanel.this.properties.put("printerType", "network");
258
                        properties.put("printerIp", textPrinterIP.getText());
260
                        ZPLPrinterPanel.this.properties.put("printerIp", textPrinterIP.getText());
259
                        properties.put("printerPort", portZPL.getValue().toString());
261
                        ZPLPrinterPanel.this.properties.put("printerPort", portZPL.getValue().toString());
260
                    }
262
                    }
261
                    properties.put("nbLabels", nbLabels.getValue().toString());
263
                    ZPLPrinterPanel.this.properties.put("nbLabels", nbLabels.getValue().toString());
262
                    properties.put("delay", delayLabels.getValue().toString());
264
                    ZPLPrinterPanel.this.properties.put("delay", delayLabels.getValue().toString());
263
                    // Save Prefs
265
                    // Save Prefs
264
                    properties.store(new FileOutputStream(getPrefFile()), "");
266
                    ZPLPrinterPanel.this.properties.store(new FileOutputStream(getPrefFile()), "");
265
                } catch (Exception e1) {
267
                } catch (Exception e1) {
266
                    ExceptionHandler.handle("Erreur de sauvegarde de " + getPrefFile().getAbsolutePath(), e1);
268
                    ExceptionHandler.handle("Erreur de sauvegarde de " + getPrefFile().getAbsolutePath(), e1);
267
                }
269
                }
268
                final String code = createZPLCode();
270
                final String code = createZPLCode();
269
                System.out.println("ZPL:");
271
                System.out.println("ZPL:");
Line 294... Line 296...
294
                } else {
296
                } else {
295
                    try {
297
                    try {
296
                        final PrinterJob pj1 = PrinterJob.getPrinterJob();
298
                        final PrinterJob pj1 = PrinterJob.getPrinterJob();
297
                        if (pj1.printDialog()) {
299
                        if (pj1.printDialog()) {
298
                            final PrintService ps = pj1.getPrintService();
300
                            final PrintService ps = pj1.getPrintService();
299
                            final DocPrintJob pj = ps.createPrintJob();
-
 
300
                            final SimpleDoc doc = new SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
-
 
301
                            final int nb = ((Number) nbLabels.getValue()).intValue();
301
                            final int nb = ((Number) nbLabels.getValue()).intValue();
302
                            for (int i = 0; i < nb; i++) {
302
                            for (int i = 0; i < nb; i++) {
-
 
303
                                final DocPrintJob pj = ps.createPrintJob();
-
 
304
                                final SimpleDoc doc = new SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
303
                                pj.print(doc, null);
305
                                pj.print(doc, null);
304
                                Thread.sleep(((Number) delayLabels.getValue()).intValue());
306
                                Thread.sleep(((Number) delayLabels.getValue()).intValue());
305
                            }
307
                            }
306
                        }
308
                        }
307
                    } catch (Exception ex) {
309
                    } catch (Exception ex) {
Line 355... Line 357...
355
 
357
 
356
    public String getValueAsString(SQLRowAccessor row, String variableName) {
358
    public String getValueAsString(SQLRowAccessor row, String variableName) {
357
        if (row == null) {
359
        if (row == null) {
358
            return null;
360
            return null;
359
        }
361
        }
-
 
362
        System.err.println("ZPLPrinterPanel.getValueAsString()" + variableName + " : " + row);
360
        if (variableName.equals("product.code")) {
363
        if (variableName.equals("product.code")) {
361
            return row.getString("CODE");
364
            return row.getString("CODE");
362
        } else if (variableName.equals("product.name")) {
365
        } else if (variableName.equals("product.name")) {
363
            return row.getString("NOM");
366
            return row.getString("NOM");
364
        } else if (variableName.equals("product.ean13")) {
367
        } else if (variableName.equals("product.ean13")) {
365
            return row.getString("CODE_BARRE");
368
            return row.getString("CODE_BARRE");
366
        } else if (variableName.equals("product.price")) {
369
        } else if (variableName.equals("product.price")) {
-
 
370
            final BigDecimal bigDecimal = row.getBigDecimal("PV_HT");
367
            return new DecimalFormat("#0.00").format(row.getBigDecimal("PV_HT"));
371
            return new DecimalFormat("#0.00").format(bigDecimal);
368
        } else if (variableName.equals("product.pricewithtax")) {
372
        } else if (variableName.equals("product.pricewithtax")) {
-
 
373
            final BigDecimal bigDecimal = row.getBigDecimal("PV_TTC");
369
            return new DecimalFormat("#0.00").format(row.getBigDecimal("PV_TTC"));
374
            return new DecimalFormat("#0.00").format(bigDecimal);
370
        } else if (variableName.equals("product.material")) {
375
        } else if (variableName.equals("product.material")) {
371
            return row.getString("MATIERE");
376
            return row.getString("MATIERE");
-
 
377
        } else if (variableName.equals("product.color")) {
-
 
378
            if (row.getTable().contains("ID_ARTICLE_DECLINAISON_COULEUR")) {
-
 
379
                if (row.getObject("ID_ARTICLE_DECLINAISON_COULEUR") != null && !row.isForeignEmpty("ID_ARTICLE_DECLINAISON_COULEUR")) {
-
 
380
                    return row.getForeign("ID_ARTICLE_DECLINAISON_COULEUR").getString("NOM");
-
 
381
                }
-
 
382
            }
-
 
383
        } else if (variableName.equals("product.size")) {
-
 
384
            if (row.getTable().contains("ID_ARTICLE_DECLINAISON_TAILLE")) {
-
 
385
                if (row.getObject("ID_ARTICLE_DECLINAISON_TAILLE") != null && !row.isForeignEmpty("ID_ARTICLE_DECLINAISON_TAILLE")) {
-
 
386
                    return row.getForeign("ID_ARTICLE_DECLINAISON_TAILLE").getString("NOM");
-
 
387
                }
-
 
388
            }
372
        }
389
        }
373
        return "";
390
        return "";
374
    }
391
    }
375
 
392
 
376
    public String getName(String variableName) {
393
    public String getName(String variableName) {
377
        String n = mapName.get(variableName);
394
        String n = this.mapName.get(variableName);
378
        if (n == null) {
395
        if (n == null) {
379
            return variableName;
396
            return variableName;
380
        }
397
        }
381
        return n;
398
        return n;
382
    }
399
    }