OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 94 | 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;
19
import org.openconcerto.erp.core.common.ui.Acompte;
20
import org.openconcerto.erp.core.common.ui.AcompteField;
21
import org.openconcerto.erp.core.common.ui.AcompteRowItemView;
22
import org.openconcerto.erp.core.common.ui.DeviseField;
23
import org.openconcerto.erp.core.common.ui.TotalPanel;
83 ilm 24
import org.openconcerto.erp.core.common.ui.AbstractVenteArticleItemTable.TypeCalcul;
80 ilm 25
import org.openconcerto.erp.core.sales.invoice.element.SaisieVenteFactureSQLElement;
26
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
27
import org.openconcerto.erp.core.sales.invoice.ui.FactureSituationItemTable;
28
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieVenteFacture;
29
import org.openconcerto.sql.Configuration;
30
import org.openconcerto.sql.element.ElementSQLObject;
83 ilm 31
import org.openconcerto.sql.element.GlobalMapper;
80 ilm 32
import org.openconcerto.sql.element.SQLElement;
33
import org.openconcerto.sql.model.SQLRow;
34
import org.openconcerto.sql.model.SQLRowValues;
35
import org.openconcerto.sql.model.SQLTable;
36
import org.openconcerto.sql.sqlobject.JUniqueTextField;
37
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
38
import org.openconcerto.sql.view.EditFrame;
39
import org.openconcerto.sql.view.list.RowValuesTable;
40
import org.openconcerto.sql.view.list.SQLTableElement;
41
import org.openconcerto.ui.JDate;
42
import org.openconcerto.ui.component.ITextArea;
43
import org.openconcerto.ui.group.Group;
44
import org.openconcerto.utils.ExceptionHandler;
45
import org.openconcerto.utils.text.SimpleDocumentListener;
46
 
47
import java.awt.Component;
48
import java.beans.PropertyChangeEvent;
49
import java.beans.PropertyChangeListener;
50
import java.math.BigDecimal;
51
import java.sql.SQLException;
52
import java.util.Date;
53
import java.util.HashSet;
54
import java.util.Set;
55
 
56
import javax.swing.JCheckBox;
57
import javax.swing.JComponent;
58
import javax.swing.JLabel;
59
import javax.swing.JTextField;
60
import javax.swing.SwingConstants;
61
import javax.swing.SwingUtilities;
62
import javax.swing.event.DocumentEvent;
63
 
64
public class VenteFactureSituationSQLComponent extends TransfertGroupSQLComponent {
65
    public static final String ID = "sales.invoice.partial";
66
 
67
    public VenteFactureSituationSQLComponent(SQLElement element) {
83 ilm 68
        super(element, (Group) GlobalMapper.getInstance().get(ID));
80 ilm 69
    }
70
 
71
    public VenteFactureSituationSQLComponent(SQLElement element, Group p) {
72
        super(element, p);
73
    }
74
 
75
    @Override
76
    protected Set<String> createRequiredNames() {
77
        final Set<String> s = new HashSet<String>(1);
78
        s.add("ID_CLIENT");
79
        s.add("NUMERO");
80
        s.add("DATE");
81
        s.add("MONTANT_FACTURABLE");
82
        s.add("ID_MODE_REGLEMENT");
83
        return s;
84
    }
85
 
86
    @Override
87
    protected RowValuesTable getRowValuesTable() {
88
 
89
        return ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).getRowValuesTable();
90
    }
91
 
92
    JCheckBox box = new JCheckBox("partial");
93
 
94
    @Override
