-
  Create a new "negative" stockable product 
- 
  !record {model: product.product, id: product_neg, view: False}:
    name: Negative product
    type: product
    categ_id: product.product_category_1
    list_price: 100.0
    standard_price: 70.0
    seller_ids:
      - delay: 1
        name: base.res_partner_2
        min_qty: 2.0
    uom_id: product.product_uom_unit
    uom_po_id: product.product_uom_unit
- 
  Create an incoming picking for this product of 300 PCE from suppliers to stock
- 
  !record {model: stock.picking, id: pick_neg}:
    name: Incoming picking (negative product)
    partner_id: base.res_partner_2
    picking_type_id: picking_type_in
    move_lines:
      - product_id: product_neg
        product_uom_qty: 300.00
        location_id: stock_location_suppliers
        location_dest_id: stock_location_stock
- 
  Confirm and assign picking
- 
  !python {model: stock.picking, id: pick_neg}: |
    self.action_confirm()
    self.action_assign()
-
  Put 120 pieces on Palneg 1 (package), 120 pieces on Palneg 2 with lot A and 60 pieces on Palneg 3
- 
  !python {model: stock.picking, id: pick_neg}: |
    #Change quantity of first to 120 and create 2 others quant operations
    stock_pack = self.env['stock.move.line']
    stock_quant_pack = self.env['stock.quant.package']
    #create lot A
    lot_a = self.env['stock.production.lot'].create({'name': 'Lot neg', 'product_id': ref('product_neg')})
    #create package
    package1 = stock_quant_pack.create({'name': 'Palneg 1'})
    package2 = stock_quant_pack.create({'name': 'Palneg 2'})
    package3 = stock_quant_pack.create({'name': 'Palneg 3'})
    #Create package for each line and assign it as result_package_id
    #create pack operation
    self.move_line_ids[0].write({'result_package_id': package1.id, 'qty_done': 120})
    new_pack1 = stock_pack.create({
      'product_id': ref('product_neg'),
      'product_uom_id': ref('product.product_uom_unit'),
      'picking_id': ref('pick_neg'),
      'lot_id': lot_a.id,
      'qty_done': 120,
      'result_package_id': package2.id,
      'location_id': ref('stock_location_suppliers'),
      'location_dest_id': ref('stock_location_stock')
    })
    new_pack2 = stock_pack.create({
      'product_id': ref('product_neg'),
      'product_uom_id': ref('product.product_uom_unit'),
      'picking_id': ref('pick_neg'),
      'result_package_id': package3.id,
      'qty_done': 60,
      'location_id': ref('stock_location_suppliers'),
      'location_dest_id': ref('stock_location_stock')
    })
- 
  Transfer the receipt
- 
  !python {model: stock.picking, id: pick_neg}: |
    self.do_transfer()
- 
  Make a delivery order of 300 pieces to the customer
- 
  !record {model: stock.picking, id: delivery_order_neg}:
    name: outgoing picking (negative product)
    partner_id: base.res_partner_4
    picking_type_id: stock.picking_type_out
    move_lines:
      - product_id: product_neg
        product_uom_qty: 300.00
        location_id: stock_location_stock
        location_dest_id: stock_location_customers
-
  Assign and confirm
-
  !python {model: stock.picking, id: delivery_order_neg}: |
    self.action_confirm()
    self.action_assign()
-
  Instead of doing the 300 pieces, you decide to take pallet 1 (do not mention product in operation here) and 140 pieces from lot A/pallet 2 and 10 pieces from pallet 3
-
  !python {model: stock.picking, id: delivery_order_neg}: |
    for rec in self.move_line_ids:
      if rec.package_id.name == 'Palneg 1':
        rec.qty_done = rec.product_qty
        rec.result_package_id = False
      elif rec.package_id.name == 'Palneg 2' and rec.lot_id.name == 'Lot neg' :
        rec.write({
          'qty_done': 140,
          'result_package_id': False,
        })
      elif rec.package_id.name == 'Palneg 3':
        rec.qty_done = 10
        rec.result_package_id = False
-
  Process this picking
-
  !python {model: stock.picking, id: delivery_order_neg}: |
    self.do_transfer()
-
  Check the quants that you have -20 pieces pallet 2 in stock, and a total quantity of 50 in stock from pallet 3 (should be 20+30, as it has been split by reservation)
-
  !python {model: stock.quant, id: False}: |
    records = self.search([('product_id','=',ref('product_neg'))])
    pallet_3_stock_qty = 0
    for rec in records:
      if rec.package_id.name == 'Palneg 2' and rec.location_id.id == ref('stock_location_stock'):
        assert rec.quantity == -20, "Should have -20 pieces in stock on pallet 2. Got " + str(rec.quantity)
        assert rec.lot_id.name == 'Lot neg', "It should have kept its Lot"
      elif rec.package_id.name == 'Palneg 3' and rec.location_id.id == ref('stock_location_stock'):
        pallet_3_stock_qty += rec.quantity
      else:
        assert rec.location_id.id != ref('stock_location_stock'), "Unrecognized quant in stock"
    assert pallet_3_stock_qty == 50, "Should have 50 pieces in stock on pallet 3"
-
  Create a picking for reconciling the negative quant
-
  !record {model: stock.picking, id: delivery_reconcile}:
    name: reconciling_delivery
    partner_id: base.res_partner_4
    picking_type_id: stock.picking_type_in
    move_lines:
      - product_id: product_neg
        product_uom_qty: 20.0
        location_id: stock_location_suppliers
        location_dest_id: stock_location_stock
-
  Receive 20 products with lot neg in stock with a new incoming shipment that should be on pallet 2
-
  !python {model: stock.picking, id: delivery_reconcile}: |
    self.action_confirm()
    pack_obj = self.env["stock.quant.package"]
    lot = self.env["stock.production.lot"].search([('product_id', '=', ref('product_neg')), ('name','=','Lot neg')], limit=1)
    pack = pack_obj.search([('name', '=', 'Palneg 2')], limit=1)
    self.move_line_ids[0].write({'lot_id': lot.id, 'qty_done': 20.0, 'result_package_id': pack.id})
    self.do_transfer()
-
  Check the negative quant was reconciled
-
  !python {model: stock.quant, id: False}: |
    neg_quants = self.search([('product_id','=', ref('product_neg')), ('quantity', '<', 0), ('location_id.id', '!=',                      
    ref('stock_location_suppliers'))])
    assert len(neg_quants) == 0, "Negative quants should have been reconciled"
    pick = self.env['stock.picking'].browse(ref('delivery_reconcile'))
    customer_quant = self.search([
      ('product_id', '=', ref('product_neg')),
      ('location_id', '=', ref('stock_location_customers')),
      ('lot_id.name','=', 'Lot neg'),
      ('quantity','=', 20)
    ])
