OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 182
Line 1... Line 1...
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-2019 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.
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.core.sales.pos.model;
14
 package org.openconcerto.erp.core.sales.pos.model;
15
 
15
 
16
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
16
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
17
import org.openconcerto.utils.DecimalUtils;
17
import org.openconcerto.utils.DecimalUtils;
-
 
18
import org.openconcerto.utils.QuickOrderedMap;
18
 
19
 
19
import java.math.BigDecimal;
20
import java.math.BigDecimal;
20
import java.math.RoundingMode;
21
import java.math.RoundingMode;
-
 
22
import java.util.ArrayList;
-
 
23
import java.util.Collections;
-
 
24
import java.util.Comparator;
21
import java.util.HashMap;
25
import java.util.HashMap;
-
 
26
import java.util.List;
22
import java.util.Map;
27
import java.util.Map;
23
 
28
 
24
public class Article {
29
public class Article {
25
    private Categorie s;
30
    private Categorie s;
26
    private String name;
31
    private String name;
Line 28... Line 33...
28
    private BigDecimal priceTTC;
33
    private BigDecimal priceTTC;
29
    private int idTaxe;
34
    private int idTaxe;
30
    private BigDecimal priceHT;
35
    private BigDecimal priceHT;
31
    private String barCode = "empty barcode";
36
    private String barCode = "empty barcode";
32
    private String code = "";
37
    private String code = "";
-
 
38
    private BigDecimal discount_pct = BigDecimal.ZERO;
-
 
39
    private BigDecimal ecotaxe = BigDecimal.ZERO;
33
    private final int id;
40
    private final int id;
34
    // unité de vente (null si à la pièce)
41
    // unité de vente (null si à la pièce)
35
    private String salesUnit;
42
    private String salesUnit;
36
    private static Map<String, Article> codes = new HashMap<String, Article>();
43
    private static Map<String, List<Article>> codes = new HashMap<>();
-
 
44
 
-
 
45
    // declinaison (ex "taille":"45", "couleur","rouge"), null si pas de declinaison
-
 
46
    private QuickOrderedMap<String, String> declinaisons;
-
 
47
    List<TarifQuantite> tarifs;
-
 
48
    private String declinaison;
37
 
49
 
38
    public Article(Categorie s1, String string, int id) {
50
    public Article(Categorie s1, String string, int id) {
39
        this.s = s1;
51
        this.s = s1;
40
        this.id = id;
52
        this.id = id;
41
        this.name = string;
53
        this.name = string;
42
        s1.addArticle(this);
54
        s1.addArticle(this);
43
    }
55
    }
44
 
56
 
45
    public Article(Article a) {
57
    public Article(Article a) {
46
        this.s = a.s;
58
        this.s = a.s;
-
 
59
        this.code = a.code;
47
        this.name = a.name;
60
        this.name = a.name;
48
        this.priceTTC = a.priceTTC;
61
        this.priceTTC = a.priceTTC;
49
        this.idTaxe = a.idTaxe;
62
        this.idTaxe = a.idTaxe;
50
        this.priceHT = a.priceHT;
63
        this.priceHT = a.priceHT;
51
        this.barCode = a.barCode;
64
        this.barCode = a.barCode;
52
        this.id = a.id;
65
        this.id = a.id;
53
        this.salesUnit = a.salesUnit;
66
        this.salesUnit = a.salesUnit;
-
 
67
        this.discount_pct = a.discount_pct;
-
 
68
        this.ecotaxe = a.ecotaxe;
-
 
69
 
54
        this.s.addArticle(this);
70
        this.s.addArticle(this);
-
 
71
        final QuickOrderedMap<String, String> decl = a.getDeclinaisons();
-
 
72
        if (decl != null) {
-
 
73
            final int size = decl.size();
-
 
74
            this.declinaisons = new QuickOrderedMap<>(size);
-
 
75
            for (int i = 0; i < size; i++) {
-
 
76
                this.declinaisons.put(decl.getKey(i), decl.getValue(i));
-
 
77
            }
-
 
78
        }
-
 
79
    }
-
 
80
 
-
 
81
    public void setTarifsPromotion(List<TarifQuantite> list) {
-
 
82
        this.tarifs = new ArrayList<>();
-
 
83
        for (TarifQuantite t : list) {
-
 
84
            if (t.getIdArticle() == this.id) {
-
 
85
                this.tarifs.add(t);
-
 
86
            }
-
 
87
        }
-
 
88
        Collections.sort(this.tarifs, new Comparator<TarifQuantite>() {
-
 
89
 
-
 
90
            @Override
-
 
91
            public int compare(TarifQuantite o1, TarifQuantite o2) {
-
 
92
                return o1.getQuantite() - o2.getQuantite();
-
 
93
            }
-
 
94
 
-
 
95
        });
-
 
96
    }
-
 
97
 
-
 
98
    public TarifQuantite getTarifPromotionForQuantity(int qty) {
-
 
99
        if (this.tarifs == null || this.tarifs.isEmpty()) {
-
 
100
            return null;
-
 
101
        }
-
 
102
        TarifQuantite result = null;
-
 
103
        for (final TarifQuantite t : this.tarifs) {
-
 
104
            if (t.getQuantite() > qty) {
-
 
105
                break;
-
 
106
            }
-
 
107
            result = t;
-
 
108
        }
-
 
109
        return result;
-
 
110
    }
-
 
111
 
-
 
112
    public void addDeclinaison(String key, String value) {
-
 
113
        if (this.declinaisons == null) {
-
 
114
            this.declinaisons = new QuickOrderedMap<>(5);
-
 
115
        }
-
 
116
        this.declinaisons.put(key, value);
-
 
117
    }
-
 
118
 
-
 
119
    public QuickOrderedMap<String, String> getDeclinaisons() {
-
 
120
        return this.declinaisons;
-
 
121
    }
-
 
122
 
-
 
123
    public boolean isPromotion() {
-
 
124
        return this.tarifs != null && !this.tarifs.isEmpty();
55
    }
125
    }
56
 
126
 
57
    public boolean isAdditionalCopyRequested() {
127
    public boolean isAdditionalCopyRequested() {
58
        return this.additionalCopyRequested;
128
        return this.additionalCopyRequested;
59
    }
129
    }
Line 65... Line 135...
65
    public int getId() {
135
    public int getId() {
66
        return this.id;
136
        return this.id;
67
    }
137
    }
68
 
138
 
69
    public BigDecimal getPriceWithoutTax() {
139
    public BigDecimal getPriceWithoutTax() {
-
 
140
        return getPriceWithoutTax(false);
-
 
141
    }
-
 
142
 
-
 
143
    public BigDecimal getPriceWithoutTax(boolean with_discount) {
-
 
144
        if (with_discount)
-
 
145
            return this.priceHT.multiply(BigDecimal.ONE.subtract(getDiscountPct()));
70
        return this.priceHT;
146
        return this.priceHT;
71
    }
147
    }
72
 
148
 
-
 
149
    public BigDecimal getPriceWithoutTax(BigDecimal qty) {
-
 
150
        return getPriceWithoutTax(qty, false);
-
 
151
    }
-
 
152
 
-
 
153
    public BigDecimal getPriceWithoutTax(BigDecimal qty, boolean withDiscount) {
-
 
154
        BigDecimal price = this.priceHT;
-
 
155
        if (getSalesUnit() == null) {
-
 
156
            // Promotion
-
 
157
            final TarifQuantite tarifPromotionForQuantity = getTarifPromotionForQuantity(qty.intValue());
-
 
158
            if (tarifPromotionForQuantity != null) {
-
 
159
                price = tarifPromotionForQuantity.getPrixHT();
-
 
160
            }
-
 
161
        }
-
 
162
 
-
 
163
        if (withDiscount) {
-
 
164
            price = price.multiply(BigDecimal.ONE.subtract(getDiscountPct()));
-
 
165
        }
-
 
166
        return price;
-
 
167
 
-
 
168
    }
-
 
169
 
73
    public void setPriceWithoutTax(BigDecimal priceHTInCents) {
170
    public void setPriceWithoutTax(BigDecimal priceHTInCents) {
74
        this.priceHT = priceHTInCents;
171
        this.priceHT = priceHTInCents;
75
    }
172
    }
76
 
173
 
77
    public int getIdTaxe() {
174
    public int getIdTaxe() {
Line 82... Line 179...
82
        this.idTaxe = idTaxe;
179
        this.idTaxe = idTaxe;
83
    }
180
    }
84
 
181
 
85
    public void setBarCode(String bar) {
182
    public void setBarCode(String bar) {
86
        this.barCode = bar;
183
        this.barCode = bar;
-
 
184
        List<Article> list = codes.get(bar);
-
 
185
        if (list == null) {
-
 
186
            list = new ArrayList<>();
87
        codes.put(bar, this);
187
            codes.put(bar, list);
-
 
188
        }
-
 
189
        list.add(this);
88
    }
190
    }
89
 
191
 
90
    public void setPriceWithTax(BigDecimal priceInCents) {
192
    public void setPriceWithTax(BigDecimal priceInCents) {
91
        this.priceTTC = priceInCents;
193
        this.priceTTC = priceInCents;
92
    }
194
    }
93
 
195
 
94
    public BigDecimal getPriceWithTax() {
196
    public BigDecimal getPriceWithTax(BigDecimal quantity) {
-
 
197
        return getPriceWithTax(quantity, false);
-
 
198
    }
-
 
199
 
-
 
200
    public BigDecimal getPriceWithTax(BigDecimal qty, boolean withDiscount) {
-
 
201
        BigDecimal price = this.priceTTC;
-
 
202
        if (getSalesUnit() == null) {
-
 
203
            // Promotion
-
 
204
            final TarifQuantite tarifPromotionForQuantity = getTarifPromotionForQuantity(qty.intValue());
-
 
205
            if (tarifPromotionForQuantity != null) {
-
 
206
                price = tarifPromotionForQuantity.getPrixTTC();
-
 
207
            }
-
 
208
        }
-
 
209
 
-
 
210
        if (withDiscount) {
-
 
211
            price = price.multiply(BigDecimal.ONE.subtract(getDiscountPct()));
-
 
212
        }
95
        return this.priceTTC;
213
        return price;
96
    }
214
    }
97
 
215
 
98
    public String getName() {
216
    public String getName() {
99
        return this.name;
217
        return this.name;
100
    }
218
    }
Line 115... Line 233...
115
        return this.s;
233
        return this.s;
116
    }
234
    }