95
    protected void addViews() {
96
 
97
        super.addViews();
98
 
99
        box.setSelected(true);
100
        this.addView(box, "PARTIAL", REQ);
101
 
102
        final DeviseField totalHT = (DeviseField) getEditor("T_HT");
103
        final DeviseField totalService = (DeviseField) getEditor("T_SERVICE");
104
        final DeviseField totalSupply = (DeviseField) getEditor("T_HA");
105
        final DeviseField totalDevise = (DeviseField) getEditor("T_DEVISE");
106
        final JTextField totalWeight = (JTextField) getEditor("T_POIDS");
107
        final DeviseField totalTTC = (DeviseField) getEditor("T_TTC");
108
        final DeviseField totalTVA = (DeviseField) getEditor("T_TVA");
109
        final DeviseField totalRemise = (DeviseField) getEditor("REMISE_HT");
110
        final DeviseField totalPORT = (DeviseField) getEditor("PORT_HT");
111
        this.addView(totalPORT, "PORT_HT");
112
        this.addView(totalRemise, "REMISE_HT");
113
        this.addView(totalTVA, "T_TVA");
114
        this.addView(totalTTC, "T_TTC");
115
        this.addView(totalWeight, "T_POIDS");
116
        this.addView(totalDevise, "T_DEVISE");
117
        this.addView(totalSupply, "T_HA");
118
        this.addView(totalService, "T_SERVICE");
119
        this.addView(totalHT, "T_HT");
120
 
121
        final SQLRequestComboBox sqlRequestComboBox = (SQLRequestComboBox) getEditor("sales.invoice.customer");
122
        sqlRequestComboBox.addModelListener("wantedID", new PropertyChangeListener() {
123
 
124
            @Override
125
            public void propertyChange(PropertyChangeEvent evt) {
126
 
127
                SQLElement sqleltModeRegl = Configuration.getInstance().getDirectory().getElement("MODE_REGLEMENT");
128
                final SQLRow client = sqlRequestComboBox.getSelectedRow();
129
                if (client != null && !client.isUndefined()) {
130
                    int idModeRegl = client.getInt("ID_MODE_REGLEMENT");
131
                    if (idModeRegl > 1) {
132
                        SQLRow rowModeRegl = sqleltModeRegl.getTable().getRow(idModeRegl);
133
                        SQLRowValues rowValsModeRegl = rowModeRegl.createUpdateRow();
134
                        rowValsModeRegl.clearPrimaryKeys();
135
                        ((ElementSQLObject) getEditor("ID_MODE_REGLEMENT")).setValue(rowValsModeRegl);
136
                    }
137
                }
138
 
139
            }
140
        });
141
        sqlRequestComboBox.setEnabled(false);
142
 
143
        final AcompteField acompteField = ((AcompteField) getEditor("sales.invoice.partial.amount"));
144
        final FactureSituationItemTable table = ((FactureSituationItemTable) getEditor("sales.invoice.partial.items.list"));
145
        acompteField.getDocument().addDocumentListener(new SimpleDocumentListener() {
146
 
147
            @Override
148
            public void update(DocumentEvent e) {
149
                Acompte a = acompteField.getValue();
83 ilm 150
                table.calculPourcentage(a,TypeCalcul.CALCUL_FACTURABLE);
80 ilm 151
            }
152
        });
153
        final TotalPanel total = ((TotalPanel) getEditor("sales.invoice.partial.total.amount"));
154
        total.addValueListener(new PropertyChangeListener() {
155
 
156
            @Override
157
            public void propertyChange(PropertyChangeEvent evt) {
158
                acompteField.setTotal(new BigDecimal(total.getTotalHT()).movePointLeft(2));
159
 
160
            }
161
        });
162
 
163
    }
164
 
83 ilm 165
    int countPole = 0;
166
 
167
    // @Override
168
    // public Component addView(MutableRowItemView rowItemView, String fields, Object specObj) {
169
    //
170
    // if (fields.contains("ID_POLE_PRODUIT") && countPole == 0) {
171
    // countPole++;
172
    // return null;
173
    // } else {
174
    // return super.addView(rowItemView, fields, specObj);
175
    // }
176
    // }
177
 
80 ilm 178
    @Override
179
    public JComponent getLabel(String id) {
180
        if (id.equals("sales.invoice.partial.amount")) {
181
            final JLabel jLabel = new JLabel("Montant à facturer");
182
            jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
183
            return jLabel;
184
        } else if (id.equals("sales.invoice.partial.total.amount")) {
185
            return new JLabel();
186
        } else {
187
            return super.getLabel(id);
188
        }
189
    }
190
 
191
    JUniqueTextField numberField;
192
 
193
    @Override
