OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
80 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.account;
15
 
16
import org.openconcerto.erp.core.common.component.TransfertGroupSQLComponent;
17
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
18
import org.openconcerto.erp.core.common.ui.AbstractArticleItemTable;
132 ilm 19
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable.TypeCalcul;
80 ilm 20
import org.openconcerto.erp.core.common.ui.Acompte;
21
import org.openconcerto.erp.core.common.ui.AcompteField;
22
import org.openconcerto.erp.core.common.ui.AcompteRowItemView;
23
import org.openconcerto.erp.core.common.ui.DeviseField;
24
import org.openconcerto.erp.core.common.ui.TotalPanel;
156 ilm 25
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseType;
142 ilm 26
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
80 ilm 27
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
28
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
29
import org.openconcerto.erp.core.sales.invoice.ui.FactureSituationItemTable;
30
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieVenteFacture;
142 ilm 31
import org.openconcerto.erp.panel.PanelOOSQLComponent;
80 ilm 32
import org.openconcerto.sql.Configuration;
33
import org.openconcerto.sql.element.ElementSQLObject;
83 ilm 34
import org.openconcerto.sql.element.GlobalMapper;
80 ilm 35
import org.openconcerto.sql.element.SQLElement;
36
import org.openconcerto.sql.model.SQLRow;
142 ilm 37
import org.openconcerto.sql.model.SQLRowAccessor;
80 ilm 38
import org.openconcerto.sql.model.SQLRowValues;
39
import org.openconcerto.sql.model.SQLTable;
144 ilm 40
import org.openconcerto.sql.model.Where;
41
import org.openconcerto.sql.sqlobject.ElementComboBox;
80 ilm 42
import org.openconcerto.sql.sqlobject.JUniqueTextField;
43
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
44
import org.openconcerto.sql.view.EditFrame;
45
import org.openconcerto.sql.view.list.RowValuesTable;
46
import org.openconcerto.sql.view.list.SQLTableElement;
47
import org.openconcerto.ui.JDate;
48
import org.openconcerto.ui.component.ITextArea;
49
import org.openconcerto.ui.group.Group;
50
import org.openconcerto.utils.ExceptionHandler;
51
import org.openconcerto.utils.text.SimpleDocumentListener;
52
 
53
import java.awt.Component;
54
import java.beans.PropertyChangeEvent;
55
import java.beans.PropertyChangeListener;
56
import java.math.BigDecimal;
57
import java.sql.SQLException;
58
import java.util.Date;
59
import java.util.HashSet;
60
import java.util.Set;
61
 
62
import javax.swing.JCheckBox;
63
import javax.swing.JComponent;
64
import javax.swing.JLabel;
142 ilm 65
import javax.swing.JOptionPane;
80 ilm 66
import javax.swing.JTextField;
67
import javax.swing.SwingConstants;
68
import javax.swing.SwingUtilities;
69
import javax.swing.event.DocumentEvent;
70
 
71
public class VenteFactureSituationSQLComponent extends TransfertGroupSQLComponent {
72
    public static final String ID = "sales.invoice.partial";
73
 
142 ilm 74
    private PanelOOSQLComponent panelOO;
75
 
80 ilm 76
    public VenteFactureSituationSQLComponent(SQLElement element) {
83 ilm 77
        super(element, (Group) GlobalMapper.getInstance().get(ID));
80 ilm 78
    }
79
 
80
    public VenteFactureSituationSQLComponent(SQLElement element, Group p) {
81
        super(element, p);
82
    }
83
 
84
    @Override
85
    protected Set<String> createRequiredNames() {
86
        final Set<String> s = new HashSet<String>(1);
87
        s.add("ID_CLIENT");
142 ilm 88
        s.add("ID_COMMERCIAL");
80 ilm 89
        s.add("NUMERO");
90
        s.add("DATE");
91
        s.add("MONTANT_FACTURABLE");
92
        s.add("ID_MODE_REGLEMENT");
93
        return s;
94
    }
95
 
96
    @Override
97
    protected RowValuesTable getRowValuesTable() {
98
 
99
        return ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).getRowValuesTable();
100
    }
101
 
102
    JCheckBox box = new JCheckBox("partial");
103
 
104
    @Override
