OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 141 | Rev 169 | 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
 
152 ilm 78
            if (idMvt > 1) {
79
                // on supprime tout ce qui est lié à la facture
80
                System.err.println("Archivage des fils");
81
                EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
82
                eltEcr.archiveMouvementProfondeur(idMvt, false);
83
            }
76 ilm 84
        }
85
 
86
        System.err.println("Regeneration des ecritures");
87
        if (idMvt > 1) {
88
            new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID(), idMvt);
89
        } else {
90
            new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID());
91
        }
92
        System.err.println("Fin regeneration");
93
    }
94
 
95
    private boolean checkUniciteNumero(String num, int idFact) {
96
        SQLTable tableFacture = Configuration.getInstance().getRoot().findTable("SAISIE_VENTE_FACTURE");
97
        final SQLSelect selNum = new SQLSelect(tableFacture.getDBSystemRoot(), true);
98
        selNum.addSelect(tableFacture.getKey(), "COUNT");
99
        final Where w = new Where(tableFacture.getField("NUMERO"), "=", num);
100
        selNum.setWhere(w);
101
        selNum.andWhere(new Where(tableFacture.getKey(), "!=", idFact));
102
 
103
        final String req = selNum.asString();
104
        final Number l = (Number) tableFacture.getBase().getDataSource().execute(req, new IResultSetHandler(SQLDataSource.SCALAR_HANDLER, false));
105
        return (l == null || l.intValue() == 0);
106
 
107
    }
108
 
109
    @Override
110
    protected void injectRow(SQLRow row, SQLRowValues rowVals, Date dateNew, SQLRow rowAbonnement) {
111
        // TODO Raccord de méthode auto-généré
112
        super.injectRow(row, rowVals, dateNew, rowAbonnement);
113
        rowVals.put("NUMERO", "ABO--" + NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class));
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"));
117
        rowVals.put("REMISE_HT", row.getObject("REMISE_HT"));
118
 
119
        rowVals.put("NOM", row.getObject("NOM"));
120
        rowVals.put("ID_CONTACT", row.getObject("ID_CONTACT"));
121
        rowVals.put("ID_COMPTE_PCE_VENTE", row.getObject("ID_COMPTE_PCE_VENTE"));
122
        rowVals.put("ID_DEVIS", row.getObject("ID_DEVIS"));
123
        rowVals.put("INFOS", row.getObject("INFOS"));
124
        rowVals.put("CREATION_AUTO_VALIDER", Boolean.FALSE);
125
        rowVals.put("PREVISIONNELLE", Boolean.TRUE);
126
 
127
        // Mode de reglement
128
        SQLRow rowMdr = row.getForeignRow("ID_MODE_REGLEMENT");
129
        SQLRowValues rowValsMdr = rowMdr.asRowValues();
130
        rowValsMdr.clearPrimaryKeys();
131
        rowVals.put("ID_MODE_REGLEMENT", rowValsMdr);
132
    }
133
 
134
    @Override
135
    protected List<RowAction> getAdditionnalRowActions() {
136
        return new MouseSheetXmlListeListener(VenteFactureXmlSheet.class).getRowActions();
137
    }
138
 
139
}