OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 146 | Rev 170 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 146 Rev 152
Line 1... Line 1...
1
package org.openconcerto.modules.project;
1
package org.openconcerto.modules.project;
2
 
2
 
3
import java.awt.Color;
3
import java.awt.Color;
-
 
4
import java.awt.GridBagConstraints;
-
 
5
import java.awt.GridBagLayout;
4
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionEvent;
-
 
7
import java.awt.event.ActionListener;
-
 
8
import java.beans.PropertyChangeEvent;
-
 
9
import java.beans.PropertyChangeListener;
5
import java.io.IOException;
10
import java.io.IOException;
-
 
11
import java.math.BigDecimal;
-
 
12
import java.math.RoundingMode;
6
import java.sql.SQLException;
13
import java.sql.SQLException;
7
import java.util.ArrayList;
14
import java.util.ArrayList;
8
import java.util.Arrays;
15
import java.util.Arrays;
9
import java.util.Collection;
16
import java.util.Collection;
10
import java.util.Date;
17
import java.util.Date;
11
import java.util.List;
18
import java.util.List;
12
import java.util.Map;
19
import java.util.Map;
13
import java.util.Set;
20
import java.util.Set;
14
 
21
 
15
import javax.swing.AbstractAction;
22
import javax.swing.AbstractAction;
-
 
23
import javax.swing.JButton;
16
import javax.swing.JComponent;
24
import javax.swing.JComponent;
17
import javax.swing.JFrame;
25
import javax.swing.JFrame;
-
 
26
import javax.swing.JLabel;
18
import javax.swing.JOptionPane;
27
import javax.swing.JOptionPane;
-
 
28
import javax.swing.JPanel;
19
import javax.swing.JScrollPane;
29
import javax.swing.JScrollPane;
20
import javax.swing.SwingUtilities;
30
import javax.swing.SwingUtilities;
21
 
31
 
22
import org.openconcerto.erp.action.CreateFrameAbstractAction;
32
import org.openconcerto.erp.action.CreateFrameAbstractAction;
23
import org.openconcerto.erp.config.ComptaPropsConfiguration;
33
import org.openconcerto.erp.config.ComptaPropsConfiguration;
Line 49... Line 59...
49
import org.openconcerto.sql.model.DBRoot;
59
import org.openconcerto.sql.model.DBRoot;
50
import org.openconcerto.sql.model.FieldPath;
60
import org.openconcerto.sql.model.FieldPath;
51
import org.openconcerto.sql.model.SQLRow;
61
import org.openconcerto.sql.model.SQLRow;
52
import org.openconcerto.sql.model.SQLRowAccessor;
62
import org.openconcerto.sql.model.SQLRowAccessor;
53
import org.openconcerto.sql.model.SQLRowValues;
63
import org.openconcerto.sql.model.SQLRowValues;
-
 
64
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
54
import org.openconcerto.sql.model.SQLSelect;
65
import org.openconcerto.sql.model.SQLSelect;
-
 
66
import org.openconcerto.sql.model.SQLSelectJoin;
55
import org.openconcerto.sql.model.SQLTable;
67
import org.openconcerto.sql.model.SQLTable;
56
import org.openconcerto.sql.model.SQLTableEvent;
68
import org.openconcerto.sql.model.SQLTableEvent;
57
import org.openconcerto.sql.model.SQLTableEvent.Mode;
69
import org.openconcerto.sql.model.SQLTableEvent.Mode;
58
import org.openconcerto.sql.model.SQLTableModifiedListener;
70
import org.openconcerto.sql.model.SQLTableModifiedListener;
59
import org.openconcerto.sql.model.Where;
71
import org.openconcerto.sql.model.Where;
60
import org.openconcerto.sql.model.graph.Link.Direction;
72
import org.openconcerto.sql.model.graph.Link.Direction;
61
import org.openconcerto.sql.model.graph.Path;
73
import org.openconcerto.sql.model.graph.Path;
-
 
74
import org.openconcerto.sql.request.ComboSQLRequest;
-
 
75
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
62
import org.openconcerto.sql.utils.AlterTable;
76
import org.openconcerto.sql.utils.AlterTable;
63
import org.openconcerto.sql.utils.SQLCreateTable;
77
import org.openconcerto.sql.utils.SQLCreateTable;
64
import org.openconcerto.sql.view.EditFrame;
78
import org.openconcerto.sql.view.EditFrame;
-
 
79
import org.openconcerto.sql.view.EditPanel.EditMode;
65
import org.openconcerto.sql.view.IListFrame;
80
import org.openconcerto.sql.view.IListFrame;
66
import org.openconcerto.sql.view.ListeAddPanel;
81
import org.openconcerto.sql.view.ListeAddPanel;
67
import org.openconcerto.sql.view.column.ColumnFooterRenderer;
82
import org.openconcerto.sql.view.column.ColumnFooterRenderer;
68
import org.openconcerto.sql.view.column.ColumnPanel;
83
import org.openconcerto.sql.view.column.ColumnPanel;
69
import org.openconcerto.sql.view.column.ColumnPanelFetcher;
84
import org.openconcerto.sql.view.column.ColumnPanelFetcher;
70
import org.openconcerto.sql.view.column.ColumnRowRenderer;
85
import org.openconcerto.sql.view.column.ColumnRowRenderer;
71
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
86
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
72
import org.openconcerto.sql.view.list.IListe;
87
import org.openconcerto.sql.view.list.IListe;
73
import org.openconcerto.sql.view.list.RowAction;
88
import org.openconcerto.sql.view.list.RowAction;
-
 
89
import org.openconcerto.ui.DefaultGridBagConstraints;
-
 
90
import org.openconcerto.ui.FrameUtil;
-
 
