OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Rev 180 | 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.customerrelationship.customer.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18
import org.openconcerto.erp.core.common.ui.DeviseField;
19
import org.openconcerto.erp.generationDoc.gestcomm.RelanceSheet;
20
import org.openconcerto.erp.preferences.PrinterNXProps;
21
import org.openconcerto.sql.element.BaseSQLComponent;
22
import org.openconcerto.sql.element.SQLComponent;
132 ilm 23
import org.openconcerto.sql.element.TreesOfSQLRows;
18 ilm 24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.sqlobject.ElementComboBox;
29
import org.openconcerto.sql.sqlobject.JUniqueTextField;
30
import org.openconcerto.sql.view.EditFrame;
31
import org.openconcerto.ui.DefaultGridBagConstraints;
32
import org.openconcerto.ui.JDate;
33
import org.openconcerto.ui.component.ITextArea;
93 ilm 34
import org.openconcerto.ui.component.InteractionMode;
18 ilm 35
import org.openconcerto.utils.ExceptionHandler;
36
 
132 ilm 37
import java.awt.GridBagConstraints;
38
import java.awt.GridBagLayout;
39
import java.sql.SQLException;
40
import java.util.ArrayList;
41
import java.util.Calendar;
42
import java.util.List;
43
 
44
import javax.swing.JCheckBox;
45
import javax.swing.JLabel;
46
import javax.swing.JPanel;
47
import javax.swing.JSeparator;
48
import javax.swing.SwingUtilities;
49
 
