OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
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.supplychain.stock.element;
14
 package org.openconcerto.erp.core.supplychain.stock.element;
15
 
15
 
16
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
16
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
17
import org.openconcerto.sql.model.SQLInjector;
17
import org.openconcerto.sql.model.SQLInjector;
18
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRow;
19
import org.openconcerto.sql.model.SQLRowAccessor;
19
import org.openconcerto.sql.model.SQLRowAccessor;
20
import org.openconcerto.sql.model.SQLRowValues;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.Where;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.preferences.SQLPreferences;
23
import org.openconcerto.sql.preferences.SQLPreferences;
24
import org.openconcerto.sql.request.UpdateBuilder;
24
import org.openconcerto.sql.request.UpdateBuilder;
25
import org.openconcerto.utils.ListMap;
25
import org.openconcerto.utils.ListMap;
26
 
26
 
27
import java.math.BigDecimal;
27
import java.math.BigDecimal;
28
import java.util.ArrayList;
28
import java.util.ArrayList;
29
import java.util.List;
29
import java.util.List;
30
 
30
 
31
/**
31
/**
32
 * Représente un article avec son stock
32
 * Représente un article avec son stock
33
 * 
33
 * 
34
 * @author Utilisateur
34
 * @author Utilisateur
35
 * 
35
 * 
36
 */
36
 */
