Dépôt officiel du code source de l'ERP OpenConcerto
Rev 182 | 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.order.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.edm.AttachmentAction;
import org.openconcerto.erp.core.sales.order.ui.ReliquatCommandeTableModel;
import org.openconcerto.erp.core.supplychain.order.component.CommandeSQLComponent;
import org.openconcerto.erp.core.supplychain.order.component.SaisieAchatSQLComponent;
import org.openconcerto.erp.core.supplychain.order.ui.EtatCommandeFournisseur;
import org.openconcerto.erp.core.supplychain.order.ui.EtatCommandeRenderer;
import org.openconcerto.erp.core.supplychain.receipt.component.BonReceptionSQLComponent;
import org.openconcerto.erp.generationDoc.gestcomm.CommandeXmlSheet;
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
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.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLSelectJoin;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.SQLTableEvent;
import org.openconcerto.sql.model.SQLTableEvent.Mode;
import org.openconcerto.sql.model.SQLTableModifiedListener;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.preferences.SQLPreferences;
import org.openconcerto.sql.request.UpdateBuilder;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel;
import org.openconcerto.sql.view.EditPanelListener;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.sql.view.list.SQLTableModelColumn;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.ui.PanelFrame;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.cc.ITransformer;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableCellRenderer;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
public class CommandeSQLElement extends ComptaSQLConfElement {
public CommandeSQLElement() {
super("COMMANDE", "une commande fournisseur", "commandes fournisseur");
if (getTable().contains("ATTACHMENTS")) {
PredicateRowAction actionAttachment = new PredicateRowAction(new AttachmentAction().getAction(), true);
actionAttachment.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(actionAttachment);
}
for (final EtatCommandeFournisseur etat : EtatCommandeFournisseur.values()) {
PredicateRowAction action = new PredicateRowAction(new AbstractAction(etat.getTranslation()) {
@Override
public void actionPerformed(ActionEvent e) {
changeStateOfRows(IListe.get(e).getSelectedRows(), etat);
}
}, false);
action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
action.setPath(Arrays.asList("Etat", "Etat", "Etat"));
getRowActions().add(action);
}
getRowActions().addAll(new MouseSheetXmlListeListener(this, CommandeXmlSheet.class).getRowActions());
// Transfert vers BR
PredicateRowAction bonAction = new PredicateRowAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
transfertBR(selectedRows);
}
}, true, "supplychain.order.create.receipt");
bonAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(bonAction);
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"), "T_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();
}
}, false);
actionsTRFA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
getRowActions().add(actionsTRFA);
// Transfert vers facture
PredicateRowAction factureAction = new PredicateRowAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
CommandeSQLElement.this.transfertFacture(IListe.get(e).getSelectedRow().getID());
}
}, false, "supplychain.order.create.purchase");
factureAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(factureAction);
getRowActions().add(getCloneAction());
PredicateRowAction tagValidAction = new PredicateRowAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
final SQLRowValues asRowValues = IListe.get(e).getSelectedRow().asRow().createEmptyUpdateRow();
asRowValues.put("EN_COURS", Boolean.FALSE);
try {
asRowValues.commit();
} catch (SQLException e1) {
ExceptionHandler.handle("Une erreur est survenue pour notifier la commande valider", e1);
}
}
}, false, "supplychain.order.valid");
tagValidAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(tagValidAction);
// Reliquat
PredicateRowAction reliquatAction = new PredicateRowAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JPanel panelReliquat = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
final ReliquatCommandeTableModel dm = new ReliquatCommandeTableModel(IListe.get(e).getSelectedRowAccessor());
if (dm.getRowCount() > 0) {
JTable table = new JTable(dm);
JScrollPane comp = new JScrollPane(table);
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
panelReliquat.add(comp, c);
PanelFrame frame = new PanelFrame(panelReliquat, "Reliquat commande");
frame.pack();
frame.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "Aucun reliquat restant");
}
}
}, false, "sales.order.reliquat.show");
reliquatAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(reliquatAction);
}
@Override
protected List<String> getListFields() {
final List<String> l = new ArrayList<>(8);
l.add("NUMERO");
l.add("NOM");
l.add("DATE");
l.add("ID_FOURNISSEUR");
l.add("ID_COMMERCIAL");
l.add("T_HT");
l.add("T_TTC");
Boolean useCommandeEnCours = SQLPreferences.getMemCached(getTable().getDBRoot()).getBoolean(GestionCommercialeGlobalPreferencePanel.COMMANDE_FOURNISSEUR_EN_COURS, false);
if (useCommandeEnCours) {
l.add("EN_COURS");
}
l.add("ETAT_COMMANDE");
l.add("INFOS");
return l;
}
@Override
protected List<String> getComboFields() {
final List<String> l = new ArrayList<>(3);
l.add("NUMERO");
l.add("NOM");
l.add("DATE");
return l;
}
@Override
protected void _initTableSource(SQLTableModelSource res) {
// TODO Auto-generated method stub
super._initTableSource(res);
SQLTableModelColumn col = res.getColumn(getTable().getField("ETAT_COMMANDE"));
if (col != null) {
col.setRenderer(new EtatCommandeRenderer());
}
}
/*
* (non-Javadoc)
*
* @see org.openconcerto.devis.SQLElement#getComponent()
*/
public SQLComponent createComponent() {
return new CommandeSQLComponent();
}
private final List<SQLTableModifiedListener> listenerCmdInserted = new ArrayList<>();
public void addInsertedCmdListener(SQLTableModifiedListener l) {
this.listenerCmdInserted.add(l);
}
public void removeInsertedCmdListener(SQLTableModifiedListener l) {
this.listenerCmdInserted.remove(l);
}
public void fireInsertedCmdListener(SQLRow row) {
for (SQLTableModifiedListener sqlTableModifiedListener : this.listenerCmdInserted) {
sqlTableModifiedListener.tableModified(new SQLTableEvent(row, Mode.ROW_ADDED, null));
}
}
public void transfertBR(final List<SQLRowValues> selectedRows) {
EditFrame f = TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "BON_RECEPTION");
BonReceptionSQLComponent comp = (BonReceptionSQLComponent) f.getSQLComponent();
final SQLTable tableElt = comp.getElement().getTable().getTable("BON_RECEPTION_ELEMENT");
SQLRowValues rowVals = new SQLRowValues(tableElt);
rowVals.put("QTE_UNITAIRE", null);
rowVals.put("QTE", null);
rowVals.put("ID_ARTICLE", null);
SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
@Override
public SQLSelect transformChecked(SQLSelect input) {
final List<Integer> ids = new ArrayList<>(selectedRows.size());
for (SQLRowValues sqlRowValues : selectedRows) {
ids.add(sqlRowValues.getID());
}
SQLSelectJoin joinBR = input.addJoin("RIGHT", tableElt.getTable("BON_RECEPTION_ELEMENT").getField("ID_BON_RECEPTION"));
SQLSelectJoin joinTR = input.addBackwardJoin("RIGHT", tableElt.getTable("TR_COMMANDE").getField("ID_BON_RECEPTION"), joinBR.getJoinedTable().getAlias());
joinTR.setWhere(new Where(joinTR.getJoinedTable().getField("ID_COMMANDE"), ids));
return input;
}
});
comp.loadQuantity(fetcher.fetch());
comp.removeZeroQtyLines();
}
/**
* Transfert d'une commande en facture
*
* @param commandeID
*/
public void transfertFacture(int commandeID) {
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.loadCommande(commandeID);
editFactureFrame.pack();
editFactureFrame.setState(JFrame.NORMAL);
editFactureFrame.setVisible(true);
editFactureFrame.addEditPanelListener(new EditPanelListener() {
@Override
public void modified() {
}
@Override
public void inserted(int id) {
try {
SQLRowValues rowValsTR = new SQLRowValues(getTable().getTable("TR_COMMANDE"));
rowValsTR.put("ID_COMMANDE", commandeID);
rowValsTR.put("ID_SAISIE_ACHAT", id);
rowValsTR.commit();
} catch (SQLException e) {
ExceptionHandler.handle("Erreur lors de l'insertion de la ligne de transfert", e);
}
}
@Override
public void deleted() {
}
@Override
public void cancelled() {
}
});
}
public RowAction getCloneAction() {
return new RowAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
SQLRowAccessor selectedRow = IListe.get(e).getSelectedRow();
SQLElement eltFact = Configuration.getInstance().getDirectory().getElement("COMMANDE");
EditFrame editFrame = new EditFrame(eltFact, EditPanel.CREATION);
((CommandeSQLComponent) editFrame.getSQLComponent()).duplicate(selectedRow.getID());
editFrame.setVisible(true);
}
}, true, "sales.quote.clone") {
@Override
public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
return (selection != null && selection.size() == 1);
}
};
}
private void changeStateOfRows(List<SQLRowValues> l, EtatCommandeFournisseur etat) {
List<Integer> ids = new ArrayList<Integer>(l.size());
for (SQLRowValues sqlRowValues : l) {
ids.add(sqlRowValues.getID());
}
UpdateBuilder builder = new UpdateBuilder(getTable());
builder.setObject("ETAT_COMMANDE", etat.getId());
builder.setWhere(Where.inValues(getTable().getKey(), ids));
getTable().getDBSystemRoot().getDataSource().execute(builder.asString());
getTable().fireTableModified(-1);
if (etat == EtatCommandeFournisseur.ANNULEE) {
// Suppression des stocks si commande annulée
SQLElement eltMvtStock = getDirectory().getElement("MOUVEMENT_STOCK");
SQLSelect sel = new SQLSelect();
sel.addSelect(eltMvtStock.getTable().getField("ID"));
Where w = Where.inValues(eltMvtStock.getTable().getField("IDSOURCE"), ids);
Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
sel.setWhere(w.and(w2));
try {
@SuppressWarnings("rawtypes")
List res = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
if (res != null) {
for (int i = 0; i < res.size(); i++) {
Object[] tmp = (Object[]) res.get(i);
eltMvtStock.archive(((Number) tmp[0]).intValue());
}
}
final SQLTable tableCmdItem = getTable().getTable("COMMANDE_ELEMENT");
UpdateBuilder build = new UpdateBuilder(tableCmdItem);
build.setObject("RECU_FORCED", Boolean.TRUE);
build.setWhere(Where.inValues(tableCmdItem.getField("ID_COMMANDE"), ids));
getTable().getDBSystemRoot().getDataSource().execute(build.asString());
} catch (SQLException e) {
ExceptionHandler.handle("Erreur lors de la suppression des mouvements de stocks", e);
}
}
}
@Override
protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
for (SQLRow row : trees.getRows()) {
// Mise à jour des stocks
SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
SQLSelect sel = new SQLSelect();
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));
@SuppressWarnings("rawtypes")
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);
}
@Override
protected String createCode() {
return "supplychain.order";
}
}