117
 
235
 
118
    @Override
236
    @Override
119
    public String toString() {
237
    public String toString() {
-
 
238
        if (this.declinaisons != null) {
120
        return "Article:" + this.name + " " + this.priceTTC + " cents" + "(HT:" + this.priceHT + ") " + getSalesUnit();
239
            return "Article:" + this.code + " | " + this.name + " " + this.priceTTC + " cents" + "(HT:" + this.priceHT + ") " + getSalesUnit() + " discount:" + getDiscountPct();
-
 
240
        }
-
 
241
        return "Article:" + this.code + " | " + this.name + "  [" + this.declinaisons + "] : " + this.priceTTC + " cents" + "(HT:" + this.priceHT + ") " + getSalesUnit() + " discount:" + getDiscountPct();
121
    }
242
    }
122
 
243
 
123
    public static Article getArticleFromBarcode(String code) {
244
    public static List<Article> getArticleFromBarcode(String code) {
124
        return codes.get(code);
245
        return codes.get(code);
125
    }
246
    }
126
 
247
 
127
    @Override
248
    @Override
128
    public boolean equals(Object obj) {
249
    public boolean equals(Object obj) {
Line 156... Line 277...
156
    }
277
    }
157
 
278
 
158
    public String getSalesUnit() {
279
    public String getSalesUnit() {
159
        return this.salesUnit;
280
        return this.salesUnit;
160
    }
281
    }
