OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 142 Rev 156
Line 22... Line 22...
22
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
22
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
23
import org.openconcerto.sql.model.SQLSelect;
23
import org.openconcerto.sql.model.SQLSelect;
24
import org.openconcerto.sql.model.SQLTable;
24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.utils.DecimalUtils;
26
import org.openconcerto.utils.DecimalUtils;
-
 
27
import org.openconcerto.utils.ListMap;
27
import org.openconcerto.utils.cc.ITransformer;
28
import org.openconcerto.utils.cc.ITransformer;
28
 
29
 
29
import java.math.BigDecimal;
30
import java.math.BigDecimal;
30
import java.math.RoundingMode;
31
import java.math.RoundingMode;
31
import java.util.ArrayList;
32
import java.util.ArrayList;
Line 209... Line 210...
209
    public List<ProductComponent> getChildWithQtyFrom(final List<ProductComponent> items) {
210
    public List<ProductComponent> getChildWithQtyFrom(final List<ProductComponent> items) {
210
 
211
 
211
        return getChildWithQtyFrom(items, new HashSet<Integer>());
212
        return getChildWithQtyFrom(items, new HashSet<Integer>());
212
    }
213
    }
213
 
214
 
214
    private List<ProductComponent> getChildWithQtyFrom(List<ProductComponent> items, Set<Integer> ancestors) {
215
    private List<ProductComponent> getChildWithQtyFrom(List<ProductComponent> items, Set<Integer> ancestorsOrigin) {
215
 
216
 
216
        if (root.contains("ARTICLE_ELEMENT")) {
217
        if (root.contains("ARTICLE_ELEMENT")) {
217
 
218
 
218
            int originalAncestorsSize = ancestors.size();
219
            int originalAncestorsSize = ancestorsOrigin.size();
-
 
220
            Set<Integer> ancestors = new HashSet<Integer>(ancestorsOrigin);
219
 
221
 
220
            List<ProductComponent> result = new ArrayList<ProductComponent>();
222
            List<ProductComponent> result = new ArrayList<ProductComponent>();
221
 
223
 
222
            // liste des ids parents
224
            // liste des ids parents
223
            final List<Integer> parentsArticleIDs = new ArrayList<Integer>();
225
            final List<Integer> parentsArticleIDs = new ArrayList<Integer>();
224
 
226
 
-
 
227
            // ID Article -- component
-
 
228
            ListMap<Integer, ProductComponent> productCompByID = new ListMap<Integer, ProductComponent>();
-
 
229
 
225
            // Quantité par parents
230
            // Quantité par parents
226
            Map<Integer, ProductComponent> productCompByID = new HashMap<Integer, ProductComponent>();
231
            // final ListMap<Integer, BigDecimal> qtyParentIDSource = new HashMap<Integer,
227
            final Map<Integer, BigDecimal> qtyParent = new HashMap<Integer, BigDecimal>();
232
            // BigDecimal>();
228
            for (ProductComponent p : items) {
233
            for (ProductComponent p : items) {
229
                parentsArticleIDs.add(p.getProduct().getID());
234
                productCompByID.add(p.getProduct().getID(), p);
-
 
235
                int idSource = p.getProduct().getID();
-
 
236
                parentsArticleIDs.add(idSource);
230
                BigDecimal qty = BigDecimal.ZERO;
237
                // BigDecimal qty = BigDecimal.ZERO;
231
                if (qtyParent.get(p.getProduct().getID()) != null) {
238
                // if (qtyParent.get(idSource) != null) {
232
                    qty = qtyParent.get(p.getProduct().getID());
239
                // qty = qtyParent.get(idSource);
233
                }
240
                // }
234
                qtyParent.put(p.getProduct().getID(), qty.add(p.getQty()));
241
                // qtyParent.put(idSource, qty.add(p.getQty()));
235
            }
242
            }
236
 
243
 
237
            // get all childs
244
            // get all childs
238
            final SQLTable costTable = root.getTable("ARTICLE_ELEMENT");
245
            final SQLTable costTable = root.getTable("ARTICLE_ELEMENT");
239
 
246
 
240
            SQLRowValues rowVals = new SQLRowValues(costTable);
247
            SQLRowValues rowVals = new SQLRowValues(costTable);
241
 
248
 
242
            final SQLRowValues stockRowValues = rowVals.putRowValues("ID_ARTICLE").put("ID", null).put("GESTION_STOCK", null).put("CODE", null).put("NOM", null).putRowValues("ID_STOCK");
249
            final SQLRowValues artRowValues = rowVals.putRowValues("ID_ARTICLE").putNulls("ID", "GESTION_STOCK", "CODE", "NOM", "ID_DEPOT_STOCK", "ID_UNITE_VENTE");
-
 
250
            SQLRowValues stockRowVals = new SQLRowValues(root.getTable("STOCK"));
243
            stockRowValues.putNulls("QTE_TH", "QTE_RECEPT_ATTENTE", "QTE_REEL", "QTE_LIV_ATTENTE");
251
            stockRowVals.putNulls("QTE_TH", "QTE_RECEPT_ATTENTE", "QTE_REEL", "QTE_LIV_ATTENTE", "ID_DEPOT_STOCK");
-
 
252
            stockRowVals.put("ID_ARTICLE", artRowValues);
244
            rowVals.putRowValues("ID_ARTICLE_PARENT").put("ID", null);
253
            rowVals.putRowValues("ID_ARTICLE_PARENT").put("ID", null);
245
            rowVals.put("QTE", null);
254
            rowVals.put("QTE", null);
246
            rowVals.put("QTE_UNITAIRE", null);
255
            rowVals.put("QTE_UNITAIRE", null);
247
 
256
 
248
            SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
257
            SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
Line 261... Line 270...
261
            if (childs.size() > 0) {
270
            if (childs.size() > 0) {
262
 
271
 
263
                for (SQLRowValues childRowValues : childs) {
272
                for (SQLRowValues childRowValues : childs) {
264
                    final SQLRowAccessor foreignArticleParent = childRowValues.getForeign("ID_ARTICLE_PARENT");
273
                    final SQLRowAccessor foreignArticleParent = childRowValues.getForeign("ID_ARTICLE_PARENT");
265
 
274
 
266
                    if (!childRowValues.isForeignEmpty("ID_ARTICLE") && childRowValues.getForeign("ID_ARTICLE") != null) {
275
                    if (childRowValues.getObject("ID_ARTICLE") != null && !childRowValues.isForeignEmpty("ID_ARTICLE")) {
-
 
276
 
267
                        ProductComponent childComponent = ProductComponent.createFrom(childRowValues);
277
                        List<ProductComponent> source = productCompByID.get(foreignArticleParent.getID());
268
                        // Test pour éviter les boucles dans les boms
278
                        // Test pour éviter les boucles dans les boms
269
                        if (!ancestors.contains(childComponent.getProduct().getID())) {
279
                        if (!ancestorsOrigin.contains(foreignArticleParent.getID())) {
270
                            ancestors.add(foreignArticleParent.getID());
280
                            ancestors.add(foreignArticleParent.getID());
-
 
281
                            for (ProductComponent productParent : source) {
-
 
282
 
-
 
283
                                final SQLRowAccessor foreignArticle = childRowValues.getForeign("ID_ARTICLE");
-
 
284
                                ProductComponent childComponent = ProductComponent.createFromRowArticle(foreignArticle, productParent.getSource());
-
 
285
 
271
                            // parentsArticleIDs.remove(foreignArticleParent.getID());
286
                                // parentsArticleIDs.remove(foreignArticleParent.getID());
272
                            // Calcul de la quantité qte_unit * qte * qteMergedParent
287
                                // Calcul de la quantité qte_unit * qte * qteMergedParent
273
                            childComponent.setQty(childComponent.getQty().multiply(qtyParent.get(foreignArticleParent.getID()), DecimalUtils.HIGH_PRECISION));
288
                                childComponent.setQty(childComponent.getQty().multiply(productParent.getQty(), DecimalUtils.HIGH_PRECISION));
274
 
289
 
275
                            // Cumul des valeurs si l'article est présent plusieurs fois dans le bom
290
                                // Cumul des valeurs si l'article est présent plusieurs fois dans le
-
 
291
                                // bom
-
 
292
                                // ProductComponent existProduct =
276
                            ProductComponent existProduct = productCompByID.get(childComponent.getProduct().getID());
293
                                // productCompByID.get(childComponent.getProduct().getID());
277
                            if (existProduct == null) {
294
                                // if (existProduct == null) {
-
 
295
                                // Maintenant on garde une ligne disctincte pour chaque kit
278
                                result.add(childComponent);
296
                                result.add(childComponent);
279
                                productCompByID.put(childComponent.getProduct().getID(), childComponent);
297
                                // productCompByID.put(childComponent.getProduct().getID(),
-
 
298
                                // childComponent);
280
                            } else {
299
                                // } else {
281
                                existProduct.addQty(childComponent.getQty());
300
                                // existProduct.addQty(childComponent.getQty());
-
 
301
                                // }
282
                            }
302
                            }
283
                        }
303
                        }
284
                    }
304
                    }
285
                }
305
                }
286
 
306
 
287
                // Recherche si un kit est présent parmis les articles
307
                // Recherche si un kit est présent parmis les articles
288
                final List<ProductComponent> bomFromChilds = getChildWithQtyFrom(new ArrayList(result), ancestors);
308
                final List<ProductComponent> bomFromChilds = getChildWithQtyFrom(new ArrayList(result), ancestors);
289
                // Merge des valeurs
309
                // Merge des valeurs
290
                for (ProductComponent s : bomFromChilds) {
310
                for (ProductComponent s : bomFromChilds) {
291
 
311
 
292
                    ProductComponent existProduct = productCompByID.get(s.getProduct().getID());
312
                    // ProductComponent existProduct = productCompByID.get(s.getProduct().getID());
293
                    if (existProduct == null) {
313
                    // if (existProduct == null) {
294
                        result.add(s);
314
                    result.add(s);
295
                        productCompByID.put(s.getProduct().getID(), s);
315
                    // productCompByID.put(s.getProduct().getID(), s);
296
                    } else {
316
                    // } else {
297
                        existProduct.addQty(s.getQty());
317
                    // existProduct.addQty(s.getQty());
298
                    }
318
                    // }
299
                }
319
                }
300
            }
320
            }
301
 
321
 
302
            // Ajout des articles présents dans l'ensemble de départ
322
            // Ajout des articles présents dans l'ensemble de départ
303
            if (originalAncestorsSize == 0) {
323
            if (originalAncestorsSize == 0) {
304
                for (ProductComponent p : items) {
324
                for (ProductComponent p : items) {
305
                    ProductComponent existProduct = productCompByID.get(p.getProduct().getID());
325
                    // ProductComponent existProduct = productCompByID.get(p.getProduct().getID());
306
                    if (existProduct == null) {
326
                    // if (existProduct == null) {
307
                        result.add(p);
327
                    result.add(p);
308
                        productCompByID.put(p.getProduct().getID(), p);
328
                    // productCompByID.put(p.getProduct().getID(), p);
309
                    } else {
329
                    // } else {
310
                        existProduct.addQty(p.getQty());
330
                    // existProduct.addQty(p.getQty());
311
                    }
331
                    // }
312
                }
332
                }
313
            }
333
            }
314
 
334
 
315
            // On supprime les ancestors (kits) du result
335
            // On supprime les ancestors (kits) du result
316
            for (Integer anc : ancestors) {
336
            for (Integer anc : ancestors) {
317
                ProductComponent comp = productCompByID.get(anc);
337
                // ProductComponent comp = productCompByID.get(anc);
318
                if (comp != null) {
338
                if (productCompByID.containsKey(anc)) {
319
                    result.remove(comp);
339
                    result.removeAll(productCompByID.get(anc));
320
                }
340
                }
321
            }
341
            }
322
 
342
 
323
            return result;
343
            return result;
324
        } else {
344
        } else {