OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.sales.quote.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.config.Gestion;
73 ilm 18
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
18 ilm 19
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
94 ilm 20
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable;
18 ilm 21
import org.openconcerto.erp.core.sales.invoice.component.SaisieVenteFactureSQLComponent;
65 ilm 22
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
18 ilm 23
import org.openconcerto.erp.core.sales.quote.component.DevisSQLComponent;
61 ilm 24
import org.openconcerto.erp.core.sales.quote.report.DevisXmlSheet;
83 ilm 25
import org.openconcerto.erp.core.sales.quote.ui.QuoteEditGroup;
93 ilm 26
import org.openconcerto.erp.core.sales.quote.ui.QuoteSQLComponent;
156 ilm 27
import org.openconcerto.erp.core.sales.shipment.component.BonDeLivraisonSQLComponent;
65 ilm 28
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
61 ilm 29
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
149 ilm 30
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 31
import org.openconcerto.sql.Configuration;
93 ilm 32
import org.openconcerto.sql.element.GlobalMapper;
18 ilm 33
import org.openconcerto.sql.element.SQLComponent;
34
import org.openconcerto.sql.element.SQLElement;
132 ilm 35
import org.openconcerto.sql.element.SQLElementLink.LinkType;
36
import org.openconcerto.sql.element.SQLElementLinksSetup;
90 ilm 37
import org.openconcerto.sql.model.FieldPath;
65 ilm 38
import org.openconcerto.sql.model.SQLField;
39
import org.openconcerto.sql.model.SQLInjector;
40
import org.openconcerto.sql.model.SQLRow;
80 ilm 41
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 42
import org.openconcerto.sql.model.SQLRowValues;
80 ilm 43
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
44
import org.openconcerto.sql.model.SQLSelect;
156 ilm 45
import org.openconcerto.sql.model.SQLSelectJoin;
65 ilm 46
import org.openconcerto.sql.model.SQLTable;
80 ilm 47
import org.openconcerto.sql.model.Where;
90 ilm 48
import org.openconcerto.sql.model.graph.Path;
149 ilm 49
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 50
import org.openconcerto.sql.request.ListSQLRequest;
174 ilm 51
import org.openconcerto.sql.ui.light.CustomRowEditor;
94 ilm 52
import org.openconcerto.sql.users.UserManager;
93 ilm 53
import org.openconcerto.sql.users.rights.UserRightsManager;
18 ilm 54
import org.openconcerto.sql.view.EditFrame;
65 ilm 55
import org.openconcerto.sql.view.EditPanel;
90 ilm 56
import org.openconcerto.sql.view.EditPanel.EditMode;
57
import org.openconcerto.sql.view.EditPanelListener;
58
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
65 ilm 59
import org.openconcerto.sql.view.list.IListe;
90 ilm 60
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
65 ilm 61
import org.openconcerto.sql.view.list.RowAction;
90 ilm 62
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
142 ilm 63
import org.openconcerto.sql.view.list.SQLTableModelSource;
83 ilm 64
import org.openconcerto.ui.light.ActivationOnSelectionControler;
65
import org.openconcerto.ui.light.ColumnSpec;
66
import org.openconcerto.ui.light.ColumnsSpec;
156 ilm 67
import org.openconcerto.ui.light.LightController;
149 ilm 68
import org.openconcerto.ui.light.LightUIButtonUnmanaged;
132 ilm 69
import org.openconcerto.ui.light.LightUIComboBox;
83 ilm 70
import org.openconcerto.ui.light.LightUIElement;
71
import org.openconcerto.ui.light.LightUILine;
94 ilm 72
import org.openconcerto.ui.light.LightUIPanel;
73
import org.openconcerto.ui.light.LightUITable;
83 ilm 74
import org.openconcerto.ui.light.LightUITextField;
75
import org.openconcerto.ui.light.Row;
94 ilm 76
import org.openconcerto.ui.light.RowSelectionSpec;
83 ilm 77
import org.openconcerto.ui.light.TableContent;
78
import org.openconcerto.ui.light.TableSpec;
90 ilm 79
import org.openconcerto.ui.table.TimestampTableCellRenderer;
80
import org.openconcerto.utils.CollectionUtils;
81
import org.openconcerto.utils.DecimalUtils;
80 ilm 82
import org.openconcerto.utils.ExceptionHandler;
90 ilm 83
import org.openconcerto.utils.FileUtils;
83 ilm 84
import org.openconcerto.utils.ListMap;
80 ilm 85
import org.openconcerto.utils.cc.ITransformer;
94 ilm 86
import org.openconcerto.utils.ui.StringWithId;
18 ilm 87
 
93 ilm 88
import java.awt.Desktop;
65 ilm 89
import java.awt.event.ActionEvent;
90 ilm 90
import java.io.File;
93 ilm 91
import java.io.IOException;
67 ilm 92
import java.math.BigDecimal;
90 ilm 93
import java.net.URI;
65 ilm 94
import java.sql.SQLException;
18 ilm 95
import java.util.ArrayList;
93 ilm 96
import java.util.Arrays;
90 ilm 97
import java.util.Calendar;
156 ilm 98
import java.util.Collections;
90 ilm 99
import java.util.Date;
18 ilm 100
import java.util.HashSet;
101
import java.util.List;
102
import java.util.Set;
103
 
65 ilm 104
import javax.swing.AbstractAction;
18 ilm 105
import javax.swing.ImageIcon;
106
import javax.swing.JFrame;
80 ilm 107
import javax.swing.JOptionPane;
108
import javax.swing.SwingWorker;
18 ilm 109
 
132 ilm 110
import org.jdom2.Document;
111
import org.jdom2.Element;
112
import org.jdom2.input.DOMBuilder;
94 ilm 113
 
