OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 174 | Go to most recent revision | 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;
149 ilm 20
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 21
import org.openconcerto.sql.Configuration;
22
import org.openconcerto.sql.element.SQLComponent;
23
import org.openconcerto.sql.element.TreesOfSQLRows;
80 ilm 24
import org.openconcerto.sql.model.FieldPath;
18 ilm 25
import org.openconcerto.sql.model.SQLRow;
80 ilm 26
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 27
import org.openconcerto.sql.model.SQLRowListRSH;
28
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLSelect;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.sql.model.Where;
83 ilm 32
import org.openconcerto.sql.model.graph.Path;
33
import org.openconcerto.sql.model.graph.PathBuilder;
149 ilm 34
import org.openconcerto.sql.preferences.SQLPreferences;
80 ilm 35
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
149 ilm 36
import org.openconcerto.sql.view.list.IListe;
37
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
38
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
142 ilm 39
import org.openconcerto.sql.view.list.SQLTableModelSource;
83 ilm 40
import org.openconcerto.utils.CollectionUtils;
149 ilm 41
import org.openconcerto.utils.ExceptionHandler;
18 ilm 42
 
149 ilm 43
import java.awt.event.ActionEvent;
18 ilm 44
import java.sql.SQLException;
45
import java.util.ArrayList;
83 ilm 46
import java.util.Arrays;
47
import java.util.Collection;
18 ilm 48
import java.util.Collections;
132 ilm 49
import java.util.HashSet;
18 ilm 50
import java.util.List;
51
import java.util.Set;
52
 
149 ilm 53
import javax.swing.AbstractAction;
54
 
18 ilm 55
public class EncaisserMontantSQLElement extends ComptaSQLConfElement {
56
 
57
    public EncaisserMontantSQLElement() {
58
        super("ENCAISSER_MONTANT", "un encaissement de montant", "encaissements de montant");
149 ilm 59
 
60
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
61
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
62
            PredicateRowAction actionClient = new PredicateRowAction(new AbstractAction("Annuler l'acompte") {
63
 
64
                public void actionPerformed(ActionEvent e) {
65
                    final SQLRow selRow = IListe.get(e).getSelectedRow().asRow();
66
                    if (selRow.getBoolean("ACOMPTE")) {
67
                        int idDevis = selRow.getForeignID("ID_DEVIS");
68
                        try {
69
                            archive(selRow.getID());
70
                            String up = "UPDATE " + getTable().getTable("DEVIS").getSQLName().quote() + " set \"T_ACOMPTE\"=(SELECT COALESCE(SUM(\"MONTANT\"),0) from "
71
                                    + getTable().getSQLName().quote() + " where \"ID_DEVIS\"=" + idDevis + " AND \"ARCHIVE\"=0) where \"ID_DEVIS\"=" + idDevis;
72
                            getTable().getDBSystemRoot().getDataSource().execute(up);
73
                        } catch (SQLException e1) {
74
                            e1.printStackTrace();
75
                            ExceptionHandler.handle("Erreur lors de l'annulation de l'acompte!", e1);
76
                        }
77
                    }
78
                }
79
            }, false);
80
            actionClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
81
            getRowActions().add(actionClient);
82
        }
83
 
18 ilm 84
    }
85
 
86
    @Override
87
    protected List<String> getListFields() {
88
        final List<String> l = new ArrayList<String>();
83 ilm 89
            l.add("DATE");
90
            l.add("NOM");
91
            l.add("ID_CLIENT");
92
            // l.add("ID_MOUVEMENT");
93
            l.add("ID_MODE_REGLEMENT");
94
            l.add("MONTANT");
149 ilm 95
            SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
96
            if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
97
                l.add("ACOMPTE");
98
            }
18 ilm 99
        return l;
100
    }
101
 
102
    @Override
103
    protected List<String> getComboFields() {
104
        final List<String> l = new ArrayList<String>();
105
        l.add("DATE");
106
        l.add("MONTANT");
107
        return l;
108
    }
109
 
110
    @Override
111
    public Set<String> getReadOnlyFields() {
112
 
113
        return Collections.singleton("ID_CLIENT");
114
    }
115
 
80 ilm 116
 
18 ilm 117
    /*
118
     * (non-Javadoc)
119
     *
120
     * @see org.openconcerto.devis.SQLElement#getComponent()
121
     */
122
    @Override
123
    public SQLComponent createComponent() {
124
        return new EncaisserMontantSQLComponent(this);
125
    }
126
 
127
    @Override
128
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
129
 
130
        // On rétablit les échéances
131
        for (SQLRow row : trees.getRows()) {
132
            for (SQLRow rowEncaisse : row.getReferentRows()) {
133
 
134
                SQLRow rowEch = rowEncaisse.getForeignRow("ID_ECHEANCE_CLIENT");
135
                // SI une echeance est associée (paiement non comptant)
136
                if (rowEch.getID() > 1) {
137
                    SQLRowValues rowVals = rowEch.createEmptyUpdateRow();
138
                    rowVals.put("REGLE", Boolean.FALSE);
139
                    if (rowEch.getBoolean("REGLE")) {
140
                        rowVals.put("MONTANT", rowEncaisse.getLong("MONTANT_REGLE"));
141
                    } else {
142
                        rowVals.put("MONTANT", rowEch.getLong("MONTANT") + rowEncaisse.getLong("MONTANT_REGLE"));
143
                    }
144
                    rowVals.update();
145
                }
146
                Configuration.getInstance().getDirectory().getElement(rowEncaisse.getTable()).archive(rowEncaisse);
147
            }
148
 
149
            // On supprime les mouvements
150
            SQLSelect sel = new SQLSelect(getTable().getBase());
151
 
152
            SQLTable tableMvt = getTable().getTable("MOUVEMENT");
153
            EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement(tableMvt.getTable("ECRITURE"));
154
            sel.addSelectStar(tableMvt);
155
            Where w = new Where(tableMvt.getField("SOURCE"), "=", getTable().getName());
156
            w = w.and(new Where(tableMvt.getField("IDSOURCE"), "=", row.getID()));
157
            sel.setWhere(w);
158
            List<SQLRow> list = (List<SQLRow>) getTable().getBase().getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel, tableMvt));
159
            for (SQLRow sqlRow : list) {
160
                eltEcr.archiveMouvementProfondeur(sqlRow.getID(), true);
161
            }
162
        }
163
 
164
        super.archive(trees, cutLinks);
165
    }
156 ilm 166
 
167
    @Override
168
    protected String createCodeSuffix() {
169
        return ".category";
170
    }
18 ilm 171
}