18 ilm 50
public class RelanceSQLElement extends ComptaSQLConfElement {
51
 
52
    public RelanceSQLElement() {
156 ilm 53
        super("RELANCE", "une relance client", "relances clients");
18 ilm 54
    }
55
 
56
    protected List<String> getListFields() {
57
        final List<String> l = new ArrayList<String>();
58
        l.add("NUMERO");
59
        l.add("ID_CLIENT");
60
        l.add("ID_SAISIE_VENTE_FACTURE");
61
        l.add("DATE");
62
        l.add("ID_TYPE_LETTRE_RELANCE");
63
        l.add("MONTANT");
64
        l.add("INFOS");
65
        return l;
66
    }
67
 
68
    protected List<String> getComboFields() {
69
        final List<String> l = new ArrayList<String>();
70
        l.add("NUMERO");
71
        return l;
72
    }
73
 
74
    /*
75
     * (non-Javadoc)
76
     *
77
     * @see org.openconcerto.devis.SQLElement#getComponent()
78
     */
79
    public SQLComponent createComponent() {
80
        return new BaseSQLComponent(this) {
81
 
82
            ElementComboBox comboFacture = new ElementComboBox();
83
            ElementComboBox comboClient = new ElementComboBox();
84
            ElementComboBox comboTypeLettre;
85
            JUniqueTextField textNumero = new JUniqueTextField();
86
            JCheckBox checkImpression = new JCheckBox("Impression");
87
            JCheckBox checkVisu = new JCheckBox("Visualisation");
88
 
89
            public void addViews() {
90
 
91
                this.setLayout(new GridBagLayout());
92
                final GridBagConstraints c = new DefaultGridBagConstraints();
93
 
94
                // Type lettre
95
                JLabel labelTypeLettre = new JLabel(getLabelFor("ID_TYPE_LETTRE_RELANCE"));
96
                labelTypeLettre.setHorizontalAlignment(JLabel.RIGHT);
97
                this.add(labelTypeLettre, c);
98
 
99
                c.gridx++;
100
                // SQLElement typeLettreElt =
101
                // Configuration.getInstance().getDirectory().getElement("TYPE_LETTRE_RELANCE");
102
                this.comboTypeLettre = new ElementComboBox(false);
103
 
104
                this.add(this.comboTypeLettre, c);
105
 
106
                // Date
107
                c.gridx++;
108
                JLabel labelDate = new JLabel(getLabelFor("DATE"));
109
                labelDate.setHorizontalAlignment(JLabel.RIGHT);
110
                this.add(labelDate, c);
111
 
112
                JDate date = new JDate(true);
113
                c.gridx++;
114
                this.add(date, c);
115
 
116
                // Numero
117
                c.gridx = 0;
118
                c.gridy++;
119
                JLabel labelNumero = new JLabel(getLabelFor("NUMERO"));
120
                labelNumero.setHorizontalAlignment(JLabel.RIGHT);
121
                this.add(labelNumero, c);
122
 
123
                c.gridx++;
124
                this.add(this.textNumero, c);
125
 
126
                // Client
127
                c.gridy++;
128
                c.gridx = 0;
129
 
130
                JLabel labelClient = new JLabel(getLabelFor("ID_CLIENT"));
131
                labelClient.setHorizontalAlignment(JLabel.RIGHT);
132
                this.add(labelClient, c);
133
                c.gridx++;
134
                c.gridwidth = GridBagConstraints.REMAINDER;
135
                this.add(this.comboClient, c);
136
 
137
                // Facture
138
                c.gridy++;
139
                c.gridx = 0;
140
                c.gridwidth = 1;
141
                JLabel labelFacture = new JLabel(getLabelFor("ID_SAISIE_VENTE_FACTURE"));
142
                labelFacture.setHorizontalAlignment(JLabel.RIGHT);
143
                this.add(labelFacture, c);
144
                c.gridx++;
145
                // c.gridwidth = GridBagConstraints.REMAINDER;
146
                this.add(this.comboFacture, c);
147
 
148
                // Montant
149
                c.gridx++;
150
                JLabel labelMontant = new JLabel(getLabelFor("MONTANT"));
151
                labelMontant.setHorizontalAlignment(JLabel.RIGHT);
152
                this.add(labelMontant, c);
153
 
154
                c.gridx++;
155
                DeviseField textMontant = new DeviseField();
156
                this.add(textMontant, c);
157
 
158
                // Commentaires
159
                c.gridx = 0;
160
                c.gridy++;
161
                this.add(new JLabel(getLabelFor("INFOS")), c);
162
 
163
                c.gridx++;
164
                c.gridwidth = GridBagConstraints.REMAINDER;
165
                c.fill = GridBagConstraints.HORIZONTAL;
166
                c.weightx = 1;
167
                this.add(new JSeparator(JSeparator.HORIZONTAL), c);
168
 
169
                c.gridx = 0;
170
                c.gridy++;
171
                c.weighty = 1;
172
                c.fill = GridBagConstraints.BOTH;
173
                ITextArea textInfos = new ITextArea();
174
                this.add(textInfos, c);
175
 
176
                JPanel panelCheck = new JPanel();
177
                panelCheck.setBorder(null);
178
                panelCheck.add(this.checkVisu);
179
                panelCheck.add(this.checkImpression);
180
                c.fill = GridBagConstraints.NONE;
181
                c.weighty = 0;
182
                c.gridy++;
183
                this.add(panelCheck, c);
184
 
185
                this.addRequiredSQLObject(textMontant, "MONTANT");
186
                this.addRequiredSQLObject(date, "DATE");
187
                this.addRequiredSQLObject(this.comboTypeLettre, "ID_TYPE_LETTRE_RELANCE");
188
                this.addRequiredSQLObject(this.comboClient, "ID_CLIENT");
189
                this.addRequiredSQLObject(this.comboFacture, "ID_SAISIE_VENTE_FACTURE");
190
                this.addSQLObject(textInfos, "INFOS");
191
                this.addRequiredSQLObject(this.textNumero, "NUMERO");
192
                this.comboTypeLettre.setButtonsVisible(false);
193
 
194
                this.textNumero.setText(NumerotationAutoSQLElement.getNextNumero(RelanceSQLElement.class));
195
                this.checkVisu.setSelected(true);
196
            }
197
 
198
            @Override
199
            public int insert(SQLRow order) {
200
 
201
                if (this.textNumero.checkValidation()) {
202
                    // incrémentation du numéro auto
203
                    if (NumerotationAutoSQLElement.getNextNumero(RelanceSQLElement.class).equalsIgnoreCase(this.textNumero.getText().trim())) {
204
                        SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
205
                        SQLRowValues rowVals = new SQLRowValues(tableNum);
206
                        int val = tableNum.getRow(2).getInt("RELANCE_START");
207
                        val++;
208
                        rowVals.put("RELANCE_START", Integer.valueOf(val));
209
 
210
                        try {
211
                            rowVals.update(2);
212
                        } catch (SQLException e) {
213
 
214
                            e.printStackTrace();
215
                        }
216
                    }
217
 
218
                    // insertion
219
                    int id = super.insert(order);
220
 
221
                    // génération du document
222
                    RelanceSheet s = new RelanceSheet(getTable().getRow(id));
223
                    String printer = PrinterNXProps.getInstance().getStringProperty("RelancePrinter");
224
                    s.generate(this.checkImpression.isSelected(), this.checkVisu.isSelected(), printer, true);
225
 
226
                    return id;
227
                } else {
228
                    ExceptionHandler.handle("Impossible d'ajouter, numéro de relance existant.");
229
                    Object root = SwingUtilities.getRoot(this);
230
                    if (root instanceof EditFrame) {
231
                        EditFrame frame = (EditFrame) root;
232
                        frame.getPanel().setAlwaysVisible(true);
233
                    }
234
                    return getSelectedID();
235
                }
236
            }
237
 
238
            @Override
239
            public void select(SQLRowAccessor r) {
240
                super.select(r);
241
 
242
                // numero de facture et client figé
93 ilm 243
                this.comboFacture.setInteractionMode(InteractionMode.DISABLED);
244
                this.comboClient.setInteractionMode(InteractionMode.DISABLED);
18 ilm 245
                this.fireValidChange();
246
            }
247
 
248
            @Override
249
            public void update() {
250
                if (!this.textNumero.checkValidation()) {
251
                    ExceptionHandler.handle("Impossible d'ajouter, numéro de relance existant.");
252
                    Object root = SwingUtilities.getRoot(this);
253
                    if (root instanceof EditFrame) {
254
                        EditFrame frame = (EditFrame) root;
255
                        frame.getPanel().setAlwaysVisible(true);
256
                    }
257
                    return;
258
                }
259
 
260
                super.update();
261
 
262
                // regénération du document
263
                RelanceSheet s = new RelanceSheet(getTable().getRow(getSelectedID()));
264
                String printer = PrinterNXProps.getInstance().getStringProperty("RelancePrinter");
265
                s.generate(this.checkImpression.isSelected(), this.checkVisu.isSelected(), printer);
266
            }
267
 
268
        };
269
    }
57 ilm 270
 
271
    @Override
132 ilm 272
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
273
 
274
        for (SQLRow row : trees.getRows()) {
275
            if (row.getForeign("ID_ECHEANCE_CLIENT") != null && !row.isForeignEmpty("ID_ECHEANCE_CLIENT")) {
276
                SQLRow rowEch = row.getForeign("ID_ECHEANCE_CLIENT");
277
                int count = rowEch.getInt("NOMBRE_RELANCE");
278
                count = Math.max(0, count - 1);
279
 
280
                SQLRowValues rowValsUp = rowEch.createEmptyUpdateRow();
281
                rowValsUp.put("NOMBRE_RELANCE", count);
282
                rowValsUp.put("DATE_LAST_RELANCE", null);
283
                if (count > 0) {
284
                    List<SQLRow> rows = rowEch.getReferentRows(row.getTable());
285
                    Calendar cal = null;
286
                    for (SQLRow sqlRow : rows) {
287
                        if (sqlRow.getID() != row.getID() && sqlRow.getDate("DATE") != null && (cal == null || cal.before(sqlRow.getDate("DATE")))) {
288
                            cal = sqlRow.getDate("DATE");
289
                        }
290
                    }
291
                    if (cal != null) {
292
                        rowValsUp.put("DATE_LAST_RELANCE", cal.getTime());
293
                    }
294
                }
295
                rowValsUp.commit();
296
            }
297
        }
298
        super.archive(trees, cutLinks);
299
    }
300
 
301
    @Override
57 ilm 302
    protected String createCode() {
156 ilm 303
        return this.createCodeOfPackage() + ".chaseletter";
57 ilm 304
    }
18 ilm 305
}