OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
156 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.common.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.ui.PanelFrame;
18
import org.openconcerto.erp.panel.ChargementCreationSocietePanel;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.element.BaseSQLComponent;
21
import org.openconcerto.sql.element.ElementSQLObject;
22
import org.openconcerto.sql.element.SQLComponent;
23
import org.openconcerto.sql.model.DBRoot;
24
import org.openconcerto.sql.model.DBSystemRoot;
25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowAccessor;
27
import org.openconcerto.sql.model.SQLRowValues;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.model.graph.TablesMap;
30
import org.openconcerto.sql.sqlobject.ElementComboBox;
31
import org.openconcerto.sql.sqlobject.SQLTextCombo;
32
import org.openconcerto.ui.DefaultGridBagConstraints;
33
import org.openconcerto.ui.FormLayouter;
34
import org.openconcerto.ui.TitledSeparator;
35
import org.openconcerto.ui.component.InteractionMode;
36
import org.openconcerto.utils.ListMap;
37
 
38
import java.awt.GridBagConstraints;
39
import java.awt.GridBagLayout;
40
import java.awt.Insets;
41
import java.sql.SQLException;
42
import java.util.ArrayList;
43
import java.util.Collections;
44
import java.util.Date;
45
import java.util.List;
46
 
47
import javax.swing.BorderFactory;
48
import javax.swing.JComboBox;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JTextField;
52
import javax.swing.SwingConstants;
53
 
54
/**
55
 * Sociétés existantes avec le nom de la base associée
56
 */