18 ilm 114
public class DevisSQLElement extends ComptaSQLConfElement {
115
 
25 ilm 116
    public static final String TABLENAME = "DEVIS";
93 ilm 117
    public static final String FACTURE_TAG_RIGHT = "TAG_FACTURE_DEVIS";
25 ilm 118
 
90 ilm 119
    public static enum Month {
120
 
132 ilm 121
        JANVIER("01", "Janvier"), FEVRIER("02", "Février"), MARS("03", "Mars"), AVRIL("04", "Avril"), MAI("05", "Mai"), JUIN("06", "Juin"), JUILLET("07", "Juillet"), AOUT("08",
122
                "Août"), SEPTEMBRE("09", "Septembre"), OCTOBRE("10", "Octobre"), NOVEMBRE("11", "Novembre"), DECEMBRE("12", "Décembre");
90 ilm 123
 
124
        private String number;
125
        private String name;
126
 
127
        Month(String number, String name) {
128
            this.number = number;
129
            this.name = name;
130
        }
131
 
132
        public String getName() {
133
            return this.name;
134
        }
135
 
136
        public String getNumber() {
137
            return this.number;
138
        }
139
 
140
        public String getPath() {
141
            return this.getNumber() + "-" + this.getName();
142
        }
156 ilm 143
    }
90 ilm 144
 
18 ilm 145
    public DevisSQLElement() {
156 ilm 146
        super(TABLENAME);
61 ilm 147
 
65 ilm 148
        getRowActions().addAll(getDevisRowActions());
93 ilm 149
        final QuoteEditGroup group = new QuoteEditGroup();
150
        GlobalMapper.getInstance().map(QuoteSQLComponent.ID, group);
151
        setDefaultGroup(group);
152
 
18 ilm 153
    }
154
 
149 ilm 155
    @Override
156
    public Set<String> getReadOnlyFields() {
156 ilm 157
        Set<String> s = new HashSet<>();
149 ilm 158
        s.add("T_ACOMPTE");
159
        return s;
160
    }
90 ilm 161
 
65 ilm 162
    private List<RowAction> getDevisRowActions() {
163
 
156 ilm 164
        List<RowAction> rowsActions = new ArrayList<>();
65 ilm 165
 
166
        // Transfert vers facture
167
        RowAction factureAction = getDevis2FactureAction();
168
 
169
        rowsActions.add(factureAction);
170
 
149 ilm 171
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
172
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
173
            PredicateRowAction actionClient = new PredicateRowAction(new AbstractAction("Saisir un acompte") {
174
                EditFrame edit;
175
 
176
                public void actionPerformed(ActionEvent e) {
177
                    final SQLElement eltEncaisser = Configuration.getInstance().getDirectory()
178
                            .getElement(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ENCAISSER_MONTANT"));
179
 
180
                    if (this.edit == null) {
181
                        this.edit = new EditFrame(eltEncaisser, EditMode.CREATION);
182
                    }
183
                    final SQLRowAccessor selRow = IListe.get(e).getSelectedRow();
184
                    SQLRowValues rowVals = new SQLRowValues(eltEncaisser.getTable());
185
                    rowVals.put("ACOMPTE", true);
186
                    rowVals.put("NOM", "Acompte devis " + selRow.getString("NUMERO"));
187
                    rowVals.put("ID_CLIENT", selRow.getForeignID("ID_CLIENT"));
188
                    rowVals.put("ID_DEVIS", selRow.getID());
189
                    SQLRowValues rowValsElt = new SQLRowValues(eltEncaisser.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
190
                    rowValsElt.put("MONTANT_A_REGLER", selRow.getLong("T_TTC"));
191
                    rowValsElt.put("DATE", selRow.getObject("DATE"));
192
                    rowValsElt.put("ID_ENCAISSER_MONTANT", rowVals);
193
                    this.edit.getSQLComponent().select(rowVals);
194
                    this.edit.setVisible(true);
195
                    this.edit.addEditPanelListener(new EditPanelListener() {
196
 
197
                        @Override
198
                        public void modified() {
199
                        }
200
 
201
                        @Override
202
                        public void inserted(int id) {
203
                            // Put id devis and refresh devis.t_acompte
204
                            SQLRow rowE = eltEncaisser.getTable().getRow(id);
205
                            String up = "UPDATE " + selRow.getTable().getSQLName().quote() + " set \"T_ACOMPTE\"=(SELECT COALESCE(SUM(\"MONTANT\"),0) from " + rowE.getTable().getSQLName().quote()
206
                                    + " where \"ARCHIVE\"=0 and \"ID_DEVIS\"=" + selRow.getID() + ") where \"ID_DEVIS\"=" + selRow.getID();
207
                            eltEncaisser.getTable().getDBSystemRoot().getDataSource().execute(up);
208
                        }
209
 
210
                        @Override
211
                        public void deleted() {
212
                        }
213
 
214
                        @Override
215
                        public void cancelled() {
216
                        }
217
                    });
218
                }
219
            }, false);
220
            actionClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
221
            rowsActions.add(actionClient);
222
        }
223
 
90 ilm 224
        PredicateRowAction actionClient = new PredicateRowAction(new AbstractAction("Détails client") {
225
            EditFrame edit;
226
            private SQLElement eltClient = Configuration.getInstance().getDirectory().getElement(((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("CLIENT"));
227
 
228
            public void actionPerformed(ActionEvent e) {
94 ilm 229
                if (this.edit == null) {
230
                    this.edit = new EditFrame(this.eltClient, EditMode.MODIFICATION);
90 ilm 231
                }
94 ilm 232
                this.edit.selectionId(IListe.get(e).getSelectedRow().getForeignID("ID_CLIENT"));
233
                this.edit.setVisible(true);
90 ilm 234
            }
235
        }, false);
236
        actionClient.setPredicate(IListeEvent.getSingleSelectionPredicate());
237
        rowsActions.add(actionClient);
238
 
239
 
65 ilm 240
        // Voir le document
241
        RowAction actionTransfertCmd = getDevis2CmdFournAction();
242
        rowsActions.add(actionTransfertCmd);
243
 
244
        // Transfert vers commande
245
        RowAction commandeAction = getDevis2CmdCliAction();
246
        rowsActions.add(commandeAction);
247
 
156 ilm 248
        RowAction blAction = getDevis2BlAction();
249
        rowsActions.add(blAction);
250
 
65 ilm 251
        RowAction accepteEtCmdAction = getAcceptAndCmdClientAction();
252
        rowsActions.add(accepteEtCmdAction);
253
 
254
        // Marqué accepté
255
        RowAction accepteAction = getAcceptAction();
256
 
257
        rowsActions.add(accepteAction);
258
 
259
        // Marqué accepté
260
        RowAction refuseAction = getRefuseAction();
261
 
262
        rowsActions.add(refuseAction);
263
 
264
        // // Dupliquer
265
        RowAction cloneAction = getCloneAction();
266
 
267
        rowsActions.add(cloneAction);
268
 
182 ilm 269
        MouseSheetXmlListeListener mouseSheetXmlListeListener = new MouseSheetXmlListeListener(this, DevisXmlSheet.class);
65 ilm 270
        mouseSheetXmlListeListener.setGenerateHeader(true);
271
        mouseSheetXmlListeListener.setShowHeader(true);
272
 
273
        rowsActions.addAll(mouseSheetXmlListeListener.getRowActions());
274
 
275
        return rowsActions;
276
    }
277
 
156 ilm 278
    public RowAction getDevis2BlAction() {
279
        return new RowAction(new AbstractAction() {
280
            public void actionPerformed(ActionEvent e) {
281
 
282
                final List<SQLRowValues> copySelectedRows = IListe.get(e).getSelectedRows();
283
                transfertDevis(copySelectedRows, "BON_DE_LIVRAISON");
284
            }
285
 
286
        }, true, "sales.quote.create.customer.delivery") {
287
            @Override
288
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
289
                if (selection != null && selection.size() == 1) {
290
                    if (selection.get(0).getForeignID("ID_ETAT_DEVIS") == EtatDevisSQLElement.ACCEPTE) {
291
                        return true;
292
                    }
293
                }
294
                return false;
295
            }
296
        };
297
    }
298
 
93 ilm 299
    public static void davBrowse(String s) throws Exception {
300
        final boolean windows = System.getProperty("os.name").startsWith("Windows");
301
        if (windows) {
302
            Desktop.getDesktop().browse(new URI(s));
303
        } else {
304
            String[] cmdarray = new String[] { "xdg-open", s };
305
            final int res = Runtime.getRuntime().exec(cmdarray).waitFor();
306
            if (res != 0)
307
                throw new IOException("error (" + res + ") executing " + Arrays.asList(cmdarray));
308
        }
309
    }
310
 
65 ilm 311
    public RowAction getCloneAction() {
67 ilm 312
        return new RowAction(new AbstractAction() {
65 ilm 313
 
314
            public void actionPerformed(ActionEvent e) {
80 ilm 315
                SQLRowAccessor selectedRow = IListe.get(e).getSelectedRow();
65 ilm 316
 
93 ilm 317
                SQLElement eltFact = Configuration.getInstance().getDirectory().getElement("DEVIS");
318
                EditFrame editFrame = new EditFrame(eltFact, EditPanel.CREATION);
65 ilm 319
 
93 ilm 320
                ((DevisSQLComponent) editFrame.getSQLComponent()).loadDevisExistant(selectedRow.getID());
321
                editFrame.setVisible(true);
65 ilm 322
            }
67 ilm 323
        }, true, "sales.quote.clone") {
156 ilm 324
            @Override
93 ilm 325
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
65 ilm 326
                return (selection != null && selection.size() == 1);
156 ilm 327
            }
65 ilm 328
        };
329
    }
330
 
331
    public RowAction getRefuseAction() {
67 ilm 332
        return new RowAction(new AbstractAction() {
65 ilm 333
            public void actionPerformed(ActionEvent e) {
80 ilm 334
                SQLRowValues rowVals = IListe.get(e).getSelectedRow().asRow().createEmptyUpdateRow();
65 ilm 335
                rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.REFUSE);
336
                try {
337
                    rowVals.update();
338
                } catch (SQLException e1) {
339
                    // TODO Auto-generated catch block
340
                    e1.printStackTrace();
341
                }
342
            }
67 ilm 343
        }, false, "sales.quote.refuse") {
156 ilm 344
            @Override
93 ilm 345
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
65 ilm 346
                if (selection != null && selection.size() == 1) {
93 ilm 347
                    if (selection.get(0).getForeignID("ID_ETAT_DEVIS") == EtatDevisSQLElement.EN_ATTENTE) {
65 ilm 348
                        return true;
349
                    }
350
                }
351
                return false;
156 ilm 352
            }
65 ilm 353
        };
354
    }
