OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | 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.supplychain.stock.element;

import org.openconcerto.sql.model.FieldPath;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.ui.table.AbstractTreeTableModel;
import org.openconcerto.ui.table.TreeTableModel;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class StockTreeModel extends AbstractTreeTableModel {
    static final SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    public StockTreeModel(Object root) {
        super(root);

    }

    @Override
    public Object getChild(Object parent, int index) {
        if (parent instanceof StockRootNode) {
            return ((StockRootNode) parent).getChild(index);
        }
        if (parent instanceof StockNode) {
            return ((StockNode) parent).getLot(index);
        }
        return null;
    }

    @Override
    public int getChildCount(Object parent) {
        if (parent instanceof StockRootNode) {
            final int size = ((StockRootNode) parent).getSize();
            return size;
        }
        if (parent instanceof StockNode) {
            final int size = ((StockNode) parent).getLotSize();
            return size;
        }
        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        if (node instanceof StockRootNode) {
            return false;
        }
        if (node instanceof StockNode) {
            return ((StockNode) node).getLotSize() == 0;
        }
        return true;
    }

    @Override
    public int getColumnCount() {
        if (((StockRootNode) getRoot()).hasBatch()) {
            return 6;
        }
        return 5;
    }

    @Override
    public String getColumnName(int column) {
        if (column == 0) {
            return "Dépôt";
        }
        if (column >= 1 && column < 5) {
            return ((StockRootNode) getRoot()).getLabelFor(column);
        }
        if (column == 5) {
            return "DLC/DLUO";
        }
        return "";
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Class getColumnClass(int column) {
        if (column == 0) {
            return TreeTableModel.class;
        }
        if (column > 0 && column < 5) {
            return Float.class;
        }
        if (column == 5) {
            return Date.class;
        }
        return String.class;
    }

    @Override
    public Object getValueAt(Object node, int column) {
        if (node instanceof StockRootNode) {
            return null;
        }
        if (node instanceof StockNode) {
            final StockNode n = ((StockNode) node);
            if (column >= 5) {
                return null;
            }
            final StockRootNode root = ((StockRootNode) getRoot());
            final FieldPath fieldPath = root.getFieldPath(column);
            return n.getValueAt(fieldPath, column);
        }
        if (node instanceof BatchNode) {
            final BatchNode rLot = (BatchNode) node;
            if (column == 4) {
                return rLot.getValues().getBigDecimal("QUANTITE").floatValue();
            }
            if (column == 5) {
                final Calendar c1 = rLot.getValues().getDate("DLC");
                final Calendar c2 = rLot.getValues().getDate("DUO");
                if (c1 == null && c2 != null) {
                    return c2.getTime();
                }
                if (c2 == null && c1 != null) {
                    return c1.getTime();
                }
                if (c1 != null && c2 != null) {
                    if (c1.before(c2)) {
                        return c1.getTime();
                    } else {
                        return c2.getTime();
                    }
                } else {
                    return null;
                }

            }

        }

        return null;
    }

    public void load(SQLRowAccessor article) {
        ((StockRootNode) getRoot()).load(article);
        fireTreeStructureChanged(this, new Object[] { getRoot() }, new int[] { 0 }, new Object[] {});
    }

}

class BatchNode {
    private final SQLRowValues sqlRowValues;
    private String str;

    public BatchNode(SQLRowValues sqlRowValues) {
        this.sqlRowValues = sqlRowValues;
        final StringBuilder b = new StringBuilder();
        if (!sqlRowValues.getString("NUMERO_LOT").isEmpty()) {
            b.append(sqlRowValues.getString("NUMERO_LOT"));

        }
        if (!sqlRowValues.getString("NUMERO_SERIE").isEmpty()) {
            b.append(sqlRowValues.getString("NUMERO_SERIE"));
            b.append(" ");
        }

        if (sqlRowValues.getDate("DLC") != null) {
            b.append(" DLC: ");
            b.append(StockTreeModel.df.format(sqlRowValues.getDate("DLC").getTime()));
            b.append(" ");
        }
        if (sqlRowValues.getDate("DLUO") != null) {
            b.append(" DLUO: ");
            b.append(StockTreeModel.df.format(sqlRowValues.getDate("DLUO").getTime()));
            b.append(" ");
        }
        this.str = b.toString().trim();
    }

    @Override
    public String toString() {
        return this.str;
    }

    public SQLRowValues getValues() {
        return this.sqlRowValues;
    }
}

class StockNode {
    private final SQLRowValues sqlRowValues;
    private final List<BatchNode> lots;

    public StockNode(SQLRowValues sqlRowValues, List<BatchNode> lots) {
        this.sqlRowValues = sqlRowValues;
        this.lots = lots;
    }

    public int getLotSize() {
        if (this.lots == null)
            return 0;
        else
            return this.lots.size();
    }

    public Object getValueAt(FieldPath fieldPath, int columnIndex) {
        return this.sqlRowValues.followPath(fieldPath.getPath()).getObject(fieldPath.getFieldName());
    }

    public BatchNode getLot(int index) {
        if (this.lots == null)
            return null;
        return this.lots.get(index);
    }

    @Override
    public String toString() {
        return this.sqlRowValues.getForeign("ID_DEPOT_STOCK").getString("NOM");
    }
}