OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Rev 156 | 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;
144 ilm 20
import org.openconcerto.erp.core.customerrelationship.customer.ui.AdresseType;
80 ilm 21
import org.openconcerto.erp.core.common.ui.Acompte;
22
import org.openconcerto.erp.core.common.ui.AcompteField;
23
import org.openconcerto.erp.core.common.ui.AcompteRowItemView;
24
import org.openconcerto.erp.core.common.ui.DeviseField;
25
import org.openconcerto.erp.core.common.ui.TotalPanel;
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
        sqlRequestComboBox.addModelListener("wantedID", new PropertyChangeListener() {
157
 
158
            @Override
159
            public void propertyChange(PropertyChangeEvent evt) {
160
 
144 ilm 161
                int wantedID = sqlRequestComboBox.getWantedID();
162
 
163
                if (wantedID != SQLRow.NONEXISTANT_ID && wantedID >= SQLRow.MIN_VALID_ID) {
164
 
165
                    sqlRequestComboBoxAdr.getRequest().setWhere(new Where(getTable().getTable("ADRESSE").getField("ID_CLIENT"), "=", wantedID)
166
                            .and(new Where(getTable().getTable("ADRESSE").getField("TYPE"), "=", AdresseType.Invoice.getId())));
167
                    sqlRequestComboBoxAdrL.getRequest().setWhere(new Where(getTable().getTable("ADRESSE").getField("ID_CLIENT"), "=", wantedID)
168
                            .and(new Where(getTable().getTable("ADRESSE").getField("TYPE"), "=", AdresseType.Delivery.getId())));
169
                } else {
170
                    sqlRequestComboBoxAdr.getRequest().setWhere(Where.FALSE);
171
                    sqlRequestComboBoxAdrL.getRequest().setWhere(Where.FALSE);
172
                }
173
 
80 ilm 174
                SQLElement sqleltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
175
                final SQLRow client = sqlRequestComboBox.getSelectedRow();
176
                if (client != null && !client.isUndefined()) {
177
                    int idModeRegl = client.getInt("ID_MODE_REGLEMENT");
178
                    if (idModeRegl > 1) {
179
                        SQLRow rowModeRegl = sqleltModeRegl.getTable().getRow(idModeRegl);
180
                        SQLRowValues rowValsModeRegl = rowModeRegl.createUpdateRow();
181
                        rowValsModeRegl.clearPrimaryKeys();
182
                        ((ElementSQLObject) getEditor("ID_MODE_REGLEMENT")).setValue(rowValsModeRegl);
183
                    }
184
                }
185
 
186
            }
187
        });
188
        sqlRequestComboBox.setEnabled(false);
189
 
190
        final AcompteField acompteField = ((AcompteField) getEditor("sales.invoice.partial.amount"));
191
        final FactureSituationItemTable table = ((FactureSituationItemTable) getEditor("sales.invoice.partial.items.list"));
192
        acompteField.getDocument().addDocumentListener(new SimpleDocumentListener() {
193
 
194
            @Override
195
            public void update(DocumentEvent e) {
196
                Acompte a = acompteField.getValue();
94 ilm 197
                table.calculPourcentage(a, TypeCalcul.CALCUL_FACTURABLE);
80 ilm 198
            }
199
        });
200
        final TotalPanel total = ((TotalPanel) getEditor("sales.invoice.partial.total.amount"));
201
        total.addValueListener(new PropertyChangeListener() {
202
 
203
            @Override
204
            public void propertyChange(PropertyChangeEvent evt) {
205
                acompteField.setTotal(new BigDecimal(total.getTotalHT()).movePointLeft(2));
206
 
207
            }
208
        });
209
 
210
    }
211
 
83 ilm 212
    int countPole = 0;
213
 
214
    // @Override
215
    // public Component addView(MutableRowItemView rowItemView, String fields, Object specObj) {
216
    //
217
    // if (fields.contains("ID_POLE_PRODUIT") && countPole == 0) {
218
    // countPole++;
219
    // return null;
220
    // } else {
221
    // return super.addView(rowItemView, fields, specObj);
222
    // }
223
    // }
224
 
80 ilm 225
    @Override
226
    public JComponent getLabel(String id) {
227
        if (id.equals("sales.invoice.partial.amount")) {
132 ilm 228
            final JLabel jLabel = new JLabel("Montant HT (ou %) à facturer");
80 ilm 229
            jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
230
            return jLabel;
142 ilm 231
        } else if (id.equals("panel.oo")) {
232
            return new JLabel();
80 ilm 233
        } else if (id.equals("sales.invoice.partial.total.amount")) {
234
            return new JLabel();
235
        } else {
236
            return super.getLabel(id);
237
        }
238
    }
239
 