91
import org.openconcerto.ui.PanelFrame;
74
import org.openconcerto.utils.CollectionUtils;
92
import org.openconcerto.utils.CollectionUtils;
75
import org.openconcerto.utils.ExceptionHandler;
93
import org.openconcerto.utils.ExceptionHandler;
76
import org.openconcerto.utils.Tuple2;
94
import org.openconcerto.utils.Tuple2;
77
import org.openconcerto.utils.cc.IClosure;
95
import org.openconcerto.utils.cc.IExnClosure;
78
import org.openconcerto.utils.cc.ITransformer;
96
import org.openconcerto.utils.cc.ITransformer;
79
import org.openconcerto.utils.i18n.TranslationManager;
97
import org.openconcerto.utils.i18n.TranslationManager;
80
 
98
 
81
public final class Module extends AbstractModule {
99
public final class Module extends AbstractModule {
82
 
100
 
83
    public static final String PROJECT_TABLENAME = "AFFAIRE";
101
    public static final String PROJECT_TABLENAME = "AFFAIRE";
84
    private List<String> listTableAffaire = Arrays.asList("DEMANDE_PRIX", "DEMANDE_ACHAT_ELEMENT", "SAISIE_VENTE_FACTURE", "AVOIR_CLIENT", "BON_DE_LIVRAISON", "COMMANDE_CLIENT", "DEVIS", "COMMANDE",
102
    private List<String> listTableAffaire = Arrays.asList("DEMANDE_PRIX", "BON_RECEPTION", "DEMANDE_ACHAT_ELEMENT", "SAISIE_VENTE_FACTURE", "AVOIR_CLIENT", "BON_DE_LIVRAISON", "COMMANDE_CLIENT",
85
            "SAISIE_ACHAT", "AVOIR_FOURNISSEUR");
103
            "DEVIS", "COMMANDE", "SAISIE_ACHAT", "AVOIR_FOURNISSEUR");
86
 
104
 
87
    public Module(ModuleFactory f) throws IOException {
105
    public Module(ModuleFactory f) throws IOException {
88
        super(f);
106
        super(f);
89
 
107
 
90
    }
108
    }
91
 
109
 
92
    @Override
110
    @Override
93
    protected void install(DBContext ctxt) {
111
    protected void install(DBContext ctxt) throws SQLException, IOException {
94
        super.install(ctxt);
112
        super.install(ctxt);
95
 
113
 
96
        if (ctxt.getLastInstalledVersion() == null) {
114
        if (ctxt.getLastInstalledVersion() == null) {
97
 
115
 
98
            if (ctxt.getRoot().getTable(PROJECT_TABLENAME) == null) {
116
            if (ctxt.getRoot().getTable(PROJECT_TABLENAME) == null) {
Line 119... Line 137...
119
                    if (!tableDevis.getFieldsName().contains("ID_AFFAIRE")) {
137
                    if (!tableDevis.getFieldsName().contains("ID_AFFAIRE")) {
120
                        AlterTableRestricted alter = ctxt.getAlterTable(table);
138
                        AlterTableRestricted alter = ctxt.getAlterTable(table);
121
                        alter.addForeignColumn("ID_AFFAIRE", createTable);
139
                        alter.addForeignColumn("ID_AFFAIRE", createTable);
122
                    }
140
                    }
123
                }
141
                }
-
 
142
 
124
                ctxt.manipulateData(new IClosure<DBRoot>() {
143
                ctxt.manipulateData(new IExnClosure<DBRoot, SQLException>() {
-
 
144
 
125
                    @Override
145
                    @Override
126
                    public void executeChecked(DBRoot input) {
146
                    public void executeChecked(DBRoot input) throws SQLException {
127
                        // Undefined Affaire
147
                        // Undefined Affaire
128
                        SQLTable tableAff = input.getTable(PROJECT_TABLENAME);
148
                        SQLTable tableAff = input.getTable(PROJECT_TABLENAME);
129
                        SQLRowValues rowVals = new SQLRowValues(tableAff);
149
                        SQLRowValues rowVals = new SQLRowValues(tableAff);
130
                        try {
-
 
131
                            rowVals.insert();
150
                        rowVals.insert();
132
                        } catch (SQLException exn) {
-
 
133
                            // TODO Bloc catch auto-généré
-
 
134
                            exn.printStackTrace();
-
 
135
                        }
-
 
136
 
151
 
137
                        // Etat Affaire
152
                        // Etat Affaire
138
                        SQLTable tableTypeAffaire = input.getTable("TYPE_AFFAIRE");
153
                        SQLTable tableTypeAffaire = input.getTable("TYPE_AFFAIRE");
139
                        rowVals = new SQLRowValues(tableTypeAffaire);
154
                        rowVals = new SQLRowValues(tableTypeAffaire);
140
                        try {
-
 
141
                            rowVals.put("NOM", "Indéfini");
155
                        rowVals.put("NOM", "Indéfini");
142
                            rowVals.insert();
156
                        rowVals.insert();
143
                        } catch (SQLException exn) {
-
 
144
                            // TODO Bloc catch auto-généré
-
 
145
                            exn.printStackTrace();
-
 
146
                        }
-
 
147
 
157
 
148
                        // Etat Affaire
158
                        // Etat Affaire
149
                        SQLTable tableEtatAffaire = input.getTable("ETAT_AFFAIRE");
159
                        SQLTable tableEtatAffaire = input.getTable("ETAT_AFFAIRE");
150
                        rowVals = new SQLRowValues(tableEtatAffaire);
160
                        rowVals = new SQLRowValues(tableEtatAffaire);
151
                        try {
-
 
152
                            rowVals.put("NOM", "Indéfini");
161
                        rowVals.put("NOM", "Indéfini");
153
                            rowVals.insert();
162
                        rowVals.insert();
154
 
163
 
155
                            rowVals.clear();
164
                        rowVals.clear();
156
                            rowVals.put("NOM", "A traiter");
165
                        rowVals.put("NOM", "A traiter");
Line 169... Line 178...
169
                            rowVals.insert();
178
                        rowVals.insert();
170
 
179
 
171
                            rowVals.clear();
180
                        rowVals.clear();
172
                            rowVals.put("NOM", "Dossier clos");
181
                        rowVals.put("NOM", "Dossier clos");
173
                            rowVals.insert();
182
                        rowVals.insert();
174
                        } catch (SQLException exn) {
-
 
175
                            exn.printStackTrace();
-
 
176
                        }
-
 
177
 
-
 
178
                    }
183
                    }
179
                });
184
                });
-
 
185
 
180
            }
186
            }
181
        }
187
        }
182
 
188
 
183
    }
189
    }