37
public class StockItem {
37
public class StockItem {
38
 
38
 
39
    public enum TypeStockMouvement {
39
    public enum TypeStockMouvement {
40
        REEL, THEORIQUE, REEL_THEORIQUE, RETOUR
40
        REEL, THEORIQUE, REEL_THEORIQUE, RETOUR
41
    };
41
    };
42
 
42
 
43
    private double realQty, virtualQty, receiptQty, deliverQty;
43
    private double realQty, virtualQty, receiptQty, deliverQty;
44
    public SQLRowAccessor article, stock;
44
    public SQLRowAccessor article, stock;
45
 
45
 
46
    List<StockItemComponent> components = new ArrayList<StockItemComponent>();
46
    List<StockItemComponent> components = new ArrayList<StockItemComponent>();
47
 
47
 
48
    public StockItem(SQLRowAccessor article, SQLRowAccessor stock) {
48
    public StockItem(SQLRowAccessor article, SQLRowAccessor stock) {
49
        this.article = article;
49
        this.article = article;
50
        this.stock = stock;
50
        this.stock = stock;
51
        this.realQty = stock.getFloat("QTE_REEL");
51
        this.realQty = stock.getFloat("QTE_REEL");
52
        this.virtualQty = stock.getFloat("QTE_TH");
52
        this.virtualQty = stock.getFloat("QTE_TH");
53
        this.receiptQty = stock.getFloat("QTE_RECEPT_ATTENTE");
53
        this.receiptQty = stock.getFloat("QTE_RECEPT_ATTENTE");
54
        this.deliverQty = stock.getFloat("QTE_LIV_ATTENTE");
54
        this.deliverQty = stock.getFloat("QTE_LIV_ATTENTE");
55
    }
55
    }
56
 
56
 
57
    public void updateQty(double qty, TypeStockMouvement t) {
57
    public void updateQty(double qty, TypeStockMouvement t) {
58
        updateQty(qty, t, false);
58
        updateQty(qty, t, false);
59
    }
59
    }
60
 
60
 
61
    public void setDeliverQty(double deliverQty) {
61
    public void setDeliverQty(double deliverQty) {
62
        this.deliverQty = deliverQty;
62
        this.deliverQty = deliverQty;
63
    }
63
    }
64
 
64
 
65
    public void setRealQty(double realQty) {
65
    public void setRealQty(double realQty) {
66
        this.realQty = realQty;
66
        this.realQty = realQty;
67
    }
67
    }
68
 
68
 
69
    public void setReceiptQty(double receiptQty) {
69
    public void setReceiptQty(double receiptQty) {
70
        this.receiptQty = receiptQty;
70
        this.receiptQty = receiptQty;
71
    }
71
    }
72
 
72
 
73
    public void setVirtualQty(double virtualQty) {
73
    public void setVirtualQty(double virtualQty) {
74
        this.virtualQty = virtualQty;
74
        this.virtualQty = virtualQty;
75
    }
75
    }
76
 
76
 
77
    public SQLRowAccessor getArticle() {
77
    public SQLRowAccessor getArticle() {
78
        return article;
78
        return article;
79
    };
79
    };
80
 
80
 
81
    public void addItemComponent(StockItemComponent item) {
81
    public void addItemComponent(StockItemComponent item) {
82
        this.components.add(item);
82
        this.components.add(item);
83
    };
83
    };
84
 
84
 
85
    public boolean updateQtyFromChildren() throws IllegalArgumentException {
85
    public boolean updateQtyFromChildren() throws IllegalArgumentException {
86
        if (components.size() == 0) {
86
        if (components.size() == 0) {
87
            if (this.article.isUndefined()) {
87
            if (this.article.isUndefined()) {
88
                return false;
88
                return false;
89
            }
89
            }
90
            String code = "";
90
            String code = "";
91
            if (this.article != null && this.article.getFields().contains("CODE") && this.article.getString("CODE") != null) {
91
            if (this.article != null && this.article.getFields().contains("CODE") && this.article.getString("CODE") != null) {
92
                code = this.article.getString("CODE");
92
                code = this.article.getString("CODE");
93
            }
93
            }
94
            System.err.println("Impossible de mettre à jour le stock, l'articel n'est pas une nomenclature " + code);
94
            System.err.println("Impossible de mettre à jour le stock, l'articel n'est pas une nomenclature " + code);
95
            return false;
95
            return false;
96
 
96
 
97
        }
97
        }
98
        StockItemComponent comp = components.get(0);
98
        StockItemComponent comp = components.get(0);
99
        double real = comp.getItem().getRealQty() == 0 ? 0 : Math.ceil(comp.getItem().getRealQty() / (comp.getQty() * comp.getQtyUnit().doubleValue()));
99
        double real = comp.getItem().getRealQty() == 0 ? 0 : Math.floor(comp.getItem().getRealQty() / (comp.getQty() * comp.getQtyUnit().doubleValue()));
100
        double virtual = comp.getItem().getVirtualQty() == 0 ? 0 : Math.ceil(comp.getItem().getVirtualQty() / (comp.getQty() * comp.getQtyUnit().doubleValue()));
100
        double virtual = comp.getItem().getVirtualQty() == 0 ? 0 : Math.floor(comp.getItem().getVirtualQty() / (comp.getQty() * comp.getQtyUnit().doubleValue()));
101
        for (StockItemComponent stockItemComponent : components) {
101
        for (StockItemComponent stockItemComponent : components) {
102
            real = Math.min(real, stockItemComponent.getItem().getRealQty() == 0 ? 0
102
            real = Math.min(real, stockItemComponent.getItem().getRealQty() == 0 ? 0
103
                    : Math.ceil(stockItemComponent.getItem().getRealQty() / (stockItemComponent.getQty() * stockItemComponent.getQtyUnit().doubleValue())));
103
                    : Math.floor(stockItemComponent.getItem().getRealQty() / (stockItemComponent.getQty() * stockItemComponent.getQtyUnit().doubleValue())));
104
            virtual = Math.min(virtual, stockItemComponent.getItem().getVirtualQty() == 0 ? 0
104
            virtual = Math.min(virtual, stockItemComponent.getItem().getVirtualQty() == 0 ? 0
105
                    : Math.ceil(stockItemComponent.getItem().getVirtualQty() / (stockItemComponent.getQty() * stockItemComponent.getQtyUnit().doubleValue())));
105
                    : Math.floor(stockItemComponent.getItem().getVirtualQty() / (stockItemComponent.getQty() * stockItemComponent.getQtyUnit().doubleValue())));
106
 
106
 
107
        }
107
        }
108
        // La quantité du kit ne peut être négative
108
        // La quantité du kit ne peut être négative
109
        this.realQty = Math.max(0, real);
109
        this.realQty = Math.max(0, real);
110
        this.virtualQty = Math.max(0, virtual);
110
        this.virtualQty = Math.max(0, virtual);
111
        return true;
111
        return true;
112
    }
112
    }
113
 
113
 
114
    public void fillCommandeFournisseur(ListMap<SQLRow, SQLRowValues> cmd) {
114
    public void fillCommandeFournisseur(ListMap<SQLRow, SQLRowValues> cmd) {
115
        // TODO Gestion Stock Min par depot
115
        // TODO Gestion Stock Min par depot
116
        SQLPreferences prefs = new SQLPreferences(article.getTable().getDBRoot());
116
        SQLPreferences prefs = new SQLPreferences(article.getTable().getDBRoot());
117
        boolean gestionStockMin = prefs.getBoolean(GestionArticleGlobalPreferencePanel.WARNING_STOCK_MIN, true);
117
        boolean gestionStockMin = prefs.getBoolean(GestionArticleGlobalPreferencePanel.WARNING_STOCK_MIN, true);
118
        if (gestionStockMin && stock.getObject("QTE_MIN") != null && getRealQty() < stock.getFloat("QTE_MIN")) {
118
        if (gestionStockMin && stock.getObject("QTE_MIN") != null && getRealQty() < stock.getFloat("QTE_MIN")) {
119
            // final float qteShow = qteNvlle;
119
            // final float qteShow = qteNvlle;
120
            SQLInjector inj = SQLInjector.getInjector(article.getTable(), article.getTable().getTable("COMMANDE_ELEMENT"));
120
            SQLInjector inj = SQLInjector.getInjector(article.getTable(), article.getTable().getTable("COMMANDE_ELEMENT"));
121
            final SQLRow asRow = article.asRow();
121
            final SQLRow asRow = article.asRow();
122
            SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(asRow));
122
            SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(asRow));
123
            rowValsElt.put("ID_STYLE", 2);
123
            rowValsElt.put("ID_STYLE", 2);
124
            final SQLRowAccessor unite = article.getForeign("ID_UNITE_VENTE");
124
            final SQLRowAccessor unite = article.getForeign("ID_UNITE_VENTE");
125
            final double qteElt = stock.getFloat("QTE_MIN") - getRealQty();
125
            final double qteElt = stock.getFloat("QTE_MIN") - getRealQty();
126
            if (unite.isUndefined() || unite.getBoolean("A_LA_PIECE")) {
126
            if (unite.isUndefined() || unite.getBoolean("A_LA_PIECE")) {
127
                rowValsElt.put("QTE", Math.round(qteElt));
127
                rowValsElt.put("QTE", Math.round(qteElt));
128
                rowValsElt.put("QTE_UNITAIRE", BigDecimal.ONE);
128
                rowValsElt.put("QTE_UNITAIRE", BigDecimal.ONE);
129
            } else {
129
            } else {
130
                rowValsElt.put("QTE", 1);
130
                rowValsElt.put("QTE", 1);
131
                rowValsElt.put("QTE_UNITAIRE", new BigDecimal(qteElt));
131
                rowValsElt.put("QTE_UNITAIRE", new BigDecimal(qteElt));
132
            }
132
            }
133
            rowValsElt.put("ID_TAXE", rowValsElt.getObject("ID_TAXE"));
133
            rowValsElt.put("ID_TAXE", rowValsElt.getObject("ID_TAXE"));
134
            rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * qteElt);
134
            rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * qteElt);
135
            rowValsElt.put("T_PA_HT", rowValsElt.getLong("PA_HT") * qteElt);
135
            rowValsElt.put("T_PA_HT", rowValsElt.getLong("PA_HT") * qteElt);
136
            rowValsElt.put("T_PA_TTC", rowValsElt.getLong("T_PA_HT") * (rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0));
136
            rowValsElt.put("T_PA_TTC", rowValsElt.getLong("T_PA_HT") * (rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0));
137
 
137
 
138
            cmd.add(asRow.getForeignRow("ID_FOURNISSEUR"), rowValsElt);
138
            cmd.add(asRow.getForeignRow("ID_FOURNISSEUR"), rowValsElt);
139
        }
139
        }
