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
93 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.component.AdresseSQLComponent;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseClientItemTable;
144 ilm 19
import org.openconcerto.erp.core.sales.product.element.ClientCodeArticleTable;
174 ilm 20
import org.openconcerto.erp.core.sales.product.ui.CustomerProductQtyPriceListTable;
132 ilm 21
import org.openconcerto.erp.preferences.ModeReglementDefautPrefPanel;
94 ilm 22
import org.openconcerto.erp.utils.TM;
93 ilm 23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.element.ElementSQLObject;
25
import org.openconcerto.sql.element.GroupSQLComponent;
26
import org.openconcerto.sql.element.SQLElement;
27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowAccessor;
29
import org.openconcerto.sql.model.SQLRowValues;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.sql.model.UndefinedRowValuesCache;
32
import org.openconcerto.sql.sqlobject.JUniqueTextField;
33
import org.openconcerto.sql.sqlobject.SQLSearchableTextCombo;
34
import org.openconcerto.sql.ui.textmenu.TextFieldWithMenu;
35
import org.openconcerto.sql.ui.textmenu.TextFieldWithMenuItemsTableFetcher;
36
import org.openconcerto.sql.ui.textmenu.TextFieldWithWebBrowsing;
37
import org.openconcerto.sql.view.EditFrame;
38
import org.openconcerto.sql.view.EditPanel.EditMode;
39
import org.openconcerto.sql.view.IListFrame;
40
import org.openconcerto.sql.view.ListeAddPanel;
41
import org.openconcerto.ui.DefaultGridBagConstraints;
42
import org.openconcerto.ui.FrameUtil;
43
import org.openconcerto.ui.JDate;
44
import org.openconcerto.ui.JLabelBold;
45
import org.openconcerto.ui.component.ComboLockedMode;
46
import org.openconcerto.ui.component.ITextArea;
47
import org.openconcerto.ui.component.InteractionMode;
142 ilm 48
import org.openconcerto.utils.ExceptionHandler;
93 ilm 49
 
94 ilm 50
import java.awt.Component;
51
import java.awt.Dimension;
52
import java.awt.GridBagConstraints;
53
import java.awt.GridBagLayout;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56
import java.sql.SQLException;
57
import java.util.HashSet;
58
import java.util.Set;
59
 
60
import javax.swing.AbstractAction;
61
import javax.swing.Icon;
62
import javax.swing.JCheckBox;
63
import javax.swing.JComponent;
64
import javax.swing.JLabel;
142 ilm 65
import javax.swing.JOptionPane;
94 ilm 66
import javax.swing.JPanel;
67
import javax.swing.JTabbedPane;
68
import javax.swing.JTextField;
142 ilm 69
import javax.swing.SwingUtilities;
94 ilm 70
import javax.swing.event.DocumentEvent;
71
import javax.swing.event.DocumentListener;
72
 
73
import com.lowagie.text.Font;
74
 
93 ilm 75
public class CustomerSQLComponent extends GroupSQLComponent {
76
    private ContactItemTable table;
144 ilm 77
    private ClientCodeArticleTable tableCustomProduct;
93 ilm 78
    private AdresseClientItemTable adresseTable = new AdresseClientItemTable();
174 ilm 79
 
93 ilm 80
    private SQLTable contactTable = Configuration.getInstance().getDirectory().getElement("CONTACT").getTable();
81
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
142 ilm 82
    private final JUniqueTextField code = new JUniqueTextField(20) {
83
        @Override
84
        public String getAutoRefreshNumber() {
85
            if (getMode() == Mode.INSERTION) {
86
                return NumerotationAutoSQLElement.getNextNumero(getElement().getClass());
87
            } else {
88
                return null;
89
            }
90
        }
91
    };
93 ilm 92
 
174 ilm 93
    private CustomerProductQtyPriceListTable clienTarifTable = new CustomerProductQtyPriceListTable();
93 ilm 94
    private SQLRowValues defaultContactRowVals = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(this.contactTable));
95
    private JCheckBox checkAdrLivraison, checkAdrFacturation;
96
 
97
    public CustomerSQLComponent(SQLElement element) {
98
        super(element);
99
        this.table = new ContactItemTable(this.defaultContactRowVals);
144 ilm 100
        this.tableCustomProduct = new ClientCodeArticleTable();
93 ilm 101
        this.table.setPreferredSize(new Dimension(this.table.getSize().width, 150));
102
    }
