OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | 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 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.shipment.component.BonDeLivraisonSQLComponent;
import org.openconcerto.erp.preferences.DefaultNXProps;
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
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.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.list.IListe;
import org.openconcerto.sql.view.list.RowAction;
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.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);
    }

    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");
        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("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 "));
        }
    }

    @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()) {

            SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());

            cmds.addAll(getSourceTrRowsFrom(row.getID(), "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT"));
            devis.addAll(getSourceTrRowsFrom(row.getID(), "DEVIS_ELEMENT", "DEVIS"));
            ids.add(row.getID());
            if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_FACT, true)) {

                // 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());
                    }
                }
            }
        }
        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);
            }
        };
    }

    @Override
    protected String createCodeSuffix() {
        return ".delivery.note";
    }

}