184
 
190
 
Line 305... Line 311...
305
                        ProjectHistory histo = new ProjectHistory();
311
                        ProjectHistory histo = new ProjectHistory();
306
                        histo.getHistoriquePanel().selectIDinJList(IListe.get(e).getSelectedRow().getForeignID("ID_AFFAIRE"));
312
                        histo.getHistoriquePanel().selectIDinJList(IListe.get(e).getSelectedRow().getForeignID("ID_AFFAIRE"));
307
                        histo.getFrame().setVisible(true);
313
                        histo.getFrame().setVisible(true);
308
                    }
314
                    }
309
                }, false) {
315
                }, false) {
-
 
316
 
310
                    @Override
317
                    @Override
311
                    public boolean enabledFor(IListeEvent evt) {
318
                    public boolean enabledFor(IListeEvent evt) {
312
                        if (evt.getSelectedRows().size() == 1) {
319
                        if (evt.getSelectedRows().size() == 1) {
313
                            SQLRowAccessor r = evt.getSelectedRow();
320
                            SQLRowAccessor r = evt.getSelectedRow();
314
                            return !r.isForeignEmpty("ID_AFFAIRE");
321
                            return !r.isForeignEmpty("ID_AFFAIRE");
Line 348... Line 355...
348
                Collection<? extends SQLRowAccessor> l = selectedRow.getReferentRows(selectedRow.getTable().getTable("AFFAIRE"));
355
                Collection<? extends SQLRowAccessor> l = selectedRow.getReferentRows(selectedRow.getTable().getTable("AFFAIRE"));
349
                histo.getHistoriquePanel().selectIDinJList(l.iterator().next().getID());
356
                histo.getHistoriquePanel().selectIDinJList(l.iterator().next().getID());
350
                histo.getFrame().setVisible(true);
357
                histo.getFrame().setVisible(true);
351
            }
358
            }
352
        }, false) {
359
        }, false) {
-
 
360
 
353
            @Override
361
            @Override
354
            public boolean enabledFor(IListeEvent evt) {
362
            public boolean enabledFor(IListeEvent evt) {
355
                if (evt.getSelectedRows().size() == 1) {
363
                if (evt.getSelectedRows().size() == 1) {
356
                    SQLRowAccessor r = evt.getSelectedRow();
364
                    SQLRowAccessor r = evt.getSelectedRow();
357
                    Collection<? extends SQLRowAccessor> l = r.getReferentRows(r.getTable().getTable("AFFAIRE"));
365
                    Collection<? extends SQLRowAccessor> l = r.getReferentRows(r.getTable().getTable("AFFAIRE"));
Line 380... Line 388...
380
                SQLTable table = elt.getTable();
388
                SQLTable table = elt.getTable();
381
                Path p = new Path(table).add(table.getTable("AFFAIRE"), Direction.REFERENT);
389
                Path p = new Path(table).add(table.getTable("AFFAIRE"), Direction.REFERENT);
382
                return CollectionUtils.createSet(new FieldPath(p, "NUMERO"));
390
                return CollectionUtils.createSet(new FieldPath(p, "NUMERO"));
383
            }
391
            }
384
        });
392
        });
385
 
-
 
-
 
393
        new CmdToBrSQLInjector();
386
        new QuoteToOrderSQLInjector();
394
        new QuoteToOrderSQLInjector();
387
        new QuoteToInvoiceSQLInjector();
395
        new QuoteToInvoiceSQLInjector();
388
        new OrderToInvoiceSQLInjector();
396
        new OrderToInvoiceSQLInjector();
389
    }
397
    }
390
 
398
 
