OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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-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.common.ui;

import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.view.list.AutoCompletionManager;
import org.openconcerto.sql.view.list.RowValuesTable;
import org.openconcerto.sql.view.list.RowValuesTableModel;
import org.openconcerto.sql.view.list.SQLTableElement;
import org.openconcerto.sql.view.list.ValidStateChecker;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Set;

public class ItemAutoCompletionManager extends AutoCompletionManager {
    private final AbstractVenteArticleItemTable t;

    public ItemAutoCompletionManager(AbstractVenteArticleItemTable t, SQLTableElement tableElementCode, SQLField field, RowValuesTable table, RowValuesTableModel rowValuesTableModel) {
        super(tableElementCode, field, table, rowValuesTableModel);
        this.t = t;
    }

    public ItemAutoCompletionManager(AbstractVenteArticleItemTable t, SQLTableElement tableElementArticle, SQLField field, RowValuesTable table, RowValuesTableModel rowValuesTableModel,
            int modeContains, boolean b, boolean c, ValidStateChecker validStateChecker) {
        super(tableElementArticle, field, table, rowValuesTableModel, modeContains, b, c, validStateChecker);
        this.t = t;
    }

    @Override
    public void fillRowValues(SQLRowAccessor from, Set<String> fields, SQLRowValues to) {

        boolean piece = from.getForeignID("ID_UNITE_VENTE") == UniteVenteArticleSQLElement.A_LA_PIECE;

        for (String fromField : fields) {
            String toField = this.getToField(fromField);
            final Object valueFrom = this.getValueFrom(from.asRow(), fromField, to);
            if (fromField.equals("POURCENT_REMISE") && valueFrom != null && valueFrom instanceof Acompte) {
                final Acompte acompte = (Acompte) valueFrom;
                to.put("POURCENT_REMISE", acompte.getPercent());
                to.put("MONTANT_REMISE", acompte.getMontant());
            } else {
                if (piece && toField.equals("QTE_UNITAIRE")) {
                    to.put(toField, BigDecimal.ONE);
                    // Pour les pièces commercials de vente
                    to.put("QTE", from.getBigDecimal("QTE_UNITAIRE").setScale(0, RoundingMode.HALF_UP).intValue());
                    // Pour les bons de livraison
                    if (to.getTable().getName().equals("BON_DE_LIVRAISON_ELEMENT")) {
                        to.put("QTE_A_LIVRER", from.getBigDecimal("QTE_UNITAIRE").setScale(0, RoundingMode.HALF_UP).intValue());
                    }
                } else {
                    to.put(toField, valueFrom);
                }
            }
        }
    }

    @Override
    protected Object getValueFrom(SQLRow from, String field, SQLRowAccessor rowDest) {

        final boolean fromArticleFournisseur = from != null && from.getTable().getName().equals("ARTICLE_FOURNISSEUR");
        if (fromArticleFournisseur && field.equals("ID_ARTICLE")) {
            return this.t.getSQLElement().getTable().getForeignTable("ID_ARTICLE").getUndefinedID();
        }

        boolean piece = from.getForeignID("ID_UNITE_VENTE") == UniteVenteArticleSQLElement.A_LA_PIECE;
        if (piece) {
            if (field.equals("QTE")) {
                return from.getBigDecimal("QTE_UNITAIRE").setScale(0, RoundingMode.HALF_UP).intValue();
            }
            if (field.equals("QTE_UNITAIRE")) {
                return BigDecimal.ONE;
            }
        } else {
            if (field.equals("QTE")) {
                return Integer.valueOf(1);
            }
        }
        if (fromArticleFournisseur) {
            if (field.equals("POURCENT_REMISE")) {
                return new Acompte(BigDecimal.ZERO, BigDecimal.ZERO);
            } else if (!from.getTable().contains(field)) {
                return null;
            }
        }

        Object res = null;
        if (!fromArticleFournisseur) {
            res = t.tarifCompletion(from, field, rowDest, true);
        }
        if (res == null) {
            res = super.getValueFrom(from, field, rowDest);
        }
        if (!fromArticleFournisseur && field.equals("POURCENT_REMISE")) {
            return t.getRemiseClient(from, (Acompte) res);
        }
        return res;

    }

}