test_fifo_price.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon
  4. from odoo.tests import tagged, Form
  5. import time
  6. @tagged('-at_install', 'post_install')
  7. class TestFifoPrice(ValuationReconciliationTestCommon):
  8. def test_00_test_fifo(self):
  9. """ Test product cost price with fifo removal strategy."""
  10. res_partner_3 = self.env['res.partner'].create({
  11. 'name': 'Gemini Partner',
  12. })
  13. # Set a product as using fifo price
  14. product_cable_management_box = self.env['product.product'].create({
  15. 'default_code': 'FIFO',
  16. 'name': 'FIFO Ice Cream',
  17. 'type': 'product',
  18. 'categ_id': self.stock_account_product_categ.id,
  19. 'list_price': 100.0,
  20. 'standard_price': 70.0,
  21. 'uom_id': self.env.ref('uom.product_uom_kgm').id,
  22. 'uom_po_id': self.env.ref('uom.product_uom_kgm').id,
  23. 'supplier_taxes_id': [],
  24. 'description': 'FIFO Ice Cream',
  25. })
  26. # I create a draft Purchase Order for first in move for 10 kg at 50 euro
  27. purchase_order_1 = self.env['purchase.order'].create({
  28. 'partner_id': res_partner_3.id,
  29. 'order_line': [(0, 0, {
  30. 'name': 'FIFO Ice Cream',
  31. 'product_id': product_cable_management_box.id,
  32. 'product_qty': 10.0,
  33. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  34. 'price_unit': 50.0,
  35. 'date_planned': time.strftime('%Y-%m-%d')})],
  36. })
  37. # Confirm the first purchase order
  38. purchase_order_1.button_confirm()
  39. # Check the "Purchase" status of purchase order 1
  40. self.assertEqual(purchase_order_1.state, 'purchase')
  41. # Process the reception of purchase order 1 and set date
  42. picking = purchase_order_1.picking_ids[0]
  43. res = picking.button_validate()
  44. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  45. # Check the standard price of the product (fifo icecream), that should have changed
  46. # because the unit cost of the purchase order is 50
  47. self.assertAlmostEqual(product_cable_management_box.standard_price, 50.0)
  48. self.assertEqual(product_cable_management_box.value_svl, 500.0, 'Wrong stock value')
  49. # I create a draft Purchase Order for second shipment for 30 kg at 80 euro
  50. purchase_order_2 = self.env['purchase.order'].create({
  51. 'partner_id': res_partner_3.id,
  52. 'order_line': [(0, 0, {
  53. 'name': 'FIFO Ice Cream',
  54. 'product_id': product_cable_management_box.id,
  55. 'product_qty': 30.0,
  56. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  57. 'price_unit': 80.0,
  58. 'date_planned': time.strftime('%Y-%m-%d')})],
  59. })
  60. # Confirm the second purchase order
  61. purchase_order_2.button_confirm()
  62. # Process the reception of purchase order 2
  63. picking = purchase_order_2.picking_ids[0]
  64. res = picking.button_validate()
  65. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  66. # Check the standard price of the product, that should have not changed because we
  67. # still have icecream in stock
  68. self.assertEqual(product_cable_management_box.standard_price, 50.0, 'Standard price as fifo price of second reception incorrect!')
  69. self.assertEqual(product_cable_management_box.value_svl, 2900.0, 'Stock valuation should be 2900')
  70. # Let us send some goods
  71. outgoing_shipment = self.env['stock.picking'].create({
  72. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
  73. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  74. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  75. 'move_ids': [(0, 0, {
  76. 'name': product_cable_management_box.name,
  77. 'product_id': product_cable_management_box.id,
  78. 'product_uom_qty': 20.0,
  79. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  80. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  81. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  82. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id})]
  83. })
  84. # I assign this outgoing shipment
  85. outgoing_shipment.action_assign()
  86. # Process the delivery of the outgoing shipment
  87. res = outgoing_shipment.button_validate()
  88. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  89. # Check stock value became 1600 .
  90. self.assertEqual(product_cable_management_box.value_svl, 1600.0, 'Stock valuation should be 1600')
  91. # Do a delivery of an extra 500 g (delivery order)
  92. outgoing_shipment_uom = self.env['stock.picking'].create({
  93. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
  94. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  95. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  96. 'move_ids': [(0, 0, {
  97. 'name': product_cable_management_box.name,
  98. 'product_id': product_cable_management_box.id,
  99. 'product_uom_qty': 500.0,
  100. 'product_uom': self.env.ref('uom.product_uom_gram').id,
  101. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  102. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  103. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id})]
  104. })
  105. # I assign this outgoing shipment
  106. outgoing_shipment_uom.action_assign()
  107. # Process the delivery of the outgoing shipment
  108. res = outgoing_shipment_uom.button_validate()
  109. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  110. # Check stock valuation and qty in stock
  111. self.assertEqual(product_cable_management_box.value_svl, 1560.0, 'Stock valuation should be 1560')
  112. self.assertEqual(product_cable_management_box.qty_available, 19.5, 'Should still have 19.5 in stock')
  113. # We will temporarily change the currency rate on the sixth of June to have the same results all year
  114. NewUSD = self.env['res.currency'].create({
  115. 'name': 'new_usd',
  116. 'symbol': '$²',
  117. 'rate_ids': [(0, 0, {'rate': 1.2834, 'name': time.strftime('%Y-%m-%d')})],
  118. })
  119. # Create PO for 30000 g at 0.150$/g and 10 kg at 150$/kg
  120. purchase_order_usd = self.env['purchase.order'].create({
  121. 'partner_id': res_partner_3.id,
  122. 'currency_id': NewUSD.id,
  123. 'order_line': [(0, 0, {
  124. 'name': 'FIFO Ice Cream',
  125. 'product_id': product_cable_management_box.id,
  126. 'product_qty': 30,
  127. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  128. 'price_unit': 0.150,
  129. 'date_planned': time.strftime('%Y-%m-%d')}),
  130. (0, 0, {
  131. 'name': product_cable_management_box.name,
  132. 'product_id': product_cable_management_box.id,
  133. 'product_qty': 10.0,
  134. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  135. 'price_unit': 150.0,
  136. 'date_planned': time.strftime('%Y-%m-%d')})]
  137. })
  138. # Confirm the purchase order in USD
  139. purchase_order_usd.button_confirm()
  140. # Process the reception of purchase order with USD
  141. picking = purchase_order_usd.picking_ids[0]
  142. res = picking.button_validate()
  143. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  144. # Create delivery order of 49.5 kg
  145. outgoing_shipment_cur = self.env['stock.picking'].create({
  146. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
  147. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  148. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  149. 'move_ids': [(0, 0, {
  150. 'name': product_cable_management_box.name,
  151. 'product_id': product_cable_management_box.id,
  152. 'product_uom_qty': 49.5,
  153. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  154. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  155. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  156. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id})]
  157. })
  158. # I assign this outgoing shipment
  159. outgoing_shipment_cur.action_assign()
  160. # Process the delivery of the outgoing shipment
  161. res = outgoing_shipment_cur.button_validate()
  162. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  163. # Do a delivery of an extra 10 kg
  164. outgoing_shipment_ret = self.env['stock.picking'].create({
  165. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
  166. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  167. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  168. 'move_ids': [(0, 0, {
  169. 'name': product_cable_management_box.name,
  170. 'product_id': product_cable_management_box.id,
  171. 'product_uom_qty': 10,
  172. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  173. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  174. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  175. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id})]
  176. })
  177. # I assign this outgoing shipment
  178. outgoing_shipment_ret.action_assign()
  179. res = outgoing_shipment_ret.button_validate()
  180. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  181. # Check rounded price is 150.0 / 1.2834
  182. self.assertEqual(round(product_cable_management_box.qty_available), 0.0, 'Wrong quantity in stock after first reception.')
  183. # Let us create some outs to get negative stock for a new product using the same config
  184. product_fifo_negative = self.env['product.product'].create({
  185. 'default_code': 'NEG',
  186. 'name': 'FIFO Negative',
  187. 'type': 'product',
  188. 'categ_id': self.stock_account_product_categ.id,
  189. 'list_price': 100.0,
  190. 'standard_price': 70.0,
  191. 'uom_id': self.env.ref('uom.product_uom_kgm').id,
  192. 'uom_po_id': self.env.ref('uom.product_uom_kgm').id,
  193. 'supplier_taxes_id': [],
  194. 'description': 'FIFO Ice Cream',
  195. })
  196. # Create outpicking.create delivery order of 100 kg.
  197. outgoing_shipment_neg = self.env['stock.picking'].create({
  198. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
  199. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  200. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  201. 'move_ids': [(0, 0, {
  202. 'name': product_fifo_negative.name,
  203. 'product_id': product_fifo_negative.id,
  204. 'product_uom_qty': 100,
  205. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  206. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  207. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  208. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id})]
  209. })
  210. # Process the delivery of the first outgoing shipment
  211. outgoing_shipment_neg.action_confirm()
  212. outgoing_shipment_neg.move_ids[0].quantity_done = 100.0
  213. outgoing_shipment_neg._action_done()
  214. # Check qty available = -100
  215. self.assertEqual(product_fifo_negative.qty_available, -100, 'Stock qty should be -100')
  216. # The behavior of fifo/lifo is not garantee if the quants are created at the same second, so just wait one second
  217. time.sleep(1)
  218. # Let create another out shipment of 400 kg
  219. outgoing_shipment_neg2 = self.env['stock.picking'].create({
  220. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
  221. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  222. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  223. 'move_ids': [(0, 0, {
  224. 'name': product_fifo_negative.name,
  225. 'product_id': product_fifo_negative.id,
  226. 'product_uom_qty': 400,
  227. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  228. 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
  229. 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
  230. 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id})]
  231. })
  232. # Process the delivery of the outgoing shipments
  233. outgoing_shipment_neg2.action_confirm()
  234. outgoing_shipment_neg2.move_ids[0].quantity_done = 400.0
  235. outgoing_shipment_neg2._action_done()
  236. # Check qty available = -500
  237. self.assertEqual(product_fifo_negative.qty_available, -500, 'Stock qty should be -500')
  238. # Receive purchase order with 50 kg Ice Cream at 50€/kg
  239. purchase_order_neg = self.env['purchase.order'].create({
  240. 'partner_id': res_partner_3.id,
  241. 'order_line': [(0, 0, {
  242. 'name': 'FIFO Ice Cream',
  243. 'product_id': product_fifo_negative.id,
  244. 'product_qty': 50.0,
  245. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  246. 'price_unit': 50.0,
  247. 'date_planned': time.strftime('%Y-%m-%d')})],
  248. })
  249. # I confirm the first purchase order
  250. purchase_order_neg.button_confirm()
  251. # Process the reception of purchase order neg
  252. picking = purchase_order_neg.picking_ids[0]
  253. res = picking.button_validate()
  254. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  255. # Receive purchase order with 600 kg FIFO Ice Cream at 80 euro/kg
  256. purchase_order_neg2 = self.env['purchase.order'].create({
  257. 'partner_id': res_partner_3.id,
  258. 'order_line': [(0, 0, {
  259. 'name': product_cable_management_box.name,
  260. 'product_id': product_fifo_negative.id,
  261. 'product_qty': 600.0,
  262. 'product_uom': self.env.ref('uom.product_uom_kgm').id,
  263. 'price_unit': 80.0,
  264. 'date_planned': time.strftime('%Y-%m-%d')})],
  265. })
  266. # I confirm the second negative purchase order
  267. purchase_order_neg2.button_confirm()
  268. # Process the reception of purchase order neg2
  269. picking = purchase_order_neg2.picking_ids[0]
  270. res = picking.button_validate()
  271. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  272. original_out_move = outgoing_shipment_neg.move_ids[0]
  273. self.assertEqual(original_out_move.product_id.value_svl, 12000.0, 'Value of the move should be 12000')
  274. self.assertEqual(original_out_move.product_id.qty_available, 150.0, 'Qty available should be 150')
  275. def test_01_test_fifo(self):
  276. """" This test ensures that unit price keeps its decimal precision """
  277. unit_price_precision = self.env.ref('product.decimal_price')
  278. unit_price_precision.digits = 3
  279. tax = self.env["account.tax"].create({
  280. "name": "Dummy Tax",
  281. "amount": "0.00",
  282. "type_tax_use": "purchase",
  283. })
  284. super_product = self.env['product.product'].create({
  285. 'name': 'Super Product',
  286. 'type': 'product',
  287. 'categ_id': self.stock_account_product_categ.id,
  288. 'standard_price': 0.035,
  289. })
  290. self.assertEqual(super_product.cost_method, 'fifo')
  291. self.assertEqual(super_product.valuation, 'real_time')
  292. purchase_order = self.env['purchase.order'].create({
  293. 'partner_id': self.env.ref('base.res_partner_3').id,
  294. 'order_line': [(0, 0, {
  295. 'name': super_product.name,
  296. 'product_id': super_product.id,
  297. 'product_qty': 1000,
  298. 'product_uom': super_product.uom_id.id,
  299. 'price_unit': super_product.standard_price,
  300. 'date_planned': time.strftime('%Y-%m-%d'),
  301. 'taxes_id': [(4, tax.id)],
  302. })],
  303. })
  304. purchase_order.button_confirm()
  305. self.assertEqual(purchase_order.state, 'purchase')
  306. picking = purchase_order.picking_ids[0]
  307. res = picking.button_validate()
  308. Form(self.env[res['res_model']].with_context(res['context'])).save().process()
  309. self.assertEqual(super_product.standard_price, 0.035)
  310. self.assertEqual(super_product.value_svl, 35.0)
  311. self.assertEqual(picking.move_ids.price_unit, 0.035)