142 ilm 105
    public void select(SQLRowAccessor r) {
106
        super.select(r);
107
        final DeviseField totalRemise = (DeviseField) getEditor("REMISE_HT");
108
        totalRemise.setEditable(false);
109
        totalRemise.setEnabled(false);
110
    }
111
 
112
    @Override
80 ilm 113
    protected void addViews() {
114
 
115
        super.addViews();
116
 
117
        box.setSelected(true);
118
        this.addView(box, "PARTIAL", REQ);
119
 
120
        final DeviseField totalHT = (DeviseField) getEditor("T_HT");
121
        final DeviseField totalService = (DeviseField) getEditor("T_SERVICE");
122
        final DeviseField totalSupply = (DeviseField) getEditor("T_HA");
123
        final DeviseField totalDevise = (DeviseField) getEditor("T_DEVISE");
124
        final JTextField totalWeight = (JTextField) getEditor("T_POIDS");
125
        final DeviseField totalTTC = (DeviseField) getEditor("T_TTC");
126
        final DeviseField totalTVA = (DeviseField) getEditor("T_TVA");
142 ilm 127
        final DeviseField totalEco = (DeviseField) getEditor("T_ECO_CONTRIBUTION");
80 ilm 128
        final DeviseField totalRemise = (DeviseField) getEditor("REMISE_HT");
142 ilm 129
        totalRemise.setEditable(false);
130
        totalRemise.setEnabled(false);
80 ilm 131
        final DeviseField totalPORT = (DeviseField) getEditor("PORT_HT");
132
        this.addView(totalPORT, "PORT_HT");
133
        this.addView(totalTVA, "T_TVA");
134
        this.addView(totalTTC, "T_TTC");
142 ilm 135
        this.addView(totalEco, "T_ECO_CONTRIBUTION");
80 ilm 136
        this.addView(totalWeight, "T_POIDS");
137
        this.addView(totalDevise, "T_DEVISE");
138
        this.addView(totalSupply, "T_HA");
139
        this.addView(totalService, "T_SERVICE");
140
        this.addView(totalHT, "T_HT");
141
 
132 ilm 142
        final DeviseField fieldNet = new DeviseField();
143
        this.addView(fieldNet, "NET_A_PAYER");
144
        totalTTC.getDocument().addDocumentListener(new SimpleDocumentListener() {
145
 
146
            @Override
147
            public void update(DocumentEvent e) {
148
                fieldNet.setText(totalTTC.getText());
149
            }
150
        });
151
 
144 ilm 152
        final SQLRequestComboBox sqlRequestComboBoxAdr = (SQLRequestComboBox) getEditor("ID_ADRESSE");
153
        final SQLRequestComboBox sqlRequestComboBoxAdrL = (SQLRequestComboBox) getEditor("ID_ADRESSE_LIVRAISON");
154
 
80 ilm 155
        final SQLRequestComboBox sqlRequestComboBox = (SQLRequestComboBox) getEditor("sales.invoice.customer");
156 ilm 156
        final FactureSituationItemTable table = ((FactureSituationItemTable) getEditor("sales.invoice.partial.items.list"));
157
        final TotalPanel total = ((TotalPanel) getEditor("sales.invoice.partial.total.amount"));
158
 
80 ilm 159
        sqlRequestComboBox.addModelListener("wantedID", new PropertyChangeListener() {
160
 
161
            @Override
162
            public void propertyChange(PropertyChangeEvent evt) {
163
 
144 ilm 164
                int wantedID = sqlRequestComboBox.getWantedID();
165
 
166
                if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
167
 
168
                    sqlRequestComboBoxAdr.getRequest().setWhere(new Where(getTable().getTable("ADRESSE").getField("ID_CLIENT"), "=", wantedID)
169
                            .and(new Where(getTable().getTable("ADRESSE").getField("TYPE"), "=", AdresseType.Invoice.getId())));
170
                    sqlRequestComboBoxAdrL.getRequest().setWhere(new Where(getTable().getTable("ADRESSE").getField("ID_CLIENT"), "=", wantedID)
171
                            .and(new Where(getTable().getTable("ADRESSE").getField("TYPE"), "=", AdresseType.Delivery.getId())));
156 ilm 172
                    SQLRow rowCli = getTable().getForeignTable("ID_CLIENT").getRow(wantedID);
173
                    if (rowCli.getObject("ID_CATEGORIE_COMPTABLE") != null && !rowCli.isForeignEmpty("ID_CATEGORIE_COMPTABLE")) {
174
                        total.setCategorieComptable(rowCli.getForeign("ID_CATEGORIE_COMPTABLE"));
175
                        table.setRowCatComptable(rowCli.getForeign("ID_CATEGORIE_COMPTABLE"));
176
                    } else {
177
                        total.setCategorieComptable(null);
178
                        table.setRowCatComptable(null);
179
                    }
144 ilm 180
                } else {
181
                    sqlRequestComboBoxAdr.getRequest().setWhere(Where.FALSE);
182
                    sqlRequestComboBoxAdrL.getRequest().setWhere(Where.FALSE);
156 ilm 183
                    total.setCategorieComptable(null);
184
                    table.setRowCatComptable(null);
144 ilm 185
                }
186
 
80 ilm 187
                SQLElement sqleltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
188
                final SQLRow client = sqlRequestComboBox.getSelectedRow();
189
                if (client != null && !client.isUndefined()) {
190
                    int idModeRegl = client.getInt("ID_MODE_REGLEMENT");
191
                    if (idModeRegl > 1) {
192
                        SQLRow rowModeRegl = sqleltModeRegl.getTable().getRow(idModeRegl);
193
                        SQLRowValues rowValsModeRegl = rowModeRegl.createUpdateRow();
194
                        rowValsModeRegl.clearPrimaryKeys();
195
                        ((ElementSQLObject) getEditor("ID_MODE_REGLEMENT")).setValue(rowValsModeRegl);
196
                    }
197
                }
198
 
199
            }
200
        });