103
 
104
    @Override
105
    protected Set<String> createRequiredNames() {
106
        final Set<String> s = new HashSet<String>();
107
        s.add("NOM");
108
        s.add("ID_ADRESSE");
109
        s.add("ID_MODE_REGLEMENT");
110
        return s;
111
    }
112
 
113
    @Override
114
    protected SQLRowValues createDefaults() {
115
 
116
        SQLRowValues rowVals = new SQLRowValues(getTable());
117
        rowVals.put("CODE", NumerotationAutoSQLElement.getNextNumero(getElement().getClass()));
132 ilm 118
        // Mode de règlement par defaut
119
        try {
120
            SQLRow r = ModeReglementDefautPrefPanel.getDefaultRow(true);
121
            SQLElement eltModeReglement = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
122
            if (r.getID() > 1) {
123
                SQLRowValues rowValsMR = eltModeReglement.createCopy(r, null);
124
                rowVals.put("ID_MODE_REGLEMENT", rowValsMR);
125
            }
126
        } catch (SQLException e) {
127
            System.err.println("Impossible de sélectionner le mode de règlement par défaut du client.");
128
            e.printStackTrace();
129
        }
93 ilm 130
        return rowVals;
131
    }
132
 
133
    @Override
134
    public JComponent createEditor(String id) {
135
        if (id.equals("CATEGORIES")) {
136
            TextFieldWithMenuItemsTableFetcher itemsFetcher = new TextFieldWithMenuItemsTableFetcher(getTable().getTable("CATEGORIE_CLIENT").getField("NOM"));
137
            TextFieldWithMenu t = new TextFieldWithMenu(itemsFetcher, false);
138
            t.addAction(new AbstractAction(TM.tr("add")) {
139
 
140
                @Override
141
                public void actionPerformed(ActionEvent e) {
142
                    EditFrame frame = new EditFrame(getElement().getDirectory().getElement("CATEGORIE_CLIENT"), EditMode.CREATION);
143
                    FrameUtil.showPacked(frame);
144
 
145
                }
146
            });
151 ilm 147
            t.addAction(new AbstractAction(TM.tr("modify.or.delete")) {
93 ilm 148
 
149
                @Override
150
                public void actionPerformed(ActionEvent e) {
151
                    final SQLElement element = getElement().getDirectory().getElement("CATEGORIE_CLIENT");
152
 
153
                    ListeAddPanel p = new ListeAddPanel(element);
154
                    // EditFrame frame = new EditFrame(element, EditMode.CREATION);
155
                    IListFrame frame = new IListFrame(p);
156
                    FrameUtil.showPacked(frame);
157
 
158
                }
159
            });
160
            return t;
94 ilm 161
        } else if (id.equals("GROUPE") || id.equals("FORME_JURIDIQUE") || id.equals("CENTRE_GESTION") || id.equals("METHODE_RELANCE")) {
93 ilm 162
            return new SQLSearchableTextCombo(ComboLockedMode.UNLOCKED, 1, 20, false);
163
        } else if (id.equals("SITE_INTERNET")) {
164
            return new TextFieldWithWebBrowsing();
156 ilm 165
        } else if (id.equals("SIRET")) {
166
            return new JTextField(20);
93 ilm 167
        } else if (id.equals("DATE")) {
168
            return new JDate(true);
169
        } else if (id.equals("customerrelationship.customer.contacts")) {
170
            return this.table;
144 ilm 171
        } else if (id.equals("customerrelationship.customer.customproduct")) {
172
            return this.tableCustomProduct;
174 ilm 173
        } else if (id.equals("customerrelationship.customer.customtarif")) {
174
            return this.clienTarifTable;
93 ilm 175
        } else if (id.equals("customerrelationship.customer.addresses")) {
176
            return createAdressesComponent();
177
        } else if (id.equals("NOM")) {
132 ilm 178
            return super.createEditor(id);
93 ilm 179
        }
180
        if (id.equals("CODE")) {
181
 
182
            return this.code;
183
        } else if (id.equals("INFOS")) {
184
            return new ITextArea(4, 40);
185
        } else if (id.equals("COMMENTAIRES")) {
186
            return new ITextArea(10, 40);
187
        } else if (id.equals("TEL")) {
188
            final JTextField textTel = new JTextField(25);
189
            textTel.getDocument().addDocumentListener(new DocumentListener() {
190
 
191
                public void changedUpdate(DocumentEvent e) {
192
                    defaultContactRowVals.put("TEL_DIRECT", textTel.getText());
193
                }
194
 
195
                public void insertUpdate(DocumentEvent e) {
196
                    defaultContactRowVals.put("TEL_DIRECT", textTel.getText());
197
                }
198
 
199
                public void removeUpdate(DocumentEvent e) {
200
                    defaultContactRowVals.put("TEL_DIRECT", textTel.getText());
201
                }
202
 
203
            });
204
            return textTel;
205
        } else if (id.equals("FAX")) {
206
            final JTextField textFax = new JTextField(25);
207
            textFax.getDocument().addDocumentListener(new DocumentListener() {
208
 
209
                public void changedUpdate(DocumentEvent e) {
210
                    defaultContactRowVals.put("FAX", textFax.getText());
211
                }
212
 
213
                public void insertUpdate(DocumentEvent e) {
214
                    defaultContactRowVals.put("FAX", textFax.getText());
215
                }
216
 
217
                public void removeUpdate(DocumentEvent e) {
218
                    defaultContactRowVals.put("FAX", textFax.getText());
219
                }
220
 
221
            });
222
            return textFax;
223
        }
94 ilm 224
        JComponent c = super.createEditor(id);
225
        return c;
93 ilm 226
    }