355
 
356
    public RowAction getAcceptAction() {
67 ilm 357
        return new RowAction(new AbstractAction() {
65 ilm 358
            public void actionPerformed(ActionEvent e) {
80 ilm 359
                SQLRow selectedRow = IListe.get(e).getSelectedRow().asRow();
65 ilm 360
                SQLRowValues rowVals = selectedRow.createEmptyUpdateRow();
361
                rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.ACCEPTE);
362
                try {
363
                    rowVals.update();
364
                } catch (SQLException e1) {
365
                    // TODO Auto-generated catch block
366
                    e1.printStackTrace();
367
                }
368
            }
67 ilm 369
        }, false, "sales.quote.accept") {
156 ilm 370
            @Override
93 ilm 371
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
65 ilm 372
                if (selection != null && selection.size() == 1) {
93 ilm 373
                    final int int1 = selection.get(0).getForeignID("ID_ETAT_DEVIS");
90 ilm 374
                    if (int1 != EtatDevisSQLElement.REFUSE && int1 != EtatDevisSQLElement.ACCEPTE) {
65 ilm 375
                        return true;
376
                    }
377
                }
378
                return false;
156 ilm 379
            }
65 ilm 380
        };
381
    }
382
 
383
    public RowAction getDevis2FactureAction() {
67 ilm 384
        return new RowAction(new AbstractAction() {
65 ilm 385
            public void actionPerformed(ActionEvent e) {
93 ilm 386
                TransfertBaseSQLComponent.openTransfertFrame(IListe.get(e).getSelectedRows(), "SAISIE_VENTE_FACTURE");
90 ilm 387
 
65 ilm 388
            }
67 ilm 389
        }, true, "sales.quote.create.invoice") {
156 ilm 390
            @Override
93 ilm 391
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
90 ilm 392
                boolean b = selection.size() > 0;
393
                for (SQLRowAccessor sqlRowAccessor : selection) {
93 ilm 394
                    b &= sqlRowAccessor.getForeignID("ID_ETAT_DEVIS") == EtatDevisSQLElement.ACCEPTE;
65 ilm 395
                }
90 ilm 396
 
397
                return b;
156 ilm 398
            }
65 ilm 399
        };
400
    }
401
 