201
        sqlRequestComboBox.setEnabled(false);
202
 
203
        final AcompteField acompteField = ((AcompteField) getEditor("sales.invoice.partial.amount"));
204
        acompteField.getDocument().addDocumentListener(new SimpleDocumentListener() {
205
 
206
            @Override
207
            public void update(DocumentEvent e) {
208
                Acompte a = acompteField.getValue();
94 ilm 209
                table.calculPourcentage(a, TypeCalcul.CALCUL_FACTURABLE);
80 ilm 210
            }
211
        });
212
        total.addValueListener(new PropertyChangeListener() {
213
 
214
            @Override
215
            public void propertyChange(PropertyChangeEvent evt) {
216
                acompteField.setTotal(new BigDecimal(total.getTotalHT()).movePointLeft(2));
217
 
218
            }
219
        });
220
 
221
    }
222
 
83 ilm 223
    int countPole = 0;
224
 
225
    // @Override
226
    // public Component addView(MutableRowItemView rowItemView, String fields, Object specObj) {
227
    //
228
    // if (fields.contains("ID_POLE_PRODUIT") && countPole == 0) {
229
    // countPole++;
230
    // return null;
231
    // } else {
232
    // return super.addView(rowItemView, fields, specObj);
233
    // }
234
    // }
235
 
80 ilm 236
    @Override
237
    public JComponent getLabel(String id) {
238
        if (id.equals("sales.invoice.partial.amount")) {
132 ilm 239
            final JLabel jLabel = new JLabel("Montant HT (ou %) à facturer");
80 ilm 240
            jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
241
            return jLabel;
142 ilm 242
        } else if (id.equals("panel.oo")) {
243
            return new JLabel();
80 ilm 244
        } else if (id.equals("sales.invoice.partial.total.amount")) {
245
            return new JLabel();
246
        } else {
247
            return super.getLabel(id);
248
        }
249
    }
250
 
251
    JUniqueTextField numberField;
252
 
253
    @Override
254
    public JComponent createEditor(String id) {
255
 
256
        if (id.equals("sales.invoice.number")) {
142 ilm 257
            this.numberField = new JUniqueTextField(20) {
258
                @Override
259
                public String getAutoRefreshNumber() {
260
                    if (getMode() == Mode.INSERTION) {
261
                        return NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), ((JDate) getEditor("DATE")).getDate());
262
                    } else {
263
                        return null;
264
                    }
265
                }
266
            };
80 ilm 267
            return this.numberField;