-
 
282
 
-
 
283
    public void setDiscountPct(BigDecimal dsct) {
-
 
284
        this.discount_pct = dsct;
-
 
285
    }
-
 
286
 
-
 
287
    public BigDecimal getEcoTaxe() {
-
 
288
        return this.ecotaxe;
-
 
289
    }
-
 
290
 
-
 
291
    public BigDecimal getDiscountPct() {
-
 
292
        return this.discount_pct;
-
 
293
    }
-
 
294
 
-
 
295
    public void setEcoTaxe(BigDecimal ecotaxe) {
-
 
296
        this.ecotaxe = ecotaxe;
-
 
297
    }
-
 
298
 
-
 
299
    /**
-
 
300
     * ligne precisant la declinaison sur le ticket
-
 
301
     */
-
 
302
    public void setDeclinaison(String declinaison) {
-
 
303
        this.declinaison = declinaison;
-
 
304
    }
-
 
305
 
-
 
306
    public String getDeclinaison() {
-
 
307
        if (this.declinaison == null && this.declinaisons != null && !this.declinaisons.isEmpty()) {
-
 
308
            StringBuilder b = new StringBuilder();
-
 
309
            for (int i = 0; i < this.declinaisons.size(); i++) {
-
 
310
                b.append(this.declinaisons.getValue(i));
-
 
311
                b.append("  ");
-
 
312
            }
-
 
313
 
-
 
314
            return b.toString().trim();
-
 
315
        }
-
 
316
        return this.declinaison;
-
 
317
    }
-
 
318
 
161
}
319
}