OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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.sales.order.element;

import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.common.ui.DeviseTableCellRenderer;
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeCellRenderer;
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClient;
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClientComboBox;
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeRowItemView;
import org.openconcerto.sql.element.BaseSQLComponent;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.element.UISQLComponent;
import org.openconcerto.sql.model.FieldPath;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.graph.Path;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanelListener;
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.RowAction;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.utils.CollectionUtils;
import org.openconcerto.utils.DecimalUtils;
import org.openconcerto.utils.ExceptionHandler;

import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractAction;

public class FacturationCommandeClientSQLElement extends ComptaSQLConfElement {

    public static final String TABLENAME = "FACTURATION_COMMANDE_CLIENT";

    public FacturationCommandeClientSQLElement() {
        super(TABLENAME, "un terme de facturation de commande client", "termes de facturation commandes clients");
        {
            RowAction action = new RowAction(new AbstractAction("Facturer") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    final SQLRow sel = IListe.get(e).getSelectedRow().asRow();
                    sel.fetchValues();
                    CommandeClientSQLElement eltCmd = (CommandeClientSQLElement) getForeignElement("ID_COMMANDE_CLIENT");
                    int typeFacture = sel.getInt("TYPE_FACTURE");
                    final SQLRow asRow = sel.getForeign("ID_COMMANDE_CLIENT").asRow();
                    asRow.fetchValues();
                    List<SQLRowValues> l = Arrays.asList(asRow.asRowValues());
                    final EditFrame f;
                    if (typeFacture == TypeFactureCommandeClient.SITUATION.getId()) {
                        f = eltCmd.transfertAcompteClient(l);
                        SQLRowValues rowValues = new SQLRowValues(getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable());
                        rowValues.put("POURCENT_FACTURABLE", sel.getObject("POURCENT"));
                        rowValues.put("MONTANT_FACTURABLE", null);
                        ((BaseSQLComponent) f.getSQLComponent()).getView("MONTANT_FACTURABLE,POURCENT_FACTURABLE").show(rowValues);
                        ;

                    } else if (typeFacture == TypeFactureCommandeClient.SOLDE.getId()) {
                        f = eltCmd.transfertSoldeClient(l);

                    } else {
                        f = eltCmd.transfertFactureClient(l);
                    }

                    SQLRowValues rowValues = new SQLRowValues(eltCmd.getTable().getTable("MODE_REGLEMENT").getTable());
                    rowValues.put("ID_TYPE_REGLEMENT", sel.getForeignID("ID_TYPE_REGLEMENT"));
                    rowValues.put("FIN_MOIS", Boolean.FALSE);
                    if (sel.getBoolean("COMPTANT")) {
                        rowValues.put("AJOURS", 0);
                        rowValues.put("LENJOUR", 0);
                        rowValues.put("DATE_FACTURE", Boolean.TRUE);
                        rowValues.put("COMPTANT", Boolean.TRUE);
                    } else {
                        rowValues.put("AJOURS", sel.getObject("AJOURS"));
                        int lenjour = sel.getInt("LENJOUR");
                        if (sel.getBoolean("FIN_MOIS")) {
                            lenjour = 31;
                        }
                        rowValues.put("LENJOUR", lenjour);
                        rowValues.put("COMPTANT", Boolean.FALSE);
                        rowValues.put("FIN_MOIS", lenjour == 31);
                        rowValues.put("DATE_FACTURE", lenjour == 0);
                    }

                    ((BaseSQLComponent) f.getSQLComponent()).getView("ID_MODE_REGLEMENT").show(rowValues);
                    ;

                    f.addEditPanelListener(new EditPanelListener() {

                        @Override
                        public void modified() {

                        }

                        @Override
                        public void inserted(int id) {
                            try {
                                sel.createEmptyUpdateRow().put("ID_SAISIE_VENTE_FACTURE", id).commit();
                                SQLTable tableFacture = getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable();
                                SQLRowValues rowValsFact = new SQLRowValues(tableFacture);
                                rowValsFact.put(tableFacture.getKey().getName(), id);
                                rowValsFact.put("ID_FACTURATION_COMMANDE_CLIENT", sel.getID());
                                rowValsFact.commit();
                            } catch (SQLException e) {
                                ExceptionHandler.handle("Erreur lors de l'affectation de la facture", e);
                            }
                        }

                        @Override
                        public void deleted() {
                        }

                        @Override
                        public void cancelled() {

                        }
                    });
                }
            }, true) {
                @Override
                public boolean enabledFor(List<SQLRowValues> selection) {

                    if (selection != null && selection.size() == 1) {
                        SQLRowAccessor sel = selection.get(0);
                        return sel.isForeignEmpty("ID_SAISIE_VENTE_FACTURE");
                    }
                    return false;
                }
            };

