OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
75 ilm 1
package org.openconcerto.modules.project.element;
2
 
3
import java.sql.SQLException;
4
import java.util.ArrayList;
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Set;
8
 
9
import javax.swing.SwingUtilities;
10
 
170 ilm 11
import org.openconcerto.erp.config.ComptaPropsConfiguration;
75 ilm 12
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
13
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
14
import org.openconcerto.sql.Configuration;
15
import org.openconcerto.sql.element.SQLComponent;
16
import org.openconcerto.sql.element.UISQLComponent;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.SQLRowValues;
170 ilm 20
import org.openconcerto.sql.model.SQLSelect;
75 ilm 21
import org.openconcerto.sql.model.SQLTable;
170 ilm 22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.request.ComboSQLRequest;
75 ilm 24
import org.openconcerto.sql.sqlobject.ElementComboBox;
25
import org.openconcerto.sql.sqlobject.JUniqueTextField;
26
import org.openconcerto.sql.view.EditFrame;
27
import org.openconcerto.ui.JDate;
28
import org.openconcerto.ui.component.ITextArea;
29
import org.openconcerto.utils.ExceptionHandler;
128 ilm 30
import org.openconcerto.utils.ListMap;
170 ilm 31
import org.openconcerto.utils.cc.ITransformer;
75 ilm 32
 
33
public class ProjectSQLElement extends ComptaSQLConfElement {
34
 
35
    public ProjectSQLElement() {
36
        super("AFFAIRE", "une affaire", "affaires");
37
    }
38
 
39
    @Override
40
    protected List<String> getListFields() {
41
        final List<String> l = new ArrayList<String>();
42
        l.add("ID_CLIENT");
170 ilm 43
        if (getTable().contains("ID_SOCIETE_COMMON")) {
44
            l.add("ID_SOCIETE_COMMON");
45
        }
75 ilm 46
        l.add("ID_TYPE_AFFAIRE");
47
        l.add("NUMERO");
48
        l.add("ID_ETAT_AFFAIRE");
49
        l.add("ID_COMMERCIAL");
50
        l.add("INFOS");
51
        return l;
52
    }
53
 
170 ilm 54
    protected void _initComboRequest(ComboSQLRequest req) {
55
        super._initComboRequest(req);
56
        if (getTable().contains("ID_SOCIETE_COMMON")) {
57
            final ComptaPropsConfiguration comptaPropsConfiguration = ((ComptaPropsConfiguration) Configuration.getInstance());
58
            final int socID = comptaPropsConfiguration.getSocieteID();
59
            req.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
60
 
61
                @Override
62
                public SQLSelect transformChecked(SQLSelect input) {
63
                    Where w = new Where(getTable().getField("ID_SOCIETE_COMMON"), "=", (Object) null).or(new Where(getTable().getField("ID_SOCIETE_COMMON"), "=", 1))
64
                            .or(new Where(getTable().getField("ID_SOCIETE_COMMON"), "=", socID));
65
 
66
                    input.andWhere(w);
67
                    return input;
68
                }
69
            });
70
        }
71
    }
72
 
75 ilm 73
    @Override
74
    protected List<String> getComboFields() {
75
        final List<String> l = new ArrayList<String>();
76
        l.add("NUMERO");
77
        l.add("ID_CLIENT");
78
        return l;
79
    }
80
 
81
    @Override
128 ilm 82
    public ListMap<String, String> getShowAs() {
83
        return ListMap.singleton(null, getComboFields());
75 ilm 84
    }
85
 
86
    @Override
87
    public Set<String> getInsertOnlyFields() {
88
        Set<String> s = new HashSet<String>(1);
89
        s.add("ID_DEVIS");
90
        return s;
91
    }
92
 
93
    @Override
