Debian Codegraph @

addons/sale_project/tests/test_reinvoice.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from freezegun import freeze_time
4 from odoo.addons.sale.tests.common import TestSaleCommon
5 from odoo.tests import Form, tagged
6 from odoo.fields import Command
7
8
9 @tagged('post_install', '-at_install')
10 class TestReInvoice(TestSaleCommon):
11
12 @classmethod
13 def setUpClass(cls):
14 super().setUpClass()
15 cls.env.user.group_ids += cls.quick_ref('project.group_project_manager')
16
17 cls.analytic_plan = cls.env['account.analytic.plan'].create({
18 'name': 'Plan',
19 })
20
21 cls.analytic_account = cls.env['account.analytic.account'].create({
22 'name': 'Test AA',
23 'code': 'TESTSALE_REINVOICE',
24 'company_id': cls.partner_a.company_id.id,
25 'plan_id': cls.analytic_plan.id,
26 'partner_id': cls.partner_a.id
27 })
28
29 cls.project = cls.env['project.project'].create({
30 'name': 'SO Project',
31 f'{cls.analytic_plan._column_name()}': cls.analytic_account.id,
32 })
33 # Remove the analytic account auto-generated when creating a timesheetable project if it exists
34 cls.project.account_id = False
35
36 cls.sale_order = cls.env['sale.order'].with_context(mail_notrack=True, mail_create_nolog=True).create({
37 'partner_id': cls.partner_a.id,
38 'partner_invoice_id': cls.partner_a.id,
39 'partner_shipping_id': cls.partner_a.id,
40 'project_id': cls.project.id,
41 })
42
43 cls.AccountMove = cls.env['account.move'].with_context(
44 default_move_type='in_invoice',
45 default_invoice_date=cls.sale_order.date_order,
46 mail_notrack=True,
47 mail_create_nolog=True,
48 )
49
50 def test_at_cost(self):
51 # Required for `analytic_distribution` to be visible in the view
52 self.env.user.group_ids += self.env.ref('analytic.group_analytic_accounting')
53 """ Test vendor bill at cost for product based on ordered and delivered quantities. """
54 # create SO line and confirm SO (with only one line)
55 sale_order_line1 = self.env['sale.order.line'].create({
56 'product_id': self.company_data['product_order_cost'].id,
57 'product_uom_qty': 2,
58 'qty_delivered': 1,
59 'order_id': self.sale_order.id,
60 })
61 sale_order_line2 = self.env['sale.order.line'].create({
62 'product_id': self.company_data['product_delivery_cost'].id,
63 'product_uom_qty': 4,
64 'qty_delivered': 1,
65 'order_id': self.sale_order.id,
66 })
67
68 self.sale_order.action_confirm()
69
70 # create invoice lines and validate it
71 move_form = Form(self.AccountMove)
72 move_form.partner_id = self.partner_a
73 with move_form.invoice_line_ids.new() as line_form:
74 line_form.product_id = self.company_data['product_order_cost']
75 line_form.quantity = 3.0
76 line_form.analytic_distribution = {self.analytic_account.id: 100}
77 with move_form.invoice_line_ids.new() as line_form:
78 line_form.product_id = self.company_data['product_delivery_cost']
79 line_form.quantity = 3.0
80 line_form.analytic_distribution = {self.analytic_account.id: 100}
81 invoice_a = move_form.save()
82 invoice_a.action_post()
83
84 sale_order_line3 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line1 and sol.product_id == self.company_data['product_order_cost'])
85 sale_order_line4 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line2 and sol.product_id == self.company_data['product_delivery_cost'])
86
87 self.assertTrue(sale_order_line3, "A new sale line should have been created with ordered product")
88 self.assertTrue(sale_order_line4, "A new sale line should have been created with delivered product")
89 self.assertEqual(len(self.sale_order.order_line), 4, "There should be 4 lines on the SO (2 vendor bill lines created)")
90 self.assertEqual(len(self.sale_order.order_line.filtered(lambda sol: sol.is_expense)), 2, "There should be 4 lines on the SO (2 vendor bill lines created)")
91
92 self.assertEqual((sale_order_line3.price_unit, sale_order_line3.qty_delivered, sale_order_line3.product_uom_qty, sale_order_line3.qty_invoiced), (self.company_data['product_order_cost'].standard_price, 3, 3, 0), 'Sale line is wrong after confirming vendor invoice')
93 self.assertEqual((sale_order_line4.price_unit, sale_order_line4.qty_delivered, sale_order_line4.product_uom_qty, sale_order_line4.qty_invoiced), (self.company_data['product_delivery_cost'].standard_price, 3, 3, 0), 'Sale line is wrong after confirming vendor invoice')
94
95 self.assertEqual(sale_order_line3.qty_delivered_method, 'analytic', "Delivered quantity of 'expense' SO line should be computed by analytic amount")
96 self.assertEqual(sale_order_line4.qty_delivered_method, 'analytic', "Delivered quantity of 'expense' SO line should be computed by analytic amount")
97
98 # create second invoice lines and validate it
99 move_form = Form(self.AccountMove)
100 move_form.partner_id = self.partner_a
101 with move_form.invoice_line_ids.new() as line_form:
102 line_form.product_id = self.company_data['product_order_cost']
103 line_form.quantity = 2.0
104 line_form.analytic_distribution = {self.analytic_account.id: 100}
105 with move_form.invoice_line_ids.new() as line_form:
106 line_form.product_id = self.company_data['product_delivery_cost']
107 line_form.quantity = 2.0
108 line_form.analytic_distribution = {self.analytic_account.id: 100}
109 invoice_b = move_form.save()
110 invoice_b.action_post()
111
112 sale_order_line5 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line1 and sol != sale_order_line3 and sol.product_id == self.company_data['product_order_cost'])
113 sale_order_line6 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line2 and sol != sale_order_line4 and sol.product_id == self.company_data['product_delivery_cost'])
114
115 self.assertTrue(sale_order_line5, "A new sale line should have been created with ordered product")
116 self.assertTrue(sale_order_line6, "A new sale line should have been created with delivered product")
117
118 self.assertEqual(len(self.sale_order.order_line), 6, "There should be still 4 lines on the SO, no new created")
119 self.assertEqual(len(self.sale_order.order_line.filtered(lambda sol: sol.is_expense)), 4, "There should be still 2 expenses lines on the SO")
120
121 self.assertEqual((sale_order_line5.price_unit, sale_order_line5.qty_delivered, sale_order_line5.product_uom_qty, sale_order_line5.qty_invoiced), (self.company_data['product_order_cost'].standard_price, 2, 2, 0), 'Sale line 5 is wrong after confirming 2e vendor invoice')
122 self.assertEqual((sale_order_line6.price_unit, sale_order_line6.qty_delivered, sale_order_line6.product_uom_qty, sale_order_line6.qty_invoiced), (self.company_data['product_delivery_cost'].standard_price, 2, 2, 0), 'Sale line 6 is wrong after confirming 2e vendor invoice')
123
124 @freeze_time('2020-01-15')
125 def test_sales_team_invoiced(self):
126 """ Test invoiced field from sales team ony take into account the amount the sales channel has invoiced this month """
127
128 invoices = self.env['account.move'].create([
129 {
130 'move_type': 'out_invoice',
131 'partner_id': self.partner_a.id,
132 'invoice_date': '2020-01-10',
133 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id, 'price_unit': 1000.0})],
134 },
135 {
136 'move_type': 'out_refund',
137 'partner_id': self.partner_a.id,
138 'invoice_date': '2020-01-10',
139 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id, 'price_unit': 500.0})],
140 },
141 {
142 'move_type': 'in_invoice',
143 'partner_id': self.partner_a.id,
144 'invoice_date': '2020-01-01',
145 'date': '2020-01-01',
146 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id, 'price_unit': 800.0})],
147 },
148 ])
149 invoices.action_post()
150
151 for invoice in invoices:
152 self.env['account.payment.register']\
153 .with_context(active_model='account.move', active_ids=invoice.ids)\
154 .create({})\
155 ._create_payments()
156
157 invoices.flush_model()
158 self.assertRecordValues(invoices.team_id, [{'invoiced': 500.0}])
159
160 def test_sales_price(self):
161 """ Test invoicing vendor bill at sales price for products based on delivered and ordered quantities. Check no existing SO line is incremented, but when invoicing a
162 second time, increment only the delivered so line.
163 """
164 # Required for `analytic_distribution` to be visible in the view
165 self.env.user.group_ids += self.env.ref('analytic.group_analytic_accounting')
166 # create SO line and confirm SO (with only one line)
167 sale_order_line1 = self.env['sale.order.line'].create({
168 'product_id': self.company_data['product_delivery_sales_price'].id,
169 'product_uom_qty': 2,
170 'qty_delivered': 1,
171 'order_id': self.sale_order.id,
172 })
173 sale_order_line2 = self.env['sale.order.line'].create({
174 'product_id': self.company_data['product_order_sales_price'].id,
175 'product_uom_qty': 3,
176 'qty_delivered': 1,
177 'order_id': self.sale_order.id,
178 })
179 self.sale_order.action_confirm()
180
181 # create invoice lines and validate it
182 move_form = Form(self.AccountMove)
183 move_form.partner_id = self.partner_a
184 with move_form.invoice_line_ids.new() as line_form:
185 line_form.product_id = self.company_data['product_delivery_sales_price']
186 line_form.quantity = 3.0
187 line_form.analytic_distribution = {self.analytic_account.id: 100}
188 with move_form.invoice_line_ids.new() as line_form:
189 line_form.product_id = self.company_data['product_order_sales_price']
190 line_form.quantity = 3.0
191 line_form.analytic_distribution = {self.analytic_account.id: 100}
192 invoice_a = move_form.save()
193 invoice_a.action_post()
194
195 sale_order_line3 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line1 and sol.product_id == self.company_data['product_delivery_sales_price'])
196 sale_order_line4 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line2 and sol.product_id == self.company_data['product_order_sales_price'])
197
198 self.assertTrue(sale_order_line3, "A new sale line should have been created with ordered product")
199 self.assertTrue(sale_order_line4, "A new sale line should have been created with delivered product")
200 self.assertEqual(len(self.sale_order.order_line), 4, "There should be 4 lines on the SO (2 vendor bill lines created)")
201 self.assertEqual(len(self.sale_order.order_line.filtered(lambda sol: sol.is_expense)), 2, "There should be 4 lines on the SO (2 vendor bill lines created)")
202
203 self.assertEqual((sale_order_line3.price_unit, sale_order_line3.qty_delivered, sale_order_line3.product_uom_qty, sale_order_line3.qty_invoiced), (self.company_data['product_delivery_sales_price'].list_price, 3, 3, 0), 'Sale line is wrong after confirming vendor invoice')
204 self.assertEqual((sale_order_line4.price_unit, sale_order_line4.qty_delivered, sale_order_line4.product_uom_qty, sale_order_line4.qty_invoiced), (self.company_data['product_order_sales_price'].list_price, 3, 3, 0), 'Sale line is wrong after confirming vendor invoice')
205
206 self.assertEqual(sale_order_line3.qty_delivered_method, 'analytic', "Delivered quantity of 'expense' SO line 3 should be computed by analytic amount")
207 self.assertEqual(sale_order_line4.qty_delivered_method, 'analytic', "Delivered quantity of 'expense' SO line 4 should be computed by analytic amount")
208
209 # create second invoice lines and validate it
210 move_form = Form(self.AccountMove)
211 move_form.partner_id = self.partner_a
212 with move_form.invoice_line_ids.new() as line_form:
213 line_form.product_id = self.company_data['product_delivery_sales_price']
214 line_form.quantity = 2.0
215 line_form.analytic_distribution = {self.analytic_account.id: 100}
216 with move_form.invoice_line_ids.new() as line_form:
217 line_form.product_id = self.company_data['product_order_sales_price']
218 line_form.quantity = 2.0
219 line_form.analytic_distribution = {self.analytic_account.id: 100}
220 invoice_b = move_form.save()
221 invoice_b.action_post()
222
223 sale_order_line5 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line1 and sol != sale_order_line3 and sol.product_id == self.company_data['product_delivery_sales_price'])
224 sale_order_line6 = self.sale_order.order_line.filtered(lambda sol: sol != sale_order_line2 and sol != sale_order_line4 and sol.product_id == self.company_data['product_order_sales_price'])
225
226 self.assertFalse(sale_order_line5, "No new sale line should have been created with delivered product !!")
227 self.assertTrue(sale_order_line6, "A new sale line should have been created with ordered product")
228
229 self.assertEqual(len(self.sale_order.order_line), 5, "There should be 5 lines on the SO, 1 new created and 1 incremented")
230 self.assertEqual(len(self.sale_order.order_line.filtered(lambda sol: sol.is_expense)), 3, "There should be 3 expenses lines on the SO")
231
232 self.assertEqual((sale_order_line6.price_unit, sale_order_line6.qty_delivered, sale_order_line4.product_uom_qty, sale_order_line6.qty_invoiced), (self.company_data['product_order_sales_price'].list_price, 2, 3, 0), 'Sale line is wrong after confirming 2e vendor invoice')
233
234 def test_no_expense(self):
235 """ Test invoicing vendor bill with no policy. Check nothing happen. """
236 # Required for `analytic_distribution` to be visible in the view
237 self.env.user.group_ids += self.env.ref('analytic.group_analytic_accounting')
238 # confirm SO
239 self.env['sale.order.line'].create({
240 'product_id': self.company_data['product_delivery_no'].id,
241 'product_uom_qty': 2,
242 'qty_delivered': 1,
243 'order_id': self.sale_order.id,
244 })
245 self.sale_order.action_confirm()
246
247 # create invoice lines and validate it
248 move_form = Form(self.AccountMove)
249 move_form.partner_id = self.partner_a
250 with move_form.invoice_line_ids.new() as line_form:
251 line_form.product_id = self.company_data['product_delivery_no']
252 line_form.quantity = 3.0
253 line_form.analytic_distribution = {self.analytic_account.id: 100}
254 invoice_a = move_form.save()
255 invoice_a.action_post()
256
257 self.assertEqual(len(self.sale_order.order_line), 1, "No SO line should have been created (or removed) when validating vendor bill")
258 self.assertTrue(invoice_a.mapped('line_ids.analytic_line_ids'), "Analytic lines should be generated")
259
260 def test_not_reinvoicing_invoiced_so_lines(self):
261 """ Test that invoiced SO lines are not re-invoiced. """
262 so_line1 = self.env['sale.order.line'].create({
263 'product_id': self.company_data['product_delivery_cost'].id,
264 'discount': 100.00,
265 'order_id': self.sale_order.id,
266 })
267 so_line2 = self.env['sale.order.line'].create({
268 'product_id': self.company_data['product_delivery_sales_price'].id,
269 'discount': 100.00,
270 'order_id': self.sale_order.id,
271 })
272
273 self.sale_order.action_confirm()
274
275 for line in self.sale_order.order_line:
276 line.qty_delivered = 1
277 # create invoice and validate it
278 invoice = self.sale_order._create_invoices()
279 invoice.action_post()
280
281 so_line3 = self.sale_order.order_line.filtered(lambda sol: sol != so_line1 and sol.product_id == self.company_data['product_delivery_cost'])
282 so_line4 = self.sale_order.order_line.filtered(lambda sol: sol != so_line2 and sol.product_id == self.company_data['product_delivery_sales_price'])
283
284 self.assertFalse(so_line3, "No re-invoicing should have created a new sale line with product #1")
285 self.assertFalse(so_line4, "No re-invoicing should have created a new sale line with product #2")
286 self.assertEqual(so_line1.qty_delivered, 1, "No re-invoicing should have impacted exising SO line 1")
287 self.assertEqual(so_line2.qty_delivered, 1, "No re-invoicing should have impacted exising SO line 2")
288
289 def test_not_recomputing_unit_price_for_expensed_so_lines(self):
290 # Required for `analytic_distribution` to be visible in the view
291 self.env.user.group_ids += self.env.ref('analytic.group_analytic_accounting')
292
293 # create SO line and confirm SO (with only one line)
294 sol_1 = self.env['sale.order.line'].create({
295 'product_id': self.company_data['product_order_cost'].id,
296 'product_uom_qty': 2,
297 'qty_delivered': 1,
298 'order_id': self.sale_order.id,
299 })
300 self.sale_order.action_confirm()
301
302 # create invoice lines and validate it
303 move_form = Form(self.AccountMove)
304 move_form.partner_id = self.partner_a
305 with move_form.invoice_line_ids.new() as line_form:
306 line_form.product_id = self.company_data['product_order_cost']
307 line_form.quantity = 3.0
308 line_form.analytic_distribution = {self.analytic_account.id: 100}
309 invoice = move_form.save()
310 invoice.action_post()
311
312 # update the quantity of the expensed line
313 sol_2 = self.sale_order.order_line.filtered(lambda sol: sol != sol_1 and sol.product_id == self.company_data['product_order_cost'])
314
315 sol_2_subtotal_before = sol_2.price_unit
316 sol_2.product_uom_qty = 3.0
317 sol_2_subtotal_after = sol_2.price_unit
318
319 self.assertEqual(sol_2_subtotal_before, sol_2_subtotal_after)
320
321 def test_cost_invoicing(self):
322 """ Test confirming a vendor invoice to reinvoice cost on the so """
323 serv_cost = self.env['product.product'].create({
324 'name': "Ordered at cost",
325 'standard_price': 160,
326 'list_price': 180,
327 'type': 'consu',
328 'invoice_policy': 'order',
329 'expense_policy': 'cost',
330 'default_code': 'PROD_COST',
331 'service_type': 'manual',
332 })
333 prod_gap = self.company_data['product_service_order']
334 project = self.env['project.project'].create({'name': 'SO Project'})
335 if not project.account_id:
336 project._create_analytic_account()
337
338 self.sale_order.write({
339 'project_id': project.id,
340 'order_line': [Command.create({
341 'product_id': prod_gap.id,
342 'product_uom_qty': 2,
343 'price_unit': prod_gap.list_price,
344 })],
345 })
346 self.sale_order.action_confirm()
347
348 inv = self.env['account.move'].with_context(default_move_type='in_invoice').create({
349 'partner_id': self.partner_a.id,
350 'invoice_date': self.sale_order.date_order,
351 'invoice_line_ids': [
352 Command.create({
353 'name': serv_cost.name,
354 'product_id': serv_cost.id,
355 'product_uom_id': serv_cost.uom_id.id,
356 'quantity': 2,
357 'price_unit': serv_cost.standard_price,
358 'analytic_distribution': {self.sale_order.project_account_id.id: 100},
359 }),
360 ],
361 })
362 inv.action_post()
363 sol = self.sale_order.order_line.filtered(lambda l: l.product_id == serv_cost)
364 self.assertTrue(sol, 'Sale: cost invoicing does not add lines when confirming vendor invoice')
365 self.assertEqual(
366 (sol.price_unit, sol.qty_delivered, sol.product_uom_qty, sol.qty_invoiced),
367 (160, 2, 2, 0),
368 'Sale: line is wrong after confirming vendor invoice')
369
370 def test_invoice_analytic_account_so_not_default(self):
371 """ Tests whether, when an analytic account rule is set and the so has an analytic account,
372 the default analytic account is not replaced by the one from the so in the invoice.
373 """
374 analytic_plan_default = self.env['account.analytic.plan'].create({'name': 'default'})
375 analytic_account_default = self.env['account.analytic.account'].create({'name': 'default', 'plan_id': analytic_plan_default.id})
376 analytic_account_so = self.env['account.analytic.account'].create({'name': 'so', 'plan_id': analytic_plan_default.id})
377
378 self.env['account.analytic.distribution.model'].create({
379 'analytic_distribution': {analytic_account_default.id: 100},
380 'product_id': self.product_a.id,
381 })
382 project = self.env['project.project'].create({
383 'name': 'SO Project',
384 f'{analytic_plan_default._column_name()}': analytic_account_so.id,
385 })
386 # Remove the analytic account auto-generated when creating a timesheetable project if it exists
387 project.account_id = False
388
389 so_form = Form(self.env['sale.order'])
390 so_form.partner_id = self.partner_a
391 so_form.project_id = project
392
393 with so_form.order_line.new() as sol:
394 sol.product_id = self.product_a
395 sol.product_uom_qty = 1
396
397 so = so_form.save()
398 so.action_confirm()
399 so._force_lines_to_invoice_policy_order()
400
401 so_context = {
402 'active_model': 'sale.order',
403 'active_ids': [so.id],
404 'active_id': so.id,
405 'default_journal_id': self.company_data['default_journal_sale'].id,
406 }
407 down_payment = self.env['sale.advance.payment.inv'].with_context(so_context).create({})
408 down_payment.create_invoices()
409
410 aml = self.env['account.move.line'].search([('move_id', 'in', so.invoice_ids.ids)])[0]
411 self.assertRecordValues(aml, [{'analytic_distribution': {str(analytic_account_default.id): 100}}])