227
 
228
    @Override
229
    public JComponent getLabel(String id) {
230
        if (id.equals("ID_MODE_REGLEMENT") || id.equals("INFOS") || id.startsWith("ID_ADRESSE")) {
231
            JLabel l = (JLabel) super.getLabel(id);
232
            l.setFont(l.getFont().deriveFont(Font.BOLD));
233
            return l;
234
        }
235
        if (id.equals("customerrelationship.customer.contact")) {
236
            return new JLabelBold("Contacts");
237
        } else if (id.equals("customerrelationship.customer.payment")) {
238
            return new JLabelBold("Mode de règlement");
239
        } else if (id.equals("customerrelationship.customer.address")) {
240
            return new JLabelBold("Adresses du client");
241
        }
94 ilm 242
        JComponent c = super.getLabel(id);
243
        return c;
93 ilm 244
    }
245
 
246
    @Override
247
    public void update() {
248
        super.update();
249
        final int selectedID = getSelectedID();
250
        this.table.updateField("ID_CLIENT", selectedID);
174 ilm 251
        this.clienTarifTable.updateField("ID_CLIENT", selectedID);
144 ilm 252
        this.tableCustomProduct.updateField("ID_CLIENT", selectedID);
93 ilm 253
        this.adresseTable.updateField("ID_CLIENT", selectedID);
254
    }
255
 
256
    @Override
257
    public void select(SQLRowAccessor r) {
258
        super.select(r);
259
        this.checkAdrLivraison.setSelected(r == null || !r.getFields().contains("ID_ADRESSE_L") || r.isForeignEmpty("ID_ADRESSE_L"));
260
        this.checkAdrFacturation.setSelected(r == null || !r.getFields().contains("ID_ADRESSE_F") || r.isForeignEmpty("ID_ADRESSE_F"));
261
        if (r != null) {
262
            this.table.insertFrom("ID_CLIENT", r.asRowValues());
174 ilm 263
            this.clienTarifTable.insertFrom("ID_CLIENT", r.asRowValues());
144 ilm 264
            this.tableCustomProduct.insertFrom("ID_CLIENT", r.asRowValues());
93 ilm 265
            this.adresseTable.insertFrom("ID_CLIENT", r.asRowValues());
266
        }
267
    }
268
 
269
    @Override
