OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Rev 177 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 174
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
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;
23
import java.awt.Graphics2D;
24
import java.awt.Graphics2D;
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
 
34
public class PaiementPanel extends JPanel implements CaisseListener, MouseListener, BarcodeListener {
36
public class PaiementPanel extends JPanel implements CaisseListener, MouseListener, BarcodeListener {
35
    private static final int PAYMENT_POS_Y = 50;
37
    private static final int PAYMENT_POS_Y = 50;
36
    private static final int PAYMENT_LINE_HEIGHT = 60;
38
    private static final int PAYMENT_LINE_HEIGHT = 60;
37
 
39
 
38
    private CaisseControler controller;
40
    private CaisseControler controller;
39
    private String calculatorValue = "";
41
    private String calculatorValue = "";
40
    int calcHeight = 5 * 68;
42
    int calcHeight = 5 * 68;
41
    int calcWidth = 4 * 69;
43
    int calcWidth = 4 * 69;
42
    int BUTTON_SIZE = 64;
44
    int BUTTON_SIZE = 64;
43
    /**
45
    /**
44
     * Mode '+' ajout d'une quantité '*' multiplication '-' soustraction ' ' remplacement
46
     * Mode '+' ajout d'une quantité '*' multiplication '-' soustraction ' ' remplacement
45
     */
47
     */
46
    private char mode = ' ';
48
    private char mode = ' ';
47
    private boolean init = true;
49
    private boolean init = true;
48
 
50
 
49
    public PaiementPanel(CaisseControler controller) {
51
    public PaiementPanel(CaisseControler controller) {
50
        this.controller = controller;
52
        this.controller = controller;
51
        this.controller.addCaisseListener(this);
53
        this.controller.addCaisseListener(this);
52
        this.controller.addBarcodeListener(this);
54
        this.controller.addBarcodeListener(this);
53
        this.setOpaque(false);
55
        this.setOpaque(false);
54
 
56
 
55
        this.addMouseListener(this);
57
        this.addMouseListener(this);
56
 
58
 
57
        this.setLayout(null);
59
        this.setLayout(null);
58
        StatusBar st = new StatusBar();
60
        StatusBar st = new StatusBar();
59
        st.setTitle("Règlement");
61
        st.setTitle("Règlement");
60
        st.setLocation(0, 0);
62
        st.setLocation(0, 0);
61
        st.setSize(320, (int) st.getPreferredSize().getHeight());
63
        st.setSize(320, (int) st.getPreferredSize().getHeight());
62
 
64
 
63
        this.add(st);
65
        this.add(st);
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) {
82
                drawMontant(g, "Espèces", p, 300 - 45, y, false);
86
                drawMontant(g, "Espèces", p, 300 - 45, y, false);
83
            } else if (p.getType() == Paiement.SOLDE) {
87
            } else if (p.getType() == Paiement.SOLDE) {
84
                drawMontant(g, "Solde", p, 300 - 45, y, false);
88
                drawMontant(g, "Solde", p, 300 - 45, y, false);
85
            }
89
            }
86
            y += PAYMENT_LINE_HEIGHT;
90
            y += PAYMENT_LINE_HEIGHT;
87
        }
91
        }
88
 
92
 
89
        drawKey(g2, "0", 0, 0, 2, 1, CaissePanel.DARK_BLUE);
93
        drawKey(g2, "0", 0, 0, 2, 1, CaissePanel.DARK_BLUE);
90
        drawKey(g2, ".", 2, 0, 1, 1, CaissePanel.DARK_BLUE);
94
        drawKey(g2, ".", 2, 0, 1, 1, CaissePanel.DARK_BLUE);
91
 
95
 
92
        drawKey(g2, "1", 0, 1, 1, 1, CaissePanel.DARK_BLUE);
96
        drawKey(g2, "1", 0, 1, 1, 1, CaissePanel.DARK_BLUE);
