OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 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.order.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
149 ilm 17
import org.openconcerto.erp.core.common.ui.DeviseTableCellRenderer;
144 ilm 18
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
19
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeCellRenderer;
20
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClient;
21
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClientComboBox;
22
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeRowItemView;
23
import org.openconcerto.sql.element.BaseSQLComponent;
24
import org.openconcerto.sql.element.SQLComponent;
25
import org.openconcerto.sql.element.UISQLComponent;
149 ilm 26
import org.openconcerto.sql.model.FieldPath;
144 ilm 27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowAccessor;
29
import org.openconcerto.sql.model.SQLRowValues;
180 ilm 30
import org.openconcerto.sql.model.SQLTable;
149 ilm 31
import org.openconcerto.sql.model.graph.Path;
144 ilm 32
import org.openconcerto.sql.view.EditFrame;
33
import org.openconcerto.sql.view.EditPanelListener;
149 ilm 34
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
144 ilm 35
import org.openconcerto.sql.view.list.IListe;
36
import org.openconcerto.sql.view.list.RowAction;
37
import org.openconcerto.sql.view.list.SQLTableModelSource;
149 ilm 38
import org.openconcerto.utils.CollectionUtils;
39
import org.openconcerto.utils.DecimalUtils;
144 ilm 40
import org.openconcerto.utils.ExceptionHandler;
41
 
42
import java.awt.event.ActionEvent;
149 ilm 43
import java.math.BigDecimal;
144 ilm 44
import java.sql.SQLException;
45
import java.util.ArrayList;
46
import java.util.Arrays;
180 ilm 47
import java.util.Collection;
144 ilm 48
import java.util.HashSet;
49
import java.util.List;
50
import java.util.Set;
51
 
52
import javax.swing.AbstractAction;
53
 
