Dépôt officiel du code source de l'ERP OpenConcerto
Blame | 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.sales.product.action;
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
import org.openconcerto.sql.model.SQLDataSource;
import org.openconcerto.sql.model.SQLInsert;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.SQLTableEvent;
import org.openconcerto.sql.model.SQLTableEvent.Mode;
import org.openconcerto.sql.model.SQLUpdate;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.sql.utils.SQLUtils;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.utils.ExceptionHandler;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class InitializeStockPanel extends JPanel {
public InitializeStockPanel(SQLElement eltArt, List<SQLRowAccessor> selectedRows) {
super(new GridBagLayout());
final JButton buttonUpdate = new JButton("Initialiser");
final SQLElement stockElt = eltArt.getDirectory().getElement("DEPOT_STOCK");
final SQLRequestComboBox comboStockDepart = new SQLRequestComboBox();
comboStockDepart.uiInit(stockElt.createComboRequest());
comboStockDepart.addModelListener("wantedID", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
updateButtons(buttonUpdate, comboStockDepart);
}
});
GridBagConstraints c = new DefaultGridBagConstraints();
c.gridx = 0;
c.weightx = 0;
this.add(new JLabel("Dépôt", SwingConstants.RIGHT), c);
c.gridx++;
this.add(comboStockDepart, c);
c.gridy++;
c.gridx = 0;
c.weightx = 0;
c.gridy++;
c.gridx = 0;
JButton buttonCancel = new JButton("Annuler");
JPanel pButton = new JPanel();
pButton.add(buttonCancel);
pButton.add(buttonUpdate);
c.gridwidth = 2;
c.anchor = GridBagConstraints.SOUTHEAST;
c.weightx = 0;
c.fill = GridBagConstraints.NONE;
this.add(pButton, c);
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JFrame) SwingUtilities.getRoot(InitializeStockPanel.this)).dispose();
}
});
buttonUpdate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonUpdate.setEnabled(false);
SQLRow rowDepot = comboStockDepart.getSelectedRow();
SwingWorker<Integer, Object> worker = new SwingWorker<Integer, Object>() {
@Override
protected Integer doInBackground() throws Exception {
try {
SQLUtils.executeAtomic(eltArt.getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, SQLException>() {
@Override
public Object handle(SQLDataSource ds) throws SQLException {
// Récupération des articles déjà initialisé
List<Integer> idsArt = new ArrayList<>();
for (SQLRowAccessor sqlRowValues : selectedRows) {
idsArt.add(sqlRowValues.getID());
}
SQLSelect selStock = new SQLSelect();
final SQLTable stockTable = eltArt.getTable().getDBRoot().getTable("STOCK");
selStock.addSelect(stockTable.getKey());
selStock.addSelect(stockTable.getField("ID_ARTICLE"));
Where w = new Where(stockTable.getField("ID_ARTICLE"), idsArt);
selStock.setWhere(w.and(new Where(stockTable.getField("ID_DEPOT_STOCK"), "=", rowDepot.getID())));
List<SQLRow> rowsStock = SQLRowListRSH.execute(selStock);
Set<Integer> artInited = new HashSet<>();
for (SQLRow sqlRowValues : rowsStock) {
artInited.add(sqlRowValues.getForeignID("ID_ARTICLE"));
}
// Insertion des stocks
List<SQLRowAccessor> rowValsArtWithInsert = new ArrayList<>();
List<SQLInsert> sqlInserts = new ArrayList<>();
for (SQLRowAccessor sqlRowValues : selectedRows) {
if (!artInited.contains(sqlRowValues.getID())) {
SQLInsert insert = new SQLInsert();
insert.add(stockTable.getField("ID_ARTICLE"), sqlRowValues.getID());
insert.add(stockTable.getField("ID_DEPOT_STOCK"), rowDepot.getID());
sqlInserts.add(insert);
rowValsArtWithInsert.add(sqlRowValues);
}
}
// Mise à jour de l'ID_STOCK de l'article si besoin
if (!sqlInserts.isEmpty()) {
final List<Number> executeSimilarInserts = SQLInsert.executeSimilarInserts(stockTable.getDBSystemRoot(), sqlInserts, true);
List<SQLUpdate> updates = new ArrayList<>();
for (int i = 0; i < executeSimilarInserts.size(); i++) {
SQLRowAccessor rowVals = rowValsArtWithInsert.get(i);
if (rowVals.getForeignID("ID_DEPOT_STOCK") == rowDepot.getID()
|| (rowVals.isForeignEmpty("ID_DEPOT_STOCK") && rowDepot.getID() == DepotStockSQLElement.DEFAULT_ID)) {
SQLUpdate up = new SQLUpdate(new Where(eltArt.getTable().getKey(), "=", rowVals.getIDNumber()));
up.add(eltArt.getTable().getField("ID_STOCK"), executeSimilarInserts.get(i));
updates.add(up);
}
}
if (!updates.isEmpty()) {
SQLUpdate.executeMultipleWithBatch(eltArt.getTable().getDBSystemRoot(), updates);
eltArt.getTable().fire(new SQLTableEvent(eltArt.getTable(), SQLRow.NONEXISTANT_ID, Mode.ROW_UPDATED));
}
}
return null;
}
});
} catch (SQLException e1) {
ExceptionHandler.handle("Stock update error", e1);
}
return null;
}
@Override
protected void done() {
try {
Integer val = get();
} catch (Exception e) {
ExceptionHandler.handle("Stock update error", e);
}
super.done();
JOptionPane.showMessageDialog(((JFrame) SwingUtilities.getRoot(InitializeStockPanel.this)), "Initialisation des stocks terminée");
}
};
worker.execute();
((JFrame) SwingUtilities.getRoot(InitializeStockPanel.this)).dispose();
}
});
updateButtons(buttonUpdate, comboStockDepart);
}
private void updateButtons(final JButton buttonUpdate, final SQLRequestComboBox comboStockDepart) {
buttonUpdate.setEnabled(comboStockDepart.getSelectedRow() != null);
}
}