94
    public SQLComponent createComponent() {
95
        return new UISQLComponent(this, 2) {
96
 
97
            final JUniqueTextField field = new JUniqueTextField();
98
 
99
            @Override
100
            protected void addViews() {
101
 
102
                this.addView(this.field, "NUMERO", "1");
103
                this.addView(new JDate(true), "DATE", "1");
104
                this.addView("ID_CLIENT", "1;" + REQ);
105
                this.addView("ID_COMMERCIAL", "1");
106
                this.addView("ID_DEVIS", "1");
170 ilm 107
                if (getTable().contains("ID_SOCIETE_COMMON")) {
108
                    this.addView("ID_SOCIETE_COMMON", "1");
109
                }
75 ilm 110
                this.addView("ID_TYPE_AFFAIRE", "1;left");
111
                final ElementComboBox boxEtatAffaire = new ElementComboBox();
112
                this.addView(boxEtatAffaire, "ID_ETAT_AFFAIRE", "1;left;" + REQ);
113
                this.addView(new ITextArea(), "INFOS", "2");
114
 
115
            }
116
 
117
            @Override
118
            public void select(SQLRowAccessor r) {
119
                super.select(r);
120
                if (r != null) {
121
                    this.field.setIdSelected(r.getID());
122
                }
123
            }
124
 
125
            @Override
126
            public void update() {
127
                if (!this.field.checkValidation()) {
128
                    ExceptionHandler.handle("Impossible d'ajouter, numéro d'affaire existant.");
129
                    Object root = SwingUtilities.getRoot(this);
130
                    if (root instanceof EditFrame) {
131
                        EditFrame frame = (EditFrame) root;
132
                        frame.getPanel().setAlwaysVisible(true);
133
                    }
134
                    return;
135
                }
136
                super.update();
137
            }
138
 
139
            @Override
140
            public int insert(SQLRow order) {
141
 
142
                int idCommande = getSelectedID();
143
 
144
                // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
145
                if (this.field.checkValidation()) {
146
 
147
                    idCommande = super.insert(order);
148
                    // incrémentation du numéro auto
149
                    if (NumerotationAutoSQLElement.getNextNumero(ProjectSQLElement.class).equalsIgnoreCase(this.field.getText().trim())) {
150
                        SQLTable tableNum = Configuration.getInstance().getRoot().findTable("NUMEROTATION_AUTO");
151
                        SQLRowValues rowVals = new SQLRowValues(tableNum);
152
                        int val = tableNum.getRow(2).getInt("AFFAIRE_START");
153
                        val++;
154
                        rowVals.put("AFFAIRE_START", new Integer(val));
155
 
156
                        try {
157
                            rowVals.update(2);
158
                        } catch (SQLException e) {
159
                            e.printStackTrace();
160
                        }
161
                    }
162
 
163
                } else {
164
                    ExceptionHandler.handle("Impossible d'ajouter, numéro d'affaire existant.");
165
                    Object root = SwingUtilities.getRoot(this);
166
                    if (root instanceof EditFrame) {
167
                        EditFrame frame = (EditFrame) root;
168
                        frame.getPanel().setAlwaysVisible(true);
169
                    }
170
                }
171
                SQLRow row = getTable().getRow(idCommande);
172
                SQLRow rowDevis = row.getForeign("ID_DEVIS");
173
                if (rowDevis != null && !rowDevis.isUndefined()) {
174
                    SQLRowValues rowVals = rowDevis.asRowValues();
175
                    rowVals.put("ID_AFFAIRE", idCommande);
176
                    try {
177
                        rowVals.update();
178
                    } catch (SQLException exn) {
179
                        // TODO Bloc catch auto-généré
180
                        exn.printStackTrace();
181
                    }
182
                }
183
                return idCommande;
184
            }
185
 
186
            @Override
187
            protected SQLRowValues createDefaults() {
188
                SQLRowValues rowVals = new SQLRowValues(getTable());
189
                rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(ProjectSQLElement.class));
190
                rowVals.put("ID_ETAT_AFFAIRE", ProjectStateSQLElement.EN_COURS);
191
                return rowVals;
192
            }
193
        };
194
 
195
    }
196
}