Dépôt officiel du code source de l'ERP OpenConcerto
Rev 182 | 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.supplychain.stock.element;
import org.openconcerto.erp.config.ComptaPropsConfiguration;
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.erp.core.supplychain.stock.report.StockReportPDF;
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
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.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.preferences.SQLPreferences;
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.PanelFrame;
import org.openconcerto.ui.SwingThreadUtils;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.FileUtils;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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) {
PanelFrame frame = new PanelFrame(new EtatStockCreationPanel(getDirectory().getElement(DepotStockSQLElement.class)), "Création état de stock");
FrameUtil.showPacked(frame);
}
}, true);
action.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
getRowActions().add(action);
}
{
PredicateRowAction action = new PredicateRowAction(new AbstractAction("Import Inventaire") {
@Override
public void actionPerformed(ActionEvent e) {
// PanelFrame frame = new PanelFrame(new
// ImportInventairePanel(getDirectory().getElement(DepotStockSQLElement.class)),
// "Import inventaire");
// FrameUtil.showPacked(frame);
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(getDirectory().getElement("ARTICLE"));
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.createSelectionCountPredicate(0, Integer.MAX_VALUE));
getRowActions().add(action);
}
{
PredicateRowAction action = new PredicateRowAction(new AbstractAction("Export d'état pour inventaire") {
@Override
public void actionPerformed(ActionEvent e) {
EtatStockInventaireXmlSheet sheet = new EtatStockInventaireXmlSheet(getDirectory(), SQLRow.getIDs(IListe.get(e).getSelectedRowAccessors()));
try {
sheet.createDocument();
sheet.showPrintAndExport(true, false, false, false, false, Collections.emptyList());
} catch (Exception e1) {
ExceptionHandler.handle("Erreur lors de la création de l'inventaire", e1);
}
}
}, true);
action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
getRowActions().add(action);
}
{
PredicateRowAction action = new PredicateRowAction(new AbstractAction("Générer tous les états de stock") {
@Override
public void actionPerformed(ActionEvent e) {
SQLSelect sel = new SQLSelect();
sel.addSelectStar(getTable().getForeignTable("ID_DEPOT_STOCK"));
final List<SQLRow> rowsDepot = SQLRowListRSH.execute(sel);
Set<Number> idsEtat = new HashSet<>();
for (SQLRow rowDepot : rowsDepot) {
Date d = new Date();
EtatStockSnapshotCreator creator = new EtatStockSnapshotCreator(rowDepot, d, getTable().getDBRoot(), false);
final int etat = creator.create();
if (etat != SQLRow.NONEXISTANT_ID) {
idsEtat.add(etat);
}
}
if (!idsEtat.isEmpty()) {
EtatStockInventaireXmlSheet sheet = new EtatStockInventaireXmlSheet(getDirectory(), idsEtat);
try {
sheet.createDocument();
sheet.showPrintAndExport(true, false, false, false, false, Collections.emptyList());
} catch (Exception e1) {
ExceptionHandler.handle("Erreur lors de la création de l'inventaire", e1);
}
}
}
}, 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("Inventaire chiffré") {
@Override
public void actionPerformed(ActionEvent e) {
SQLRow r = IListe.get(e).getSelectedRow().fetchNewRow();
try {
String companyName = ComptaPropsConfiguration.getInstanceCompta().getRowSociete().getString("NOM");
final Calendar date = r.getDate("DATE");
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
StockReportPDF pdf = new StockReportPDF(companyName, df.format(date.getTime()));
pdf.addStockDepot(r);
final File f = new File("out.pdf");
pdf.savePDF(f);
FileUtils.openFile(f);
} catch (Exception ex) {
ExceptionHandler.handle("Erreur", ex);
}
}
}, true);
action.setPredicate(IListeEvent.getSingleSelectionPredicate());
getRowActions().add(action);
}
}
@Override
protected List<String> getListFields() {
final List<String> l = new ArrayList<>(3);
l.add("DATE");
final SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
final boolean hasDeclinaison = prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVER_DECLINAISON, false);
if (hasDeclinaison) {
l.add("ID_DEPOT_STOCK");
}
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";
}
}