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

import org.openconcerto.erp.core.sales.pos.model.TicketItem;
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import javax.swing.JPanel;

public class StockErrorPanel extends JPanel {
    private final CaisseControler controller;

    public StockErrorPanel(CaisseFrame caisseFrame, Map<TicketItem, Integer> missingQty, Runnable runnable) {
        this.controller = caisseFrame.getControler();
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0;
        c.insets = new Insets(5, 5, 5, 5);
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.NONE;

        int missing = 0;
        for (Integer q : missingQty.values()) {
            missing += q;
        }
        String label = "un article n'est pas en stock";
        if (missing > 1) {
            label = missing + " articles ne sont pas en stock";
        }
        POSLabel labels = new POSLabel(label + " : ");
        this.add(labels, c);

        for (Map.Entry<TicketItem, Integer> e : missingQty.entrySet()) {
            c.gridy++;
            final String declinaison = e.getKey().getArticle().getDeclinaison();
            if (declinaison != null && declinaison.trim().isEmpty()) {
                this.add(new POSLabel(e.getKey().getArticle().getName() + " " + declinaison + " : " + e.getValue()), c);
            } else {
                this.add(new POSLabel(e.getKey().getArticle().getName() + " : " + e.getValue()), c);
            }
        }
        c.fill = GridBagConstraints.NONE;
        // Récupération des dépôts
        final SQLElementDirectory dir = caisseFrame.getConf().getDirectory();
        final SQLTable tDepot = dir.getElement(DepotStockSQLElement.class).getTable();
        final SQLTable tArticle = dir.getElement(ReferenceArticleSQLElement.class).getTable();

        final SQLSelect select = new SQLSelect();
        select.addSelect(tDepot.getField("ID"));
        select.addSelect(tDepot.getField("NOM"));
        for (final SQLRow rowDepotArrivee : SQLRowListRSH.execute(select)) {
            final int idDepotSource = rowDepotArrivee.getInt("ID");
            final String nomDepot = rowDepotArrivee.getString("NOM");
            final int idDepotIDestination = caisseFrame.getPOSConf().getDepotID();
            if (idDepotSource != idDepotIDestination) {
                POSButton b = new POSButton("Créer un mouvement de stock depuis le dépôt " + nomDepot);
                b.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ev) {
                        System.err.println("StockErrorPanel.StockErrorPanel(...) mouvement de stock");
                        final SQLRow selectedRowDepotDepart = tDepot.getRow(idDepotSource);
                        final Date date = new Date();
                        for (Map.Entry<TicketItem, Integer> e : missingQty.entrySet()) {
                            final int idArticle = e.getKey().getArticle().getId();
                            final BigDecimal qteReel = new BigDecimal(e.getValue());
                            final SQLRow selectedRowArticle = tArticle.getRow(idArticle);
                            dir.getElement(MouvementStockSQLElement.class).transfertStock(qteReel, date, selectedRowArticle, selectedRowDepotDepart, rowDepotArrivee, "transfert depuis caisse");
                        }
                        caisseFrame.showCaisse();
                        runnable.run();
                    }
                });
                c.gridy++;
                this.add(b, c);
            }
        }
        POSButton b = new POSButton("Ignorer le problème de stock");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.err.println("StockErrorPanel.StockErrorPanel(...) ignore stock");
                caisseFrame.showCaisse();
                runnable.run();

            }
        });
        c.gridy++;
        this.add(b, c);
        POSButton b2 = new POSButton("Annuler la validation du ticket");
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.err.println("StockErrorPanel.StockErrorPanel(...) annulation");
                caisseFrame.showCaisse();

            }
        });
        c.gridy++;
        this.add(b2, c);
    }

}