OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 76 | Rev 152 | Go to most recent revision | 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.element.SaisieVenteFactureSQLElement;
15
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
16
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieVenteFacture;
17
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
18
import org.openconcerto.sql.Configuration;
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");
36
 
37
    @Override
38
    protected void validItem(SQLRowAccessor sqlRowAccessor) {
39
        // Affectation d'un numero
141 ilm 40
        SQLRowValues rowVals = sqlRowAccessor.createEmptyUpdateRow();
76 ilm 41
        String nextNumero = NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class);
141 ilm 42
        rowVals.put("DATE", new Date());
76 ilm 43
        rowVals.put("NUMERO", nextNumero);
44
 
45
        SQLRowValues rowValsNum = new SQLRowValues(tableNum);
46
 
47
        String labelNumberFor = NumerotationAutoSQLElement.getLabelNumberFor(SaisieVenteFactureSQLElement.class);
48
        int val = tableNum.getRow(2).getInt(labelNumberFor);
49
        val++;
50
        rowValsNum.put(labelNumberFor, Integer.valueOf(val));
51
 
52
        if (!checkUniciteNumero(nextNumero, sqlRowAccessor.getID())) {
53
            JOptionPane.showMessageDialog(null, "Impossible de valider les factures. La numérotation automatique n'est pas correcte.");
54
            return;
55
        }
56
 
57
        try {
58
            rowValsNum.update(2);
59
        } catch (SQLException e) {
60
            e.printStackTrace();
61
        }
62
 
63
        // Validation
64
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.TRUE);
65
        rowVals.put("PREVISIONNELLE", Boolean.FALSE);
66
        try {
67
            rowVals.update();
68
        } catch (SQLException exn) {
69
            exn.printStackTrace();
70
        }
71
 
72
        int idMvt = 1;
73
        // Création des écritures associées
74
        if (sqlRowAccessor.getObject("ID_MOUVEMENT") != null) {
75
 
76
            idMvt = sqlRowAccessor.getInt("ID_MOUVEMENT");
77
 
78
            // on supprime tout ce qui est lié à la facture
79
            System.err.println("Archivage des fils");
80
            EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
81
            eltEcr.archiveMouvementProfondeur(idMvt, false);
82
        }
83
 
84
        System.err.println("Regeneration des ecritures");
85
        if (idMvt > 1) {
86
            new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID(), idMvt);
87
        } else {
88
            new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID());
89
        }
90
        System.err.println("Fin regeneration");
91
    }
92
 
93
    private boolean checkUniciteNumero(String num, int idFact) {
94
        SQLTable tableFacture = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE");
95
        final SQLSelect selNum = new SQLSelect(tableFacture.getDBSystemRoot(), true);
96
        selNum.addSelect(tableFacture.getKey(), "COUNT");
97
        final Where w = new Where(tableFacture.getField("NUMERO"), "=", num);
98
        selNum.setWhere(w);
99
        selNum.andWhere(new Where(tableFacture.getKey(), "!=", idFact));
100
 
101
        final String req = selNum.asString();
102
        final Number l = (Number) tableFacture.getBase().getDataSource().execute(req, new IResultSetHandler(SQLDataSource.SCALAR_HANDLER, false));
103
        return (l == null || l.intValue() == 0);
104
 
105
    }
106
 
107
    @Override
108
    protected void injectRow(SQLRow row, SQLRowValues rowVals, Date dateNew, SQLRow rowAbonnement) {
109
        // TODO Raccord de méthode auto-généré
110
        super.injectRow(row, rowVals, dateNew, rowAbonnement);
111
        rowVals.put("NUMERO", "ABO--" + NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class));
112
        rowVals.put("ID_ADRESSE", row.getObject("ID_ADRESSE"));
113
        rowVals.put("ID_COMPTE_PCE_SERVICE", row.getObject("ID_COMPTE_PCE_SERVICE"));
114
        rowVals.put("PORT_HT", row.getObject("PORT_HT"));
115
        rowVals.put("REMISE_HT", row.getObject("REMISE_HT"));
116
 
117
        rowVals.put("NOM", row.getObject("NOM"));
118
        rowVals.put("ID_CONTACT", row.getObject("ID_CONTACT"));
119
        rowVals.put("ID_COMPTE_PCE_VENTE", row.getObject("ID_COMPTE_PCE_VENTE"));
120
        rowVals.put("ID_DEVIS", row.getObject("ID_DEVIS"));
121
        rowVals.put("INFOS", row.getObject("INFOS"));
122
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.FALSE);
123
        rowVals.put("PREVISIONNELLE", Boolean.TRUE);
124
 
125
        // Mode de reglement
126
        SQLRow rowMdr = row.getForeignRow("ID_MODE_REGLEMENT");
127
        SQLRowValues rowValsMdr = rowMdr.asRowValues();
128
        rowValsMdr.clearPrimaryKeys();
129
        rowVals.put("ID_MODE_REGLEMENT", rowValsMdr);
130
    }
131
 
132
    @Override
133
    protected List<RowAction> getAdditionnalRowActions() {
134
        return new MouseSheetXmlListeListener(VenteFactureXmlSheet.class).getRowActions();
135
    }
136
 
137
}