140
 
140
 
141
    }
141
    }
142
 
142
 
143
    /**
143
    /**
144
     * Mise à jour des quantités de stocks. Stock Reel : inc/dec QTE_REEL, inc/dec
144
     * Mise à jour des quantités de stocks. Stock Reel : inc/dec QTE_REEL, inc/dec
145
     * QTE_LIV_ATTENTE/inc/dec QTE_RECEPT_ATTENTE Stock Th : inc/dec QTE_TH, inc/dec
145
     * QTE_LIV_ATTENTE/inc/dec QTE_RECEPT_ATTENTE Stock Th : inc/dec QTE_TH, inc/dec
146
     * QTE_LIV_ATTENTE/inc/dec QTE_RECEPT_ATTENTE
146
     * QTE_LIV_ATTENTE/inc/dec QTE_RECEPT_ATTENTE
147
     * 
147
     * 
148
     * @param qty quantité à ajouter ou à soustraire
148
     * @param qty quantité à ajouter ou à soustraire
149
     * @param t Type de stock à mettre à jour (réel ou virtuel)
149
     * @param t Type de stock à mettre à jour (réel ou virtuel)
150
     * @param archive annulation du stock
150
     * @param archive annulation du stock
151
     */
151
     */
152
    public void updateQty(double qty, TypeStockMouvement t, boolean archive) {
152
    public void updateQty(double qty, TypeStockMouvement t, boolean archive) {
153
 
153
 
154
        if (t == TypeStockMouvement.REEL || t == TypeStockMouvement.REEL_THEORIQUE || t == TypeStockMouvement.RETOUR) {
154
        if (t == TypeStockMouvement.REEL || t == TypeStockMouvement.REEL_THEORIQUE || t == TypeStockMouvement.RETOUR) {
155
            final double qteNvlle;
155
            final double qteNvlle;
156
            final double qteOrigin = this.realQty;
156
            final double qteOrigin = this.realQty;
157
            if (archive) {
157
            if (archive) {
158
                qteNvlle = qteOrigin - qty;
158
                qteNvlle = qteOrigin - qty;
159
                if (t != TypeStockMouvement.RETOUR) {
159
                if (t != TypeStockMouvement.RETOUR) {
160
                    // Réception
160
                    // Réception
161
                    if (qty > 0) {
161
                    if (qty > 0) {
162
                        this.receiptQty += qty;
162
                        this.receiptQty += qty;
163
                    } else {
163
                    } else {
164
                        // Livraison
164
                        // Livraison
165
                        this.deliverQty -= qty;
165
                        this.deliverQty -= qty;
166
                    }
166
                    }
167
                }
167
                }
168
            } else {
168
            } else {
169
                qteNvlle = qteOrigin + qty;
169
                qteNvlle = qteOrigin + qty;
170
                if (t != TypeStockMouvement.RETOUR) {
170
                if (t != TypeStockMouvement.RETOUR) {
171
                    // Réception
171
                    // Réception
172
                    if (qty > 0) {
172
                    if (qty > 0) {
173
                        this.receiptQty -= qty;
173
                        this.receiptQty -= qty;
174
                    } else {
174
                    } else {
175
                        // Livraison
175
                        // Livraison
176
                        this.deliverQty += qty;
176
                        this.deliverQty += qty;
177
                    }
177
                    }
178
                }
178
                }
179
            }
179
            }
180
 
180
 
181
            this.realQty = qteNvlle;
181
            this.realQty = qteNvlle;
182
 
182
 
183
        }
183
        }
184
 
184
 
185
        if (t == TypeStockMouvement.THEORIQUE || t == TypeStockMouvement.REEL_THEORIQUE || t == TypeStockMouvement.RETOUR) {
185
        if (t == TypeStockMouvement.THEORIQUE || t == TypeStockMouvement.REEL_THEORIQUE || t == TypeStockMouvement.RETOUR) {
186
 
186
 
187
            // THEORIQUE
187
            // THEORIQUE
188
            final double qteNvlle;
188
            final double qteNvlle;
189
            final double qteOrigin = this.virtualQty;
189
            final double qteOrigin = this.virtualQty;
190
            if (archive) {
190
            if (archive) {
191
                qteNvlle = qteOrigin - qty;
191
                qteNvlle = qteOrigin - qty;
192
                if (t != TypeStockMouvement.RETOUR) {
192
                if (t != TypeStockMouvement.RETOUR) {
193
                    // Réception
193
                    // Réception
194
                    if (qty > 0) {
194
                    if (qty > 0) {
195
                        this.receiptQty -= qty;
195
                        this.receiptQty -= qty;
196
                    } else {
196
                    } else {
197
                        // Livraison
197
                        // Livraison
198
                        this.deliverQty += qty;
198
                        this.deliverQty += qty;
199
                    }
199
                    }
200
                }
200
                }
201
            } else {
201
            } else {
202
                qteNvlle = qteOrigin + qty;
202
                qteNvlle = qteOrigin + qty;
203
                if (t != TypeStockMouvement.RETOUR) {
203
                if (t != TypeStockMouvement.RETOUR) {
204
                    // Réception
204
                    // Réception
205
                    if (qty > 0) {
205
                    if (qty > 0) {
206
                        this.receiptQty += qty;
206
                        this.receiptQty += qty;
207
                    } else {
207
                    } else {
208
                        // Livraison
208
                        // Livraison
209
                        this.deliverQty -= qty;
209
                        this.deliverQty -= qty;
210
                    }
210
                    }
211
                }
211
                }
212
            }
212
            }
213
 
213
 
214
            this.virtualQty = qteNvlle;
214
            this.virtualQty = qteNvlle;
215
        }
215
        }
216
    }
216
    }
