Debian Codegraph @

addons/point_of_sale/tests/test_report_session.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 import odoo
3
4 from odoo.addons.point_of_sale.tests.common import TestPoSCommon
5 from odoo.exceptions import UserError
6
7 @odoo.tests.tagged('post_install', '-at_install')
8 class TestReportSession(TestPoSCommon):
9
10 def setUp(self):
11 super(TestReportSession, self).setUp()
12 self.config = self.basic_config
13
14 def test_report_session(self):
15
16 self.tax1 = self.env['account.tax'].create({
17 'name': 'Tax 1',
18 'amount': 10,
19 'price_include_override': 'tax_included',
20 })
21 self.product1 = self.create_product('Product A', self.categ_basic, 110, self.tax1.id)
22 product_to_archive = self.create_product('Product to archive', self.categ_basic, 100, self.tax1.id)
23
24 self.config.open_ui()
25 # check that an unsed product can be archived by any user with archive rights
26 self.res_users_stock_user.group_ids |= self.env.ref('product.group_product_manager')
27 product_to_archive.with_user(self.res_users_stock_user).action_archive()
28
29 session_id = self.config.current_session_id.id
30 order = self.env['pos.order'].create({
31 'company_id': self.env.company.id,
32 'session_id': session_id,
33 'partner_id': self.partner_a.id,
34 'lines': [(0, 0, {
35 'name': "OL/0001",
36 'product_id': self.product1.id,
37 'price_unit': 110,
38 'discount': 0,
39 'qty': 1,
40 'tax_ids': [[6, False, [self.tax1.id]]],
41 'price_subtotal': 100,
42 'price_subtotal_incl': 110,
43 })],
44 'pricelist_id': self.config.pricelist_id.id,
45 'amount_paid': 110.0,
46 'amount_total': 110.0,
47 'amount_tax': 10.0,
48 'amount_return': 0.0,
49 'last_order_preparation_change': '{}',
50 'to_invoice': False,
51 })
52 # check that an used product can not be archived
53 with self.assertRaisesRegex(UserError, "Hold up! Archiving products while POS sessions are active is like pulling a plate mid-meal.\nMake sure to close all sessions first to avoid any issues."):
54 self.product1.with_user(self.res_users_stock_user).action_archive()
55
56 self.make_payment(order, self.bank_split_pm1, 60)
57 self.make_payment(order, self.bank_pm1, 50)
58
59 self.config.current_session_id.action_pos_session_closing_control(bank_payment_method_diffs={self.bank_split_pm1.id: 50, self.bank_pm1.id: 40})
60
61 # PoS Orders have negative IDs to avoid conflict, so reports[0] will correspond to the newest order
62 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details(session_ids=[session_id])
63 split_payment_bank = [p for p in report['payments'] if p.get('id', 0) == self.bank_split_pm1.id]
64 self.assertEqual(split_payment_bank[0]['cash_moves'][0]['amount'], 50)
65 bank_payment = [p for p in report['payments'] if p.get('id', 0) == self.bank_pm1.id]
66 # self.assertEqual(bank_payment[0]['cash_moves'][0]['amount'], 40) TODO WAN
67 self.assertEqual(report['products_info']['total'], 100, "Total amount of products should be 100, as we want total without tax")
68 self.assertEqual(report['products'][0]['products'][0]['base_amount'], 100, "Base amount of product should be 100, as we want price without tax")
69
70 def test_report_session_2(self):
71
72 self.product1 = self.create_product('Product A', self.categ_basic, 100)
73
74 self.config.open_ui()
75 session_id_1 = self.config.current_session_id.id
76 order_info = {'company_id': self.env.company.id,
77 'session_id': session_id_1,
78 'partner_id': self.partner_a.id,
79 'lines': [(0, 0, {
80 'name': "OL/0001",
81 'product_id': self.product1.id,
82 'price_unit': 100,
83 'discount': 0,
84 'qty': 1,
85 'tax_ids': [],
86 'price_subtotal': 100,
87 'price_subtotal_incl': 100,
88 })],
89 'pricelist_id': self.config.pricelist_id.id,
90 'amount_paid': 100.0,
91 'amount_total': 100.0,
92 'amount_tax': 0.0,
93 'amount_return': 0.0,
94 'to_invoice': False,
95 }
96
97 order = self.env['pos.order'].create(order_info)
98 self.make_payment(order, self.bank_pm1, 100)
99
100 order = self.env['pos.order'].create(order_info)
101 self.make_payment(order, self.cash_pm1, 100)
102
103 self.config.current_session_id.action_pos_session_closing_control()
104
105 self.config.open_ui()
106 session_id_2 = self.config.current_session_id.id
107 order_info['session_id'] = session_id_2
108 order = self.env['pos.order'].create(order_info)
109 self.make_payment(order, self.bank_pm1, 100)
110
111 order = self.env['pos.order'].create(order_info)
112 self.make_payment(order, self.cash_pm1, 100)
113
114 self.config.current_session_id.action_pos_session_closing_control()
115
116 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details()
117 for payment in report['payments']:
118 session_name = self.env['pos.session'].browse(payment['session']).name
119 payment_method_name = self.env['pos.payment.method'].browse(payment['id']).name
120 self.assertEqual(payment['name'], payment_method_name + " " + session_name)
121
122 pdf = self.env['ir.actions.report']._render_qweb_pdf('point_of_sale.sale_details_report', res_ids=session_id_2)
123 self.assertTrue(pdf)
124
125 def test_report_listing(self):
126 product1 = self.create_product('Product 1', self.categ_basic, 150)
127 product2 = self.create_product('Product 2', self.categ_basic, 150)
128
129 cash_payment_method = self.env['pos.payment.method'].create({
130 'name': 'Cash',
131 'receivable_account_id': self.company_data['default_account_receivable'].id,
132 'journal_id': self.company_data['default_journal_cash'].id,
133 'company_id': self.env.company.id,
134 })
135 bank_payment_method = self.env['pos.payment.method'].create({
136 'name': 'Bank',
137 'journal_id': self.company_data['default_journal_bank'].id,
138 'receivable_account_id': self.company_data['default_account_receivable'].id,
139 'company_id': self.env.company.id,
140 })
141 self.config.write({'payment_method_ids': [(4, bank_payment_method.id), (4, cash_payment_method.id)]})
142
143 self.open_new_session()
144 session = self.pos_session
145
146 self.tax_sale_a['amount'] = 10
147 order = self.env['pos.order'].create({
148 'session_id': session.id,
149 'lines': [(0, 0, {
150 'name': "TR/0001",
151 'product_id': product1.id,
152 'price_unit': 150,
153 'discount': 0,
154 'qty': 1.0,
155 'price_subtotal': 150,
156 'tax_ids': [(6, 0, self.tax_sale_a.ids)],
157 'price_subtotal_incl': 165,
158 }), (0, 0, {
159 'name': "TR/0001",
160 'product_id': product2.id,
161 'price_unit': 150,
162 'discount': 0,
163 'qty': 1.0,
164 'price_subtotal': 150,
165 'tax_ids': [(6, 0, self.tax_sale_a.ids)],
166 'price_subtotal_incl': 165,
167 })],
168 'amount_total': 330.0,
169 'amount_tax': 30.0,
170 'amount_paid': 0.0,
171 'amount_return': 0.0,
172 })
173 payment_context = {"active_ids": order.ids, "active_id": order.id}
174
175 order_payment = self.env['pos.make.payment'].with_context(**payment_context).create([{
176 'amount': am,
177 'payment_method_id': pm
178 } for am in [65, 100] for pm in [cash_payment_method.id, bank_payment_method.id]])
179 for payment in order_payment:
180 payment.with_context(**payment_context).check()
181
182 order_report_lines = self.env['report.pos.order'].sudo().search([('order_id', '=', order.id)])
183
184 self.assertEqual(len(order_report_lines), 2)
185 self.assertEqual(order_report_lines[0].payment_method_id.id, order_report_lines[1].payment_method_id.id)
186
187 for order in order_report_lines:
188 self.assertEqual(order.price_total, 165.0)
189 self.assertEqual(order.nbr_lines, 1)
190 self.assertEqual(order.product_qty, 1)
191
192 order_report_lines_count_product1 = self.env['report.pos.order'].sudo().search_count([('product_id', '=', product1.id)])
193 order_report_lines_count_product2 = self.env['report.pos.order'].sudo().search_count([('product_id', '=', product2.id)])
194
195 self.assertEqual(order_report_lines_count_product1, 1)
196 self.assertEqual(order_report_lines_count_product2, 1)
197
198 def test_report_session_3(self):
199 self.product1 = self.create_product('Product A', self.categ_basic, 100)
200 self.config.open_ui()
201 session_id = self.config.current_session_id.id
202 order_info = {'company_id': self.env.company.id,
203 'session_id': session_id,
204 'partner_id': self.partner_a.id,
205 'lines': [(0, 0, {
206 'name': "OL/0001",
207 'product_id': self.product1.id,
208 'price_unit': 0,
209 'discount': 0,
210 'qty': 14.9,
211 'tax_ids': [],
212 'price_subtotal': 0,
213 'price_subtotal_incl': 0,
214 })],
215 'pricelist_id': self.config.pricelist_id.id,
216 'amount_paid': 0.0,
217 'amount_total': 0.0,
218 'amount_tax': 0.0,
219 'amount_return': 0.0,
220 'to_invoice': False,
221 }
222 order = self.env['pos.order'].create(order_info)
223 self.make_payment(order, self.bank_pm1, 0)
224 order_info['lines'][0][2]['qty'] = 59.7
225 order = self.env['pos.order'].create(order_info)
226 self.make_payment(order, self.bank_pm1, 0)
227 self.config.current_session_id.action_pos_session_closing_control()
228 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details()
229 self.assertEqual(report['products'][0]['products'][0]['quantity'], 74.6, "Quantity of product should be 74.6, as we want the sum of the quantity of the two orders")
230
231 def test_report_bank_expected_different_than_counted(self):
232 """
233 Test that in the pos session report, the difference between the expected and counted bank payment is correct.
234 Test both with a default outstanding account on the payment and without.
235 """
236 self.tax1 = self.env['account.tax'].create({
237 'name': 'Tax 1',
238 'amount': 10,
239 'price_include_override': 'tax_included',
240 })
241 self.product1 = self.create_product('Product A', self.categ_basic, 100, self.tax1.id)
242
243 self.bank_pm1.outstanding_account_id = self.outstanding_bank.id
244 self.config.open_ui()
245
246 session1_id = self.config.current_session_id.id
247 order1 = self.env['pos.order'].create({
248 'company_id': self.env.company.id,
249 'session_id': session1_id,
250 'partner_id': self.partner_a.id,
251 'lines': [(0, 0, {
252 'name': "OL/0001",
253 'product_id': self.product1.id,
254 'price_unit': 100,
255 'discount': 0,
256 'qty': 1,
257 'tax_ids': [[6, False, [self.tax1.id]]],
258 'price_subtotal': 100,
259 'price_subtotal_incl': 100,
260 })],
261 'pricelist_id': self.config.pricelist_id.id,
262 'amount_paid': 100.0,
263 'amount_total': 100.0,
264 'amount_tax': 10.0,
265 'amount_return': 0.0,
266 'last_order_preparation_change': '{}',
267 'to_invoice': False,
268 })
269
270 self.make_payment(order1, self.bank_pm1, 100)
271
272 self.config.current_session_id.action_pos_session_closing_control(
273 bank_payment_method_diffs={self.bank_pm1.id: -20})
274 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details(session_ids=[session1_id])
275 self.assertEqual(report['payments'][1]['money_difference'], -20)
276
277 self.bank_pm1.outstanding_account_id = False
278 self.config.open_ui()
279
280 session2_id = self.config.current_session_id.id
281 order2 = self.env['pos.order'].create({
282 'company_id': self.env.company.id,
283 'session_id': session2_id,
284 'partner_id': self.partner_a.id,
285 'lines': [(0, 0, {
286 'name': "OL/0001",
287 'product_id': self.product1.id,
288 'price_unit': 100,
289 'discount': 0,
290 'qty': 1,
291 'tax_ids': [[6, False, [self.tax1.id]]],
292 'price_subtotal': 100,
293 'price_subtotal_incl': 100,
294 })],
295 'pricelist_id': self.config.pricelist_id.id,
296 'amount_paid': 100.0,
297 'amount_total': 100.0,
298 'amount_tax': 10.0,
299 'amount_return': 0.0,
300 'last_order_preparation_change': '{}',
301 'to_invoice': False,
302 })
303
304 self.make_payment(order2, self.bank_pm1, 100)
305
306 self.config.current_session_id.action_pos_session_closing_control(bank_payment_method_diffs={self.bank_pm1.id: -20})
307 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details(session_ids=[session2_id])
308 self.assertEqual(report['payments'][1]['money_difference'], -20)
309
310 def test_report_session_4(self):
311 self.tax1 = self.env['account.tax'].create({
312 'name': 'Tax 1',
313 'amount': 10,
314 'price_include': True,
315 })
316
317 self.tax2 = self.env['account.tax'].create({
318 'name': 'Tax 2',
319 'amount': 15,
320 'price_include': True,
321 })
322 self.product1 = self.create_product('Product A', self.categ_basic, 125, self.tax1.id)
323
324 self.config.open_ui()
325 session_id = self.config.current_session_id.id
326 order_info = {
327 'company_id': self.env.company.id,
328 'session_id': session_id,
329 'partner_id': self.partner_a.id,
330 'lines': [(0, 0, {
331 'name': "OL/0001",
332 'product_id': self.product1.id,
333 'price_unit': 125,
334 'discount': 0,
335 'qty': 1,
336 'tax_ids': [[6, False, [self.tax1.id, self.tax2.id]]],
337 'price_subtotal': 100,
338 'price_subtotal_incl': 125,
339 })],
340 'pricelist_id': self.config.pricelist_id.id,
341 'amount_paid': 125.0,
342 'amount_total': 156.25,
343 'amount_tax': 25.0,
344 'amount_return': 0.0,
345 'last_order_preparation_change': '{}',
346 'to_invoice': False,
347 }
348 order = self.env['pos.order'].create(order_info)
349 self.make_payment(order, self.bank_pm1, 156.25)
350 self.config.current_session_id.action_pos_session_closing_control()
351 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details()
352 self.assertEqual(report["taxes_info"]["base_amount"], 100, "Base amount should be equal to 100")
353
354 def test_report_sum_taxes_base_amounts(self):
355 tax_included = self.env['account.tax'].create({
356 'name': 'Tax Included',
357 'amount': 5,
358 'price_include_override': 'tax_included',
359 })
360 product = self.create_product('Product A', self.categ_basic, 110, tax_included.id)
361
362 self.config.open_ui()
363 session_id = self.config.current_session_id.id
364 order_info = [{
365 'company_id': self.env.company.id,
366 'session_id': session_id,
367 'lines': [(0, 0, {
368 'name': "OL/0001",
369 'product_id': product.id,
370 'price_unit': 100,
371 'discount': 0,
372 'qty': 1,
373 'tax_ids': [[6, False, [tax_included.id]]],
374 'price_subtotal': 100,
375 'price_subtotal_incl': 100,
376 })],
377 'pricelist_id': self.config.pricelist_id.id,
378 'amount_paid': 100.0,
379 'amount_total': 100.0,
380 'amount_tax': 4.76,
381 'amount_return': 0.0,
382 'last_order_preparation_change': '{}',
383 'to_invoice': False,
384 } for _ in range(5)]
385
386 for order_data in order_info:
387 order = self.env['pos.order'].create(order_data)
388 self.make_payment(order, self.bank_pm1, 100.0)
389
390 self.config.current_session_id.action_pos_session_closing_control()
391 report = self.env['report.point_of_sale.report_saledetails'].get_sale_details()
392 self.assertAlmostEqual(report["taxes"][0]["base_amount"], 95.24 * 5, msg="Base amount should be equal to 476.20")