-
  Set a product as using average price. 
-
  !record {model: product.product, id: product_variable_icecream}:
    default_code: VARAVG
    name: Variable Ice Cream
    type: product
    categ_id: product.product_category_1
    uom_id: product.product_uom_kgm
    uom_po_id: product.product_uom_kgm
    procure_method: make_to_stock
    valuation: real_time
    cost_method: average
    property_stock_account_input: account.o_expense
    property_stock_account_output: account.o_income
    description: Average 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 incoming shipment for 10 pieces at 60€
- 
  !record {model: purchase.order, id: purchase_order_variable1}:
    partner_id: base.res_partner_3
    order_line:
      - product_id: product_variable_icecream
        product_qty: 10.0
        product_uom: product.product_uom_categ_kgm
        price_unit: 60.0
        name: 'Average Ice Cream'
- 
  I create a draft Purchase Order for second incoming shipment for 30 pieces at 80€
- 
  !record {model: purchase.order, id: purchase_order_variable2}:
    partner_id: base.res_partner_3
    order_line:
      - product_id: product_variable_icecream
        product_qty: 30.0
        product_uom: product.product_uom_categ_kgm
        price_unit: 80.0
        name: 'Average Ice Cream'
- 
  I confirm the first purchase order
-
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_variable1")])
- 
  I confirm the second purchase order
- 
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_variable2")])
-
  I check the "Approved" status of purchase order 1
-
  !assert {model: purchase.order, id: purchase_order_variable1}:
    - state == 'purchase'
- 
  Process the reception of purchase order 1
-
  !python {model: stock.partial.picking}: |
    pick_ids = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_variable1")).picking_ids
    partial_id = self.create(cr, uid, {}, context={'active_model': 'stock.picking','active_ids': [pick_ids[0].id]})
    self.do_transfer(cr, uid, [partial_id])
- 
  Check the standard price of the product (variable icecream). 
- 
  !python {model: product.product}: |
    product = self.browse(cr, uid, ref("product_variable_icecream"))
    assert product.qty_available == 10.0, 'Wrong quantity in stock after first reception'
    assert product.standard_price == 60.0, 'Standard price should be the price of the first reception!'
- 
  Process the reception of purchase order 2
- 
  !python {model: stock.partial.picking}: |
    pick_ids = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_variable2")).picking_ids
    partial_id = self.create(cr, uid, {}, context={'active_model': 'stock.picking','active_ids': [pick_ids[0].id]})
    self.do_transfer(cr, uid, [partial_id])
- 
  Check the standard price and change the product's cost method towards fifo
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_variable_icecream")).standard_price == 75.0, 'After second reception, we should have an average price of 75.0 on the product'
    self.write(cr, uid, [ref("product_variable_icecream")], {'cost_method': 'real'}, context=context)
- 
  Create picking to send some goods
- 
  !record {model: stock.picking, id: outgoing_variable_shipment}:
    type: out
- 
  Create move for picking
- 
  !record {model: stock.move, id: outgoing_shipment_variable_icecream}:
    picking_id: outgoing_variable_shipment
    product_id: product_variable_icecream
    product_uom: product.product_uom_kgm
    product_qty: 20.0
    type: out
- 
  I confirm outgoing shipment of 20 kg of Variable Ice Cream. 
- 
  !workflow {model: stock.picking, action: button_confirm, ref: outgoing_variable_shipment}
- 
  Process the delivery of the outgoing shipment
- 
  !python {model: stock.partial.picking}: |
    partial_id = self.create(cr, uid, {}, context={'active_model': 'stock.picking','active_ids': [ref("outgoing_variable_shipment")], 'default_type':'out'})
    self.do_transfer(cr, uid, [partial_id])
- 
  Check the standard price (60 * 10 + 30 * 80) / 40 = 75.0 did not change
- 
  !python {model: product.product}: |
    assert self.browse(cr, uid, ref("product_variable_icecream")).standard_price == 75.0, 'Standard price should not have changed with outgoing picking!'
    assert self.browse(cr, uid, ref("product_variable_icecream")).qty_available == 20.0, 'Pieces were not picked correctly as the quantity on hand is wrong'
- 
  I create a draft Purchase Order for third incoming shipment for 20 pieces at 60 euro
- 
  !record {model: purchase.order, id: purchase_order_variable3}:
    partner_id: base.res_partner_3
    order_line:
      - product_id: product_variable_icecream
        product_qty: 20.0
        product_uom: product.product_uom_categ_kgm
        price_unit: 60.0
        name: 'Variable Ice Cream'
- 
  I confirm this third purchase order
-
  !python {model: purchase.order}: |
    self.button_confirm(cr, uid, [ref("purchase_order_variable3")])
- 
  Process the reception of purchase order 3
- 
  !python {model: stock.partial.picking}: |
    pick_ids = self.pool.get('purchase.order').browse(cr, uid, ref("purchase_order_variable3")).picking_ids
    partial_id = self.create(cr, uid, {}, context={'active_model': 'stock.picking','active_ids': [pick_ids[0].id]})
    self.do_transfer(cr, uid, [partial_id])
- 
  Change the cost method back to average and check price on product changed back to 70.0 = (20*60 + 10*60 + 30*80) / 60
- 
  !python {model: product.product}: |
    self.write(cr, uid, [ref("product_variable_icecream")], {'cost_method': 'average'}, context=context)
    assert self.browse(cr, uid, ref("product_variable_icecream")).standard_price == 70.0, 'After third reception and changing to average again, we should have an average price of 70.0 on the product'
