Debian Codegraph @

addons/point_of_sale/tests/test_pos_basic_config.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 import odoo
5
6 from odoo import fields
7 from odoo.addons.point_of_sale.tests.common import TestPoSCommon
8 from odoo.exceptions import ValidationError
9 from freezegun import freeze_time
10 from dateutil.relativedelta import relativedelta
11 from datetime import datetime, timedelta
12 from pprint import pformat
13 import unittest.mock
14
15 @odoo.tests.tagged('post_install', '-at_install')
16 class TestPoSBasicConfig(TestPoSCommon):
17 """ Test PoS with basic configuration
18
19 The tests contain base scenarios in using pos.
20 More specialized cases are tested in other tests.
21 """
22
23 def setUp(self):
24 super(TestPoSBasicConfig, self).setUp()
25 self.config = self.basic_config
26 self.product0 = self.create_product('Product 0', self.categ_basic, 0.0, 0.0)
27 self.product1 = self.create_product('Product 1', self.categ_basic, 10.0, 5)
28 self.product2 = self.create_product('Product 2', self.categ_basic, 20.0, 10)
29 self.product3 = self.create_product('Product 3', self.categ_basic, 30.0, 15)
30 self.product4 = self.create_product('Product_4', self.categ_basic, 9.96, 4.98)
31 self.product99 = self.create_product('Product_99', self.categ_basic, 99, 50)
32 self.product_multi_tax = self.create_product('Multi-tax product', self.categ_basic, 100, 100, (self.taxes['tax8'] | self.taxes['tax9']).ids)
33 self.adjust_inventory([self.product1, self.product2, self.product3], [100, 50, 50])
34 self.company_data_2 = self.setup_other_company()
35
36 def test_orders_no_invoiced(self):
37 """ Test for orders without invoice
38
39 3 orders
40 - first 2 orders with cash payment
41 - last order with bank payment
42
43 Orders
44 ======
45 +---------+----------+-----------+----------+-----+-------+
46 | order | payments | invoiced? | product | qty | total |
47 +---------+----------+-----------+----------+-----+-------+
48 | order 1 | cash | no | product1 | 10 | 100 |
49 | | | | product2 | 5 | 100 |
50 +---------+----------+-----------+----------+-----+-------+
51 | order 2 | cash | no | product2 | 7 | 140 |
52 | | | | product3 | 1 | 30 |
53 +---------+----------+-----------+----------+-----+-------+
54 | order 3 | bank | no | product1 | 1 | 10 |
55 | | | | product2 | 3 | 60 |
56 | | | | product3 | 5 | 150 |
57 +---------+----------+-----------+----------+-----+-------+
58
59 Expected Result
60 ===============
61 +---------------------+---------+
62 | account | balance |
63 +---------------------+---------+
64 | sale | -590 |
65 | pos receivable cash | 370 |
66 | pos receivable bank | 220 |
67 +---------------------+---------+
68 | Total balance | 0.0 |
69 +---------------------+---------+
70 """
71 start_qty_available = {
72 self.product1: self.product1.qty_available,
73 self.product2: self.product2.qty_available,
74 self.product3: self.product3.qty_available,
75 }
76
77 def _before_closing_cb():
78 # check values before closing the session
79 self.assertEqual(3, self.pos_session.order_count)
80 orders_total = sum(order.amount_total for order in self.pos_session.order_ids)
81 self.assertAlmostEqual(orders_total, self.pos_session.total_payments_amount, msg='Total order amount should be equal to the total payment amount.')
82
83 # check product qty_available after syncing the order
84 self.assertEqual(
85 self.product1.qty_available + 11,
86 start_qty_available[self.product1],
87 )
88 self.assertEqual(
89 self.product2.qty_available + 15,
90 start_qty_available[self.product2],
91 )
92 self.assertEqual(
93 self.product3.qty_available + 6,
94 start_qty_available[self.product3],
95 )
96
97 # picking and stock moves should be in done state
98 for order in self.pos_session.order_ids:
99 self.assertEqual(
100 order.picking_ids[0].state,
101 'done',
102 'Picking should be in done state.'
103 )
104 move_ids = order.picking_ids[0].move_ids
105 self.assertEqual(
106 move_ids.mapped('state'),
107 ['done'] * len(move_ids),
108 'Move Lines should be in done state.'
109 )
110
111 self._run_test({
112 'payment_methods': self.cash_pm1 | self.bank_pm1,
113 'orders': [
114 {'pos_order_lines_ui_args': [(self.product1, 10), (self.product2, 5)], 'uuid': '00100-010-0001'},
115 {'pos_order_lines_ui_args': [(self.product2, 7), (self.product3, 1)], 'uuid': '00100-010-0002'},
116 {'pos_order_lines_ui_args': [(self.product1, 1), (self.product3, 5), (self.product2, 3)], 'payments': [(self.bank_pm1, 220)], 'uuid': '00100-010-0003'},
117 ],
118 'before_closing_cb': _before_closing_cb,
119 'journal_entries_before_closing': {},
120 'journal_entries_after_closing': {
121 'session_journal_entry': {
122 'line_ids': [
123 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 590, 'reconciled': False},
124 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 220, 'credit': 0, 'reconciled': True},
125 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 370, 'credit': 0, 'reconciled': True},
126 ],
127 },
128 'cash_statement': [
129 ((370, ), {
130 'line_ids': [
131 {'account_id': self.cash_pm1.journal_id.default_account_id.id, 'partner_id': False, 'debit': 370, 'credit': 0, 'reconciled': False},
132 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 370, 'reconciled': True},
133 ]
134 }),
135 ],
136 'bank_payments': [
137 ((220, ), {
138 'line_ids': [
139 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 220, 'credit': 0, 'reconciled': False},
140 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 220, 'reconciled': True},
141 ]
142 }),
143 ],
144 },
145 })
146
147 def test_orders_with_invoiced(self):
148 """ Test for orders: one with invoice
149
150 3 orders
151 - order 1, paid by cash
152 - order 2, paid by bank
153 - order 3, paid by bank, invoiced
154
155 Orders
156 ======
157 +---------+----------+---------------+----------+-----+-------+
158 | order | payments | invoiced? | product | qty | total |
159 +---------+----------+---------------+----------+-----+-------+
160 | order 1 | cash | no | product1 | 6 | 60 |
161 | | | | product2 | 3 | 60 |
162 | | | | product3 | 1 | 30 |
163 +---------+----------+---------------+----------+-----+-------+
164 | order 2 | bank | no | product1 | 1 | 10 |
165 | | | | product2 | 20 | 400 |
166 +---------+----------+---------------+----------+-----+-------+
167 | order 3 | bank | yes, customer | product1 | 10 | 100 |
168 | | | | product3 | 1 | 30 |
169 +---------+----------+---------------+----------+-----+-------+
170
171 Expected Result
172 ===============
173 +---------------------+---------+
174 | account | balance |
175 +---------------------+---------+
176 | sale | -560 |
177 | pos receivable cash | 150 |
178 | pos receivable bank | 540 |
179 | receivable | -130 |
180 +---------------------+---------+
181 | Total balance | 0.0 |
182 +---------------------+---------+
183 """
184 start_qty_available = {
185 self.product1: self.product1.qty_available,
186 self.product2: self.product2.qty_available,
187 self.product3: self.product3.qty_available,
188 }
189
190 def _before_closing_cb():
191 # check values before closing the session
192 self.assertEqual(3, self.pos_session.order_count)
193 orders_total = sum(order.amount_total for order in self.pos_session.order_ids)
194 self.assertAlmostEqual(orders_total, self.pos_session.total_payments_amount, msg='Total order amount should be equal to the total payment amount.')
195
196 # check product qty_available after syncing the order
197 self.assertEqual(
198 self.product1.qty_available + 17,
199 start_qty_available[self.product1],
200 )
201 self.assertEqual(
202 self.product2.qty_available + 23,
203 start_qty_available[self.product2],
204 )
205 self.assertEqual(
206 self.product3.qty_available + 2,
207 start_qty_available[self.product3],
208 )
209
210 # picking and stock moves should be in done state
211 # no exception for invoiced orders
212 for order in self.pos_session.order_ids:
213 self.assertEqual(
214 order.picking_ids[0].state,
215 'done',
216 'Picking should be in done state.'
217 )
218 move_ids = order.picking_ids[0].move_ids
219 self.assertEqual(
220 move_ids.mapped('state'),
221 ['done'] * len(move_ids),
222 'Move Lines should be in done state.'
223 )
224
225 # check account move in the invoiced order
226 invoiced_order = self.pos_session.order_ids.filtered(lambda order: order.account_move)
227 self.assertEqual(1, len(invoiced_order), 'Only one order is invoiced in this test.')
228
229 # check account_move of orders before validating the session.
230 self.assertTrue(invoiced_order.account_move, msg="Invoiced orders must have account_move.")
231 uninvoiced_orders = self.pos_session.order_ids - invoiced_order
232 self.assertTrue(
233 all(not order.account_move for order in uninvoiced_orders),
234 msg="Uninvoiced orders do not have account_move."
235 )
236
237 def _after_closing_cb():
238 # check state of orders after validating the session.
239 uninvoiced_orders = self.pos_session.order_ids.filtered(lambda order: not order.is_invoiced)
240 self.assertTrue(
241 all([order.state == 'done' for order in uninvoiced_orders]),
242 msg="State should be 'done' for uninvoiced orders after validating the session."
243 )
244
245 self._run_test({
246 'payment_methods': self.cash_pm1 | self.bank_pm1,
247 'orders': [
248 {'pos_order_lines_ui_args': [(self.product1, 6), (self.product2, 3), (self.product3, 1), ], 'payments': [(self.cash_pm1, 150)], 'uuid': '00100-010-0001'},
249 {'pos_order_lines_ui_args': [(self.product1, 1), (self.product2, 20), ], 'payments': [(self.bank_pm1, 410)], 'uuid': '00100-010-0002'},
250 {'pos_order_lines_ui_args': [(self.product1, 10), (self.product3, 1), ], 'payments': [(self.bank_pm1, 130)], 'is_invoiced': True, 'customer': self.customer, 'uuid': '00100-010-0003'},
251 ],
252 'before_closing_cb': _before_closing_cb,
253 'journal_entries_before_closing': {
254 '00100-010-0003': {
255 'invoice': {
256 'line_ids': [
257 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
258 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 30, 'reconciled': False},
259 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 130, 'credit': 0, 'reconciled': True},
260 ]
261 },
262 'payments': [
263 ((self.bank_pm1, 130), {
264 'line_ids': [
265 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 130, 'reconciled': True},
266 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 130, 'credit': 0, 'reconciled': False},
267 ]
268 }),
269 ],
270 }
271 },
272 'after_closing_cb': _after_closing_cb,
273 'journal_entries_after_closing': {
274 'session_journal_entry': {
275 'line_ids': [
276 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 560, 'reconciled': False},
277 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 540, 'credit': 0, 'reconciled': True},
278 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 150, 'credit': 0, 'reconciled': True},
279 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 130, 'reconciled': True},
280 ],
281 },
282 'cash_statement': [
283 ((150, ), {
284 'line_ids': [
285 {'account_id': self.cash_pm1.journal_id.default_account_id.id, 'partner_id': False, 'debit': 150, 'credit': 0, 'reconciled': False},
286 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 150, 'reconciled': True},
287 ]
288 }),
289 ],
290 'bank_payments': [
291 ((540, ), {
292 'line_ids': [
293 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 540, 'credit': 0, 'reconciled': False},
294 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 540, 'reconciled': True},
295 ]
296 }),
297 ],
298 },
299 })
300
301 def test_orders_with_zero_valued_invoiced(self):
302 """One invoiced order but with zero receivable line balance."""
303 self._run_test({
304 'payment_methods': self.cash_pm1 | self.bank_pm1,
305 'orders': [
306 {'pos_order_lines_ui_args': [(self.product0, 1)], 'payments': [(self.bank_pm1, 0)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '00100-010-0001'},
307 ],
308 'journal_entries_before_closing': {
309 '00100-010-0001': {
310 'invoice': {
311 'line_ids': [
312 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 0, 'reconciled': False},
313 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 0, 'reconciled': True},
314 ]
315 },
316 'payments': [
317 ((self.bank_pm1, 0), False),
318 ],
319 }
320 },
321 'journal_entries_after_closing': {
322 'session_journal_entry': False,
323 'cash_statement': [],
324 'bank_payments': [],
325 },
326 })
327
328 def test_return_order_invoiced(self):
329
330 def _before_closing_cb():
331 order = self.pos_session.order_ids.filtered(lambda order: '666-666-666' in order.uuid)
332
333 # refund
334 order.refund()
335 refund_order = self.pos_session.order_ids.filtered(lambda order: order.state == 'draft')
336
337 # pay the refund
338 context_make_payment = {"active_ids": [refund_order.id], "active_id": refund_order.id}
339 make_payment = self.env['pos.make.payment'].with_context(context_make_payment).create({
340 'payment_method_id': self.cash_pm1.id,
341 'amount': -100,
342 })
343 make_payment.check()
344
345 # invoice refund
346 refund_order.action_pos_order_invoice()
347
348 self._run_test({
349 'payment_methods': self.cash_pm1 | self.bank_pm1,
350 'orders': [
351 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments': [(self.cash_pm1, 100)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '666-666-666'},
352 ],
353 'before_closing_cb': _before_closing_cb,
354 'journal_entries_before_closing': {
355 '666-666-666': {
356 'invoice': {
357 'line_ids': [
358 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
359 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
360 ]
361 },
362 'payments': [
363 ((self.cash_pm1, 100), {
364 'line_ids': [
365 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
366 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
367 ]
368 }),
369 ],
370 }
371 },
372 'journal_entries_after_closing': {
373 'session_journal_entry': {
374 'line_ids': [
375 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 0, 'reconciled': True},
376 ],
377 },
378 'cash_statement': [],
379 'bank_payments': [],
380 },
381 })
382
383 def test_return_order(self):
384 """ Test return order
385
386 2 orders
387 - 2nd order is returned
388
389 Orders
390 ======
391 +------------------+----------+-----------+----------+-----+-------+
392 | order | payments | invoiced? | product | qty | total |
393 +------------------+----------+-----------+----------+-----+-------+
394 | order 1 | bank | no | product1 | 1 | 10 |
395 | | | | product2 | 5 | 100 |
396 +------------------+----------+-----------+----------+-----+-------+
397 | order 2 | cash | no | product1 | 3 | 30 |
398 | | | | product2 | 2 | 40 |
399 | | | | product3 | 1 | 30 |
400 +------------------+----------+-----------+----------+-----+-------+
401 | order 3 (return) | cash | no | product1 | -3 | -30 |
402 | | | | product2 | -2 | -40 |
403 | | | | product3 | -1 | -30 |
404 +------------------+----------+-----------+----------+-----+-------+
405
406 Expected Result
407 ===============
408 +---------------------+---------+
409 | account | balance |
410 +---------------------+---------+
411 | sale (sales) | -210 |
412 | sale (refund) | 100 |
413 | pos receivable bank | 110 |
414 +---------------------+---------+
415 | Total balance | 0.0 |
416 +---------------------+---------+
417 """
418 start_qty_available = {
419 self.product1: self.product1.qty_available,
420 self.product2: self.product2.qty_available,
421 self.product3: self.product3.qty_available,
422 }
423
424 def _before_closing_cb():
425 # check values before closing the session
426 self.assertEqual(2, self.pos_session.order_count)
427 orders_total = sum(order.amount_total for order in self.pos_session.order_ids)
428 self.assertAlmostEqual(orders_total, self.pos_session.total_payments_amount, msg='Total order amount should be equal to the total payment amount.')
429
430 # return order
431 order_to_return = self.pos_session.order_ids.filtered(lambda order: '12345-123-1234' in order.uuid)
432 order_to_return.refund()
433 refund_order = self.pos_session.order_ids.filtered(lambda order: order.state == 'draft')
434
435 # check if amount to pay
436 self.assertAlmostEqual(refund_order.amount_total - refund_order.amount_paid, -100)
437
438 # pay the refund
439 context_make_payment = {"active_ids": [refund_order.id], "active_id": refund_order.id}
440 make_payment = self.env['pos.make.payment'].with_context(context_make_payment).create({
441 'payment_method_id': self.cash_pm1.id,
442 'amount': -100,
443 })
444 make_payment.check()
445 self.assertEqual(refund_order.state, 'paid', 'Payment is registered, order should be paid.')
446 self.assertAlmostEqual(refund_order.amount_paid, -100.0, msg='Amount paid for return order should be negative.')
447
448 # check product qty_available after syncing the order
449 self.assertEqual(
450 self.product1.qty_available + 1,
451 start_qty_available[self.product1],
452 )
453 self.assertEqual(
454 self.product2.qty_available + 5,
455 start_qty_available[self.product2],
456 )
457 self.assertEqual(
458 self.product3.qty_available,
459 start_qty_available[self.product3],
460 )
461
462 # picking and stock moves should be in done state
463 # no exception of return orders
464 for order in self.pos_session.order_ids:
465 self.assertEqual(
466 order.picking_ids[0].state,
467 'done',
468 'Picking should be in done state.'
469 )
470 move_ids = order.picking_ids[0].move_ids
471 self.assertEqual(
472 move_ids.mapped('state'),
473 ['done'] * len(move_ids),
474 'Move Lines should be in done state.'
475 )
476
477 self._run_test({
478 'payment_methods': self.cash_pm1 | self.bank_pm1,
479 'orders': [
480 {'pos_order_lines_ui_args': [(self.product1, 1), (self.product2, 5)], 'payments': [(self.bank_pm1, 110)], 'uuid': '00100-010-0001'},
481 {'pos_order_lines_ui_args': [(self.product1, 3), (self.product2, 2), (self.product3, 1)], 'payments': [(self.cash_pm1, 100)], 'uuid': '12345-123-1234'},
482 ],
483 'before_closing_cb': _before_closing_cb,
484 'journal_entries_before_closing': {},
485 'journal_entries_after_closing': {
486 'session_journal_entry': {
487 'line_ids': [
488 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 210, 'reconciled': False},
489 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
490 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 110, 'credit': 0, 'reconciled': True},
491 ],
492 },
493 'cash_statement': [],
494 'bank_payments': [
495 ((110, ), {
496 'line_ids': [
497 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 110, 'credit': 0, 'reconciled': False},
498 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 110, 'reconciled': True},
499 ]
500 }),
501 ],
502 },
503 })
504
505 def test_split_cash_payments(self):
506 self._run_test({
507 'payment_methods': self.cash_split_pm1 | self.bank_pm1,
508 'orders': [
509 {'pos_order_lines_ui_args': [(self.product1, 10), (self.product2, 5)], 'payments': [(self.cash_split_pm1, 100), (self.bank_pm1, 100)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0001'},
510 {'pos_order_lines_ui_args': [(self.product2, 7), (self.product3, 1)], 'payments': [(self.cash_split_pm1, 70), (self.bank_pm1, 100)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0002'},
511 {'pos_order_lines_ui_args': [(self.product1, 1), (self.product3, 5), (self.product2, 3)], 'payments': [(self.cash_split_pm1, 120), (self.bank_pm1, 100)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0003'},
512 ],
513 'journal_entries_before_closing': {},
514 'journal_entries_after_closing': {
515 'session_journal_entry': {
516 'line_ids': [
517 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 590, 'reconciled': False},
518 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 300, 'credit': 0, 'reconciled': True},
519 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
520 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 70, 'credit': 0, 'reconciled': True},
521 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 120, 'credit': 0, 'reconciled': True},
522 ],
523 },
524 'cash_statement': [
525 ((100, ), {
526 'line_ids': [
527 {'account_id': self.cash_split_pm1.journal_id.default_account_id.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': False},
528 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
529 ]
530 }),
531 ((70, ), {
532 'line_ids': [
533 {'account_id': self.cash_split_pm1.journal_id.default_account_id.id, 'partner_id': self.customer.id, 'debit': 70, 'credit': 0, 'reconciled': False},
534 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 70, 'reconciled': True},
535 ]
536 }),
537 ((120, ), {
538 'line_ids': [
539 {'account_id': self.cash_split_pm1.journal_id.default_account_id.id, 'partner_id': self.customer.id, 'debit': 120, 'credit': 0, 'reconciled': False},
540 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 120, 'reconciled': True},
541 ]
542 }),
543 ],
544 'bank_payments': [
545 ((300, ), {
546 'line_ids': [
547 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 300, 'credit': 0, 'reconciled': False},
548 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 300, 'reconciled': True},
549 ]
550 })
551 ],
552 },
553 })
554
555 def test_rounding_method(self):
556 # set the cash rounding method
557 self.config.cash_rounding = True
558 self.config.rounding_method = self.env['account.cash.rounding'].create({
559 'name': 'add_invoice_line',
560 'rounding': 0.05,
561 'strategy': 'add_invoice_line',
562 'profit_account_id': self.company['default_cash_difference_income_account_id'].copy().id,
563 'loss_account_id': self.company['default_cash_difference_expense_account_id'].copy().id,
564 'rounding_method': 'HALF-UP',
565 })
566
567 self.open_new_session()
568
569 """ Test for orders: one with invoice
570
571 3 orders
572 - order 1, paid by cash
573 - order 2, paid by bank
574 - order 3, paid by bank, invoiced
575
576 Orders
577 ======
578 +---------+----------+---------------+----------+-----+-------+
579 | order | payments | invoiced? | product | qty | total |
580 +---------+----------+---------------+----------+-----+-------+
581 | order 1 | bank | no | product1 | 6 | 60 |
582 | | | | product4 | 4 | 39.84 |
583 +---------+----------+---------------+----------+-----+-------+
584 | order 2 | bank | yes | product4 | 3 | 29.88 |
585 | | | | product2 | 20 | 400 |
586 +---------+----------+---------------+----------+-----+-------+
587
588 Expected Result
589 ===============
590 +---------------------+---------+
591 | account | balance |
592 +---------------------+---------+
593 | sale | -596,56 |
594 | pos receivable bank | 516,64 |
595 | Rounding applied | -0,01 |
596 +---------------------+---------+
597 | Total balance | 0.0 |
598 +---------------------+---------+
599 """
600
601 # create orders
602 orders = []
603
604 # create orders
605 orders = []
606 orders.append(self.create_ui_order_data(
607 [(self.product4, 3), (self.product2, 20)],
608 payments=[(self.bank_pm1, 429.90)]
609 ))
610
611 orders.append(self.create_ui_order_data(
612 [(self.product1, 6), (self.product4, 4)],
613 payments=[(self.bank_pm1, 99.85)]
614 ))
615
616 # sync orders
617 self.env['pos.order'].sync_from_ui(orders)
618
619 self.assertEqual(orders[0]['amount_return'], 0, msg='The amount return should be 0')
620 self.assertEqual(orders[1]['amount_return'], 0, msg='The amount return should be 0')
621
622 # close the session
623 self.pos_session.action_pos_session_validate()
624
625 # check values after the session is closed
626 session_account_move = self.pos_session.move_id
627
628 rounding_line = session_account_move.line_ids.filtered(lambda line: line.name == 'Rounding line')
629 self.assertAlmostEqual(rounding_line.credit, 0.03, msg='The credit should be equals to 0.03')
630
631 def test_correct_partner_on_invoice_receivables(self):
632 self._run_test({
633 'payment_methods': self.cash_pm1 | self.cash_split_pm1 | self.bank_pm1 | self.bank_split_pm1,
634 'orders': [
635 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.cash_pm1, 100)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '00100-010-0001'},
636 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.bank_pm1, 100)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '00100-010-0002'},
637 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.cash_split_pm1, 100)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '00100-010-0003'},
638 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.bank_split_pm1, 100)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '00100-010-0004'},
639 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.cash_pm1, 100)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0005'},
640 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.bank_pm1, 100)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0006'},
641 {'pos_order_lines_ui_args': [(self.product99, 1)], 'payments':[(self.cash_split_pm1, 99)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0007'},
642 {'pos_order_lines_ui_args': [(self.product99, 1)], 'payments':[(self.bank_split_pm1, 99)], 'customer': self.customer, 'is_invoiced': False, 'uuid': '00100-010-0008'},
643 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.bank_pm1, 100)], 'customer': self.other_customer, 'is_invoiced': True, 'uuid': '00100-010-0009'},
644 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.bank_pm1, 100)], 'customer': self.other_customer, 'is_invoiced': True, 'uuid': '00100-010-0010'},
645 {'pos_order_lines_ui_args': [(self.product1, 10)], 'payments':[(self.bank_pm1, 100)], 'customer': self.customer, 'is_invoiced': True, 'uuid': '00100-010-0011'},
646 ],
647 'journal_entries_before_closing': {
648 '00100-010-0001': {
649 'invoice': {
650 'line_ids': [
651 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
652 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
653 ]
654 },
655 'payments': [
656 ((self.cash_pm1, 100), {
657 'line_ids': [
658 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
659 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
660 ]
661 }),
662 ],
663 },
664 '00100-010-0002': {
665 'invoice': {
666 'line_ids': [
667 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
668 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
669 ]
670 },
671 'payments': [
672 ((self.bank_pm1, 100), {
673 'line_ids': [
674 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
675 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
676 ]
677 }),
678 ],
679 },
680 '00100-010-0003': {
681 'invoice': {
682 'line_ids': [
683 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
684 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
685 ]
686 },
687 'payments': [
688 ((self.cash_split_pm1, 100), {
689 'line_ids': [
690 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
691 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
692 ]
693 }),
694 ],
695 },
696 '00100-010-0004': {
697 'invoice': {
698 'line_ids': [
699 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
700 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
701 ]
702 },
703 'payments': [
704 ((self.bank_split_pm1, 100), {
705 'line_ids': [
706 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
707 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
708 ]
709 }),
710 ],
711 },
712 '00100-010-0009': {
713 'invoice': {
714 'line_ids': [
715 {'account_id': self.sales_account.id, 'partner_id': self.other_customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
716 {'account_id': self.other_receivable_account.id, 'partner_id': self.other_customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
717 ]
718 },
719 'payments': [
720 ((self.bank_pm1, 100), {
721 'line_ids': [
722 {'account_id': self.other_receivable_account.id, 'partner_id': self.other_customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
723 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
724 ]
725 }),
726 ],
727 },
728 '00100-010-0010': {
729 'invoice': {
730 'line_ids': [
731 {'account_id': self.sales_account.id, 'partner_id': self.other_customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
732 {'account_id': self.other_receivable_account.id, 'partner_id': self.other_customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
733 ]
734 },
735 'payments': [
736 ((self.bank_pm1, 100), {
737 'line_ids': [
738 {'account_id': self.other_receivable_account.id, 'partner_id': self.other_customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
739 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
740 ]
741 }),
742 ],
743 },
744 '00100-010-0011': {
745 'invoice': {
746 'line_ids': [
747 {'account_id': self.sales_account.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': False},
748 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
749 ]
750 },
751 'payments': [
752 ((self.bank_pm1, 100), {
753 'line_ids': [
754 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
755 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 100, 'credit': 0, 'reconciled': False},
756 ]
757 }),
758 ],
759 },
760 },
761 'journal_entries_after_closing': {
762 'session_journal_entry': {
763 'line_ids': [
764 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 398, 'reconciled': False},
765 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 500, 'credit': 0, 'reconciled': True},
766 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
767 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 99, 'credit': 0, 'reconciled': True},
768 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': True},
769 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 99, 'credit': 0, 'reconciled': True},
770 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 200, 'credit': 0, 'reconciled': True},
771 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 100, 'reconciled': True},
772 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 400, 'reconciled': True},
773 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 100, 'reconciled': True},
774 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 100, 'reconciled': True},
775 ],
776 },
777 'cash_statement': [
778 ((100, ), {
779 'line_ids': [
780 {'account_id': self.cash_split_pm1.journal_id.default_account_id.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': False},
781 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
782 ]
783 }),
784 ((99, ), {
785 'line_ids': [
786 {'account_id': self.cash_split_pm1.journal_id.default_account_id.id, 'partner_id': self.customer.id, 'debit': 99, 'credit': 0, 'reconciled': False},
787 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 99, 'reconciled': True},
788 ]
789 }),
790 ((200, ), {
791 'line_ids': [
792 {'account_id': self.cash_pm1.journal_id.default_account_id.id, 'partner_id': False, 'debit': 200, 'credit': 0, 'reconciled': False},
793 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 200, 'reconciled': True},
794 ]
795 }),
796 ],
797 'bank_payments': [
798 ((100, ), {
799 'line_ids': [
800 {'account_id': self.bank_split_pm1.outstanding_account_id.id, 'partner_id': self.customer.id, 'debit': 100, 'credit': 0, 'reconciled': False},
801 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 100, 'reconciled': True},
802 ]
803 }),
804 ((99, ), {
805 'line_ids': [
806 {'account_id': self.bank_split_pm1.outstanding_account_id.id, 'partner_id': self.customer.id, 'debit': 99, 'credit': 0, 'reconciled': False},
807 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 99, 'reconciled': True},
808 ]
809 }),
810 ((500, ), {
811 'line_ids': [
812 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 500, 'credit': 0, 'reconciled': False},
813 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 500, 'reconciled': True},
814 ]
815 }),
816 ],
817 },
818 })
819
820 def test_cash_register_if_no_order(self):
821 # Process one order with product3
822 self.open_new_session(0)
823 session = self.pos_session
824 order_data = self.create_ui_order_data([(self.product3, 1)])
825 amount_paid = order_data['amount_paid']
826 with (
827 self.assertLogs('odoo.addons.point_of_sale.models.pos_order', level='DEBUG') as cm,
828 unittest.mock.patch('odoo.addons.point_of_sale.models.pos_order.randrange', return_value=1996)
829 ):
830 res = self.env['pos.order'].sync_from_ui([order_data])
831 # Basic check for logs on order synchronization
832 order_log_str = self.env['pos.order']._get_order_log_representation(order_data)
833 odoo_order_id = res['pos.order'][0]['id']
834 self.assertEqual(len(cm.output), 4)
835 self.assertEqual(cm.output[0], f"INFO:odoo.addons.point_of_sale.models.pos_order:PoS synchronisation #1996 started for PoS orders references: [{order_log_str}]")
836 self.assertTrue(cm.output[1].startswith(f'DEBUG:odoo.addons.point_of_sale.models.pos_order:PoS synchronisation #1996 processing order {order_log_str} order full data: '))
837 self.assertEqual(cm.output[2], f'INFO:odoo.addons.point_of_sale.models.pos_order:PoS synchronisation #1996 order {order_log_str} created pos.order #{odoo_order_id}')
838 self.assertEqual(cm.output[3], 'INFO:odoo.addons.point_of_sale.models.pos_order:PoS synchronisation #1996 finished')
839
840 session.post_closing_cash_details(amount_paid)
841 session.close_session_from_ui()
842
843 self.assertEqual(session.cash_register_balance_start, 0)
844 self.assertEqual(session.cash_register_balance_end_real, amount_paid)
845
846 # Open/Close session without any order in cash control
847 self.open_new_session(amount_paid)
848 session = self.pos_session
849 session.post_closing_cash_details(amount_paid)
850 session.close_session_from_ui()
851 self.assertEqual(session.cash_register_balance_start, amount_paid)
852 self.assertEqual(session.cash_register_balance_end_real, amount_paid)
853 self.assertEqual(self.config.last_session_closing_cash, amount_paid)
854
855 def test_start_balance_with_two_pos(self):
856 """ When having several POS with cash control, this tests ensures that each POS has its correct opening amount """
857
858 def open_and_check(pos_data):
859 self.config = pos_data['config']
860 self.open_new_session(pos_data['amount_paid'])
861 session = self.pos_session
862 self.assertEqual(session.cash_register_balance_start, pos_data['amount_paid'])
863
864 pos01_config = self.config
865 self.cash_journal = self.env['account.journal'].create(
866 {'name': 'CASH journal', 'type': 'cash', 'code': 'CSH00'})
867 self.cash_payment_method = self.env['pos.payment.method'].create({
868 'name': 'Cash Test',
869 'journal_id': self.cash_journal.id,
870 'receivable_account_id': pos01_config.payment_method_ids.filtered(lambda s: s.is_cash_count)[
871 1].receivable_account_id.id
872 })
873 pos02_config = pos01_config.copy({
874 'payment_method_ids': self.cash_payment_method
875 })
876 pos01_data = {'config': pos01_config, 'p_qty': 1, 'amount_paid': 0}
877 pos02_data = {'config': pos02_config, 'p_qty': 3, 'amount_paid': 0}
878
879 for pos_data in [pos01_data, pos02_data]:
880 open_and_check(pos_data)
881 session = self.pos_session
882
883 order_data = self.create_ui_order_data([(self.product3, pos_data['p_qty'])])
884 pos_data['amount_paid'] += order_data['amount_paid']
885 self.env['pos.order'].sync_from_ui([order_data])
886
887 session.post_closing_cash_details(pos_data['amount_paid'])
888 session.close_session_from_ui()
889
890 open_and_check(pos01_data)
891 open_and_check(pos02_data)
892
893 def test_pos_session_name_sequencing(self):
894 """ This test check if the session name is correctly set according to the sequence """
895
896 sequence = self.env['ir.sequence'].search([('code', '=', 'pos.session')])
897 sequence.prefix = '/'
898 sequence.write({'number_next_actual': 1000})
899 name = self.config.name
900
901 self.open_new_session(0)
902 self.assertEqual(self.pos_session.name, name + '/01000')
903
904 self.pos_session.close_session_from_ui()
905
906 sequence.prefix = 'TEST/'
907
908 self.open_new_session(0)
909 self.assertEqual(self.pos_session.name, 'TEST/01001')
910
911 def test_load_data_should_not_fail(self):
912 """load_data shouldn't fail
913
914 (Include test conditions here if possible)
915
916 - When there are partners that belong to different company
917 """
918
919 # create a partner that belongs to different company
920 company2 = self.company_data_2['company']
921 self.env['res.partner'].create({
922 'name': 'Test',
923 'company_id': company2.id,
924 })
925
926 self.open_new_session()
927
928 # calling load_data should not raise an error
929 self.pos_session.load_data([])
930
931 def test_invoice_past_refund(self):
932 """ Test invoicing a past refund
933
934 Orders
935 ======
936 +------------------+----------+-----------+----------+-----+-------+
937 | order | payments | invoiced? | product | qty | total |
938 +------------------+----------+-----------+----------+-----+-------+
939 | order 1 | cash | no | product3 | 1 | 30 |
940 +------------------+----------+-----------+----------+-----+-------+
941 | order 2 (return) | cash | no | product3 | -1 | -30 |
942 +------------------+----------+-----------+----------+-----+-------+
943
944 Expected Result
945 ===============
946 +---------------------+---------+
947 | account | balance |
948 +---------------------+---------+
949 | sale (sales) | -30 |
950 | sale (refund) | 30 |
951 +---------------------+---------+
952 | Total balance | 0.0 |
953 +---------------------+---------+
954 """
955 def _before_closing_cb():
956 # Return the order
957 order_to_return = self.pos_session.order_ids.filtered(lambda order: '12345-123-1234' in order.uuid)
958 order_to_return.refund()
959 refund_order = self.pos_session.order_ids.filtered(lambda order: order.state == 'draft')
960
961 # Check if there's an amount to pay
962 self.assertAlmostEqual(refund_order.amount_total - refund_order.amount_paid, -30)
963
964 # Pay the refund
965 context_make_payment = {"active_ids": [refund_order.id], "active_id": refund_order.id}
966 make_payment = self.env['pos.make.payment'].with_context(context_make_payment).create({
967 'payment_method_id': self.cash_pm1.id,
968 'amount': -30,
969 })
970 make_payment.check()
971
972 self._run_test({
973 'payment_methods': self.cash_pm1 | self.bank_pm1,
974 'orders': [
975 {'pos_order_lines_ui_args': [(self.product3, 1)], 'payments': [(self.cash_pm1, 30)], 'uuid': '12345-123-1234'},
976 ],
977 'before_closing_cb': _before_closing_cb,
978 'journal_entries_before_closing': {},
979 'journal_entries_after_closing': {
980 'session_journal_entry': {
981 'line_ids': [
982 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 30, 'reconciled': False},
983 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 30, 'credit': 0, 'reconciled': False},
984 ],
985 },
986 'cash_statement': [],
987 'bank_payments': [],
988 },
989 })
990
991 closed_session = self.pos_session
992 self.assertTrue(closed_session.state == 'closed', 'Session should be closed.')
993
994 return_to_invoice = closed_session.order_ids[1]
995 test_customer = self.env['res.partner'].create({'name': 'Test Customer'})
996 new_session_date = return_to_invoice.date_order + relativedelta(days=2)
997
998 with freeze_time(new_session_date):
999 # Create a new session after 2 days
1000 self.open_new_session(0)
1001 # Invoice the uninvoiced refund
1002 return_to_invoice.write({'partner_id': test_customer.id})
1003 return_to_invoice.action_pos_order_invoice()
1004 # Check the credit note
1005 self.assertTrue(return_to_invoice.account_move, 'Invoice should be created.')
1006 self.assertEqual(return_to_invoice.account_move.move_type, 'out_refund', 'Invoice should be a credit note.')
1007 self.assertEqual(return_to_invoice.account_move.invoice_date, new_session_date.date(), 'Invoice date should be the same as the session it is created in.')
1008 self.assertRecordValues(return_to_invoice.account_move, [{
1009 'amount_untaxed': 30,
1010 'amount_tax': 0,
1011 'amount_total': 30,
1012 }])
1013 self.assertRecordValues(return_to_invoice.account_move.line_ids, [
1014 {'account_id': self.sales_account.id, 'balance': 30},
1015 {'account_id': self.receivable_account.id, 'balance': -30},
1016 ])
1017
1018 def test_invoice_past_order(self):
1019 # create 1 uninvoiced order then close the session
1020 self._run_test({
1021 'payment_methods': self.cash_pm1 | self.bank_pm1,
1022 'orders': [
1023 {'pos_order_lines_ui_args': [(self.product99, 1)], 'payments': [(self.bank_pm1, 99)], 'customer': False, 'is_invoiced': False, 'uuid': '00100-010-0001'},
1024 ],
1025 'journal_entries_before_closing': {},
1026 'journal_entries_after_closing': {
1027 'session_journal_entry': {
1028 'line_ids': [
1029 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 99, 'reconciled': False},
1030 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 99, 'credit': 0, 'reconciled': True},
1031 ],
1032 },
1033 'cash_statement': [],
1034 'bank_payments': [
1035 ((99, ), {
1036 'line_ids': [
1037 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 99, 'credit': 0, 'reconciled': False},
1038 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 99, 'reconciled': True},
1039 ]
1040 })
1041 ],
1042 },
1043 })
1044
1045 # keep reference of the closed session
1046 closed_session = self.pos_session
1047 self.assertTrue(closed_session.state == 'closed', 'Session should be closed.')
1048
1049 order_to_invoice = closed_session.order_ids[0]
1050 test_customer = self.env['res.partner'].create({'name': 'Test Customer'})
1051
1052 with freeze_time(fields.Datetime.now() + relativedelta(days=2)):
1053 # create new session after 2 days
1054 self.open_new_session(0)
1055 # invoice the uninvoiced order
1056 order_to_invoice.write({'partner_id': test_customer.id})
1057 order_to_invoice.action_pos_order_invoice()
1058 # check invoice
1059 invoice = order_to_invoice.account_move
1060 self.assertTrue(invoice, 'Invoice should be created.')
1061 self.assertNotEqual(invoice.invoice_date, order_to_invoice.date_order.date(), 'Invoice date should not be the same as order date since the session was closed.')
1062
1063 # check that the payment date is set to the order date which
1064 # is the real payment date and not to the invoice_date
1065 payment = invoice.line_ids.full_reconcile_id.reconciled_line_ids.move_id - invoice
1066 self.assertEqual(payment.date, order_to_invoice.date_order.date())
1067
1068 def test_invoice_past_order_affecting_taxes(self):
1069 """ Test whether two taxes affecting each other don't trigger a recomputation on invoice generation
1070 """
1071 # Create 1 uninvoiced order then close the session
1072 self._run_test({
1073 'payment_methods': self.cash_pm1 | self.bank_pm1,
1074 'orders': [
1075 {'pos_order_lines_ui_args': [(self.product_multi_tax, 1)], 'payments': [(self.bank_pm1, 117.72)], 'customer': False, 'is_invoiced': False, 'uuid': '00100-010-0001'},
1076 ],
1077 'journal_entries_before_closing': {},
1078 'journal_entries_after_closing': {
1079 'session_journal_entry': {
1080 'line_ids': [
1081 {'account_id': self.tax_received_account.id, 'partner_id': False, 'debit': 0, 'credit': 8, 'reconciled': False},
1082 {'account_id': self.tax_received_account.id, 'partner_id': False, 'debit': 0, 'credit': 9.72, 'reconciled': False},
1083 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 100, 'reconciled': False},
1084 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 117.72, 'credit': 0, 'reconciled': True},
1085 ],
1086 },
1087 'cash_statement': [],
1088 'bank_payments': [
1089 ((117.72, ), {
1090 'line_ids': [
1091 {'account_id': self.bank_pm1.outstanding_account_id.id, 'partner_id': False, 'debit': 117.72, 'credit': 0, 'reconciled': False},
1092 {'account_id': self.bank_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 117.72, 'reconciled': True},
1093 ]
1094 })
1095 ],
1096 },
1097 })
1098
1099 closed_session = self.pos_session
1100 self.assertTrue(closed_session.state == 'closed', 'Session should be closed.')
1101
1102 order_to_invoice = closed_session.order_ids[0]
1103 test_customer = self.env['res.partner'].create({'name': 'Test Customer'})
1104
1105 # Create a new session
1106 self.open_new_session(0)
1107 # Invoice the uninvoiced order
1108 order_to_invoice.write({'partner_id': test_customer.id})
1109 order_to_invoice.action_pos_order_invoice()
1110 # Check the invoice for the lines
1111 self.assertTrue(order_to_invoice.account_move, 'Invoice should be created.')
1112 self.assertRecordValues(order_to_invoice.account_move.line_ids, [
1113 {'account_id': self.sales_account.id, 'balance': -100, 'reconciled': False},
1114 {'account_id': self.tax_received_account.id, 'balance': -8, 'reconciled': False},
1115 {'account_id': self.tax_received_account.id, 'balance': -9.72, 'reconciled': False},
1116 {'account_id': self.receivable_account.id, 'balance': 117.72, 'reconciled': True},
1117 ])
1118
1119 def test_limited_products_loading(self):
1120 self.env['ir.config_parameter'].sudo().set_param('point_of_sale.limited_product_count', 3)
1121
1122 # Make the service products that are available in the pos inactive.
1123 # We don't need them to test the loading of 'consu' products.
1124 self.env['product.template'].search([('available_in_pos', '=', True), ('type', '=', 'service')]).write({'available_in_pos': False})
1125
1126 session = self.open_new_session(0)
1127 self.product1.write({'company_id': False})
1128 self.product2.write({'company_id': False})
1129 self.product3.write({'company_id': False})
1130
1131 def get_top_product_ids(count):
1132 data = session.load_data([])
1133 special_product = session.config_id._get_special_products().ids
1134 available_top_product = [product for product in data['product.template'] if product['product_variant_ids'][0] not in special_product]
1135 return [p['product_variant_ids'][0] for p in available_top_product[:count]]
1136
1137 self.patch(self.env.cr, 'now', lambda: datetime.now() + timedelta(days=1))
1138 self.env['pos.order'].sync_from_ui([self.create_ui_order_data([(self.product1, 1)])])
1139 self.assertEqual(get_top_product_ids(1), [self.product1.id])
1140
1141 self.patch(self.env.cr, 'now', lambda: datetime.now() + timedelta(days=2))
1142 self.env['pos.order'].sync_from_ui([self.create_ui_order_data([(self.product2, 1)])])
1143 self.assertEqual(get_top_product_ids(2), [self.product1.id, self.product2.id])
1144
1145 self.patch(self.env.cr, 'now', lambda: datetime.now() + timedelta(days=3))
1146 self.env['pos.order'].sync_from_ui([self.create_ui_order_data([(self.product3, 1)])])
1147 self.assertEqual(get_top_product_ids(3), [self.product1.id, self.product2.id, self.product3.id])
1148
1149 def test_closing_entry_by_product(self):
1150 # set the Group by Product at Closing Entry
1151 self.config.is_closing_entry_by_product = True
1152 self.open_new_session()
1153
1154 # 4 orders
1155
1156 # Orders
1157 # ======
1158 # +---------+----------+---------------+----------+-----+-------+
1159 # | order | payments | invoiced? | product | qty | total |
1160 # +---------+----------+---------------+----------+-----+-------+
1161 # | order 1 | bank | no | product1 | 2 | 60 |
1162 # | | | | product4 | 3 | 39.84 |
1163 # +---------+----------+---------------+----------+-----+-------+
1164 # | order 2 | bank | yes | product4 | 1 | 29.88 |
1165 # | | | | product2 | 5 | 400 |
1166 # +---------+----------+---------------+----------+-----+-------+
1167 # | order 3 | bank | yes | product1 | 3 | 29.88 |
1168 # | | | | product2 | 10 | 400 |
1169 # +---------+----------+---------------+----------+-----+-------+
1170 # | order 4 | bank | yes | product1 | 5 | 29.88 |
1171 # | | | | product0 | 10| 400 |
1172 # +---------+----------+---------------+----------+-----+-------+
1173
1174 # Expected Output
1175 # +---------------+-----------+
1176 # | invoice_line | Quantity |
1177 # +---------------+-----------+
1178 # | Product 0 | 10 |
1179 # +---------------+-----------+
1180 # | Product 1 | 10 |
1181 # +---------------+-----------+
1182 # | Product 2 | 15 |
1183 # +---------------+-----------+
1184 # | Product 4 | 4 |
1185 # +---------------+-----------+
1186
1187 # create orders
1188 orders = []
1189
1190 # create orders
1191 orders = []
1192 orders.append(self.create_ui_order_data(
1193 [(self.product1, 2), (self.product4, 3)],
1194 payments=[(self.bank_pm1, 49.88)]
1195 ))
1196 orders.append(self.create_ui_order_data(
1197 [(self.product4, 1), (self.product2, 5)],
1198 payments=[(self.bank_pm1, 109.96)]
1199 ))
1200 orders.append(self.create_ui_order_data(
1201 [(self.product1, 3), (self.product2, 10)],
1202 payments=[(self.bank_pm1, 230)]
1203 ))
1204 orders.append(self.create_ui_order_data(
1205 [(self.product1, 5), (self.product0, 10)],
1206 payments=[(self.bank_pm1, 50)]
1207 ))
1208
1209 # sync orders
1210 self.env['pos.order'].sync_from_ui(orders)
1211 # close the session
1212 self.pos_session.action_pos_session_validate()
1213
1214 # check values after the session is closed
1215 session_account_move = self.pos_session.move_id
1216
1217 # Define expected quantities for each product
1218 expected_product_quantity = {
1219 self.product0: 10,
1220 self.product1: 10,
1221 self.product2: 15,
1222 self.product4: 4,
1223 }
1224 # Iterate through invoice lines and assert the expected quantities
1225 for i in session_account_move.line_ids:
1226 if i.product_id and expected_product_quantity.get(i.product_id):
1227 self.assertEqual(i.quantity, expected_product_quantity.get(i.product_id), f"Unexpected quantity for {i.product_id.name}")
1228
1229 def test_pos_payment_method_copy(self):
1230 """
1231 Test POS payment method copy:
1232 - Create two payment methods in which one of the payment method's journal type be cash
1233 - Copy multiple payment methods
1234 - Check the duplicated cash payment method journal should be empty
1235 """
1236 pm_1 = self.cash_pm1
1237 pm_2 = self.bank_pm1
1238 pm_3, pm_4 = (pm_1 + pm_2).copy()
1239
1240 self.assertTrue(pm_3)
1241 self.assertFalse(pm_3.journal_id)
1242 self.assertTrue(pm_4)
1243 self.assertEqual(pm_4.journal_id.type, "bank")
1244
1245 def test_single_config_global_invoice(self):
1246 """For a single POS config, create multiple orders and consolidate them into a single invoice"""
1247 self.open_new_session()
1248 # create orders
1249 orders = []
1250 orders.append(self.create_ui_order_data(
1251 [(self.product1, 2), (self.product4, 3)],
1252 payments=[(self.bank_pm1, 49.88)]
1253 ))
1254 orders.append(self.create_ui_order_data(
1255 [(self.product4, 1), (self.product2, 5)],
1256 payments=[(self.bank_pm1, 109.96)]
1257 ))
1258
1259 # sync orders
1260 self.env['pos.order'].sync_from_ui(orders)
1261 # close the session
1262 self.pos_session.action_pos_session_validate()
1263
1264 pos_orders = self.env['pos.order'].search([])
1265 # set customer for the orders
1266 pos_orders.write({'partner_id': self.customer.id})
1267
1268 # create consolidated invoice
1269 self.env['pos.make.invoice'].create({"consolidated_billing": True}).with_context({"active_ids": pos_orders.ids}).action_create_invoices()
1270 # check if have single invoice
1271 self.assertEqual(len(pos_orders), 2)
1272 self.assertEqual(len(pos_orders.account_move), 1)
1273 self.assertEqual(pos_orders.account_move.partner_id, self.customer)
1274 self.assertEqual(pos_orders.account_move.amount_total, sum(pos_orders.mapped('amount_total')))
1275 self.assertEqual(pos_orders.account_move.payment_state, 'paid')
1276 self.assertEqual(pos_orders.account_move.state, 'posted')
1277
1278 def test_multi_config_global_invoice(self):
1279 self.open_new_session()
1280 orders = []
1281 orders.append(self.create_ui_order_data(
1282 [(self.product1, 3), (self.product2, 10)],
1283 payments=[(self.bank_pm1, 230)]
1284 ))
1285 orders.append(self.create_ui_order_data(
1286 [(self.product1, 5), (self.product0, 10)],
1287 payments=[(self.bank_pm1, 50)]
1288 ))
1289 self.env['pos.order'].sync_from_ui(orders)
1290 self.pos_session.action_pos_session_validate()
1291
1292 # open new session & create orders
1293 self.open_new_session()
1294 orders2 = []
1295 orders2.append(self.create_ui_order_data(
1296 [(self.product1, 2), (self.product4, 3)],
1297 payments=[(self.bank_pm1, 49.88)]
1298 ))
1299 orders2.append(self.create_ui_order_data(
1300 [(self.product4, 1), (self.product2, 5)],
1301 payments=[(self.bank_pm1, 109.96)]
1302 ))
1303 self.env['pos.order'].sync_from_ui(orders2)
1304 self.pos_session.action_pos_session_validate()
1305
1306 pos_orders = self.env['pos.order'].search([])
1307 # set customer for the orders
1308 pos_orders.write({'partner_id': self.customer.id})
1309
1310 # create consolidated invoice
1311 self.env['pos.make.invoice'].create({"consolidated_billing": True}).with_context({"active_ids": pos_orders.ids}).action_create_invoices()
1312 # check if have single invoice
1313 self.assertEqual(len(pos_orders), 4)
1314 self.assertEqual(len(pos_orders.account_move), 1)
1315 self.assertEqual(pos_orders.account_move.partner_id, self.customer)
1316 self.assertEqual(pos_orders.account_move.amount_total, round(sum(pos_orders.mapped('amount_total')), 2))
1317 self.assertEqual(pos_orders.account_move.payment_state, 'paid')
1318 self.assertEqual(pos_orders.account_move.state, 'posted')
1319
1320 def test_double_syncing_same_order(self):
1321 """ Test that double syncing the same order doesn't create duplicates records
1322 """
1323 self.open_new_session()
1324
1325 # Create an order
1326 order_data = self.create_ui_order_data([(self.product1, 1)], payments=[(self.cash_pm1, 10)], customer=self.customer, is_invoiced=True)
1327 order_data['access_token'] = '0123456789'
1328 res = self.env['pos.order'].sync_from_ui([order_data])
1329 order_id = res['pos.order'][0]['id']
1330
1331 # Sync the same order again
1332 res = self.env['pos.order'].sync_from_ui([order_data])
1333 self.assertEqual(res['pos.order'][0]['id'], order_id, 'Syncing the same order should not create a new one')
1334
1335 order = self.env['pos.order'].browse(order_id)
1336 self.assertEqual(order.picking_count, 1, 'Order should have one picking')
1337 self.assertEqual(len(order.payment_ids), 1, 'Order should have one payment')
1338 self.assertEqual(self.env['account.move'].search_count([('pos_order_ids', 'in', order.ids)]), 1, 'Order should have one invoice')
1339
1340 def test_pos_archived_combination(self):
1341 product = self.env['product.template'].create({
1342 'name': 'Product Test',
1343 'available_in_pos': True,
1344 'list_price': 10,
1345 'taxes_id': False,
1346 })
1347
1348 attribute_1, attribute_2, attribute_3 = self.env['product.attribute'].create([{
1349 'name': 'Attribute 1',
1350 'create_variant': 'always',
1351 'value_ids': [(0, 0, {
1352 'name': 'Value 1',
1353 }), (0, 0, {
1354 'name': 'Value 2',
1355 })],
1356 }, {
1357 'name': 'Attribute 2',
1358 'create_variant': 'always',
1359 'value_ids': [(0, 0, {
1360 'name': 'Value 1',
1361 }), (0, 0, {
1362 'name': 'Value 2',
1363 })],
1364 }, {
1365 'name': 'Attribute 3',
1366 'create_variant': 'always',
1367 'value_ids': [(0, 0, {
1368 'name': 'Value 1',
1369 }), (0, 0, {
1370 'name': 'Value 2',
1371 })],
1372 }])
1373
1374 _, _, ptal = self.env['product.template.attribute.line'].create([{
1375 'product_tmpl_id': product.id,
1376 'attribute_id': attribute_1.id,
1377 'value_ids': [(6, 0, attribute_1.value_ids.ids)],
1378 'sequence': 3,
1379 }, {
1380 'product_tmpl_id': product.id,
1381 'attribute_id': attribute_2.id,
1382 'value_ids': [(6, 0, attribute_2.value_ids.ids)],
1383 'sequence': 2,
1384 }, {
1385 'product_tmpl_id': product.id,
1386 'attribute_id': attribute_3.id,
1387 'value_ids': [(6, 0, attribute_3.value_ids.ids)],
1388 'sequence': 1,
1389 }])
1390
1391 product.write({
1392 'attribute_line_ids': [(2, ptal.id)],
1393 })
1394
1395 self.open_new_session()
1396 response = self.pos_session.load_data([])
1397 product_data = next((item for item in response['product.template'] if item['id'] == product.id), None)
1398
1399 self.assertEqual(len(product_data['_archived_combinations']), 0, "There should be no archived combinations for the product")
1400
1401 first_variant = product.product_variant_ids[0]
1402 first_variant.write({'active': False})
1403
1404 response = self.pos_session.load_data([])
1405 product_data = next((item for item in response['product.template'] if item['id'] == product.id), None)
1406
1407 self.assertEqual(len(product_data['_archived_combinations']), 1, "There should be one archived combination for the product")
1408 self.assertEqual(len(product_data['_archived_combinations'][0]), 2, "Archived combination should have two values")
1409 self.assertTrue(all(value in product_data['_archived_combinations'][0] for value in first_variant.product_template_attribute_value_ids.ids), "Archived combination should match the first variant's attribute values")
1410
1411 def test_refunded_order_id(self):
1412 """
1413 An order containing refunded lines from two different orders is no longer allowed,
1414 but some legacy records of this kind may still exist.
1415 This test ensures that the refunded_order_id is correctly computed in such cases.
1416 """
1417 current_session = self.open_new_session()
1418 orders = list(self._create_orders([
1419 {'pos_order_lines_ui_args': [(self.product1, 1)]},
1420 {'pos_order_lines_ui_args': [(self.product2, 1)]}
1421 ]).values())
1422
1423 refund_order = self.env['pos.order'].create({
1424 'company_id': self.env.company.id,
1425 'session_id': current_session.id,
1426 'lines': [
1427 (0, 0, {
1428 'product_id': self.product1.id,
1429 'price_unit': -10,
1430 'qty': 1,
1431 'tax_ids': [[6, False, []]],
1432 'price_subtotal': -10,
1433 'price_subtotal_incl': -10,
1434 'refunded_orderline_id': orders[0].lines[0].id
1435 }),
1436 (0, 0, {
1437 'product_id': self.product2.id,
1438 'price_unit': -10,
1439 'qty': 1,
1440 'tax_ids': [[6, False, []]],
1441 'price_subtotal': -10,
1442 'price_subtotal_incl': -10,
1443 'refunded_orderline_id': orders[1].lines[0].id
1444 })
1445 ],
1446 'amount_paid': -10,
1447 'amount_total': -10,
1448 'amount_tax': 0.0,
1449 'amount_return': 0.0,
1450 })
1451
1452 self.assertEqual(refund_order.refunded_order_id, orders[0])
1453
1454 def test_cannot_archive_journal_linked_to_pos_payment_method(self):
1455 """Test that archiving a journal linked to a POS payment method is blocked, and allowed when not linked."""
1456
1457 test_journal = self.env['account.journal'].create({
1458 'name': 'Test POS Journal',
1459 'type': 'cash',
1460 'code': 'TPJ',
1461 'company_id': self.env.company.id,
1462 })
1463 test_payment_method = self.env['pos.payment.method'].create({
1464 'name': 'Test PM',
1465 'journal_id': test_journal.id,
1466 'receivable_account_id': self.cash_pm1.receivable_account_id.id,
1467 })
1468
1469 with self.assertRaises(ValidationError):
1470 test_journal.action_archive()
1471
1472 # Unlink the payment method and try again (should succeed)
1473 test_payment_method.journal_id = False
1474 test_journal.action_archive()
1475 self.assertFalse(test_journal.active, "Journal should be archived when not linked to a POS payment method.")