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.sales.order.ui;

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 java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class ReliquatCommandeTableModel extends AbstractTableModel {

    private List<String> columns = Arrays.asList("Code", "Nom", "Qté cmd", "Reliquat");

    private final List<? extends SQLRowAccessor> values;

    public ReliquatCommandeTableModel(SQLRowAccessor rowCmd) {
        super();
        this.values = getValues(rowCmd);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return String.class;
        } else if (columnIndex == 1) {
            return String.class;
        } else if (columnIndex == 2) {
            return BigDecimal.class;
        } else {

            return BigDecimal.class;

        }
    }

    @Override
    public int getColumnCount() {

        return this.columns.size();
    }

    @Override
    public int getRowCount() {

        return this.values.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        if (columnIndex == 0) {
            return this.values.get(rowIndex).getString("CODE");
        } else if (columnIndex == 1) {
            return this.values.get(rowIndex).getString("NOM");
        } else if (columnIndex == 2) {
            return this.values.get(rowIndex).getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(this.values.get(rowIndex).getString("QTE"))).setScale(2, RoundingMode.HALF_UP);
        } else {
            BigDecimal qteCmd = this.values.get(rowIndex).getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(this.values.get(rowIndex).getString("QTE"))).setScale(2, RoundingMode.HALF_UP);

            SQLRowAccessor rowAccessor = this.values.get(rowIndex);
            String field = rowAccessor.getTable().getName().equals("COMMANDE_ELEMENT") ? "QTE_RECUE" : "QTE_LIVREE";
            BigDecimal qteLivre = rowAccessor.getBigDecimal(field) == null ? BigDecimal.ZERO : this.values.get(rowIndex).getBigDecimal(field).setScale(2, RoundingMode.HALF_UP);
            return qteCmd.subtract(qteLivre);
        }

    }

    @Override
    public String getColumnName(int column) {

        return this.columns.get(column);
    }

    private List<? extends SQLRowAccessor> getValues(SQLRowAccessor rowCmd) {
        final SQLTable tableCmd = rowCmd.getTable();
        final SQLTable table = tableCmd.getTable(tableCmd.getName() + "_ELEMENT");
        SQLRowValues rowVals = new SQLRowValues(table);
        final String fieldQteRecueLivree = tableCmd.getName().equals("COMMANDE") ? "QTE_RECUE" : "QTE_LIVREE";
        rowVals.putNulls("CODE", "NOM", "QTE", "QTE_UNITAIRE", fieldQteRecueLivree);

        final Where w = new Where(table.getField("ID_" + tableCmd.getName()), "=", rowCmd.getID());
        final Where w2 = Where
                .createRaw(table.getField(fieldQteRecueLivree).getQuotedName() + " < (" + table.getField("QTE").getQuotedName() + "*" + table.getField("QTE_UNITAIRE").getQuotedName() + ")",
                        table.getField("QTE_UNITAIRE"), table.getField("QTE"), table.getField(fieldQteRecueLivree))
                .or(Where.isNull(table.getField(fieldQteRecueLivree)));

        final String fieldRecueLivree = tableCmd.getName().equals("COMMANDE") ? "RECU" : "LIVRE";
        final String fieldRecueLivreeForced = tableCmd.getName().equals("COMMANDE") ? "RECU_FORCED" : "LIVRE_FORCED";
        return SQLRowValuesListFetcher.create(rowVals)
                .fetch(w.and(w2).and(new Where(table.getField(fieldRecueLivree), "=", Boolean.FALSE)).and(new Where(table.getField(fieldRecueLivreeForced), "=", Boolean.FALSE)));
    }
}