402
    public RowAction getDevis2CmdFournAction() {
67 ilm 403
        return new RowAction(new AbstractAction() {
65 ilm 404
            public void actionPerformed(ActionEvent e) {
80 ilm 405
                final SQLRow selectedRow = IListe.get(e).fetchSelectedRow();
73 ilm 406
                ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
407
 
408
                    @Override
409
                    public void run() {
410
                        transfertCommande(selectedRow);
411
 
412
                    }
413
                });
414
 
65 ilm 415
            }
67 ilm 416
        }, false, "sales.quote.create.supplier.order") {
156 ilm 417
            @Override
93 ilm 418
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
65 ilm 419
                if (selection != null && selection.size() == 1) {
93 ilm 420
                    if (selection.get(0).getForeignID("ID_ETAT_DEVIS") == EtatDevisSQLElement.ACCEPTE) {
65 ilm 421
                        return true;
422
                    }
423
                }
424
                return false;
156 ilm 425
            }
65 ilm 426
        };
427
    }
428
 
429
    public RowAction getDevis2CmdCliAction() {
67 ilm 430
        return new RowAction(new AbstractAction() {
65 ilm 431
            public void actionPerformed(ActionEvent e) {
80 ilm 432
 
93 ilm 433
                final List<SQLRowValues> copySelectedRows = IListe.get(e).getSelectedRows();
80 ilm 434
                transfertCommandeClient(copySelectedRows);
65 ilm 435
            }
80 ilm 436
 
67 ilm 437
        }, true, "sales.quote.create.customer.order") {
156 ilm 438
            @Override
93 ilm 439
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
65 ilm 440
                if (selection != null && selection.size() == 1) {
93 ilm 441
                    if (selection.get(0).getForeignID("ID_ETAT_DEVIS") == EtatDevisSQLElement.ACCEPTE) {
65 ilm 442
                        return true;
443
                    }
444
                }
445
                return false;
156 ilm 446
            }
65 ilm 447
        };
448
    }
449
 
450
    public RowAction getAcceptAndCmdClientAction() {
67 ilm 451
        return new RowAction(new AbstractAction() {
65 ilm 452
            public void actionPerformed(ActionEvent e) {
80 ilm 453
                SQLRow selectedRow = IListe.get(e).fetchSelectedRow();
65 ilm 454
                SQLRowValues rowVals = selectedRow.createEmptyUpdateRow();
455
                rowVals.put("ID_ETAT_DEVIS", EtatDevisSQLElement.ACCEPTE);
456
                try {
457
                    rowVals.update();
458
                } catch (SQLException e1) {
80 ilm 459
                    ExceptionHandler.handle("Erreur la de la mise à jour de l'état du devis!", e1);
460
 
65 ilm 461
                }
93 ilm 462
                transfertCommandeClient(IListe.get(e).getSelectedRows());
65 ilm 463
            }
67 ilm 464
        }, false, "sales.quote.accept.create.customer.order") {
156 ilm 465
            @Override
93 ilm 466
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
65 ilm 467
                if (selection != null && selection.size() == 1) {
93 ilm 468
                    if (selection.get(0).getForeignID("ID_ETAT_DEVIS") == EtatDevisSQLElement.EN_ATTENTE) {
65 ilm 469
                        return true;
470
                    }
471
                }
472
                return false;
156 ilm 473
            }
65 ilm 474
        };
475
    }
476
 
156 ilm 477
    public void transfertDevis(final List<SQLRowValues> copySelectedRows, final String destTable) {
80 ilm 478
        SwingWorker<Boolean, Object> worker = new SwingWorker<Boolean, Object>() {
479
            @Override
480
            protected Boolean doInBackground() throws Exception {
481
 
482
                final SQLTable tableTransfert = getTable().getTable("TR_DEVIS");
483
                SQLRowValues rowVals = new SQLRowValues(tableTransfert);
484
                rowVals.put("ID_DEVIS", new SQLRowValues(getTable()).put("NUMERO", null));
156 ilm 485
                rowVals.put("ID_" + destTable, null);
80 ilm 486
                rowVals.put("ID", null);
487
 
156 ilm 488
                final List<Number> lID = new ArrayList<>();
80 ilm 489
                for (SQLRowValues sqlRowValues : copySelectedRows) {
490
                    lID.add(sqlRowValues.getID());
491
                }
492
 
493
                SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals);
494
                fetch.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
495
 
496
                    @Override
497
                    public SQLSelect transformChecked(SQLSelect input) {
498
                        Where w = new Where(tableTransfert.getField("ID_DEVIS"), lID);
156 ilm 499
                        w = w.and(new Where(tableTransfert.getField("ID_" + destTable), "IS NOT", (Object) null));
80 ilm 500
                        input.setWhere(w);
501
                        return input;
502
                    }
503
                });
504
 
505
                List<SQLRowValues> rows = fetch.fetch();
506
                if (rows != null && rows.size() > 0) {
507
                    String numero = "";
508
 
509
                    for (SQLRowValues sqlRow : rows) {
510
                        numero += sqlRow.getForeign("ID_DEVIS").getString("NUMERO") + " ,";
511
                    }
512
 
513
                    numero = numero.substring(0, numero.length() - 2);
514
                    String label = "Attention ";
515
                    if (rows.size() > 1) {
156 ilm 516
                        label += " les devis " + numero + " ont déjà été transféré!";
80 ilm 517
                    } else {
156 ilm 518
                        label += " le devis " + numero + " a déjà été transféré!";
80 ilm 519
                    }
520
                    label += "\n Voulez vous continuer?";
521
 
156 ilm 522
                    int ans = JOptionPane.showConfirmDialog(null, label, "Transfert devis", JOptionPane.YES_NO_OPTION);
80 ilm 523
                    if (ans == JOptionPane.NO_OPTION) {
524
                        return Boolean.FALSE;
525
                    }
526
 
527
                }
528
                return Boolean.TRUE;
529
 
530
            }
531
 
532
            @Override