270
    public int insert(SQLRow order) {
142 ilm 271
        int id;
93 ilm 272
 
142 ilm 273
        int attempt = 0;
274
        // on verifie qu'un client du meme numero n'a pas été inséré entre temps
156 ilm 275
        if (this.code.getText().trim().length() > 0 && !this.code.checkValidation(false)) {
142 ilm 276
            while (attempt < JUniqueTextField.RETRY_COUNT) {
277
                String num = NumerotationAutoSQLElement.getNextNumero(getElement().getClass());
278
                this.code.setText(num);
279
                attempt++;
280
                if (this.code.checkValidation(false)) {
281
                    System.err.println("ATEMPT " + attempt + " SUCCESS WITH NUMERO " + num);
282
                    break;
283
                }
93 ilm 284
                try {
142 ilm 285
                    Thread.sleep(JUniqueTextField.SLEEP_WAIT_MS);
286
                } catch (InterruptedException e) {
93 ilm 287
                    e.printStackTrace();
288
                }
289
            }
290
        }
142 ilm 291
        final String num = this.code.getText();
292
        if (attempt == JUniqueTextField.RETRY_COUNT) {
293
            id = getSelectedID();
294
            ExceptionHandler.handle("Impossible d'ajouter, numéro de client existant.");
295
            final Object root = SwingUtilities.getRoot(this);
296
            if (root instanceof EditFrame) {
297
                final EditFrame frame = (EditFrame) root;
298
                frame.getPanel().setAlwaysVisible(true);
299
            }
300
        } else {
301
            id = super.insert(order);
302
            this.table.updateField("ID_CLIENT", id);
144 ilm 303
            this.tableCustomProduct.updateField("ID_CLIENT", id);
174 ilm 304
            this.clienTarifTable.updateField("ID_CLIENT", id);
142 ilm 305
            this.adresseTable.updateField("ID_CLIENT", id);
306
            if (NumerotationAutoSQLElement.getNextNumero(getElement().getClass()).equalsIgnoreCase(this.code.getText().trim())) {
307
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
308
                final SQLRow rowNumAuto = this.tableNum.getRow(2);
309
                if (rowNumAuto.getObject("CLIENT_START") != null) {
310
                    int val = rowNumAuto.getInt("CLIENT_START");
311
                    val++;
312
                    rowVals.put("CLIENT_START", new Integer(val));
93 ilm 313
 
142 ilm 314
                    try {
315
                        rowVals.update(2);
316
                    } catch (SQLException e) {
317
                        e.printStackTrace();
318
                    }
319
                }
320
            }
321
            if (attempt > 0) {
322
                SwingUtilities.invokeLater(new Runnable() {
323
                    public void run() {
324
                        JOptionPane.showMessageDialog(null, "Le numéro a été actualisé en " + num);
325
                    }
326
                });
327
            }
328
        }
93 ilm 329
        return id;
330
    }
331
 
332
    private JComponent createAdressesComponent() {
333
        final JTabbedPane tabbedAdresse = new JTabbedPane() {
334
            public void insertTab(String title, Icon icon, Component component, String tip, int index) {
335
                if (component instanceof JComponent) {
336
                    ((JComponent) component).setOpaque(false);
337
                }
338
                super.insertTab(title, icon, component, tip, index);
339
            }
340
 
341
        };
342
 
343
        // Adr principale
344
 
345
        final JPanel panelAdressePrincipale = new JPanel(new GridBagLayout());
346
        GridBagConstraints cPanel = new DefaultGridBagConstraints();
347
        cPanel.weightx = 1;
348
        cPanel.weighty = 1;
349
        cPanel.anchor = GridBagConstraints.NORTH;
350
        this.addView("ID_ADRESSE", REQ + ";" + DEC + ";" + SEP);
351
 
352
        final ElementSQLObject componentPrincipale = (ElementSQLObject) this.getView("ID_ADRESSE");
353
        ((AdresseSQLComponent) componentPrincipale.getSQLChild()).setDestinataireVisible(true);
354
        componentPrincipale.setOpaque(false);
355
        panelAdressePrincipale.add(componentPrincipale, cPanel);
356
        tabbedAdresse.add(getLabelFor("ID_ADRESSE"), panelAdressePrincipale);
357
        tabbedAdresse.setOpaque(false);
358
 
359
        // Adresse de facturation
360
 
361
        tabbedAdresse.add(getLabelFor("ID_ADRESSE_F"), createFacturationPanel());
362
 
363
        // Adresse de livraison
364
 
365
        tabbedAdresse.add(getLabelFor("ID_ADRESSE_L"), createLivraisonPanel());
366
 
367
        // Adresses supplémentaires
368
        String labelAdrSuppl = TM.tr("additional.address");
369
        tabbedAdresse.add(labelAdrSuppl, this.adresseTable);
370
 
371
        return tabbedAdresse;
372
    }
