'Testing consume quants and produced quants without workorder'

def test_01_without_workorder(self):

    unit = self.ref('product.product_uom_unit')
    custom_laptop = self.env.ref('product.product_product_27')
    custom_laptop.tracking = 'lot'
    product_charger = self.env['product.product'].create({'name': 'Charger', 'type': 'product', 'tracking': 'lot', 'uom_id': unit, 'uom_po_id': unit})
    product_keybord = self.env['product.product'].create({'name': 'Usb    Keybord', 'type': 'product', 'tracking': 'lot', 'uom_id': unit, 'uom_po_id': unit})
    bom_custom_laptop = self.env['mrp.bom'].create({'product_tmpl_id': custom_laptop.product_tmpl_id.id, 'product_qty': 10, 'product_uom_id': unit, 'bom_line_ids': [(0, 0, {'product_id': product_charger.id, 'product_qty': 20, 'product_uom_id': unit}), (0, 0, {'product_id': product_keybord.id, 'product_qty': 20, 'product_uom_id': unit})]})
    mo_custom_laptop = self.env['mrp.production'].create({'product_id': custom_laptop.id, 'product_qty': 10, 'product_uom_id': unit, 'bom_id': bom_custom_laptop.id})
    mo_custom_laptop.action_assign()
    self.assertEqual(mo_custom_laptop.availability, 'waiting')
    lot_charger = self.env['stock.production.lot'].create({'product_id': product_charger.id})
    lot_keybord = self.env['stock.production.lot'].create({'product_id': product_keybord.id})
    inventory = self.env['stock.inventory'].create({'name': 'Inventory    Product    Table', 'filter': 'partial', 'line_ids': [(0, 0, {'product_id': product_charger.id, 'product_uom_id': product_charger.uom_id.id, 'product_qty': 20, 'prod_lot_id': lot_charger.id, 'location_id': self.source_location_id}), (0, 0, {'product_id': product_keybord.id, 'product_uom_id': product_keybord.uom_id.id, 'product_qty': 20, 'prod_lot_id': lot_keybord.id, 'location_id': self.source_location_id})]})
    inventory.action_done()
    mo_custom_laptop.action_assign()
    self.assertEqual(mo_custom_laptop.availability, 'assigned')
    for move in mo_custom_laptop.move_raw_ids:
        self.assertEqual(move.product_uom_qty, 20, ('Wrong    consume    quantity    of    raw    material    %s:    %s    instead    of    %s' % (move.product_id.name, move.product_uom_qty, 20)))
        self.assertEqual(move.quantity_done, 0, ('Wrong    produced    quantity    on    raw    material    %s:    %s    instead    of    %s' % (move.product_id.name, move.quantity_done, 0)))
    context = {'active_ids': [mo_custom_laptop.id], 'active_id': mo_custom_laptop.id}
    product_consume = self.env['mrp.product.produce'].with_context(context).create({'product_qty': 6.0})
    laptop_lot_001 = self.env['stock.production.lot'].create({'product_id': custom_laptop.id})
    product_consume.lot_id = laptop_lot_001.id
    product_consume.consume_line_ids.write({'quantity_done': 12})
    product_consume.do_produce()
    for move in mo_custom_laptop.move_raw_ids:
        self.assertEqual(move.quantity_done, 12, ('Wrong    produced    quantity    on    raw    material    %s' % move.product_id.name))
    self.assertEqual(len(mo_custom_laptop.move_raw_ids), 2)
    mo_custom_laptop.post_inventory()
    self.assertEqual(len(mo_custom_laptop.move_raw_ids), 4)
    charger_done_move = mo_custom_laptop.move_raw_ids.filtered((lambda x: ((x.product_id.id == product_charger.id) and (x.state == 'done'))))
    keybord_done_move = mo_custom_laptop.move_raw_ids.filtered((lambda x: ((x.product_id.id == product_keybord.id) and (x.state == 'done'))))
    self.assertEquals(charger_done_move.product_uom_qty, 12)
    self.assertEquals(keybord_done_move.product_uom_qty, 12)
    context = {'active_ids': [mo_custom_laptop.id], 'active_id': mo_custom_laptop.id}
    product_consume = self.env['mrp.product.produce'].with_context(context).create({'product_qty': 4.0})
    laptop_lot_002 = self.env['stock.production.lot'].create({'product_id': custom_laptop.id})
    product_consume.lot_id = laptop_lot_002.id
    self.assertEquals(len(product_consume.consume_line_ids), 2)
    product_consume.consume_line_ids.write({'quantity_done': 8})
    product_consume.do_produce()
    charger_move = mo_custom_laptop.move_raw_ids.filtered((lambda x: ((x.product_id.id == product_charger.id) and (x.state != 'done'))))
    keybord_move = mo_custom_laptop.move_raw_ids.filtered((lambda x: ((x.product_id.id == product_keybord.id) and (x.state != 'done'))))
    self.assertEquals(charger_move.quantity_done, 8, ('Wrong    consumed    quantity    of    %s' % charger_move.product_id.name))
    self.assertEquals(keybord_move.quantity_done, 8, ('Wrong    consumed    quantity    of    %s' % keybord_move.product_id.name))
    mo_custom_laptop.post_inventory()
    raw_moves_state = any(((move.state != 'done') for move in mo_custom_laptop.move_raw_ids))
    finsh_moves_state = any(((move.state != 'done') for move in mo_custom_laptop.move_finished_ids))
    self.assertFalse(raw_moves_state, 'Wrong    state    in    consumed    moves    of    production    order.')
    self.assertFalse(finsh_moves_state, 'Wrong    state    in    consumed    moves    of    production    order.')
    finshed_quant_lot_001 = mo_custom_laptop.move_finished_ids.filtered((lambda x: ((x.product_id.id == custom_laptop.id) and (x.product_uom_qty == 6)))).mapped('quant_ids')
    finshed_quant_lot_002 = mo_custom_laptop.move_finished_ids.filtered((lambda x: ((x.product_id.id == custom_laptop.id) and (x.product_uom_qty == 4)))).mapped('quant_ids')
    for consume_quant in finshed_quant_lot_001[0].consumed_quant_ids:
        self.assertEqual(consume_quant.qty, 12)
        self.assertEqual(consume_quant.produced_quant_ids[0].lot_id.id, finshed_quant_lot_001[0].lot_id.id)
        self.assertEqual(consume_quant.produced_quant_ids[0].id, finshed_quant_lot_001[0].id)
    self.assertEqual(len(finshed_quant_lot_001[0].consumed_quant_ids), 2, ('Wrong    consumed    quant    linked    with    produced    quant    for    lot    %s    ' % laptop_lot_001.name))
    self.assertEqual(len(finshed_quant_lot_002[0].consumed_quant_ids), 2, ('Wrong    consumed    quant    linked    with    produced    quant    for    lot    %s    ' % laptop_lot_002.name))
    for consume_quant in finshed_quant_lot_002[0].consumed_quant_ids:
        self.assertEqual(consume_quant.qty, 8)
        self.assertEqual(consume_quant.produced_quant_ids[0].lot_id.id, finshed_quant_lot_002[0].lot_id.id)
        self.assertEqual(consume_quant.produced_quant_ids[0].id, finshed_quant_lot_002[0].id)
    charger_quants = mo_custom_laptop.move_raw_ids.filtered((lambda x: ((x.product_id.id == product_charger.id) and (x.state == 'done')))).mapped('quant_ids')
    keybord_moves = mo_custom_laptop.move_raw_ids.filtered((lambda x: ((x.product_id.id == product_keybord.id) and (x.state == 'done')))).mapped('quant_ids')
    self.assertEqual(sum(charger_quants.mapped('qty')), 20)
    self.assertEqual(sum(keybord_moves.mapped('qty')), 20)