533
            protected void done() {
534
 
535
                try {
536
                    Boolean b = get();
537
                    if (b != null && b) {
156 ilm 538
                        EditFrame frame = TransfertBaseSQLComponent.openTransfertFrame(copySelectedRows, destTable);
539
                        if (destTable.equalsIgnoreCase("BON_DE_LIVRAISON")) {
540
                            BonDeLivraisonSQLComponent comp = (BonDeLivraisonSQLComponent) frame.getSQLComponent();
541
                            final SQLTable tableElt = comp.getElement().getTable().getTable("BON_DE_LIVRAISON_ELEMENT");
542
                            SQLRowValues rowVals = new SQLRowValues(tableElt);
543
                            rowVals.put("QTE_UNITAIRE", null);
544
                            rowVals.put("QTE", null);
545
                            rowVals.put("QTE_LIVREE", null);
546
                            rowVals.put("ID_ARTICLE", null);
547
                            rowVals.put("PV_HT", null);
548
                            rowVals.put("ID_DEVIS_ELEMENT", null);
549
 
550
                            SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
551
                            fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
552
 
553
                                @Override
554
                                public SQLSelect transformChecked(SQLSelect input) {
555
                                    List<Integer> ids = new ArrayList<>(copySelectedRows.size());
556
                                    for (SQLRowValues sqlRowValues : copySelectedRows) {
557
                                        ids.add(sqlRowValues.getID());
558
                                    }
559
                                    SQLSelectJoin joinBR = input.addJoin("RIGHT", tableElt.getTable("BON_DE_LIVRAISON_ELEMENT").getField("ID_BON_DE_LIVRAISON"));
560
                                    SQLSelectJoin joinTR = input.addBackwardJoin("RIGHT", tableElt.getTable("TR_DEVIS").getField("ID_BON_DE_LIVRAISON"), joinBR.getJoinedTable().getAlias());
561
                                    joinTR.setWhere(new Where(joinTR.getJoinedTable().getField("ID_DEVIS"), ids));
562
                                    System.err.println(input.asString());
563
                                    return input;
564
                                }
565
                            });
566
                            comp.loadQuantity(fetcher.fetch(), "DEVIS_ELEMENT");
567
                        }
80 ilm 568
                    }
569
                } catch (Exception e) {
156 ilm 570
                    ExceptionHandler.handle("Erreur lors du transfert des devis!", e);
80 ilm 571
                }
572
                super.done();
573
            }
574
        };
575
        worker.execute();
576
    }
577
 
156 ilm 578
    public void transfertCommandeClient(final List<SQLRowValues> copySelectedRows) {
579
        transfertDevis(copySelectedRows, "COMMANDE_CLIENT");
580
    }
581
 
582
    @Override
18 ilm 583
    protected List<String> getComboFields() {
156 ilm 584
        List<String> l = new ArrayList<>(1);
18 ilm 585
        l.add("NUMERO");
586
        return l;
587
    }
588
 
83 ilm 589
    private void transfertCommande(final SQLRow row) {
590
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
591
 
592
            @Override
593
            public void run() {
594
                DevisItemSQLElement elt = (DevisItemSQLElement) Configuration.getInstance().getDirectory().getElement("DEVIS_ELEMENT");
595
                SQLTable tableCmdElt = Configuration.getInstance().getDirectory().getElement("COMMANDE_ELEMENT").getTable();
596
                SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement("ARTICLE");
597
                List<SQLRow> rows = row.getReferentRows(elt.getTable());
156 ilm 598
                final ListMap<SQLRow, SQLRowValues> map = new ListMap<>();
83 ilm 599
                SQLRow rowDeviseF = null;
600
                for (SQLRow sqlRow : rows) {
601
                    // on récupére l'article qui lui correspond
602
                    SQLRowValues rowArticle = new SQLRowValues(eltArticle.getTable());
603
                    for (SQLField field : eltArticle.getTable().getFields()) {
604
                        if (sqlRow.getTable().getFieldsName().contains(field.getName())) {
605
                            rowArticle.put(field.getName(), sqlRow.getObject(field.getName()));
606
                        }
607
                    }
608
 
609
                    // rowArticle.loadAllSafe(rowEltFact);
610
                    int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
611
                    SQLRow rowArticleFind = eltArticle.getTable().getRow(idArticle);
132 ilm 612
                    if (rowArticleFind != null) {
613
                        SQLInjector inj = SQLInjector.getInjector(rowArticle.getTable(), tableCmdElt);
614
                        SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(rowArticleFind));
615
                        rowValsElt.put("ID_STYLE", sqlRow.getObject("ID_STYLE"));
616
                        rowValsElt.put("QTE", sqlRow.getObject("QTE"));
617
                        rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * rowValsElt.getInt("QTE"));
618
                        rowValsElt.put("T_PA_HT", ((BigDecimal) rowValsElt.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE"), DecimalUtils.HIGH_PRECISION)));
156 ilm 619
                        rowValsElt.put("T_PA_TTC", ((BigDecimal) rowValsElt.getObject("T_PA_HT")).multiply(BigDecimal.valueOf(rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0),
620
                                DecimalUtils.HIGH_PRECISION));
83 ilm 621
 
132 ilm 622
                        // gestion de la devise
623
                        rowDeviseF = sqlRow.getForeignRow("ID_DEVISE");
624
                        SQLRow rowDeviseHA = rowArticleFind.getForeignRow("ID_DEVISE_HA");
625
                        BigDecimal qte = new BigDecimal(rowValsElt.getInt("QTE"));
626
                        if (rowDeviseF != null && !rowDeviseF.isUndefined()) {
627
                            if (rowDeviseF.getID() == rowDeviseHA.getID()) {
628
                                rowValsElt.put("PA_DEVISE", rowArticleFind.getObject("PA_DEVISE"));
629
                                rowValsElt.put("PA_DEVISE_T", ((BigDecimal) rowArticleFind.getObject("PA_DEVISE")).multiply(qte, DecimalUtils.HIGH_PRECISION));
630
                                rowValsElt.put("ID_DEVISE", rowDeviseF.getID());
631
                            } else {
632
                                BigDecimal taux = (BigDecimal) rowDeviseF.getObject("TAUX");
633
                                rowValsElt.put("PA_DEVISE", taux.multiply((BigDecimal) rowValsElt.getObject("PA_HT")));
634
                                rowValsElt.put("PA_DEVISE_T", ((BigDecimal) rowValsElt.getObject("PA_DEVISE")).multiply(qte, DecimalUtils.HIGH_PRECISION));
635
                                rowValsElt.put("ID_DEVISE", rowDeviseF.getID());
636
                            }
637
                        }
83 ilm 638
 
132 ilm 639
                        map.add(rowArticleFind.getForeignRow("ID_FOURNISSEUR"), rowValsElt);
83 ilm 640
                    }
65 ilm 641
                }
83 ilm 642
                MouvementStockSQLElement.createCommandeF(map, rowDeviseF);
65 ilm 643
            }
83 ilm 644
        });
65 ilm 645
 
646
    }
647
 
