test_purchase_stock_accrued_entries.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. from odoo import fields, Command
  3. from odoo.addons.account.tests.common import AccountTestInvoicingCommon
  4. from odoo.tests import tagged, Form
  5. from odoo.exceptions import UserError
  6. @tagged('post_install', '-at_install')
  7. class TestAccruedPurchaseStock(AccountTestInvoicingCommon):
  8. @classmethod
  9. def setUpClass(cls, chart_template_ref=None):
  10. super().setUpClass(chart_template_ref=chart_template_ref)
  11. uom_unit = cls.env.ref('uom.product_uom_unit')
  12. product = cls.env['product.product'].create({
  13. 'name': "Product",
  14. 'list_price': 30.0,
  15. 'type': 'consu',
  16. 'uom_id': uom_unit.id,
  17. 'uom_po_id': uom_unit.id,
  18. })
  19. cls.purchase_order = cls.env['purchase.order'].with_context(tracking_disable=True).create({
  20. 'partner_id': cls.partner_a.id,
  21. 'order_line': [
  22. Command.create({
  23. 'name': product.name,
  24. 'product_id': product.id,
  25. 'product_qty': 10.0,
  26. 'product_uom': product.uom_id.id,
  27. 'price_unit': product.list_price,
  28. 'taxes_id': False,
  29. }),
  30. ]
  31. })
  32. cls.purchase_order.button_confirm()
  33. cls.account_expense = cls.company_data['default_account_expense']
  34. cls.account_revenue = cls.company_data['default_account_revenue']
  35. def test_purchase_stock_accruals(self):
  36. # receive 2 on 2020-01-02
  37. pick = self.purchase_order.picking_ids
  38. pick.move_ids.write({'quantity_done': 2})
  39. pick.button_validate()
  40. wiz_act = pick.button_validate()
  41. wiz = Form(self.env[wiz_act['res_model']].with_context(wiz_act['context'])).save()
  42. wiz.process()
  43. pick.move_ids.write({'date': fields.Date.to_date('2020-01-02')})
  44. # receive 3 on 2020-01-06
  45. pick = pick.copy()
  46. pick.move_ids.write({'quantity_done': 3})
  47. wiz_act = pick.button_validate()
  48. pick.move_ids.write({'date': fields.Date.to_date('2020-01-06')})
  49. wizard = self.env['account.accrued.orders.wizard'].with_context({
  50. 'active_model': 'purchase.order',
  51. 'active_ids': self.purchase_order.ids,
  52. }).create({
  53. 'account_id': self.account_expense.id,
  54. 'date': '2020-01-01',
  55. })
  56. # nothing to invoice on 2020-01-01
  57. with self.assertRaises(UserError):
  58. wizard.create_entries()
  59. # 2 to invoice on 2020-01-04
  60. wizard.date = fields.Date.to_date('2020-01-04')
  61. self.assertRecordValues(self.env['account.move'].search(wizard.create_entries()['domain']).line_ids, [
  62. # reverse move lines
  63. {'account_id': self.account_expense.id, 'debit': 0, 'credit': 60},
  64. {'account_id': wizard.account_id.id, 'debit': 60, 'credit': 0},
  65. # move lines
  66. {'account_id': self.account_expense.id, 'debit': 60, 'credit': 0},
  67. {'account_id': wizard.account_id.id, 'debit': 0, 'credit': 60},
  68. ])
  69. # 5 to invoice on 2020-01-07
  70. wizard.date = fields.Date.to_date('2020-01-07')
  71. self.assertRecordValues(self.env['account.move'].search(wizard.create_entries()['domain']).line_ids, [
  72. # reverse move lines
  73. {'account_id': self.account_expense.id, 'debit': 0, 'credit': 150},
  74. {'account_id': wizard.account_id.id, 'debit': 150, 'credit': 0},
  75. # move lines
  76. {'account_id': self.account_expense.id, 'debit': 150, 'credit': 0},
  77. {'account_id': wizard.account_id.id, 'debit': 0, 'credit': 150},
  78. ])
  79. def test_purchase_stock_invoiced_accrued_entries(self):
  80. # deliver 2 on 2020-01-02
  81. pick = self.purchase_order.picking_ids
  82. pick.move_ids.write({'quantity_done': 2})
  83. pick.button_validate()
  84. wiz_act = pick.button_validate()
  85. wiz = Form(self.env[wiz_act['res_model']].with_context(wiz_act['context'])).save()
  86. wiz.process()
  87. pick.move_ids.write({'date': fields.Date.to_date('2020-01-02')})
  88. # invoice on 2020-01-04
  89. move = self.env['account.move'].browse(self.purchase_order.action_create_invoice()['res_id'])
  90. move.invoice_date = fields.Date.to_date('2020-01-04')
  91. move.action_post()
  92. # deliver 3 on 2020-01-06
  93. pick = pick.copy()
  94. pick.move_ids.write({'quantity_done': 3})
  95. wiz_act = pick.button_validate()
  96. pick.move_ids.write({'date': fields.Date.to_date('2020-01-06')})
  97. # invoice on 2020-01-08
  98. move = self.env['account.move'].browse(self.purchase_order.action_create_invoice()['res_id'])
  99. move.invoice_date = fields.Date.to_date('2020-01-08')
  100. move.action_post()
  101. wizard = self.env['account.accrued.orders.wizard'].with_context({
  102. 'active_model': 'purchase.order',
  103. 'active_ids': self.purchase_order.ids,
  104. }).create({
  105. 'account_id': self.company_data['default_account_expense'].id,
  106. 'date': '2020-01-02',
  107. })
  108. # 2 to invoice on 2020-01-07
  109. self.assertRecordValues(self.env['account.move'].search(wizard.create_entries()['domain']).line_ids, [
  110. # reverse move lines
  111. {'account_id': self.account_expense.id, 'debit': 0, 'credit': 60},
  112. {'account_id': wizard.account_id.id, 'debit': 60, 'credit': 0},
  113. # move lines
  114. {'account_id': self.account_expense.id, 'debit': 60, 'credit': 0},
  115. {'account_id': wizard.account_id.id, 'debit': 0, 'credit': 60},
  116. ])
  117. # nothing to invoice on 2020-01-05
  118. wizard.date = fields.Date.to_date('2020-01-05')
  119. with self.assertRaises(UserError):
  120. wizard.create_entries()
  121. # 3 to invoice on 2020-01-07
  122. wizard.date = fields.Date.to_date('2020-01-07')
  123. self.assertRecordValues(self.env['account.move'].search(wizard.create_entries()['domain']).line_ids, [
  124. # reverse move lines
  125. {'account_id': self.account_expense.id, 'debit': 0, 'credit': 90},
  126. {'account_id': wizard.account_id.id, 'debit': 90, 'credit': 0},
  127. # move lines
  128. {'account_id': self.account_expense.id, 'debit': 90, 'credit': 0},
  129. {'account_id': wizard.account_id.id, 'debit': 0, 'credit': 90},
  130. ])
  131. # nothing to invoice on 2020-01-09
  132. wizard.date = fields.Date.to_date('2020-01-09')
  133. with self.assertRaises(UserError):
  134. wizard.create_entries()