OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
142 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
142 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.pos.ui;
15
 
16
import org.openconcerto.erp.core.common.ui.NumericTextField;
17
import org.openconcerto.erp.core.sales.pos.model.Article;
174 ilm 18
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
142 ilm 19
 
20
import java.awt.Color;
21
import java.awt.GridBagConstraints;
22
import java.awt.GridBagLayout;
151 ilm 23
import java.awt.GridLayout;
142 ilm 24
import java.awt.Insets;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.math.BigDecimal;
28
 
29
import javax.swing.ButtonGroup;
30
import javax.swing.JPanel;
151 ilm 31
import javax.swing.SwingUtilities;
142 ilm 32
import javax.swing.event.DocumentEvent;
33
import javax.swing.event.DocumentListener;
34
 
35
public class PriceEditorPanel extends JPanel {
174 ilm 36
    private final transient TicketItem item;
151 ilm 37
    private final POSLabel labelPrice;
38
    private final POSRadioButton rHT;
39
    private final POSRadioButton rTTC;
40
    private final POSRadioButton rDiscountPercent;
41
    private final POSRadioButton rDiscount;
42
    private final NumericTextField htTextField;
43
    private final NumericTextField ttcTextField;
44
    private final NumericTextField discountPercentTextField;
182 ilm 45
    // Remise HT
151 ilm 46
    private final NumericTextField discountTextField;
47
    private final NumericKeypadPanel keyPad;
142 ilm 48
 
174 ilm 49
    public PriceEditorPanel(final CaisseFrame caisseFrame, final TicketItem item) {
50
        this.item = item;
142 ilm 51
        this.setBackground(Color.WHITE);
52
        this.setOpaque(true);
53
        this.setLayout(new GridBagLayout());
54
        final GridBagConstraints c = new GridBagConstraints();
55
        c.fill = GridBagConstraints.HORIZONTAL;
56
        c.anchor = GridBagConstraints.EAST;
57
        c.weightx = 0;
151 ilm 58
        c.weighty = 1;
142 ilm 59
        c.gridx = 0;
60
        c.gridy = 0;
61
        c.insets = new Insets(20, 20, 30, 20);
62
        // Line 1
63
        c.gridwidth = 2;
64
        POSLabel title = new POSLabel("Modification du prix de vente");
65
        this.add(title, c);
66
        // Line 2
67
        c.gridy++;
68
        c.gridwidth = 1;
174 ilm 69
        this.rTTC = new POSRadioButton("prix TTC");
70
        this.rTTC.setSelected(true);
151 ilm 71
        c.weightx = 0;
174 ilm 72
        this.add(this.rTTC, c);
73
        this.ttcTextField = new NumericTextField();
74
        this.ttcTextField.setFont(title.getFont());
182 ilm 75
        this.ttcTextField.setValue(item.getArticle().getPriceWithTax(BigDecimal.ONE, false));
174 ilm 76
        this.ttcTextField.requestFocusInWindow();
151 ilm 77
        c.gridx++;
78
        c.weightx = 1;
174 ilm 79
        this.add(this.ttcTextField, c);
151 ilm 80
        // Line 3
81
        c.gridy++;
174 ilm 82
        this.rHT = new POSRadioButton("prix HT");
151 ilm 83
        c.gridx = 0;
142 ilm 84
        c.weightx = 0;
174 ilm 85
        this.add(this.rHT, c);
86
        this.htTextField = new NumericTextField();
182 ilm 87
        this.htTextField.setValue(item.getArticle().getPriceWithoutTax(BigDecimal.ONE));
174 ilm 88
        this.htTextField.setFont(title.getFont());
142 ilm 89
        c.gridx++;
90
        c.weightx = 1;
174 ilm 91
        this.add(this.htTextField, c);
142 ilm 92
        // Line 4
93
        c.gridy++;
174 ilm 94
        this.rDiscountPercent = new POSRadioButton("remise en %");
142 ilm 95
        c.gridx = 0;
96
        c.weightx = 0;
174 ilm 97
        this.add(this.rDiscountPercent, c);
98
        this.discountPercentTextField = new NumericTextField();
99
        this.discountPercentTextField.setValue(BigDecimal.ZERO);
100
        this.discountPercentTextField.setFont(title.getFont());
142 ilm 101
        c.gridx++;
102
        c.weightx = 1;
174 ilm 103
        this.add(this.discountPercentTextField, c);
142 ilm 104
        // Line 5
174 ilm 105
        this.rDiscount = new POSRadioButton("remise HT");
142 ilm 106
        c.gridx = 0;
107
        c.weightx = 0;
108
        c.gridy++;
174 ilm 109
        this.add(this.rDiscount, c);
110
        this.discountTextField = new NumericTextField();
111
        this.discountTextField.setValue(BigDecimal.ZERO);
112
        this.discountTextField.setFont(title.getFont());
142 ilm 113
        c.gridx++;
114
        c.weightx = 1;
174 ilm 115
        this.add(this.discountTextField, c);
142 ilm 116
 
117
        final ButtonGroup group = new ButtonGroup();
174 ilm 118
        group.add(this.rHT);
119
        group.add(this.rTTC);
120
        group.add(this.rDiscountPercent);
121
        group.add(this.rDiscount);
142 ilm 122
        //
123
        //
124
        c.gridy++;
125
        c.gridx = 0;
126
        c.gridwidth = 2;
127
        final POSLabel labelPriceOld = new POSLabel("Ancien Prix : ");
182 ilm 128
        labelPriceOld.setText("Ancien Prix : " + TicketCellRenderer.toString(item.getArticle().getPriceWithTax(BigDecimal.ONE)) + "€ TTC, "
129
                + TicketCellRenderer.toString(item.getArticle().getPriceWithoutTax(BigDecimal.ONE)) + "€ HT");
142 ilm 130
        this.add(labelPriceOld, c);
131
 
132
        c.gridy++;
133
        c.gridx = 0;
174 ilm 134
        this.labelPrice = new POSLabel("Nouveau Prix : ");
135
        this.add(this.labelPrice, c);
142 ilm 136
 
137
        c.gridy++;
138
        c.gridx = 0;
139
        c.fill = GridBagConstraints.NONE;
140
        c.anchor = GridBagConstraints.SOUTHEAST;
141
 
151 ilm 142
        final JPanel buttons = new JPanel(new GridLayout(1, 2, 10, 0));
142 ilm 143
        buttons.setOpaque(false);
151 ilm 144
        POSButton bCancel = new POSButton("Annuler");
145
        buttons.add(bCancel, c);
142 ilm 146
        POSButton bApply = new POSButton("Appliquer");
147
        buttons.add(bApply, c);
148
        bApply.addActionListener(new ActionListener() {
149
 
150
            @Override
151
            public void actionPerformed(ActionEvent e) {
182 ilm 152
                if (rHT.isSelected() || rTTC.isSelected()) {
153
                    caisseFrame.getControler().setArticleHT(item, getHTFromUI());
154
                } else if (rDiscountPercent.isSelected()) {
155
                    item.getArticle().setDiscountPct(discountPercentTextField.getValue().divide(new BigDecimal(100)));
156
                } else if (rDiscount.isSelected()) {
157
                    // Remise HT
158
                    final BigDecimal priceWithoutTax = item.getArticle().getPriceWithoutTax(BigDecimal.ONE, false);
159
                    BigDecimal pct = BigDecimal.ONE.subtract((priceWithoutTax.subtract(discountTextField.getValue())).divide(priceWithoutTax));
160
                    item.getArticle().setDiscountPct(pct);
161
                }
142 ilm 162
                caisseFrame.showCaisse();
163
            }
164
        });
165
        bCancel.addActionListener(new ActionListener() {
166
 
167
            @Override
168
            public void actionPerformed(ActionEvent e) {
169
                caisseFrame.showCaisse();
170
            }
171
        });
172
 
173
        this.add(buttons, c);
151 ilm 174
 
175
        c.anchor = GridBagConstraints.CENTER;
176
        c.weightx = 0;
177
        c.weighty = 0;
178
        c.gridx = 3;
179
        c.gridy = 2;
180
        c.insets = new Insets(20, 20, 30, 20);
181
        // Line 1
182
        c.gridheight = 5;
174 ilm 183
        this.keyPad = new NumericKeypadPanel(this.ttcTextField);
184
        this.add(this.keyPad, c);
151 ilm 185
 
182 ilm 186
        updatePrice(item.getArticle().getPriceWithoutTax(BigDecimal.ONE));
142 ilm 187
        updateTextFields();
188
        //
189
        final ActionListener listenerRadio = new ActionListener() {
190
 
191
            @Override
192
            public void actionPerformed(ActionEvent e) {
193
                updatePrice(getHTFromUI());
194
 
195
                updateTextFields();
196
 
197
            }
198
        };
199
        this.rDiscount.addActionListener(listenerRadio);
200
        this.rDiscountPercent.addActionListener(listenerRadio);
201
        this.rHT.addActionListener(listenerRadio);
202
        this.rTTC.addActionListener(listenerRadio);
203
 
204
        final DocumentListener docListener = new DocumentListener() {
205
 
206
            @Override
207
            public void removeUpdate(DocumentEvent e) {
208
                changedUpdate(e);
209
 
210
            }
211
 
212
            @Override
213
            public void insertUpdate(DocumentEvent e) {
214
                changedUpdate(e);
215
 
216
            }
217
 
218
            @Override
219
            public void changedUpdate(DocumentEvent e) {
220
                updatePrice(getHTFromUI());
221
            }
222
        };
223
        this.ttcTextField.getDocument().addDocumentListener(docListener);
224
        this.htTextField.getDocument().addDocumentListener(docListener);
225
        this.discountPercentTextField.getDocument().addDocumentListener(docListener);
226
        this.discountTextField.getDocument().addDocumentListener(docListener);
151 ilm 227
 
228
        SwingUtilities.invokeLater(new Runnable() {
229
            public void run() {
230
                updateTextFields();// This will set focus on the text field
231
            }
232
        });
142 ilm 233
    }
234
 
235
    protected BigDecimal getHTFromUI() {
236
        BigDecimal r = null;
237
        try {
238
            if (this.rHT.isSelected()) {
239
                r = this.htTextField.getValue();
240
            } else if (this.rTTC.isSelected()) {
174 ilm 241
                r = Article.computePriceWithoutTax(this.ttcTextField.getValue(), this.item.getArticle().getIdTaxe());
142 ilm 242
            } else if (this.rDiscountPercent.isSelected()) {
182 ilm 243
                r = this.item.getArticle().getPriceWithoutTax(BigDecimal.ONE, false)
244
                        .subtract(this.item.getArticle().getPriceWithoutTax(BigDecimal.ONE).multiply(this.discountPercentTextField.getValue().divide(new BigDecimal(100))));
142 ilm 245
            } else if (this.rDiscount.isSelected()) {
182 ilm 246
                r = this.item.getArticle().getPriceWithoutTax(BigDecimal.ONE, false).subtract(this.discountTextField.getValue());
142 ilm 247
            }
248
        } catch (Exception e) {
249
            e.printStackTrace();
250
        }
251
        if (r == null) {
252
            // fallback if something wrong
182 ilm 253
            r = this.item.getArticle().getPriceWithoutTax(BigDecimal.ONE);
142 ilm 254
        }
255
        return r;
256
    }
257
 
151 ilm 258
    private void updatePrice(BigDecimal ht) {
174 ilm 259
        BigDecimal ttc = Article.computePriceWithTax(ht, this.item.getArticle().getIdTaxe());
260
        this.labelPrice.setText("Nouveau Prix : " + TicketCellRenderer.toString(ttc) + "€ TTC, " + TicketCellRenderer.toString(ht) + "€ HT");
142 ilm 261
    }
262
 
151 ilm 263
    private void updateTextFields() {
142 ilm 264
        this.invalidate();
174 ilm 265
        this.htTextField.setVisible(false);
266
        this.ttcTextField.setVisible(false);
267
        this.discountPercentTextField.setVisible(false);
268
        this.discountTextField.setVisible(false);
269
        if (this.rHT.isSelected()) {
270
            enableTextField(this.htTextField);
271
        } else if (this.rTTC.isSelected()) {
272
            enableTextField(this.ttcTextField);
273
        } else if (this.rDiscountPercent.isSelected()) {
274
            enableTextField(this.discountPercentTextField);
275
        } else if (this.rDiscount.isSelected()) {
276
            enableTextField(this.discountTextField);
142 ilm 277
        }
278
        this.validate();
279
        repaint();
280
    }
151 ilm 281
 
282
    private void enableTextField(NumericTextField field) {
283
        field.setVisible(true);
284
        field.requestFocusInWindow();
285
        this.keyPad.setNumericTextField(field);
286
    }
142 ilm 287
}