OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 156
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.finance.accounting.component;
14
 package org.openconcerto.erp.core.finance.accounting.component;
15
 
15
 
-
 
16
import org.openconcerto.erp.core.finance.accounting.model.CurrencyConverter;
16
import org.openconcerto.sql.element.GroupSQLComponent;
17
import org.openconcerto.sql.element.GroupSQLComponent;
17
import org.openconcerto.sql.element.SQLElement;
18
import org.openconcerto.sql.element.SQLElement;
-
 
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;
-
 
27
 
-
 
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;
18
 
36
 
19
public class DeviseHistoriqueSQLComponent extends GroupSQLComponent {
37
public class DeviseHistoriqueSQLComponent extends GroupSQLComponent {
20
 
38
 
21
    public static final String ID = "accounting.currencyrate.history";
39
    public static final String ID = "accounting.currencyrate.history";
22
 
40
 
23
    public DeviseHistoriqueSQLComponent(SQLElement element) {
41
    public DeviseHistoriqueSQLComponent(SQLElement element) {
24
        super(element);
42
        super(element);
25
    }
43
    }
26
 
44
 
-
 
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
 
27
}
166
}