'Create inventory with different unit of measure.'

def test_20_create_inventory_with_different_uom(self):

    inventory = self.InvObj.create({'name': 'Test', 'product_id': self.UnitA.id, 'filter': 'product'})
    inventory.prepare_inventory()
    self.assertFalse(inventory.line_ids, 'Inventory    line    should    not    created.')
    inventory_line = self.InvLineObj.create({'inventory_id': inventory.id, 'product_id': self.UnitA.id, 'product_uom_id': self.uom_dozen.id, 'product_qty': 10, 'location_id': self.stock_location})
    inventory.action_done()
    quants = self.StockQuantObj.search([('product_id', '=', self.UnitA.id), ('location_id', '=', self.stock_location)])
    total_qty = [quant.qty for quant in quants]
    self.assertEqual(sum(total_qty), 120, ('Expecting    120    Units    ,    got    %.4f    Units    on    location    stock!' % sum(total_qty)))
    self.assertEqual(self.UnitA.qty_available, 120, ('Expecting    120    Units    ,    got    %.4f    Units    of    quantity    available!' % self.UnitA.qty_available))
    inventory = self.InvObj.create({'name': 'Test', 'product_id': self.UnitA.id, 'filter': 'product'})
    inventory.prepare_inventory()
    self.assertEqual(len(inventory.line_ids), 1, 'One    inventory    line    should    be    created.')
    inventory_line = self.InvLineObj.search([('product_id', '=', self.UnitA.id), ('inventory_id', '=', inventory.id)], limit=1)
    self.assertEqual(inventory_line.product_qty, 120, 'Wrong    product    quantity    in    inventory    line.')
    inventory_line.write({'product_qty': 144})
    inventory.action_done()
    move = self.MoveObj.search([('product_id', '=', self.UnitA.id), ('inventory_id', '=', inventory.id)], limit=1)
    self.assertEqual(move.product_uom_qty, 24, 'Wrong    move    quantity    of    product    UnitA.')
    quants = self.StockQuantObj.search([('product_id', '=', self.UnitA.id), ('location_id', '=', self.stock_location)])
    total_qty = [quant.qty for quant in quants]
    self.assertEqual(sum(total_qty), 144, ('Expecting    144    Units    ,    got    %.4f    Units    on    location    stock!' % sum(total_qty)))
    self.assertEqual(self.UnitA.qty_available, 144, ('Expecting    144    Units    ,    got    %.4f    Units    of    quantity    available!' % self.UnitA.qty_available))
    productKG = self.ProductObj.create({'name': 'Product    KG', 'uom_id': self.uom_kg.id, 'uom_po_id': self.uom_kg.id})
    inventory = self.InvObj.create({'name': 'Inventory    Product    KG', 'product_id': productKG.id, 'filter': 'product'})
    inventory.prepare_inventory()
    self.assertFalse(inventory.line_ids, 'Inventory    line    should    not    created.')
    inventory_line = self.InvLineObj.create({'inventory_id': inventory.id, 'product_id': productKG.id, 'product_uom_id': self.uom_tone.id, 'product_qty': 5, 'location_id': self.stock_location})
    inventory.action_done()
    quants = self.StockQuantObj.search([('product_id', '=', productKG.id), ('location_id', '=', self.stock_location)])
    total_qty = [quant.qty for quant in quants]
    self.assertEqual(sum(total_qty), 5000, ('Expecting    5000    kg    ,    got    %.4f    kg    on    location    stock!' % sum(total_qty)))
    self.assertEqual(productKG.qty_available, 5000, ('Expecting    5000    kg    ,    got    %.4f    kg    of    quantity    available!' % productKG.qty_available))
    inventory = self.InvObj.create({'name': 'Test', 'product_id': productKG.id, 'filter': 'product'})
    inventory.prepare_inventory()
    self.assertEqual(len(inventory.line_ids), 1, 'One    inventory    line    should    be    created.')
    inventory_line = self.InvLineObj.search([('product_id', '=', productKG.id), ('inventory_id', '=', inventory.id)], limit=1)
    self.assertEqual(inventory_line.product_qty, 5000, 'Wrong    product    quantity    in    inventory    line.')
    inventory_line.write({'product_qty': 4000})
    inventory.action_done()
    move = self.MoveObj.search([('product_id', '=', productKG.id), ('inventory_id', '=', inventory.id)], limit=1)
    self.assertEqual(move.product_uom_qty, 1000, 'Wrong    move    quantity    of    product    KG.')
    quants = self.StockQuantObj.search([('product_id', '=', productKG.id), ('location_id', '=', self.stock_location)])
    total_qty = [quant.qty for quant in quants]
    self.assertEqual(sum(total_qty), 4000, ('Expecting    4000    kg    ,    got    %.4f    on    location    stock!' % sum(total_qty)))
    self.assertEqual(productKG.qty_available, 4000, ('Expecting    4000    kg    ,    got    %.4f    of    quantity    available!' % productKG.qty_available))
    packproduct = self.ProductObj.create({'name': 'Pack    Product', 'uom_id': self.uom_unit.id, 'uom_po_id': self.uom_unit.id})
    lotproduct = self.ProductObj.create({'name': 'Lot    Product', 'uom_id': self.uom_unit.id, 'uom_po_id': self.uom_unit.id})
    inventory = self.InvObj.create({'name': 'Test    Partial    and    Pack', 'filter': 'partial', 'location_id': self.stock_location})
    inventory.prepare_inventory()
    pack_obj = self.env['stock.quant.package']
    lot_obj = self.env['stock.production.lot']
    pack1 = pack_obj.create({'name': 'PACK00TEST1'})
    pack2 = pack_obj.create({'name': 'PACK00TEST2'})
    lot1 = lot_obj.create({'name': 'Lot001', 'product_id': lotproduct.id})
    move = self.MoveObj.search([('product_id', '=', productKG.id), ('inventory_id', '=', inventory.id)], limit=1)
    self.assertEqual(len(move), 0, 'Partial    filter    should    not    create    a    lines    upon    prepare')
    line_vals = []
    line_vals += [{'location_id': self.stock_location, 'product_id': packproduct.id, 'product_qty': 10, 'product_uom_id': packproduct.uom_id.id}]
    line_vals += [{'location_id': self.stock_location, 'product_id': packproduct.id, 'product_qty': 20, 'product_uom_id': packproduct.uom_id.id, 'package_id': pack1.id}]
    line_vals += [{'location_id': self.stock_location, 'product_id': lotproduct.id, 'product_qty': 30, 'product_uom_id': lotproduct.uom_id.id, 'prod_lot_id': lot1.id}]
    line_vals += [{'location_id': self.stock_location, 'product_id': lotproduct.id, 'product_qty': 25, 'product_uom_id': lotproduct.uom_id.id, 'prod_lot_id': False}]
    inventory.write({'line_ids': [(0, 0, x) for x in line_vals]})
    inventory.action_done()
    self.assertEqual(packproduct.qty_available, 30, 'Wrong    qty    available    for    packproduct')
    self.assertEqual(lotproduct.qty_available, 55, 'Wrong    qty    available    for    lotproduct')
    quants = self.StockQuantObj.search([('product_id', '=', packproduct.id), ('location_id', '=', self.stock_location), ('package_id', '=', pack1.id)])
    total_qty = sum([quant.qty for quant in quants])
    self.assertEqual(total_qty, 20, ('Expecting    20    units    on    package    1    of    packproduct,    but    we    got    %.4f    on    location    stock!' % total_qty))
    inventory2 = self.InvObj.create({'name': 'Test    Partial    Lot    and    Pack2', 'filter': 'partial', 'location_id': self.stock_location})
    inventory2.prepare_inventory()
    line_vals = []
    line_vals += [{'location_id': self.stock_location, 'product_id': packproduct.id, 'product_qty': 20, 'product_uom_id': packproduct.uom_id.id}]
    line_vals += [{'location_id': self.stock_location, 'product_id': lotproduct.id, 'product_qty': 0, 'product_uom_id': lotproduct.uom_id.id, 'prod_lot_id': False}]
    line_vals += [{'location_id': self.stock_location, 'product_id': lotproduct.id, 'product_qty': 10, 'product_uom_id': lotproduct.uom_id.id, 'prod_lot_id': lot1.id}]
    inventory2.write({'line_ids': [(0, 0, x) for x in line_vals]})
    inventory2.action_done()
    self.assertEqual(packproduct.qty_available, 40, 'Wrong    qty    available    for    packproduct')
    self.assertEqual(lotproduct.qty_available, 10, 'Wrong    qty    available    for    lotproduct')
    quants = self.StockQuantObj.search([('product_id', '=', lotproduct.id), ('location_id', '=', self.stock_location), ('lot_id', '=', lot1.id)])
    total_qty = sum([quant.qty for quant in quants])
    self.assertEqual(total_qty, 10, ('Expecting    0    units    lot    of    lotproduct,    but    we    got    %.4f    on    location    stock!' % total_qty))
    quants = self.StockQuantObj.search([('product_id', '=', lotproduct.id), ('location_id', '=', self.stock_location), ('lot_id', '=', False)])
    total_qty = sum([quant.qty for quant in quants])
    self.assertEqual(total_qty, 0, ('Expecting    0    units    lot    of    lotproduct,    but    we    got    %.4f    on    location    stock!' % total_qty))
    category_id = self.ref('product.product_category_5')
    inventory3 = self.InvObj.create({'name': 'Test    Category', 'filter': 'category', 'location_id': self.stock_location, 'category_id': category_id})
    inventory3.prepare_inventory()
    products_category = inventory3.line_ids.mapped('product_id.categ_id')
    self.assertEqual(len(products_category), 1, 'Inventory    line    should    have    only    one    category')
    inventory3.action_done()
    inventory4 = self.InvObj.create({'name': 'Test    Exhausted    Product', 'filter': 'category', 'location_id': self.stock_location, 'category_id': category_id, 'exhausted': True})
    inventory4.prepare_inventory()
    inventory4._get_inventory_lines_values()
    inventory4_lines_count = len(inventory4.line_ids)
    inventory4.action_done()
    product = self.ProductObj.create({'name': 'Product    A', 'type': 'product', 'categ_id': category_id})
    inventory5 = self.InvObj.create({'name': 'Test    Exhausted    Product', 'filter': 'category', 'location_id': self.stock_location, 'category_id': category_id, 'exhausted': True})
    inventory5.prepare_inventory()
    inventory5._get_inventory_lines_values()
    inventory5_lines_count = len(inventory5.line_ids)
    inventory5.action_done()
    self.assertEqual(inventory5_lines_count, (inventory4_lines_count + 1), 'The    new    product    is    not    taken    into    account    in    the    inventory    valuation.')
    self.assertTrue((product.id in inventory5.line_ids.mapped('product_id').ids), 'The    new    product    is    not    take    into    account    in    the    inventory    valuation.')
