Line 12... |
Line 12... |
12 |
*/
|
12 |
*/
|
13 |
|
13 |
|
14 |
package org.openconcerto.erp.core.sales.product.model;
|
14 |
package org.openconcerto.erp.core.sales.product.model;
|
15 |
|
15 |
|
16 |
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
|
16 |
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField;
|
- |
|
17 |
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
|
17 |
import org.openconcerto.sql.model.SQLRow;
|
18 |
import org.openconcerto.sql.model.SQLRow;
|
18 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
19 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
- |
|
20 |
import org.openconcerto.sql.model.SQLRowValues;
|
- |
|
21 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
19 |
import org.openconcerto.sql.model.SQLTable;
|
22 |
import org.openconcerto.sql.model.SQLTable;
|
- |
|
23 |
import org.openconcerto.sql.model.Where;
|
20 |
import org.openconcerto.utils.DecimalUtils;
|
24 |
import org.openconcerto.utils.DecimalUtils;
|
- |
|
25 |
import org.openconcerto.utils.ExceptionHandler;
|
21 |
|
26 |
|
22 |
import java.math.BigDecimal;
|
27 |
import java.math.BigDecimal;
|
23 |
import java.math.RoundingMode;
|
28 |
import java.math.RoundingMode;
|
- |
|
29 |
import java.sql.SQLException;
|
24 |
import java.util.ArrayList;
|
30 |
import java.util.ArrayList;
|
25 |
import java.util.Calendar;
|
31 |
import java.util.Calendar;
|
26 |
import java.util.Collection;
|
32 |
import java.util.Collection;
|
27 |
import java.util.Date;
|
33 |
import java.util.Date;
|
28 |
import java.util.List;
|
34 |
import java.util.List;
|
29 |
|
35 |
|
30 |
public class ProductComponent {
|
36 |
public class ProductComponent {
|
31 |
private final SQLRowAccessor product;
|
37 |
private final SQLRowAccessor product;
|
- |
|
38 |
private final SQLRowAccessor source;
|
- |
|
39 |
private final SQLRowAccessor stock;
|
32 |
private BigDecimal qty;
|
40 |
private BigDecimal qty;
|
33 |
private final ProductHelper helper;
|
41 |
private final ProductHelper helper;
|
34 |
|
42 |
|
35 |
public ProductComponent(SQLRowAccessor product, BigDecimal qty) {
|
43 |
public ProductComponent(SQLRowAccessor product, BigDecimal qty, SQLRowAccessor source, SQLRowAccessor stock) {
|
36 |
this.product = product;
|
44 |
this.product = product;
|
37 |
this.qty = qty;
|
45 |
this.qty = qty;
|
38 |
this.helper = new ProductHelper(product.getTable().getDBRoot());
|
46 |
this.helper = new ProductHelper(product.getTable().getDBRoot());
|
- |
|
47 |
this.source = source;
|
- |
|
48 |
this.stock = stock;
|
39 |
}
|
49 |
}
|
40 |
|
50 |
|
41 |
public SQLRowAccessor getProduct() {
|
51 |
public SQLRowAccessor getProduct() {
|
42 |
return product;
|
52 |
return product;
|
43 |
}
|
53 |
}
|
44 |
|
54 |
|
- |
|
55 |
public SQLRowAccessor getSource() {
|
- |
|
56 |
return source;
|
- |
|
57 |
}
|
- |
|
58 |
|
- |
|
59 |
public SQLRowAccessor getStock() {
|
- |
|
60 |
return stock;
|
- |
|
61 |
}
|
- |
|
62 |
|
45 |
public BigDecimal getQty() {
|
63 |
public BigDecimal getQty() {
|
46 |
return qty;
|
64 |
return qty;
|
47 |
}
|
65 |
}
|
48 |
|
66 |
|
49 |
public void setQty(BigDecimal qty) {
|
67 |
public void setQty(BigDecimal qty) {
|
Line 78... |
Line 96... |
78 |
}
|
96 |
}
|
79 |
|
97 |
|
80 |
result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d);
|
98 |
result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d);
|
81 |
if (result == null) {
|
99 |
if (result == null) {
|
82 |
result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate);
|
100 |
result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate);
|
- |
|
101 |
} else if (prices.size() > 0) {
|
- |
|
102 |
result = prices.get(0).getPrice();
|
83 |
}
|
103 |
}
|
84 |
}
|
104 |
}
|
85 |
if (result == null) {
|
105 |
if (result == null) {
|
86 |
SQLTable tableATF = product.getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
|
106 |
SQLTable tableATF = product.getTable().getDBRoot().getTable("ARTICLE_TARIF_FOURNISSEUR");
|
87 |
Collection<SQLRow> atfs = product.asRow().getReferentRows(tableATF);
|
107 |
Collection<SQLRow> atfs = product.asRow().getReferentRows(tableATF);
|
Line 110... |
Line 130... |
110 |
return result;
|
130 |
return result;
|
111 |
}
|
131 |
}
|
112 |
return null;
|
132 |
return null;
|
113 |
}
|
133 |
}
|
114 |
|
134 |
|
- |
|
135 |
public static ProductComponent createFromRowArticle(SQLRowAccessor rowArticle, SQLRowAccessor rowValsSource) {
|
- |
|
136 |
SQLRowAccessor rowStock = getStock(rowArticle, rowArticle, rowValsSource);
|
- |
|
137 |
|
- |
|
138 |
return new ProductComponent(rowArticle, BigDecimal.ONE, rowValsSource, rowStock);
|
- |
|
139 |
}
|
- |
|
140 |
|
115 |
public static ProductComponent createFrom(SQLRowAccessor rowVals) {
|
141 |
public static ProductComponent createFrom(SQLRowAccessor rowVals) {
|
116 |
return createFrom(rowVals, 1);
|
142 |
return createFrom(rowVals, 1, rowVals);
|
117 |
}
|
143 |
}
|
118 |
|
144 |
|
119 |
public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple) {
|
145 |
public static ProductComponent createFrom(SQLRowAccessor rowVals, SQLRowAccessor rowValsSource) {
|
- |
|
146 |
return createFrom(rowVals, 1, rowValsSource);
|
- |
|
147 |
}
|
120 |
|
148 |
|
- |
|
149 |
public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple, SQLRowAccessor rowValsSource) {
|
- |
|
150 |
|
- |
|
151 |
if (rowVals.getForeign("ID_ARTICLE") == null || rowVals.isForeignEmpty("ID_ARTICLE")) {
|
- |
|
152 |
throw new IllegalArgumentException("Aucun article associé à la row " + rowVals.getTable().getName() + " " + rowVals.getID());
|
- |
|
153 |
}
|
121 |
final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE"));
|
154 |
final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE"));
|
122 |
final int qte = qteMult * qteMultiple;
|
155 |
final int qte = qteMult * qteMultiple;
|
123 |
final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE");
|
156 |
final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE");
|
124 |
BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
|
157 |
BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION);
|
- |
|
158 |
SQLRowAccessor rowStock = getStock(rowVals.getForeign("ID_ARTICLE"), rowVals, rowValsSource);
|
- |
|
159 |
|
- |
|
160 |
// }
|
- |
|
161 |
// else {
|
- |
|
162 |
// rowStock = rowVals.getForeign("ID_ARTICLE").getForeign("ID_STOCK");
|
- |
|
163 |
// }
|
- |
|
164 |
return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal, rowValsSource, rowStock);
|
125 |
return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
|
165 |
// return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal);
|
- |
|
166 |
}
|
- |
|
167 |
|
- |
|
168 |
private static SQLRowAccessor getStock(SQLRowAccessor rowValsProduct, SQLRowAccessor rowValsElt, SQLRowAccessor rowValsSource) {
|
- |
|
169 |
SQLRowAccessor rowStock = null;
|
- |
|
170 |
final int idDepot;
|
- |
|
171 |
if (rowValsSource.getFields().contains("ID_DEPOT_STOCK") && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) {
|
- |
|
172 |
idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK");
|
- |
|
173 |
} else {
|
- |
|
174 |
if (rowValsElt.getForeign("ID_DEPOT_STOCK") != null && !rowValsElt.isForeignEmpty("ID_DEPOT_STOCK")) {
|
- |
|
175 |
idDepot = rowValsElt.getForeignID("ID_DEPOT_STOCK");
|
- |
|
176 |
} else {
|
- |
|
177 |
idDepot = DepotStockSQLElement.DEFAULT_ID;
|
- |
|
178 |
try {
|
- |
|
179 |
rowValsElt.createEmptyUpdateRow().put("ID_DEPOT_STOCK", idDepot).commit();
|
- |
|
180 |
} catch (SQLException e) {
|
- |
|
181 |
ExceptionHandler.handle("Erreur lors de l'initialisation du stock!", e);
|
- |
|
182 |
}
|
- |
|
183 |
|
- |
|
184 |
}
|
- |
|
185 |
}
|
- |
|
186 |
|
- |
|
187 |
SQLTable stockTable = rowValsElt.getTable().getTable("STOCK");
|
- |
|
188 |
SQLRowValues putRowValuesStock = new SQLRowValues(stockTable);
|
- |
|
189 |
putRowValuesStock.putNulls(stockTable.getTable().getFieldsName());
|
- |
|
190 |
|
- |
|
191 |
SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock);
|
- |
|
192 |
Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", idDepot);
|
- |
|
193 |
Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", rowValsProduct.getID());
|
- |
|
194 |
Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2));
|
- |
|
195 |
if (rowValsResult.size() == 0) {
|
- |
|
196 |
SQLRowValues rowValsStock = new SQLRowValues(stockTable);
|
- |
|
197 |
rowValsStock.put("ID_ARTICLE", rowValsProduct.getID());
|
- |
|
198 |
rowValsStock.put("ID_DEPOT_STOCK", idDepot);
|
- |
|
199 |
rowValsStock.put("QTE_TH", 0F);
|
- |
|
200 |
rowValsStock.put("QTE_REEL", 0F);
|
- |
|
201 |
rowValsStock.put("QTE_RECEPT_ATTENTE", 0F);
|
- |
|
202 |
rowValsStock.put("QTE_LIV_ATTENTE", 0F);
|
- |
|
203 |
try {
|
- |
|
204 |
rowStock = rowValsStock.insert();
|
- |
|
205 |
if (idDepot == DepotStockSQLElement.DEFAULT_ID) {
|
- |
|
206 |
rowValsProduct.createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit();
|
- |
|
207 |
}
|
- |
|
208 |
} catch (SQLException e) {
|
- |
|
209 |
ExceptionHandler.handle("Erreur lors la création du stock!", e);
|
- |
|
210 |
}
|
- |
|
211 |
} else if (rowValsResult.size() == 1) {
|
- |
|
212 |
rowStock = rowValsResult.iterator().next();
|
- |
|
213 |
} else if (rowValsResult.size() > 1) {
|
- |
|
214 |
throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsProduct.getID() + " Depot " + rowValsElt.getForeignID("ID_DEPOT_STOCK"));
|
- |
|
215 |
}
|
- |
|
216 |
return rowStock;
|
126 |
}
|
217 |
}
|
127 |
}
|
218 |
}
|