54
public class FacturationCommandeClientSQLElement extends ComptaSQLConfElement {
55
 
56
    public static final String TABLENAME = "FACTURATION_COMMANDE_CLIENT";
57
 
58
    public FacturationCommandeClientSQLElement() {
156 ilm 59
        super(TABLENAME, "un terme de facturation de commande client", "termes de facturation commandes clients");
144 ilm 60
        {
61
            RowAction action = new RowAction(new AbstractAction("Facturer") {
62
 
63
                @Override
64
                public void actionPerformed(ActionEvent e) {
65
                    final SQLRow sel = IListe.get(e).getSelectedRow().asRow();
66
                    sel.fetchValues();
67
                    CommandeClientSQLElement eltCmd = (CommandeClientSQLElement) getForeignElement("ID_COMMANDE_CLIENT");
68
                    int typeFacture = sel.getInt("TYPE_FACTURE");
69
                    final SQLRow asRow = sel.getForeign("ID_COMMANDE_CLIENT").asRow();
70
                    asRow.fetchValues();
71
                    List<SQLRowValues> l = Arrays.asList(asRow.asRowValues());
72
                    final EditFrame f;
73
                    if (typeFacture == TypeFactureCommandeClient.SITUATION.getId()) {
74
                        f = eltCmd.transfertAcompteClient(l);
75
                        SQLRowValues rowValues = new SQLRowValues(getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable());
76
                        rowValues.put("POURCENT_FACTURABLE", sel.getObject("POURCENT"));
77
                        rowValues.put("MONTANT_FACTURABLE", null);
78
                        ((BaseSQLComponent) f.getSQLComponent()).getView("MONTANT_FACTURABLE,POURCENT_FACTURABLE").show(rowValues);
79
                        ;
80
 
81
                    } else if (typeFacture == TypeFactureCommandeClient.SOLDE.getId()) {
82
                        f = eltCmd.transfertSoldeClient(l);
83
 
84
                    } else {
85
                        f = eltCmd.transfertFactureClient(l);
86
                    }
87
 
88
                    SQLRowValues rowValues = new SQLRowValues(eltCmd.getTable().getTable("MODE_REGLEMENT").getTable());
89
                    rowValues.put("ID_TYPE_REGLEMENT", sel.getForeignID("ID_TYPE_REGLEMENT"));
90
                    rowValues.put("FIN_MOIS", Boolean.FALSE);
91
                    if (sel.getBoolean("COMPTANT")) {
92
                        rowValues.put("AJOURS", 0);
93
                        rowValues.put("LENJOUR", 0);
94
                        rowValues.put("DATE_FACTURE", Boolean.TRUE);
95
                        rowValues.put("COMPTANT", Boolean.TRUE);
96
                    } else {
97
                        rowValues.put("AJOURS", sel.getObject("AJOURS"));
98
                        int lenjour = sel.getInt("LENJOUR");
99
                        if (sel.getBoolean("FIN_MOIS")) {
100
                            lenjour = 31;
101
                        }
102
                        rowValues.put("LENJOUR", lenjour);
103
                        rowValues.put("COMPTANT", Boolean.FALSE);
104
                        rowValues.put("FIN_MOIS", lenjour == 31);
105
                        rowValues.put("DATE_FACTURE", lenjour == 0);
106
                    }
107
 
108
                    ((BaseSQLComponent) f.getSQLComponent()).getView("ID_MODE_REGLEMENT").show(rowValues);
109
                    ;
110
 
111
                    f.addEditPanelListener(new EditPanelListener() {
112
 
113
                        @Override
114
                        public void modified() {
115
 
116
                        }
117
 
118
                        @Override
119
                        public void inserted(int id) {
120
                            try {
121
                                sel.createEmptyUpdateRow().put("ID_SAISIE_VENTE_FACTURE", id).commit();
180 ilm 122
                                SQLTable tableFacture = getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable();
123
                                SQLRowValues rowValsFact = new SQLRowValues(tableFacture);
124
                                rowValsFact.put(tableFacture.getKey().getName(), id);
125
                                rowValsFact.put("ID_FACTURATION_COMMANDE_CLIENT", sel.getID());
126
                                rowValsFact.commit();
144 ilm 127
                            } catch (SQLException e) {
128
                                ExceptionHandler.handle("Erreur lors de l'affectation de la facture", e);
129
                            }
130
                        }
131
 
132
                        @Override
133
                        public void deleted() {
134
                        }
135
 
136
                        @Override
137
                        public void cancelled() {
138
 
139
                        }
140
                    });
141
                }
142
            }, true) {
143
                @Override
144
                public boolean enabledFor(List<SQLRowValues> selection) {
145
 
146
                    if (selection != null && selection.size() == 1) {
147
                        SQLRowAccessor sel = selection.get(0);
148
                        return sel.isForeignEmpty("ID_SAISIE_VENTE_FACTURE");
149
                    }
150
                    return false;
151
                }
152
            };
153
 
154
            getRowActions().add(action);
155
        }
156
 
157
        {
158
            RowAction action = new RowAction(new AbstractAction("Voir la facture") {
159
 
160
                @Override
161
                public void actionPerformed(ActionEvent e) {
162
                    final SQLRowAccessor sel = IListe.get(e).getSelectedRow();
163
                    SQLRowAccessor fact = sel.getForeign("ID_SAISIE_VENTE_FACTURE");
164
                    VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(fact.asRow());
165
                    try {
166
                        sheet.showPreviewDocument();
167
                    } catch (Exception e1) {
168
                        // TODO Auto-generated catch block
169
                        e1.printStackTrace();
170
                    }
171
                }
172
            }, true) {
173
                @Override
174
                public boolean enabledFor(List<SQLRowValues> selection) {
175
 
176
                    if (selection != null && selection.size() == 1) {
177
                        SQLRowAccessor sel = selection.get(0);
178
                        return !sel.isForeignEmpty("ID_SAISIE_VENTE_FACTURE");
179
                    }
180
                    return false;
181
                }
182
            };
183
 
184
            getRowActions().add(action);
185
        }
186
    }
187
 
188
    /*
189
     * (non-Javadoc)
190
     *
191
     * @see org.openconcerto.devis.BaseSQLElement#getComboFields()
192
     */
193
    protected List<String> getComboFields() {
194
        final List<String> l = new ArrayList<>(1);
195
        l.add("NOM");
196
        return l;
197
    }
198
 
199
    @Override
200
    public Set<String> getReadOnlyFields() {
201
        Set<String> s = new HashSet<>(1);
202
        s.add("CHOICE");
203
        return s;
204
    }
205
 
206
    @Override
207
    protected String getParentFFName() {
208
        return "ID_COMMANDE_CLIENT";
209
    }
210
 
211
    /*
212
     * (non-Javadoc)
213
     *
214
     * @see org.openconcerto.devis.BaseSQLElement#getListFields()
215
     */
216
    protected List<String> getListFields() {
217
        final List<String> l = new ArrayList<>(7);
218
        l.add("NOM");
219
        l.add("TYPE_FACTURE");
220
        l.add("ID_TYPE_REGLEMENT");
221
        l.add("POURCENT");
222
        l.add("DATE_PREVISIONNELLE");
223
        l.add("ID_COMMANDE_CLIENT");
224
        l.add("ID_SAISIE_VENTE_FACTURE");
225
        return l;
226
    }
227
 
228
    @Override
229
    protected void _initTableSource(SQLTableModelSource res) {
230
        res.init();
149 ilm 231
        res.getColumn(getTable().getField("POURCENT")).setRenderer(new DeviseTableCellRenderer());
144 ilm 232
        res.getColumn(getTable().getField("TYPE_FACTURE")).setRenderer(new TypeFactureCommandeCellRenderer());
180 ilm 233
        final BaseSQLTableModelColumn pourcentFact = new BaseSQLTableModelColumn("Montant à facturer", BigDecimal.class) {
149 ilm 234
 
235
            @Override
236
            protected Object show_(SQLRowAccessor r) {
237
                long ht = r.getForeign("ID_COMMANDE_CLIENT").getLong("T_TTC");
238
                BigDecimal percent = r.getBigDecimal("POURCENT");
239
                return percent.movePointLeft(2).multiply(new BigDecimal(ht).movePointLeft(2), DecimalUtils.HIGH_PRECISION);
240
            }
241
 
242
            @Override
243
            public Set<FieldPath> getPaths() {
244
                Path p = new Path(getTable());
245
                Path p2 = p.add(p.getFirst().getField("ID_COMMANDE_CLIENT"));
246
                return CollectionUtils.createSet(new FieldPath(p, "POURCENT"), new FieldPath(p2, "T_TTC"));
247
            }
248
        };
249
        pourcentFact.setRenderer(new DeviseTableCellRenderer());
250
        res.getColumns().add(pourcentFact);
180 ilm 251
 
252
        final BaseSQLTableModelColumn montantFact = new BaseSQLTableModelColumn("Montant facturé", BigDecimal.class) {
253
 
254
            @Override
255
            protected Object show_(SQLRowAccessor r) {
256
                Collection<? extends SQLRowAccessor> rows = r.getReferentRows(getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable());
257
                long ht = 0;
258
                for (SQLRowAccessor sqlRowAccessor : rows) {
259
                    ht += sqlRowAccessor.getLong("T_TTC");
260
                }
261
 
262
                return new BigDecimal(ht).movePointLeft(2);
263
            }
264
 
265
            @Override
266
            public Set<FieldPath> getPaths() {
267
                Path p = new Path(getTable());
268
                Path p2 = p.add(p.getFirst().getField("ID_COMMANDE_CLIENT"));
269
                Path p3 = new Path(getTable());
270
                p3 = p3.add(getTable().getForeignTable("ID_SAISIE_VENTE_FACTURE").getField("ID_FACTURATION_COMMANDE_CLIENT"));
271
                return CollectionUtils.createSet(new FieldPath(p, "POURCENT"), new FieldPath(p2, "T_TTC"), new FieldPath(p3, "T_TTC"), new FieldPath(p2, "T_HT"), new FieldPath(p3, "T_HT"));
272
            }
273
        };
274
        montantFact.setRenderer(new DeviseTableCellRenderer());
275
        res.getColumns().add(montantFact);
144 ilm 276
        super._initTableSource(res);
277
    }
278
 
279
    /*
280
     * (non-Javadoc)
281
     *
282
     * @see org.openconcerto.devis.SQLElement#getComponent()
283
     */
284
    public SQLComponent createComponent() {
285
        return new UISQLComponent(this) {
286
            public void addViews() {
287
                this.addView("NOM");
288
                this.addView("POURCENT");
289
                this.addView("ID_TYPE_REGLEMENT");
290
                this.addView("AJOURS");
291
                this.addView("COMPTANT");
292
                this.addView("CHOICE");
293
 
294
                TypeFactureCommandeClientComboBox box = new TypeFactureCommandeClientComboBox();
295
 
296
                this.addView(new TypeFactureCommandeRowItemView(box), "TYPE_FACTURE", REQ);
297
                this.addView("ID_MODELE");
298
            }
299
 
300
            @Override
301
            protected SQLRowValues createDefaults() {
302
                SQLRowValues rowVals = new SQLRowValues(getTable());
303
                rowVals.put("CHOICE", Boolean.TRUE);
304
                return rowVals;
305
            }
306
        };
307
    }
308
 
309
    @Override
310
    protected String createCode() {
156 ilm 311
        return createCodeOfPackage() + ".facturation";
144 ilm 312
    }
313
 
314
}