373
 
374
    private Component createLivraisonPanel() {
375
        final JPanel panel = new JPanel(new GridBagLayout());
376
        final GridBagConstraints c = new DefaultGridBagConstraints();
377
        final String field = "ID_ADRESSE_L";
378
 
379
        this.addView(field, DEC + ";" + SEP);
380
        final ElementSQLObject component = (ElementSQLObject) this.getView(field);
381
        final AdresseSQLComponent adresseSQLComponent = (AdresseSQLComponent) component.getSQLChild();
382
        adresseSQLComponent.setDestinataireVisible(true);
383
        System.err.println("CustomerSQLComponent.createLivraisonPanel()" + component + " " + System.identityHashCode(component));
384
        System.err.println("CustomerSQLComponent.createLivraisonPanel()" + adresseSQLComponent + " " + System.identityHashCode(adresseSQLComponent));
385
        component.setOpaque(false);
386
        c.weightx = 1;
387
        this.checkAdrLivraison = new JCheckBox(TM.tr("delivery.address.same.main.address"));
388
        this.checkAdrLivraison.setOpaque(false);
389
        panel.add(component, c);
390
 
391
        c.gridy++;
392
        c.weighty = 1;
393
        c.anchor = GridBagConstraints.NORTH;
394
        panel.add(this.checkAdrLivraison, c);
395
        // Listener
396
        this.checkAdrLivraison.addActionListener(new ActionListener() {
397
 
398
            public void actionPerformed(java.awt.event.ActionEvent e) {
399
                boolean b = checkAdrLivraison.isSelected();
400
                System.err.println("CustomerSQLComponent.createAdressesComponent().new ActionListener() {...}.actionPerformed() checkAdrLivraison " + b);
401
                component.setEditable((!b) ? InteractionMode.READ_WRITE : InteractionMode.DISABLED);
402
                component.setCreated(!b);
403
            };
404
        });
405
        panel.setOpaque(false);
406
        return panel;
407
    }
408
 
409
    private Component createFacturationPanel() {
410
        final JPanel panel = new JPanel(new GridBagLayout());
411
        final GridBagConstraints c = new DefaultGridBagConstraints();
412
        final String field = "ID_ADRESSE_F";
413
 
414
        this.addView(field, DEC + ";" + SEP);
415
        final ElementSQLObject component = (ElementSQLObject) this.getView(field);
416
        final AdresseSQLComponent adresseSQLComponent = (AdresseSQLComponent) component.getSQLChild();
417
        System.err.println("CustomerSQLComponent.createFacturationPanel()" + component + " " + System.identityHashCode(component));
418
        System.err.println("CustomerSQLComponent.createFacturationPanel()" + adresseSQLComponent + " " + System.identityHashCode(adresseSQLComponent));
419
        adresseSQLComponent.setDestinataireVisible(true);
420
        component.setOpaque(false);
421
        c.weightx = 1;
422
        this.checkAdrFacturation = new JCheckBox(TM.tr("invoice.address.same.main.address"));
423
        this.checkAdrFacturation.setOpaque(false);
424
        c.anchor = GridBagConstraints.NORTH;
425
        panel.add(component, c);
426
 
427
        c.gridy++;
428
        c.weighty = 1;
429
        c.anchor = GridBagConstraints.NORTH;
430
        panel.add(this.checkAdrFacturation, c);
431
        // Listener
432
        this.checkAdrFacturation.addActionListener(new ActionListener() {
433
 
434
            public void actionPerformed(java.awt.event.ActionEvent e) {
435
                boolean b = checkAdrFacturation.isSelected();
436
                System.err.println("CustomerSQLComponent.createAdressesComponent().new ActionListener() {...}.actionPerformed() checkAdrFacturation " + b);
437
                component.setEditable((!b) ? InteractionMode.READ_WRITE : InteractionMode.DISABLED);
438
                component.setCreated(!b);
439
            };
440
        });
441
        panel.setOpaque(false);
442
        return panel;
443
    }
444
 
445
}