OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 76 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
76 ilm 1
/*
2
 * Créé le 1 juin 2012
3
 */
4
package org.openconcerto.modules.subscription.element;
5
 
6
import java.awt.Component;
7
import java.awt.GridBagConstraints;
8
import java.awt.GridBagLayout;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
96 ilm 11
import java.sql.SQLException;
12
import java.util.Date;
76 ilm 13
 
14
import javax.swing.JCheckBox;
15
import javax.swing.JComponent;
16
import javax.swing.JLabel;
17
import javax.swing.JPanel;
18
import javax.swing.JTabbedPane;
19
import javax.swing.JTextField;
20
import javax.swing.SwingConstants;
21
 
96 ilm 22
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
23
import org.openconcerto.erp.core.sales.quote.element.DevisSQLElement;
76 ilm 24
import org.openconcerto.sql.element.BaseSQLComponent;
25
import org.openconcerto.sql.element.SQLElement;
96 ilm 26
import org.openconcerto.sql.model.SQLRow;
27
import org.openconcerto.sql.model.SQLRowAccessor;
28
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLTable;
76 ilm 30
import org.openconcerto.sql.sqlobject.ElementComboBox;
96 ilm 31
import org.openconcerto.sql.sqlobject.JUniqueTextField;
76 ilm 32
import org.openconcerto.ui.DefaultGridBagConstraints;
33
import org.openconcerto.ui.JDate;
34
import org.openconcerto.ui.component.ITextArea;
35
 
36
public class SubscriptionSQLComponent extends BaseSQLComponent {
37
 
38
    private static final long serialVersionUID = 4274010869219769289L;
96 ilm 39
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
76 ilm 40
 
96 ilm 41
    final JLabel labelNumero = new JLabel(getLabelFor("NUMERO"), SwingConstants.RIGHT);
42
    final JLabel labelDate = new JLabel(getLabelFor("DATE"), SwingConstants.RIGHT);
43
    final JDate date = new JDate(true);
44
    final JLabel labelClient = new JLabel(getLabelFor("ID_CLIENT"), SwingConstants.RIGHT);
45
    final ElementComboBox client = new ElementComboBox();
46
 
76 ilm 47
    public SubscriptionSQLComponent(SQLElement element) {
48
        super(element);
49
    }
50
 
96 ilm 51
    public void setLightUI(boolean b) {
52
        labelClient.setVisible(b);
53
        labelNumero.setVisible(b);
54
        labelDate.setVisible(b);
55
        textNumero.setVisible(b);
56
 
57
        date.setVisible(b);
58
        client.setVisible(b);
59
    }
60
 
61
    final JUniqueTextField textNumero = new JUniqueTextField(8);
62
 
76 ilm 63
    @Override
64
    public void addViews() {
65
 
66
        this.setLayout(new GridBagLayout());
67
        final GridBagConstraints c = new DefaultGridBagConstraints();
68
        // Numéro
69
        c.weightx = 0;
96 ilm 70
 
71
        this.add(labelNumero, c);
76 ilm 72
        c.gridx++;
73
        c.weightx = 1;
74
        this.add(textNumero, c);
96 ilm 75
 
76 ilm 76
        // Date
77
        c.gridx++;
78
        c.weightx = 0;
96 ilm 79
        this.add(labelDate, c);
76 ilm 80
        c.gridx++;
81
        c.weightx = 1;
96 ilm 82
 
76 ilm 83
        this.add(date, c);
84
        // Libellé
85
        c.gridy++;
86
        c.gridx = 0;
87
        c.weightx = 0;
88
        this.add(new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT), c);
89
        c.gridx++;
90
        c.weightx = 1;
91
        c.gridwidth = 3;
92
        final JTextField textNom = new JTextField();
93
        this.add(textNom, c);
94
 
95
        // Libellé
96
        c.gridy++;
97
        c.gridx = 0;
98
        c.weightx = 0;
99
        c.gridwidth = 1;
100
        this.add(new JLabel(getLabelFor("INTITULE_FACTURE"), SwingConstants.RIGHT), c);
101
        c.gridx++;
102
        c.weightx = 1;
103
        c.gridwidth = 3;
104
        final JTextField intitule = new JTextField();
105
        this.add(intitule, c);
106
        this.addView(intitule, "INTITULE_FACTURE");
107
 
108
        // Description
109
        c.gridwidth = 1;
110
        c.gridy++;
111
        c.gridx = 0;
112
        c.weightx = 0;
113
        this.add(new JLabel(getLabelFor("DESCRIPTION"), SwingConstants.RIGHT), c);
114
        c.gridx++;
115
        c.weightx = 1;
116
        c.gridwidth = 3;
117
        final ITextArea textDescription = new ITextArea();
118
        this.add(textDescription, c);
119
 
120
        // Client
121
        c.gridwidth = 1;
122
        c.gridy++;
123
        c.gridx = 0;
124
        c.weightx = 0;
96 ilm 125
        this.add(labelClient, c);
76 ilm 126
        c.gridx++;
127
        c.weightx = 1;
128
        c.gridwidth = 3;
96 ilm 129
 
76 ilm 130
        this.add(client, c);
131
 
132
        //
133
        c.gridwidth = 4;
134
        c.gridx = 0;
135
        c.gridy++;
136
 
137
        c.weightx = 1;
138
        c.weighty = 1;
139
 
140
        final JTabbedPane tabbedPane = new JTabbedPane();
141
        tabbedPane.add("Facture", createTypeAboComponent("FACTURE", "ID_SAISIE_VENTE_FACTURE"));
142
        tabbedPane.add("Devis", createTypeAboComponent("DEVIS", "ID_DEVIS"));
143
        tabbedPane.add("Bon de commande", createTypeAboComponent("COMMANDE", "ID_COMMANDE_CLIENT"));
144
 
145
        this.add(tabbedPane, c);
146
 
147
        this.addView(textNumero, "NUMERO", REQ);
148
        this.addView(date, "DATE");
149
        this.addView(textNom, "NOM");
150
        this.addView(textDescription, "DESCRIPTION");
151
        this.addView(client, "ID_CLIENT");
152
 
153
        // Codé mais jamais lancé.. a verifier: nom des champs
154
    }