57
public class SocieteCommonSQLElement extends ComptaSQLConfElement {
58
 
59
    private static final String TABLE_NAME = "SOCIETE_COMMON";
60
 
61
    static public final DBRoot getRoot(final SQLRow company) {
62
        try {
63
            return getRoot(company, false);
64
        } catch (SQLException e) {
65
            // shouldn't happen since we don't allow refresh
66
            throw new IllegalStateException(e);
67
        }
68
    }
69
 
70
    static public final DBRoot getRoot(final SQLRow company, final boolean allowRefresh) throws SQLException {
71
        final String rootName = company.getString("DATABASE_NAME");
72
        final DBSystemRoot sysRoot = company.getTable().getDBSystemRoot();
73
        if (allowRefresh && !sysRoot.contains(rootName)) {
74
            sysRoot.addRootToMap(rootName);
75
            sysRoot.refresh(TablesMap.createFromTables(rootName, Collections.singleton(TABLE_NAME)), true);
76
        }
77
        return sysRoot.getRoot(rootName);
78
    }
79
 
80
    public SocieteCommonSQLElement(DBRoot root) {
81
        super(root.getTable(TABLE_NAME), "une société", "sociétés");
82
    }
83
 
84
    @Override
85
    public ListMap<String, String> getShowAs() {
86
        return ListMap.singleton(null, getListFields());
87
    }
88
 
89
    protected List<String> getListFields() {
90
        final List<String> l = new ArrayList<String>();
91
        l.add("NOM");
92
        return l;
93
    }
94
 
95
    protected List<String> getComboFields() {
96
        final List<String> l = new ArrayList<String>();
97
        l.add("NOM");
98
        return l;
99
    }
100
 
101
    public final static Date getDateDebutExercice() {
102
 
103
        SQLTable tableExercice = Configuration.getInstance().getBase().getTable("EXERCICE_COMMON");
104
 
105
        SQLRow societeRow = ((ComptaPropsConfiguration) Configuration.getInstance()).getRowSociete();
106
        return (Date) tableExercice.getRow(societeRow.getInt("ID_EXERCICE_COMMON")).getObject("DATE_DEB");
107
    }
108
 
109
    /*
110
     * (non-Javadoc)
111
     *
112
     * @see org.openconcerto.devis.SQLElement#getComponent()
113
     */
114
    public SQLComponent createComponent() {
115
        return new BaseSQLComponent(this) {
116
 
117
            private JTextField textNom, textNumSiret, textNumNII, textNumAPE;
118
            private JTextField textNumTel, textNumFax, textEmail, textNumUrssaff;
119
            // private ITextArea textAdresse;
120
            private JComboBox combo;
121
            private JLabel labelPlan;
122
            private ElementSQLObject eltExercice;
123
            private TitledSeparator sep, sepPlan;
124
            private SQLTextCombo textType;
125
 
126
            public void addViews() {
127
                this.setLayout(new GridBagLayout());
128
                final GridBagConstraints c = new DefaultGridBagConstraints();
129
 
130
                this.textNom = new JTextField();
131
                this.textNumAPE = new JTextField();
132
                this.textNumNII = new JTextField();
133
                this.textNumSiret = new JTextField();
134
                this.textNumTel = new JTextField();
135
                this.textNumFax = new JTextField();
136
                this.textEmail = new JTextField();
137
                this.textType = new SQLTextCombo();
138
                this.textNumUrssaff = new JTextField();
139
 
140
                /***********************************************************************************
141
                 * Informations générales
142
                 **********************************************************************************/
143
                TitledSeparator sepInfos = new TitledSeparator("Informations générales");
144
                c.gridwidth = 4;
145
                c.gridx = 0;
146
                c.gridy++;
147
                this.add(sepInfos, c);
148
 
149
                // Type
150
                c.gridy++;
151
                c.gridwidth = 1;
152
                c.weightx = 0;
153
                JLabel labelType = new JLabel(getLabelFor("TYPE"));
154
                labelType.setHorizontalAlignment(SwingConstants.RIGHT);
155
                this.add(labelType, c);
156
 
157
                c.gridx++;
158
                c.gridwidth = 1;
159
                c.weightx = 1;
160
                this.add(this.textType, c);
161
 
162
                // Nom
163
                c.gridx++;
164
                c.gridwidth = 1;
165
                c.weightx = 0;
166
                JLabel labelNom = new JLabel(getLabelFor("NOM"));
167
                labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
168
                this.add(labelNom, c);
169
 
170
                c.gridx++;
171
                c.gridwidth = 1;
172
                c.weightx = 1;
173
                this.add(this.textNom, c);
174
 
175
                // Numero Siret
176
                c.gridy++;
177
                c.gridx = 0;
178
                c.weightx = 0;
179
                JLabel labelNumSiret = new JLabel(getLabelFor("NUM_SIRET"));
180
                labelNumSiret.setHorizontalAlignment(SwingConstants.RIGHT);
181
                this.add(labelNumSiret, c);
182
 
183
                c.gridx++;
184
                c.weightx = 1;
185
                this.add(this.textNumSiret, c);
186
 
187
                // Numero NII
188
                c.gridx++;
189
                c.weightx = 0;
190
                JLabel labelNumNII = new JLabel(getLabelFor("NUM_NII"));
191
                labelNumNII.setHorizontalAlignment(SwingConstants.RIGHT);
192
                this.add(labelNumNII, c);
193
 
194
                c.gridx++;
195
                c.weightx = 1;
196
                this.add(this.textNumNII, c);
197
 
198
                // Numero URSSAF
199
                c.gridy++;
200
                c.gridx = 0;
201
                c.weightx = 0;
202
                JLabel labelNumUrs = new JLabel(getLabelFor("NUMERO_URSSAF"));
203
                labelNumUrs.setHorizontalAlignment(SwingConstants.RIGHT);
204
                this.add(labelNumUrs, c);
205
 
206
                c.gridx++;
207
                c.weightx = 1;
208
                this.add(this.textNumUrssaff, c);
209
 
210
                // Numero APE
211
                c.gridx++;
212
                c.weightx = 0;
213
                JLabel labelNumAPE = new JLabel(getLabelFor("NUM_APE"));
214
                labelNumAPE.setHorizontalAlignment(SwingConstants.RIGHT);
215
                this.add(labelNumAPE, c);
216
 
217
                c.gridx++;
218
                c.weightx = 1;
219
                this.add(this.textNumAPE, c);
220
 
221
                // Org social
222
                c.gridy++;
223
                c.gridx = 0;
224
                c.weightx = 0;
225
                JLabel labelIDSoc = new JLabel(getLabelFor("ORG_PROTECTION_SOCIAL_ID"));
226
                labelIDSoc.setHorizontalAlignment(SwingConstants.RIGHT);
227
                this.add(labelIDSoc, c);
228
 
229
                c.gridx++;
230
                c.weightx = 1;
231
                JTextField fieldIDSoc = new JTextField();
232
                this.add(fieldIDSoc, c);
233
                this.addView(fieldIDSoc, "ORG_PROTECTION_SOCIAL_ID");
234
 
235
                // RCS
236
                c.gridy++;
237
                c.gridx = 0;
238
                c.weightx = 0;
239
                JLabel labelRCS = new JLabel(getLabelFor("RCS"));
240
                labelRCS.setHorizontalAlignment(SwingConstants.RIGHT);
241
                this.add(labelRCS, c);
242
 
243
                c.gridx++;
244
                c.weightx = 1;
245
                JTextField fieldRCS = new JTextField();
246
                this.add(fieldRCS, c);
247
                this.addView(fieldRCS, "RCS");
248
 
249
                // Capital
250
                c.gridx++;
251
                c.weightx = 0;
252
                JLabel labelCapital = new JLabel(getLabelFor("CAPITAL"));
253
                labelCapital.setHorizontalAlignment(SwingConstants.RIGHT);
254
                this.add(labelCapital, c);
255
 
256
                c.gridx++;
257
                c.weightx = 1;
258
                JTextField fieldCapital = new JTextField();
259
                this.add(fieldCapital, c);
260
                this.addView(fieldCapital, "CAPITAL");
261
 
262
                // IBAN
263
                c.gridy++;
264
                c.gridx = 0;
265
                c.weightx = 0;
266
                JLabel labelIban = new JLabel(getLabelFor("IBAN"));
267
                labelIban.setHorizontalAlignment(SwingConstants.RIGHT);
268
                this.add(labelIban, c);
269
 
270
                c.gridx++;
271
                c.weightx = 1;
272
                JTextField fieldIban = new JTextField();
273
                this.add(fieldIban, c);
274
                this.addView(fieldIban, "IBAN");
275
 
276
                // BIC
277
                c.gridx++;
278
                c.weightx = 0;
279
                JLabel labelBIC = new JLabel(getLabelFor("BIC"));
280
                labelBIC.setHorizontalAlignment(SwingConstants.RIGHT);
281
                this.add(labelBIC, c);
282
 
283
                c.gridx++;
284
                c.weightx = 1;
285
                JTextField fieldBIC = new JTextField();
286
                this.add(fieldBIC, c);
287
                this.addView(fieldBIC, "BIC");
288
 
289
                // SEPA creditor
290
                c.gridx = 0;
291
                c.gridy++;
292
                c.weightx = 0;
293
                JLabel labelICS = new JLabel(getLabelFor("SEPA_CREDITOR_ID"));
294
                labelICS.setHorizontalAlignment(SwingConstants.RIGHT);
295
                this.add(labelICS, c);
296
 
297
                c.gridx++;
298
                c.weightx = 1;
299
                JTextField fieldICS = new JTextField();
300
                this.add(fieldICS, c);
301
                this.addView(fieldICS, "SEPA_CREDITOR_ID");
302
 
303
                // Assurance
304
                if (getTable().contains("NUMERO_POLICE")) {
305
                    c.gridy++;
306
                    c.gridx = 0;
307
                    c.weightx = 0;
308
                    JLabel labelPolice = new JLabel(getLabelFor("NUMERO_POLICE"));
309
                    labelPolice.setHorizontalAlignment(SwingConstants.RIGHT);
310
                    this.add(labelPolice, c);
311
 
312
                    c.gridx++;
313
                    c.weightx = 1;
314
                    JTextField fieldPolice = new JTextField();
315
                    this.add(fieldPolice, c);
316
                    this.addView(fieldPolice, "NUMERO_POLICE");
317
                }
318
 
319
                // Adresse
320
                final TitledSeparator sepAdresse = new TitledSeparator(getLabelFor("ID_ADRESSE_COMMON"));
321
                c.gridx = 0;
322
                c.gridy++;
323
                c.gridwidth = 4;
324
                c.gridheight = 1;
325
                c.weightx = 1;
326
                c.weighty = 0;
327
                c.fill = GridBagConstraints.BOTH;
328
                this.add(sepAdresse, c);
329
                this.addView("ID_ADRESSE_COMMON", REQ + ";" + DEC + ";" + SEP);
330
                ElementSQLObject eltAdr = (ElementSQLObject) this.getView("ID_ADRESSE_COMMON");
331
                c.gridy++;
332
                this.add(eltAdr, c);
333
                // Contact
334
                JPanel panelContact = new JPanel();
335
                panelContact.setLayout(new GridBagLayout());
336
                panelContact.setBorder(BorderFactory.createTitledBorder("Contacts"));
337
                final GridBagConstraints cc = new DefaultGridBagConstraints();
338
 
339
                // Numero de telephone
340
                JLabel labelNumTel = new JLabel(getLabelFor("NUM_TEL"));
341
                labelNumTel.setHorizontalAlignment(SwingConstants.RIGHT);
342
                cc.weightx = 0;
343
                panelContact.add(labelNumTel, cc);
344
 
345
                cc.gridx++;
346
                cc.weightx = 1;
347
                panelContact.add(this.textNumTel, cc);
348
 
349
                // Numero de fax
350
                cc.gridy++;
351
                cc.gridx = 0;
352
                cc.weightx = 0;
353
                JLabel labelNumFax = new JLabel(getLabelFor("NUM_FAX"));
354
                labelNumFax.setHorizontalAlignment(SwingConstants.RIGHT);
355
                panelContact.add(labelNumFax, cc);
356
 
357
                cc.gridx++;
358
                cc.weightx = 1;
359
                panelContact.add(this.textNumFax, cc);
360
 
361
                // EMail
362
                cc.gridx = 0;
363
                cc.gridy++;
364
                cc.weightx = 0;
365
                JLabel labelEMail = new JLabel(getLabelFor("MAIL"));
366
                labelEMail.setHorizontalAlignment(SwingConstants.RIGHT);
367
                panelContact.add(labelEMail, cc);
368
 
369
                cc.gridx++;
370
                cc.weightx = 1;
371
                panelContact.add(this.textEmail, cc);
372
 
373
                c.gridy++;
374
                c.gridwidth = GridBagConstraints.REMAINDER;
375
                c.fill = GridBagConstraints.BOTH;
376
                this.add(panelContact, c);
377
                c.fill = GridBagConstraints.HORIZONTAL;
378
                c.gridwidth = 1;
379
 
380
                // Devise
381
                if (getTable().contains("ID_DEVISE")) {
382
                    c.gridy++;
383
                    c.gridx = 0;
384
                    c.weightx = 0;
385
                    c.gridwidth = 1;
386
                    JLabel labelDevise = new JLabel(getLabelFor("ID_DEVISE"));
387
                    labelDevise.setHorizontalAlignment(SwingConstants.RIGHT);
388
                    this.add(labelDevise, c);
389
 
390
                    c.gridx++;
391
                    c.gridwidth = 3;
392
                    c.fill = GridBagConstraints.NONE;
393
                    ElementComboBox boxDevise = new ElementComboBox();
394
                    this.add(boxDevise, c);
395
                    this.addView(boxDevise, "ID_DEVISE");
396
                }
397
 
398
                /***********************************************************************************
399
                 * DATE D'EXERCICE
400
                 **********************************************************************************/
401
                this.sep = new TitledSeparator("Date de l'exercice");
402
                c.gridwidth = GridBagConstraints.REMAINDER;
403
                c.gridx = 0;
404
                c.gridy++;
405
                c.insets = new Insets(10, 2, 1, 2);
406
                c.fill = GridBagConstraints.HORIZONTAL;
407
                this.add(this.sep, c);
408
 
409
                c.gridx = 0;
410
                c.gridy++;
411
                c.weightx = 0;
412
                c.weighty = 1;
413
                c.anchor = GridBagConstraints.NORTHWEST;
414
                c.insets = new Insets(2, 2, 1, 2);
415
 
416
                this.addView("ID_EXERCICE_COMMON", REQ + ";" + DEC + ";" + SEP);
417
                this.eltExercice = (ElementSQLObject) this.getView("ID_EXERCICE_COMMON");
418
                c.gridwidth = GridBagConstraints.REMAINDER;
419
                this.add(this.eltExercice, c);
420
 
421
                // Regime fiscale
422
                /*
423
                 * c.gridx = 0; c.gridy++; c.gridwidth = 1; c.weightx = 0; JLabel labelRegime = new
424
                 * JLabel("Régime fiscale ");
425
                 * labelRegime.setHorizontalAlignment(SwingConstants.RIGHT); this.add(labelRegime,
426
                 * c);
427
                 */
428
 
429
                /***********************************************************************************
430
                 * Choix du plan comptable
431
                 **********************************************************************************/
432
                this.sepPlan = new TitledSeparator("Plan comptable de l'entreprise");
433
                c.gridwidth = GridBagConstraints.REMAINDER;
434
                c.gridx = 0;
435
                c.gridy++;
436
                c.insets = new Insets(10, 2, 1, 2);
437
                this.add(this.sepPlan, c);
438
                JPanel panelPlan = new JPanel();
439
                this.labelPlan = new JLabel("Choix du plan comptable", SwingConstants.RIGHT);
440
                this.combo = new JComboBox();
441
                this.combo.addItem("Base");
442
                this.combo.addItem("Abrégé");
443
                this.combo.addItem("Développé");
444
                panelPlan.add(this.labelPlan);
445
                panelPlan.add(this.combo);
446
 
447
                c.gridx = 0;
448
                c.gridy++;
449
                c.gridwidth = GridBagConstraints.REMAINDER;
450
                c.insets = new Insets(2, 2, 1, 2);
451
                c.fill = GridBagConstraints.NONE;
452
                this.add(panelPlan, c);
453
 
454
                c.gridy++;
455
                final JPanel additionalPanel = new JPanel();
456
                this.add(additionalPanel, c);
457
                this.setAdditionalFieldsPanel(new FormLayouter(additionalPanel, 2, 1));
458
 
459
                this.addRequiredSQLObject(this.textNom, "NOM");
460
                // this.addSQLObject(this.textAdresse, "ADRESSE");
461
                this.addRequiredSQLObject(this.textNumAPE, "NUM_APE");
462
                this.addRequiredSQLObject(this.textNumNII, "NUM_NII");
463
                this.addRequiredSQLObject(this.textNumSiret, "NUM_SIRET");
464
                this.addSQLObject(this.textEmail, "MAIL");
465
                this.addSQLObject(this.textNumTel, "NUM_TEL");
466
                this.addSQLObject(this.textNumFax, "NUM_FAX");
467
                this.addSQLObject(this.textNumUrssaff, "NUMERO_URSSAF");
468
                this.addSQLObject(this.textType, "TYPE");
469
            }
470
 
471
            public int insert(SQLRow order) {
472
                int id = super.insert(order);
473
                SQLRow row = getTable().getRow(id);
474
                SQLRowValues rowVals = row.getForeignRow("ID_EXERCICE_COMMON").createEmptyUpdateRow();
475
                rowVals.put("ID_SOCIETE_COMMON", id);
476
                try {
477
                    rowVals.update();
478
                } catch (SQLException e) {
479
                    // TODO Auto-generated catch block
480
                    e.printStackTrace();
481
                }
482
                creationBase(id, this.combo.getSelectedIndex());
483
                return id;
484
            }
485
 
486
            /*
487
             * public int insert() {
488
             *
489
             * int id = super.insert(); creationBase(id); return id; }
490
             */
491
 
492
            private void creationBase(int id, int typePCG) {
493
 
494
                System.err.println("display chargement societe panel");
495
 
496
                PanelFrame frameChargement = new PanelFrame(new ChargementCreationSocietePanel(id, typePCG), "Création d'une société");
497
                frameChargement.setVisible(true);
498
 
499
                /*
500
                 * System.err.println("Création de la base");
501
                 * ActionDB.dupliqueMySQLDB("Default", "OpenConcerto" + id); SQLRowValues
502
                 * rowVals = new SQLRowValues(getTable()); rowVals.put("DATABASE_NAME", "OpenConcerto"
503
                 * + id); try { rowVals.update(id); } catch (SQLException e) {
504
                 *
505
                 * e.printStackTrace(); }
506
                 */
507
                /*
508
                 * SQLTable comptePCETable = ((ComptaPropsConfiguration)
509
                 * Configuration.getInstance()).getSQLBaseSociete().getTable("COMPTE_PCE"); SQLTable
510
                 * comptePCGTable = ((ComptaPropsConfiguration)
511
                 * Configuration.getInstance()).getSQLBaseSociete().getTable("COMPTE_PCG"); // MAYBE
512
                 * Vérifier qu'aucun n'est deja créé???? // On crée le PCE à partir du PCG
513
                 * selectionné; SQLSelect selCompte = new SQLSelect(getTable().getBase());
514
                 * selCompte.addSelect(comptePCGTable.getField("NUMERO"));
515
                 * selCompte.addSelect(comptePCGTable.getField("NOM"));
516
                 * selCompte.addSelect(comptePCGTable.getField("INFOS"));
517
                 *
518
                 * if (this.combo.getSelectedIndex() == 0) {
519
                 * selCompte.setWhere("ID_TYPE_COMPTE_PCG_BASE", "!=", 1); } else { if
520
                 * (this.combo.getSelectedIndex() == 1) {
521
                 * selCompte.setWhere("ID_TYPE_COMPTE_PCG_AB", "!=", 1); } }
522
                 *
523
                 * String reqCompte = selCompte.asString(); Object obRep =
524
                 * getTable().getBase().getDataSource().execute(reqCompte, new ArrayListHandler());
525
                 *
526
                 * List tmpCpt = (List) obRep;
527
                 *
528
                 * for (int i = 0; i < tmpCpt.size(); i++) { Object[] tmp = (Object[])
529
                 * tmpCpt.get(i);
530
                 *
531
                 * SQLRowValues vals = new SQLRowValues(comptePCETable); vals.put("NUMERO", tmp[0]);
532
                 * vals.put("NOM", tmp[1]); vals.put("INFOS", tmp[2]);
533
                 *
534
                 * try { vals.insert(); } catch (SQLException e) { e.printStackTrace(); } }
535
                 */
536
            }
537
 
538
            @Override
539
            public void select(SQLRowAccessor r) {
540
                super.select(r);
541
                if (r != null) {
542
                    disableEdition();
543
                }
544
            }
545
 
546
            @Override
547
            public void update() {
548
                // TODO Auto-generated method stub
549
                super.update();
550
                ComptaPropsConfiguration.getInstanceCompta().getRowSociete().fetchValues();
551
            }
552
 
553
            public void disableEdition() {
554
                this.combo.setVisible(false);
555
                this.labelPlan.setVisible(false);
556
                this.eltExercice.setEditable(InteractionMode.DISABLED);
557
                this.sepPlan.setVisible(false);
558
            }
559
        };
560
    }
561
}