'Test delivered quantity on SO based on delivered quantity in pickings.'

def test_01_sale_mrp_delivery_kit(self):

    self.partner = self.env.ref('base.res_partner_1')
    self.product = self.env.ref('mrp.product_product_build_kit')
    self.product.invoice_policy = 'delivery'
    so_vals = {'partner_id': self.partner.id, 'partner_invoice_id': self.partner.id, 'partner_shipping_id': self.partner.id, 'order_line': [(0, 0, {'name': self.product.name, 'product_id': self.product.id, 'product_uom_qty': 5, 'product_uom': self.product.uom_id.id, 'price_unit': self.product.list_price})], 'pricelist_id': self.env.ref('product.list0').id}
    self.so = self.SaleOrder.create(so_vals)
    self.so.action_confirm()
    self.assertTrue(self.so.picking_ids, 'Sale    MRP:    no    picking    created    for    "invoice    on    delivery"    stockable    products')
    with self.assertRaises(UserError):
        self.so.action_invoice_create()
    self.assertEqual(self.so.invoice_status, 'no', 'Sale    MRP:    so    invoice_status    should    be    "nothing    to    invoice"    after    invoicing')
    pick = self.so.picking_ids
    pick.force_assign()
    pick.pack_operation_product_ids.write({'qty_done': 1})
    wiz_act = pick.do_new_transfer()
    wiz = self.env[wiz_act['res_model']].browse(wiz_act['res_id'])
    wiz.process()
    self.assertEqual(self.so.invoice_status, 'no', 'Sale    MRP:    so    invoice_status    should    be    "no"    after    partial    delivery    of    a    kit')
    del_qty = sum((sol.qty_delivered for sol in self.so.order_line))
    self.assertEqual(del_qty, 0.0, 'Sale    MRP:    delivered    quantity    should    be    zero    after    partial    delivery    of    a    kit')
    self.assertEqual(len(self.so.picking_ids), 2, 'Sale    MRP:    number    of    pickings    should    be    2')
    pick_2 = self.so.picking_ids[0]
    pick_2.force_assign()
    pick_2.pack_operation_product_ids.write({'qty_done': 4})
    pick_2.do_new_transfer()
    del_qty = sum((sol.qty_delivered for sol in self.so.order_line))
    self.assertEqual(del_qty, 5.0, 'Sale    MRP:    delivered    quantity    should    be    5.0    after    complete    delivery    of    a    kit')
    self.assertEqual(self.so.invoice_status, 'to    invoice', 'Sale    MRP:    so    invoice_status    should    be    "to    invoice"    after    complete    delivery    of    a    kit')
