OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 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.finance.accounting.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.element.BaseSQLComponent;
20
import org.openconcerto.sql.element.SQLComponent;
80 ilm 21
import org.openconcerto.sql.element.SQLElement;
132 ilm 22
import org.openconcerto.sql.element.TreesOfSQLRows;
18 ilm 23
import org.openconcerto.sql.model.SQLBase;
24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.model.Where;
28
import org.openconcerto.sql.view.EditFrame;
29
import org.openconcerto.sql.view.EditPanel;
30
import org.openconcerto.ui.DefaultGridBagConstraints;
73 ilm 31
import org.openconcerto.utils.ExceptionHandler;
132 ilm 32
import org.openconcerto.utils.ListMap;
18 ilm 33
 
34
import java.awt.GridBagConstraints;
35
import java.awt.GridBagLayout;
36
import java.sql.SQLException;
37
import java.util.ArrayList;
38
import java.util.List;
39
 
40
import javax.swing.JLabel;
41
import javax.swing.JTextField;
42
 
43
import org.apache.commons.dbutils.handlers.ArrayListHandler;
44
 
45
// TODO Mettre le montant et la date du mouvement ???
46
/***************************************************************************************************
47
 * Un mouvement regroupe un ensemble d'ecritures liées dont le solde est nul
48
 **************************************************************************************************/
49
public class MouvementSQLElement extends ComptaSQLConfElement {
50
 
51
    private final static int MODIFICATION = 1;
52
    private final static int READONLY = 2;
53
 
54
    public MouvementSQLElement() {
55
        super("MOUVEMENT", "un mouvement", "mouvements");
56
    }
57
 
58
    @Override
59
    protected List<String> getListFields() {
60
        final List<String> list = new ArrayList<String>(3);
61
        list.add("NUMERO");
62
        list.add("ID_PIECE");
63
        list.add("SOURCE");
64
        return list;
65
    }
66
 
67
    @Override
68
    protected List<String> getComboFields() {
69
        final List<String> list = new ArrayList<String>(2);
70
        list.add("ID_PIECE");
71
        return list;
72
    }
73
 
74
    @Override
132 ilm 75
    public ListMap<String, String> getShowAs() {
76
        return ListMap.singleton(null, "NUMERO", "ID_PIECE");
18 ilm 77
    }
78
 
79
    @Override
80
    public SQLComponent createComponent() {
81
        return new BaseSQLComponent(this) {
82
 
83
            @Override
84
            public void addViews() {
85
                this.setLayout(new GridBagLayout());
86
                final GridBagConstraints c = new DefaultGridBagConstraints();
87
 
88
                final JLabel labelSource = new JLabel("Source ");
89
                this.add(labelSource, c);
90
 
91
                c.gridx++;
92
                final JTextField source = new JTextField();
93
                this.add(source, c);
94
 
95
                this.addSQLObject(source, "SOURCE");
96
            }
97
        };
98
    }
99
 
100
    @Override
132 ilm 101
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
102
        // TODO Auto-generated method stub
18 ilm 103
 
132 ilm 104
        for (SQLRow row : trees.getRows()) {
105
            // si le mouvement n'est pas archive, on le supprime avec sa source
106
            if (MouvementSQLElement.isEditable(row.getID())) {
107
                super.archive(new TreesOfSQLRows(this, row), cutLinks);
108
                final String source = row.getString("SOURCE");
109
                final int idSource = row.getInt("IDSOURCE");
18 ilm 110
 
132 ilm 111
                if (source.trim().length() > 0 && idSource > 1) {
112
                    Configuration.getInstance().getDirectory().getElement(source).archive(idSource);
113
                }
114
            } else {
115
                System.err.println("impossible d'arichiver le mouvement d'id [" + row.getID() + "] car il est validé.");
18 ilm 116
            }
117
        }
118
    }
119
 
120
    public static final int getSourceId(final int idMvt) {
121
        final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
122
        final SQLTable tableMvt = base.getTable("MOUVEMENT");
123
        final SQLRow row = tableMvt.getRow(idMvt);
124
        if (row.getInt("ID_MOUVEMENT_PERE") != 1) {
125
            return getSourceId(row.getInt("ID_MOUVEMENT_PERE"));
126
        } else {
127
            return idMvt;
128
        }
129
    }
130
 
