Debian Codegraph @

addons/point_of_sale/static/tests/unit/models/pos_order_line.test.js

raw
1 import { test, expect, describe } from "@odoo/hoot";
2 import { setupPosEnv, getFilledOrder } from "../utils";
3 import { definePosModels } from "../data/generate_model_definitions";
4
5 definePosModels();
6
7 function getAllPricesData(otherData = {}) {
8 return {
9 "pos.order": [
10 {
11 id: 1,
12 name: "Test Order",
13 lines: [1],
14 },
15 {
16 id: 2,
17 name: "Test Combo order",
18 },
19 ],
20 "pos.order.line": [
21 {
22 id: 1,
23 order_id: 1,
24 product_id: 5,
25 price_unit: 100.0,
26 qty: 2,
27 tax_ids: [1],
28 },
29 {
30 id: 2,
31 order_id: 2,
32 product_id: 7,
33 price_unit: 0.0,
34 qty: 1,
35 combo_line_ids: [3, 4],
36 tax_ids: [],
37 },
38 {
39 id: 3,
40 order_id: 2,
41 product_id: 8,
42 price_unit: 1,
43 qty: 2,
44 combo_parent_id: 2,
45 tax_ids: [],
46 },
47 {
48 id: 4,
49 order_id: 2,
50 product_id: 10,
51 price_unit: 8,
52 qty: 1,
53 combo_parent_id: 2,
54 tax_ids: [],
55 },
56 ],
57 ...otherData,
58 };
59 }
60
61 test("[get prices()] Base test", async () => {
62 const store = await setupPosEnv();
63 const models = store.models;
64 const data = models.loadConnectedData(getAllPricesData());
65
66 const lineTax = data["pos.order.line"][0].prices;
67 expect(lineTax.total_included).toBe(230.0);
68 expect(lineTax.total_excluded).toBe(200.0);
69 expect(lineTax.taxes_data[0].base_amount).toBe(200.0);
70 expect(lineTax.taxes_data[0].tax_amount).toBe(30.0);
71
72 // Test with line qty = 0
73 data["pos.order.line"][0].qty = 0;
74 const zeroQtyLineTax = data["pos.order.line"][0].prices;
75 expect(zeroQtyLineTax.total_included).toBe(0.0);
76 expect(zeroQtyLineTax.total_excluded).toBe(0.0);
77 expect(zeroQtyLineTax.taxes_data[0].base_amount).toBe(0.0);
78 expect(zeroQtyLineTax.taxes_data[0].tax_amount).toBe(0.0);
79 });
80
81 test("[get prices()] with discount applied", async () => {
82 const store = await setupPosEnv();
83 const models = store.models;
84 const data = models.loadConnectedData(getAllPricesData());
85 const orderLine = data["pos.order.line"][0];
86
87 // Prices with a discount of 10% applied: 230 * 0.9 = 207.0
88 orderLine.setDiscount(10.0); // 10% discount
89 const lineTax = orderLine.prices;
90 expect(lineTax.total_included).toBe(207.0);
91 expect(lineTax.total_excluded).toBe(180.0);
92 expect(lineTax.taxes_data[0].tax_amount).toBe(27.0);
93
94 // Price with a discount of 100% applied
95 orderLine.setDiscount(100.0);
96 const updatedLineTax = orderLine.prices;
97 expect(updatedLineTax.total_excluded).toBe(0.0);
98 expect(updatedLineTax.total_included).toBe(0.0);
99 expect(updatedLineTax.taxes_data[0].tax_amount).toBe(0.0);
100 });
101
102 test("[get prices()] with multiple taxes settings", async () => {
103 const store = await setupPosEnv();
104 const models = store.models;
105 const rawData = getAllPricesData();
106 const product = models["product.product"].get(5);
107 product.taxes_id = models["account.tax"].readMany([1, 2]); // Set two taxes on the product
108 rawData["pos.order.line"][0].qty = 1;
109 rawData["pos.order.line"][0].tax_ids = [1, 2];
110
111 const data = models.loadConnectedData(rawData);
112 const orderLine = data["pos.order.line"][0];
113
114 // Test with two taxes applied (15% and 25%)
115 const lineTax = orderLine.prices;
116 expect(lineTax.total_excluded).toBe(100.0);
117 expect(lineTax.total_included).toBe(140.0);
118 expect(lineTax.taxes_data[0].tax_amount).toBe(15.0);
119 expect(lineTax.taxes_data[1].tax_amount).toBe(25.0);
120
121 // Test with "include_base_amount" and "include_base_amount" to true for both taxes
122 models["account.tax"].get(1).include_base_amount = true;
123 models["account.tax"].get(2).include_base_amount = true;
124 orderLine.order_id.triggerRecomputeAllPrices(); // Force the recompute because updating taxes do not trigger it
125 const updatedLineTax = orderLine.prices;
126 expect(updatedLineTax.total_excluded).toBe(100.0);
127 expect(updatedLineTax.total_included).toBe(143.75);
128 expect(updatedLineTax.taxes_data[0].tax_amount).toBe(15.0);
129 expect(updatedLineTax.taxes_data[1].tax_amount).toBe(28.75);
130
131 // Test without any taxes
132 product.taxes_id = [];
133 orderLine.tax_ids = []; // Do not need to force the recompute here, changing line taxes does it for us
134 const noTaxLine = data["pos.order.line"][0].prices;
135 expect(noTaxLine.total_excluded).toBe(100.0);
136 expect(noTaxLine.total_included).toBe(100.0);
137 expect(noTaxLine.taxes_data).toHaveLength(0);
138 });
139
140 test("[get prices()] with fixed-amount tax", async () => {
141 const store = await setupPosEnv();
142 const models = store.models;
143 const data = models.loadConnectedData(getAllPricesData());
144 models["account.tax"].get(1).amount_type = "fixed";
145 const orderLine = data["pos.order.line"][0];
146 orderLine.qty = 3;
147 orderLine.price_unit = 10.0;
148 const lineTax = orderLine.prices;
149 // 3 * 10 = 30, tax = 3 * 15 = 45
150 expect(lineTax.total_excluded).toBe(30.0);
151 expect(lineTax.total_included).toBe(75.0);
152 expect(lineTax.taxes_data[0].tax_amount).toBe(45.0);
153 });
154
155 test("[get prices()] with one price-included and one price-excluded tax", async () => {
156 const store = await setupPosEnv();
157 const models = store.models;
158 const product = models["product.product"].get(5);
159 const rawData = getAllPricesData();
160 models["account.tax"].get(1).price_include = true;
161 product.taxes_id = models["account.tax"].readMany([1, 2]);
162 rawData["pos.order.line"][0].tax_ids = [1, 2];
163
164 const data = models.loadConnectedData(rawData);
165 const orderLine = data["pos.order.line"][0];
166 orderLine.qty = 1;
167 orderLine.price_unit = 115.0; // price includes 15% tax
168 const lineTax = orderLine.prices;
169 // priceWithoutTax: 115 / 1.15 = 100, 25% tax = 25, priceWithTax = 115 + 25 = 140
170 expect(lineTax.total_excluded).toBe(100.0);
171 expect(lineTax.total_included).toBe(140.0);
172 expect(lineTax.taxes_data[0].tax_amount).toBe(15.0);
173 expect(lineTax.taxes_data[1].tax_amount).toBe(25.0);
174 });
175
176 test("[get quantityStr] Base test", async () => {
177 const store = await setupPosEnv();
178 const models = store.models;
179 const data = models.loadConnectedData(getAllPricesData());
180 const orderLine = data["pos.order.line"][0];
181 const qtyStr = orderLine.quantityStr; // Test with qty = 2
182 expect(qtyStr.qtyStr).toBe("2");
183 expect(qtyStr.unitPart).toBe("2");
184 expect(qtyStr.decimalPoint).toBe(".");
185 expect(qtyStr.decimalPart).toBe("");
186
187 orderLine.qty = 2.5; // Test with qty = 2.5
188 const updatedQtyStr = orderLine.quantityStr;
189 expect(updatedQtyStr.qtyStr).toBe("2.50");
190 expect(updatedQtyStr.unitPart).toBe("2");
191 expect(updatedQtyStr.decimalPoint).toBe(".");
192 expect(updatedQtyStr.decimalPart).toBe("50");
193 });
194
195 test("[setQuantity] Base test", async () => {
196 const store = await setupPosEnv();
197 const models = store.models;
198 const data = models.loadConnectedData(getAllPricesData());
199 const orderLine = data["pos.order.line"][0];
200 orderLine.setQuantity(2.77);
201 expect(orderLine.qty).toBe(2.77);
202 orderLine.setQuantity(2.779);
203 expect(orderLine.qty).toBe(2.78);
204 orderLine.setQuantity(2.771);
205 expect(orderLine.qty).toBe(2.77);
206
207 const comboOrderline = data["pos.order.line"][1];
208 const comboChild1 = data["pos.order.line"][2];
209 const comboChild2 = data["pos.order.line"][3];
210 expect(comboOrderline.qty).toBe(1);
211 expect(comboChild1.qty).toBe(2);
212 expect(comboChild2.qty).toBe(1);
213 expect(comboOrderline.price_unit).toBe(0);
214 expect(comboChild1.price_unit).toBe(1);
215 expect(comboChild2.price_unit).toBe(8);
216 comboOrderline.setQuantity(3, true);
217 expect(comboOrderline.qty).toBe(3);
218 expect(comboChild1.qty).toBe(6);
219 expect(comboChild2.qty).toBe(3);
220 expect(comboOrderline.price_unit).toBe(0);
221 expect(comboChild1.price_unit).toBe(1);
222 expect(comboChild2.price_unit).toBe(8);
223 comboOrderline.setQuantity(2, true);
224 expect(comboOrderline.qty).toBe(2);
225 expect(comboChild1.qty).toBe(4);
226 expect(comboChild2.qty).toBe(2);
227 expect(comboOrderline.price_unit).toBe(0);
228 expect(comboChild1.price_unit).toBe(1);
229 expect(comboChild2.price_unit).toBe(8);
230 });
231
232 test("[canBeMergedWith]: Base test", async () => {
233 const store = await setupPosEnv();
234 // Test with different products
235 const order = await getFilledOrder(store);
236 const line1 = order.lines[0];
237 const line2 = order.lines[1];
238 expect(line1.canBeMergedWith(line2)).toBe(false);
239 // Test with same product, same price, same qty, no discount, different name
240 line2.product_id = line1.product_id;
241 expect(line1.canBeMergedWith(line2)).toBe(false);
242 // Test with same product, same price, same qty, no discount, same name
243 line2.setFullProductName("TEST");
244 expect(line1.canBeMergedWith(line2)).toBe(true);
245 // Test with different note
246 line1.setNote("Test note");
247 expect(line1.canBeMergedWith(line2)).toBe(false);
248 // Test with same note
249 line2.setNote("Test note");
250 expect(line1.canBeMergedWith(line2)).toBe(true);
251 // Test with discount applied
252 line1.setDiscount(10.0);
253 expect(line1.canBeMergedWith(line2)).toBe(false);
254 // Test with same discount
255 line2.setDiscount(10.0);
256 expect(line1.canBeMergedWith(line2)).toBe(true);
257 // Test with different price unit
258 line2.price_unit = line1.price_unit + 1;
259 line2.price_type = "manual";
260 expect(line1.canBeMergedWith(line2)).toBe(false);
261 // Test to merge lines
262 line1.merge(line2);
263 expect(line1.qty).toBe(5);
264 });
265
266 describe("Test taxes after fiscal position", () => {
267 test("Orderline containing a taxed product", async () => {
268 const store = await setupPosEnv();
269 const models = store.models;
270 const dataDict = getAllPricesData();
271 dataDict["pos.order"][0]["fiscal_position_id"] = 2;
272 const data = models.loadConnectedData(dataDict);
273 const orderLine = data["pos.order.line"][0];
274 const lineValues = orderLine.prepareBaseLineForTaxesComputationExtraValues();
275 expect(lineValues.tax_ids.length).toBe(0);
276 });
277 test("Taxed Orderline orderline after fiscal position", async () => {
278 const store = await setupPosEnv();
279 const models = store.models;
280 const dataDict = getAllPricesData();
281 dataDict["pos.order"][0]["fiscal_position_id"] = 1;
282 const data = models.loadConnectedData(dataDict);
283 // Taxed order line
284 const orderLine = data["pos.order.line"][0];
285 // Taxed Product
286 const taxedProductLineValues = orderLine.prepareBaseLineForTaxesComputationExtraValues();
287 expect(taxedProductLineValues.tax_ids.length).toBe(1);
288 // Non Taxed Product
289 orderLine.product_id.taxes_id = [];
290 const nonTaxedProductlineValues = orderLine.prepareBaseLineForTaxesComputationExtraValues();
291 expect(nonTaxedProductlineValues.tax_ids.length).toBe(1);
292 });
293 test("Non-taxed orderline after fiscal position", async () => {
294 const store = await setupPosEnv();
295 const models = store.models;
296 const dataDict = getAllPricesData();
297 dataDict["pos.order"][0]["fiscal_position_id"] = 1;
298 const data = models.loadConnectedData(dataDict);
299 const orderLine = data["pos.order.line"][0];
300 // Non taxed order line
301 orderLine.tax_ids = [];
302 // Taxed product
303 const taxedProductLineValues = orderLine.prepareBaseLineForTaxesComputationExtraValues();
304 expect(taxedProductLineValues.tax_ids.length).toBe(0);
305 orderLine.product_id.taxes_id = [];
306 // Non Taxed product
307 const nonTaxedProductlineValues = orderLine.prepareBaseLineForTaxesComputationExtraValues();
308 expect(nonTaxedProductlineValues.tax_ids.length).toBe(0);
309 });
310 });