391
    public void checkAffaire(int id) {
399
    public static JFrame checkAffaire(int id) {
-
 
400
        return checkAffaire(id, true);
-
 
401
    }
-
 
402
 
-
 
403
    public static JFrame checkAffaire(int id, final boolean createCommandeClient) {
392
        final SQLTable tableDevis = Configuration.getInstance().getRoot().findTable("DEVIS");
404
        final SQLTable tableDevis = Configuration.getInstance().getRoot().findTable("DEVIS");
393
        final SQLTable tableNum = Configuration.getInstance().getRoot().findTable("NUMEROTATION_AUTO");
405
        final SQLTable tableNum = Configuration.getInstance().getRoot().findTable("NUMEROTATION_AUTO");
394
        final SQLRow row = tableDevis.getRow(id);
406
        final SQLRow row = tableDevis.getRow(id);
395
        final SQLRow rowAffaire = row.getForeign("ID_AFFAIRE");
407
        final SQLRow rowAffaire = row.getForeign("ID_AFFAIRE");
396
        if (rowAffaire == null || rowAffaire.isUndefined()) {
408
        if (rowAffaire == null || rowAffaire.isUndefined()) {
397
            if (row.getInt("ID_ETAT_DEVIS") == EtatDevisSQLElement.ACCEPTE) {
409
            if (row.getInt("ID_ETAT_DEVIS") == EtatDevisSQLElement.ACCEPTE) {
398
                // FIXME Vérifier si le devis n'est pas déjà rattaché à une affaire
410
                // FIXME Vérifier si le devis n'est pas déjà rattaché à une affaire
-
 
411
 
-
 
412
                JPanel p = new JPanel(new GridBagLayout());
-
 
413
                GridBagConstraints c = new DefaultGridBagConstraints();
-
 
414
                c.gridwidth = GridBagConstraints.REMAINDER;
-
 
415
                p.add(new JLabel("Voulez vous intégrer ce devis sur une affaire existante?"), c);
-
 
416
                c.gridy++;
-
 
417
                c.gridwidth = 1;
-
 
418
                p.add(new JLabel("Rattacher à l'affaire : "), c);
-
 
419
                c.gridx++;
-
 
420
                final SQLRequestComboBox box = new SQLRequestComboBox();
-
 
421
                final ComboSQLRequest comboRequest = Configuration.getInstance().getDirectory().getElement("AFFAIRE").getComboRequest(true);
-
 
422
                Where w = new Where(comboRequest.getPrimaryTable().getField("ID_CLIENT"), "=", row.getForeignID("ID_CLIENT"));
-
 
423
                comboRequest.setWhere(w);
-
 
424
                box.uiInit(comboRequest);
-
 
425
                p.add(box, c);
-
 
426
                c.gridy++;
-
 
427
                c.gridx = 0;
-
 
428
                final JButton buttonApply = new JButton("Appliquer");
-
 
429
                JButton buttonAnnuler = new JButton("Nouvelle affaire");
-
 
430
                p.add(buttonApply, c);
-
 
431
                buttonApply.setEnabled(false);
-
 
432
                box.addValueListener(new PropertyChangeListener() {
-
 
433
 
-
 
434
                    @Override
-
 
435
                    public void propertyChange(PropertyChangeEvent evt) {
-
 
436
                        buttonApply.setEnabled(box.getSelectedRow() != null);
-
 
437
 
-
 
438
                    }
-
 
439
                });
-
 
440
                c.gridx++;
-
 
441
                p.add(buttonAnnuler, c);
-
 
442
                final PanelFrame f = new PanelFrame(p, "Affection d'un devis à une affaire");
-
 
443
 
-
 
444
                buttonAnnuler.addActionListener(new ActionListener() {
-
 
445
 
-
 
446
                    @Override
-
 
447
                    public void actionPerformed(ActionEvent ae) {
-
 
448
                        f.dispose();
399
                final SQLTable table = tableDevis.getTable(PROJECT_TABLENAME);
449
                        final SQLTable table = tableDevis.getTable(PROJECT_TABLENAME);
400
                final SQLRowValues rowVals = new SQLRowValues(table);
450
                        final SQLRowValues rowVals = new SQLRowValues(table);
401
 
451
 
402
                final String nextNumero = NumerotationAutoSQLElement.getNextNumero(ProjectSQLElement.class);
452
                        final String nextNumero = NumerotationAutoSQLElement.getNextNumero(ProjectSQLElement.class);
403
                rowVals.put("NUMERO", nextNumero);
453
                        rowVals.put("NUMERO", nextNumero);
Line 421... Line 471...
421
 
471
 
422
                SQLRowValues rowValsDevis = row.asRowValues();
472
                        SQLRowValues rowValsDevis = row.asRowValues();
423
                rowValsDevis.put("ID_AFFAIRE", rowVals);
473
                        rowValsDevis.put("ID_AFFAIRE", rowVals);
424
 
474
 
425
                try {
475
                        try {
-
 
476
                            int id = rowVals.commit().getID();
-
 
477
                            row.fetchValues();
-
 
478
                            if (createCommandeClient) {
-
 
479
                                Configuration.getInstance().getDirectory().getElement(DevisSQLElement.class).transfertCommandeClient(Arrays.asList(row.asRowValues()));
-
 
480
                                EditFrame frame = new EditFrame(Configuration.getInstance().getDirectory().getElement("AFFAIRE"), EditMode.MODIFICATION);
-
 
481
                                frame.selectionId(id);
-
 
482
                                FrameUtil.showPacked(frame);
-
 
483
                            }
-
 
484
                        } catch (SQLException exn) {
-
 
485
                            // TODO Bloc catch auto-généré
-
 
486
                            exn.printStackTrace();
-
 
487
                        }
-
 
488
                    }
-
 
489
                });
-
 
490
 
-
 
491
                buttonApply.addActionListener(new ActionListener() {
-
 
492
 
-
 
493
                    @Override
-
 
494
                    public void actionPerformed(ActionEvent e) {
-
 
495
                        f.dispose();
-
 
496
 
-
 
497
                        SQLRowValues rowValsDevis = row.asRowValues();
-
 
498
                        rowValsDevis.put("ID_AFFAIRE", box.getSelectedId());
-
 
499
 
-
 
500
                        try {
426
                    rowVals.commit();
501
                            rowValsDevis.commit();
427
                } catch (SQLException exn) {
502
                        } catch (SQLException exn) {
428
                    // TODO Bloc catch auto-généré
503
                            // TODO Bloc catch auto-généré
429
                    exn.printStackTrace();
504
                            exn.printStackTrace();
430
                }
505
                        }
431
            }
506
                    }
-
 
507
                });
-
 
508
                FrameUtil.show(f);
-
 
509
                return f;
-
 
510
            }
432
 
511
 
433
        } else if (row.getInt("ID_ETAT_DEVIS") != EtatDevisSQLElement.ACCEPTE) {
512
        } else if (row.getInt("ID_ETAT_DEVIS") != EtatDevisSQLElement.ACCEPTE) {
434
            SQLRowValues rowVals = rowAffaire.asRowValues();
513
            SQLRowValues rowVals = rowAffaire.asRowValues();
435
            rowVals.putEmptyLink("ID_DEVIS");
514
            rowVals.putEmptyLink("ID_DEVIS");
436
            try {
515
            try {
437
                rowVals.update();
516
                rowVals.update();
438
            } catch (SQLException exn) {
517
            } catch (SQLException exn) {
439
                exn.printStackTrace();
518
                exn.printStackTrace();
440
            }
519
            }
441
        }
520
        }
-
 
521
        return null;
442
    }
522
    }
443
 
523
 
444
    List<Integer> idsAsked = new ArrayList<Integer>();
524
    List<Integer> idsAsked = new ArrayList<Integer>();
445
 
525
 
446
    public void checkProjectState(final SQLRowAccessor rowFact) {
526
    public void checkProjectState(final SQLRowAccessor rowFact) {
Line 499... Line 579...
499
                }
579
                }