93
        drawKey(g2, "2", 1, 1, 1, 1, CaissePanel.DARK_BLUE);
97
        drawKey(g2, "2", 1, 1, 1, 1, CaissePanel.DARK_BLUE);
94
        drawKey(g2, "3", 2, 1, 1, 1, CaissePanel.DARK_BLUE);
98
        drawKey(g2, "3", 2, 1, 1, 1, CaissePanel.DARK_BLUE);
95
        drawKey(g2, "=", 3, 1, 1, 2, CaissePanel.DARK_BLUE);
99
        drawKey(g2, "=", 3, 1, 1, 2, CaissePanel.DARK_BLUE);
96
 
100
 
97
        drawKey(g2, "4", 0, 2, 1, 1, CaissePanel.DARK_BLUE);
101
        drawKey(g2, "4", 0, 2, 1, 1, CaissePanel.DARK_BLUE);
98
        drawKey(g2, "5", 1, 2, 1, 1, CaissePanel.DARK_BLUE);
102
        drawKey(g2, "5", 1, 2, 1, 1, CaissePanel.DARK_BLUE);
99
        drawKey(g2, "6", 2, 2, 1, 1, CaissePanel.DARK_BLUE);
103
        drawKey(g2, "6", 2, 2, 1, 1, CaissePanel.DARK_BLUE);
100
 
104
 
101
        drawKey(g2, "7", 0, 3, 1, 1, CaissePanel.DARK_BLUE);
105
        drawKey(g2, "7", 0, 3, 1, 1, CaissePanel.DARK_BLUE);
102
        drawKey(g2, "8", 1, 3, 1, 1, CaissePanel.DARK_BLUE);
106
        drawKey(g2, "8", 1, 3, 1, 1, CaissePanel.DARK_BLUE);
103
        drawKey(g2, "9", 2, 3, 1, 1, CaissePanel.DARK_BLUE);
107
        drawKey(g2, "9", 2, 3, 1, 1, CaissePanel.DARK_BLUE);
104
        drawKey(g2, "+", 3, 3, 1, 2, CaissePanel.DARK_BLUE);
108
        drawKey(g2, "+", 3, 3, 1, 2, CaissePanel.DARK_BLUE);
105
 
109
 
106
        drawKey(g2, "C", 0, 4, 2, 1, CaissePanel.LIGHT_BLUE);
110
        drawKey(g2, "C", 0, 4, 2, 1, CaissePanel.LIGHT_BLUE);
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);
117
        g2.fillRect(3 + col * 69, this.getHeight() - (68 * (row + 1)) + 2, 69 * w - 5, 68 * h - 4);
122
        g2.fillRect(3 + col * 69, this.getHeight() - (68 * (row + 1)) + 2, 69 * w - 5, 68 * h - 4);
118
        // label
123
        // label
119
        g2.setColor(Color.WHITE);
124
        g2.setColor(Color.WHITE);
120
        g2.setFont(g2.getFont().deriveFont(32f));
125
        g2.setFont(g2.getFont().deriveFont(32f));
121
        final int width2 = (int) g2.getFontMetrics().getStringBounds(string, g2).getWidth();
126
        final int width2 = (int) g2.getFontMetrics().getStringBounds(string, g2).getWidth();
122
        int x = -width2 / 2 + (69) / 2 + col * 69;
127
        int x = -width2 / 2 + (69) / 2 + col * 69;
123
        int y = this.getHeight() - (row * 68 + 20);
128
        int y = this.getHeight() - (row * 68 + 20);
124
 
129
 
125
        g2.drawString(string, x, y);
130
        g2.drawString(string, x, y);
126
    }
131
    }
127
 
132
 
128
    public int getLCDY() {
133
    public int getLCDY() {
129
        return this.getHeight() - this.calcHeight - 66;
134
        return this.getHeight() - this.calcHeight - 66;
130
    }
135
    }
131
 
