OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.finance.payment.element;
15
 
80 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18 ilm 17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
19
import org.openconcerto.erp.core.finance.payment.component.EncaisserMontantSQLComponent;
174 ilm 20
import org.openconcerto.erp.core.sales.invoice.component.SaisieVenteFactureSQLComponent;
21
import org.openconcerto.erp.generationEcritures.GenerationReglementVenteNG;
22
import org.openconcerto.erp.model.PrixTTC;
149 ilm 23
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 24
import org.openconcerto.sql.Configuration;
25
import org.openconcerto.sql.element.SQLComponent;
174 ilm 26
import org.openconcerto.sql.element.SQLElement;
18 ilm 27
import org.openconcerto.sql.element.TreesOfSQLRows;
80 ilm 28
import org.openconcerto.sql.model.FieldPath;
18 ilm 29
import org.openconcerto.sql.model.SQLRow;
80 ilm 30
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 31
import org.openconcerto.sql.model.SQLRowListRSH;
32
import org.openconcerto.sql.model.SQLRowValues;
33
import org.openconcerto.sql.model.SQLSelect;
34
import org.openconcerto.sql.model.SQLTable;
35
import org.openconcerto.sql.model.Where;
83 ilm 36
import org.openconcerto.sql.model.graph.Path;
37
import org.openconcerto.sql.model.graph.PathBuilder;
149 ilm 38
import org.openconcerto.sql.preferences.SQLPreferences;
174 ilm 39
import org.openconcerto.sql.view.EditFrame;
80 ilm 40
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
149 ilm 41
import org.openconcerto.sql.view.list.IListe;
42
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
43
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
142 ilm 44
import org.openconcerto.sql.view.list.SQLTableModelSource;
83 ilm 45
import org.openconcerto.utils.CollectionUtils;
149 ilm 46
import org.openconcerto.utils.ExceptionHandler;
174 ilm 47
import org.openconcerto.utils.StringUtils;
18 ilm 48
 
149 ilm 49
import java.awt.event.ActionEvent;
18 ilm 50
import java.sql.SQLException;
51
import java.util.ArrayList;
83 ilm 52
import java.util.Arrays;
53
import java.util.Collection;
18 ilm 54
import java.util.Collections;
132 ilm 55
import java.util.HashSet;
18 ilm 56
import java.util.List;
57
import java.util.Set;
58
 
149 ilm 59
import javax.swing.AbstractAction;
174 ilm 60
import javax.swing.JOptionPane;
61
import javax.swing.SwingUtilities;
149 ilm 62
 
18 ilm 63
public class EncaisserMontantSQLElement extends ComptaSQLConfElement {
64
 
65
    public EncaisserMontantSQLElement() {
66
        super("ENCAISSER_MONTANT", "un encaissement de montant", "encaissements de montant");
149 ilm 67
 
68
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
69
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
70
            PredicateRowAction actionClient = new PredicateRowAction(new AbstractAction("Annuler l'acompte") {
71
 
72
                public void actionPerformed(ActionEvent e) {
73
                    final SQLRow selRow = IListe.get(e).getSelectedRow().asRow();
74
                    if (selRow.getBoolean("ACOMPTE")) {
75
                        int idDevis = selRow.getForeignID("ID_DEVIS");
76
                        try {
77
                            archive(selRow.getID());
78
                            String up = "UPDATE " + getTable().getTable("DEVIS").getSQLName().quote() + " set \"T_ACOMPTE\"=(SELECT COALESCE(SUM(\"MONTANT\"),0) from "
79
                                    + getTable().getSQLName().quote() + " where \"ID_DEVIS\"=" + idDevis + " AND \"ARCHIVE\"=0) where \"ID_DEVIS\"=" + idDevis;
80
                            getTable().getDBSystemRoot().getDataSource().execute(up);
81
                        } catch (SQLException e1) {
82
                            e1.printStackTrace();
83
                            ExceptionHandler.handle("Erreur lors de l'annulation de l'acompte!", e1);
84
                        }
85
                    }
86
                }
87
            }, false);
88
            actionClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
89
            getRowActions().add(actionClient);
90
        }
91
 
18 ilm 92
    }
93
 
94
    @Override
95
    protected List<String> getListFields() {
96
        final List<String> l = new ArrayList<String>();
83 ilm 97
            l.add("DATE");
98
            l.add("NOM");
99
            l.add("ID_CLIENT");
100
            // l.add("ID_MOUVEMENT");
101
            l.add("ID_MODE_REGLEMENT");
102
            l.add("MONTANT");
149 ilm 103
            SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
104
            if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
105
                l.add("ACOMPTE");
106
            }
18 ilm 107
        return l;
108
    }
109
 
110
    @Override
111
    protected List<String> getComboFields() {
112
        final List<String> l = new ArrayList<String>();
113
        l.add("DATE");
114
        l.add("MONTANT");
115
        return l;
116
    }
117
 
118
    @Override
119
    public Set<String> getReadOnlyFields() {
120
 
121
        return Collections.singleton("ID_CLIENT");
122
    }
123
 
80 ilm 124
 
18 ilm 125
    /*
126
     * (non-Javadoc)
127
     *
128
     * @see org.openconcerto.devis.SQLElement#getComponent()
129
     */
130
    @Override
131
    public SQLComponent createComponent() {
132
        return new EncaisserMontantSQLComponent(this);
133
    }
134
 
135
    @Override
136
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
137
 
138
        // On rétablit les échéances