240
    JUniqueTextField numberField;
241
 
242
    @Override
243
    public JComponent createEditor(String id) {
244
 
245
        if (id.equals("sales.invoice.number")) {
142 ilm 246
            this.numberField = new JUniqueTextField(20) {
247
                @Override
248
                public String getAutoRefreshNumber() {
249
                    if (getMode() == Mode.INSERTION) {
250
                        return NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), ((JDate) getEditor("DATE")).getDate());
251
                    } else {
252
                        return null;
253
                    }
254
                }
255
            };
80 ilm 256
            return this.numberField;
142 ilm 257
        } else if (id.equals("panel.oo")) {
258
            this.panelOO = new PanelOOSQLComponent(this);
259
            return this.panelOO;
80 ilm 260
        } else if (id.equals("INFOS")) {
261
            final ITextArea jTextArea = new ITextArea();
262
            jTextArea.setFont(new JLabel().getFont());
263
            return jTextArea;
264
        } else if (id.equals("sales.invoice.partial.items.list")) {
265
            return new FactureSituationItemTable();
266
        } else if (id.equals("DATE")) {
267
            return new JDate(true);
268
        } else if (id.equals("T_POIDS")) {
269
            return new JTextField();
270
        } else if (id.equals("sales.invoice.partial.total.amount")) {
271
            final AbstractArticleItemTable items = (AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list");
272
            // Set only VAT Editable
273
            for (int i = 0; i < items.getRowValuesTable().getColumnModel().getColumnCount(false); i++) {
274
                final SQLTableElement sqlTableElementAt = items.getRowValuesTable().getRowValuesTableModel().getSQLTableElementAt(i);
275
                if (sqlTableElementAt.getField() == null || !sqlTableElementAt.getField().getName().equalsIgnoreCase("ID_TAXE")) {
276
                    sqlTableElementAt.setEditable(false);
277
                } else {
278
                    sqlTableElementAt.setEditable(true);
279
                }
280
            }
281
 
282
            final DeviseField totalHT = (DeviseField) getEditor("T_HT");
142 ilm 283
            final DeviseField totalEco = (DeviseField) getEditor("T_ECO_CONTRIBUTION");
80 ilm 284
            final DeviseField totalService = (DeviseField) getEditor("T_SERVICE");
285
            final DeviseField totalSupply = (DeviseField) getEditor("T_HA");
286
            final DeviseField totalDevise = (DeviseField) getEditor("T_DEVISE");
287
            final JTextField totalWeight = (JTextField) getEditor("T_POIDS");
288
            final DeviseField totalTTC = (DeviseField) getEditor("T_TTC");
289
            final DeviseField totalTVA = (DeviseField) getEditor("T_TVA");
290
            final DeviseField totalRemise = (DeviseField) getEditor("REMISE_HT");
142 ilm 291
            totalRemise.setEditable(false);
292
            totalRemise.setEnabled(false);
293
            final AcompteField acompte = (AcompteField) getEditor("sales.invoice.partial.amount");
80 ilm 294
            final DeviseField totalPORT = (DeviseField) getEditor("PORT_HT");
142 ilm 295
            final SQLRequestComboBox taxePort = (SQLRequestComboBox) getEditor("ID_TAXE_PORT");
296
            return new TotalPanel(items, totalEco, totalHT, totalTVA, totalTTC, totalPORT, totalRemise, totalService, totalSupply, totalDevise, totalWeight, null, taxePort, acompte);
80 ilm 297
        } else if (id.startsWith("T_")) {
298
            return new DeviseField();
299
        } else if (id.equals("REMISE_HT") || id.equals("PORT_HT")) {
142 ilm 300
            DeviseField fieldD = new DeviseField();
301
            fieldD.setEditable(false);
302
            fieldD.setEnabled(false);
303
            return fieldD;
144 ilm 304
        } else if (id.startsWith("ID_ADRESSE")) {
305
            ElementComboBox comboAdrF = new ElementComboBox();
306
            comboAdrF.setButtonsVisible(false);
307
            final SQLElement adrElement = getElement().getForeignElement("ID_ADRESSE");
308
            comboAdrF.init(adrElement, adrElement.getComboRequest(true));
309
            comboAdrF.getRequest().setWhere(Where.FALSE);
310
            return comboAdrF;
80 ilm 311
        } else if (id.equals("sales.invoice.partial.amount")) {
312
            return new AcompteField();
313
        }
314
        return super.createEditor(id);
315
    }
316
 
317
    private final SQLTable tableNum = getElement().getTable().getTable("NUMEROTATION_AUTO");
318
 
319
    @Override