136
 
132
    public int getLCDHeight() {
137
    public int getLCDHeight() {
133
        return 64;
138
        return 64;
134
    }
139
    }
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 ";
169
                if (paiement.getType() == Paiement.CB) {
174
                if (paiement.getType() == Paiement.CB) {
170
                    str += " CB";
175
                    str += " CB";
171
                } else if (paiement.getType() == Paiement.ESPECES) {
176
                } else if (paiement.getType() == Paiement.ESPECES) {
172
                    str += " en espèces";
177
                    str += " en espèces";
173
                } else if (paiement.getType() == Paiement.CHEQUE) {
178
                } else if (paiement.getType() == Paiement.CHEQUE) {
174
                    str += " par chèque";
179
                    str += " par chèque";
175
                } else if (paiement.getType() == Paiement.SOLDE) {
180
                } else if (paiement.getType() == Paiement.SOLDE) {
176
                    str += " depuis solde";
181
                    str += " depuis solde";
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;
188
        switch (yy) {
193
        switch (yy) {
189
        case 0:
194
        case 0:
190
            if (xx == 0) {
195
            if (xx == 0) {
191
                return '0';
196
                return '0';
192
            } else if (xx == 1) {
197
            } else if (xx == 1) {
193
                return '0';
198
                return '0';
194
            } else if (xx == 2) {
199
            } else if (xx == 2) {
195
                return '.';
200
                return '.';
196
            } else if (xx == 3) {
201
            } else if (xx == 3) {
197
                return '=';
202
                return '=';
198
            } else {
203
            } else {
199
                break;
204
                break;
200
            }
205
            }
201
        case 1:
206
        case 1:
202
            if (xx == 0) {
207
            if (xx == 0) {
203
                return '1';
208
                return '1';
204
            } else if (xx == 1) {
209
            } else if (xx == 1) {
205
                return '2';
210
                return '2';
206
            } else if (xx == 2) {
211
            } else if (xx == 2) {
207
                return '3';
212
                return '3';
208
            } else if (xx == 3) {
213
            } else if (xx == 3) {
209
                return '=';
214
                return '=';
210
            } else {
215
            } else {
211
                break;
216
                break;
212
            }
217
            }
213
        case 2:
218
        case 2:
214
            if (xx == 0) {
219
            if (xx == 0) {
215
                return '4';
220
                return '4';
216
            } else if (xx == 1) {
221
            } else if (xx == 1) {
217
                return '5';
222
                return '5';
218
            } else if (xx == 2) {
223
            } else if (xx == 2) {
219
                return '6';
224
                return '6';
220
            } else if (xx == 3) {
225
            } else if (xx == 3) {
221
                return '+';
226
                return '+';
222
            } else {
227
            } else {
223
                break;
228
                break;
224
            }
229
            }
225
        case 3:
230
        case 3:
226
            if (xx == 0) {
231
            if (xx == 0) {
227
                return '7';
232
                return '7';
228
            } else if (xx == 1) {
233
            } else if (xx == 1) {
229
                return '8';
234
                return '8';
230
            } else if (xx == 2) {
235
            } else if (xx == 2) {
231
                return '9';
236
                return '9';
232
            } else if (xx == 3) {
237
            } else if (xx == 3) {
233
                return '+';
238
                return '+';
234
            } else {
239
            } else {
235
                break;
240
                break;
236
            }
241
            }
237
        case 4:
242
        case 4:
238
            if (xx == 0) {
243
            if (xx == 0) {
239
                return 'c';
244
                return 'c';
240
            } else if (xx == 1) {
245
            } else if (xx == 1) {
241
                return 'c';
246
                return 'c';
242
            } else if (xx == 2) {
247
            } else if (xx == 2) {
243
                return '*';
248
                return '*';
244
            } else if (xx == 3) {
249
            } else if (xx == 3) {
245
                return '-';
250
                return '-';
246
            } else {
251
            } else {
247
                break;
252
                break;
248
            }
253
            }
249
 
254
 
250
        }
255
        }
251
        return '?';
256
        return '?';
252
    }
257
    }
