OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 182 | 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.supplychain.stock.element;

import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.common.ui.PanelFrame;
import org.openconcerto.erp.core.sales.product.action.InventairePanel;
import org.openconcerto.erp.generationDoc.gestcomm.FicheArticleXmlSheet;
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
import org.openconcerto.sql.element.BaseSQLComponent;
import org.openconcerto.sql.element.SQLComponent;
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.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.sqlobject.ElementComboBox;
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.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.FrameUtil;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.ListMap;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class StockSQLElement extends ComptaSQLConfElement {

    public StockSQLElement() {
        super("STOCK", "un stock", "stocks");
        getRowActions().addAll(new MouseSheetXmlListeListener(FicheArticleXmlSheet.class).getRowActions());
        PredicateRowAction stock = new PredicateRowAction(new AbstractAction("Mettre à jour les stocks") {

            @Override
            public void actionPerformed(ActionEvent e) {

                PanelFrame p = new PanelFrame(new InventairePanel(IListe.get(e), IListe.get(e).getSelectedRows()), "Mise à jour des stocks");
                FrameUtil.show(p);

            }
        }, true, false);
        stock.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
        getRowActions().add(stock);
    }

    protected List<String> getListFields() {
        return Arrays.asList("ID_ARTICLE", "QTE_MIN", "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE", "ID_DEPOT_STOCK");
    }

    @Override
    public Set<String> getReadOnlyFields() {
        Set<String> s = new HashSet<>();
        if (getTable().contains("ID_ARTICLE")) {
            s.add("ID_ARTICLE");
        }
        s.add("QTE_TH");
        s.add("QTE_REEL");
        s.add("QTE_LIV_ATTENTE");
        s.add("QTE_RECEPT_ATTENTE");
        if (getTable().contains("ID_DEPOT_STOCK")) {
            s.add("ID_DEPOT_STOCK");
        }
        return s;
    }

    @Override
    protected List<String> getComboFields() {
        return Arrays.asList("ID_DEPOT_STOCK", "QTE_REEL");
    }

    @Override
    protected String getParentFFName() {
        return "ID_ARTICLE";
    }

    @Override
    public ListMap<String, String> getShowAs() {
        if (getTable().contains("ID_DEPOT_STOCK")) {
            return ListMap.singleton(null, "QTE_TH", "QTE_REEL", "QTE_MIN", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE", "ID_DEPOT_STOCK");
        } else {
            return ListMap.singleton(null, "QTE_TH", "QTE_REEL", "QTE_LIV_ATTENTE", "QTE_RECEPT_ATTENTE");
        }
    }

    public static SQLRowAccessor getStockFetched(SQLRowAccessor rowValsSource) {
        SQLRowAccessor rowStock = null;
        final int idDepot;
        if (rowValsSource.getForeign("ID_DEPOT_STOCK") != null && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
        } else {
            idDepot = rowValsSource.getForeign("ID_ARTICLE").getForeignID("ID_DEPOT_STOCK");
        }
        SQLTable stockTable = rowValsSource.getTable().getTable("STOCK");

        Collection<? extends SQLRowAccessor> rows = rowValsSource.getForeign("ID_ARTICLE").getReferentRows(stockTable);
        for (SQLRowAccessor sqlRowAccessor : rows) {
            if (sqlRowAccessor.getForeignID("ID_DEPOT_STOCK") == idDepot) {
                rowStock = sqlRowAccessor;
                break;
            }
        }

        return rowStock;
    }

    public static SQLRowAccessor getStock(SQLRowAccessor rowValsSource) {

        SQLRowAccessor rowStock = null;
        final int idDepot;
        if (rowValsSource.getForeign("ID_DEPOT_STOCK") != null && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
            idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
        } else {
            SQLRowAccessor rowValsArt = rowValsSource.getForeign("ID_ARTICLE");
            if (rowValsArt.getObject("ID_DEPOT_STOCK") == null) {
                rowValsArt = rowValsArt.asRow();
                ((SQLRow) rowValsArt).fetchValues();
                System.err.println("REFETCH ARTICLE");
                Thread.dumpStack();
            }
            idDepot = rowValsArt.getForeignID("ID_DEPOT_STOCK");
        }
        SQLTable stockTable = rowValsSource.getTable().getTable("STOCK");
        SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
        putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());

        SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
        Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", idDepot);
        Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", rowValsSource.getForeignID("ID_ARTICLE"));
        Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
        if (rowValsResult.isEmpty()) {
            SQLRowValues rowValsStock = new SQLRowValues(stockTable);
            rowValsStock.put("ID_ARTICLE", rowValsSource.getForeignID("ID_ARTICLE"));
            rowValsStock.put("ID_DEPOT_STOCK", idDepot);
            rowValsStock.put("QTE_TH", 0F);
            rowValsStock.put("QTE_REEL", 0F);
            rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
            rowValsStock.put("QTE_LIV_ATTENTE", 0F);
            try {
                rowStock = rowValsStock.insert();
                if (idDepot == DepotStockSQLElement.DEFAULT_ID) {
                    rowValsSource.getForeign("ID_ARTICLE").createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
                }
            } catch (SQLException e) {
                ExceptionHandler.handle("Erreur lors la création du stock!", e);
            }
        } else if (rowValsResult.size() == 1) {
            rowStock = rowValsResult.iterator().next();
        } else if (rowValsResult.size() > 1) {
            throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsSource.getForeign("ID_ARTICLE").getID() + " Depot " + idDepot);
        }
        return rowStock;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.SQLElement#getComponent()
     */
    public SQLComponent createComponent() {

        return new BaseSQLComponent(this) {

            public void addViews() {
                this.setLayout(new GridBagLayout());
                final GridBagConstraints c = new DefaultGridBagConstraints();

                // Article
                JLabel labelA = new JLabel(getLabelFor("ID_ARTICLE"));
                c.weightx = 0;
                this.add(labelA, c);

                c.gridx++;
                ElementComboBox boxA = new ElementComboBox();
                this.add(boxA, c);

                // Depot
                JLabel labelD = new JLabel(getLabelFor("ID_DEPOT_STOCK"));
                c.gridx++;
                c.weightx = 0;
                this.add(labelD, c);

                c.gridx++;
                ElementComboBox boxD = new ElementComboBox();
                this.add(boxD, c);

                // Qté Réelle
                JLabel labelQteR = new JLabel(getLabelFor("QTE_REEL"));
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                this.add(labelQteR, c);

                c.gridx++;
                JTextField textQteReel = new JTextField(6);
                this.add(textQteReel, c);

                // Qté Réelle
                JLabel labelQteT = new JLabel(getLabelFor("QTE_TH"));
                c.gridx++;
                c.weightx = 0;
                this.add(labelQteT, c);

                c.gridx++;
                JTextField textQteT = new JTextField(6);
                this.add(textQteT, c);

                // Qté Réelle
                JLabel labelQteRe = new JLabel(getLabelFor("QTE_RECEPT_ATTENTE"));
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                this.add(labelQteRe, c);

                c.gridx++;
                JTextField textQteRe = new JTextField(6);
                this.add(textQteRe, c);

                JLabel labelQteL = new JLabel(getLabelFor("QTE_LIV_ATTENTE"));
                c.gridx++;
                c.weightx = 0;
                this.add(labelQteL, c);

                c.gridx++;
                JTextField textQteL = new JTextField(6);
                this.add(textQteL, c);

                // Qté Min
                JLabel labelQteTMin = new JLabel(getLabelFor("QTE_MIN"));
                c.gridx = 0;
                c.gridy++;
                c.weightx = 0;
                this.add(labelQteTMin, c);

                c.gridx++;
                JTextField textQteMin = new JTextField(6);
                this.add(textQteMin, c);

                this.addSQLObject(textQteReel, "QTE_REEL");
                this.addSQLObject(textQteT, "QTE_TH");
                this.addSQLObject(textQteMin, "QTE_MIN");
                this.addSQLObject(textQteL, "QTE_LIV_ATTENTE");
                this.addSQLObject(textQteRe, "QTE_RECEPT_ATTENTE");
                this.addSQLObject(boxA, "ID_ARTICLE");
                this.addSQLObject(boxD, "ID_DEPOT_STOCK");
            }
        };
    }

    @Override
    protected String createCode() {
        return "supplychain.stock";
    }
}