1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from freezegun import freeze_time
from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon
from odoo.tests import Form, tagged
from odoo import Command, fields
@tagged('post_install', '-at_install')
class TestValuationReconciliation(ValuationReconciliationTestCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.other_currency = cls.setup_other_currency('EUR', rounding=0.001)
@classmethod
def collect_company_accounting_data(cls, company):
company_data = super().collect_company_accounting_data(company)
# Create stock config.
company_data.update({
'default_account_stock_price_diff': cls.env['account.account'].create({
'name': 'default_account_stock_price_diff',
'code': 'STOCKDIFF',
'reconcile': True,
'account_type': 'asset_current',
}),
})
return company_data
def _create_purchase(self, product, date, quantity=1.0, set_tax=False, price_unit=66.0):
with freeze_time(date):
rslt = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
'currency_id': self.other_currency.id,
'order_line': [
(0, 0, {
'name': product.name,
'product_id': product.id,
'product_qty': quantity,
'product_uom': product.uom_po_id.id,
'price_unit': price_unit,
'date_planned': date,
'taxes_id': [(6, 0, product.supplier_taxes_id.ids)] if set_tax else False,
})],
'date_order': date,
})
rslt.button_confirm()
return rslt
def _create_invoice_for_po(self, purchase_order, date):
with freeze_time(date):
move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice', default_date=date))
move_form.invoice_date = date
move_form.partner_id = self.partner_a
move_form.currency_id = self.other_currency
move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-purchase_order.id)
return move_form.save()
def test_shipment_invoice(self):
""" Tests the case into which we receive the goods first, and then make the invoice.
"""
test_product = self.test_product_delivery
date_po_and_delivery = '2018-01-01'
purchase_order = self._create_purchase(test_product, date_po_and_delivery)
self._process_pickings(purchase_order.picking_ids, date=date_po_and_delivery)
invoice = self._create_invoice_for_po(purchase_order, '2018-02-02')
invoice.action_post()
picking = self.env['stock.picking'].search([('purchase_id','=',purchase_order.id)])
self.check_reconciliation(invoice, picking)
# cancel the invoice
invoice.button_cancel()
def test_invoice_shipment(self):
""" Tests the case into which we make the invoice first, and then receive the goods.
"""
# Create a PO and an invoice for it
test_product = self.test_product_order
purchase_order = self._create_purchase(test_product, '2017-12-01')
invoice = self._create_invoice_for_po(purchase_order, '2017-12-23')
move_form = Form(invoice)
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.quantity = 1
invoice = move_form.save()
# Validate the invoice and refund the goods
invoice.action_post()
self._process_pickings(purchase_order.picking_ids, date='2017-12-24')
picking = self.env['stock.picking'].search([('purchase_id', '=', purchase_order.id)])
self.check_reconciliation(invoice, picking)
# Return the goods and refund the invoice
with freeze_time('2018-01-13'):
stock_return_picking_form = Form(self.env['stock.return.picking'].with_context(
active_ids=picking.ids, active_id=picking.ids[0], active_model='stock.picking'))
stock_return_picking = stock_return_picking_form.save()
stock_return_picking.product_return_moves.quantity = 1.0
stock_return_picking_action = stock_return_picking.action_create_returns()
return_pick = self.env['stock.picking'].browse(stock_return_picking_action['res_id'])
return_pick.action_assign()
return_pick.move_ids.quantity = 1
return_pick.move_ids.picked = True
return_pick._action_done()
# Refund the invoice
refund_invoice_wiz = self.env['account.move.reversal'].with_context(active_model="account.move", active_ids=[invoice.id]).create({
'reason': 'test_invoice_shipment_refund',
'date': '2018-03-15',
'journal_id': invoice.journal_id.id,
})
new_invoice = self.env['account.move'].browse(refund_invoice_wiz.modify_moves()['res_id'])
refund_invoice = invoice.reversal_move_ids
# Check the result
self.assertEqual(invoice.payment_state, 'reversed', "Invoice should be in 'reversed' state")
self.assertEqual(refund_invoice.payment_state, 'paid', "Refund should be in 'paid' state")
self.assertEqual(new_invoice.state, 'draft', "New invoice should be in 'draft' state")
self.check_reconciliation(refund_invoice, return_pick)
def test_multiple_shipments_invoices(self):
""" Tests the case into which we receive part of the goods first, then 2 invoices at different rates, and finally the remaining quantities
"""
test_product = self.test_product_delivery
date_po_and_delivery0 = '2017-01-01'
purchase_order = self._create_purchase(test_product, date_po_and_delivery0, quantity=5.0)
self._process_pickings(purchase_order.picking_ids, quantity=2.0, date=date_po_and_delivery0)
picking = self.env['stock.picking'].search([('purchase_id', '=', purchase_order.id)], order="id asc", limit=1)
invoice = self._create_invoice_for_po(purchase_order, '2017-01-15')
move_form = Form(invoice)
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.quantity = 3.0
invoice = move_form.save()
invoice.action_post()
self.check_reconciliation(invoice, picking, full_reconcile=False)
invoice2 = self._create_invoice_for_po(purchase_order, '2017-02-15')
move_form = Form(invoice2)
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.quantity = 2.0
invoice2 = move_form.save()
invoice2.action_post()
self.check_reconciliation(invoice2, picking, full_reconcile=False)
# We don't need to make the date of processing explicit since the very last rate
# will be taken
self._process_pickings(purchase_order.picking_ids.filtered(lambda x: x.state != 'done'), quantity=3.0)
picking = self.env['stock.picking'].search([('purchase_id', '=', purchase_order.id)], order='id desc', limit=1)
self.check_reconciliation(invoice2, picking)
def test_rounding_discount(self):
self.env.ref("product.decimal_discount").digits = 5
tax_exclude_id = self.env["account.tax"].create(
{
"name": "Exclude tax",
"amount": "0.00",
"type_tax_use": "purchase",
}
)
test_product = self.test_product_delivery
test_product.supplier_taxes_id = [(6, 0, tax_exclude_id.ids)]
date_po_and_delivery = '2018-01-01'
purchase_order = self._create_purchase(test_product, date_po_and_delivery, quantity=10000, set_tax=True)
self._process_pickings(purchase_order.picking_ids, date=date_po_and_delivery)
invoice = self._create_invoice_for_po(purchase_order, '2018-01-01')
# Set a discount
move_form = Form(invoice)
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.discount = 0.92431
move_form.save()
invoice.action_post()
# Check the price difference amount.
invoice_layer = self.env['stock.valuation.layer'].search([('account_move_line_id', 'in', invoice.line_ids.ids)])
self.assertTrue(len(invoice_layer) == 1, "A price difference line should be created")
self.assertAlmostEqual(invoice_layer.value, -3050.22)
picking = self.env['stock.picking'].search([('purchase_id', '=', purchase_order.id)])
self.assertAlmostEqual(invoice_layer.value + picking.move_ids.stock_valuation_layer_ids.value, invoice.line_ids[0].debit)
self.assertAlmostEqual(invoice_layer.value + picking.move_ids.stock_valuation_layer_ids.value, invoice.invoice_line_ids.price_subtotal/2, 2)
self.check_reconciliation(invoice, picking)
def test_rounding_price_unit(self):
self.env.ref("product.decimal_price").digits = 6
test_product = self.test_product_delivery
date_po_and_delivery = '2018-01-01'
purchase_order = self._create_purchase(test_product, date_po_and_delivery, quantity=1000000, price_unit=0.0005)
self._process_pickings(purchase_order.picking_ids, date=date_po_and_delivery)
invoice = self._create_invoice_for_po(purchase_order, '2018-01-01')
# Set a discount
move_form = Form(invoice)
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.price_unit = 0.0006
move_form.save()
invoice.action_post()
# Check the price difference amount. It's expected that price_unit * qty != price_total.
invoice_layer = self.env['stock.valuation.layer'].search([('account_move_line_id', 'in', invoice.line_ids.ids)])
self.assertTrue(len(invoice_layer) == 1, "A price difference line should be created")
# self.assertAlmostEqual(invoice_layer.price_unit, 0.0001)
self.assertAlmostEqual(invoice_layer.value, 50.0)
picking = self.env['stock.picking'].search([('purchase_id', '=', purchase_order.id)])
self.check_reconciliation(invoice, picking)
@freeze_time('2021-01-03')
def test_price_difference_exchange_difference_accounting_date(self):
self.stock_account_product_categ.property_account_creditor_price_difference_categ = self.company_data['default_account_stock_price_diff']
test_product = self.test_product_delivery
test_product.categ_id.write({"property_cost_method": "standard"})
test_product.write({'standard_price': 100.0})
date_po_receipt = '2021-01-02'
rate_po_receipt = 25.0
date_bill = '2021-01-01'
rate_bill = 30.0
date_accounting = '2021-01-03'
rate_accounting = 26.0
foreign_currency = self.other_currency
company_currency = self.env.company.currency_id
self.env['res.currency.rate'].create([
{
'name': date_po_receipt,
'rate': rate_po_receipt,
'currency_id': foreign_currency.id,
'company_id': self.env.company.id,
}, {
'name': date_bill,
'rate': rate_bill,
'currency_id': foreign_currency.id,
'company_id': self.env.company.id,
}, {
'name': date_accounting,
'rate': rate_accounting,
'currency_id': foreign_currency.id,
'company_id': self.env.company.id,
}, {
'name': date_po_receipt,
'rate': 1.0,
'currency_id': company_currency.id,
'company_id': self.env.company.id,
}, {
'name': date_accounting,
'rate': 1.0,
'currency_id': company_currency.id,
'company_id': self.env.company.id,
}, {
'name': date_bill,
'rate': 1.0,
'currency_id': company_currency.id,
'company_id': self.env.company.id,
}])
#purchase order created in foreign currency
purchase_order = self._create_purchase(test_product, date_po_receipt, quantity=10, price_unit=3000)
with freeze_time(date_po_receipt):
self._process_pickings(purchase_order.picking_ids)
invoice = self._create_invoice_for_po(purchase_order, date_bill)
with Form(invoice) as move_form:
move_form.invoice_date = fields.Date.from_string(date_bill)
move_form.date = fields.Date.from_string(date_accounting)
invoice.action_post()
price_diff_line = invoice.line_ids.filtered(lambda l: l.account_id == self.stock_account_product_categ.property_account_creditor_price_difference_categ)
self.assertTrue(len(price_diff_line) == 1, "A price difference line should be created")
self.assertAlmostEqual(price_diff_line.balance, 192.31)
self.assertAlmostEqual(price_diff_line.price_subtotal, 5000.0)
picking = self.env['stock.picking'].search([('purchase_id', '=', purchase_order.id)])
interim_account_id = self.company_data['default_account_stock_in'].id
valuation_line = picking.move_ids.mapped('account_move_ids.line_ids').filtered(lambda x: x.account_id.id == interim_account_id)
self.assertTrue(valuation_line.full_reconcile_id, "The reconciliation should be total at that point.")
def test_reconcile_cash_basis_bill(self):
''' Test the generation of the CABA move after bill payment
'''
self.env.company.tax_exigibility = True
cash_basis_base_account = self.env['account.account'].create({
'code': 'cash.basis.base.account',
'name': 'cash_basis_base_account',
'account_type': 'income',
})
self.company_data['company'].account_cash_basis_base_account_id = cash_basis_base_account
cash_basis_transfer_account = self.env['account.account'].create({
'code': 'cash.basis.transfer.account',
'name': 'cash_basis_transfer_account',
'account_type': 'income',
})
tax_account_1 = self.env['account.account'].create({
'code': 'tax.account.1',
'name': 'tax_account_1',
'account_type': 'income',
})
tax_tags = self.env['account.account.tag'].create({
'name': 'tax_tag_%s' % str(i),
'applicability': 'taxes',
} for i in range(8))
cash_basis_tax_a_third_amount = self.env['account.tax'].create({
'name': 'tax_1',
'amount': 33.3333,
'company_id': self.company_data['company'].id,
'cash_basis_transition_account_id': cash_basis_transfer_account.id,
'type_tax_use': 'purchase',
'tax_exigibility': 'on_payment',
'invoice_repartition_line_ids': [
(0, 0, {
'repartition_type': 'base',
'tag_ids': [(6, 0, tax_tags[0].ids)],
}),
(0, 0, {
'repartition_type': 'tax',
'account_id': tax_account_1.id,
'tag_ids': [(6, 0, tax_tags[1].ids)],
}),
],
'refund_repartition_line_ids': [
(0, 0, {
'repartition_type': 'base',
'tag_ids': [(6, 0, tax_tags[2].ids)],
}),
(0, 0, {
'repartition_type': 'tax',
'account_id': tax_account_1.id,
'tag_ids': [(6, 0, tax_tags[3].ids)],
}),
],
})
product_A = self.env["product.product"].create(
{
"name": "Product A",
"is_storable": True,
"default_code": "prda",
"categ_id": self.stock_account_product_categ.id,
"taxes_id": [(5, 0, 0)],
"supplier_taxes_id": [(6, 0, cash_basis_tax_a_third_amount.ids)],
"lst_price": 100.0,
"standard_price": 10.0,
"property_account_income_id": self.company_data["default_account_revenue"].id,
"property_account_expense_id": self.company_data["default_account_expense"].id,
}
)
product_A.categ_id.write(
{
"property_valuation": "real_time",
"property_cost_method": "standard",
}
)
date_po_and_delivery = '2018-01-01'
purchase_order = self._create_purchase(product_A, date_po_and_delivery, set_tax=True, price_unit=300.0)
self._process_pickings(purchase_order.picking_ids, date=date_po_and_delivery)
bill = self._create_invoice_for_po(purchase_order, '2018-02-02')
bill.action_post()
# Register a payment creating the CABA journal entry on the fly and reconcile it with the tax line.
self.env['account.payment.register']\
.with_context(active_ids=bill.ids, active_model='account.move')\
.create({})\
._create_payments()
partial_rec = bill.mapped('line_ids.matched_debit_ids')
caba_move = self.env['account.move'].search([('tax_cash_basis_rec_id', '=', partial_rec.id)])
# Tax values based on payment
# Invoice amount 300
self.assertRecordValues(caba_move.line_ids, [
# pylint: disable=C0326
# Base amount:
{'debit': 0.0, 'credit': 150.0, 'amount_currency': -300.0, 'account_id': cash_basis_base_account.id},
{'debit': 150.0, 'credit': 0.0, 'amount_currency': 300.0, 'account_id': cash_basis_base_account.id},
# tax:
{'debit': 0.0, 'credit': 50.0, 'amount_currency': -100.0, 'account_id': cash_basis_transfer_account.id},
{'debit': 50.0, 'credit': 0.0, 'amount_currency': 100.0, 'account_id': tax_account_1.id},
])
def test_reconciliation_differed_billing(self):
"""
Test whether products received billed at different time will be correctly reconciled
valuation: automated
- create a rfq
- receive products
- create bill - set quantity of product A = 0 - save
- create bill - confirm
-> the reconciliation should not take into account the lines of the first bill
"""
date_po_and_delivery = '2022-03-02'
self.product_a.write({
'categ_id': self.stock_account_product_categ,
'is_storable': True,
})
self.product_b.write({
'categ_id': self.stock_account_product_categ,
'is_storable': True,
})
purchase_order = self.env['purchase.order'].create({
'currency_id': self.other_currency.id,
'order_line': [
Command.create({
'name': self.product_a.name,
'product_id': self.product_a.id,
'product_qty': 1,
}),
Command.create({
'name': self.product_b.name,
'product_id': self.product_b.id,
'product_qty': 1,
}),
],
'partner_id': self.partner_a.id,
})
purchase_order.button_confirm()
self._process_pickings(purchase_order.picking_ids, date=date_po_and_delivery)
bill_1 = self._create_invoice_for_po(purchase_order, date_po_and_delivery)
move_form = Form(bill_1)
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.quantity = 0
move_form.save()
bill_2 = self._create_invoice_for_po(purchase_order, date=date_po_and_delivery)
bill_2.action_post()
aml = bill_2.line_ids.filtered(lambda line: line.display_type == "product")
pol = purchase_order.order_line
self.assertRecordValues(pol, [{'qty_invoiced': line.qty_received} for line in pol])
self.assertRecordValues(aml, [{'reconciled': True} for line in aml])
def test_create_fifo_vacuum_anglo_saxon_expense_entry(self):
# create purchase
self.product_a.write({
'standard_price': 27.0,
'categ_id': self.stock_account_product_categ,
'is_storable': True,
})
self.stock_account_product_categ['property_cost_method'] = 'average'
#create purchase
date_po_and_delivery = '2018-01-01'
purchase_order = self._create_purchase(self.product_a, date_po_and_delivery, 1, price_unit=27)
# proccess picking
self._process_pickings(purchase_order.picking_ids, date=date_po_and_delivery)
# create return
picking = purchase_order.picking_ids[0]
stock_return_picking_form = Form(self.env['stock.return.picking']
.with_context(active_ids=picking.ids, active_id=picking.ids[0],
active_model='stock.picking'))
stock_return_picking = stock_return_picking_form.save()
stock_return_picking.product_return_moves.write({'quantity': 1000.0})
stock_return_picking_action = stock_return_picking.action_create_returns()
return_pick = self.env['stock.picking'].browse(stock_return_picking_action['res_id'])
return_pick.move_line_ids.write({'quantity': 1000})
return_pick.button_validate()
# create vendor bill
move_form = Form(self.env['account.move'].with_context(default_move_type='in_refund'))
move_form._view['modifiers']['purchase_id']['invisible'] = False
move_form.partner_id = purchase_order.partner_id
move_form.invoice_date = date_po_and_delivery
move_form.purchase_id = purchase_order
with move_form.invoice_line_ids.edit(0) as line_form:
line_form.quantity = 999.0
invoice = move_form.save()
invoice.action_post()
# register payment
self.env['account.payment.register']\
.with_context(active_ids=invoice.ids, active_model='account.move')\
.create({})\
._create_payments()
# create another purchase
purchase_order2 = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
'currency_id': self.env.company.currency_id.id,
'order_line': [
(0, 0, {
'name': self.product_a.name,
'product_id': self.product_a.id,
'product_qty': 1,
'product_uom': self.product_a.uom_po_id.id,
'price_unit': 29,
'date_planned': date_po_and_delivery,
})],
'date_order': date_po_and_delivery,
})
# confirm PO
purchase_order2.button_confirm()
# process pickings
self._process_pickings(purchase_order2.picking_ids, date_po_and_delivery)
picking2 = purchase_order2.picking_ids[0]
self.assertEqual(picking2.state, 'done')
@freeze_time('2000-05-05')
def test_currency_exchange_journal_items(self):
""" Prices modified by discounts and currency exchanges should still yield accurate price
units when calculated by valuation mechanisms.
"""
self.env.company.currency_id = self.env.ref('base.IQD').id
self.test_product_order.standard_price = 500
self.stock_account_product_categ.property_cost_method = 'average'
self.env['res.currency.rate'].create({
'name': '2000-05-05',
'company_rate': .00756,
'currency_id': self.env.ref('base.USD').id,
'company_id': self.env.company.id,
})
purchase_order = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
'currency_id': self.env.ref('base.USD').id,
'order_line': [(0, 0, {
'product_id': self.test_product_order.id,
'product_uom_qty': 13,
'discount': 1,
})],
})
purchase_order.button_confirm()
purchase_order.picking_ids.move_ids.quantity = 13
purchase_order.picking_ids.button_validate()
pre_bill_remaining_value = purchase_order.picking_ids.move_ids.stock_valuation_layer_ids.remaining_value
purchase_order.action_create_invoice()
purchase_order.invoice_ids.invoice_date = '2000-05-05'
purchase_order.invoice_ids.action_post()
post_bill_remaining_value = purchase_order.picking_ids.move_ids.stock_valuation_layer_ids.remaining_value
self.assertEqual(post_bill_remaining_value, pre_bill_remaining_value)
|