253
 
258
 
254
    private void drawMontant(Graphics g, String label, Paiement p, int x, int y, boolean showAdd) {
259
    private void drawMontant(Graphics g, String label, Paiement p, int x, int y, boolean showAdd) {
255
        y = y + 36;
260
        y = y + 36;
256
        Graphics2D g2 = (Graphics2D) g;
261
        Graphics2D g2 = (Graphics2D) g;
257
 
262
 
258
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
263
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
259
        int cents = p.getCents();
264
        int cents = p.getCents();
260
        int euros = p.getEuros();
265
        int euros = p.getEuros();
261
        // Background
266
        // Background
262
        g.setColor(new Color(240, 240, 240));
267
        g.setColor(new Color(240, 240, 240));
263
        g.fillRect(3, y - 36, this.getWidth() - 5, 44);
268
        g.fillRect(3, y - 36, this.getWidth() - 5, 44);
264
 
269
 
265
        g.setColor(CaissePanel.DARK_BLUE);
270
        g.setColor(CaissePanel.DARK_BLUE);
266
 
271
 
267
        g.fillRect(3, y - 36, 95, 44);
272
        g.fillRect(3, y - 36, 95, 44);
268
 
273
 
269
        if (showAdd) {
274
        if (showAdd) {
270
            g.setColor(CaissePanel.DARK_BLUE);
275
            g.setColor(CaissePanel.DARK_BLUE);
271
            g.fillRect(this.getWidth() - 46, y - 36, 44, 44);
276
            g.fillRect(this.getWidth() - 46, y - 36, 44, 44);
272
        }
277
        }
273
        // Label
278
        // Label
274
        g.setFont(g.getFont().deriveFont(20f));
279
        g.setFont(g.getFont().deriveFont(20f));
275
        g.setFont(g.getFont().deriveFont(Font.BOLD));
280
        g.setFont(g.getFont().deriveFont(Font.BOLD));
276
 
281
 
277
        g.setColor(Color.WHITE);
282
        g.setColor(Color.WHITE);
278
        g.drawString(label, 10, y - 8);
283
        g.drawString(label, 10, y - 8);
279
        if (showAdd) {
284
        if (showAdd) {
280
            g.drawString("+", this.getWidth() - 32, y - 8);
285
            g.drawString("+", this.getWidth() - 32, y - 8);
281
        }
286
        }
282
        // Cents
287
        // Cents
283
        g.setColor(Color.GRAY);
288
        g.setColor(Color.GRAY);
284
        String sCents = String.valueOf(cents);
289
        String sCents = String.valueOf(cents);
285
        if (sCents.length() < 2) {
290
        if (sCents.length() < 2) {
286
            sCents = "0" + sCents;
291
            sCents = "0" + sCents;
287
        }
292
        }
288
        g.setFont(getFont().deriveFont(18f));
293
        g.setFont(getFont().deriveFont(18f));
289
        Rectangle2D r1 = g.getFontMetrics().getStringBounds(sCents, g2);
294
        Rectangle2D r1 = g.getFontMetrics().getStringBounds(sCents, g2);
290
        g.drawString(sCents, (int) (x - r1.getWidth()), y);
295
        g.drawString(sCents, (int) (x - r1.getWidth()), y);
291
        // Euros
296
        // Euros
292
        g.setFont(g.getFont().deriveFont(36f));
297
        g.setFont(g.getFont().deriveFont(36f));
293
        g.setFont(g.getFont().deriveFont(Font.BOLD));
298
        g.setFont(g.getFont().deriveFont(Font.BOLD));
294
        g.setColor(Color.BLACK);
299
        g.setColor(Color.BLACK);
295
        String sEuros = String.valueOf(euros) + ".";
300
        String sEuros = String.valueOf(euros) + ".";
296
        Rectangle2D r2 = g.getFontMetrics().getStringBounds(sEuros, g2);
301
        Rectangle2D r2 = g.getFontMetrics().getStringBounds(sEuros, g2);
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
362
    }
371
    }
