Dépôt officiel du code source de l'ERP OpenConcerto
Rev 174 | 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.supplychain.receipt.element;
import org.openconcerto.erp.config.Gestion;
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.supplychain.order.component.SaisieAchatSQLComponent;
import org.openconcerto.erp.core.supplychain.receipt.component.BonReceptionSQLComponent;
import org.openconcerto.erp.generationDoc.gestcomm.BonReceptionXmlSheet;
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.TreesOfSQLRows;
import org.openconcerto.sql.model.AliasedTable;
import org.openconcerto.sql.model.SQLName;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.UpdateBuilder;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.ListMap;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
import org.openconcerto.sql.model.SQLRowAccessor;
public class BonReceptionSQLElement extends ComptaSQLConfElement {
public BonReceptionSQLElement() {
super("BON_RECEPTION", "un bon de réception", "bons de réception");
PredicateRowAction actionsTRFA = new PredicateRowAction(new AbstractAction("Transfert vers facture fournisseur") {
public void actionPerformed(ActionEvent e) {
final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
SwingWorker<Boolean, Object> worker = new SwingWorker<Boolean, Object>() {
@Override
protected Boolean doInBackground() throws Exception {
boolean b = TransfertBaseSQLComponent.isAlreadyAllTransfert(selectedRows, getTable(), getTable().getTable("FACTURE_FOURNISSEUR"), "TOTAL_HT", "T_HT");
if (b) {
String label = "Attention ";
if (selectedRows.size() > 1) {
label += " les " + getPluralName() + " ont déjà été transféré!";
} else {
label += getSingularName() + " a déjà été transféré!";
}
label += "\n Voulez vous continuer?";
int ans = JOptionPane.showConfirmDialog(null, label, "Transfert " + getSingularName(), JOptionPane.YES_NO_OPTION);
if (ans == JOptionPane.NO_OPTION) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
@Override
protected void done() {
try {
Boolean b = get();
if (b) {
TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "FACTURE_FOURNISSEUR");
}
} catch (Exception e) {
ExceptionHandler.handle("Erreur lors du transfert des " + getPluralName() + "!", e);
}
}
};
worker.execute();
}
}, true);
actionsTRFA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
PredicateRowAction actionTRSimple = new PredicateRowAction(new AbstractAction("Transfert vers facture simple") {
public void actionPerformed(ActionEvent e) {
transfertFacture(IListe.get(e).getSelectedRow().getID());
}
}, false);
actionTRSimple.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(actionsTRFA);
getRowActions().add(actionTRSimple);
MouseSheetXmlListeListener mouseSheetXmlListeListener = new MouseSheetXmlListeListener(this, BonReceptionXmlSheet.class);
mouseSheetXmlListeListener.setGenerateHeader(true);
mouseSheetXmlListeListener.setShowHeader(true);
getRowActions().addAll(mouseSheetXmlListeListener.getRowActions());
}
@Override
public ListMap<String, String> getShowAs() {
return ListMap.singleton(null, "NUMERO", "DATE");
}
@Override
protected List<String> getListFields() {
final List<String> l = new ArrayList<>(5);
l.add("NUMERO");
l.add("NOM");
l.add("DATE");
l.add("ID_FOURNISSEUR");
l.add("TOTAL_HT");
l.add("INFOS");
return l;
}
@Override
protected List<String> getComboFields() {
final List<String> l = new ArrayList<>(2);
l.add("NUMERO");
l.add("DATE");
return l;
}
/*
* (non-Javadoc)
*
* @see org.openconcerto.devis.SQLElement#getComponent()
*/
public SQLComponent createComponent() {
return new BonReceptionSQLComponent();
}
/**
* Transfert d'un BR en facture
*
* @param brID
*/
public void transfertFacture(int brID) {
SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_ACHAT");
EditFrame editFactureFrame = new EditFrame(elt);
editFactureFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
SaisieAchatSQLComponent comp = (SaisieAchatSQLComponent) editFactureFrame.getSQLComponent();
comp.loadBonReception(brID);
editFactureFrame.pack();
editFactureFrame.setState(JFrame.NORMAL);
editFactureFrame.setVisible(true);
}
public List<Object> getCmdFrom(int brOrigin) {
SQLTable tableBRElement = getTable().getTable("BON_RECEPTION_ELEMENT");
SQLTable tableCmdElement = getTable().getTable("COMMANDE_ELEMENT");
String up = "SELECT DISTINCT c2.\"ID_COMMANDE\" FROM " + new SQLName(tableBRElement.getDBRoot().getName(), tableBRElement.getName()).quote() + " b2, "
+ new SQLName(tableCmdElement.getDBRoot().getName(), tableCmdElement.getName()).quote() + " c2 WHERE b2.\"ID_BON_RECEPTION\"=" + brOrigin
+ " AND c2.\"ARCHIVE\"=0 AND b2.\"ARCHIVE\"=0 AND c2.\"ID\">1 AND b2.\"ID\">1 AND b2.\"ID_COMMANDE_ELEMENT\"=c2.\"ID\"";
List<Object> cmds = getTable().getDBSystemRoot().getDataSource().executeCol(up);
return cmds;
}
public void updateCmdElement(List<Object> cmds, int idbrOrigin) {
SQLTable tableBRElement = getTable().getTable("BON_RECEPTION_ELEMENT");
SQLTable tableCmdElement = getTable().getTable("COMMANDE_ELEMENT");
UpdateBuilder build = new UpdateBuilder(tableCmdElement);
build.set("QTE_RECUE", "(SELECT SUM(b.\"QTE\" * b.\"QTE_UNITAIRE\") from " + new SQLName(tableBRElement.getDBRoot().getName(), tableBRElement.getName()).quote()
+ " b where c.\"ID\"=b.\"ID_COMMANDE_ELEMENT\" AND c.\"ID\">1 AND c.\"ARCHIVE\"=0 AND b.\"ID\">1 AND b.\"ARCHIVE\"=0 )");
AliasedTable alias = new AliasedTable(tableCmdElement, "c");
build.setWhere(new Where(alias.getField("ID_COMMANDE"), cmds));
getTable().getDBSystemRoot().getDataSource().execute(build.asString().replaceAll(" SET", " c SET "));
}
@Override
protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
List<Object> cmds = null;
List<Integer> ids = new ArrayList<>();
for (SQLRow row : trees.getRows()) {
cmds = getCmdFrom(row.getID());
ids.add(row.getID());
// Mise à jour des stocks
SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
sel.addSelect(eltMvtStock.getTable().getField("ID"));
Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
sel.setWhere(w.and(w2));
List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
if (l != null) {
for (int i = 0; i < l.size(); i++) {
Object[] tmp = (Object[]) l.get(i);
eltMvtStock.archive(((Number) tmp[0]).intValue());
}
}
}
super.archive(trees, cutLinks);
for (Integer id : ids) {
updateCmdElement(cmds, id);
}
}
@Override
protected String createCodeSuffix() {
return ".note";
}
/**
* Mise à jour des stocks pour chaque article composant du bon
*
* @throws SQLException
*/
public void updateStock(int id) throws SQLException {
SQLRow row = getTable().getRow(id);
StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
@Override
public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
return getLibelleStock(rowOrigin, rowElt);
}
}, row, row.getReferentRows(getTable().getTable("BON_RECEPTION_ELEMENT")),
getTable().contains("CREATE_VIRTUAL_STOCK") && row.getBoolean("CREATE_VIRTUAL_STOCK") ? TypeStockUpdate.REAL_VIRTUAL_RECEPT : TypeStockUpdate.REAL_RECEPT);
if (getTable().getDBRoot().contains("RELIQUAT_BR")) {
List<SQLRow> l = row.getReferentRows(getTable().getTable("RELIQUAT_BR").getField("ID_BON_RECEPTION_ORIGINE"));
for (SQLRow sqlRow : l) {
stockUpdater.addReliquat(sqlRow.getForeign("ID_ARTICLE"), sqlRow.getInt("QTE"), sqlRow.getBigDecimal("QTE_UNITAIRE"));
}
}
stockUpdater.update();
}
public String getLibelleStock(SQLRowAccessor row, SQLRowAccessor rowElt) {
return "Bon de réception N°" + row.getString("NUMERO");
}
}