500
                selectedRow.getTable().fireTableModified(selectedRow.getID());
580
                selectedRow.getTable().fireTableModified(selectedRow.getID());
501
                checkAffaire(selectedRow.getID());
581
                checkAffaire(selectedRow.getID());
502
            }
582
            }
503
        }, false, "project.accept") {
583
        }, false, "project.accept") {
-
 
584
 
504
            public boolean enabledFor(List<SQLRowValues> selection) {
585
            public boolean enabledFor(List<SQLRowValues> selection) {
505
                if (selection != null && selection.size() == 1) {
586
                if (selection != null && selection.size() == 1) {
506
                    if (selection.get(0).getInt("ID_ETAT_DEVIS") == EtatDevisSQLElement.EN_ATTENTE) {
587
                    if (selection.get(0).getInt("ID_ETAT_DEVIS") == EtatDevisSQLElement.EN_ATTENTE) {
507
                        return true;
588
                        return true;
508
                    }
589
                    }
509
                }
590
                }
510
                return false;
591
                return false;
511
            };
592
            };
-
 
593
 
512
        };
594
        };
513
    }
595
    }
514
 
596
 
515
    @Override
597
    @Override
516
    protected void setupComponents(final ComponentsContext ctxt) {
598
    protected void setupComponents(final ComponentsContext ctxt) {
-
 
599
 
517
        DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
600
        DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
518
        List<String> table2check = Arrays.asList("DEMANDE_PRIX", "DEMANDE_ACHAT_ELEMENT");
601
        List<String> table2check = Arrays.asList("BON_RECEPTION", "DEMANDE_PRIX", "DEMANDE_ACHAT_ELEMENT");
519
        for (String table : table2check) {
602
        for (String table : table2check) {
520
            if (root.contains(table)) {
603
            if (root.contains(table)) {
521
                SQLTable tableCR = root.getTable(table);
604
                SQLTable tableCR = root.getTable(table);
522
                if (!tableCR.contains("ID_AFFAIRE")) {
605
                if (!tableCR.contains("ID_AFFAIRE")) {
523
                    try {
606
                    try {
Line 533... Line 616...
533
                        e.printStackTrace();
616
                        e.printStackTrace();
534
                    }
617
                    }
535
                }
618
                }
536
            }
619
            }
537
        }
620
        }
-
 
621
 
538
        for (String table : this.listTableAffaire) {
622
        for (String table : this.listTableAffaire) {
539
            if (!table.equalsIgnoreCase("DEVIS"))
623
            if (!table.equalsIgnoreCase("DEVIS"))
540
                ctxt.putAdditionalField(table, "ID_AFFAIRE");
624
                ctxt.putAdditionalField(table, "ID_AFFAIRE");
541
        }
625
        }
542
 
626
 
Line 565... Line 649...
565
            public JFrame createFrame() {
649
            public JFrame createFrame() {
566
                return new EditFrame(ctxt.getElement(PROJECT_TABLENAME));
650
                return new EditFrame(ctxt.getElement(PROJECT_TABLENAME));
567
            }
651
            }
568
        }, MainFrame.CREATE_MENU);
652
        }, MainFrame.CREATE_MENU);
569
 
653
 
570
        // ctxt.addMenuItem(new AbstractAction("Export Affaire") {
-
 
571
        // @Override
-
 
572
        // public void actionPerformed(ActionEvent e) {
-
 
573
        // ProjectImportExport importExport = new
-
 
574
        // ProjectImportExport(ctxt.getElement("AFFAIRE").getTable().getDBRoot());
-
 
575
        // importExport.export();
654
        addMenuImportExport(ctxt);
576
        // }
-
 
577
        // }, MainFrame.HELP_MENU);
-
 
578
        //
-
 
579
        // ctxt.addMenuItem(new AbstractAction("Import Affaire") {
-
 
580
        // @Override
-
 
581
        // public void actionPerformed(ActionEvent e) {
-
 
582
        // ProjectImportExport importExport = new
-
 
583
        // ProjectImportExport(ctxt.getElement("AFFAIRE").getTable().getDBRoot());
-
 
584
        // importExport.importProjects();
-
 
585
        // }
-
 
586
        // }, MainFrame.HELP_MENU);
-
 
587
        //
-
 
588
        // ctxt.addMenuItem(new AbstractAction("Fix ID_AFFAIRE") {
-
 
589
        // @Override
-
 
590
        // public void actionPerformed(ActionEvent e) {
-
 
591
        // {
-
 
592
        // SQLTable tableCmd = ctxt.getElement("COMMANDE_CLIENT").getTable();
-
 
593
        // SQLSelect selBadCmd = new SQLSelect();
-
 
594
        // selBadCmd.addSelect(tableCmd.getKey());
-
 
595
        // selBadCmd.addSelect(tableCmd.getField("ID_AFFAIRE"));
-
 
596
        // SQLSelectJoin joinTR = selBadCmd.addBackwardJoin("RIGHT", "tr",
-
 
597
        // tableCmd.getTable().getTable("TR_DEVIS").getField("ID_COMMANDE_CLIENT"), null);
-
 
598
        // SQLSelectJoin joinDevis = selBadCmd.addJoin("LEFT",
-
 
599
        // joinTR.getJoinedTable().getField("ID_DEVIS"));
-
 
600
        // SQLSelectJoin joinAff = selBadCmd.addBackwardJoin("LEFT", "aff",
-
 
601
        // tableCmd.getTable().getTable("AFFAIRE").getField("ID_DEVIS"), null);
-
 
602
        // selBadCmd.addSelect(joinAff.getJoinedTable().getKey());
-
 
603
        //
-
 
604
        // selBadCmd.setWhere(Where.isNull(tableCmd.getField("ID_AFFAIRE")));
-
 
605
        // System.err.println(selBadCmd.asString());
-
 
606
        //
-
 
607
        // List<Object[]> result = (List<Object[]>)
-
 
608
        // tableCmd.getDBSystemRoot().getDataSource().executeA(selBadCmd.asString());
