OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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