            getRowActions().add(action);
        }

        {
            RowAction action = new RowAction(new AbstractAction("Voir la facture") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    final SQLRowAccessor sel = IListe.get(e).getSelectedRow();
                    SQLRowAccessor fact = sel.getForeign("ID_SAISIE_VENTE_FACTURE");
                    VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(fact.asRow());
                    try {
                        sheet.showPreviewDocument();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }, true) {
                @Override
                public boolean enabledFor(List<SQLRowValues> selection) {

                    if (selection != null && selection.size() == 1) {
                        SQLRowAccessor sel = selection.get(0);
                        return !sel.isForeignEmpty("ID_SAISIE_VENTE_FACTURE");
                    }
                    return false;
                }
            };

            getRowActions().add(action);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.BaseSQLElement#getComboFields()
     */
    protected List<String> getComboFields() {
        final List<String> l = new ArrayList<>(1);
        l.add("NOM");
        return l;
    }

    @Override
    public Set<String> getReadOnlyFields() {
        Set<String> s = new HashSet<>(1);
        s.add("CHOICE");
        return s;
    }

    @Override
    protected String getParentFFName() {
        return "ID_COMMANDE_CLIENT";
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.BaseSQLElement#getListFields()
     */
    protected List<String> getListFields() {
        final List<String> l = new ArrayList<>(7);
        l.add("NOM");
        l.add("TYPE_FACTURE");
        l.add("ID_TYPE_REGLEMENT");
        l.add("POURCENT");
        l.add("DATE_PREVISIONNELLE");
        l.add("ID_COMMANDE_CLIENT");
        l.add("ID_SAISIE_VENTE_FACTURE");
        return l;
    }

    @Override
    protected void _initTableSource(SQLTableModelSource res) {
        res.init();
        res.getColumn(getTable().getField("POURCENT")).setRenderer(new DeviseTableCellRenderer());
        res.getColumn(getTable().getField("TYPE_FACTURE")).setRenderer(new TypeFactureCommandeCellRenderer());
        final BaseSQLTableModelColumn pourcentFact = new BaseSQLTableModelColumn("Montant à facturer", BigDecimal.class) {

            @Override
            protected Object show_(SQLRowAccessor r) {
                long ht = r.getForeign("ID_COMMANDE_CLIENT").getLong("T_TTC");
                BigDecimal percent = r.getBigDecimal("POURCENT");
                return percent.movePointLeft(2).multiply(new BigDecimal(ht).movePointLeft(2), DecimalUtils.HIGH_PRECISION);
            }

            @Override
            public Set<FieldPath> getPaths() {
                Path p = new Path(getTable());
                Path p2 = p.add(p.getFirst().getField("ID_COMMANDE_CLIENT"));
                return CollectionUtils.createSet(new FieldPath(p, "POURCENT"), new FieldPath(p2, "T_TTC"));
            }
        };
        pourcentFact.setRenderer(new DeviseTableCellRenderer());
        res.getColumns().add(pourcentFact);

        final BaseSQLTableModelColumn montantFact = new BaseSQLTableModelColumn("Montant facturé", BigDecimal.class) {

            @Override
            protected Object show_(SQLRowAccessor r) {
                Collection<? extends SQLRowAccessor> rows = r.getReferentRows(getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable());
                long ht = 0;
                for (SQLRowAccessor sqlRowAccessor : rows) {
                    ht += sqlRowAccessor.getLong("T_TTC");
                }

                return new BigDecimal(ht).movePointLeft(2);
            }

            @Override
            public Set<FieldPath> getPaths() {
                Path p = new Path(getTable());
                Path p2 = p.add(p.getFirst().getField("ID_COMMANDE_CLIENT"));
                Path p3 = new Path(getTable());
                p3 = p3.add(getTable().getForeignTable("ID_SAISIE_VENTE_FACTURE").getField("ID_FACTURATION_COMMANDE_CLIENT"));
                return CollectionUtils.createSet(new FieldPath(p, "POURCENT"), new FieldPath(p2, "T_TTC"), new FieldPath(p3, "T_TTC"), new FieldPath(p2, "T_HT"), new FieldPath(p3, "T_HT"));
            }
        };
        montantFact.setRenderer(new DeviseTableCellRenderer());
        res.getColumns().add(montantFact);
        super._initTableSource(res);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.openconcerto.devis.SQLElement#getComponent()
     */
    public SQLComponent createComponent() {
        return new UISQLComponent(this) {
            public void addViews() {
                this.addView("NOM");
                this.addView("POURCENT");
                this.addView("ID_TYPE_REGLEMENT");
                this.addView("AJOURS");
                this.addView("COMPTANT");
                this.addView("CHOICE");

                TypeFactureCommandeClientComboBox box = new TypeFactureCommandeClientComboBox();

                this.addView(new TypeFactureCommandeRowItemView(box), "TYPE_FACTURE", REQ);
                this.addView("ID_MODELE");
            }

            @Override
            protected SQLRowValues createDefaults() {
                SQLRowValues rowVals = new SQLRowValues(getTable());
                rowVals.put("CHOICE", Boolean.TRUE);
                return rowVals;
            }
        };
    }

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

}