Dépôt officiel du code source de l'ERP OpenConcerto
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-2019 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.common.ui;
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ComboSQLRequest.KeepMode;
import org.openconcerto.sql.sqlobject.IComboModel;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.sql.view.list.RowValuesTableModel;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.FontUtils;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.CellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;
public class AjoutDeclinaisonButton extends JButton implements ActionListener {
AbstractArticleItemTable itemTable;
public AjoutDeclinaisonButton(AbstractArticleItemTable itemTable) {
super("Ajouter des déclinaisons");
this.itemTable = itemTable;
this.addActionListener(this);
}
@Override
public void setEnabled(boolean b) {
// Toujours activé
super.setEnabled(true);
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
final JFrame f = new JFrame(this.getText());
final JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
final GridBagConstraints c = new DefaultGridBagConstraints();
// Ligne 1
c.gridx = 0;
c.weightx = 0;
p.add(new JLabel("Article virtuel", SwingConstants.RIGHT), c);
c.gridx++;
final String variant1ForeignFieldName = "ID_ARTICLE_DECLINAISON_COULEUR";
final String variant2ForeignFieldName = "ID_ARTICLE_DECLINAISON_TAILLE";
final SQLRequestComboBox combo = new SQLRequestComboBox(false);
final ReferenceArticleSQLElement articleElement = this.itemTable.getSQLElement().getDirectory().getElement(ReferenceArticleSQLElement.class);
final SQLTable articleTable = articleElement.getTable();
Where where = new Where(articleTable.getField("VIRTUEL"), "=", true).and(new Where(articleTable.getField("OBSOLETE"), "=", false));
final IComboModel comboModel = new IComboModel(articleElement.createComboRequest(null, where));
comboModel.getRequest().keepRows(KeepMode.ROW);
comboModel.getRequest().setWhere(where);
combo.uiInit(comboModel);
p.add(combo, c);
// Ligne 2 : table
AjoutDeclinaisonTableModel model = new AjoutDeclinaisonTableModel();
JTable table = new JTable(model);
final JTextField tf = new JTextField();
tf.setHorizontalAlignment(SwingConstants.RIGHT);
TableCellEditor editor = new DefaultCellEditor(tf) {
@Override
public Object getCellEditorValue() {
final String str = (String) super.getCellEditorValue();
if (str == null || str.length() == 0) {
return 0;
}
try {
int i = Integer.parseInt(str);
if (i < 0) {
i = 0;
}
return i;
} catch (Exception ex) {
return 0;
}
}
};
table.setRowHeight(FontUtils.getPreferredRowHeight(table));
table.setDefaultEditor(Integer.class, editor);
tf.addFocusListener(new FocusAdapter() {
public void focusGained(final FocusEvent e) {
tf.selectAll();
}
});
c.gridx = 0;
c.gridy++;
c.gridwidth = 2;
c.weighty = 1;
c.weightx = 1;
c.fill = GridBagConstraints.BOTH;
p.add(new JScrollPane(table), c);
// Ligne 3: boutons
final JButton bAnnuler = new JButton("Annuler");
final JButton bAjouter = new JButton("Ajouter");
final JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout());
buttons.add(bAnnuler);
buttons.add(bAjouter);
c.anchor = GridBagConstraints.SOUTHEAST;
c.gridy++;
c.fill = GridBagConstraints.NONE;
p.add(buttons, c);
f.setContentPane(p);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(800, 600);
f.setLocationRelativeTo(this);
f.setVisible(true);
// Listeners
combo.addValueListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.err.println("AjoutDeclinaisonButton.actionPerformed(...).new PropertyChangeListener() {...}.propertyChange()");
combo.setEnabled(false);
final SQLRow selectedRowArticle = combo.getSelectedRow();
if (selectedRowArticle != null) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
DBRoot root = AjoutDeclinaisonButton.this.itemTable.getSQLElement().getTable().getDBRoot();
// TODO voir pour un selecteur
SQLField variant1 = selectedRowArticle.getTable().getField(variant1ForeignFieldName);
SQLField variant2 = selectedRowArticle.getTable().getField(variant2ForeignFieldName);
model.loadFrom(root, selectedRowArticle, variant1, variant2, table);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
combo.setEnabled(true);
}
});
}
});
t.start();
}
}
});
bAnnuler.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.dispose();
}
});
bAjouter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CellEditor cellEditor = table.getCellEditor();
if (cellEditor != null && table.isEditing()) {
cellEditor.stopCellEditing();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
fetchAndClose(f, model);
}
});
}
private void fetchAndClose(final JFrame f, AjoutDeclinaisonTableModel model) {
final RowValuesTableModel rowValuesTableModel = itemTable.getRowValuesTable().getRowValuesTableModel();
List<SQLRowValues> values = model.getFilledArticles(rowValuesTableModel.getSQLElement().getTable());
System.err.println("AjoutDeclinaisonButton.actionPerformed(...).new ActionListener() {...}.actionPerformed() TODO : remplir et ajouter : " + values);
// TODO remplir et ajouter au table model
List<Number> idsArt = new ArrayList<Number>();
Map<Number, Integer> mapQte = new HashMap<>();
for (SQLRowValues sqlRowValues : values) {
final Number foreignIDNumber = sqlRowValues.getForeignIDNumber("ID_ARTICLE");
idsArt.add(foreignIDNumber);
mapQte.put(foreignIDNumber, sqlRowValues.getInt("QTE"));
}
Set<String> fieldsFrom = new HashSet<>(itemTable.getCodeCompletionManager().getFieldsFrom());
fieldsFrom.remove("QTE");
SQLRowValues rowValsArtFetch = new SQLRowValues(rowValuesTableModel.getSQLElement().getTable().getForeignTable("ID_ARTICLE"));
rowValsArtFetch.putNulls(rowValsArtFetch.getTable().getFieldsName());
List<SQLRowValues> artsFetched = SQLRowValuesListFetcher.create(rowValsArtFetch).fetch(new Where(rowValsArtFetch.getTable().getKey(), idsArt));
for (SQLRowValues sqlRowValues : artsFetched) {
final SQLRowValues row2Insert = new SQLRowValues(itemTable.getDefaultRowValues());
row2Insert.put("ID_ARTICLE", sqlRowValues.getID());
row2Insert.put("QTE", Math.round(mapQte.get(sqlRowValues.getIDNumber())));
row2Insert.put("CODE", sqlRowValues.getObject("CODE"));
row2Insert.put("NOM", sqlRowValues.getObject("NOM"));
itemTable.getRowValuesTable().getRowValuesTableModel().addRowAt(itemTable.getRowValuesTable().getRowValuesTableModel().getRowCount(), row2Insert);
// Completion depuis l'article trouvé
System.err.println("AjoutDeclinaisonButton.actionPerformed(...).new ActionListener() {...}.fetchAndClose()");
itemTable.getCodeCompletionManager().fillRowValues(sqlRowValues, fieldsFrom, row2Insert);
row2Insert.put("QTE", Math.round(mapQte.get(sqlRowValues.getIDNumber())));
if (row2Insert.contains("PV_HT") && row2Insert.getObject("PV_HT") != null) {
row2Insert.put("T_PV_HT", row2Insert.getBigDecimal("PV_HT").multiply(new BigDecimal(row2Insert.getInt("QTE"))));
if (row2Insert.contains("ID_TAXE") && row2Insert.getObject("ID_TAXE") != null) {
float taux = TaxeCache.getCache().getTauxFromId(row2Insert.getForeignID("ID_TAXE"));
row2Insert.put("T_PV_TTC", row2Insert.getBigDecimal("T_PV_HT").multiply(new BigDecimal(taux)));
}
}
}
// Fermeture de la fenetre
f.dispose();
}
});
}
}