-
  Set the company currency as EURO for the sake of repeatibility
-
  !record {model: res.company, id: base.main_company}:
    currency_id: base.EUR
-
  Set a product as using fifo price
-
  !record {model: product.product, id: product_fifo_icecream}:
    default_code: FIFO
    name: FIFO Ice Cream
    type: product
    categ_id: product.product_category_1
    list_price: 100.0
    standard_price: 70.0
    uom_id: product.product_uom_kgm
    uom_po_id: product.product_uom_kgm
    cost_method: real
    valuation: real_time
    property_stock_account_input: o_expense
    property_stock_account_output: o_income
    description: FIFO Ice Cream can be mass-produced and thus is widely available in developed parts of the world. Ice cream can be purchased in large cartons (vats and squrounds) from supermarkets and grocery stores, in smaller quantities from ice cream shops, convenience stores, and milk bars, and in individual servings from small carts or vans at public events.
- 
  I create a draft Purchase Order for first in move for 10 kg at 50 euro
- 
  !record {model: purchase.order, id: purchase_order_fifo1}:
    partner_id: base.res_partner_3
    currency_id: base.EUR
    order_line:
      - product_id: product_fifo_icecream
        product_qty: 10.0
        product_uom: product.product_uom_kgm
        price_unit: 50.0
        name: 'FIFO Ice Cream'
        date_planned: !eval time.strftime('%Y-%m-%d')
- 
  I confirm the first purchase order
-
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_fifo1")])
- 
  I check the "Purchase" status of purchase order 1
-
  !assert {model: purchase.order, id: purchase_order_fifo1}:
    - state == 'purchase'
- 
  Process the reception of purchase order 1 and set date
-
  !python {model: stock.picking}: |
    picking_obj = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_fifo1"), context=context).picking_ids[0]
    picking_obj.do_transfer()
- 
  Check the standard price of the product (fifo icecream), that should have not changed because the standard price is supposed to be updated only when goods are going out of the stock
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_fifo_icecream")).standard_price == 70.0, 'Standard price should not have changed!'
- 
  I create a draft Purchase Order for second shipment for 30 kg at 80 euro
- 
  !record {model: purchase.order, id: purchase_order_fifo2}:
    partner_id: base.res_partner_3
    order_line:
      - product_id: product_fifo_icecream
        product_qty: 30.0
        product_uom: product.product_uom_kgm
        price_unit: 80.0
        name: 'FIFO Ice Cream'
        date_planned: !eval time.strftime('%Y-%m-%d')
- 
  I confirm the second purchase order
- 
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_fifo2")])
- 
  Process the reception of purchase order 2
- 
  !python {model: stock.picking}: |
    picking_obj = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_fifo2"), context=context).picking_ids[0]
    picking_obj.do_transfer()
- 
  Check the standard price of the product, that should have not changed because the standard price is supposed to be updated only when goods are going out of the stock
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_fifo_icecream")).standard_price == 70.0, 'Standard price as fifo price of second reception incorrect!'
- 
  Let us send some goods
- 
  !record {model: stock.picking, id: outgoing_fifo_shipment}:
    picking_type_id: stock.picking_type_out
-
  Picking needs movement from stock
-
  !record {model: stock.move, id: outgoing_shipment_fifo_icecream}:
    picking_id: outgoing_fifo_shipment
    product_id: product_fifo_icecream
    product_uom: product.product_uom_kgm
    product_uom_qty: 20.0
    location_id: stock.stock_location_stock
    location_dest_id: stock.stock_location_customers
    picking_type_id: stock.picking_type_out
- 
  I assign this outgoing shipment 
- 
  !python {model: stock.picking}: |
    self.action_assign(cr, uid, [ref("outgoing_fifo_shipment")])
- 
  Process the delivery of the outgoing shipment
- 
  !python {model: stock.picking}: |
     pick_obj = self.pool.get('stock.picking').browse(cr, uid, ref("outgoing_fifo_shipment"), context=context)
     pick_obj.do_transfer()
- 
  Check product standard price changed to 65.0 (because last outgoing shipment was made of 10 kg at 50€ and 10 kg at 80€)
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_fifo_icecream")).standard_price == 65.0, "Product price not updated accordingly. %s found instead of 65" %(self.browse(cr, uid, ref("product_fifo_icecream")).standard_price,)
- 
  Do a delivery of an extra 500 g (delivery order)
- 
  !record {model: stock.picking, id: outgoing_fifo_shipment_uom}:
    picking_type_id: stock.picking_type_out
-
  Picking needs movement from stock
-
  !record {model: stock.move, id: outgoing_shipment_fifo_icecream_gram}:
    picking_id: outgoing_fifo_shipment_uom
    product_id: product_fifo_icecream
    product_uom: product.product_uom_gram
    location_id: stock.stock_location_stock
    location_dest_id: stock.stock_location_customers
    product_uom_qty: 500.0
    picking_type_id: stock.picking_type_out
