OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | 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.order.action;

import org.openconcerto.erp.action.CreateFrameAbstractAction;
import org.openconcerto.erp.core.common.ui.IListFilterDatePanel;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.FieldPath;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.graph.Path;
import org.openconcerto.sql.model.graph.PathBuilder;
import org.openconcerto.sql.view.IListFrame;
import org.openconcerto.sql.view.ListeAddPanel;
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.table.PercentTableCellRenderer;
import org.openconcerto.utils.CollectionUtils;
import org.openconcerto.utils.DecimalUtils;

import java.awt.GridBagConstraints;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.Action;
import javax.swing.JFrame;

public class ListeDesCommandesAction extends CreateFrameAbstractAction {
    public ListeDesCommandesAction() {
        super();
        this.putValue(Action.NAME, "Liste des commandes fournisseurs");
    }

    public JFrame createFrame() {

        final SQLElement elementCmd = Configuration.getInstance().getDirectory().getElement("COMMANDE");
        final SQLTableModelSourceOnline tableSource = elementCmd.getTableSource(true);
        BaseSQLTableModelColumn colAvancementR = new BaseSQLTableModelColumn("Avancement réception", BigDecimal.class) {

            @Override
            protected Object show_(SQLRowAccessor r) {

                return getAvancementLFromBR(r);
            }

            @Override
            public Set<FieldPath> getPaths() {
                final Path p = new PathBuilder(elementCmd.getTable()).addTable("COMMANDE_ELEMENT").build();
                return CollectionUtils.createSet(new FieldPath(p, "RECU_FORCED"), new FieldPath(p, "RECU"), new FieldPath(p, "QTE_RECUE"), new FieldPath(p, "PA_HT"), new FieldPath(p, "ID_ARTICLE"),
                        new FieldPath(p, "QTE"), new FieldPath(p, "QTE_UNITAIRE"));
            }
        };
        tableSource.getColumns().add(colAvancementR);
        colAvancementR.setRenderer(new PercentTableCellRenderer());

        BaseSQLTableModelColumn colAvancement = new BaseSQLTableModelColumn("Avancement facturation", BigDecimal.class) {

            @Override
            protected Object show_(SQLRowAccessor r) {

                return getAvancementFact(r);
            }

            @Override
            public Set<FieldPath> getPaths() {
                final Path p = new PathBuilder(elementCmd.getTable()).addTable("TR_COMMANDE").addTable("SAISIE_ACHAT").build();
                final Path p2 = new PathBuilder(elementCmd.getTable()).addTable("TR_COMMANDE").addTable("FACTURE_FOURNISSEUR").build();

                final Path pBr = new PathBuilder(elementCmd.getTable()).addTable("TR_COMMANDE").addTable("BON_RECEPTION").build();
                final Path trBr = pBr.add(elementCmd.getTable().getTable("TR_BON_RECEPTION"));
                final Path pFact2 = trBr.addTable("FACTURE_FOURNISSEUR");
                final Path pAchat2 = trBr.addTable("SAISIE_ACHAT");
                return CollectionUtils.createSet(new FieldPath(p, "MONTANT_HT"), new FieldPath(p2, "T_HT"), new FieldPath(pAchat2, "MONTANT_HT"), new FieldPath(pFact2, "T_HT"));
            }
        };
        tableSource.getColumns().add(colAvancement);
        colAvancement.setRenderer(new PercentTableCellRenderer());

        final ListeAddPanel panel = new ListeAddPanel(elementCmd, new IListe(tableSource));
        // Date panel
        Map<IListe, SQLField> map = new HashMap<IListe, SQLField>();
        map.put(panel.getListe(), elementCmd.getTable().getField("DATE"));

        IListFilterDatePanel datePanel = new IListFilterDatePanel(map, IListFilterDatePanel.getDefaultMap());
        GridBagConstraints c = new DefaultGridBagConstraints();
        c.weightx = 0;
        c.fill = GridBagConstraints.NONE;
        c.gridy = 4;
        c.anchor = GridBagConstraints.CENTER;
        c.weighty = 0;
        datePanel.setFilterOnDefault();
        panel.add(datePanel, c);
        final IListFrame frame = new IListFrame(panel);

        return frame;
    }