194
    public JComponent createEditor(String id) {
195
 
196
        if (id.equals("sales.invoice.number")) {
197
            this.numberField = new JUniqueTextField(20);
198
            return this.numberField;
199
        } else if (id.equals("INFOS")) {
200
            final ITextArea jTextArea = new ITextArea();
201
            jTextArea.setFont(new JLabel().getFont());
202
            return jTextArea;
203
        } else if (id.equals("sales.invoice.partial.items.list")) {
204
            return new FactureSituationItemTable();
205
        } else if (id.equals("DATE")) {
206
            return new JDate(true);
207
        } else if (id.equals("T_POIDS")) {
208
            return new JTextField();
209
        } else if (id.equals("sales.invoice.partial.total.amount")) {
210
            final AbstractArticleItemTable items = (AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list");
211
            // Set only VAT Editable
212
            for (int i = 0; i < items.getRowValuesTable().getColumnModel().getColumnCount(false); i++) {
213
                final SQLTableElement sqlTableElementAt = items.getRowValuesTable().getRowValuesTableModel().getSQLTableElementAt(i);
214
                if (sqlTableElementAt.getField() == null || !sqlTableElementAt.getField().getName().equalsIgnoreCase("ID_TAXE")) {
215
                    sqlTableElementAt.setEditable(false);
216
                } else {
217
                    sqlTableElementAt.setEditable(true);
218
                }
219
            }
220
 
221
            final DeviseField totalHT = (DeviseField) getEditor("T_HT");
222
            final DeviseField totalService = (DeviseField) getEditor("T_SERVICE");
223
            final DeviseField totalSupply = (DeviseField) getEditor("T_HA");
224
            final DeviseField totalDevise = (DeviseField) getEditor("T_DEVISE");
225
            final JTextField totalWeight = (JTextField) getEditor("T_POIDS");
226
            final DeviseField totalTTC = (DeviseField) getEditor("T_TTC");
227
            final DeviseField totalTVA = (DeviseField) getEditor("T_TVA");
228
            final DeviseField totalRemise = (DeviseField) getEditor("REMISE_HT");
229
            final DeviseField totalPORT = (DeviseField) getEditor("PORT_HT");
230
 
231
            return new TotalPanel(items, totalHT, totalTVA, totalTTC, totalRemise, totalPORT, totalService, totalSupply, totalDevise, totalWeight, null);
232
        } else if (id.startsWith("T_")) {
233
            return new DeviseField();
234
        } else if (id.equals("REMISE_HT") || id.equals("PORT_HT")) {
235
            return new DeviseField();
236
        } else if (id.equals("sales.invoice.partial.amount")) {
237
            return new AcompteField();
238
        }
239
        return super.createEditor(id);
240
    }
241
 
242
    private final SQLTable tableNum = getElement().getTable().getTable("NUMEROTATION_AUTO");
243
 
244
    @Override
245
    public int insert(SQLRow order) {
246
 
247
        int idSaisieVF = SQLRow.NONEXISTANT_ID;
248
 
249
        if (this.numberField.checkValidation()) {
250
 
251
            idSaisieVF = super.insert(order);
252
            SQLRow rowFacture = getTable().getRow(idSaisieVF);
253
            // incrémentation du numéro auto
254
            if (NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, rowFacture.getDate("DATE").getTime()).equalsIgnoreCase(this.numberField.getText().trim())) {
255
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
256
 
257
                String labelNumberFor = NumerotationAutoSQLElement.getLabelNumberFor(SaisieVenteFactureSQLElement.class);
258
                int val = this.tableNum.getRow(2).getInt(labelNumberFor);
259
                val++;
260
                rowVals.put(labelNumberFor, Integer.valueOf(val));
261
                try {
262
                    rowVals.update(2);
263
                } catch (SQLException e1) {
264
                    e1.printStackTrace();
265
                }
266
 
267
                ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).updateField("ID_SAISIE_VENTE_FACTURE", idSaisieVF);
268
 
269
                new GenerationMvtSaisieVenteFacture(idSaisieVF);
270
 
271
                try {
272
                    VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(rowFacture);
273
                    sheet.createDocument();
274
                    sheet.showPrintAndExport(true, false, false);
275
                } catch (Exception e) {
276
                    ExceptionHandler.handle("Une erreur est survenue lors de la création du document.", e);
277
                }
278
 
279
            }
280
        } else {
281
            ExceptionHandler.handle("Impossible d'ajouter, numéro de facture existant.");
282
            Object root = SwingUtilities.getRoot(this);
283
            if (root instanceof EditFrame) {
284
                EditFrame frame = (EditFrame) root;
285
                frame.getPanel().setAlwaysVisible(true);
286
            }
287
        }
288
        return idSaisieVF;
289
    }
290
 
291
    @Override
292
    public void update() {
293
 
294
        super.update();
295
        int id = getSelectedID();
296
        ((AbstractArticleItemTable) getEditor("sales.invoice.partial.items.list")).updateField("ID_SAISIE_VENTE_FACTURE", id);
297
 
298
    }
299
 
300
    @Override
301
    protected SQLRowValues createDefaults() {
302
        SQLRowValues rowVals = new SQLRowValues(getTable());
303
        rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(SaisieVenteFactureSQLElement.class, new Date()));
304
        rowVals.put("PARTIAL", Boolean.TRUE);
305
        return rowVals;
306
    }
307
 
308
    @Override
309
    public Component addView(JComponent comp, String id) {
310
        if (id.equals("sales.invoice.partial.amount")) {
311
            return super.addView(new AcompteRowItemView((AcompteField) comp), "MONTANT_FACTURABLE,POURCENT_FACTURABLE", REQ);
312
        } else {
313
            return super.addView(comp, id);
314
        }
315
    }
316
}