OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
93 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.component;
15
 
156 ilm 16
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
93 ilm 17
import org.openconcerto.sql.element.GroupSQLComponent;
18
import org.openconcerto.sql.element.SQLElement;
156 ilm 19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
22
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLSelectJoin;
24
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.utils.ExceptionHandler;
26
import org.openconcerto.utils.cc.ITransformer;
93 ilm 27
 
156 ilm 28
import java.math.BigDecimal;
29
import java.math.RoundingMode;
30
import java.sql.SQLException;
31
import java.util.Date;
32
import java.util.List;
33
 
34
import javax.swing.JOptionPane;
35
import javax.swing.SwingWorker;
36
 
93 ilm 37
public class DeviseHistoriqueSQLComponent extends GroupSQLComponent {
38
 
39
    public static final String ID = "accounting.currencyrate.history";
40
 
41
    public DeviseHistoriqueSQLComponent(SQLElement element) {
42
        super(element);
43
    }
44
 
156 ilm 45
    @Override
46
    public int insert(SQLRow order) {
47
        int id = super.insert(order);
48
        SQLRow row = getTable().getRow(id);
49
        updateProduct(row);
50
        return id;
51
    }
52
 
53
    @Override
54
    public void update() {
55
        super.update();
56
        SQLRow row = getTable().getRow(getSelectedID());
57
        updateProduct(row);
58
    }
59
 
60
    @Override
61
    protected SQLRowValues createDefaults() {
62
        CurrencyConverter c = new CurrencyConverter();
63
        String companyCode = c.getCompanyCurrencyCode();
64
        SQLRowValues rowVals = new SQLRowValues(getTable());
65
        rowVals.put("SRC", companyCode);
66
        return rowVals;
67
    }
68
 
69
    private void updateProduct(SQLRow rowDeviseH) {
70
        CurrencyConverter c = new CurrencyConverter();
71
        String companyCode = c.getCompanyCurrencyCode();
72
        final String srcCode = rowDeviseH.getString("SRC");
73
        if (srcCode.equalsIgnoreCase(companyCode)) {
74
            int ans = JOptionPane.showConfirmDialog(null, "Voulez vous actualiser les prix d'achats?", "Actualisation tarif", JOptionPane.YES_NO_OPTION);
75
 
76
            if (ans == JOptionPane.YES_OPTION) {
77
                SwingWorker<Object, Object> w = new SwingWorker<Object, Object>() {
78
                    @Override
79
                    protected Object doInBackground() throws Exception {
80
                        final String codeDevise = rowDeviseH.getString("DST");
81
                        SQLRowValues rowValsARt = new SQLRowValues(getTable().getTable("ARTICLE"));
82
                        rowValsARt.putNulls("PA_HT", "PA_DEVISE");
83
                        rowValsARt.putRowValues("ID_DEVISE_HA").putNulls("CODE");
84
                        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsARt);
85
                        fetcher.addSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
86
 
87
                            @Override
88
                            public SQLSelect transformChecked(SQLSelect input) {
89
                                SQLSelectJoin join = input.getJoin(getTable().getTable("ARTICLE").getField("ID_DEVISE_HA"));
90
                                input.setWhere(new Where(join.getJoinedTable().getField("CODE"), "=", codeDevise));
91
                                return input;
92
                            }
93
                        }, 0);
94
 
95
                        List<SQLRowValues> result = fetcher.fetch();
96
                        final Date date = new Date();
97
                        for (SQLRowValues sqRowValues : result) {
98
 
99
                            BigDecimal haDevise = sqRowValues.getBigDecimal("PA_DEVISE");
100
                            if (haDevise == null) {
101
                                haDevise = BigDecimal.ZERO;
102
                            }
103
 
104
                            final BigDecimal convert = c.convert(haDevise, codeDevise, companyCode, date, true).setScale(sqRowValues.getTable().getField("PA_DEVISE").getType().getDecimalDigits(),
105
                                    RoundingMode.HALF_UP);
106
                            try {
107
                                sqRowValues.createEmptyUpdateRow().put("PA_HT", convert).put("PRIX_METRIQUE_HA_1", convert).commit();
108
                            } catch (SQLException e) {
109
                                e.printStackTrace();
110
                            }
111
 
112
                        }
113
 
114
                        // ARTICLE_FOURNISSEUR
115
                        SQLRowValues rowValsARtFour = new SQLRowValues(getTable().getTable("ARTICLE_FOURNISSEUR"));
116
                        rowValsARtFour.putNulls("PA_HT", "PA_DEVISE");
117
                        rowValsARtFour.putRowValues("ID_DEVISE_HA").putNulls("CODE");
118
                        SQLRowValuesListFetcher fetcherFourni = SQLRowValuesListFetcher.create(rowValsARtFour);
119
                        fetcherFourni.addSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
120
 
121
                            @Override
122
                            public SQLSelect transformChecked(SQLSelect input) {
123
                                SQLSelectJoin join = input.getJoin(getTable().getTable("ARTICLE_FOURNISSEUR").getField("ID_DEVISE_HA"));
124
                                input.setWhere(new Where(join.getJoinedTable().getField("CODE"), "=", codeDevise));
125
                                return input;
126
                            }
127
                        }, 0);
128
 
129
                        List<SQLRowValues> resultFournisseur = fetcherFourni.fetch();
130
 
131
                        for (SQLRowValues sqRowValues : resultFournisseur) {
132
 
133
                            BigDecimal haDevise = sqRowValues.getBigDecimal("PA_DEVISE");
134
                            if (haDevise == null) {
135
                                haDevise = BigDecimal.ZERO;
136
                            }
137
 
138
                            final BigDecimal convert = c.convert(haDevise, codeDevise, companyCode, date, true).setScale(sqRowValues.getTable().getField("PA_DEVISE").getType().getDecimalDigits(),
139
                                    RoundingMode.HALF_UP);
140
                            try {
141
                                sqRowValues.createEmptyUpdateRow().put("PA_HT", convert).put("PRIX_METRIQUE_HA_1", convert).commit();
142
                            } catch (SQLException e) {
143
                                e.printStackTrace();
144
                            }
145
 
146
                        }
147
                        return null;
148
                    }
149
 
150
                    @Override
151
                    protected void done() {
152
                        try {
153
                            get();
154
                            JOptionPane.showMessageDialog(null, "Mise à jour des tarifs terminées", "Mise à jour tarifs", JOptionPane.INFORMATION_MESSAGE);
155
                        } catch (Exception e) {
156
                            ExceptionHandler.handle("Erreur lors de la mise à jour des tarifs!", e);
157
                        }
158
                    }
159
                };
160
                w.run();
161
            }
162
        }
163
 
164
    }
165
 
93 ilm 166
}