131
    public static final void showSource(final int idMvt) {
132
 
133
        // on recupere le mouvement racine
134
        final int id = getSourceId(idMvt);
135
 
136
        if (id != 1) {
137
            final EditFrame f;
80 ilm 138
            final ComptaPropsConfiguration comptaPropsConfiguration = (ComptaPropsConfiguration) Configuration.getInstance();
139
            final SQLTable tableMvt = comptaPropsConfiguration.getRootSociete().getTable("MOUVEMENT");
18 ilm 140
            final String stringTableSource = tableMvt.getRow(id).getString("SOURCE").trim();
141
            final int mode = MouvementSQLElement.isEditable(id) ? MouvementSQLElement.MODIFICATION : MouvementSQLElement.READONLY;
142
 
143
            if (stringTableSource.length() != 0 && tableMvt.getRow(id).getInt("IDSOURCE") != 1) {
80 ilm 144
                final SQLElement elementSource = comptaPropsConfiguration.getDirectory().getElement(stringTableSource);
18 ilm 145
                if (mode == MouvementSQLElement.MODIFICATION) {
80 ilm 146
 
147
                    f = new EditFrame(elementSource, EditPanel.MODIFICATION);
18 ilm 148
                    f.getPanel().disableDelete();
149
                } else {
80 ilm 150
                    f = new EditFrame(elementSource, EditPanel.READONLY);
18 ilm 151
                }
152
                f.selectionId(tableMvt.getRow(id).getInt("IDSOURCE"));
153
            } else {
80 ilm 154
                final SQLElement elementSource = comptaPropsConfiguration.getDirectory().getElement(SaisieKmSQLElement.class);
18 ilm 155
                if (mode == MouvementSQLElement.MODIFICATION) {
80 ilm 156
                    f = new EditFrame(elementSource, EditPanel.MODIFICATION);
18 ilm 157
                } else {
80 ilm 158
                    f = new EditFrame(elementSource, EditPanel.READONLY);
18 ilm 159
                }
73 ilm 160
                // FIXME se passer de requete dans Swing...
161
                try {
162
                    f.selectionId(SaisieKmSQLElement.createSaisie(id));
163
                } catch (Exception e) {
164
                    ExceptionHandler.handle("Impossible de selectionner la source", e);
165
                }
18 ilm 166
            }
167
            f.pack();
168
            f.setVisible(true);
169
        } else {
170
            System.err.println("Aucun mouvement associé, impossible de modifier ou d'accéder à la source de cette ecriture!");
171
        }
172
    }
173
 
174
    public static final boolean isEditable(final int idMvt) {
175
 
176
        // TODO si l'id est incorrect(<=1) renvoyé false??
177
        final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
178
        final SQLTable tableEcriture = base.getTable("ECRITURE");
179
 
180
        final SQLSelect sel = new SQLSelect(base);
181
 
182
        sel.addSelect(tableEcriture.getField("VALIDE"));
183
 
184
        final Where w = new Where(tableEcriture.getField("ID_MOUVEMENT"), "=", idMvt);
185
 
186
        sel.setWhere(w);
187
 
188
        final String req = sel.asString();
189
        final Object ob = base.getDataSource().execute(req, new ArrayListHandler());
190
 
191
        final List myList = (List) ob;
192
 
193
        final int size = myList.size();
194
        for (int i = 0; i < size; i++) {
195
            final Object[] objTmp = (Object[]) myList.get(i);
196
            if (((Boolean) objTmp[0]).booleanValue()) {
197
                return false;
198
            }
199
        }
200
 
201
        return true;
202
    }
203
 
204
    public static final int getIDForSource(final String source, final int idSource) {
205
        final SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
206
        final SQLTable tableMvt = base.getTable("MOUVEMENT");
207
 
208
        final SQLSelect sel = new SQLSelect(base);
209
 
210
        sel.addSelect(tableMvt.getField("ID"));
211
 
212
        final Where w = new Where(tableMvt.getField("SOURCE"), "=", source);
213
        final Where w2 = new Where(tableMvt.getField("IDSOURCE"), "=", idSource);
214
 
215
        sel.setWhere(w.and(w2));
216
 
217
        final String req = sel.asString();
218
        final Object ob = base.getDataSource().execute(req, new ArrayListHandler());
219
 
220
        final List<Object[]> myList = (List<Object[]>) ob;
221
 
222
        final int id;
223
        if (myList.size() != 0) {
224
            final Object[] objTmp = myList.get(0);
225
            id = ((Number) objTmp[0]).intValue();
226
        } else {
227
            id = 1;
228
        }
229
        return id;
230
 
231
    }
57 ilm 232
 
233
    @Override
234
    protected String createCode() {
156 ilm 235
        return createCodeOfPackage() + ".entry";
57 ilm 236
    }
237
 
18 ilm 238
}