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
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.ui;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
149 ilm 18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
18 ilm 19
import org.openconcerto.erp.model.ISQLCompteSelector;
20
import org.openconcerto.sql.Configuration;
21
import org.openconcerto.sql.model.SQLBase;
22
import org.openconcerto.sql.model.SQLRow;
73 ilm 23
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLTable;
156 ilm 26
import org.openconcerto.sql.request.SQLFieldTranslator;
149 ilm 27
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
18 ilm 28
import org.openconcerto.ui.DefaultGridBagConstraints;
29
import org.openconcerto.ui.preferences.DefaultPreferencePanel;
30
 
31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.sql.SQLException;
34
 
149 ilm 35
import javax.swing.JCheckBox;
18 ilm 36
import javax.swing.JLabel;
156 ilm 37
import javax.swing.JOptionPane;
18 ilm 38
import javax.swing.JPanel;
156 ilm 39
import javax.swing.SwingUtilities;
18 ilm 40
 
41
public class CompteCloturePreferencePanel extends DefaultPreferencePanel {
42
    private ISQLCompteSelector selCompteOuverture, selCompteFermeture, selCompteResultat, selCompteResultatPerte;
149 ilm 43
    private SQLRequestComboBox selJournal;
44
    private JCheckBox boxCompteSolde;
18 ilm 45
    private final static SQLBase base = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
46
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
47
    private SQLRowValues rowPrefCompteVals = new SQLRowValues(tablePrefCompte);
48
 
49
    public CompteCloturePreferencePanel() {
50
 
51
        final SQLRow rowPrefCompte = tablePrefCompte.getRow(2);
52
        this.rowPrefCompteVals.loadAbsolutelyAll(rowPrefCompte);
53
 
54
        this.setLayout(new GridBagLayout());
55
        final GridBagConstraints c = new DefaultGridBagConstraints();
56
 
57
        // Compte ouverture
58
        this.add(new JLabel("Compte bilan d'ouverture"), c);
59
        c.weightx = 1;
60
        c.gridx++;
61
        this.selCompteOuverture = new ISQLCompteSelector();
62
        this.selCompteOuverture.init();
63
        this.add(this.selCompteOuverture, c);
64
 
65
        // Compte fermeture
66
        c.gridy++;
67
        c.weightx = 0;
68
        c.gridx = 0;
69
        this.add(new JLabel("Compte bilan de clôture"), c);
70
        c.weightx = 1;
71
        c.gridx++;
72
        this.selCompteFermeture = new ISQLCompteSelector();
73
        this.selCompteFermeture.init();
74
        this.add(this.selCompteFermeture, c);
75
 
76
        // Compte résultat
77
        c.gridy++;
78
        c.gridx = 0;
79
        c.weightx = 0;
80
 
81
        this.add(new JLabel("Compte de résultat (bénéfice)"), c);
82
        c.weightx = 1;
83
        c.gridx++;
84
        this.selCompteResultat = new ISQLCompteSelector();
85
        this.selCompteResultat.init();
86
        this.add(this.selCompteResultat, c);
87
 
88
        // Compte résultat
89
        c.gridy++;
90
        c.gridx = 0;
91
        c.weightx = 0;
92
        this.add(new JLabel("Compte de résultat (perte)"), c);
93
        c.weightx = 1;
94
        c.gridx++;
95
        this.selCompteResultatPerte = new ISQLCompteSelector();
96
        this.selCompteResultatPerte.init();
97
        this.add(this.selCompteResultatPerte, c);
98
 
149 ilm 99
        // Compte résultat
100
        c.gridy++;
101
        c.gridx = 0;
102
        c.weightx = 0;
103
        this.add(new JLabel("Journal des à nouveaux"), c);
104
        c.weightx = 1;
105
        c.gridx++;
106
        this.selJournal = new SQLRequestComboBox(false);
107
        this.selJournal.uiInit(Configuration.getInstance().getDirectory().getElement("JOURNAL").createComboRequest());
108
        this.add(this.selJournal, c);
109
 
110
        // Compte résultat
111
        c.gridy++;
112
        c.gridx = 0;
113
        c.weightx = 0;
114
        this.add(new JLabel("Générer des écritures de fermetures et d'à nouveaux pour les comptes soldés"), c);
115
        c.weightx = 1;
116
        c.gridx++;
117
        this.boxCompteSolde = new JCheckBox();
118
        this.boxCompteSolde.setSelected(true);
119
        this.add(this.boxCompteSolde, c);
120
 
18 ilm 121
        // Spacer
122
 
123
        JPanel p = new JPanel();
124
        p.setOpaque(false);
125
        c.gridy++;
126
        c.weighty = 1;
127
        this.add(p, c);
128
        setValues();
129
    }
130
 
131
    public void storeValues() {
132
 
133
        this.rowPrefCompteVals.put("ID_COMPTE_PCE_BILAN_O", this.selCompteOuverture.getValue());
134
        this.rowPrefCompteVals.put("ID_COMPTE_PCE_BILAN_F", this.selCompteFermeture.getValue());
135
        this.rowPrefCompteVals.put("ID_COMPTE_PCE_RESULTAT", this.selCompteResultat.getValue());
174 ilm 136
        this.rowPrefCompteVals.put("ID_COMPTE_PCE_RESULTAT_PERTE", this.selCompteResultatPerte.getValue());
149 ilm 137
        this.rowPrefCompteVals.put("ID_JOURNAL_AN", this.selJournal.getValue());
138
        this.rowPrefCompteVals.put("CREATE_NUL_SOLDE_ECR", this.boxCompteSolde.isSelected());
18 ilm 139
 
140
        try {
156 ilm 141
            final Object[] pb = this.rowPrefCompteVals.getInvalid();
142
            if (pb != null) {
143
                final SQLFieldTranslator trans = Configuration.getInstance().getTranslator();
144
                JOptionPane.showMessageDialog(SwingUtilities.getRoot(this), "Impossible de valider les modifications! Le champ <"
145
                        + trans.getLabelFor(this.rowPrefCompteVals.getTable().getField(pb[0].toString())) + "> pointe sur un compte invalide!(" + pb[1] + ")");
146
            } else {
147
                this.rowPrefCompteVals.update();
148
            }
18 ilm 149
        } catch (SQLException e) {
150
            e.printStackTrace();
151
        }
152
    }
153
 
154
    public void restoreToDefaults() {
155
 
156
        try {
157
            // Ouverture
158
            String compte;
159
            compte = ComptePCESQLElement.getComptePceDefault("BilanOuverture");
160
 
161
            int value = ComptePCESQLElement.getId(compte);
162
            this.selCompteOuverture.setValue(value);
163
 
164
            // Fermeture
165
            compte = ComptePCESQLElement.getComptePceDefault("BilanFermeture");
166
            value = ComptePCESQLElement.getId(compte);
167
            this.selCompteFermeture.setValue(value);
168
 
169
            // Resultat
170
            compte = ComptePCESQLElement.getComptePceDefault("Resultat");
171
            value = ComptePCESQLElement.getId(compte);
172
            this.selCompteResultat.setValue(value);
173
 
174
            // Resultat
175
            compte = ComptePCESQLElement.getComptePceDefault("ResultatPerte");
176
            value = ComptePCESQLElement.getId(compte);
177
            this.selCompteResultatPerte.setValue(value);
178
 
149 ilm 179
            // AN
180
            this.selJournal.setValue(JournalSQLElement.OD);
181
 
182
            this.boxCompteSolde.setSelected(true);
183
 
18 ilm 184
        } catch (Exception e) {
185
            e.printStackTrace();
186
        }
187
    }
188
 
189
    public String getTitleName() {
190
        return "Clôture";
191
    }
192
 
193
    private void setValues() {
194
 
195
        try {
196
            // Ouverture
73 ilm 197
            SQLRowAccessor rowBilanO = this.rowPrefCompteVals.getForeign("ID_COMPTE_PCE_BILAN_O");
198
            int value = 1;
199
            if (rowBilanO == null || rowBilanO.isUndefined()) {
18 ilm 200
                String compte = ComptePCESQLElement.getComptePceDefault("BilanOuverture");
201
                value = ComptePCESQLElement.getId(compte);
73 ilm 202
            } else {
203
                value = rowBilanO.getID();
18 ilm 204
            }
205
            this.selCompteOuverture.setValue(value);
206
 
207
            // Fermeture
73 ilm 208
            SQLRowAccessor rowBilanF = this.rowPrefCompteVals.getForeign("ID_COMPTE_PCE_BILAN_F");
209
            if (rowBilanF == null || rowBilanF.isUndefined()) {
18 ilm 210
                String compte = ComptePCESQLElement.getComptePceDefault("BilanFermeture");
211
                value = ComptePCESQLElement.getId(compte);
73 ilm 212
            } else {
213
                value = rowBilanF.getID();
18 ilm 214
            }
215
            this.selCompteFermeture.setValue(value);
216
 
217
            // Resultat
73 ilm 218
            SQLRowAccessor rowResultat = this.rowPrefCompteVals.getForeign("ID_COMPTE_PCE_RESULTAT");
219
            if (rowResultat == null || rowResultat.isUndefined()) {
18 ilm 220
                String compte = ComptePCESQLElement.getComptePceDefault("Resultat");
221
                value = ComptePCESQLElement.getId(compte);
73 ilm 222
            } else {
223
                value = rowResultat.getID();
18 ilm 224
            }
225
            this.selCompteResultat.setValue(value);
226
 
227
            // Resultat Perte
73 ilm 228
            SQLRowAccessor rowResultatP = this.rowPrefCompteVals.getForeign("ID_COMPTE_PCE_RESULTAT_PERTE");
229
            if (rowResultatP == null || rowResultatP.isUndefined()) {
18 ilm 230
                String compte = ComptePCESQLElement.getComptePceDefault("ResultatPerte");
231
                value = ComptePCESQLElement.getId(compte);
73 ilm 232
            } else {
233
                value = rowResultatP.getID();
18 ilm 234
            }
235
            this.selCompteResultatPerte.setValue(value);
236
 
149 ilm 237
            // Journal
238
            SQLRowAccessor rowJ = this.rowPrefCompteVals.getForeign("ID_JOURNAL_AN");
239
            if (rowJ == null || rowJ.isUndefined()) {
240
                value = JournalSQLElement.OD;
241
            } else {
242
                value = rowJ.getID();
243
            }
244
            this.selJournal.setValue(value);
245
 
246
            // Journal
247
            boolean b = true;
248
            if (this.rowPrefCompteVals.getObject("CREATE_NUL_SOLDE_ECR") != null) {
249
                b = this.rowPrefCompteVals.getBoolean("CREATE_NUL_SOLDE_ECR");
250
            }
251
            this.boxCompteSolde.setSelected(b);
252
 
18 ilm 253
        } catch (Exception e) {
254
            e.printStackTrace();
255
        }
256
    }
257
}