OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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