-
 
609
        // try {
-
 
610
        // for (Object[] objects : result) {
-
 
611
        // if (objects[2] != null && objects[2].toString().trim().length() > 0)
-
 
612
        // tableCmd.getRow(((Number)
-
 
613
        // objects[0]).intValue()).createEmptyUpdateRow().put("ID_AFFAIRE", ((Number)
-
 
614
        // objects[2]).intValue()).commit();
-
 
615
        // }
-
 
616
        // } catch (SQLException e1) {
-
 
617
        // // TODO Auto-generated catch block
-
 
618
        // e1.printStackTrace();
-
 
619
        // }
-
 
620
        //
-
 
621
        // // List<SQLRow> list = SQLRowListRSH.execute(selBadVF);
-
 
622
        // }
-
 
623
        // {
-
 
624
        // SQLTable tableVF = ctxt.getElement("SAISIE_VENTE_FACTURE").getTable();
-
 
625
        // SQLSelect selBadVF = new SQLSelect();
-
 
626
        // selBadVF.addSelect(tableVF.getKey());
-
 
627
        // selBadVF.addSelect(tableVF.getField("ID_AFFAIRE"));
-
 
628
        // SQLSelectJoin joinTR = selBadVF.addBackwardJoin("RIGHT", "tr",
-
 
629
        // tableVF.getTable().getTable("TR_COMMANDE_CLIENT").getField("ID_SAISIE_VENTE_FACTURE"),
-
 
630
        // null);
-
 
631
        // SQLSelectJoin joinCmd = selBadVF.addJoin("LEFT",
-
 
632
        // joinTR.getJoinedTable().getField("ID_COMMANDE_CLIENT"));
-
 
633
        // selBadVF.addSelect(joinCmd.getJoinedTable().getField("ID_AFFAIRE"));
-
 
634
        //
-
 
635
        // selBadVF.setWhere(Where.isNull(tableVF.getField("ID_AFFAIRE")));
-
 
636
        // System.err.println(selBadVF.asString());
-
 
637
        // List<Object[]> result = (List<Object[]>)
-
 
638
        // tableVF.getDBSystemRoot().getDataSource().executeA(selBadVF.asString());
-
 
639
        // try {
-
 
640
        // for (Object[] objects : result) {
-
 
641
        // if (objects[2] != null && objects[2].toString().trim().length() > 0)
-
 
642
        // tableVF.getRow(((Number) objects[0]).intValue()).createEmptyUpdateRow().put("ID_AFFAIRE",
-
 
643
        // ((Number) objects[2]).intValue()).commit();
-
 
644
        // }
-
 
645
        // } catch (SQLException e1) {
-
 
646
        // // TODO Auto-generated catch block
-
 
647
        // e1.printStackTrace();
-
 
648
        // }
-
 
649
        // }
-
 
650
        // }
-
 
651
        // }, MainFrame.CREATE_MENU);
-
 
652
        //
-
 
653
        // ctxt.addMenuItem(new AbstractAction("Recalcul Total achat facture prorata") {
-
 
654
        // @Override
-
 
655
        // public void actionPerformed(ActionEvent e) {
-
 
656
        //
-
 
657
        // {
-
 
658
        // final SQLTable table = ctxt.getElement("SAISIE_VENTE_FACTURE").getTable();
-
 
659
        // SQLRowValues rowValsFact = new SQLRowValues(table);
-
 
660
        // rowValsFact.putNulls("SOLDE", "PARTIAL", "T_HA", "T_HT");
-
 
661
        //
-
 
662
        // final SQLTable tableCmd = ctxt.getElement("COMMANDE_CLIENT").getTable();
-
 
663
        // SQLRowValues rowValsCmd = new SQLRowValues(tableCmd);
-
 
664
        // rowValsCmd.putNulls("T_HT");
-
 
665
        //
-
 
666
        // final SQLTable tableCmdElt = ctxt.getElement("COMMANDE_CLIENT_ELEMENT").getTable();
-
 
667
        // SQLRowValues rowValsCmdElt = new SQLRowValues(tableCmdElt);
-
 
668
        // rowValsCmdElt.put("ID_COMMANDE_CLIENT", rowValsCmd);
-
 
669
        // rowValsCmdElt.putNulls("T_PA_HT", "NIVEAU");
-
 
670
        //
-
 
671
        // final SQLTable tableTR = table.getTable("TR_COMMANDE_CLIENT");
-
 
672
        // SQLRowValues rowValsTR = new SQLRowValues(tableTR);
-
 
673
        // rowValsTR.put("ID_SAISIE_VENTE_FACTURE", rowValsFact);
-
 
674
        // rowValsTR.put("ID_COMMANDE_CLIENT", rowValsCmd);
-
 
675
        //
-
 
676
        // SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsFact);
-
 
677
        // fetcher.appendSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
-
 
678
        // @Override
-
 
679
        // public SQLSelect transformChecked(SQLSelect input) {
-
 
680
        // Where w = new Where(table.getField("SOLDE"), "=", Boolean.TRUE);
-
 
681
        // w = w.or(new Where(table.getField("PARTIAL"), "=", Boolean.TRUE));
-
 
682
        // input.setWhere(w);
-
 
683
        // return input;
-
 
684
        // }
-
 
685
        // });
-
 
686
        //
-
 
687
        // List<SQLRowValues> rowVals = fetcher.fetch();
-
 
688
        //
-
 
689
        // for (SQLRowValues sqlRowValues : rowVals) {
-
 
690
        // // Recuperation de la commande
-
 
691
        // final Collection<SQLRowValues> referentRows = sqlRowValues.getReferentRows(tableTR);
-
 
692
        // if (!referentRows.isEmpty()) {
-
 
693
        // SQLRowAccessor rowTR = referentRows.iterator().next();
-
 
694
        // SQLRowAccessor rowCmd = rowTR.getForeign("ID_COMMANDE_CLIENT");
-
 
695
        // final long htCmd = rowCmd.getLong("T_HT");
-
 
