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.erp.core.sales.product.element.LotSQLElement;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.FieldPath;
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.model.graph.Path;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StockRootNode {
    private final List<StockNode> children = new ArrayList<>();
    private final List<SQLRowValues> listRowValsStock = new ArrayList<>();
    private final List<FieldPath> cols = new ArrayList<>();
    private final SQLElement eltStock;
    private boolean hasBatch = false;

    public StockRootNode(SQLElement e) {
        this.eltStock = e;
        final SQLTable tableStock = this.eltStock.getTable();
        final Path pStock = new Path(tableStock);
        final Path pDepotStock = pStock.add(tableStock.getField("ID_DEPOT_STOCK"));
        this.cols.add(new FieldPath(pDepotStock, "NOM"));
        this.cols.add(new FieldPath(pStock, "QTE_TH"));
        this.cols.add(new FieldPath(pStock, "QTE_RECEPT_ATTENTE"));
        this.cols.add(new FieldPath(pStock, "QTE_LIV_ATTENTE"));
        this.cols.add(new FieldPath(pStock, "QTE_REEL"));

    }

    public void load(SQLRowAccessor article) {

        this.children.clear();
        final SQLTable tableStock = this.eltStock.getTable();
        final SQLRowValues rowValsStock = new SQLRowValues(tableStock);
        rowValsStock.putNulls("QTE_TH", "QTE_RECEPT_ATTENTE", "QTE_LIV_ATTENTE", "QTE_REEL");
        rowValsStock.putRowValues("ID_DEPOT_STOCK").putNulls("NOM");

        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsStock);
        final List<SQLRowValues> fetch = fetcher.fetch(
                new Where(tableStock.getField("ID_ARTICLE"), "=", article.getID()).and(new Where(tableStock.getField("QTE_REEL"), "!=", 0).or(new Where(tableStock.getField("QTE_TH"), "!=", 0))));
        this.listRowValsStock.clear();
        this.listRowValsStock.addAll(fetch);
        final List<Integer> idsStock = new ArrayList<>();
        for (SQLRowValues r : this.listRowValsStock) {
            idsStock.add(r.getID());
        }

        final SQLTable tableLot = this.eltStock.getDirectory().getElement(LotSQLElement.class).getTable();
        SQLRowValues rLot = new SQLRowValues(tableLot);
        rLot.putNulls("ID_STOCK", "QUANTITE", "NUMERO_LOT", "NUMERO_SERIE", "DLC", "DLUO");
        final List<SQLRowValues> fetchLots = SQLRowValuesListFetcher.create(rLot).fetch(Where.inValues(tableLot.getField("ID_STOCK"), idsStock));
        this.hasBatch = !fetchLots.isEmpty();

        final Map<Integer, List<BatchNode>> stockLots = new HashMap<>();
        for (SQLRowValues lot : fetchLots) {
            int idStock = lot.getInt("ID_STOCK");
            List<BatchNode> lots = stockLots.get(idStock);
            if (lots == null) {
                lots = new ArrayList<>();
                stockLots.put(idStock, lots);
            }
            lots.add(new BatchNode(lot));
        }
        for (SQLRowValues r : this.listRowValsStock) {
            this.children.add(new StockNode(r, stockLots.get(r.getID())));
        }
    }

    public boolean hasBatch() {
        return this.hasBatch;
    }

    public StockNode getChild(int i) {
        return this.children.get(i);
    }

    public int getSize() {
        return this.children.size();
    }

    public String getLabelFor(int column) {
        return this.eltStock.getDirectory().getTranslator().getLabelFor(this.cols.get(column).getField()).replace("Quantité", "Quantité\n");
    }

    public FieldPath getFieldPath(int columnIndex) {
        final FieldPath fieldPath = this.cols.get(columnIndex);
        return fieldPath;
    }

}