217
 
217
 
218
    public double getDeliverQty() {
218
    public double getDeliverQty() {
219
        return deliverQty;
219
        return deliverQty;
220
    }
220
    }
221
 
221
 
222
    public double getRealQty() {
222
    public double getRealQty() {
223
        return realQty;
223
        return realQty;
224
    }
224
    }
225
 
225
 
226
    public double getReceiptQty() {
226
    public double getReceiptQty() {
227
        return receiptQty;
227
        return receiptQty;
228
    }
228
    }
229
 
229
 
230
    public double getVirtualQty() {
230
    public double getVirtualQty() {
231
        return virtualQty;
231
        return virtualQty;
232
    }
232
    }
233
 
233
 
234
    public boolean isStockInit() {
234
    public boolean isStockInit() {
235
        return this.stock != null && !this.stock.isUndefined();
235
        return this.stock != null && !this.stock.isUndefined();
236
    }
236
    }
237
 
237
 
238
    public void clearStockValues() {
238
    public void clearStockValues() {
239
        this.realQty = 0;
239
        this.realQty = 0;
240
        this.deliverQty = 0;
240
        this.deliverQty = 0;
241
        this.receiptQty = 0;
241
        this.receiptQty = 0;
242
        this.virtualQty = 0;
242
        this.virtualQty = 0;
243
    }
243
    }
244
 
244
 
245
    public String getUpdateRequest() {
245
    public String getUpdateRequest() {
246
        final SQLTable stockTable = this.stock.getTable();
246
        final SQLTable stockTable = this.stock.getTable();
247
        UpdateBuilder update = new UpdateBuilder(stockTable);
247
        UpdateBuilder update = new UpdateBuilder(stockTable);
248
        update.setWhere(new Where(stockTable.getKey(), "=", this.stock.getID()));
248
        update.setWhere(new Where(stockTable.getKey(), "=", this.stock.getID()));
249
        update.setObject("QTE_REEL", getRealQty());
249
        update.setObject("QTE_REEL", getRealQty());
250
        update.setObject("QTE_TH", getVirtualQty());
250
        update.setObject("QTE_TH", getVirtualQty());
251
        update.setObject("QTE_LIV_ATTENTE", getDeliverQty());
251
        update.setObject("QTE_LIV_ATTENTE", getDeliverQty());
252
        update.setObject("QTE_RECEPT_ATTENTE", getReceiptQty());
252
        update.setObject("QTE_RECEPT_ATTENTE", getReceiptQty());
253
        return update.asString();
253
        return update.asString();
254
    }
254
    }
255
 
255
 
256
}
256
}