- 
  I assign this outgoing shipment 
- 
  !python {model: stock.picking}: |
    self.action_assign(cr, uid, [ref("outgoing_fifo_shipment_uom")])
-
  Process the delivery of the outgoing shipment
- 
  !python {model: stock.picking}: |
    pick_obj = self.pool.get('stock.picking').browse(cr, uid, ref("outgoing_fifo_shipment_uom"), context=context)
    pick_obj.do_transfer()
- 
  Check product price changed to 80.0 (because last outgoing shipment was made of 0.5 kg at 80€)
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_fifo_icecream")).standard_price == 80.0, "Product price not updated accordingly. %s found instead of 80" %(self.browse(cr, uid, ref("product_fifo_icecream")).standard_price,)
- 
  We will temporarily change the currency rate on the sixth of June to have the same results all year
- 
  Purchase order in USD
-
  !record {model: res.currency, id: new_USD}:
    name: new_usd
    rate_ids:
      - rate: 1.2834
        name: !eval time.strftime('%Y-%m-%d')
- 
  I create a draft Purchase Order for 30000 g at 0.150 USD/g and 10 kg at 150 USD/kg
- 
  !record {model: purchase.order, id: purchase_order_fifo_usd}:
    partner_id: base.res_partner_3
    currency_id: new_USD
    order_line:
      - product_id: product_fifo_icecream
        product_qty: 30000
        product_uom: product.product_uom_gram
        price_unit: 0.150
        name: 'FIFO Ice Cream'
        date_planned: !eval time.strftime('%Y-%m-%d')
      - product_id: product_fifo_icecream
        product_qty: 10.0
        product_uom: product.product_uom_kgm
        price_unit: 150.0
        name: 'FIFO Ice Cream'
        date_planned: !eval time.strftime('%Y-%m-%d')
- 
  I confirm the purchase order in USD
-
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_fifo_usd")])
- 
  Process the reception of purchase order with usd
-
  !python {model: stock.picking}: |
    pick_obj = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_fifo_usd"), context=context).picking_ids[0]
    pick_obj.do_transfer()
- 
  We create delivery order of 49.5 kg
- 
  !record {model: stock.picking, id: outgoing_fifo_shipment_cur}:
    picking_type_id: stock.picking_type_out
-
  Picking needs movement from stock
-
  !record {model: stock.move, id: outgoing_shipment_fifo_icecream_cur}:
    picking_id: outgoing_fifo_shipment_cur
    product_id: product_fifo_icecream
    product_uom: product.product_uom_kgm
    product_uom_qty: 49.5
    location_id: stock.stock_location_stock
    location_dest_id: stock.stock_location_customers
    picking_type_id: stock.picking_type_out
- 
  I assign this outgoing shipment 
- 
  !python {model: stock.picking}: |
    self.action_assign(cr, uid, [ref("outgoing_fifo_shipment_cur")])
-
  Process the delivery of the outgoing shipment
- 
  !python {model: stock.picking}: |
    picking_obj = self.pool.get('stock.picking').browse(cr, uid, ref("outgoing_fifo_shipment_cur"), context=context)
    picking_obj.do_transfer()
