OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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 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.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.common.ui.ListeViewPanel;
import org.openconcerto.erp.core.sales.invoice.report.EtatStockInventaireXmlSheet;
import org.openconcerto.sql.element.GroupSQLComponent;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
import org.openconcerto.sql.model.SQLDataSource;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.utils.SQLUtils;
import org.openconcerto.sql.view.IListFrame;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.ui.FrameUtil;
import org.openconcerto.ui.SwingThreadUtils;
import org.openconcerto.utils.ExceptionHandler;

import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class EtatStockSQLElement extends ComptaSQLConfElement {

    public EtatStockSQLElement() {
        super("ETAT_STOCK");
        setDefaultGroup(new EtatStockGroup());
        {
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Créer un état de stock") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    EtatStockSnapshotCreator creator = new EtatStockSnapshotCreator(new Date(), getTable().getDBRoot());
                    creator.create();
                }
            }, true);
            action.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
            getRowActions().add(action);
        }
        {
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Voir les articles") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    final SQLElement element = getDirectory().getElement("ETAT_STOCK_ELEMENT");
                    SQLTableModelSource source = element.createTableSource(new Where(element.getTable().getField("ID_ETAT_STOCK"), "=", IListe.get(e).getSelectedId()));
                    final ListeViewPanel panel = new ListeViewPanel(element, new IListe(source));
                    IListFrame frame = new IListFrame(panel);
                    FrameUtil.show(frame);
                }
            }, true);
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
            getRowActions().add(action);
        }

        {
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Export pour inventaire") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    EtatStockInventaireXmlSheet sheet = new EtatStockInventaireXmlSheet(IListe.get(e).getSelectedRow().asRow());
                    try {
                        sheet.createDocument();
                        sheet.showPrintAndExport(true, false, false, false, false);
                    } catch (Exception e1) {
                        ExceptionHandler.handle("Erreur lors de la création de l'inventaire", e1);
                    }
                }
            }, true);
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
            getRowActions().add(action);
        }

        {
            PredicateRowAction action = new PredicateRowAction(new AbstractAction("Import Inventaire") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
                    final FileDialog fd = new FileDialog(frame, "Import Inventaire", FileDialog.LOAD);
                    fd.setVisible(true);
                    if (fd.getFile() != null) {
                        final File f = new File(fd.getDirectory(), fd.getFile());
                        if (!f.exists()) {
                            JOptionPane.showMessageDialog(null, "Le ficher selectionné n'existe pas", "Erreur", JOptionPane.ERROR_MESSAGE);
                        } else if (f.isDirectory()) {
                            JOptionPane.showMessageDialog(null, "Le fichier selectionné n'est pas valide", "Erreur", JOptionPane.ERROR_MESSAGE);
                        } else {
                            new Thread(new Runnable() {

                                @Override
                                public void run() {
                                    final InventaireFromEtatStockImporter impoter = new InventaireFromEtatStockImporter();
                                    try {
                                        SQLUtils.executeAtomic(getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, IOException>() {
                                            @Override
                                            public Object handle(final SQLDataSource ds) throws SQLException, IOException {

                                                impoter.importArticles(f, getTable().getDBRoot());
                                                return null;
                                            }
                                        });
                                    } catch (Exception e1) {
                                        ExceptionHandler.handle("Erreur lors de l'importation", e1);
                                    }

                                }
                            }).start();
                        }
                    }
                }
            }, true);
            action.setPredicate(IListeEvent.getSingleSelectionPredicate());
            getRowActions().add(action);
        }
    }

    @Override
    protected List<String> getListFields() {
        final List<String> l = new ArrayList<>(3);
        l.add("DATE");
        l.add("MONTANT_HA");
        l.add("INVENTAIRE");
        return l;
    }

    @Override
    protected List<String> getComboFields() {
        final List<String> l = new ArrayList<>(2);
        l.add("DATE");
        l.add("MONTANT_HA");
        return l;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.SQLElement#getComponent()
     */
    public SQLComponent createComponent() {
        return new GroupSQLComponent(this) {

            @Override
            public JComponent createEditor(String id) {
                if (id.equals("supplychain.stock.state.items")) {
                    return new EtatStockTable((JTextField) getEditor("MONTANT_HA"));
                } else {
                    return super.createEditor(id);
                }
            }

            @Override
            public void select(SQLRowAccessor r) {
                super.select(r);
                EtatStockTable table = (EtatStockTable) getEditor("supplychain.stock.state.items");
                if (r != null) {
                    table.insertFrom("ID_ETAT_STOCK", r.getID());
                }
            }

            @Override
            public void update() {
                super.update();
                EtatStockTable table = (EtatStockTable) getEditor("supplychain.stock.state.items");
                table.updateField("ID_ETAT_STOCK", getSelectedID());
            }
        };
    }

    @Override
    protected String createCode() {
        return createCodeOfPackage() + ".state";
    }

}