OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 151 Rev 174
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.core.sales.pos.ui;
14
 package org.openconcerto.erp.core.sales.pos.ui;
15
 
15
 
16
import org.openconcerto.erp.core.sales.pos.model.Article;
16
import org.openconcerto.erp.core.sales.pos.model.Article;
17
import org.openconcerto.erp.core.sales.pos.model.Paiement;
17
import org.openconcerto.erp.core.sales.pos.model.Paiement;
-
 
18
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
18
 
19
 
19
import java.awt.Color;
20
import java.awt.Color;
20
import java.awt.Dimension;
21
import java.awt.Dimension;
21
import java.awt.Font;
22
import java.awt.Font;
22
import java.awt.Graphics;
23
import java.awt.Graphics;
Line 24... Line 25...
24
import java.awt.RenderingHints;
25
import java.awt.RenderingHints;
25
import java.awt.event.KeyEvent;
26
import java.awt.event.KeyEvent;
26
import java.awt.event.MouseEvent;
27
import java.awt.event.MouseEvent;
27
import java.awt.event.MouseListener;
28
import java.awt.event.MouseListener;
28
import java.awt.geom.Rectangle2D;
29
import java.awt.geom.Rectangle2D;
-
 
30
import java.math.BigDecimal;
29
import java.util.List;
31
import java.util.List;
30
 
32
 
31
import javax.swing.JOptionPane;
33
import javax.swing.JOptionPane;
32
import javax.swing.JPanel;
34
import javax.swing.JPanel;
33
 
35
 
Line 64... Line 66...
64
 
66
 
65
    }
67
    }
66
 
68
 
67
    @Override
69
    @Override