- 
 Check rounded price is 102 euro (because last outgoing shipment was made of 19.5kg at 80€ and 30kg at $150 (rate=1.2834)
- 
  !python {model: product.product}: |
    assert round(self.browse(cr, uid, ref("product_fifo_icecream")).standard_price) == 102, "Product price not updated accordingly. %s found instead of 102 (rounded values)" %(round(self.browse(cr, uid, ref("product_fifo_icecream")).standard_price),)
-
  Do a delivery of an extra 10 kg
- 
  !record {model: stock.picking, id: outgoing_fifo_shipment_ret}:
    picking_type_id: stock.picking_type_out
- 
  Picking needs movement from stock
-
  !record {model: stock.move, id: outgoing_shipment_fifo_icecream_ret}:
    picking_id: outgoing_fifo_shipment_ret
    product_id: product_fifo_icecream
    product_uom: product.product_uom_kgm
    product_uom_qty: 10.0
    location_id: stock.stock_location_stock
    location_dest_id: stock.stock_location_customers
    picking_type_id: stock.picking_type_out
- 
  I assign this outgoing shipment
- 
  !python {model: stock.picking}: |
    self.action_assign(cr, uid, [ref("outgoing_fifo_shipment_ret")])
- 
  Process the delivery of the outgoing shipment
- 
  !python {model: stock.picking}: |
    picking_obj = self.pool.get('stock.picking').browse(cr, uid, ref("outgoing_fifo_shipment_ret"), context=context)
    picking_obj.do_transfer()
- 
  Check rounded price is 150.0 / 1.2834
- 
  !python {model: product.product}: |
    product = self.browse(cr, uid, ref("product_fifo_icecream"))
    assert round(product.standard_price) == round(150.0 / 1.2834), "Product price not updated accordingly. %s found instead of %s" %(product.standard_price, round(150.0/1.2834))
    assert product.qty_available == 0.0, 'Wrong quantity in stock after first reception'
-
  Let us create some outs to get negative stock for a new product using the same config
-
  !record {model: product.product, id: product_fifo_negative}:
    default_code: NEG
    name: FIFO Negative
    type: product
    categ_id: product.product_category_1
    list_price: 100.0
    standard_price: 70.0
    uom_id: product.product_uom_kgm
    uom_po_id: product.product_uom_kgm
    cost_method: real
    valuation: real_time
    property_stock_account_input: o_expense
    property_stock_account_output: o_income
    description: 
- 
  Create outpicking.  We create delivery order of 100 kg.
- 
  !record {model: stock.picking, id: outgoing_fifo_shipment_neg}:
    picking_type_id: stock.picking_type_out
-
  Picking needs movement from stock
-
  !record {model: stock.move, id: outgoing_shipment_fifo_icecream_neg}:
    picking_id: outgoing_fifo_shipment_neg
    product_id: product_fifo_negative
    product_uom: product.product_uom_kgm
    product_uom_qty: 100.0
    location_id: stock.stock_location_stock
    location_dest_id: stock.stock_location_customers
    picking_type_id: stock.picking_type_out
- 
  Process the delivery of the first outgoing shipment
- 
  !python {model: stock.picking}: |
    picking_obj = self.browse(cr, uid, ref("outgoing_fifo_shipment_neg"), context=context)
    picking_obj.do_transfer()
-
  The behavior of fifo/lifo is not garantee if the quants are created at the same second, so i just wait one second
-
  !python {model: stock.picking}: |
    import time
    time.sleep(1)
-
  Let us create another out of 400 kg
- 
  !record {model: stock.picking, id: outgoing_fifo_shipment_neg2}:
    picking_type_id: stock.picking_type_out
-
  Picking needs movement from stock
-
  !record {model: stock.move, id: outgoing_shipment_fifo_icecream_neg2}:
    picking_id: outgoing_fifo_shipment_neg2
    product_id: product_fifo_negative
    product_uom: product.product_uom_kgm
    product_uom_qty: 400.0
    location_id: stock.stock_location_stock
    location_dest_id: stock.stock_location_customers
    picking_type_id: stock.picking_type_out
- 
  Process the delivery of the outgoing shipments
- 
  !python {model: stock.picking}: |
    picking_obj1 = self.browse(cr, uid, ref("outgoing_fifo_shipment_neg2"), context=context)
    picking_obj1.do_transfer()

- 
  Receive purchase order with 50 kg FIFO Ice Cream at 50 euro/kg
- 
  !record {model: purchase.order, id: purchase_order_fifo_neg}:
    partner_id: base.res_partner_3
    order_line:
      - product_id: product_fifo_negative
        product_qty: 50.0
        product_uom: product.product_uom_kgm
        price_unit: 50.0
        name: 'FIFO Ice Cream'
        date_planned: !eval time.strftime('%Y-%m-%d')
- 
  I confirm the first purchase order
-
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_fifo_neg")])
- 
  Process the reception of purchase order 1
-
  !python {model: stock.picking}: |
    picking_obj = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_fifo_neg"), context=context).picking_ids[0]
    picking_obj.do_transfer()
- 
  Assert price on product is still the old price as the out move has not been received fully yet
- 
  !python {model: product.product}: |

    assert self.browse(cr, uid, ref("product_fifo_negative")).standard_price == 70, 'The product price should not have been updated'
- 
  Receive purchase order with 60 kg FIFO Ice Cream at 80 euro/kg
- 
  !record {model: purchase.order, id: purchase_order_fifo_neg2}:
    partner_id: base.res_partner_3
    order_line:
      - product_id: product_fifo_negative
        product_qty: 60.0
        product_uom: product.product_uom_kgm
        price_unit: 80
        name: 'FIFO Ice Cream'
        date_planned: !eval time.strftime('%Y-%m-%d')
- 
  I confirm the second negative purchase order
- 
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_fifo_neg2")])
- 
  Process the reception of purchase order 2
- 
  !python {model: stock.picking}: |
    picking_obj = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_fifo_neg2"), context=context).picking_ids[0]
    picking_obj.do_transfer()
- 
  The price of the product should have changed back to 65.0
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_fifo_negative")).standard_price == 65.0, "Product price not updated accordingly. %s found instead of 65" %(self.browse(cr, uid, ref("product_fifo_negative")).standard_price,)