139
        for (SQLRow row : trees.getRows()) {
140
            for (SQLRow rowEncaisse : row.getReferentRows()) {
141
 
142
                SQLRow rowEch = rowEncaisse.getForeignRow("ID_ECHEANCE_CLIENT");
143
                // SI une echeance est associée (paiement non comptant)
144
                if (rowEch.getID() > 1) {
145
                    SQLRowValues rowVals = rowEch.createEmptyUpdateRow();
146
                    rowVals.put("REGLE", Boolean.FALSE);
147
                    if (rowEch.getBoolean("REGLE")) {
148
                        rowVals.put("MONTANT", rowEncaisse.getLong("MONTANT_REGLE"));
149
                    } else {
150
                        rowVals.put("MONTANT", rowEch.getLong("MONTANT") + rowEncaisse.getLong("MONTANT_REGLE"));
151
                    }
152
                    rowVals.update();
153
                }
154
                Configuration.getInstance().getDirectory().getElement(rowEncaisse.getTable()).archive(rowEncaisse);
155
            }
156
 
157
            // On supprime les mouvements
158
            SQLSelect sel = new SQLSelect(getTable().getBase());
159
 
160
            SQLTable tableMvt = getTable().getTable("MOUVEMENT");
161
            EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement(tableMvt.getTable("ECRITURE"));
162
            sel.addSelectStar(tableMvt);
163
            Where w = new Where(tableMvt.getField("SOURCE"), "=", getTable().getName());
164
            w = w.and(new Where(tableMvt.getField("IDSOURCE"), "=", row.getID()));
165
            sel.setWhere(w);
166
            List<SQLRow> list = (List<SQLRow>) getTable().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel, tableMvt));
167
            for (SQLRow sqlRow : list) {
168
                eltEcr.archiveMouvementProfondeur(sqlRow.getID(), true);
169
            }
170
        }
171
 
172
        super.archive(trees, cutLinks);
173
    }
156 ilm 174
 
174 ilm 175
    public void regleFacture(SQLRow row) throws Exception {
176
 
177
        System.out.println("Génération des ecritures du reglement");
178
        String s = row.getString("NOM");
179
        SQLRow rowModeRegl = row.getForeignRow("ID_MODE_REGLEMENT");
180
        SQLRow rowTypeRegl = rowModeRegl.getForeignRow("ID_TYPE_REGLEMENT");
181
 
182
        // Compte Client
183
        SQLRow clientRow = row.getForeignRow("ID_CLIENT");
184
 
185
        String label = "Règlement vente " + ((s == null) ? "" : s) + " (" + rowTypeRegl.getString("NOM") + ") " + StringUtils.limitLength(clientRow.getString("NOM"), 20);
186
        long montant = row.getLong("MONTANT");
187
        PrixTTC ttc = new PrixTTC(montant);
188
 
189
        List<SQLRow> l = row.getReferentRows(row.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
190
        if (l.isEmpty()) {
191
            SwingUtilities.invokeLater(new Runnable() {
192
 
193
                @Override
194
                public void run() {
195
                    JOptionPane.showMessageDialog(null, "Un problème a été rencontré lors de l'encaissement! \n Les écritures comptables non pu être générer!");
196
                }
197
            });
198
            System.err.println("Liste des échéances vides pour l'encaissement ID " + row.getID());
199
            Thread.dumpStack();
200
            return;
201
        }
202
        new GenerationReglementVenteNG(label, clientRow, ttc, row.getDate("DATE").getTime(), rowModeRegl, row, l.get(0).getForeignRow("ID_MOUVEMENT_ECHEANCE"), false, false, row.getString("TIERS"),
203
                row.getForeign("ID_COMPTE_PCE_TIERS"));
204
 
205
        // Mise a jour du montant de l'echeance
206
        boolean supplement = false;
207
 
208
        if (!row.getBoolean("ACOMPTE")) {
209
            // On marque les echeances comme reglees
210
            for (SQLRow sqlRow : l) {
211
 
212
                final SQLRow rowEch = sqlRow.getForeignRow("ID_ECHEANCE_CLIENT");
213
                SQLRowValues rowValsEch = rowEch.createEmptyUpdateRow();
214
                if (sqlRow.getLong("MONTANT_REGLE") >= sqlRow.getLong("MONTANT_A_REGLER")) {
215
                    rowValsEch.put("REGLE", Boolean.TRUE);
216
                    if (sqlRow.getLong("MONTANT_REGLE") > sqlRow.getLong("MONTANT_A_REGLER")) {
217
                        supplement = true;
218
                    }
219
                }
220
                rowValsEch.put("MONTANT", Long.valueOf(rowEch.getLong("MONTANT") - sqlRow.getLong("MONTANT_REGLE")));
221
 
222
                rowValsEch.update();
223
                // this.comboEcheance.rowDeleted(tableEch, rowEch.getID());
224
                // getTable().fireTableModified(rowEch.getID());
225
            }
226
        }
227
        // si le montant réglé est supérieur, on crée une facture de complément
228
        if (supplement) {
229
            SQLElement elt = getDirectory().getElement("SAISIE_VENTE_FACTURE");
230
            SwingUtilities.invokeLater(new Runnable() {
231
 
232
                @Override
233
                public void run() {
234
 
235
                    EditFrame f = new EditFrame(elt, EditFrame.CREATION);
236
                    SaisieVenteFactureSQLComponent comp = (SaisieVenteFactureSQLComponent) f.getSQLComponent();
237
                    comp.setComplement(true);
238
                    f.setVisible(true);
239
                }
240
            });
241
        }
242
 
243
    }
244
 
156 ilm 245
    @Override
246
    protected String createCodeSuffix() {
247
        return ".category";
248
    }
18 ilm 249
}