'Test sale to mrp flow with diffrent unit of measure.'

def test_00_sale_mrp_flow(self):

    route_manufacture = self.warehouse.manufacture_pull_id.route_id.id
    route_mto = self.warehouse.mto_pull_id.route_id.id
    def create_product(name, uom_id, route_ids=[]):
        return self.Product.create({'name': name, 'type': 'product', 'uom_id': uom_id, 'uom_po_id': uom_id, 'route_ids': route_ids})
    def create_bom_lines(bom_id, product_id, qty, uom_id, procure_method):
        self.MrpBomLine.create({'product_id': product_id, 'product_qty': qty, 'bom_id': bom_id, 'product_uom_id': uom_id})
    def create_bom(product_tmpl_id, qty, uom_id, type):
        return self.MrpBom.create({'product_tmpl_id': product_tmpl_id, 'product_qty': qty, 'type': type, 'product_uom_id': uom_id})
    self.uom_kg = self.ProductUom.create({'name': 'Test-KG', 'category_id': self.categ_kgm.id, 'factor_inv': 1, 'factor': 1, 'uom_type': 'reference', 'rounding': 1e-06})
    self.uom_gm = self.ProductUom.create({'name': 'Test-G', 'category_id': self.categ_kgm.id, 'uom_type': 'smaller', 'factor': 1000.0, 'rounding': 0.001})
    self.uom_unit = self.ProductUom.create({'name': 'Test-Unit', 'category_id': self.categ_unit.id, 'factor': 1, 'uom_type': 'reference', 'rounding': 1.0})
    self.uom_dozen = self.ProductUom.create({'name': 'Test-DozenA', 'category_id': self.categ_unit.id, 'factor_inv': 12, 'uom_type': 'bigger', 'rounding': 0.001})
    product_a = create_product('Product    A', self.uom_unit.id, route_ids=[(6, 0, [route_manufacture, route_mto])])
    product_c = create_product('Product    C', self.uom_kg.id, route_ids=[])
    product_b = create_product('Product    B', self.uom_dozen.id, route_ids=[(6, 0, [route_manufacture, route_mto])])
    product_d = create_product('Product    D', self.uom_unit.id, route_ids=[(6, 0, [route_manufacture, route_mto])])
    bom_a = create_bom(product_a.product_tmpl_id.id, 2, self.uom_dozen.id, 'normal')
    create_bom_lines(bom_a.id, product_b.id, 3, self.uom_unit.id, 'make_to_order')
    create_bom_lines(bom_a.id, product_c.id, 300.5, self.uom_gm.id, 'make_to_stock')
    create_bom_lines(bom_a.id, product_d.id, 4, self.uom_unit.id, 'make_to_order')
    bom_b = create_bom(product_b.product_tmpl_id.id, 1, self.uom_unit.id, 'phantom')
    create_bom_lines(bom_b.id, product_c.id, 0.4, self.uom_kg.id, 'make_to_stock')
    bom_d = create_bom(product_d.product_tmpl_id.id, 1, self.uom_unit.id, 'normal')
    create_bom_lines(bom_d.id, product_c.id, 1, self.uom_kg.id, 'make_to_stock')
    order = self.SaleOrder.create({'partner_id': self.partner_agrolite.id, 'partner_invoice_id': self.partner_agrolite.id, 'partner_shipping_id': self.partner_agrolite.id, 'date_order': datetime.today(), 'pricelist_id': self.env.ref('product.list0').id})
    self.SaleOrderLine.create({'name': product_a.name, 'order_id': order.id, 'product_id': product_a.id, 'product_uom_qty': 10, 'product_uom': self.uom_dozen.id})
    self.assertTrue(order, 'Sale    order    not    created.')
    order.action_confirm()
    self.ProcurementOrder.run_scheduler()
    mnf_product_a = self.ProcurementOrder.search([('product_id', '=', product_a.id), ('group_id', '=', order.procurement_group_id.id), ('production_id', '!=', False)]).production_id
    self.assertTrue(mnf_product_a, 'Manufacturing    order    not    created.')
    self.assertEqual(mnf_product_a.product_qty, 10, 'Wrong    product    quantity    in    manufacturing    order.')
    self.assertEqual(mnf_product_a.product_uom_id.id, self.uom_dozen.id, 'Wrong    unit    of    measure    in    manufacturing    order.')
    self.assertEqual(mnf_product_a.state, 'confirmed', 'Manufacturing    order    should    be    confirmed.')
    moves = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_c.id), ('product_uom', '=', self.uom_kg.id)])
    self.assertEqual(len(moves), 1, 'Production    move    lines    are    not    generated    proper.')
    list_qty = {move.product_uom_qty for move in moves}
    self.assertEqual(list_qty, {6.0}, "Wrong    product    quantity    in    'To    consume    line'    of    manufacturing    order.")
    for move in moves:
        self.assertEqual(move.state, 'confirmed', "Wrong    state    in    'To    consume    line'    of    manufacturing    order.")
    move = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_c.id), ('product_uom', '=', self.uom_gm.id)])
    self.assertEqual(len(move), 1, 'Production    move    lines    are    not    generated    proper.')
    self.assertEqual(move.product_uom_qty, 1502.5, "Wrong    product    quantity    in    'To    consume    line'    of    manufacturing    order.")
    self.assertEqual(move.state, 'confirmed', "Wrong    state    in    'To    consume    line'    of    manufacturing    order.")
    move = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_d.id)])
    self.assertEqual(len(move), 1, 'Production    lines    are    not    generated    proper.')
    procurement_d = self.ProcurementOrder.search([('product_id', '=', product_d.id), ('group_id', '=', order.procurement_group_id.id)])
    self.assertEqual(len(procurement_d), 1, 'Procurement    order    not    generated.')
    self.assertTrue(procurement_d.production_id, 'Production    order    not    generated    from    procurement.')
    mnf_product_d = procurement_d.production_id
    self.assertEqual(mnf_product_d.state, 'confirmed', 'Manufacturing    order    should    be    confirmed.')
    move = self.StockMove.search([('raw_material_production_id', '=', mnf_product_d.id), ('product_id', '=', product_c.id)])
    self.assertEqual(move.product_uom_qty, 20, "Wrong    product    quantity    in    'To    consume    line'    of    manufacturing    order.")
    self.assertEqual(move.product_uom.id, self.uom_kg.id, "Wrong    unit    of    measure    in    'To    consume    line'    of    manufacturing    order.")
    self.assertEqual(move.state, 'confirmed', "Wrong    state    in    'To    consume    line'    of    manufacturing    order.")
    inventory = self.Inventory.create({'name': 'Inventory    Product    KG', 'product_id': product_c.id, 'filter': 'product'})
    inventory.prepare_inventory()
    self.assertFalse(inventory.line_ids, 'Inventory    line    should    not    created.')
    self.InventoryLine.create({'inventory_id': inventory.id, 'product_id': product_c.id, 'product_uom_id': self.uom_kg.id, 'product_qty': 20, 'location_id': self.stock_location.id})
    inventory.action_done()
    mnf_product_d.action_assign()
    self.assertEqual(mnf_product_d.availability, 'assigned', 'Availability    should    be    assigned')
    self.assertEqual(move.state, 'assigned', "Wrong    state    in    'To    consume    line'    of    manufacturing    order.")
    produce_d = self.ProductProduce.with_context({'active_ids': [mnf_product_d.id], 'active_id': mnf_product_d.id}).create({'product_qty': 20})
    produce_d.do_produce()
    mnf_product_d.post_inventory()
    self.assertEqual(mnf_product_d.state, 'progress', 'Manufacturing    order    should    still    be    in    progress    state.')
    self.assertEqual(product_d.qty_available, 20, 'Wrong    quantity    available    of    product    D.')
    self.assertEqual(mnf_product_a.state, 'confirmed', 'Manufacturing    order    should    be    confirmed.')
    move = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_d.id)])
    self.assertEqual(move.state, 'assigned', "Wrong    state    in    'To    consume    line'    of    manufacturing    order.")
    inventory = self.Inventory.create({'name': 'Inventory    Product    C    KG', 'product_id': product_c.id, 'filter': 'product'})
    inventory.prepare_inventory()
    self.assertFalse(inventory.line_ids, 'Inventory    line    should    not    created.')
    self.InventoryLine.create({'inventory_id': inventory.id, 'product_id': product_c.id, 'product_uom_id': self.uom_kg.id, 'product_qty': 27.5025, 'location_id': self.stock_location.id})
    inventory.action_done()
    mnf_product_a.action_assign()
    self.assertEqual(mnf_product_a.availability, 'assigned', 'Manufacturing    order    inventory    state    should    be    available.')
    moves = self.StockMove.search([('raw_material_production_id', '=', mnf_product_a.id), ('product_id', '=', product_c.id)])
    for move in moves:
        self.assertEqual(move.state, 'assigned', "Wrong    state    in    'To    consume    line'    of    manufacturing    order.")
    produce_a = self.ProductProduce.with_context({'active_ids': [mnf_product_a.id], 'active_id': mnf_product_a.id}).create({})
    produce_a.do_produce()
    mnf_product_a.post_inventory()
    self.assertEqual(mnf_product_a.state, 'progress', 'Manufacturing    order    should    still    be    in    the    progress    state.')
    self.assertEqual(product_a.qty_available, 120, 'Wrong    quantity    available    of    product    A.')