320
    public int insert(SQLRow order) {
321
 
322
        int idSaisieVF = SQLRow.NONEXISTANT_ID;
323
 
142 ilm 324
        int attempt = 0;
325
        // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
326
        if (!this.numberField.checkValidation(false)) {
327
            while (attempt < JUniqueTextField.RETRY_COUNT) {
328
                String num = NumerotationAutoSQLElement.getNextNumero(getElement().getClass(), ((JDate) getEditor("DATE")).getDate());
329
                this.numberField.setText(num);
330
                attempt++;
331
                if (this.numberField.checkValidation(false)) {
332
                    System.err.println("ATEMPT " + attempt + " SUCCESS WITH NUMERO " + num);
333
                    break;
334
                }
335
                try {
336
                    Thread.sleep(JUniqueTextField.SLEEP_WAIT_MS);
337
                } catch (InterruptedException e) {
338
                    e.printStackTrace();
339
                }
340
            }
341
        }
342
        final String num = this.numberField.getText();
343
        if (attempt == JUniqueTextField.RETRY_COUNT) {
344
            idSaisieVF = getSelectedID();
345
            ExceptionHandler.handle("Impossible d'ajouter, numéro de facture existant.");
346
            final Object root = SwingUtilities.getRoot(this);
347
            if (root instanceof EditFrame) {
348
                final EditFrame frame = (EditFrame) root;
349
                frame.getPanel().setAlwaysVisible(true);
350
            }
351
        } else {
80 ilm 352
            idSaisieVF = super.insert(order);
353
            SQLRow rowFacture = getTable().getRow(idSaisieVF);
354
            // incrémentation du numéro auto
355
            if (NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, rowFacture.getDate("DATE").getTime()).equalsIgnoreCase(this.numberField.getText().trim())) {
356
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
357
 
358
                String labelNumberFor = NumerotationAutoSQLElement.getLabelNumberFor(SaisieVenteFactureSQLElement.class);
359
                int val = this.tableNum.getRow(2).getInt(labelNumberFor);
360
                val++;
361
                rowVals.put(labelNumberFor, Integer.valueOf(val));
362
                try {
363
                    rowVals.update(2);
364
                } catch (SQLException e1) {
365
                    e1.printStackTrace();
366
                }
132 ilm 367
            }
368
            ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).updateField("ID_SAISIE_VENTE_FACTURE", idSaisieVF);
80 ilm 369
 
132 ilm 370
            new GenerationMvtSaisieVenteFacture(idSaisieVF);
80 ilm 371
 
132 ilm 372
            try {
373
                VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
374
                sheet.createDocument();
142 ilm 375
                sheet.showPrintAndExport(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
132 ilm 376
            } catch (Exception e) {
377
                ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", e);
378
            }
142 ilm 379
            if (attempt > 0) {
380
                SwingUtilities.invokeLater(new Runnable() {
381
                    public void run() {
382
                        JOptionPane.showMessageDialog(null, "Le numéro a été actualisé en " + num);
383
                    }
384
                });
80 ilm 385
            }
386
        }
387
        return idSaisieVF;
388
    }
389
 
390
    @Override
391
    public void update() {
392
 
142 ilm 393
        int id = getSelectedID();
80 ilm 394
        super.update();
142 ilm 395
        final SQLRow rowFacture = getTable().getRow(id);
80 ilm 396
        ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).updateField("ID_SAISIE_VENTE_FACTURE", id);
397
 
142 ilm 398
        int idMvt = rowFacture.getInt("ID_MOUVEMENT");
399
        // on supprime tout ce qui est lié à la facture
400
        System.err.println("Archivage des fils");
401
        EcritureSQLElement eltEcr = (EcritureSQLElement) getDirectory().getElement("ECRITURE");
402
        eltEcr.archiveMouvementProfondeur(idMvt, false);
403
        new GenerationMvtSaisieVenteFacture(id);
404
        try {
405
            VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
406
            sheet.createDocument();
407
            sheet.showPrintAndExport(this.panelOO.isVisualisationSelected(), this.panelOO.isImpressionSelected(), true);
408
        } catch (Exception e) {
409
            ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", e);
410
        }
80 ilm 411
    }
412
 
413
    @Override
414
    protected SQLRowValues createDefaults() {
415
        SQLRowValues rowVals = new SQLRowValues(getTable());
416
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, new Date()));
417
        rowVals.put("PARTIAL", Boolean.TRUE);
418
        return rowVals;
419
    }
420
 
421
    @Override
422
    public Component addView(JComponent comp, String id) {
423
        if (id.equals("sales.invoice.partial.amount")) {
424
            return super.addView(new AcompteRowItemView((AcompteField) comp), "MONTANT_FACTURABLE,POURCENT_FACTURABLE", REQ);
425
        } else {
426
            return super.addView(comp, id);
427
        }
428
    }
429
}