18 ilm 648
    protected List<String> getListFields() {
156 ilm 649
        List<String> l = new ArrayList<>();
18 ilm 650
        l.add("NUMERO");
651
        l.add("DATE");
652
        l.add("ID_CLIENT");
653
        l.add("OBJET");
41 ilm 654
        l.add("ID_COMMERCIAL");
94 ilm 655
        if (UserRightsManager.getCurrentUserRights().haveRight(AbstractVenteArticleItemTable.SHOW_PRIX_ACHAT_CODE)) {
656
            l.add("T_HA");
657
        }
18 ilm 658
        l.add("T_HT");
659
        l.add("T_TTC");
182 ilm 660
        if (getTable().contains("TRANSFORMATION")) {
661
            l.add("TRANSFORMATION");
662
        }
149 ilm 663
        SQLPreferences prefs = SQLPreferences.getMemCached(getTable().getDBRoot());
664
        if (prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.ACOMPTE_DEVIS, false)) {
665
            l.add("T_ACOMPTE");
666
        }
18 ilm 667
        l.add("INFOS");
93 ilm 668
        if (getTable().contains("DUNNING_DATE")) {
669
            l.add("DUNNING_DATE");
670
        }
18 ilm 671
        return l;
672
    }
673
 
94 ilm 674
    @Override
142 ilm 675
    protected synchronized void _initTableSource(final SQLTableModelSource table) {
676
        super._initTableSource(table);
94 ilm 677
 
151 ilm 678
            final BaseSQLTableModelColumn colAdrLiv = new BaseSQLTableModelColumn("Adresse de livraison", String.class) {
94 ilm 679
 
151 ilm 680
                @Override
681
                protected Object show_(SQLRowAccessor r) {
94 ilm 682
 
177 ilm 683
                    SQLRowAccessor rowAd = null;
151 ilm 684
                    if (!r.isForeignEmpty("ID_ADRESSE_LIVRAISON")) {
685
                        rowAd = r.getForeign("ID_ADRESSE_LIVRAISON");
177 ilm 686
                    } else if (!r.isForeignEmpty("ID_CLIENT")) {
687
                        if (r.getForeign("ID_CLIENT").getObject("ID_ADRESSE_L") != null && !r.getForeign("ID_CLIENT").isForeignEmpty("ID_ADRESSE_L")) {
688
 
689
                            rowAd = r.getForeign("ID_CLIENT").getForeign("ID_ADRESSE_L");
690
                        } else {
691
                            rowAd = r.getForeign("ID_CLIENT").getForeign("ID_ADRESSE");
692
                        }
151 ilm 693
                    }
94 ilm 694
 
177 ilm 695
                    String lib = rowAd == null ? "" : rowAd.getString("LIBELLE") + " " + rowAd.getString("VILLE");
94 ilm 696
 
151 ilm 697
                    return lib;
698
                }
94 ilm 699
 
151 ilm 700
                @Override
701
                public Set<FieldPath> getPaths() {
702
                    SQLTable devisTable = getTable();
703
                    Path p = new Path(devisTable);
704
                    p = p.add(devisTable.getField("ID_CLIENT"));
705
                    p = p.add(p.getLast().getField("ID_ADRESSE_L"));
94 ilm 706
 
151 ilm 707
                    Path p2 = new Path(devisTable);
708
                    p2 = p2.add(devisTable.getField("ID_CLIENT"));
709
                    p2 = p2.add(p2.getLast().getField("ID_ADRESSE"));
94 ilm 710
 
151 ilm 711
                    Path p3 = new Path(devisTable);
712
                    p3 = p3.add(devisTable.getField("ID_ADRESSE_LIVRAISON"));
94 ilm 713
 
151 ilm 714
                    return CollectionUtils.createSet(new FieldPath(p, "LIBELLE"), new FieldPath(p, "VILLE"), new FieldPath(p2, "LIBELLE"), new FieldPath(p2, "VILLE"), new FieldPath(p3, "LIBELLE"),
715
                            new FieldPath(p3, "VILLE"));
716
                }
717
            };
718
            table.getColumns().add(3, colAdrLiv);
94 ilm 719
    }
720
 
18 ilm 721
    @Override
132 ilm 722
    public ListMap<String, String> getShowAs() {
723
        return ListMap.singleton(null, "NUMERO");
28 ilm 724
    }
725
 
726
    @Override
132 ilm 727
    protected void _initListRequest(ListSQLRequest req) {
728
        super._initListRequest(req);
729
        req.addToGraphToFetch("ID_ETAT_DEVIS");
18 ilm 730
    }
731
 
732
    @Override
132 ilm 733
    protected void setupLinks(SQLElementLinksSetup links) {
734
        super.setupLinks(links);
735
        if (getTable().contains("ID_ADRESSE")) {
736
            links.get("ID_ADRESSE").setType(LinkType.ASSOCIATION);
737
        }
738
        if (getTable().contains("ID_ADRESSE_LIVRAISON")) {
739
            links.get("ID_ADRESSE_LIVRAISON").setType(LinkType.ASSOCIATION);
740
        }
18 ilm 741
    }
742
 
743
    /*
744
     * (non-Javadoc)
745
     *
746
     * @see org.openconcerto.devis.SQLElement#getComponent()
747
     */
748
    public SQLComponent createComponent() {
93 ilm 749
        return new DevisSQLComponent(this);
18 ilm 750
    }
751
 
752
    /**
753
     * Transfert d'un devis en facture
754
     *
755
     * @param devisID
756
     */
90 ilm 757
    public void transfertFacture(final int devisID) {
18 ilm 758
 
759
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
760
        EditFrame editFactureFrame = new EditFrame(elt);
761
        editFactureFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
762
 
763
        SaisieVenteFactureSQLComponent comp = (SaisieVenteFactureSQLComponent) editFactureFrame.getSQLComponent();
764
 
765
        comp.setDefaults();
766
        comp.loadDevis(devisID);
767
 
90 ilm 768
 
18 ilm 769
        editFactureFrame.pack();
770
        editFactureFrame.setState(JFrame.NORMAL);
771
        editFactureFrame.setVisible(true);
772
    }
773
 
83 ilm 774
    @Override
