Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/OpenConcerto/src/org/openconcerto/erp/core/sales/product/model/ProductComponent.java |
---|
14,13 → 14,19 |
package org.openconcerto.erp.core.sales.product.model; |
import org.openconcerto.erp.core.sales.product.model.ProductHelper.SupplierPriceField; |
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement; |
import org.openconcerto.sql.model.SQLRow; |
import org.openconcerto.sql.model.SQLRowAccessor; |
import org.openconcerto.sql.model.SQLRowValues; |
import org.openconcerto.sql.model.SQLRowValuesListFetcher; |
import org.openconcerto.sql.model.SQLTable; |
import org.openconcerto.sql.model.Where; |
import org.openconcerto.utils.DecimalUtils; |
import org.openconcerto.utils.ExceptionHandler; |
import java.math.BigDecimal; |
import java.math.RoundingMode; |
import java.sql.SQLException; |
import java.util.ArrayList; |
import java.util.Calendar; |
import java.util.Collection; |
29,13 → 35,17 |
public class ProductComponent { |
private final SQLRowAccessor product; |
private final SQLRowAccessor source; |
private final SQLRowAccessor stock; |
private BigDecimal qty; |
private final ProductHelper helper; |
public ProductComponent(SQLRowAccessor product, BigDecimal qty) { |
public ProductComponent(SQLRowAccessor product, BigDecimal qty, SQLRowAccessor source, SQLRowAccessor stock) { |
this.product = product; |
this.qty = qty; |
this.helper = new ProductHelper(product.getTable().getDBRoot()); |
this.source = source; |
this.stock = stock; |
} |
public SQLRowAccessor getProduct() { |
42,6 → 52,14 |
return product; |
} |
public SQLRowAccessor getSource() { |
return source; |
} |
public SQLRowAccessor getStock() { |
return stock; |
} |
public BigDecimal getQty() { |
return qty; |
} |
80,6 → 98,8 |
result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, d); |
if (result == null) { |
result = PriceByQty.getPriceForQty(qty.setScale(0, RoundingMode.HALF_UP).intValue(), prices, lastDate); |
} else if (prices.size() > 0) { |
result = prices.get(0).getPrice(); |
} |
} |
if (result == null) { |
112,16 → 132,87 |
return null; |
} |
public static ProductComponent createFromRowArticle(SQLRowAccessor rowArticle, SQLRowAccessor rowValsSource) { |
SQLRowAccessor rowStock = getStock(rowArticle, rowArticle, rowValsSource); |
return new ProductComponent(rowArticle, BigDecimal.ONE, rowValsSource, rowStock); |
} |
public static ProductComponent createFrom(SQLRowAccessor rowVals) { |
return createFrom(rowVals, 1); |
return createFrom(rowVals, 1, rowVals); |
} |
public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple) { |
public static ProductComponent createFrom(SQLRowAccessor rowVals, SQLRowAccessor rowValsSource) { |
return createFrom(rowVals, 1, rowValsSource); |
} |
public static ProductComponent createFrom(SQLRowAccessor rowVals, int qteMultiple, SQLRowAccessor rowValsSource) { |
if (rowVals.getForeign("ID_ARTICLE") == null || rowVals.isForeignEmpty("ID_ARTICLE")) { |
throw new IllegalArgumentException("Aucun article associé à la row " + rowVals.getTable().getName() + " " + rowVals.getID()); |
} |
final int qteMult = (rowVals.getTable().getName().equalsIgnoreCase("BON_DE_LIVRAISON_ELEMENT") ? rowVals.getInt("QTE_LIVREE") : rowVals.getInt("QTE")); |
final int qte = qteMult * qteMultiple; |
final BigDecimal qteUV = rowVals.getBigDecimal("QTE_UNITAIRE"); |
BigDecimal qteFinal = qteUV.multiply(new BigDecimal(qte), DecimalUtils.HIGH_PRECISION); |
return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal); |
SQLRowAccessor rowStock = getStock(rowVals.getForeign("ID_ARTICLE"), rowVals, rowValsSource); |
// } |
// else { |
// rowStock = rowVals.getForeign("ID_ARTICLE").getForeign("ID_STOCK"); |
// } |
return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal, rowValsSource, rowStock); |
// return new ProductComponent(rowVals.getForeign("ID_ARTICLE"), qteFinal); |
} |
private static SQLRowAccessor getStock(SQLRowAccessor rowValsProduct, SQLRowAccessor rowValsElt, SQLRowAccessor rowValsSource) { |
SQLRowAccessor rowStock = null; |
final int idDepot; |
if (rowValsSource.getFields().contains("ID_DEPOT_STOCK") && !rowValsSource.isForeignEmpty("ID_DEPOT_STOCK")) { |
idDepot = rowValsSource.getForeignID("ID_DEPOT_STOCK"); |
} else { |
if (rowValsElt.getForeign("ID_DEPOT_STOCK") != null && !rowValsElt.isForeignEmpty("ID_DEPOT_STOCK")) { |
idDepot = rowValsElt.getForeignID("ID_DEPOT_STOCK"); |
} else { |
idDepot = DepotStockSQLElement.DEFAULT_ID; |
try { |
rowValsElt.createEmptyUpdateRow().put("ID_DEPOT_STOCK", idDepot).commit(); |
} catch (SQLException e) { |
ExceptionHandler.handle("Erreur lors de l'initialisation du stock!", e); |
} |
} |
} |
SQLTable stockTable = rowValsElt.getTable().getTable("STOCK"); |
SQLRowValues putRowValuesStock = new SQLRowValues(stockTable); |
putRowValuesStock.putNulls(stockTable.getTable().getFieldsName()); |
SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(putRowValuesStock); |
Where w = new Where(putRowValuesStock.getTable().getField("ID_DEPOT_STOCK"), "=", idDepot); |
Where w2 = new Where(putRowValuesStock.getTable().getField("ID_ARTICLE"), "=", rowValsProduct.getID()); |
Collection<SQLRowValues> rowValsResult = fetch.fetch(w.and(w2)); |
if (rowValsResult.size() == 0) { |
SQLRowValues rowValsStock = new SQLRowValues(stockTable); |
rowValsStock.put("ID_ARTICLE", rowValsProduct.getID()); |
rowValsStock.put("ID_DEPOT_STOCK", idDepot); |
rowValsStock.put("QTE_TH", 0F); |
rowValsStock.put("QTE_REEL", 0F); |
rowValsStock.put("QTE_RECEPT_ATTENTE", 0F); |
rowValsStock.put("QTE_LIV_ATTENTE", 0F); |
try { |
rowStock = rowValsStock.insert(); |
if (idDepot == DepotStockSQLElement.DEFAULT_ID) { |
rowValsProduct.createEmptyUpdateRow().put("ID_STOCK", rowStock.getID()).commit(); |
} |
} catch (SQLException e) { |
ExceptionHandler.handle("Erreur lors la création du stock!", e); |
} |
} else if (rowValsResult.size() == 1) { |
rowStock = rowValsResult.iterator().next(); |
} else if (rowValsResult.size() > 1) { |
throw new IllegalStateException("2 lignes de stocks pour le même dépôt! Article " + rowValsProduct.getID() + " Depot " + rowValsElt.getForeignID("ID_DEPOT_STOCK")); |
} |
return rowStock; |
} |
} |