Debian Codegraph @

addons/point_of_sale/static/src/app/models/utils/compute_combo_items.js

raw
1 export const computeComboItems = (
2 parentProduct,
3 childLineConf,
4 pricelist,
5 decimalPrecision,
6 productTemplateAttributeValueById,
7 childLineExtra = [],
8 currency_id = false
9 ) => {
10 const comboItems = [];
11 const parentLstPrice = parentProduct.getPrice(pricelist, 1, 0, false, parentProduct);
12 const originalTotal = childLineConf.reduce((acc, conf) => {
13 const originalPrice = conf.combo_item_id.combo_id.base_price * conf.qty;
14 return acc + originalPrice;
15 }, 0);
16
17 const getAttributesPriceExtra = (attributeValueIds) =>
18 (attributeValueIds ?? [])
19 .map((attr) => attr?.price_extra || 0)
20 .reduce((acc, price) => acc + price, 0);
21
22 let remainingTotal = parentLstPrice;
23 const ProductPrice = currency_id || decimalPrecision.find((dp) => dp.name === "Product Price");
24 for (const conf of childLineConf) {
25 const comboItem = conf.combo_item_id;
26 const combo = comboItem.combo_id;
27 let priceUnit = ProductPrice.round((combo.base_price * parentLstPrice) / originalTotal);
28 remainingTotal -= priceUnit * conf.qty;
29
30 if (comboItem.id == childLineConf[childLineConf.length - 1].combo_item_id.id) {
31 priceUnit += remainingTotal;
32 }
33 const attribute_value_ids = conf.configuration?.attribute_value_ids?.map(
34 (id) => productTemplateAttributeValueById[id]
35 );
36
37 const totalPriceExtra =
38 priceUnit + getAttributesPriceExtra(attribute_value_ids) + comboItem.extra_price;
39 comboItems.push({
40 combo_item_id: comboItem,
41 price_unit: totalPriceExtra,
42 attribute_value_ids,
43 attribute_custom_values: conf.configuration?.attribute_custom_values || {},
44 qty: conf.qty,
45 });
46 }
47
48 // Process extra child lines using combo 'base_price'
49 for (const extra of childLineExtra) {
50 const comboItem = extra.combo_item_id;
51 const priceUnit = ProductPrice.round(comboItem.combo_id.base_price);
52 const attribute_value_ids = extra.configuration?.attribute_value_ids.map(
53 (id) => productTemplateAttributeValueById[id]
54 );
55
56 const totalPriceExtra =
57 priceUnit + getAttributesPriceExtra(attribute_value_ids) + comboItem.extra_price;
58 comboItems.push({
59 combo_item_id: comboItem,
60 price_unit: totalPriceExtra,
61 attribute_value_ids,
62 attribute_custom_values: extra.configuration?.attribute_custom_values || {},
63 qty: extra.qty,
64 });
65 }
66
67 return comboItems;
68 };