OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Details | 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;
17
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureXmlSheet;
18
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeCellRenderer;
19
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClient;
20
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeClientComboBox;
21
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeRowItemView;
22
import org.openconcerto.sql.element.BaseSQLComponent;
23
import org.openconcerto.sql.element.SQLComponent;
24
import org.openconcerto.sql.element.UISQLComponent;
25
import org.openconcerto.sql.model.SQLRow;
26
import org.openconcerto.sql.model.SQLRowAccessor;
27
import org.openconcerto.sql.model.SQLRowValues;
28
import org.openconcerto.sql.view.EditFrame;
29
import org.openconcerto.sql.view.EditPanelListener;
30
import org.openconcerto.sql.view.list.IListe;
31
import org.openconcerto.sql.view.list.RowAction;
32
import org.openconcerto.sql.view.list.SQLTableModelSource;
33
import org.openconcerto.utils.ExceptionHandler;
34
 
35
import java.awt.event.ActionEvent;
36
import java.sql.SQLException;
37
import java.util.ArrayList;
38
import java.util.Arrays;
39
import java.util.HashSet;
40
import java.util.List;
41
import java.util.Set;
42
 
43
import javax.swing.AbstractAction;
44
 
45
public class FacturationCommandeClientSQLElement extends ComptaSQLConfElement {
46
 
47
    public static final String TABLENAME = "FACTURATION_COMMANDE_CLIENT";
48
 
49
    public FacturationCommandeClientSQLElement() {
50
        super(TABLENAME, "une terme de facturation de commande client", "termes de facturation commandes clients");
51
        {
52
            RowAction action = new RowAction(new AbstractAction("Facturer") {
53
 
54
                @Override
55
                public void actionPerformed(ActionEvent e) {
56
                    final SQLRow sel = IListe.get(e).getSelectedRow().asRow();
57
                    sel.fetchValues();
58
                    CommandeClientSQLElement eltCmd = (CommandeClientSQLElement) getForeignElement("ID_COMMANDE_CLIENT");
59
                    int typeFacture = sel.getInt("TYPE_FACTURE");
60
                    final SQLRow asRow = sel.getForeign("ID_COMMANDE_CLIENT").asRow();
61
                    asRow.fetchValues();
62
                    List<SQLRowValues> l = Arrays.asList(asRow.asRowValues());
63
                    final EditFrame f;
64
                    if (typeFacture == TypeFactureCommandeClient.SITUATION.getId()) {
65
                        f = eltCmd.transfertAcompteClient(l);
66
                        SQLRowValues rowValues = new SQLRowValues(getForeignElement("ID_SAISIE_VENTE_FACTURE").getTable());
67
                        rowValues.put("POURCENT_FACTURABLE", sel.getObject("POURCENT"));
68
                        rowValues.put("MONTANT_FACTURABLE", null);
69
                        ((BaseSQLComponent) f.getSQLComponent()).getView("MONTANT_FACTURABLE,POURCENT_FACTURABLE").show(rowValues);
70
                        ;
71
 
72
                    } else if (typeFacture == TypeFactureCommandeClient.SOLDE.getId()) {
73
                        f = eltCmd.transfertSoldeClient(l);
74
 
75
                    } else {
76
                        f = eltCmd.transfertFactureClient(l);
77
                    }
78
 
79
                    SQLRowValues rowValues = new SQLRowValues(eltCmd.getTable().getTable("MODE_REGLEMENT").getTable());
80
                    rowValues.put("ID_TYPE_REGLEMENT", sel.getForeignID("ID_TYPE_REGLEMENT"));
81
                    rowValues.put("FIN_MOIS", Boolean.FALSE);
82
                    if (sel.getBoolean("COMPTANT")) {
83
                        rowValues.put("AJOURS", 0);
84
                        rowValues.put("LENJOUR", 0);
85
                        rowValues.put("DATE_FACTURE", Boolean.TRUE);
86
                        rowValues.put("COMPTANT", Boolean.TRUE);
87
                    } else {
88
                        rowValues.put("AJOURS", sel.getObject("AJOURS"));
89
                        int lenjour = sel.getInt("LENJOUR");
90
                        if (sel.getBoolean("FIN_MOIS")) {
91
                            lenjour = 31;
92
                        }
93
                        rowValues.put("LENJOUR", lenjour);
94
                        rowValues.put("COMPTANT", Boolean.FALSE);
95
                        rowValues.put("FIN_MOIS", lenjour == 31);
96
                        rowValues.put("DATE_FACTURE", lenjour == 0);
97
                    }
98
 
99
                    ((BaseSQLComponent) f.getSQLComponent()).getView("ID_MODE_REGLEMENT").show(rowValues);
100
                    ;
101
 
102
                    f.addEditPanelListener(new EditPanelListener() {
103
 
104
                        @Override
105
                        public void modified() {
106
 
107
                        }
108
 
109
                        @Override
110
                        public void inserted(int id) {
111
                            try {
112
                                sel.createEmptyUpdateRow().put("ID_SAISIE_VENTE_FACTURE", id).commit();
113
                            } catch (SQLException e) {
114
                                ExceptionHandler.handle("Erreur lors de l'affectation de la facture", e);
115
                            }
116
                        }
117
 
118
                        @Override
119
                        public void deleted() {
120
                        }
121
 
122
                        @Override
123
                        public void cancelled() {
124
 
125
                        }
126
                    });
127
                }
128
            }, true) {
129
                @Override
130
                public boolean enabledFor(List<SQLRowValues> selection) {
131
 
132
                    if (selection != null && selection.size() == 1) {
133
                        SQLRowAccessor sel = selection.get(0);
134
                        return sel.isForeignEmpty("ID_SAISIE_VENTE_FACTURE");
135
                    }
136
                    return false;
137
                }
138
            };
139
 
140
            getRowActions().add(action);
141
        }
142
 
143
        {
144
            RowAction action = new RowAction(new AbstractAction("Voir la facture") {
145
 
146
                @Override
147
                public void actionPerformed(ActionEvent e) {
148
                    final SQLRowAccessor sel = IListe.get(e).getSelectedRow();
149
                    SQLRowAccessor fact = sel.getForeign("ID_SAISIE_VENTE_FACTURE");
150
                    VenteFactureXmlSheet sheet = new VenteFactureXmlSheet(fact.asRow());
151
                    try {
152
                        sheet.showPreviewDocument();
153
                    } catch (Exception e1) {
154
                        // TODO Auto-generated catch block
155
                        e1.printStackTrace();
156
                    }
157
                }
158
            }, true) {
159
                @Override
160
                public boolean enabledFor(List<SQLRowValues> selection) {
161
 
162
                    if (selection != null && selection.size() == 1) {
163
                        SQLRowAccessor sel = selection.get(0);
164
                        return !sel.isForeignEmpty("ID_SAISIE_VENTE_FACTURE");
165
                    }
166
                    return false;
167
                }
168
            };
169
 
170
            getRowActions().add(action);
171
        }
172
    }
173
 
174
    /*
175
     * (non-Javadoc)
176
     *
177
     * @see org.openconcerto.devis.BaseSQLElement#getComboFields()
178
     */
179
    protected List<String> getComboFields() {
180
        final List<String> l = new ArrayList<>(1);
181
        l.add("NOM");
182
        return l;
183
    }
184
 
185
    @Override
186
    public Set<String> getReadOnlyFields() {
187
        Set<String> s = new HashSet<>(1);
188
        s.add("CHOICE");
189
        return s;
190
    }
191
 
192
    @Override
193
    protected String getParentFFName() {
194
        return "ID_COMMANDE_CLIENT";
195
    }
196
 
197
    /*
198
     * (non-Javadoc)
199
     *
200
     * @see org.openconcerto.devis.BaseSQLElement#getListFields()
201
     */
202
    protected List<String> getListFields() {
203
        final List<String> l = new ArrayList<>(7);
204
        l.add("NOM");
205
        l.add("TYPE_FACTURE");
206
        l.add("ID_TYPE_REGLEMENT");
207
        l.add("POURCENT");
208
        l.add("DATE_PREVISIONNELLE");
209
        l.add("ID_COMMANDE_CLIENT");
210
        l.add("ID_SAISIE_VENTE_FACTURE");
211
        return l;
212
    }
213
 
214
    @Override
215
    protected void _initTableSource(SQLTableModelSource res) {
216
        res.init();
217
        res.getColumn(getTable().getField("TYPE_FACTURE")).setRenderer(new TypeFactureCommandeCellRenderer());
218
        super._initTableSource(res);
219
    }
220
 
221
    /*
222
     * (non-Javadoc)
223
     *
224
     * @see org.openconcerto.devis.SQLElement#getComponent()
225
     */
226
    public SQLComponent createComponent() {
227
        return new UISQLComponent(this) {
228
            public void addViews() {
229
                this.addView("NOM");
230
                this.addView("POURCENT");
231
                this.addView("ID_TYPE_REGLEMENT");
232
                this.addView("AJOURS");
233
                this.addView("COMPTANT");
234
                this.addView("CHOICE");
235
 
236
                TypeFactureCommandeClientComboBox box = new TypeFactureCommandeClientComboBox();
237
 
238
                this.addView(new TypeFactureCommandeRowItemView(box), "TYPE_FACTURE", REQ);
239
                this.addView("ID_MODELE");
240
            }
241
 
242
            @Override
243
            protected SQLRowValues createDefaults() {
244
                SQLRowValues rowVals = new SQLRowValues(getTable());
245
                rowVals.put("CHOICE", Boolean.TRUE);
246
                return rowVals;
247
            }
248
        };
249
    }
250
 
251
    @Override
252
    protected String createCode() {
253
        return createCodeFromPackage() + ".facturation";
254
    }
255
 
256
}