174 ilm 775
    public List<CustomRowEditor> getCustomRowEditors(Configuration configuration, String sessionToken) {
776
        List<CustomRowEditor> map = super.getCustomRowEditors(configuration, sessionToken);
777
        map.add(new CustomRowEditor("sales.quote.items.list") {
83 ilm 778
 
779
            @Override
174 ilm 780
            public LightUIElement createUIElement() {
132 ilm 781
                final ColumnSpec c1 = new ColumnSpec("sales.quote.item.style", StringWithId.class, "Style", new StringWithId(2, "Normal"), true, new LightUIComboBox("sales.quote.item.style"));
94 ilm 782
                final ColumnSpec c2 = new ColumnSpec("sales.quote.item.code", String.class, "Code", "", true, new LightUITextField("sales.quote.item.code"));
783
                final ColumnSpec c3 = new ColumnSpec("sales.quote.item.label", String.class, "Nom", "", true, new LightUITextField("sales.quote.item.name"));
784
                final ColumnSpec c4 = new ColumnSpec("sales.quote.item.description", String.class, "Descriptif", "", true, new LightUITextField("sales.quote.item.description"));
132 ilm 785
                final ColumnSpec c5 = new ColumnSpec("sales.quote.item.purchase.unit.price", BigDecimal.class, "P.U. Achat HT", new BigDecimal(0), true,
786
                        new LightUITextField("sales.quote.item.purchase.unit.price"));
787
                final ColumnSpec c6 = new ColumnSpec("sales.quote.item.sales.unit.price", BigDecimal.class, "P.U. Vente HT", new BigDecimal(0), true,
788
                        new LightUITextField("sales.quote.item.sales.unit.price"));
94 ilm 789
                final ColumnSpec c7 = new ColumnSpec("sales.quote.item.quantity", Integer.class, "Quantité", new BigDecimal(1), true, new LightUITextField("sales.quote.item.quantity"));
83 ilm 790
 
156 ilm 791
                final List<ColumnSpec> columnsSpec = new ArrayList<>(7);
94 ilm 792
                columnsSpec.add(c1);
793
                columnsSpec.add(c2);
794
                columnsSpec.add(c3);
795
                columnsSpec.add(c4);
796
                columnsSpec.add(c5);
797
                columnsSpec.add(c6);
798
                columnsSpec.add(c7);
156 ilm 799
                final List<String> possibleColumnIds = new ArrayList<>(columnsSpec.size());
94 ilm 800
                for (ColumnSpec c : columnsSpec) {
801
                    possibleColumnIds.add(c.getId());
83 ilm 802
                }
803
 
94 ilm 804
                final String lId = "sales.quote.items";
805
                final long userId = UserManager.getUserID();
806
                Document columnsPrefs = null;
807
                try {
808
                    final DOMBuilder in = new DOMBuilder();
809
                    final org.w3c.dom.Document w3cDoc = Configuration.getInstance().getXMLConf(userId, lId);
810
                    if (w3cDoc != null) {
811
                        columnsPrefs = in.build(w3cDoc);
812
                    }
142 ilm 813
                    if (columnsPrefs == null) {
814
                        throw new IllegalStateException("Columns Prefs is null");
815
                    }
94 ilm 816
                } catch (Exception ex) {
132 ilm 817
                    throw new IllegalArgumentException(
818
                            "DevisSQLElement getItemsCustomEditorProvider - Failed to get ColumnPrefs for descriptor " + lId + " and for user " + userId + "\n" + ex.getMessage());
94 ilm 819
                }
83 ilm 820
 
94 ilm 821
                final Element rootElement = columnsPrefs.getRootElement();
822
                if (!rootElement.getName().equals("list")) {
823
                    throw new IllegalArgumentException("invalid xml, roots node list expected but " + rootElement.getName() + " found");
824
                }
825
                final List<Element> xmlColumns = rootElement.getChildren();
826
                final int columnsCount = columnsSpec.size();
827
                if (xmlColumns.size() != columnsCount) {
828
                    throw new IllegalArgumentException("incorrect columns count in xml");
829
                }
830
 
831
                for (int i = 0; i < columnsCount; i++) {
832
                    final ColumnSpec columnSpec = columnsSpec.get(i);
833
                    final String columnId = columnSpec.getId();
834
                    boolean find = false;
835
 
836
                    for (int j = 0; j < columnsCount; j++) {
837
                        final Element xmlColumn = xmlColumns.get(j);
838
                        final String xmlColumnId = xmlColumn.getAttribute("id").getValue();
839
 
840
                        if (xmlColumnId.equals(columnId)) {
841
 
842
                            if (!xmlColumn.getName().equals("column")) {
843
                                throw new IllegalArgumentException("ColumnSpec setPrefs - Invalid xml, element node column expected but " + xmlColumn.getName() + " found");
844
                            }
845
                            if (xmlColumn.getAttribute("width") == null || xmlColumn.getAttribute("min-width") == null || xmlColumn.getAttribute("max-width") == null) {
846
                                throw new IllegalArgumentException("ColumnSpec setPrefs - Invalid column node for " + columnId + ", it must have attribute width, min-width, max-width");
847
                            }
848
 
849
                            final int width = Integer.parseInt(xmlColumn.getAttribute("width").getValue());
850
                            final int maxWidth = Integer.parseInt(xmlColumn.getAttribute("max-width").getValue());
851
                            final int minWidth = Integer.parseInt(xmlColumn.getAttribute("min-width").getValue());
852
 
853
                            columnSpec.setPrefs(width, maxWidth, minWidth);
854
                            if (i != j) {
855
                                final ColumnSpec swap = columnsSpec.get(i);
856
                                columnsSpec.set(i, columnsSpec.get(j));
857
                                columnsSpec.set(j, swap);
858
                            }
859
                            find = true;
860
                            break;
861
                        }
862
                    }
863
                    if (!find) {
864
                        throw new IllegalArgumentException("xml contain unknow column: " + columnId);
865
                    }
866
                }
867
                final ColumnsSpec cSpec = new ColumnsSpec(lId, columnsSpec, possibleColumnIds, null);
868
                cSpec.setAllowMove(true);
869
                cSpec.setAllowResize(true);
174 ilm 870
                final RowSelectionSpec selectionSpec = new RowSelectionSpec(getItemId());
871
                final TableSpec tSpec = new TableSpec(getItemId(), selectionSpec, cSpec);
94 ilm 872
                tSpec.setColumns(cSpec);
873
 
174 ilm 874
                String id = getItemId();
94 ilm 875
                final LightUITable eList = new LightUITable(id);
876
                eList.setTableSpec(tSpec);
83 ilm 877
 
132 ilm 878
                LightUIPanel panel = new LightUIPanel("sales.quote.items.list");
879
                panel.setGridWidth(1);
880
                panel.setFillWidth(true);
83 ilm 881
 
882
                LightUILine toolbarLine = new LightUILine();
883
 
149 ilm 884
                LightUIButtonUnmanaged b1 = new LightUIButtonUnmanaged("up");
83 ilm 885
                b1.setIcon("up.png");
132 ilm 886
                panel.addControler(new ActivationOnSelectionControler(id, b1.getId()));
156 ilm 887
                panel.addControler(new LightController(LightController.TYPE_UP, id, b1.getId()));
132 ilm 888
                toolbarLine.addChild(b1);
83 ilm 889
 
149 ilm 890
                final LightUIButtonUnmanaged b2 = new LightUIButtonUnmanaged("down");
83 ilm 891
                b2.setIcon("down.png");
132 ilm 892
                panel.addControler(new ActivationOnSelectionControler(id, b2.getId()));
156 ilm 893
                panel.addControler(new LightController(LightController.TYPE_DOWN, id, b2.getId()));
132 ilm 894
                toolbarLine.addChild(b2);
83 ilm 895
                // Add
149 ilm 896
                LightUIElement addButton = new LightUIButtonUnmanaged("add", "Ajouter une ligne");
156 ilm 897
                panel.addControler(new LightController(LightController.TYPE_ADD_DEFAULT, id, addButton.getId()));
132 ilm 898
                toolbarLine.addChild(addButton);
83 ilm 899
                // Insert
149 ilm 900
                LightUIElement insertButton = new LightUIButtonUnmanaged("insert", "Insérer une ligne");
156 ilm 901
                panel.addControler(new LightController(LightController.TYPE_INSERT_DEFAULT, id, insertButton.getId()));
132 ilm 902
                toolbarLine.addChild(insertButton);
83 ilm 903
 
904
                // Copy
149 ilm 905
                LightUIElement copyButton = new LightUIButtonUnmanaged("copy", "Dupliquer");
132 ilm 906
                panel.addControler(new ActivationOnSelectionControler(id, copyButton.getId()));
156 ilm 907
                panel.addControler(new LightController(LightController.TYPE_COPY, id, copyButton.getId()));
132 ilm 908
                toolbarLine.addChild(copyButton);
83 ilm 909
 
910
                // Remove
149 ilm 911
                LightUIElement removeButton = new LightUIButtonUnmanaged("remove", "Supprimer");
132 ilm 912
                panel.addControler(new ActivationOnSelectionControler(id, removeButton.getId()));
156 ilm 913
                panel.addControler(new LightController(LightController.TYPE_REMOVE, id, removeButton.getId()));
132 ilm 914
                toolbarLine.addChild(removeButton);
83 ilm 915
 
132 ilm 916
                panel.addChild(toolbarLine);
83 ilm 917
                final LightUILine listLine = new LightUILine();
918
                listLine.setWeightY(1);
919
                listLine.setFillHeight(true);
132 ilm 920
                listLine.addChild(eList);
83 ilm 921
 
922
                //
132 ilm 923
                panel.addChild(listLine);
83 ilm 924
 
132 ilm 925
                return panel;
174 ilm 926
 
83 ilm 927
            }
928
 
174 ilm 929
            @Override
930
            public void fillFrom(LightUIElement uiElement, SQLRowAccessor sqlRow) {
931
                LightUIPanel p = (LightUIPanel) uiElement;
932
                LightUITable ltable = p.getFirstChild(LightUITable.class);
933
                TableSpec tSpec = ltable.getTableSpec();
934
                ColumnsSpec columnsSpec = tSpec.getColumns();
935
                // send: id,value
936
                final SQLElement elem = configuration.getDirectory().getElement("DEVIS_ELEMENT");
937
                final SQLTable table = elem.getTable();
938
                final List<String> fieldsToFetch = new ArrayList<>();
939
                for (ColumnSpec cs : columnsSpec.getColumns()) {
940
                    String colId = cs.getId();
941
                    SQLField f = configuration.getFieldMapper().getSQLFieldForItem(colId);
942
                    if (f != null) {
943
                        fieldsToFetch.add(f.getName());
944
                    } else {
945
                        throw new IllegalStateException("No field in " + table + " for column id " + colId);
946
                    }
947
                }
948
 
949
                final Where where = new Where(table.getField("ID_DEVIS"), "=", sqlRow.getID());
950
                final ListSQLRequest req = elem.createListRequest(fieldsToFetch, where, configuration.getShowAs());
951
                List<SQLRowValues> fetchedRows = req.getValues();
952
 
953
                List<Row> rows = new ArrayList<>();
954
                for (final SQLRowValues vals : fetchedRows) {
955
                    Row r = new Row(vals.getID(), columnsSpec.getColumns().size());
956
                    List<Object> values = new ArrayList<>();
957
                    for (ColumnSpec cs : columnsSpec.getColumns()) {
958
                        String colId = cs.getId();
959
                        SQLField f = configuration.getFieldMapper().getSQLFieldForItem(colId);
960
                        if (f != null) {
961
                            Object object = vals.getObject(f.getName());
962
                            if (object instanceof SQLRowValues) {
963
                                SQLRowValues sqlRowValues = (SQLRowValues) object;
964
                                long rowId = sqlRowValues.getIDNumber().longValue();
965
                                List<SQLField> fieldsToExpand = configuration.getShowAs().getFieldExpand(sqlRowValues.getTable());
966
                                final StringBuilder b = new StringBuilder();
967
                                for (SQLField sqlField : fieldsToExpand) {
968
                                    b.append(sqlRowValues.getObject(sqlField.getName()).toString());
969
                                    b.append(' ');
970
                                }
971
                                object = new StringWithId(rowId, b.toString().trim());
972
                            }
973
                            values.add(object);
974
                        } else {
975
                            throw new IllegalStateException("No field in " + table + " for column id " + colId);
976
                        }
977
                    }
978
                    r.setValues(values);
979
                    rows.add(r);
980
                }
981
 
982
                TableContent tableContent = new TableContent();
983
                tableContent.setRows(rows);
984
                tSpec.setContent(tableContent);
985
 
986
            }
987
 
988
            @Override
989
            public void store(LightUIElement uiElement, SQLRowValues row) {
990
                // TODO Auto-generated method stub
991
 
992
            }
993
 
132 ilm 994
        });
995
        return map;
83 ilm 996
    }
156 ilm 997
 
998
    @Override
999
    protected String createCode() {
1000
        return "sales.quote";
1001
    }
18 ilm 1002
}