OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | 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.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.component.AdresseSQLComponent;
18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
19
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
20
import org.openconcerto.erp.generationDoc.gestcomm.CourrierClientSheet;
21
import org.openconcerto.erp.preferences.PrinterNXProps;
22
import org.openconcerto.sql.Configuration;
23
import org.openconcerto.sql.element.BaseSQLComponent;
24
import org.openconcerto.sql.element.ElementSQLObject;
25
import org.openconcerto.sql.element.SQLComponent;
26
import org.openconcerto.sql.element.SQLElement;
27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowListRSH;
29
import org.openconcerto.sql.model.SQLRowValues;
30
import org.openconcerto.sql.model.SQLSelect;
31
import org.openconcerto.sql.model.SQLTable;
32
import org.openconcerto.sql.model.SQLTableEvent;
33
import org.openconcerto.sql.model.SQLTableModifiedListener;
34
import org.openconcerto.sql.model.Where;
35
import org.openconcerto.sql.sqlobject.ElementComboBox;
36
import org.openconcerto.sql.sqlobject.JUniqueTextField;
37
import org.openconcerto.sql.sqlobject.SQLTextCombo;
38
import org.openconcerto.sql.users.UserManager;
39
import org.openconcerto.sql.view.EditFrame;
40
import org.openconcerto.ui.DefaultGridBagConstraints;
41
import org.openconcerto.ui.JDate;
42
import org.openconcerto.ui.TitledSeparator;
43
import org.openconcerto.utils.ExceptionHandler;
44
 
45
import java.awt.FlowLayout;
46
import java.awt.GridBagConstraints;
47
import java.awt.GridBagLayout;
48
import java.awt.Insets;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.beans.PropertyChangeEvent;
52
import java.beans.PropertyChangeListener;
53
import java.sql.SQLException;
54
import java.util.ArrayList;
55
import java.util.List;
56
 
57
import javax.swing.JButton;
58
import javax.swing.JCheckBox;
59
import javax.swing.JLabel;
60
import javax.swing.JPanel;
61
import javax.swing.JTextArea;
62
import javax.swing.SwingUtilities;
63
 
