Dépôt officiel du code source de l'ERP OpenConcerto
Rev 151 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.pos.ui;
import org.openconcerto.erp.core.common.ui.NumericTextField;
import org.openconcerto.erp.core.sales.pos.model.Article;
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class PriceEditorPanel extends JPanel {
private final transient TicketItem item;
private final POSLabel labelPrice;
private final POSRadioButton rHT;
private final POSRadioButton rTTC;
private final POSRadioButton rDiscountPercent;
private final POSRadioButton rDiscount;
private final NumericTextField htTextField;
private final NumericTextField ttcTextField;
private final NumericTextField discountPercentTextField;
private final NumericTextField discountTextField;
private final NumericKeypadPanel keyPad;
public PriceEditorPanel(final CaisseFrame caisseFrame, final TicketItem item) {
this.item = item;
this.setBackground(Color.WHITE);
this.setOpaque(true);
this.setLayout(new GridBagLayout());
final GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.EAST;
c.weightx = 0;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(20, 20, 30, 20);
// Line 1
c.gridwidth = 2;
POSLabel title = new POSLabel("Modification du prix de vente");
this.add(title, c);
// Line 2
c.gridy++;
c.gridwidth = 1;
this.rTTC = new POSRadioButton("prix TTC");
this.rTTC.setSelected(true);
c.weightx = 0;
this.add(this.rTTC, c);
this.ttcTextField = new NumericTextField();
this.ttcTextField.setFont(title.getFont());
this.ttcTextField.setValue(item.getArticle().getPriceWithTax());
this.ttcTextField.requestFocusInWindow();
c.gridx++;
c.weightx = 1;
this.add(this.ttcTextField, c);
// Line 3
c.gridy++;
this.rHT = new POSRadioButton("prix HT");
c.gridx = 0;
c.weightx = 0;
this.add(this.rHT, c);
this.htTextField = new NumericTextField();
this.htTextField.setValue(item.getArticle().getPriceWithoutTax());
this.htTextField.setFont(title.getFont());
c.gridx++;
c.weightx = 1;
this.add(this.htTextField, c);
// Line 4
c.gridy++;
this.rDiscountPercent = new POSRadioButton("remise en %");
c.gridx = 0;
c.weightx = 0;
this.add(this.rDiscountPercent, c);
this.discountPercentTextField = new NumericTextField();
this.discountPercentTextField.setValue(BigDecimal.ZERO);
this.discountPercentTextField.setFont(title.getFont());
c.gridx++;
c.weightx = 1;
this.add(this.discountPercentTextField, c);
// Line 5
this.rDiscount = new POSRadioButton("remise HT");
c.gridx = 0;
c.weightx = 0;
c.gridy++;
this.add(this.rDiscount, c);
this.discountTextField = new NumericTextField();
this.discountTextField.setValue(BigDecimal.ZERO);
this.discountTextField.setFont(title.getFont());
c.gridx++;
c.weightx = 1;
this.add(this.discountTextField, c);
final ButtonGroup group = new ButtonGroup();
group.add(this.rHT);
group.add(this.rTTC);
group.add(this.rDiscountPercent);
group.add(this.rDiscount);
//
//
c.gridy++;
c.gridx = 0;
c.gridwidth = 2;
final POSLabel labelPriceOld = new POSLabel("Ancien Prix : ");
labelPriceOld.setText(
"Ancien Prix : " + TicketCellRenderer.toString(item.getArticle().getPriceWithTax()) + "€ TTC, " + TicketCellRenderer.toString(item.getArticle().getPriceWithoutTax()) + "€ HT");
this.add(labelPriceOld, c);
c.gridy++;
c.gridx = 0;
this.labelPrice = new POSLabel("Nouveau Prix : ");
this.add(this.labelPrice, c);
c.gridy++;
c.gridx = 0;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.SOUTHEAST;
final JPanel buttons = new JPanel(new GridLayout(1, 2, 10, 0));
buttons.setOpaque(false);
POSButton bCancel = new POSButton("Annuler");
buttons.add(bCancel, c);
POSButton bApply = new POSButton("Appliquer");
buttons.add(bApply, c);
bApply.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
caisseFrame.getControler().setArticleHT(item, getHTFromUI());
caisseFrame.showCaisse();
}
});
bCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
caisseFrame.showCaisse();
}
});
this.add(buttons, c);
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0;
c.weighty = 0;
c.gridx = 3;
c.gridy = 2;
c.insets = new Insets(20, 20, 30, 20);
// Line 1
c.gridheight = 5;
this.keyPad = new NumericKeypadPanel(this.ttcTextField);
this.add(this.keyPad, c);
updatePrice(item.getArticle().getPriceWithoutTax());
updateTextFields();
//
final ActionListener listenerRadio = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updatePrice(getHTFromUI());
updateTextFields();
}
};
this.rDiscount.addActionListener(listenerRadio);
this.rDiscountPercent.addActionListener(listenerRadio);
this.rHT.addActionListener(listenerRadio);
this.rTTC.addActionListener(listenerRadio);
final DocumentListener docListener = new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
updatePrice(getHTFromUI());
}
};
this.ttcTextField.getDocument().addDocumentListener(docListener);
this.htTextField.getDocument().addDocumentListener(docListener);
this.discountPercentTextField.getDocument().addDocumentListener(docListener);
this.discountTextField.getDocument().addDocumentListener(docListener);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updateTextFields();// This will set focus on the text field
}
});
}
protected BigDecimal getHTFromUI() {
BigDecimal r = null;
try {
if (this.rHT.isSelected()) {
r = this.htTextField.getValue();
} else if (this.rTTC.isSelected()) {
r = Article.computePriceWithoutTax(this.ttcTextField.getValue(), this.item.getArticle().getIdTaxe());
} else if (this.rDiscountPercent.isSelected()) {
r = this.item.getArticle().getPriceWithoutTax().subtract(this.item.getArticle().getPriceWithoutTax().multiply(this.discountPercentTextField.getValue().divide(new BigDecimal(100))));
} else if (this.rDiscount.isSelected()) {
r = this.item.getArticle().getPriceWithoutTax().subtract(this.discountTextField.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
if (r == null) {
// fallback if something wrong
r = this.item.getArticle().getPriceWithoutTax();
}
return r;
}
private void updatePrice(BigDecimal ht) {
BigDecimal ttc = Article.computePriceWithTax(ht, this.item.getArticle().getIdTaxe());
this.labelPrice.setText("Nouveau Prix : " + TicketCellRenderer.toString(ttc) + "€ TTC, " + TicketCellRenderer.toString(ht) + "€ HT");
}
private void updateTextFields() {
this.invalidate();
this.htTextField.setVisible(false);
this.ttcTextField.setVisible(false);
this.discountPercentTextField.setVisible(false);
this.discountTextField.setVisible(false);
if (this.rHT.isSelected()) {
enableTextField(this.htTextField);
} else if (this.rTTC.isSelected()) {
enableTextField(this.ttcTextField);
} else if (this.rDiscountPercent.isSelected()) {
enableTextField(this.discountPercentTextField);
} else if (this.rDiscount.isSelected()) {
enableTextField(this.discountTextField);
}
this.validate();
repaint();
}
private void enableTextField(NumericTextField field) {
field.setVisible(true);
field.requestFocusInWindow();
this.keyPad.setNumericTextField(field);
}
}