696
        // if (htCmd != 0) {
-
 
697
        //
-
 
698
        // BigDecimal totalHA = BigDecimal.ZERO;
-
 
699
        // Collection<? extends SQLRowAccessor> elt = rowCmd.getReferentRows(tableCmdElt);
-
 
700
        // for (SQLRowAccessor sqlRowAccessor : elt) {
-
 
701
        // if (sqlRowAccessor.getInt("NIVEAU") == 1) {
-
 
702
        // totalHA = totalHA.add(sqlRowAccessor.getBigDecimal("T_PA_HT"));
-
 
703
        // }
-
 
704
        // }
-
 
705
        // BigDecimal prorata = new BigDecimal((double) sqlRowValues.getLong("T_HT") / (double)
-
 
706
        // htCmd);
-
 
707
        // try {
-
 
708
        // sqlRowValues.createEmptyUpdateRow().put("T_HA",
-
 
709
        // totalHA.multiply(prorata).movePointRight(2).setScale(0,
-
 
710
        // RoundingMode.HALF_UP).longValue()).update();
-
 
711
        // } catch (SQLException e1) {
-
 
712
        // e1.printStackTrace();
-
 
713
        // }
-
 
714
        // }
-
 
715
        // }
-
 
716
        // }
-
 
717
        // }
-
 
718
        // }
-
 
719
        // }, MainFrame.CREATE_MENU);
-
 
720
 
655
 
721
    }
656
    }
722
 
657
 
-
 