64
public class CourrierClientSQLElement extends ComptaSQLConfElement {
65
 
66
    public CourrierClientSQLElement() {
67
        super("COURRIER_CLIENT", "un courrier", "courriers");
68
    }
69
 
70
    protected List<String> getListFields() {
71
        final List<String> l = new ArrayList<String>();
72
        l.add("NUMERO");
73
        l.add("NOM");
74
        l.add("DATE");
75
        l.add("INFOS");
76
        return l;
77
    }
78
 
79
    protected List<String> getComboFields() {
80
        final List<String> l = new ArrayList<String>();
81
        l.add("NOM");
82
        return l;
83
    }
84
 
85
    /*
86
     * (non-Javadoc)
87
     *
88
     * @see org.openconcerto.devis.SQLElement#getComponent()
89
     */
90
    public SQLComponent createComponent() {
91
        return new BaseSQLComponent(this) {
92
 
93
            JUniqueTextField textNumero;
94
            JDate date;
95
            SQLTextCombo objet;
83 ilm 96
            ElementComboBox selAffaire;
18 ilm 97
            ElementComboBox comboModele = new ElementComboBox();
98
            ElementComboBox boxAdresse;
99
            JCheckBox checkImpression = new JCheckBox("Impression");
100
            JCheckBox checkVisu = new JCheckBox("Visualisation");
101
            JButton buttonShowDoc;
102
 
103
            public void addViews() {
104
                DefaultGridBagConstraints c = new DefaultGridBagConstraints();
105
 
106
                this.setLayout(new GridBagLayout());
107
 
108
                // Numéro
109
                c.fill = GridBagConstraints.NONE;
110
                this.add(new JLabel(getLabelFor("NUMERO")), c);
111
                this.textNumero = new JUniqueTextField();
112
                c.gridx++;
113
                c.weightx = 1;
114
                c.fill = GridBagConstraints.HORIZONTAL;
115
                this.add(this.textNumero, c);
116
 
117
                // Date
118
                c.weightx = 0;
119
                c.gridx++;
120
                c.fill = GridBagConstraints.NONE;
121
                this.add(new JLabel(getLabelFor("DATE")), c);
122
                this.date = new JDate(true);
123
                c.weightx = 1;
124
                c.gridx++;
125
                c.fill = GridBagConstraints.HORIZONTAL;
126
                this.add(this.date, c);
127
 
128
                // Objet
129
                c.gridy++;
130
                c.gridx = 0;
131
                c.gridwidth = 1;
132
                c.weightx = 0;
133
                c.fill = GridBagConstraints.NONE;
134
                this.add(new JLabel(getLabelFor("NOM")), c);
135
                c.weightx = 1;
136
                c.gridx++;
137
                c.gridwidth = GridBagConstraints.REMAINDER;
138
                c.fill = GridBagConstraints.HORIZONTAL;
139
                this.objet = new SQLTextCombo();
140
                this.add(this.objet, c);
141
 
142
                // Modele
143
                c.gridy++;
144
                c.gridx = 0;
145
                c.gridwidth = 1;
146
                c.weightx = 0;
147
                c.fill = GridBagConstraints.NONE;
148
                this.add(new JLabel(getLabelFor("ID_MODELE_COURRIER_CLIENT")), c);
149
                c.gridx++;
150
                c.weightx = 1;
151
                c.fill = GridBagConstraints.HORIZONTAL;
152
                this.add(this.comboModele, c);
153
 
154
                // Adresse
155
                c.gridx = 0;
156
                c.gridy++;
157
                c.weightx = 1;
158
                c.weighty = 0;
159
                c.gridwidth = GridBagConstraints.REMAINDER;
160
                c.insets = new Insets(15, 2, 3, 2);
161
                TitledSeparator sep = new TitledSeparator("Adresse");
162
                this.add(sep, c);
163
                c.insets = c.getDefaultInsets();
164
 
165
                this.addView("ID_ADRESSE", REQ + ";" + DEC + ";" + SEP);
166
                final ElementSQLObject eltAdr = (ElementSQLObject) this.getView("ID_ADRESSE");
167
 
168
                // Combo
169
                this.boxAdresse = new ElementComboBox();
170
                final SQLElement adresseElt = Configuration.getInstance().getDirectory().getElement("ADRESSE");
171
 
172
                this.boxAdresse.init(adresseElt);
173
                c.gridwidth = 1;
174
                c.gridx = 0;
175
                c.gridy++;
176
                c.weightx = 0;
177
                this.boxAdresse.setAddIconVisible(false);
178
                this.add(new JLabel("Importer l'adresse"), c);
179
                c.gridx++;
180
                c.gridwidth = GridBagConstraints.REMAINDER;
181
                c.weightx = 1;
182
                this.add(this.boxAdresse, c);
183
                this.boxAdresse.addValueListener(new PropertyChangeListener() {
184
                    @Override
185
                    public void propertyChange(PropertyChangeEvent evt) {
186
                        // TODO Auto-generated method stub
187
                        if (boxAdresse.getSelectedId() > 1) {
188
                            SQLRowValues rowVals = adresseElt.getTable().getRow(boxAdresse.getSelectedId()).createUpdateRow();
189
                            rowVals.clearPrimaryKeys();
190
                            eltAdr.setValue(rowVals);
191
                        }
192
                    }
193
                });
194
 
195
                // Adr principale
196
 
197
                c.gridwidth = GridBagConstraints.REMAINDER;
198
                c.gridx = 0;
199
                c.gridy++;
200
                AdresseSQLComponent adrSqlComp = (AdresseSQLComponent) eltAdr.getSQLChild();
201
                adrSqlComp.setDestinataireVisible(true);
202
                this.add(eltAdr, c);
203
 
204
                // Infos
205
                c.gridx = 0;
206
                c.gridy++;
207
                c.weighty = 0;
208
                c.fill = GridBagConstraints.HORIZONTAL;
209
                c.gridwidth = GridBagConstraints.REMAINDER;
210
                c.weightx = 1;
211
                c.insets = new Insets(15, 2, 3, 2);
212
                this.add(new TitledSeparator(getLabelFor("INFOS")), c);
213
                c.gridy++;
214
                c.weightx = 1;
215
                c.weighty = 1;
216
                c.fill = GridBagConstraints.BOTH;
217
                c.gridwidth = GridBagConstraints.REMAINDER;
218
                c.insets = c.getDefaultInsets();
219
                JTextArea textInfos = new JTextArea();
220
                this.add(textInfos, c);
221
 
222
                JPanel panelCheckBox = new JPanel();
223
                panelCheckBox.add(this.checkVisu);
224
                this.checkVisu.setSelected(true);
225
                panelCheckBox.add(this.checkImpression);
226
                this.buttonShowDoc = new JButton("Voir le document");
227
                panelCheckBox.add(this.buttonShowDoc, FlowLayout.RIGHT);
228
                c.gridx = 0;
229
                c.gridy++;
230
                c.fill = GridBagConstraints.NONE;
231
                c.weighty = 0;
232
                c.weightx = 0;
233
                c.anchor = GridBagConstraints.WEST;
234
                this.add(panelCheckBox, c);
235
 
236
                this.buttonShowDoc.addActionListener(new ActionListener() {
237
                    public void actionPerformed(ActionEvent e) {
238
 
239
                        CourrierClientSheet s = new CourrierClientSheet(getTable().getRow(getSelectedID()));
240
                        s.showDocument();
241
                    }
242
                });
243
 
244
                this.addRequiredSQLObject(this.objet, "NOM");
245
                this.addRequiredSQLObject(this.textNumero, "NUMERO");
246
                this.addRequiredSQLObject(this.comboModele, "ID_MODELE_COURRIER_CLIENT");
247
                this.addRequiredSQLObject(this.date, "DATE");
248
            }
249
 
250
            private void setWhereComboAdresse(SQLRow rowAff) {
251
                System.err.println("Set Where Adresse " + rowAff);
252
                if (rowAff != null) {
253
                    SQLElement adresseElt = Configuration.getInstance().getDirectory().getElement("ADRESSE");
254
                    SQLRow rowCli = rowAff.getForeignRow("ID_CLIENT");
255
                    Where w = new Where(adresseElt.getTable().getKey(), "=", rowAff.getInt("ID_ADRESSE_COURRIER_1"));
256
                    w = w.or(new Where(adresseElt.getTable().getKey(), "=", rowAff.getInt("ID_ADRESSE_COURRIER_2")));
257
                    w = w.or(new Where(adresseElt.getTable().getKey(), "=", rowAff.getInt("ID_ADRESSE_COURRIER_3")));
258
                    if (rowCli != null) {
259
                        SQLRow rowAdresse = rowCli.getForeignRow("ID_ADRESSE");
260
 
261
                        w = w.or(new Where(adresseElt.getTable().getKey(), "=", rowAdresse.getID()));
262
                        this.boxAdresse.getRequest().setWhere(w);
263
                        this.boxAdresse.fillCombo();
264
                        return;
265
                    }
266
                }
267
                this.boxAdresse.getRequest().setWhere(null);
268
                this.boxAdresse.fillCombo();
269
            }
270
 
271
            @Override
272
            protected SQLRowValues createDefaults() {
273
                SQLRowValues rowVals = new SQLRowValues(getTable());
274
 
275
                rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(CourrierClientSQLElement.class));
276
 
277
                return rowVals;
278
            }
279
 
280
            @Override
281
            public int insert(SQLRow order) {
282
 
283
                if (this.textNumero.checkValidation()) {
284
                    // incrémentation du numéro auto
285
                    if (NumerotationAutoSQLElement.getNextNumero(CourrierClientSQLElement.class).equalsIgnoreCase(this.textNumero.getText().trim())) {
286
                        SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
287
                        SQLRowValues rowVals = new SQLRowValues(tableNum);
288
                        int val = tableNum.getRow(2).getInt("COURRIER_START");
289
                        val++;
290
                        rowVals.put("COURRIER_START", new Integer(val));
291
 
292
                        try {
293
                            rowVals.update(2);
294
                        } catch (SQLException e) {
295
 
296
                            e.printStackTrace();
297
                        }
298
                    }
299
                    int id = super.insert(order);
300
                    CourrierClientSheet s = new CourrierClientSheet(getTable().getRow(id));
301
                    String printer = PrinterNXProps.getInstance().getStringProperty("CourrierClientPrinter");
302
                    s.generate(this.checkImpression.isSelected(), this.checkVisu.isSelected(), printer);
303
                    return id;
304
 
305
                } else {
306
                    ExceptionHandler.handle("Impossible d'ajouter, numéro de courrier existant.");
307
                    Object root = SwingUtilities.getRoot(this);
308
                    if (root instanceof EditFrame) {
309
                        EditFrame frame = (EditFrame) root;
310
                        frame.getPanel().setAlwaysVisible(true);
311
                    }
312
                    return getSelectedID();
313
                }
314
            }
315
 
316
            @Override
317
            public void update() {
318
 
319
                if (!this.textNumero.checkValidation()) {
320
                    ExceptionHandler.handle("Impossible d'ajouter, numéro de courrier existant.");
321
                    Object root = SwingUtilities.getRoot(this);
322
                    if (root instanceof EditFrame) {
323
                        EditFrame frame = (EditFrame) root;
324
                        frame.getPanel().setAlwaysVisible(true);
325
                    }
326
                }
327
 
328
                super.update();
329
 
330
                if (this.checkVisu.isSelected()) {
331
                    CourrierClientSheet s = new CourrierClientSheet(getTable().getRow(getSelectedID()));
332
                    s.showDocument();
333
                }
334
            }
335
        };
336
    }
57 ilm 337
 
338
    @Override
339
    protected String createCode() {
156 ilm 340
        return this.createCodeOfPackage() + ".mail";
57 ilm 341
    }
18 ilm 342
}