155
 
156
    private Component createTypeAboComponent(String type, String idName) {
157
        final String fieldCreate = "CREATE_" + type;
158
        final String fieldStart = "DATE_DEBUT_" + type;
159
        final String fieldStop = "DATE_FIN_" + type;
160
        final String fieldPeriodicity = "NB_MOIS_" + type;
161
 
162
        final JPanel panel = new JPanel();
163
        panel.setOpaque(false);
164
 
165
        panel.setLayout(new GridBagLayout());
166
        final GridBagConstraints c = new DefaultGridBagConstraints();
167
 
168
        // Checkbox
169
        c.gridx = 1;
170
        c.weightx = 1;
171
        final JCheckBox check = new JCheckBox(getLabelFor(fieldCreate));
172
        check.setOpaque(false);
173
        panel.add(check, c);
174
 
175
        // Item
176
        c.gridx = 0;
177
        c.gridy++;
178
        c.weightx = 0;
179
        panel.add(new JLabel(getLabelFor(idName), SwingConstants.RIGHT), c);
180
        c.gridx++;
181
        c.weightx = 1;
182
        final ElementComboBox item = new ElementComboBox();
183
        // SQLElement elt =
184
        // Configuration.getInstance().getDirectory().getElement(getTable().getForeignTable(idName));
185
        // ComboSQLRequest req = new ComboSQLRequest(elt.getComboRequest(true),
186
        // Arrays.asList(elt.getTable().getField("NUMERO"), elt.getTable().getField("ID_CLIENT")));
187
        // item.init(elt, req);
188
        panel.add(item, c);
189
 
190
        // Start
191
        c.gridx = 0;
192
        c.gridy++;
193
        c.weightx = 0;
194
        c.fill = GridBagConstraints.HORIZONTAL;
195
        panel.add(new JLabel(getLabelFor(fieldStart), SwingConstants.RIGHT), c);
196
        c.gridx++;
197
        c.weightx = 1;
198
        c.fill = GridBagConstraints.NONE;
199
        final JDate startDate = new JDate(true);
200
        panel.add(startDate, c);
201
 
202
        // Stop
203
        c.gridx = 0;
204
        c.gridy++;
205
        c.weightx = 0;
206
        c.fill = GridBagConstraints.HORIZONTAL;
207
        panel.add(new JLabel(getLabelFor(fieldStop), SwingConstants.RIGHT), c);
208
        c.gridx++;
209
        c.weightx = 1;
96 ilm 210
        final JDate stopDate = new JDate();
76 ilm 211
        c.fill = GridBagConstraints.NONE;
212
        panel.add(stopDate, c);
213
 
214
        // Periodicity
215
        c.gridx = 0;
216
        c.gridy++;
217
        c.weightx = 0;
218
        c.fill = GridBagConstraints.HORIZONTAL;
219
        panel.add(new JLabel(getLabelFor(fieldPeriodicity), SwingConstants.RIGHT), c);
220
        c.gridx++;
221
        c.weightx = 1;
222
        c.fill = GridBagConstraints.NONE;
223
        final JTextField textPeriod = new JTextField(5);
224
        panel.add(textPeriod, c);
225
 
226
        check.addActionListener(new ActionListener() {
227
 
228
            @Override
229
            public void actionPerformed(ActionEvent e) {
230
                setFieldEnabled(item, startDate, stopDate, textPeriod, check.isSelected());
231
            }
232
        });
233
 
234
        this.addView(check, fieldCreate);
235
        this.addView(item, idName);
236
        this.addView(startDate, fieldStart);
237
        this.addView(stopDate, fieldStop);
238
        this.addView(textPeriod, fieldPeriodicity);
239
 
240
        setFieldEnabled(item, startDate, stopDate, textPeriod, false);
241
        return panel;
242
    }