658
    private void addMenuImportExport(final MenuContext ctxt) {
-
 
659
        ////
-
 
660
 
-
 
661
        ctxt.addMenuItem(new AbstractAction("Export Affaire") {
723
    @Override
662
            @Override
-
 
663
            public void actionPerformed(ActionEvent e) {
-
 
664
                ProjectImportExport importExport = new ProjectImportExport(ctxt.getElement("AFFAIRE").getTable().getDBRoot());
-
 
665
                importExport.export();
-
 
666
            }
-
 
667
        }, MainFrame.HELP_MENU);
-
 
668
 
-
 
669
        ctxt.addMenuItem(new AbstractAction("Import Affaire") {
-
 
670
            @Override
-
 
671
            public void actionPerformed(ActionEvent e) {
-
 
672
                ProjectImportExport importExport = new ProjectImportExport(ctxt.getElement("AFFAIRE").getTable().getDBRoot());
-
 
673
                importExport.importProjects();
-
 
674
            }
-
 
675
        }, MainFrame.HELP_MENU);
-
 
676
 
-
 
677
        ctxt.addMenuItem(new AbstractAction("Fix ID_AFFAIRE") {
-
 
678
            @Override
-
 
679
            public void actionPerformed(ActionEvent e) {
-
 
680
                {
-
 
681
                    SQLTable tableCmd = ctxt.getElement("COMMANDE_CLIENT").getTable();
-
 
682
                    SQLSelect selBadCmd = new SQLSelect();
-
 
683
                    selBadCmd.addSelect(tableCmd.getKey());
-
 
684
                    selBadCmd.addSelect(tableCmd.getField("ID_AFFAIRE"));
-
 
685
                    SQLSelectJoin joinTR = selBadCmd.addBackwardJoin("RIGHT", "tr", tableCmd.getTable().getTable("TR_DEVIS").getField("ID_COMMANDE_CLIENT"), null);
-
 
686
                    SQLSelectJoin joinDevis = selBadCmd.addJoin("LEFT", joinTR.getJoinedTable().getField("ID_DEVIS"));
-
 
687
                    SQLSelectJoin joinAff = selBadCmd.addBackwardJoin("LEFT", "aff", tableCmd.getTable().getTable("AFFAIRE").getField("ID_DEVIS"), null);
-
 
688
                    selBadCmd.addSelect(joinAff.getJoinedTable().getKey());
-
 
689
 
-
 
690
                    selBadCmd.setWhere(Where.isNull(tableCmd.getField("ID_AFFAIRE")));
-
 
691
                    System.err.println(selBadCmd.asString());
-
 
692
 
-
 
693
                    List<Object[]> result = (List<Object[]>) tableCmd.getDBSystemRoot().getDataSource().executeA(selBadCmd.asString());
-
 
694
                    try {
-
 
695
                        for (Object[] objects : result) {
-
 
696
                            if (objects[2] != null && objects[2].toString().trim().length() > 0)
-
 
697
                                tableCmd.getRow(((Number) objects[0]).intValue()).createEmptyUpdateRow().put("ID_AFFAIRE", ((Number) objects[2]).intValue()).commit();
-
 
698
                        }
-
 
699
                    } catch (SQLException e1) {
-
 
700
                        // TODO Auto-generated catch block
-
 
701
                        e1.printStackTrace();
-
 
702
                    }
-
 
703
 
-
 
704
                    // List<SQLRow> list = SQLRowListRSH.execute(selBadVF);
-
 
705
                }
-
 
706
                {
-
 
707
                    SQLTable tableVF = ctxt.getElement("SAISIE_VENTE_FACTURE").getTable();
-
 
708
                    SQLSelect selBadVF = new SQLSelect();
-
 
709
                    selBadVF.addSelect(tableVF.getKey());
-
 
710
                    selBadVF.addSelect(tableVF.getField("ID_AFFAIRE"));
-
 
711
                    SQLSelectJoin joinTR = selBadVF.addBackwardJoin("RIGHT", "tr", tableVF.getTable().getTable("TR_COMMANDE_CLIENT").getField("ID_SAISIE_VENTE_FACTURE"), null);
-
 
712
                    SQLSelectJoin joinCmd = selBadVF.addJoin("LEFT", joinTR.getJoinedTable().getField("ID_COMMANDE_CLIENT"));
-
 
713
                    selBadVF.addSelect(joinCmd.getJoinedTable().getField("ID_AFFAIRE"));
-
 
714
 
-
 
715
                    selBadVF.setWhere(Where.isNull(tableVF.getField("ID_AFFAIRE")));
-
 
716
                    System.err.println(selBadVF.asString());
-
 
717
                    List<Object[]> result = (List<Object[]>) tableVF.getDBSystemRoot().getDataSource().executeA(selBadVF.asString());
724
    protected void start() {
718
                    try {
-
 
719
                        for (Object[] objects : result) {
-
 
720
                            if (objects[2] != null && objects[2].toString().trim().length() > 0)
-
 
721
                                tableVF.getRow(((Number) objects[0]).intValue()).createEmptyUpdateRow().put("ID_AFFAIRE", ((Number) objects[2]).intValue()).commit();
-
 
722
                        }
-
 
723
                    } catch (SQLException e1) {
-
 
724
                        // TODO Auto-generated catch block
-
 
725
                        e1.printStackTrace();
-
 
726
                    }
-
 
727
                }
-
 
728
            }
-
 
729
        }, MainFrame.CREATE_MENU);
-
 
730
 
-
 
731
        ctxt.addMenuItem(new AbstractAction("Recalcul Total achat facture prorata") {
-
 
732
            @Override
-
 
733
            public void actionPerformed(ActionEvent e) {
725
 
734
 
-
 
735
                {
-
 
736
                    final SQLTable table = ctxt.getElement("SAISIE_VENTE_FACTURE").getTable();
-
 
737
                    SQLRowValues rowValsFact = new SQLRowValues(table);
-
 
738
                    rowValsFact.putNulls("SOLDE", "PARTIAL", "T_HA", "T_HT");
-
 
739
 
-
 
740
                    final SQLTable tableCmd = ctxt.getElement("COMMANDE_CLIENT").getTable();
-
 
741
                    SQLRowValues rowValsCmd = new SQLRowValues(tableCmd);
-
 
742
                    rowValsCmd.putNulls("T_HT");
-
 
743
 
-
 
744
                    final SQLTable tableCmdElt = ctxt.getElement("COMMANDE_CLIENT_ELEMENT").getTable();
-
 
745
                    SQLRowValues rowValsCmdElt = new SQLRowValues(tableCmdElt);
-
 
746
                    rowValsCmdElt.put("ID_COMMANDE_CLIENT", rowValsCmd);
-
 
747
                    rowValsCmdElt.putNulls("T_PA_HT", "NIVEAU");
-
 
748
 
-
 
749
                    final SQLTable tableTR = table.getTable("TR_COMMANDE_CLIENT");
-
 
750
                    SQLRowValues rowValsTR = new SQLRowValues(tableTR);
-
 
751
                    rowValsTR.put("ID_SAISIE_VENTE_FACTURE", rowValsFact);
-
 
752
                    rowValsTR.put("ID_COMMANDE_CLIENT", rowValsCmd);
-
 
753
 
-
 
754
                    SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsFact);
-
 
755
                    fetcher.appendSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
-
 
756
                        @Override
-
 
757
                        public SQLSelect transformChecked(SQLSelect input) {
-
 
758
                            Where w = new Where(table.getField("SOLDE"), "=", Boolean.TRUE);
-
 
759
                            w = w.or(new Where(table.getField("PARTIAL"), "=", Boolean.TRUE));
-
 
760
                            input.setWhere(w);
-
 
761
                            return input;
-
 
762
                        }
-
 
763
                    });
-
 
764
 
-
 
765
                    List<SQLRowValues> rowVals = fetcher.fetch();
-
 
766
 
-
 
767
                    for (SQLRowValues sqlRowValues : rowVals) {
-
 
768
                        // Recuperation de la commande
-
 
769
                        final Collection<SQLRowValues> referentRows = sqlRowValues.getReferentRows(tableTR);
-
 
770
                        if (!referentRows.isEmpty()) {
-
 
771
                            SQLRowAccessor rowTR = referentRows.iterator().next();
-
 
772
                            SQLRowAccessor rowCmd = rowTR.getForeign("ID_COMMANDE_CLIENT");
-
 
773
                            final long htCmd = rowCmd.getLong("T_HT");
-
 
774
                            if (htCmd != 0) {
-
 
775
 
-
 
776
                                BigDecimal totalHA = BigDecimal.ZERO;
-
 
777
                                Collection<? extends SQLRowAccessor> elt = rowCmd.getReferentRows(tableCmdElt);
-
 
778
                                for (SQLRowAccessor sqlRowAccessor : elt) {
-
 
779
                                    if (sqlRowAccessor.getInt("NIVEAU") == 1) {
-
 
780
                                        totalHA = totalHA.add(sqlRowAccessor.getBigDecimal("T_PA_HT"));
-
 
781
                                    }
-
 
782
                                }
-
 
783
                                BigDecimal prorata = new BigDecimal((double) sqlRowValues.getLong("T_HT") / (double) htCmd);
-
 
784
                                try {
-
 
785
                                    sqlRowValues.createEmptyUpdateRow().put("T_HA", totalHA.multiply(prorata).movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue()).update();
-
 
786
                                } catch (SQLException e1) {
-
 
787
                                    e1.printStackTrace();
-
 
788
                                }
-
 
789
                            }
-
 
790
                        }
-
 
791
                    }
-
 
792
                }
-
 
793
            }
-
 
794
        }, MainFrame.CREATE_MENU);
-
 
795
    }
726
 
796
 
-
 
797
    @Override
-
 
798
    protected void start() {
727
 
799
 
728
        final JComponent quoteComponent = createQuotePanel();
800
        final JComponent quoteComponent = createQuotePanel();
729
        final JComponent orderComponent = createOrderPanel();
801
        final JComponent orderComponent = createOrderPanel();
730
        MainFrame.getInstance().getTabbedPane().addTab("Commandes en cours", orderComponent);
802
        MainFrame.getInstance().getTabbedPane().addTab("Commandes en cours", orderComponent);
731
        MainFrame.getInstance().getTabbedPane().addTab("Devis en attente", quoteComponent);
803
        MainFrame.getInstance().getTabbedPane().addTab("Devis en attente", quoteComponent);