142 ilm 268
        } else if (id.equals("panel.oo")) {
269
            this.panelOO = new PanelOOSQLComponent(this);
270
            return this.panelOO;
80 ilm 271
        } else if (id.equals("INFOS")) {
272
            final ITextArea jTextArea = new ITextArea();
273
            jTextArea.setFont(new JLabel().getFont());
274
            return jTextArea;
275
        } else if (id.equals("sales.invoice.partial.items.list")) {
276
            return new FactureSituationItemTable();
277
        } else if (id.equals("DATE")) {
278
            return new JDate(true);
279
        } else if (id.equals("T_POIDS")) {
280
            return new JTextField();
281
        } else if (id.equals("sales.invoice.partial.total.amount")) {
282
            final AbstractArticleItemTable items = (AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list");
283
            // Set only VAT Editable
284
            for (int i = 0; i < items.getRowValuesTable().getColumnModel().getColumnCount(false); i++) {
285
                final SQLTableElement sqlTableElementAt = items.getRowValuesTable().getRowValuesTableModel().getSQLTableElementAt(i);
286
                if (sqlTableElementAt.getField() == null || !sqlTableElementAt.getField().getName().equalsIgnoreCase("ID_TAXE")) {
287
                    sqlTableElementAt.setEditable(false);
288
                } else {
289
                    sqlTableElementAt.setEditable(true);
290
                }
291
            }
292
 
293
            final DeviseField totalHT = (DeviseField) getEditor("T_HT");
142 ilm 294
            final DeviseField totalEco = (DeviseField) getEditor("T_ECO_CONTRIBUTION");
80 ilm 295
            final DeviseField totalService = (DeviseField) getEditor("T_SERVICE");
296
            final DeviseField totalSupply = (DeviseField) getEditor("T_HA");
297
            final DeviseField totalDevise = (DeviseField) getEditor("T_DEVISE");
298
            final JTextField totalWeight = (JTextField) getEditor("T_POIDS");
299
            final DeviseField totalTTC = (DeviseField) getEditor("T_TTC");
300
            final DeviseField totalTVA = (DeviseField) getEditor("T_TVA");
301
            final DeviseField totalRemise = (DeviseField) getEditor("REMISE_HT");
142 ilm 302
            totalRemise.setEditable(false);
303
            totalRemise.setEnabled(false);
304
            final AcompteField acompte = (AcompteField) getEditor("sales.invoice.partial.amount");
80 ilm 305
            final DeviseField totalPORT = (DeviseField) getEditor("PORT_HT");
142 ilm 306
            final SQLRequestComboBox taxePort = (SQLRequestComboBox) getEditor("ID_TAXE_PORT");
307
            return new TotalPanel(items, totalEco, totalHT, totalTVA, totalTTC, totalPORT, totalRemise, totalService, totalSupply, totalDevise, totalWeight, null, taxePort, acompte);
80 ilm 308
        } else if (id.startsWith("T_")) {
309
            return new DeviseField();
310
        } else if (id.equals("REMISE_HT") || id.equals("PORT_HT")) {
142 ilm 311
            DeviseField fieldD = new DeviseField();
312
            fieldD.setEditable(false);
313
            fieldD.setEnabled(false);
314
            return fieldD;
144 ilm 315
        } else if (id.startsWith("ID_ADRESSE")) {
316
            ElementComboBox comboAdrF = new ElementComboBox();
317
            comboAdrF.setButtonsVisible(false);
318
            final SQLElement adrElement = getElement().getForeignElement("ID_ADRESSE");
319
            comboAdrF.init(adrElement, adrElement.getComboRequest(true));
320
            comboAdrF.getRequest().setWhere(Where.FALSE);
321
            return comboAdrF;
80 ilm 322
        } else if (id.equals("sales.invoice.partial.amount")) {
323
            return new AcompteField();
324
        }
325
        return super.createEditor(id);
326
    }
327
 
328
    private final SQLTable tableNum = getElement().getTable().getTable("NUMEROTATION_AUTO");
329
 
330
    @Override