363
 
372
 
364
    @Override
373
    @Override
365
    public void mouseEntered(MouseEvent e) {
374
    public void mouseEntered(MouseEvent e) {
366
        // Nothing to do here
375
        // Nothing to do here
367
    }
376
    }
368
 
377
 
369
    @Override
378
    @Override
370
    public void mouseExited(MouseEvent e) {
379
    public void mouseExited(MouseEvent e) {
371
        // Nothing to do here
380
        // Nothing to do here
372
    }
381
    }
373
 
382
 
374
    @Override
383
    @Override
375
    public void mousePressed(MouseEvent e) {
384
    public void mousePressed(MouseEvent e) {
376
        if (e.getY() > getLCDY() && e.getY() < (getLCDY() + getLCDHeight())) {
385
        if (e.getY() > getLCDY() && e.getY() < (getLCDY() + getLCDHeight())) {
377
            lcdPressed();
386
            lcdPressed();
378
            return;
387
            return;
379
        }
388
        }
380
 
389
 
381
        char c = getToucheFrom(e.getX(), e.getY());
390
        char c = getToucheFrom(e.getX(), e.getY());
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
        }
500
        repaint();
514
        repaint();
501
    }
515
    }
502
 
516
 
503
    private int getCentsFrom(String str) {
517
    private int getCentsFrom(String str) {
504
        int i = str.indexOf('.');
518
        int i = str.indexOf('.');
505
        if (i >= 0) {
519
        if (i >= 0) {
506
            String euros = str.substring(0, i);
520
            String euros = str.substring(0, i);
507
            String cents = str.substring(i + 1);
521
            String cents = str.substring(i + 1);
508
            if (cents.length() == 1) {
522
            if (cents.length() == 1) {
509
                cents += "0";
523
                cents += "0";
510
            }
524
            }
511
            int e = 0;
525
            int e = 0;
512
            if (euros.length() > 0) {
526
            if (euros.length() > 0) {
513
                e = Integer.parseInt(euros);
527
                e = Integer.parseInt(euros);
514
            }
528
            }
515
            int c = 0;
529
            int c = 0;
516
            if (cents.length() > 0) {
530
            if (cents.length() > 0) {
517
                c = Integer.parseInt(cents);
531
                c = Integer.parseInt(cents);
518
            }
532
            }
519
            return e * 100 + c;
533
            return e * 100 + c;
520
        }
534
        }
521
        return Integer.parseInt(str) * 100;
535
        return Integer.parseInt(str) * 100;
522
 
536
 
523
    }
537
    }
524
 
538
 
525
    @Override
539
    @Override
526
    public void mouseReleased(MouseEvent e) {
540
    public void mouseReleased(MouseEvent e) {
527
        // Nothing to do here
541
        // Nothing to do here
528
    }
542
    }
529
 
543
 
530
    @Override
544
    @Override
531
    public void barcodeRead(String code) {
545
    public void barcodeRead(String code) {
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
544
    public void keyReceived(KeyEvent e) {
558
    public void keyReceived(KeyEvent e) {
545
        if (e.getID() == KeyEvent.KEY_TYPED) {
559
        if (e.getID() == KeyEvent.KEY_TYPED) {
546
            System.out.println("PaiementPanel.keyPressed()" + e.getKeyChar());
560
            System.out.println("PaiementPanel.keyPressed()" + e.getKeyChar());
547
            handleCharacter(e.getKeyChar());
561
            handleCharacter(e.getKeyChar());
548
        }
562
        }
549
 
563
 
550
    }
564
    }
551
 
565
 
552
}
566
}