Debian Codegraph @

addons/stock_account/tests/test_account_move.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 from freezegun import freeze_time
4
5 from odoo.addons.stock_account.tests.common import TestStockValuationCommon
6 from odoo.exceptions import UserError
7 from odoo.tests import Form, tagged
8 from odoo import fields, Command
9
10
11 @tagged("post_install", "-at_install")
12 class TestAccountMove(TestStockValuationCommon):
13 def test_standard_perpetual_01_mc_01(self):
14 product = self.product_standard_auto
15 self._use_multi_currencies([('2017-01-01', 2.0)])
16 rate = self.other_currency.rate_ids.rate
17
18 move_form = Form(self.env["account.move"].with_context(default_move_type="out_invoice"))
19 move_form.partner_id = self.partner
20 move_form.currency_id = self.other_currency
21 with move_form.invoice_line_ids.new() as line_form:
22 line_form.product_id = product
23 line_form.tax_ids.clear()
24 invoice = move_form.save()
25
26 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_total)
27 self.assertEqual(len(invoice.mapped("line_ids")), 2)
28 self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 1)
29
30 invoice._post()
31
32 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_total)
33 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_residual)
34 self.assertEqual(len(invoice.mapped("line_ids")), 4)
35 self.assertEqual(len(invoice.mapped("line_ids").filtered(lambda l: l.display_type == 'cogs')), 2)
36 self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 2)
37
38 def test_fifo_perpetual_01_mc_01(self):
39 product = self.product_fifo_auto
40 self._use_multi_currencies([('2017-01-01', 2.0)])
41 rate = self.other_currency.rate_ids.rate
42
43 move_form = Form(self.env["account.move"].with_context(default_move_type="out_invoice"))
44 move_form.partner_id = self.partner
45 move_form.currency_id = self.other_currency
46 with move_form.invoice_line_ids.new() as line_form:
47 line_form.product_id = product
48 line_form.tax_ids.clear()
49 invoice = move_form.save()
50
51 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_total)
52 self.assertEqual(len(invoice.mapped("line_ids")), 2)
53 self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 1)
54
55 invoice._post()
56 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_total)
57 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_residual)
58 self.assertEqual(len(invoice.mapped("line_ids")), 4)
59 self.assertEqual(len(invoice.mapped("line_ids").filtered(lambda l: l.display_type == 'cogs')), 2)
60 self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 2)
61
62 def test_average_perpetual_01_mc_01(self):
63 product = self.product_avco_auto
64 self._use_multi_currencies([('2017-01-01', 2.0)])
65 rate = self.other_currency.rate_ids.rate
66
67 move_form = Form(self.env["account.move"].with_context(default_move_type="out_invoice"))
68 move_form.partner_id = self.partner
69 move_form.currency_id = self.other_currency
70 with move_form.invoice_line_ids.new() as line_form:
71 line_form.product_id = product
72 line_form.tax_ids.clear()
73 invoice = move_form.save()
74
75 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_total)
76 self.assertEqual(len(invoice.mapped("line_ids")), 2)
77 self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 1)
78
79 invoice._post()
80
81 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_total)
82 self.assertAlmostEqual(product.lst_price * rate, invoice.amount_residual)
83 self.assertEqual(len(invoice.mapped("line_ids")), 4)
84 self.assertEqual(len(invoice.mapped("line_ids").filtered(lambda l: l.display_type == 'cogs')), 2)
85 self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 2)
86
87 def test_storno_accounting(self):
88 """Storno accounting uses negative numbers on debit/credit to cancel other moves.
89 This test checks that we do the same for the anglosaxon lines when storno is enabled.
90 """
91 self._use_multi_currencies([('2017-01-01', 2.0)])
92
93 product = self.product_standard_auto
94 self.env.company.account_storno = True
95 self.env.company.anglo_saxon_accounting = True
96
97 move = self.env['account.move'].create({
98 'move_type': 'out_refund',
99 'invoice_date': fields.Date.from_string('2019-01-01'),
100 'partner_id': self.partner.id,
101 'currency_id': self.other_currency.id,
102 'invoice_line_ids': [
103 (0, None, {'product_id': product.id}),
104 ]
105 })
106 move.action_post()
107
108 stock_output_line = move.line_ids.filtered(lambda l: l.account_id == self.account_stock_valuation)
109 self.assertEqual(stock_output_line.debit, 0)
110 self.assertEqual(stock_output_line.credit, -10)
111
112 expense_line = move.line_ids.filtered(lambda l: l.account_id == product._get_product_accounts()['expense'])
113 self.assertEqual(expense_line.debit, -10)
114 self.assertEqual(expense_line.credit, 0)
115
116 def test_standard_manual_tax_edit(self):
117 ''' Test manually editing tax amount, cogs creation should not reset tax amount '''
118 product = self.product_standard_auto
119 product.lst_price = 100
120 move_form = Form(self.env["account.move"].with_context(default_move_type="out_invoice"))
121 move_form.partner_id = self.partner
122 self.company.income_account_id.write({
123 'tax_ids': [(6, 0, [self.env.company.account_sale_tax_id.id])]
124 })
125 with move_form.invoice_line_ids.new() as line_form:
126 line_form.product_id = product
127 invoice = move_form.save()
128
129 self.assertEqual(invoice.amount_total, 115)
130 self.assertEqual(invoice.amount_untaxed, 100)
131 self.assertEqual(invoice.amount_tax, 15)
132
133 # simulate manual tax edit via widget
134 tax_totals = invoice.tax_totals
135 tax_totals['subtotals'][0]['tax_groups'][0]['tax_amount_currency'] = 14.0
136 invoice.tax_totals = tax_totals
137
138 self.assertEqual(len(invoice.mapped("line_ids")), 3)
139 self.assertEqual(invoice.amount_total, 114)
140 self.assertEqual(invoice.amount_untaxed, 100)
141 self.assertEqual(invoice.amount_tax, 14)
142
143 invoice._post()
144
145 self.assertEqual(len(invoice.mapped("line_ids")), 5)
146 self.assertEqual(invoice.amount_total, 114)
147 self.assertEqual(invoice.amount_untaxed, 100)
148 self.assertEqual(invoice.amount_tax, 14)
149
150 def test_basic_bill(self):
151 """
152 When billing a storable product with a basic category (manual
153 valuation), the account used should be the expenses one. This test
154 checks the flow with two companies:
155 - One that existed before the installation of `stock_account` (to test
156 the post-install hook)
157 - One created after the module installation
158 """
159 self.env.user.company_ids |= self.other_company
160
161 for company in (self.company | self.other_company):
162 bill_form = Form(self.env['account.move'].with_company(company.id).with_context(default_move_type='in_invoice'))
163 bill_form.partner_id = self.partner
164 bill_form.invoice_date = fields.Date.today()
165 with bill_form.invoice_line_ids.new() as line:
166 line.product_id = self.product_standard
167 line.price_unit = 100
168 bill = bill_form.save()
169 bill.action_post()
170
171 product_accounts = self.product_standard.product_tmpl_id.with_company(company.id).get_product_accounts()
172 self.assertEqual(bill.invoice_line_ids.account_id, product_accounts['expense'])
173
174 def test_cogs_analytic_accounting(self):
175 """Check analytic distribution is correctly propagated to COGS lines"""
176 product = self.product_standard_auto
177 default_plan = self.env['account.analytic.plan'].create({
178 'name': 'Default',
179 })
180 analytic_account = self.env['account.analytic.account'].create({
181 'name': 'Account 1',
182 'plan_id': default_plan.id,
183 'company_id': False,
184 })
185
186 move = self.env['account.move'].create({
187 'move_type': 'out_refund',
188 'invoice_date': fields.Date.from_string('2019-01-01'),
189 'partner_id': self.partner.id,
190 'invoice_line_ids': [
191 Command.create({
192 'product_id': product.id,
193 'analytic_distribution': {
194 analytic_account.id: 100,
195 },
196 }),
197 ]
198 })
199 move.action_post()
200
201 cogs_line = move.line_ids.filtered(lambda l: l.account_id == product._get_product_accounts()['expense'])
202 self.assertEqual(cogs_line.analytic_distribution, {str(analytic_account.id): 100})
203
204 def test_cogs_account_branch_company(self):
205 """Check branch company accounts are selected"""
206 product = self.product_standard_auto
207 branch = self.branch
208 test_account = self.env['account.account'].with_company(branch.id).create({
209 'name': 'STCK Test Account',
210 'code': '100119',
211 'reconcile': True,
212 'account_type': 'asset_current',
213 })
214 self.category_standard_auto.with_company(branch.id).property_valuation = "real_time"
215 self.category_standard_auto.with_company(branch.id).property_stock_valuation_account_id = test_account
216
217 bill = self.env['account.move'].with_company(branch.id).with_context(default_move_type='in_invoice').create({
218 'partner_id': self.partner.id,
219 'invoice_date': fields.Date.today(),
220 'invoice_line_ids': [
221 Command.create({
222 'product_id': product.id,
223 'price_unit': 100,
224 }),
225 ],
226 })
227 self.assertEqual(bill.invoice_line_ids.account_id, test_account)
228
229 def test_apply_inventory_adjustment_on_multiple_quants_simultaneously(self):
230 product = self.product_standard_auto
231 product_b = product.copy()
232 products = product + product_b
233
234 self._use_inventory_location_accounting()
235
236 self.env['stock.quant']._update_available_quantity(product, self.stock_location, 5)
237 self.env['stock.quant']._update_available_quantity(product_b, self.stock_location, 15)
238
239 quants = products.stock_quant_ids
240 quants.inventory_quantity = 10.0
241 wizard = self.env['stock.inventory.adjustment.name'].create({'quant_ids': quants})
242 wizard.action_apply()
243 inv_adjustment_journal_items = self.env['account.move.line'].search([('product_id', 'in', products.ids)], order='id asc', limit=4)
244 prod_a_accounts = product.product_tmpl_id.get_product_accounts()
245 prod_b_accounts = product_b.product_tmpl_id.get_product_accounts()
246 self.assertRecordValues(
247 inv_adjustment_journal_items,
248 [
249 {'account_id': self.account_inventory.id, 'product_id': product.id},
250 {'account_id': prod_a_accounts['stock_valuation'].id, 'product_id': product.id},
251 {'account_id': prod_b_accounts['stock_valuation'].id, 'product_id': product_b.id},
252 {'account_id': self.account_inventory.id, 'product_id': product_b.id},
253 ]
254 )
255
256 @freeze_time("2020-01-22")
257 def test_backdate_picking_with_lock_date(self):
258 """
259 Check that pickings can not be backdate or validated prior to the
260 fiscal and hard lock date.
261 """
262 self.env['account.lock_exception'].search([]).sudo().unlink()
263 lock_date = fields.Date.from_string('2011-01-01')
264 prior_to_lock_date = fields.Datetime.add(lock_date, days=-1)
265 post_to_lock_date = fields.Datetime.add(lock_date, days=+1)
266 self.env['stock.quant']._update_available_quantity(self.product_standard, self.stock_location, 10)
267 receipts = receipt, receipt_done = self.env['stock.picking'].create([
268 {
269 'location_id': self.supplier_location.id,
270 'location_dest_id': self.stock_location.id,
271 'picking_type_id': self.picking_type_in.id,
272 'owner_id': self.env.company.partner_id.id,
273 'move_ids': [Command.create({
274 'product_id': self.product_standard.id,
275 'location_id': self.supplier_location.id,
276 'location_dest_id': self.stock_location.id,
277 'product_uom_qty': 1.0,
278 })]
279 } for _ in range(2)
280 ])
281 receipts.action_confirm()
282 receipt_done.button_validate()
283 # Check that the purchase, sale and tax lock dates do not impose any restrictions
284 self.env.company.write({
285 'sale_lock_date': lock_date,
286 'purchase_lock_date': lock_date,
287 'tax_lock_date': lock_date,
288 })
289 # Receipts can be backdated
290 receipt.scheduled_date = prior_to_lock_date
291 receipt_done.date_done = prior_to_lock_date
292
293 # Check that the fiscal year lock date imposes restrictions
294 self.env.company.write({
295 'sale_lock_date': False,
296 'purchase_lock_date': False,
297 'tax_lock_date': False,
298 'fiscalyear_lock_date': lock_date,
299 })
300 # Receipts can not be backdated prior to lock date
301 receipt.scheduled_date = post_to_lock_date
302 receipt_done.date_done = post_to_lock_date
303 with self.assertRaises(UserError):
304 receipt.scheduled_date = prior_to_lock_date
305 with self.assertRaises(UserError):
306 receipt_done.date_done = prior_to_lock_date
307
308 # Check that the hard lock date imposes restrictions
309 self.env.company.write({
310 'fiscalyear_lock_date': False,
311 'hard_lock_date': lock_date,
312 })
313 # Receipts can not be backdated prior to lock date
314 receipt.scheduled_date = post_to_lock_date
315 receipt_done.date_done = post_to_lock_date
316 with self.assertRaises(UserError):
317 receipt.scheduled_date = prior_to_lock_date
318 with self.assertRaises(UserError):
319 receipt_done.date_done = prior_to_lock_date
320
321 def test_invoice_with_journal_item_without_label(self):
322 """Test posting an invoice whose invoice lines have no label.
323 The 'name' field is optional on account.move.line and should be
324 handled safely when generating accounting entries.
325 """
326 move = self.env['account.move'].create({
327 'move_type': 'out_invoice',
328 'partner_id': self.partner.id,
329 'invoice_line_ids': [
330 Command.create({
331 'product_id': self.product_standard.id,
332 'name': False,
333 }),
334 ],
335 })
336 move.action_post()
337 # name should remain falsy on the invoice line
338 self.assertFalse(move.invoice_line_ids.name)
339 # ensure the invoice is posted successfully
340 self.assertEqual(move.state, 'posted')