Debian Codegraph @

addons/point_of_sale/tests/test_pos_stock_account.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from unittest import skip
5
6 from odoo import tools
7 import odoo
8 from odoo.addons.point_of_sale.tests.common import TestPoSCommon
9
10
11 @odoo.tests.tagged('post_install', '-at_install')
12 @skip('Temporary to fast merge new valuation')
13 class TestPoSStock(TestPoSCommon):
14 """ Tests for anglo saxon accounting scenario.
15 """
16 def setUp(self):
17 super(TestPoSStock, self).setUp()
18
19 self.config = self.basic_config
20 self.product1 = self.create_product('Product 1', self.categ_anglo, 10.0, 5.0)
21 self.product2 = self.create_product('Product 2', self.categ_anglo, 20.0, 10.0)
22 self.product3 = self.create_product('Product 3', self.categ_basic, 30.0, 15.0)
23 self.product4 = self.create_product('Product 4', self.categ_anglo, 10.0, 5.0)
24 self.product4.type = 'consu'
25 self.product4.is_storable = False
26 # start inventory with 10 items for each product
27 self.adjust_inventory([self.product1, self.product2, self.product3], [10, 10, 10])
28
29 # change cost(standard_price) of anglo products
30 # then set inventory from 10 -> 15
31 self.product1.write({'standard_price': 6.0})
32 self.product2.write({'standard_price': 6.0})
33 self.adjust_inventory([self.product1, self.product2, self.product3], [15, 15, 15])
34
35 # change cost(standard_price) of anglo products
36 # then set inventory from 15 -> 25
37 self.product1.write({'standard_price': 13.0})
38 self.product2.write({'standard_price': 13.0})
39 self.adjust_inventory([self.product1, self.product2, self.product3], [25, 25, 25])
40
41 self.output_account = self.categ_anglo.property_stock_account_output_categ_id
42 self.expense_account = self.categ_anglo.property_account_expense_categ_id
43 self.valuation_account = self.categ_anglo.property_stock_valuation_account_id
44
45 def test_01_orders_no_invoiced(self):
46 """
47
48 Orders
49 ======
50 +---------+----------+-----+-------------+------------+
51 | order | product | qty | total price | total cost |
52 +---------+----------+-----+-------------+------------+
53 | order 1 | product1 | 10 | 100.0 | 50.0 | -> 10 items at cost of 5.0 is consumed, remains 5 items at 6.0 and 10 items at 13.0
54 | | product2 | 10 | 200.0 | 100.0 | -> 10 items at cost of 10.0 is consumed, remains 5 items at 6.0 and 10 items at 13.0
55 +---------+----------+-----+-------------+------------+
56 | order 2 | product2 | 7 | 140.0 | 56.0 | -> 5 items at cost of 6.0 and 2 items at cost of 13.0, remains 8 items at cost of 13.0
57 | | product3 | 7 | 210.0 | 0.0 |
58 +---------+----------+-----+-------------+------------+
59 | order 3 | product1 | 6 | 60.0 | 43.0 | -> 5 items at cost of 6.0 and 1 item at cost of 13.0, remains 9 items at cost of 13.0
60 | | product2 | 6 | 120.0 | 78.0 | -> 6 items at cost of 13.0, remains 2 items at cost of 13.0
61 | | product3 | 6 | 180.0 | 0.0 |
62 +---------+----------+-----+-------------+------------+
63 | order 4 | product4 | 6 | 60.0 | 0.0 | -> consumable product cost = 0
64 +---------+----------+-----+-------------+------------+
65
66 Expected Result
67 ===============
68 +---------------------+---------+
69 | account | balance |
70 +---------------------+---------+
71 | sale_account | -1070.0 |
72 | pos_receivable-cash | 1070.0 |
73 | expense_account | 327.0 |
74 | output_account | -327.0 |
75 +---------------------+---------+
76 | Total balance | 0.00 |
77 +---------------------+---------+
78 """
79
80 def _before_closing_cb():
81 # check values before closing the session
82 self.assertEqual(4, self.pos_session.order_count)
83 orders_total = sum(order.amount_total for order in self.pos_session.order_ids)
84 self.assertAlmostEqual(orders_total, self.pos_session.total_payments_amount, msg='Total order amount should be equal to the total payment amount.')
85 self.assertAlmostEqual(orders_total, 1070.0, msg='The orders\'s total amount should equal the computed.')
86
87 # check product qty_available after syncing the order
88 self.assertEqual(self.product1.qty_available, 9)
89 self.assertEqual(self.product2.qty_available, 2)
90 self.assertEqual(self.product3.qty_available, 12)
91
92 # picking and stock moves should be in done state
93 for order in self.pos_session.order_ids:
94 self.assertEqual(order.picking_ids[0].state, 'done', 'Picking should be in done state.')
95 self.assertTrue(all(state == 'done' for state in order.picking_ids[0].move_ids.mapped('state')), 'Move Lines should be in done state.')
96
97 self._run_test({
98 'payment_methods': self.cash_pm1 | self.bank_pm1,
99 'orders': [
100 {'pos_order_lines_ui_args': [(self.product1, 10), (self.product2, 10)], 'uuid': '00100-010-0001'},
101 {'pos_order_lines_ui_args': [(self.product2, 7), (self.product3, 7)], 'uuid': '00100-010-0002'},
102 {'pos_order_lines_ui_args': [(self.product1, 6), (self.product2, 6), (self.product3, 6)], 'uuid': '00100-010-0003'},
103 {'pos_order_lines_ui_args': [(self.product4, 6)], 'uuid': '00100-010-0004'},
104 ],
105 'before_closing_cb': _before_closing_cb,
106 'journal_entries_before_closing': {},
107 'journal_entries_after_closing': {
108 'session_journal_entry': {
109 'line_ids': [
110 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 1070.0, 'reconciled': False},
111 {'account_id': self.expense_account.id, 'partner_id': False, 'debit': 327, 'credit': 0, 'reconciled': False},
112 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 1070.0, 'credit': 0, 'reconciled': True},
113 {'account_id': self.output_account.id, 'partner_id': False, 'debit': 0, 'credit': 327, 'reconciled': True},
114 ],
115 },
116 'cash_statement': [
117 ((1070.0, ), {
118 'line_ids': [
119 {'account_id': self.cash_pm1.journal_id.default_account_id.id, 'partner_id': False, 'debit': 1070.0, 'credit': 0, 'reconciled': False},
120 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 1070.0, 'reconciled': True},
121 ]
122 }),
123 ],
124 'bank_payments': [],
125 },
126 })
127
128 def test_02_orders_with_invoice(self):
129 """
130
131 Orders
132 ======
133 Same with test_01 but order 3 is invoiced.
134
135 Expected Result
136 ===============
137 +---------------------+---------+
138 | account | balance |
139 +---------------------+---------+
140 | sale_account | -650.0 |
141 | pos_receivable-cash | 1010.0 |
142 | receivable | -360.0 |
143 | expense_account | 206.0 |
144 | output_account | -206.0 |
145 +---------------------+---------+
146 | Total balance | 0.00 |
147 +---------------------+---------+
148 """
149
150 def _before_closing_cb():
151 # check values before closing the session
152 self.assertEqual(3, self.pos_session.order_count)
153 orders_total = sum(order.amount_total for order in self.pos_session.order_ids)
154 self.assertAlmostEqual(orders_total, self.pos_session.total_payments_amount, msg='Total order amount should be equal to the total payment amount.')
155 self.assertAlmostEqual(orders_total, 1010.0, msg='The orders\'s total amount should equal the computed.')
156
157 # check product qty_available after syncing the order
158 self.assertEqual(self.product1.qty_available, 9)
159 self.assertEqual(self.product2.qty_available, 2)
160 self.assertEqual(self.product3.qty_available, 12)
161
162 # picking and stock moves should be in done state
163 for order in self.pos_session.order_ids:
164 self.assertEqual(order.picking_ids[0].state, 'done', 'Picking should be in done state.')
165 self.assertTrue(all(state == 'done' for state in order.picking_ids[0].move_ids.mapped('state')), 'Move Lines should be in done state.')
166
167 self._run_test({
168 'payment_methods': self.cash_pm1 | self.bank_pm1,
169 'orders': [
170 {'pos_order_lines_ui_args': [(self.product1, 10), (self.product2, 10)], 'uuid': '00100-010-0001'},
171 {'pos_order_lines_ui_args': [(self.product2, 7), (self.product3, 7)], 'uuid': '00100-010-0002'},
172 {'pos_order_lines_ui_args': [(self.product1, 6), (self.product2, 6), (self.product3, 6)], 'is_invoiced': True, 'customer': self.customer, 'uuid': '00100-010-0003'},
173 ],
174 'before_closing_cb': _before_closing_cb,
175 'journal_entries_before_closing': {
176 '00100-010-0003': {
177 'payments': [
178 ((self.cash_pm1, 360.0), {
179 'line_ids': [
180 {'account_id': self.c1_receivable.id, 'partner_id': self.customer.id, 'debit': 0, 'credit': 360.0, 'reconciled': True},
181 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 360.0, 'credit': 0, 'reconciled': False},
182 ]
183 }),
184 ],
185 },
186 },
187 'journal_entries_after_closing': {
188 'session_journal_entry': {
189 'line_ids': [
190 {'account_id': self.sales_account.id, 'partner_id': False, 'debit': 0, 'credit': 650, 'reconciled': False},
191 {'account_id': self.expense_account.id, 'partner_id': False, 'debit': 206, 'credit': 0, 'reconciled': False},
192 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 1010.0, 'credit': 0, 'reconciled': True},
193 {'account_id': self.pos_receivable_account.id, 'partner_id': False, 'debit': 0, 'credit': 360, 'reconciled': True},
194 {'account_id': self.output_account.id, 'partner_id': False, 'debit': 0, 'credit': 206, 'reconciled': True},
195 ],
196 },
197 'cash_statement': [
198 ((1010.0, ), {
199 'line_ids': [
200 {'account_id': self.cash_pm1.journal_id.default_account_id.id, 'partner_id': False, 'debit': 1010.0, 'credit': 0, 'reconciled': False},
201 {'account_id': self.cash_pm1.receivable_account_id.id, 'partner_id': False, 'debit': 0, 'credit': 1010.0, 'reconciled': True},
202 ]
203 }),
204 ],
205 'bank_payments': [],
206 },
207 })
208
209
210 def test_03_order_product_w_owner(self):
211 """
212 Test order via POS a product having stock owner.
213 """
214
215 group_owner = self.env.ref('stock.group_tracking_owner')
216 self.env.user.write({'group_ids': [(4, group_owner.id)]})
217 self.product4 = self.create_product('Product 3', self.categ_basic, 30.0, 15.0)
218 self.env['stock.quant'].with_context(inventory_mode=True).create({
219 'product_id': self.product4.id,
220 'inventory_quantity': 10,
221 'location_id': self.stock_location_components.id,
222 'owner_id': self.partner_a.id,
223 }).action_apply_inventory()
224
225 self.open_new_session()
226
227 # create orders
228 orders = []
229 orders.append(self.create_ui_order_data([(self.product4, 1)]))
230
231 # sync orders
232 order = self.env['pos.order'].sync_from_ui(orders)
233
234 # check values before closing the session
235 self.assertEqual(1, self.pos_session.order_count)
236
237 # check product qty_available after syncing the order
238 self.assertEqual(self.product4.qty_available, 9)
239
240 # picking and stock moves should be in done state
241 for order in self.pos_session.order_ids:
242 self.assertEqual(order.picking_ids[0].state, 'done', 'Picking should be in done state.')
243 self.assertTrue(all(state == 'done' for state in order.picking_ids[0].move_ids.mapped('state')), 'Move Lines should be in done state.')
244 self.assertTrue(self.partner_a == order.picking_ids[0].move_ids[0].move_line_ids[0].owner_id, 'Move Lines Owner should be taken into account.')
245
246 # close the session
247 self.pos_session.action_pos_session_validate()
248
249 def test_04_order_refund(self):
250 self.categ4 = self.env['product.category'].create({
251 'name': 'Category 4',
252 'property_cost_method': 'fifo',
253 'property_valuation': 'real_time',
254 })
255 self.product4 = self.create_product('Product 4', self.categ4, 30.0, 15.0)
256
257 self.open_new_session()
258 orders = []
259 orders.append(self.create_ui_order_data([(self.product4, 1)]))
260 order = self.env['pos.order'].sync_from_ui(orders)
261
262 refund_action = self.env['pos.order'].browse(order['pos.order'][0]['id']).refund()
263 refund = self.env['pos.order'].browse(refund_action['res_id'])
264
265 payment_context = {"active_ids": refund.ids, "active_id": refund.id}
266 refund_payment = self.env['pos.make.payment'].with_context(**payment_context).create({
267 'amount': refund.amount_total,
268 'payment_method_id': self.cash_pm1.id,
269 })
270 refund_payment.with_context(**payment_context).check()
271
272 self.pos_session.action_pos_session_validate()
273 expense_account_move_line = self.env['account.move.line'].search([('account_id', '=', self.expense_account.id), ('product_id', '=', False)])
274 self.assertEqual(expense_account_move_line.balance, 0.0, "Expense account should be 0.0")
275
276 def test_stock_duplicate_warehouse_with_PoS_operation_type(self):
277 wh = self.env['stock.warehouse'].create({
278 'name': 'WH1',
279 'code': 'WH1',
280 'company_id': self.env.company.id,
281 })
282 wh_copy = wh.copy()
283 self.assertTrue(wh_copy.pos_type_id)
284 self.assertNotEqual(wh.pos_type_id, wh_copy.pos_type_id)