331
    public int insert(SQLRow order) {
332
 
333
        int idSaisieVF = SQLRow.NONEXISTANT_ID;
334
 
142 ilm 335
        int attempt = 0;
336
        // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
337
        if (!this.numberField.checkValidation(false)) {
338
            while (attempt < JUniqueTextField.RETRY_COUNT) {
339
                String num = NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), ((JDate) getEditor("DATE")).getDate());
340
                this.numberField.setText(num);
341
                attempt++;
342
                if (this.numberField.checkValidation(false)) {
343
                    System.err.println("ATEMPT " + attempt + " SUCCESS WITH NUMERO " + num);
344
                    break;
345
                }
346
                try {
347
                    Thread.sleep(JUniqueTextField.SLEEP_WAIT_MS);
348
                } catch (InterruptedException e) {
349
                    e.printStackTrace();
350
                }
351
            }
352
        }
353
        final String num = this.numberField.getText();
354
        if (attempt == JUniqueTextField.RETRY_COUNT) {
355
            idSaisieVF = getSelectedID();
356
            ExceptionHandler.handle("Impossible d'ajouter, numéro de facture existant.");
357
            final Object root = SwingUtilities.getRoot(this);
358
            if (root instanceof EditFrame) {
359
                final EditFrame frame = (EditFrame) root;
360
                frame.getPanel().setAlwaysVisible(true);
361
            }
362
        } else {
80 ilm 363
            idSaisieVF = super.insert(order);
364
            SQLRow rowFacture = getTable().getRow(idSaisieVF);
365
            // incrémentation du numéro auto
366
            if (NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, rowFacture.getDate("DATE").getTime()).equalsIgnoreCase(this.numberField.getText().trim())) {
367
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
368
 
369
                String labelNumberFor = NumerotationAutoSQLElement.getLabelNumberFor(SaisieVenteFactureSQLElement.class);
370
                int val = this.tableNum.getRow(2).getInt(labelNumberFor);
371
                val++;
372
                rowVals.put(labelNumberFor, Integer.valueOf(val));
373
                try {
374
                    rowVals.update(2);
375
                } catch (SQLException e1) {
376
                    e1.printStackTrace();
377
                }
132 ilm 378
            }
379
            ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).updateField("ID_SAISIE_VENTE_FACTURE", idSaisieVF);
80 ilm 380
 
132 ilm 381
            new GenerationMvtSaisieVenteFacture(idSaisieVF);
80 ilm 382
 
132 ilm 383
            try {
384
                VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
385
                sheet.createDocument();
142 ilm 386
                sheet.showPrintAndExport(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
132 ilm 387
            } catch (Exception e) {
388
                ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", e);
389
            }
142 ilm 390
            if (attempt > 0) {
391
                SwingUtilities.invokeLater(new Runnable() {
392
                    public void run() {
393
                        JOptionPane.showMessageDialog(null, "Le numéro a été actualisé en " + num);
394
                    }
395
                });
80 ilm 396
            }
397
        }
398
        return idSaisieVF;
399
    }
400
 
401
    @Override
402
    public void update() {
403
 
142 ilm 404
        int id = getSelectedID();
80 ilm 405
        super.update();
142 ilm 406
        final SQLRow rowFacture = getTable().getRow(id);
80 ilm 407
        ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).updateField("ID_SAISIE_VENTE_FACTURE", id);
408
 
142 ilm 409
        int idMvt = rowFacture.getInt("ID_MOUVEMENT");
410
        // on supprime tout ce qui est lié à la facture
411
        System.err.println("Archivage des fils");
412
        EcritureSQLElement eltEcr = (EcritureSQLElement) getDirectory().getElement("ECRITURE");
413
        eltEcr.archiveMouvementProfondeur(idMvt, false);
414
        new GenerationMvtSaisieVenteFacture(id);
415
        try {
416
            VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
417
            sheet.createDocument();
418
            sheet.showPrintAndExport(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
419
        } catch (Exception e) {
420
            ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", e);
421
        }
80 ilm 422
    }
423
 
424
    @Override
425
    protected SQLRowValues createDefaults() {
426
        SQLRowValues rowVals = new SQLRowValues(getTable());
427
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, new Date()));
428
        rowVals.put("PARTIAL", Boolean.TRUE);
429
        return rowVals;
430
    }
431
 
432
    @Override
433
    public Component addView(JComponent comp, String id) {
434
        if (id.equals("sales.invoice.partial.amount")) {
435
            return super.addView(new AcompteRowItemView((AcompteField) comp), "MONTANT_FACTURABLE,POURCENT_FACTURABLE", REQ);
436
        } else {
437
            return super.addView(comp, id);
438
        }
439
    }
440
}