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 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.sales.pos.model;
15
 
142 ilm 16
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
17
import org.openconcerto.utils.DecimalUtils;
182 ilm 18
import org.openconcerto.utils.QuickOrderedMap;
142 ilm 19
 
67 ilm 20
import java.math.BigDecimal;
142 ilm 21
import java.math.RoundingMode;
182 ilm 22
import java.util.ArrayList;
23
import java.util.Collections;
24
import java.util.Comparator;
18 ilm 25
import java.util.HashMap;
182 ilm 26
import java.util.List;
18 ilm 27
import java.util.Map;
28
 
29
public class Article {
30
    private Categorie s;
31
    private String name;
151 ilm 32
    private boolean additionalCopyRequested = false;
132 ilm 33
    private BigDecimal priceTTC;
34
    private int idTaxe;
35
    private BigDecimal priceHT;
36
    private String barCode = "empty barcode";
37
    private String code = "";
182 ilm 38
    private BigDecimal discount_pct = BigDecimal.ZERO;
39
    private BigDecimal ecotaxe = BigDecimal.ZERO;
61 ilm 40
    private final int id;
174 ilm 41
    // unité de vente (null si à la pièce)
42
    private String salesUnit;
182 ilm 43
    private static Map<String, List<Article>> codes = new HashMap<>();
18 ilm 44
 
182 ilm 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;
49
 
61 ilm 50
    public Article(Categorie s1, String string, int id) {
18 ilm 51
        this.s = s1;
61 ilm 52
        this.id = id;
18 ilm 53
        this.name = string;
54
        s1.addArticle(this);
55
    }
56
 
142 ilm 57
    public Article(Article a) {
58
        this.s = a.s;
182 ilm 59
        this.code = a.code;
142 ilm 60
        this.name = a.name;
61
        this.priceTTC = a.priceTTC;
62
        this.idTaxe = a.idTaxe;
63
        this.priceHT = a.priceHT;
64
        this.barCode = a.barCode;
65
        this.id = a.id;
174 ilm 66
        this.salesUnit = a.salesUnit;
182 ilm 67
        this.discount_pct = a.discount_pct;
68
        this.ecotaxe = a.ecotaxe;
69
 
142 ilm 70
        this.s.addArticle(this);
182 ilm 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
        }
142 ilm 79
    }
80
 
182 ilm 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();
125
    }
126
 
151 ilm 127
    public boolean isAdditionalCopyRequested() {
174 ilm 128
        return this.additionalCopyRequested;
151 ilm 129
    }
130
 
131
    public void setAdditionalCopyRequested(boolean additionalCopyRequested) {
132
        this.additionalCopyRequested = additionalCopyRequested;
133
    }
134
 
61 ilm 135
    public int getId() {
136
        return this.id;
137
    }
138
 
132 ilm 139
    public BigDecimal getPriceWithoutTax() {
182 ilm 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()));
132 ilm 146
        return this.priceHT;
18 ilm 147
    }
148
 
182 ilm 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
 
132 ilm 170
    public void setPriceWithoutTax(BigDecimal priceHTInCents) {
171
        this.priceHT = priceHTInCents;
18 ilm 172
    }
173
 
174
    public int getIdTaxe() {
21 ilm 175
        return this.idTaxe;
18 ilm 176
    }
177
 
178
    public void setIdTaxe(int idTaxe) {
179
        this.idTaxe = idTaxe;
180
    }
181
 
182
    public void setBarCode(String bar) {
25 ilm 183
        this.barCode = bar;
182 ilm 184
        List<Article> list = codes.get(bar);
185
        if (list == null) {
186
            list = new ArrayList<>();
187
            codes.put(bar, list);
188
        }
189
        list.add(this);
18 ilm 190
    }
191
 
132 ilm 192
    public void setPriceWithTax(BigDecimal priceInCents) {
193
        this.priceTTC = priceInCents;
18 ilm 194
    }
195
 
182 ilm 196
    public BigDecimal getPriceWithTax(BigDecimal quantity) {
197
        return getPriceWithTax(quantity, false);
18 ilm 198
    }
199
 
182 ilm 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
        }
213
        return price;
214
    }
215
 
18 ilm 216
    public String getName() {
21 ilm 217
        return this.name;
18 ilm 218
    }
219
 
25 ilm 220
    public void setCode(String code) {
221
        this.code = code;
222
    }
223
 
18 ilm 224
    public String getCode() {
21 ilm 225
        return this.code;
25 ilm 226
    };
227
 
228
    public String getBarCode() {
229
        return this.barCode;
18 ilm 230
    }
231
 
232
    public Categorie getCategorie() {
21 ilm 233
        return this.s;
18 ilm 234
    }
235
 
236
    @Override
237
    public String toString() {
182 ilm 238
        if (this.declinaisons != null) {
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();
18 ilm 242
    }
243
 
182 ilm 244
    public static List<Article> getArticleFromBarcode(String code) {
18 ilm 245
        return codes.get(code);
246
    }
142 ilm 247
 
248
    @Override
249
    public boolean equals(Object obj) {
250
        if (obj == null)
251
            return false;
252
        return this.id == ((Article) obj).id;
253
    }
254
 
255
    @Override
256
    public int hashCode() {
257
        return this.id;
258
    }
259
 
260
    public void updatePriceWithoutTax(BigDecimal ht) {
261
        this.priceHT = ht;
262
        this.priceTTC = computePriceWithTax(ht, this.getIdTaxe());
263
    }
264
 
265
    public static BigDecimal computePriceWithTax(BigDecimal ht, int idTaxe) {
174 ilm 266
        final BigDecimal tax = BigDecimal.valueOf(TaxeCache.getCache().getTauxFromId(idTaxe)).movePointLeft(2).add(BigDecimal.ONE);
142 ilm 267
        return ht.multiply(tax).setScale(2, RoundingMode.HALF_UP);
268
    }
269
 
270
    public static BigDecimal computePriceWithoutTax(BigDecimal ttc, int idTaxe) {
174 ilm 271
        final BigDecimal tax = BigDecimal.valueOf(TaxeCache.getCache().getTauxFromId(idTaxe)).movePointLeft(2).add(BigDecimal.ONE);
142 ilm 272
        return ttc.divide(tax, DecimalUtils.HIGH_PRECISION).setScale(6, RoundingMode.HALF_UP);
273
    }
174 ilm 274
 
275
    public void setSalesUnit(String name) {
276
        this.salesUnit = name;
277
    }
278
 
279
    public String getSalesUnit() {
280
        return this.salesUnit;
281
    }
182 ilm 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
 
18 ilm 319
}