Debian Codegraph @

addons/sale_expense/tests/test_reinvoice.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import Command
4 from odoo.addons.hr_expense.tests.common import TestExpenseCommon
5 from odoo.addons.sale.tests.common import TestSaleCommon
6 from odoo.tests import tagged
7
8
9 @tagged('-at_install', 'post_install')
10 class TestReInvoice(TestExpenseCommon, TestSaleCommon):
11 """
12 Test that expenses, when linked to a sale order and invoiced are correctly re-invoiced on the sale order.
13 It should cover the following rules:
14 - Lines are never grouped together (even if re-invoiced at sale price and with a re-invoice delivered policy)
15 - When posting the move of an expense, it creates the corresponding SOLs with the correct expense quantity
16 - The amount of analytic account linked do not impact the quantities on the SOLs
17 - The quantities ordered and delivered are reset to 0 when:
18 - the expense move has been reset to draft
19 - the expense move is reversed
20 - the expense move has been reset to draft
21 - As it should be a one-to-one relation between model, we need to ensure that one expense only impacts one SOL
22 The test tries to cover all the possible combinations of expense and invoicing policies, as well as the different actions
23 """
24
25 @classmethod
26 def setUpClass(cls):
27 super().setUpClass()
28 new_sale_tax, new_purchase_tax = cls.env['account.tax'].create([{
29 'name': 'Tax 12.499%',
30 'amount': 12.499,
31 'amount_type': 'percent',
32 'type_tax_use': tax_type,
33 'repartition_line_ids': [
34 Command.create({'document_type': 'invoice', 'repartition_type': 'base'}),
35 Command.create({
36 'document_type': 'invoice',
37 'repartition_type': 'tax',
38 'account_id': cls.company_data[f'default_account_tax_{tax_type}'].id
39 }),
40 Command.create({'document_type': 'refund', 'repartition_type': 'base'}),
41 Command.create({
42 'document_type': 'refund',
43 'repartition_type': 'tax',
44 'account_id': cls.company_data[f'default_account_tax_{tax_type}'].id
45 }),
46 ],
47 } for tax_type in ('sale', 'purchase')])
48 cls.company_data.update({
49 'service_order_sales_price': cls.env['product.product'].with_company(cls.company_data['company']).create({
50 'name': 'service_order_sales_price',
51 'categ_id': cls.product_category.id,
52 'standard_price': 0.,
53 'list_price': 280.39,
54 'type': 'service',
55 'weight': 0.01,
56 'uom_id': cls.env.ref('uom.product_uom_unit').id,
57 'default_code': 'FURN_99991',
58 'invoice_policy': 'order',
59 'expense_policy': 'sales_price',
60 'taxes_id': [Command.set([new_sale_tax.id])],
61 'supplier_taxes_id': [Command.set([new_purchase_tax.id])],
62 'can_be_expensed': True,
63 }),
64 'service_delivery_sales_price': cls.env['product.product'].with_company(cls.company_data['company']).create({
65 'name': 'service_order_sales_price',
66 'categ_id': cls.product_category.id,
67 'standard_price': 0.,
68 'list_price': 280.39,
69 'type': 'service',
70 'weight': 0.01,
71 'uom_id': cls.env.ref('uom.product_uom_unit').id,
72 'default_code': 'FURN_99992',
73 'invoice_policy': 'delivery',
74 'expense_policy': 'sales_price',
75 'taxes_id': [Command.set([new_sale_tax.id])],
76 'supplier_taxes_id': [Command.set([new_purchase_tax.id])],
77 'can_be_expensed': True,
78 }),
79 'service_delivery_cost_price': cls.env['product.product'].with_company(cls.company_data['company']).create({
80 'name': 'service_delivery_cost_price',
81 'categ_id': cls.product_category.id,
82 'standard_price': 235.28,
83 'list_price': 280.39,
84 'type': 'service',
85 'weight': 0.01,
86 'uom_id': cls.env.ref('uom.product_uom_unit').id,
87 'default_code': 'FURN_99993',
88 'invoice_policy': 'delivery',
89 'expense_policy': 'cost',
90 'taxes_id': [Command.set([new_sale_tax.id])],
91 'supplier_taxes_id': [Command.set([new_purchase_tax.id])],
92 'can_be_expensed': True,
93 }),
94 'service_order_cost_price': cls.env['product.product'].with_company(cls.company_data['company']).create({
95 'name': 'service_order_cost_price',
96 'categ_id': cls.product_category.id,
97 'standard_price': 235.28,
98 'list_price': 280.39,
99 'type': 'service',
100 'weight': 0.01,
101 'uom_id': cls.env.ref('uom.product_uom_unit').id,
102 'default_code': 'FURN_99994',
103 'invoice_policy': 'order',
104 'expense_policy': 'cost',
105 'taxes_id': [Command.set([new_sale_tax.id])],
106 'supplier_taxes_id': [Command.set([new_purchase_tax.id])],
107 'can_be_expensed': True,
108 }),
109 })
110 # create SO line and confirm SO (with only one line)
111 cls.expense_sale_order = cls.env['sale.order'].with_context(mail_notrack=True, mail_create_nolog=True).create({
112 'partner_id': cls.partner_a.id,
113 'partner_invoice_id': cls.partner_a.id,
114 'partner_shipping_id': cls.partner_a.id,
115 'order_line': [Command.create({
116 'name': 'expense_employee: expense_1 invoicing=order, expense=sales_price',
117 # Using the same name as one of the expense
118 'product_id': cls.company_data['product_order_sales_price'].id,
119 'product_uom_qty': 3.0,
120 'price_unit': cls.company_data['product_order_sales_price'].standard_price,
121 })],
122 })
123 cls.expense_sale_order.action_confirm()
124
125 # Create 6 expenses, covering all the expense & invoicing policies combinaisons
126 cls.sale_exp_order_sale_1 = cls.create_expenses({
127 'name': 'expense_1 invoicing=order, expense=sales_price',
128 'date': '2016-01-01',
129 'product_id': cls.company_data['service_order_sales_price'].id,
130 'total_amount': 100.34,
131 'analytic_distribution': {cls.analytic_account_1.id: 100},
132 'employee_id': cls.expense_employee.id,
133 'sale_order_id': cls.expense_sale_order.id,
134 })
135 cls.sale_exp_order_sale_2 = cls.create_expenses({
136 'name': 'expense_2 invoicing=order, expense=sales_price',
137 'date': '2016-01-02',
138 'product_id': cls.company_data['service_order_sales_price'].id,
139 'total_amount': 100.21,
140 'sale_order_id': cls.expense_sale_order.id,
141 })
142 cls.sale_exp_deliv_sale_3 = cls.create_expenses({
143 'name': 'expense_3 invoicing=delivery, expense=sales_price',
144 'date': '2016-01-03',
145 'product_id': cls.company_data['service_delivery_sales_price'].id,
146 'total_amount': 10012.49,
147 'analytic_distribution': {cls.analytic_account_1.id: 100},
148 'sale_order_id': cls.expense_sale_order.id,
149 })
150 cls.sale_exp_deliv_sale_4 = cls.create_expenses({
151 'name': 'expense_4 invoicing=delivery, expense=sales_price',
152 'date': '2016-01-03',
153 'product_id': cls.company_data['service_delivery_sales_price'].id,
154 'analytic_distribution': {cls.analytic_account_1.id: 100},
155 'total_amount': 10012.49,
156 'sale_order_id': cls.expense_sale_order.id,
157 })
158 cls.sale_exp_deliv_cost_5 = cls.create_expenses({
159 'name': 'expense_5 invoicing=delivery, expense=cost',
160 'date': '2016-01-03',
161 'product_id': cls.company_data['service_delivery_cost_price'].id,
162 'quantity': 5,
163 'sale_order_id': cls.expense_sale_order.id,
164 })
165 cls.sale_exp_order_cost_6 = cls.create_expenses({
166 'name': 'expense_6 invoicing=order, expense=cost',
167 'date': '2016-01-03',
168 'product_id': cls.company_data['service_order_cost_price'].id,
169 'quantity': 6,
170 'sale_order_id': cls.expense_sale_order.id,
171 },
172 )
173 cls.sale_expense_all = (
174 cls.sale_exp_order_sale_1
175 | cls.sale_exp_order_sale_2
176 | cls.sale_exp_deliv_sale_3
177 | cls.sale_exp_deliv_sale_4
178 | cls.sale_exp_deliv_cost_5
179 | cls.sale_exp_order_cost_6
180 )
181 cls.sale_expense_all.action_submit()
182 cls.sale_expense_all._do_approve() # Skip duplicate wizard
183
184 def test_expenses_reinvoice_case_1_create_moves(self):
185 """
186 CASE 1: Creation of the expenses moves. The sale order lines are created.
187 """
188 self.post_expenses_with_wizard(self.sale_expense_all)
189
190 self.assertRecordValues(self.expense_sale_order.order_line, [
191 # [0] Line not created from a re-invoiced, should never be changed
192 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'is_expense': False, 'expense_ids': []},
193 # [1-6] Expenses Lines: created with the correct quantities and linked to the expense
194 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'is_expense': True, 'expense_ids': [self.sale_exp_order_sale_1.id]},
195 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'is_expense': True, 'expense_ids': [self.sale_exp_order_sale_2.id]},
196 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'is_expense': True, 'expense_ids': [self.sale_exp_deliv_sale_3.id]},
197 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'is_expense': True, 'expense_ids': [self.sale_exp_deliv_sale_4.id]},
198 {'qty_delivered': 5.0, 'product_uom_qty': 5.0, 'is_expense': True, 'expense_ids': [self.sale_exp_deliv_cost_5.id]},
199 {'qty_delivered': 6.0, 'product_uom_qty': 6.0, 'is_expense': True, 'expense_ids': [self.sale_exp_order_cost_6.id]},
200 ])
201
202 def test_expenses_reinvoice_case_2_reset_expense_to_draft(self):
203 """
204 CASE 2: Reset to draft of the expenses, the quantities of the corresponding SOL are set to 0
205 """
206 # CASE 1 steps
207 self.post_expenses_with_wizard(self.sale_expense_all)
208
209 # CASE 2 steps
210 self.sale_expense_all.account_move_id.button_draft()
211 self.sale_expense_all.account_move_id.unlink()
212 self.sale_expense_all.action_reset()
213
214 self.assertRecordValues(self.expense_sale_order.order_line, [
215 # [0] Line not created from a re-invoiced, should never be changed
216 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
217 # [1-6] Expense Lines: quantities are reset to 0 and expenses are unlinked
218 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
219 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
220 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
221 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
222 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
223 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
224 ])
225
226 def test_expenses_reinvoice_case_3_recreate_move_after_reset(self):
227 """
228 CASE 3: Re-Approve and Re-Post the expense after a reset, creating new SOLs with the correct quantities
229 """
230 # CASE 1 steps
231 self.post_expenses_with_wizard(self.sale_expense_all)
232
233 # CASE 2 steps
234 self.sale_expense_all.account_move_id.button_draft()
235 self.sale_expense_all.account_move_id.unlink()
236 self.sale_expense_all.action_reset()
237
238 # CASE 3 steps
239 self.sale_expense_all.action_submit()
240 self.sale_expense_all._do_approve() # Skip duplicate wizard
241 self.post_expenses_with_wizard(self.sale_expense_all)
242
243 self.assertRecordValues(self.expense_sale_order.order_line, [
244 # [0] Line not created from a re-invoiced, should never be changed
245 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
246 # [1-6] CASE 2 Lines: no change
247 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
248 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
249 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
250 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
251 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
252 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
253 # [7-12] CASE 3 Lines: created with the correct quantities and linked to the expense
254 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_order_sale_1.id]},
255 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_order_sale_2.id]},
256 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_deliv_sale_3.id]},
257 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_deliv_sale_4.id]},
258 {'qty_delivered': 5.0, 'product_uom_qty': 5.0, 'expense_ids': [self.sale_exp_deliv_cost_5.id]},
259 {'qty_delivered': 6.0, 'product_uom_qty': 6.0, 'expense_ids': [self.sale_exp_order_cost_6.id]},
260 ])
261
262 def test_expenses_reinvoice_case_4_reset_expense_move_to_draft(self):
263 """
264 CASE 4: Reset to draft of the expenses move, the quantities of the corresponding SOL are set to 0
265 """
266 # CASE 1 steps
267 self.post_expenses_with_wizard(self.sale_expense_all)
268
269 # CASE 4 steps
270 self.sale_expense_all.account_move_id.button_draft()
271
272 self.assertRecordValues(self.expense_sale_order.order_line, [
273 # [0] Line not created from a re-invoiced, should never be changed
274 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
275 # [1-6] EXPENSES Lines: quantities are reset to 0 and expenses are unlinked
276 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
277 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
278 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
279 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
280 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
281 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
282 ])
283
284 def test_expenses_reinvoice_case_5_repost_expense_move_after_reset_to_draft(self):
285 """
286 CASE 5: Re-Post the expenses move, creating new SOLs with the correct quantities
287 """
288 # CASE 1 steps
289 self.post_expenses_with_wizard(self.sale_expense_all)
290
291 # CASE 4 steps
292 self.sale_expense_all.account_move_id.button_draft()
293
294 # CASE 5 steps
295 self.sale_expense_all.account_move_id.action_post()
296
297 self.assertRecordValues(self.expense_sale_order.order_line, [
298 # [0] Line not created from a re-invoiced, should never be changed
299 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
300 # [1-6] EXPENSE CASE 4 Lines: no change
301 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
302 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
303 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
304 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
305 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
306 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
307 # [7-12] EXPENSE CASE 5 Lines: created with the correct quantities and linked to the expense
308 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_order_sale_1.id]},
309 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_order_sale_2.id]},
310 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_deliv_sale_3.id]},
311 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_deliv_sale_4.id]},
312 {'qty_delivered': 5.0, 'product_uom_qty': 5.0, 'expense_ids': [self.sale_exp_deliv_cost_5.id]},
313 {'qty_delivered': 6.0, 'product_uom_qty': 6.0, 'expense_ids': [self.sale_exp_order_cost_6.id]},
314 ])
315
316 def test_expenses_reinvoice_case_6_reverse_expense_move(self):
317 """
318 CASE 6: Reverse the expenses move, the quantities of the corresponding SOL are reset to 0
319 """
320 # CASE 1 steps
321 self.post_expenses_with_wizard(self.sale_expense_all)
322
323 # CASE 6 steps
324 self.sale_expense_all.account_move_id._reverse_moves()
325
326 self.assertRecordValues(self.expense_sale_order.order_line, [
327 # [0] Line not created from a re-invoiced, should never be changed
328 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
329 # [1-6] EXPENSE Lines: quantities are reset to 0 and expenses are unlinked
330 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
331 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
332 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
333 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
334 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
335 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
336 ])
337
338 def test_expenses_reinvoice_case_7_ensure_one2one_relationship(self):
339 """
340 CASE 7: Test that two exact same sols are not both reset to 0 when the expense of one of them is resetting the quantities to 0
341 """
342 # For every expense, we duplicate it.
343 # - the former will be linked to the same sol
344 # - the latter will go on a different
345 sale_exp_order_sale_1_copy = self.sale_exp_order_sale_1.copy()
346 sale_exp_order_sale_2_copy = self.sale_exp_order_sale_2.copy()
347 sale_exp_deliv_sale_3_copy = self.sale_exp_deliv_sale_3.copy()
348 sale_exp_deliv_sale_4_copy = self.sale_exp_deliv_sale_4.copy()
349 sale_exp_deliv_cost_5_copy = self.sale_exp_deliv_cost_5.copy()
350 sale_exp_order_cost_6_copy = self.sale_exp_order_cost_6.copy()
351
352 sale_expense_copies_all = (
353 sale_exp_order_sale_1_copy
354 | sale_exp_order_sale_2_copy
355 | sale_exp_deliv_sale_3_copy
356 | sale_exp_deliv_sale_4_copy
357 | sale_exp_deliv_cost_5_copy
358 | sale_exp_order_cost_6_copy
359 )
360 sale_expense_original_all = self.sale_expense_all
361 self.sale_expense_all |= sale_expense_copies_all
362
363 sale_expense_copies_all.action_submit()
364 sale_expense_copies_all._do_approve() # Skip duplicate wizard
365 self.post_expenses_with_wizard(sale_expense_original_all)
366 self.post_expenses_with_wizard(sale_expense_copies_all) # To ensure there are two different moves
367
368 # Check that all the expenses can be found on the sale order
369 self.assertRecordValues(self.expense_sale_order.order_line, [
370 # [0] Line not created from a re-invoiced, should never be changed
371 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
372 # [1-6] Original Lines: Created with the correct quantities
373 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_order_sale_1.id]},
374 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_order_sale_2.id]},
375 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_deliv_sale_3.id]},
376 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [self.sale_exp_deliv_sale_4.id]},
377 {'qty_delivered': 5.0, 'product_uom_qty': 5.0, 'expense_ids': [self.sale_exp_deliv_cost_5.id]},
378 {'qty_delivered': 6.0, 'product_uom_qty': 6.0, 'expense_ids': [self.sale_exp_order_cost_6.id]},
379 # [7-12] Copy Lines: Created with the correct quantities
380 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_order_sale_1_copy.id]},
381 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_order_sale_2_copy.id]},
382 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_deliv_sale_3_copy.id]},
383 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_deliv_sale_4_copy.id]},
384 {'qty_delivered': 5.0, 'product_uom_qty': 5.0, 'expense_ids': [sale_exp_deliv_cost_5_copy.id]},
385 {'qty_delivered': 6.0, 'product_uom_qty': 6.0, 'expense_ids': [sale_exp_order_cost_6_copy.id]},
386 ])
387
388 # Reset the six expenses to draft and check that only them are unlinked
389 sale_expense_original_all.account_move_id.button_draft()
390 self.assertRecordValues(self.expense_sale_order.order_line, [
391 # [0] Line not created from a re-invoiced, should never be changed
392 {'qty_delivered': 0.0, 'product_uom_qty': 3.0, 'expense_ids': []},
393 # [1-6] Original Lines: quantities are reset to 0 and expenses are unlinked
394 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
395 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
396 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
397 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
398 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
399 {'qty_delivered': 0.0, 'product_uom_qty': 0.0, 'expense_ids': []},
400 # [7-12] Copy Lines: Not caught by the reset
401 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_order_sale_1_copy.id]},
402 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_order_sale_2_copy.id]},
403 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_deliv_sale_3_copy.id]},
404 {'qty_delivered': 1.0, 'product_uom_qty': 1.0, 'expense_ids': [sale_exp_deliv_sale_4_copy.id]},
405 {'qty_delivered': 5.0, 'product_uom_qty': 5.0, 'expense_ids': [sale_exp_deliv_cost_5_copy.id]},
406 {'qty_delivered': 6.0, 'product_uom_qty': 6.0, 'expense_ids': [sale_exp_order_cost_6_copy.id]},
407 ])
408
409 def test_expenses_reinvoice_analytic_distribution(self):
410 """ Test expense line with multiple analytic accounts is re-invoiced correctly """
411
412 (self.company_data['product_order_sales_price'] + self.company_data['product_delivery_sales_price']).write({
413 'can_be_expensed': True,
414 })
415
416 # create SO line and confirm SO (with only one line)
417 sale_order = self.env['sale.order'].with_context(mail_notrack=True, mail_create_nolog=True).create({
418 'partner_id': self.partner_a.id,
419 'partner_invoice_id': self.partner_a.id,
420 'partner_shipping_id': self.partner_a.id,
421 'order_line': [Command.create({
422 'name': self.company_data['product_order_sales_price'].name,
423 'product_id': self.company_data['product_order_sales_price'].id,
424 'product_uom_qty': 2.0,
425 'price_unit': 1000.0,
426 })],
427 })
428 sale_order.action_confirm()
429
430 expense = self.create_expenses({
431 'name': 'expense_1',
432 'date': '2016-01-01',
433 'product_id': self.company_data['product_order_sales_price'].id,
434 'quantity': 2,
435 'analytic_distribution': {self.analytic_account_1.id: 50, self.analytic_account_2.id: 50},
436 'employee_id': self.expense_employee.id,
437 'sale_order_id': sale_order.id,
438 })
439
440 expense.action_submit()
441 expense.action_approve()
442 self.post_expenses_with_wizard(expense)
443
444 self.assertRecordValues(sale_order.order_line, [
445 # Original SO line:
446 {'qty_delivered': 0.0, 'product_uom_qty': 2.0, 'is_expense': False},
447 # Expense line:
448 {'qty_delivered': 2.0,'product_uom_qty': 2.0, 'is_expense': True},
449 ])
450
451 def test_expense_reinvoice_tax_multine_line(self):
452 """
453 Tests that when a tax has multine distribution, the creation of an expense can go forward without issues
454 """
455 multi_distribution_tax = self.env['account.tax'].create({
456 'name': 'Tax 10.00%',
457 'amount': 10.00,
458 'amount_type': 'percent',
459 'type_tax_use': 'purchase',
460 'invoice_repartition_line_ids': [
461 Command.create({
462 'repartition_type': 'base',
463 'use_in_tax_closing': False,
464 }),
465 Command.create({
466 'repartition_type': 'tax',
467 'factor_percent': 70,
468 'use_in_tax_closing': False,
469 }),
470 Command.create({
471 'repartition_type': 'tax',
472 'factor_percent': 30,
473 'account_id': self.company_data['default_account_tax_purchase'].id,
474 'use_in_tax_closing': True,
475 }),
476 ],
477 'refund_repartition_line_ids': [
478 Command.create({
479 'repartition_type': 'base',
480 'use_in_tax_closing': False,
481 }),
482 Command.create({
483 'repartition_type': 'tax',
484 'factor_percent': 70,
485 'use_in_tax_closing': False,
486 }),
487 Command.create({
488 'repartition_type': 'tax',
489 'factor_percent': 30,
490 'account_id': self.company_data['default_account_tax_purchase'].id,
491 'use_in_tax_closing': True,
492 }),
493 ],
494 })
495 (self.company_data['product_order_sales_price'] + self.company_data['product_delivery_sales_price']).write({
496 'can_be_expensed': True,
497 })
498
499 # create SO line and confirm SO (with only one line)
500 sale_order = self.env['sale.order'].with_context(mail_notrack=True, mail_create_nolog=True).create({
501 'partner_id': self.partner_a.id,
502 'partner_invoice_id': self.partner_a.id,
503 'partner_shipping_id': self.partner_a.id,
504 'order_line': [Command.create({
505 'name': self.company_data['product_order_sales_price'].name,
506 'product_id': self.company_data['product_order_sales_price'].id,
507 'product_uom_qty': 1.0,
508 'price_unit': 1000.0,
509 })],
510 })
511 sale_order.action_confirm()
512
513 expense = self.create_expenses([{
514 'name': 'expense_1',
515 'date': '2016-01-01',
516 'product_id': self.company_data['product_order_sales_price'].id,
517 'quantity': 1,
518 'employee_id': self.expense_employee.id,
519 'sale_order_id': sale_order.id,
520 'tax_ids': multi_distribution_tax.ids,
521 }])
522
523 expense.action_submit()
524 expense._do_approve()
525 self.post_expenses_with_wizard(expense)
526
527 self.assertRecordValues(sale_order.order_line, [
528 # Original SO line:
529 {
530 'qty_delivered': 0.0,
531 'product_uom_qty': 1.0,
532 'is_expense': False,
533 },
534 # Expense lines:
535 {
536 'qty_delivered': 1.0,
537 'product_uom_qty': 1.0,
538 'is_expense': True,
539 },
540 ])