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.sales.shipment.element;
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.sales.product.element.LotSQLElement;
import org.openconcerto.erp.core.sales.shipment.component.BonDeLivraisonSQLComponent;
import org.openconcerto.erp.preferences.DefaultNXProps;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.SQLElementLink.LinkType;
import org.openconcerto.sql.element.SQLElementLinksSetup;
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.SQLRowAccessor;
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.SQLTableModifiedListener;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ListSQLRequest;
import org.openconcerto.sql.request.UpdateBuilder;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.RowAction;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.sql.view.list.action.ListEvent;
import org.openconcerto.ui.preferences.DefaultProps;
import org.openconcerto.utils.ListMap;
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 org.apache.commons.dbutils.handlers.ArrayListHandler;
public class BonDeLivraisonSQLElement extends ComptaSQLConfElement {
// TODO afficher uniquement les factures non livrees dans la combo
// MAYBE mettre un niceCellRenderer dans les rowValuesTable
public BonDeLivraisonSQLElement(String single, String plural) {
super("BON_DE_LIVRAISON", single, plural);
// Verrou facture
if (getTable().contains("VERROU_FACTURATION")) {
PredicateRowAction lock = new PredicateRowAction(new AbstractAction("Verrouiller") {
@Override
public void actionPerformed(ActionEvent e) {
updateVerrouFacture(IListe.get(e).getSelectedRowAccessors(), true);
}
}, false);
lock.setPredicate(ListEvent.getNonEmptySelectionPredicate());
lock.setPath(Arrays.asList("Verrou facturaction", "Verrou facturaction", "Verrou facturaction"));
PredicateRowAction unlock = new PredicateRowAction(new AbstractAction("Déverrouiller") {
@Override
public void actionPerformed(ActionEvent e) {
updateVerrouFacture(IListe.get(e).getSelectedRowAccessors(), false);
}
}, false);
unlock.setPath(Arrays.asList("Verrou facturaction", "Verrou facturaction", "Verrou facturaction"));
unlock.setPredicate(ListEvent.getNonEmptySelectionPredicate());
getRowActions().add(lock);
getRowActions().add(unlock);
}
}
@Override
protected void _initListRequest(ListSQLRequest req) {
super._initListRequest(req);
req.addToGraphToFetch("VERROU_FACTURATION");
}
@Override
protected void _initTableSource(SQLTableModelSource res) {
// TODO Auto-generated method stub
super._initTableSource(res);
addCommercialFilter(res, getTable().getField("ID_COMMERCIAL"));
}
private void updateVerrouFacture(List<SQLRowAccessor> rows, boolean state) {
UpdateBuilder builder = new UpdateBuilder(getTable());
builder.setObject("VERROU_FACTURATION", state);
builder.setWhere(Where.inValues(getTable().getKey(), SQLRow.getIDs(rows)));
getTable().getDBSystemRoot().getDataSource().execute(builder.asString());
getTable().fireTableModified();
}
public BonDeLivraisonSQLElement() {
this("un bon de livraison", "bons de livraison");
}
@Override
protected void setupLinks(SQLElementLinksSetup links) {
super.setupLinks(links);
if (getTable().contains("ID_ADRESSE")) {
links.get("ID_ADRESSE").setType(LinkType.ASSOCIATION);
}
if (getTable().contains("ID_ADRESSE_LIVRAISON")) {
links.get("ID_ADRESSE_LIVRAISON").setType(LinkType.ASSOCIATION);
}
}
@Override
protected List<String> getListFields() {
final List<String> l = new ArrayList<>();
l.add("NUMERO");
l.add("DATE");
if (getTable().contains("ID_COMMERCIAL")) {
l.add("ID_COMMERCIAL");
}
l.add("ID_CLIENT");
DefaultProps props = DefaultNXProps.getInstance();
Boolean b = props.getBooleanValue("ArticleShowPoids");
if (b) {
l.add("TOTAL_POIDS");
}
l.add("NOM");
l.add("TOTAL_HT");
l.add("TOTAL_TTC");
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;
}
@Override
public ListMap<String, String> getShowAs() {
return ListMap.singleton(null, "NUMERO", "DATE");
}
/*
* (non-Javadoc)
*
* @see org.openconcerto.devis.SQLElement#getComponent()
*/
public SQLComponent createComponent() {
return new BonDeLivraisonSQLComponent();
}
public List<Object> getSourceTrRowsFrom(int blOrigin, String tableSourceItems, String tableSourceRoot) {
SQLTable tableBLElement = getTable().getTable("BON_DE_LIVRAISON_ELEMENT");
SQLTable tableCmdElement = getTable().getTable(tableSourceItems);
String idRoot = "c2.\"" + getTable().getTable(tableSourceRoot).getKey().getFieldName() + "\"";
String up = "SELECT DISTINCT c2.\"ID_" + tableSourceRoot + "\" FROm " + new SQLName(tableBLElement.getDBRoot().getName(), tableBLElement.getName()).quote() + " b2, "
+ new SQLName(tableCmdElement.getDBRoot().getName(), tableCmdElement.getName()).quote() + " c2 WHERE b2.\"ID_BON_DE_LIVRAISON\"=" + blOrigin
+ " AND c2.\"ARCHIVE\"=0 AND b2.\"ARCHIVE\"=0 AND " + idRoot + ">1 AND b2.\"ID\">1 AND b2.\"ID_" + tableSourceItems + "\"=" + idRoot;
return getTable().getDBSystemRoot().getDataSource().executeCol(up);
}
public void updateQteLivree(List<Object> items, String tableItems, String tableRoot) {
if (items != null && !items.isEmpty()) {
SQLTable tableBLElement = getTable().getTable("BON_DE_LIVRAISON_ELEMENT");
SQLTable tableCmdElement = getTable().getTable(tableItems);
String itemsKey = "c.\"" + getTable().getTable(tableItems).getKey().getName() + "\"";
UpdateBuilder build = new UpdateBuilder(tableCmdElement);
build.set("QTE_LIVREE", "(SELECT SUM(b.\"QTE_LIVREE\" * b.\"QTE_UNITAIRE\") from " + new SQLName(tableBLElement.getDBRoot().getName(), tableBLElement.getName()).quote() + " b where "
+ itemsKey + "=b.\"ID_" + tableItems + "\" AND " + itemsKey + ">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_" + tableRoot), items));
getTable().getDBSystemRoot().getDataSource().execute(build.asString().replaceAll(" SET", " c SET "));
// Update commande state
if (tableRoot.equalsIgnoreCase("COMMANDE_CLIENT")) {
final SQLTable tableCmd = getTable().getTable("COMMANDE_CLIENT");
AliasedTable aliasCmd = new AliasedTable(tableCmd, "c");
Where wIN = Where.inValues(aliasCmd.getKey(), items);
String req = "UPDATE " + tableCmd.getSQLName().quote() + " c SET \"ETAT_COMMANDE\"=5 WHERE " + wIN.getClause()
+ "and (\"ETAT_COMMANDE\"=1 or \"ETAT_COMMANDE\"=2 or c.\"ETAT_COMMANDE\"=3 or c.\"ETAT_COMMANDE\"=4) "
+ "and (select SUM((i.\"QTE\"*i.\"QTE_UNITAIRE\")-i.\"QTE_LIVREE\") from " + tableCmdElement.getSQLName().quote() + " i"
+ " where i.\"ID_COMMANDE_CLIENT\"=c.\"ID\" and i.\"ARCHIVE\"=0)<=0";
getTable().getDBSystemRoot().getDataSource().execute(req);
}
}
}
@Override
protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
List<Object> cmds = new ArrayList<>();
List<Object> devis = new ArrayList<>();
List<Integer> ids = new ArrayList<>();
for (SQLRow row : trees.getRows()) {
cmds.addAll(getSourceTrRowsFrom(row.getID(), "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT"));
devis.addAll(getSourceTrRowsFrom(row.getID(), "DEVIS_ELEMENT", "DEVIS"));
ids.add(row.getID());
// 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("unchecked")
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());
}
}
}
getDirectory().getElement(LotSQLElement.class).removeLotQuantiteFromBL(ids);
super.archive(trees, cutLinks);
updateQteLivree(devis, "DEVIS_ELEMENT", "DEVIS");
updateQteLivree(cmds, "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT");
}
public RowAction getCloneAction() {
return new RowAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
SQLRowAccessor selectedRow = IListe.get(e).getSelectedRow();
EditFrame editFrame = new EditFrame(BonDeLivraisonSQLElement.this, EditPanel.CREATION);
((BonDeLivraisonSQLComponent) 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 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));
}
}
@Override
protected String createCodeSuffix() {
return ".delivery.note";
}
}