243
 
244
    // @Override
245
    // protected SQLRowValues createDefaults() {
246
    // SQLRowValues rowVals = new SQLRowValues(getTable());
247
    // return super.createDefaults();
248
    // }
249
 
250
    public void setFieldEnabled(JComponent item, JComponent startDate, JComponent stopDate, JComponent textPeriod, boolean b) {
251
        System.err.println(b);
96 ilm 252
        // Thread.dumpStack();
76 ilm 253
        item.setEnabled(b);
254
        startDate.setEnabled(b);
255
        stopDate.setEnabled(b);
256
        textPeriod.setEnabled(b);
257
    }
96 ilm 258
 
259
    @Override
260
    protected SQLRowValues createDefaults() {
261
        SQLRowValues vals = new SQLRowValues(getTable());
262
        vals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(this.getElement().getClass(), new Date()));
263
        return vals;
264
    }
265
 
266
    @Override
267
    public void select(SQLRowAccessor r) {
268
        // TODO Auto-generated method stub
269
        super.select(r);
270
        if (r != null) {
271
            this.textNumero.setIdSelected(r.getID());
272
        }
273
    }
274
 
275
    @Override
276
    public int insert(SQLRow order) {
277
        // TODO Auto-generated method stub
278
        int id = super.insert(order);
279
        // incrémentation du numéro auto
280
        if (NumerotationAutoSQLElement.getNextNumero(DevisSQLElement.class).equalsIgnoreCase(this.textNumero.getText().trim())) {
281
            final SQLRowValues rowVals = new SQLRowValues(this.tableNum);
282
            int val = this.tableNum.getRow(2).getInt(NumerotationAutoSQLElement.getLabelNumberFor(DevisSQLElement.class));
283
            val++;
284
            rowVals.put(NumerotationAutoSQLElement.getLabelNumberFor(DevisSQLElement.class), new Integer(val));
285
            try {
286
                rowVals.update(2);
287
            } catch (final SQLException e) {
288
                e.printStackTrace();
289
            }
290
        }
291
        return id;
292
    }
293
 
294
    @Override
295
    public void update() {
296
        // TODO Auto-generated method stub
297
        super.update();
298
    }
76 ilm 299
}