OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 169 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
76 ilm 1
/*
2
 * Créé le 18 mai 2012
3
 */
4
package org.openconcerto.modules.subscription.panel;
5
 
6
import java.sql.SQLException;
7
import java.util.Date;
8
import java.util.List;
9
 
10
import javax.swing.JOptionPane;
11
 
12
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
13
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
14
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
15
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieVenteFacture;
16
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
17
import org.openconcerto.sql.Configuration;
181 ilm 18
import org.openconcerto.sql.element.SQLElement;
76 ilm 19
import org.openconcerto.sql.model.IResultSetHandler;
20
import org.openconcerto.sql.model.SQLDataSource;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowAccessor;
23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLSelect;
25
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.sql.model.Where;
27
import org.openconcerto.sql.view.list.RowAction;
28
 
29
public class FacturesAboPanel extends AboPanel {
30
 
31
    public FacturesAboPanel() {
32
        super(Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE"), Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE_ELEMENT"), "FACTURE");
33
    }
34
 
35
    private final SQLTable tableNum = Configuration.getInstance().getRoot().findTable("NUMEROTATION_AUTO");
181 ilm 36
    private SQLElement eltFact = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
76 ilm 37
 
38
    @Override
39
    protected void validItem(SQLRowAccessor sqlRowAccessor) {
40
        // Affectation d'un numero
141 ilm 41
        SQLRowValues rowVals = sqlRowAccessor.createEmptyUpdateRow();
181 ilm 42
        String nextNumero = NumerotationAutoSQLElement.getNextNumero(this.eltFact.getClass());
141 ilm 43
        rowVals.put("DATE", new Date());
76 ilm 44
        rowVals.put("NUMERO", nextNumero);
45
 
46
        SQLRowValues rowValsNum = new SQLRowValues(tableNum);
47
 
181 ilm 48
        String labelNumberFor = NumerotationAutoSQLElement.getLabelNumberFor(this.eltFact.getClass());
76 ilm 49
        int val = tableNum.getRow(2).getInt(labelNumberFor);
50
        val++;
51
        rowValsNum.put(labelNumberFor, Integer.valueOf(val));
52
 
53
        if (!checkUniciteNumero(nextNumero, sqlRowAccessor.getID())) {
54
            JOptionPane.showMessageDialog(null, "Impossible de valider les factures. La numérotation automatique n'est pas correcte.");
55
            return;
56
        }
57
 
58
        try {
59
            rowValsNum.update(2);
60
        } catch (SQLException e) {
61
            e.printStackTrace();
62
        }
63
 
64
        // Validation
65
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.TRUE);
66
        rowVals.put("PREVISIONNELLE", Boolean.FALSE);
67
        try {
68
            rowVals.update();
69
        } catch (SQLException exn) {
70
            exn.printStackTrace();
71
        }
72
 
73
        int idMvt = 1;
74
        // Création des écritures associées
75
        if (sqlRowAccessor.getObject("ID_MOUVEMENT") != null) {
76
 
77
            idMvt = sqlRowAccessor.getInt("ID_MOUVEMENT");
78
 
152 ilm 79
            if (idMvt > 1) {
80
                // on supprime tout ce qui est lié à la facture
81
                System.err.println("Archivage des fils");
82
                EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
83
                eltEcr.archiveMouvementProfondeur(idMvt, false);
84
            }
76 ilm 85
        }
86
 
87
        System.err.println("Regeneration des ecritures");
88
        if (idMvt > 1) {
89
            new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID(), idMvt);
90
        } else {
91
            new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID());
92
        }
93
        System.err.println("Fin regeneration");
94
    }
95
 
96
    private boolean checkUniciteNumero(String num, int idFact) {
97
        SQLTable tableFacture = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE");
169 ilm 98
        final SQLSelect selNum = new SQLSelect();
76 ilm 99
        selNum.addSelect(tableFacture.getKey(), "COUNT");
100
        final Where w = new Where(tableFacture.getField("NUMERO"), "=", num);
101
        selNum.setWhere(w);
102
        selNum.andWhere(new Where(tableFacture.getKey(), "!=", idFact));
103
 
104
        final String req = selNum.asString();
105
        final Number l = (Number) tableFacture.getBase().getDataSource().execute(req, new IResultSetHandler(SQLDataSource.SCALAR_HANDLER, false));
106
        return (l == null || l.intValue() == 0);
107
 
108
    }
109
 
110
    @Override
111
    protected void injectRow(SQLRow row, SQLRowValues rowVals, Date dateNew, SQLRow rowAbonnement) {
112
        super.injectRow(row, rowVals, dateNew, rowAbonnement);
181 ilm 113
        rowVals.put("NUMERO", "ABO--" + NumerotationAutoSQLElement.getNextNumero(this.eltFact.getClass()));
76 ilm 114
        rowVals.put("ID_ADRESSE", row.getObject("ID_ADRESSE"));
115
        rowVals.put("ID_COMPTE_PCE_SERVICE", row.getObject("ID_COMPTE_PCE_SERVICE"));
116
        rowVals.put("PORT_HT", row.getObject("PORT_HT"));
169 ilm 117
        rowVals.put("NET_A_PAYER", row.getObject("T_TTC"));
76 ilm 118
        rowVals.put("REMISE_HT", row.getObject("REMISE_HT"));
119
 
120
        rowVals.put("NOM", row.getObject("NOM"));
121
        rowVals.put("ID_CONTACT", row.getObject("ID_CONTACT"));
122
        rowVals.put("ID_COMPTE_PCE_VENTE", row.getObject("ID_COMPTE_PCE_VENTE"));
123
        rowVals.put("ID_DEVIS", row.getObject("ID_DEVIS"));
124
        rowVals.put("INFOS", row.getObject("INFOS"));
125
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.FALSE);
126
        rowVals.put("PREVISIONNELLE", Boolean.TRUE);
127
 
128
        // Mode de reglement
129
        SQLRow rowMdr = row.getForeignRow("ID_MODE_REGLEMENT");
130
        SQLRowValues rowValsMdr = rowMdr.asRowValues();
131
        rowValsMdr.clearPrimaryKeys();
132
        rowVals.put("ID_MODE_REGLEMENT", rowValsMdr);
133
    }
134
 
135
    @Override
136
    protected List<RowAction> getAdditionnalRowActions() {
137
        return new MouseSheetXmlListeListener(VenteFactureXmlSheet.class).getRowActions();
138
    }
139
 
140
}