OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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