68
    public void paint(Graphics g) {
70
    public void paint(Graphics g) {
-
 
71
 
69
        Graphics2D g2 = (Graphics2D) g;
72
        Graphics2D g2 = (Graphics2D) g;
70
        g.setColor(new Color(250, 250, 250));
73
        g.setColor(new Color(250, 250, 250));
71
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
74
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
72
 
75
 
73
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
76
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-
 
77
        g.setColor(Color.LIGHT_GRAY);
74
        g.drawLine(0, 0, 0, this.getHeight());
78
        g.drawLine(0, 0, 0, this.getHeight());
75
        int y = PAYMENT_POS_Y;
79
        int y = PAYMENT_POS_Y;
76
        for (Paiement p : controller.getPaiements()) {
80
        for (Paiement p : this.controller.getPaiements()) {
77
            if (p.getType() == Paiement.CB) {
81
            if (p.getType() == Paiement.CB) {
78
                drawMontant(g, "CB", p, 242 - 15, y, true);
82
                drawMontant(g, "CB", p, 242 - 15, y, true);
79
            } else if (p.getType() == Paiement.CHEQUE) {
83
            } else if (p.getType() == Paiement.CHEQUE) {
80
                drawMontant(g, "Chèque", p, 242 - 15, y, true);
84
                drawMontant(g, "Chèque", p, 242 - 15, y, true);
81
            } else if (p.getType() == Paiement.ESPECES) {
85
            } else if (p.getType() == Paiement.ESPECES) {
Line 107... Line 111...
107
        drawKey(g2, "x", 2, 4, 1, 1, CaissePanel.DARK_BLUE);
111
        drawKey(g2, "x", 2, 4, 1, 1, CaissePanel.DARK_BLUE);
108
        drawKey(g2, "-", 3, 4, 1, 1, CaissePanel.DARK_BLUE);
112
        drawKey(g2, "-", 3, 4, 1, 1, CaissePanel.DARK_BLUE);
109
 
113
 
110
        drawCalculator(g);
114
        drawCalculator(g);
111
        super.paint(g);
115
        super.paint(g);
-
 
116
 
112
    }
117
    }
113
 
118
 
114
    private void drawKey(Graphics2D g2, String string, int col, int row, int w, int h, Color color) {
119
    private void drawKey(Graphics2D g2, String string, int col, int row, int w, int h, Color color) {
115
        // background
120
        // background
116
        g2.setColor(color);
121
        g2.setColor(color);
Line 135... Line 140...
135
 
140
 
136
    private void drawCalculator(Graphics g) {
141
    private void drawCalculator(Graphics g) {
137
        Graphics2D g2 = (Graphics2D) g;
142
        Graphics2D g2 = (Graphics2D) g;
138
 
143
 
139
        // LCD
144
        // LCD
140
        if (controller.getArticleSelected() != null || controller.getPaiementSelected() != null) {
145
        if (this.controller.getArticleSelected() != null || this.controller.getPaiementSelected() != null) {
141
            g.setColor(new Color(232, 242, 254));
146
            g.setColor(new Color(232, 242, 254));
142
        } else {
147
        } else {
143
            g.setColor(new Color(240, 240, 240));
148
            g.setColor(new Color(240, 240, 240));
144
        }
149
        }
145
        g.fillRect(3, getLCDY(), this.getWidth() - 5, getLCDHeight());
150
        g.fillRect(3, getLCDY(), this.getWidth() - 5, getLCDHeight());
146
        //
151
        //
147
        int y = this.getHeight() - this.calcHeight - 10;
152
        int y = this.getHeight() - this.calcHeight - 10;
148
        g.setFont(new Font("Arial", Font.PLAIN, 32));
153
        g.setFont(new Font("Arial", Font.PLAIN, 32));
149
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
154
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
150
        final Article article = controller.getArticleSelected();
155
        final Article article = this.controller.getArticleSelected();
151
        g.setColor(Color.DARK_GRAY);
156
        g.setColor(Color.DARK_GRAY);
152
        if (article != null) {
157
        if (article != null) {
153
            String string = calculatorValue;
158
            String string = this.calculatorValue;
154
            g.setFont(g.getFont().deriveFont(52f));
159
            g.setFont(g.getFont().deriveFont(52f));
155
            Rectangle2D r1 = g.getFontMetrics().getStringBounds(string, g2);
160
            Rectangle2D r1 = g.getFontMetrics().getStringBounds(string, g2);
156
            g.drawString(string, (int) (260 - r1.getWidth()), y);
161
            g.drawString(string, (int) (260 - r1.getWidth()), y);
157
            g.setFont(g.getFont().deriveFont(14f));
162
            g.setFont(g.getFont().deriveFont(14f));
158
            g.drawString("Quantité", 10, 460 + y);
163
            g.drawString("Quantité", 10, 460 + y);
159
 
164
 
160
        } else {
165
        } else {
161
            final Paiement paiement = controller.getPaiementSelected();
166
            final Paiement paiement = this.controller.getPaiementSelected();
162
            if (paiement != null) {
167
            if (paiement != null) {
163
                String string = calculatorValue;
168
                String string = this.calculatorValue;
164
                g.setFont(g.getFont().deriveFont(52f));
169
                g.setFont(g.getFont().deriveFont(52f));
165
                Rectangle2D r1 = g.getFontMetrics().getStringBounds(string, g2);
170
                Rectangle2D r1 = g.getFontMetrics().getStringBounds(string, g2);
166
                g.drawString(string, (int) (260 - r1.getWidth()), y);
171
                g.drawString(string, (int) (260 - r1.getWidth()), y);
167
                g.setFont(g.getFont().deriveFont(14f));
172
                g.setFont(g.getFont().deriveFont(14f));
168
                String str = "Paiement ";
173
                String str = "Paiement ";
Line 177... Line 182...
177
                }
182
                }
178
                g.drawString(str, 10, y - 40);
183
                g.drawString(str, 10, y - 40);
179
            }
184
            }
180
        }
185
        }
181
        g.setFont(g.getFont().deriveFont(14f));
186
        g.setFont(g.getFont().deriveFont(14f));
182
        g.drawString("" + mode, 10, y - 20);
187
        g.drawString("" + this.mode, 10, y - 20);
183
    }
188
    }
184
 
189
 
185
    private char getToucheFrom(int x, int y) {
190
    private char getToucheFrom(int x, int y) {
186
        int yy = (this.getHeight() - y) / 68;
191
        int yy = (this.getHeight() - y) / 68;
187
        int xx = x / 69;
192
        int xx = x / 69;
Line 297... Line 302...
297
        g.drawString(sEuros, (int) (x - r1.getWidth() - r2.getWidth()), y);
302
        g.drawString(sEuros, (int) (x - r1.getWidth() - r2.getWidth()), y);
298
    }
303
    }
299
 
304
 
300
    @Override
305
    @Override
301
    public Dimension getPreferredSize() {
306
    public Dimension getPreferredSize() {
302
        return new Dimension(calcWidth, 768);
307
        return new Dimension(this.calcWidth, 768);
303
    }
308
    }
304
 
309
 
305
    public Dimension getMinimumSize() {
310
    public Dimension getMinimumSize() {
306
        return new Dimension(calcWidth, 768);
311
        return new Dimension(this.calcWidth, 768);
307
    }
312
    }
308
 
313
 
309
    @Override
314
    @Override
310
    public void caisseStateChanged() {
315
    public void caisseStateChanged() {
311
        if (controller.getArticleSelected() != null) {
316
        if (this.controller.getArticleSelected() != null) {
312
            initCaisseArticle();
317
            initCaisseArticle();
313
        } else if (controller.getPaiementSelected() != null) {
318
        } else if (this.controller.getPaiementSelected() != null) {
314
            initCaissePaiement();
319
            initCaissePaiement();
315
        }
320
        }
316
        // Add / Remove solde if needed
321
        // Add / Remove solde if needed
317
        boolean soldeIsShown = false;
322
        boolean soldeIsShown = false;
318
        for (Paiement p : controller.getPaiements()) {
323
        for (Paiement p : this.controller.getPaiements()) {
319
            if (p.getType() == Paiement.SOLDE) {
324
            if (p.getType() == Paiement.SOLDE) {
320
                soldeIsShown = true;
325
                soldeIsShown = true;
321
                break;
326
                break;
322
            }
327
            }
323
        }
328
        }
324
 
329
 
325
        if (controller.isClientDefined()) {
330
        if (this.controller.isClientDefined()) {
326
            if (!soldeIsShown && controller.getClient().getSolde().signum() == 1) {
331
            if (!soldeIsShown && this.controller.getClient().getSolde().signum() == 1) {
327
                // add
332
                // add
328
                controller.addPaiement(new Paiement(Paiement.SOLDE));
333
                this.controller.addPaiement(new Paiement(Paiement.SOLDE));
329
            }
334
            }
330
        } else {
335
        } else {
331
            if (soldeIsShown) {
336
            if (soldeIsShown) {
332
                // remove
337
                // remove
333
                final List<Paiement> paiements = controller.getPaiements();
338
                final List<Paiement> paiements = this.controller.getPaiements();
334
                for (int i = 0; i < paiements.size(); i++) {
339
                for (int i = 0; i < paiements.size(); i++) {
335
                    final Paiement p = paiements.get(i);
340
                    final Paiement p = paiements.get(i);
336
                    if (p.getType() == Paiement.SOLDE) {
341
                    if (p.getType() == Paiement.SOLDE) {
337
                        controller.getPaiements().remove(i);
342
                        this.controller.getPaiements().remove(i);
338
                        break;
343
                        break;
339
                    }
344
                    }
340
                }
345
                }
341
            }
346
            }
342
        }
347
        }
343
        repaint();
348
        repaint();
344
 
349
 
345
    }
350
    }
346
 
351
 
347
    private void initCaisseArticle() {
352
    private void initCaisseArticle() {
-
 
353
        if (this.controller.getTicketItemSelected() != null) {
348
        calculatorValue = String.valueOf(controller.getItemCount(controller.getArticleSelected()));
354
            this.calculatorValue = this.controller.getTicketItemSelected().getQty().toPlainString();
-
 
355
        } else {
-
 
356
            this.calculatorValue = "";
-
 
357
        }
349
        init = true;
358
        this.init = true;
350
        mode = ' ';
359
        this.mode = ' ';
351
    }
360
    }
352
 
361
 
353
    private void initCaissePaiement() {
362
    private void initCaissePaiement() {
354
        calculatorValue = TicketCellRenderer.centsToString(controller.getPaiementSelected().getMontantInCents());
363
        this.calculatorValue = TicketCellRenderer.centsToString(this.controller.getPaiementSelected().getMontantInCents());
355
        init = true;
364
        this.init = true;
356
        mode = ' ';
365
        this.mode = ' ';
357
    }
366
    }
358
 
367
 
359
    @Override
368
    @Override
360
    public void mouseClicked(MouseEvent e) {
369
    public void mouseClicked(MouseEvent e) {
361
        // Nothing to do here
370
        // Nothing to do here
Line 382... Line 391...
382
        if (c != '?') {
391
        if (c != '?') {
383
            handleCharacter(c);
392
            handleCharacter(c);
384
        } else {
393
        } else {
385
            Paiement p = getPaiementFrom(e.getY());
394
            Paiement p = getPaiementFrom(e.getY());
386
            if (p != null) {
395
            if (p != null) {
387
                if (e.getX() > this.getWidth() - 68 && p.getType() != Paiement.ESPECES && controller.canAddPaiement(p.getType())) {
396
                if (e.getX() > this.getWidth() - 68 && p.getType() != Paiement.ESPECES && this.controller.canAddPaiement(p.getType())) {
388
                    p = new Paiement(p.getType());
397
                    p = new Paiement(p.getType());
389
                    controller.addPaiement(p);
398
                    this.controller.addPaiement(p);
390
                }
399
                }
391
                controller.autoFillPaiement(p);
400
                this.controller.autoFillPaiement(p);
392
                this.calculatorValue = TicketCellRenderer.centsToString(p.getMontantInCents());
401
                this.calculatorValue = TicketCellRenderer.centsToString(p.getMontantInCents());
393
                if (p.getType() == Paiement.ESPECES) {
402
                if (p.getType() == Paiement.ESPECES) {
394
                    try {
403
                    try {
395
                        controller.openDrawer();
404
                        this.controller.openDrawer();
396
                    } catch (Throwable ex) {
405
                    } catch (Throwable ex) {
397
                        JOptionPane.showMessageDialog(PaiementPanel.this, "Ouverture du tiroir caisse impossible");
406
                        JOptionPane.showMessageDialog(PaiementPanel.this, "Ouverture du tiroir caisse impossible");
398
                    }
407
                    }
399
                } else if (p.getType() == Paiement.CB) {
408
                } else if (p.getType() == Paiement.CB) {
400
                    controller.sendCBRequest(p);
409
                    this.controller.sendCBRequest(p);
401
                }
410
                }
402
 
411
 
403
            }
412
            }
404
            controller.setPaiementSelected(p);
413
            this.controller.setPaiementSelected(p);
405
        }
414
        }
406
    }
415
    }
407
 
416
 
408
    public void lcdPressed() {
417
    public void lcdPressed() {
409
        System.err.println("PaiementPanel.lcdPressed()");
418
        System.err.println("PaiementPanel.lcdPressed()");
410
        final Article articleSelected = controller.getArticleSelected();
419
        final TicketItem articleSelected = this.controller.getTicketItemSelected();
411
        if (articleSelected != null) {
420
        if (articleSelected != null) {
412
            controller.openPriceEditor(articleSelected);
421
            this.controller.openPriceEditor(articleSelected);
413
        }
422
        }
414
        repaint();
423
        repaint();
415
 
424
 
416
    }
425
    }
417
 
426
 
418
    private void handleCharacter(char c) {
427
    private void handleCharacter(char c) {
419
        System.out.println("Handle: " + c);
428
        System.out.println("Handle: " + c);
420
        if (c == '?')
429
        if (c == '?')
421
            return;
430
            return;
422
        final Article article = controller.getArticleSelected();
431
        final TicketItem article = this.controller.getTicketItemSelected();
423
 
432
 
424
        if (c == '+' || c == '-' || c == '*') {
433
        if (c == '+' || c == '-' || c == '*') {
425
            mode = c;
434
            this.mode = c;
426
 
435
 
427
            repaint();
436
            repaint();
428
            return;
437
            return;
429
        }
438
        }
430
 
439
 
431
        if (article != null) {
440
        if (article != null) {
432
            // Changement de quantité
441
            // Changement de quantité
433
            if (c == 'c' || c == '/') {
442
            if (c == 'c' || c == '/') {
434
                System.out.println("Clear quantité");
443
                System.out.println("Clear quantité");
435
                mode = ' ';
444
                this.mode = ' ';
436
                controller.clearArticle(article);
445
                this.controller.removeTicketItem(article);
437
            } else if (c == '=' || c == '\n') {
446
            } else if (c == '=' || c == '\n') {
438
                if (!init) {
447
                if (!this.init) {
439
                    int v = Integer.parseInt(calculatorValue);
448
                    BigDecimal v = new BigDecimal(this.calculatorValue);
440
                    if (mode == ' ') {
449
                    if (this.mode == ' ') {
441
                        controller.setArticleCount(article, v);
450
                        article.setQty(v);
442
                    } else if (mode == '+') {
451
                    } else if (this.mode == '+') {
443
                        controller.setArticleCount(article, controller.getItemCount(article) + v);
452
                        article.setQty(article.getQty().add(v));
444
                    } else if (mode == '-') {
453
                    } else if (this.mode == '-') {
445
                        controller.setArticleCount(article, controller.getItemCount(article) - v);
454
                        article.setQty(article.getQty().subtract(v));
446
                    } else if (mode == '*') {
455
                    } else if (this.mode == '*') {
447
                        controller.setArticleCount(article, controller.getItemCount(article) * v);
456
                        article.setQty(article.getQty().multiply(v));
448
                    }
457
                    }
-
 
458
                    // Reselect to fire a refresh
-
 
459
                    this.controller.setTicketItemSelected(article);
449
                }
460
                }
450
                initCaisseArticle();
461
                initCaisseArticle();
451
            } else if (Character.isDigit(c)) {
462
            } else if (Character.isDigit(c)) {
452
                if (init) {
463
                if (this.init) {
453
                    calculatorValue = "";
464
                    this.calculatorValue = "";
454
                    init = false;
465
                    this.init = false;
-
 
466
                }
-
 
467
                if (this.calculatorValue.length() < 8) {
-
 
468
                    this.calculatorValue += c;
455
                }
469
                }
456
                if (calculatorValue.length() < 8) {
-
 
457
                    calculatorValue += c;
-
 
458
                }
470
            }
-
 
471
            if (article.getArticle().getSalesUnit() != null && c == '.' && (this.calculatorValue.indexOf('.') < 0)) {
-
 
472
                this.calculatorValue += ".";
459
            }
473
            }
460
 
474
 
461
        } else {
475
        } else {
462
            final Paiement paiement = controller.getPaiementSelected();
476
            final Paiement paiement = this.controller.getPaiementSelected();
463
            if (paiement != null) {
477
            if (paiement != null) {
464
                // Changement de paiement
478
                // Changement de paiement
465
                if (c == 'c' || c == '/') {
479
                if (c == 'c' || c == '/') {
466
                    System.out.println("Clear paiement");
480
                    System.out.println("Clear paiement");
467
                    mode = ' ';
481
                    this.mode = ' ';
468
                    controller.clearPaiement(paiement);
482
                    this.controller.clearPaiement(paiement);
469
                } else if (c == '.' && (calculatorValue.indexOf('.') < 0)) {
483
                } else if (c == '.' && (this.calculatorValue.indexOf('.') < 0)) {
470
                    calculatorValue += ".";
484
                    this.calculatorValue += ".";
471
                } else if (c == '=' || c == '\n') {
485
                } else if (c == '=' || c == '\n') {
472
                    if (!init) {
486
                    if (!this.init) {
473
                        int v = getCentsFrom(this.calculatorValue);
487
                        int v = getCentsFrom(this.calculatorValue);
474
                        if (mode == ' ') {
488
                        if (this.mode == ' ') {
475
                            controller.setPaiementValue(paiement, v);
489
                            this.controller.setPaiementValue(paiement, v);
476
                        } else if (mode == '+') {
490
                        } else if (this.mode == '+') {
477
                            controller.setPaiementValue(paiement, paiement.getMontantInCents() + v);
491
                            this.controller.setPaiementValue(paiement, paiement.getMontantInCents() + v);
478
                        } else if (mode == '-') {
492
                        } else if (this.mode == '-') {
479
                            controller.setPaiementValue(paiement, paiement.getMontantInCents() - v);
493
                            this.controller.setPaiementValue(paiement, paiement.getMontantInCents() - v);
480
                        } else if (mode == '*') {
494
                        } else if (this.mode == '*') {
481
                            controller.setPaiementValue(paiement, paiement.getMontantInCents() * v);
495
                            this.controller.setPaiementValue(paiement, paiement.getMontantInCents() * v);
482
                        }
496
                        }
483
                    }
497
                    }
484
                    initCaissePaiement();
498
                    initCaissePaiement();
485
                    controller.setPaiementSelected(null);
499
                    this.controller.setPaiementSelected(null);
486
                } else if (Character.isDigit(c)) {
500
                } else if (Character.isDigit(c)) {
487
                    if (init) {
501
                    if (this.init) {
488
                        calculatorValue = "";
502
                        this.calculatorValue = "";
489
                        init = false;
503
                        this.init = false;
490
                    }
504
                    }
491
                    if (calculatorValue.length() < 9) {
505
                    if (this.calculatorValue.length() < 9) {
492
                        int i = calculatorValue.indexOf('.');
506
                        int i = this.calculatorValue.indexOf('.');
493
                        if (i < 0 || (calculatorValue.length() - i < 3)) {
507
                        if (i < 0 || (this.calculatorValue.length() - i < 3)) {
494
                            calculatorValue += c;
508
                            this.calculatorValue += c;
495
                        }
509
                        }
496
                    }
510
                    }
497
                }
511
                }
498
            }
512
            }
499
        }
513
        }
Line 532... Line 546...
532
        // Nothing to do here
546
        // Nothing to do here
533
    }
547
    }
534
 
548
 
535
    private Paiement getPaiementFrom(int y) {
549
    private Paiement getPaiementFrom(int y) {
536
        int index = (y - PAYMENT_POS_Y) / PAYMENT_LINE_HEIGHT;
550
        int index = (y - PAYMENT_POS_Y) / PAYMENT_LINE_HEIGHT;
537
        if (index < controller.getPaiements().size() && index >= 0) {
551
        if (index < this.controller.getPaiements().size() && index >= 0) {
538
            return controller.getPaiements().get(index);
552
            return this.controller.getPaiements().get(index);
539
        }
553
        }
540
        return null;
554
        return null;
541
    }
555
    }
542
 
556
 
543
    @Override
557
    @Override