    private BigDecimal getAvancementLFromBR(SQLRowAccessor r) {
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("COMMANDE_ELEMENT"));
        BigDecimal totalQte = BigDecimal.ZERO;
        BigDecimal totalQteL = BigDecimal.ZERO;
        for (SQLRowAccessor row : rows) {
            BigDecimal qte = row.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(row.getInt("QTE")));
            // On ne prend en compte que les articles ou les lignes différentes de 0
            if (!row.isForeignEmpty("ID_ARTICLE") || row.getBigDecimal("PA_HT").signum() != 0) {
                totalQte = totalQte.add(qte);
                if (row.getBoolean("RECU_FORCED") || row.getBoolean("RECU")) {
                    totalQteL = totalQteL.add(qte);
                } else if (row.getBigDecimal("QTE_RECUE") != null) {
                    final BigDecimal qteLivree = row.getBigDecimal("QTE_RECUE");
                    if (qteLivree != null) {
                        totalQteL = totalQteL.add(qteLivree);
                    }
                }
            }
        }
        if (totalQte.signum() != 0) {
            return totalQteL.divide(totalQte, DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
        } else {
            return BigDecimal.ONE.movePointRight(2);
        }
    }

    BigDecimal bigDecimal100 = new BigDecimal(100.0);

    private BigDecimal getAvancementFact(SQLRowAccessor r) {
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_COMMANDE"));
        long totalFact = 0;
        long total = r.getLong("T_HT");
        for (SQLRowAccessor row : rows) {
            if (!row.isForeignEmpty("ID_FACTURE_FOURNISSEUR")) {
                SQLRowAccessor rowFact = row.getForeign("ID_FACTURE_FOURNISSEUR");
                Long l = rowFact.getLong("T_HT");
                totalFact += l;
            }
            if (!row.isForeignEmpty("ID_SAISIE_ACHAT")) {
                SQLRowAccessor rowFact = row.getForeign("ID_SAISIE_ACHAT");
                Long l = rowFact.getLong("MONTANT_HT");
                totalFact += l;
            }

            if (!row.isForeignEmpty("ID_BON_RECEPTION")) {
                Collection<? extends SQLRowAccessor> rowsBr = row.getForeign("ID_BON_RECEPTION").getReferentRows(r.getTable().getTable("TR_BON_RECEPTION"));
                for (SQLRowAccessor rowTrBr : rowsBr) {
                    if (!rowTrBr.isForeignEmpty("ID_FACTURE_FOURNISSEUR")) {
                        SQLRowAccessor rowFact = rowTrBr.getForeign("ID_FACTURE_FOURNISSEUR");
                        Long l = rowFact.getLong("T_HT");
                        totalFact += l;
                    }
                    if (!rowTrBr.isForeignEmpty("ID_SAISIE_ACHAT")) {
                        SQLRowAccessor rowFact = rowTrBr.getForeign("ID_SAISIE_ACHAT");
                        Long l = rowFact.getLong("MONTANT_HT");
                        totalFact += l;
                    }

                }
            }
        }

        if (total > 0) {
            return this.bigDecimal100.min(new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP));
        } else {
            return BigDecimal.ONE.movePointRight(2);
        }
    }

    private BigDecimal getAvancement(SQLRowAccessor r) {
        Collection<? extends SQLRowAccessor> rows = r.getReferentRows(r.getTable().getTable("TR_COMMANDE"));
        long totalFact = 0;
        long total = r.getLong("T_HT");
        for (SQLRowAccessor row : rows) {
            if (!row.isForeignEmpty("ID_BON_RECEPTION")) {
                SQLRowAccessor rowFact = row.getForeign("ID_BON_RECEPTION");
                Long l = rowFact.getLong("TOTAL_HT");
                totalFact += l;
            }
        }
        if (total > 0) {
            return new BigDecimal(totalFact).divide(new BigDecimal(total), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP);
        } else {
            return BigDecimal.ONE.movePointRight(2);
        }
    }

}