Skip to content

Commit

Permalink
[FIX] no neg_quant_qty and rename for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
jco-odoo committed Feb 21, 2017
1 parent d0982bd commit 02424bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
36 changes: 19 additions & 17 deletions addons/stock/models/stock_inventory.py
Expand Up @@ -384,19 +384,21 @@ def _generate_moves(self):
moves = self.env['stock.move']
Quant = self.env['stock.quant']
for line in self:
neg_quants = self.env['stock.quant']
neg_quants = Quant.search([('qty', '<', 0.0), ('product_id', '=', line.product_id.id),
('location_id', '=', line.location_id.id), ('package_id', '=', line.package_id.id),
('lot_id', '=', line.prod_lot_id.id), ('owner_id', '=', line.partner_id.id)])
if float_utils.float_compare(line.theoretical_qty, line.product_qty, precision_rounding=line.product_id.uom_id.rounding) == 0 and not neg_quants:
continue
continue

neg_quant_qty = - sum([x.qty for x in neg_quants])
if float_utils.float_compare(line.theoretical_qty, 0, precision_rounding=line.product_id.uom_id.rounding) < 0:
neg_quant_qty += line.theoretical_qty # As the theoretical_qty is negative, we add it to neg_quant_qty
diff = line.theoretical_qty - line.product_qty
if diff > 0:
neg_quant_qty -= diff
if neg_quant_qty < 0:
neg_quant_qty = 0

vals = {
'name': _('INV:') + (line.inventory_id.name or ''),
'product_id': line.product_id.id,
Expand All @@ -407,31 +409,31 @@ def _generate_moves(self):
'state': 'confirmed',
'restrict_lot_id': line.prod_lot_id.id,
'restrict_partner_id': line.partner_id.id}
move_neg = False
move_pos = False
move_add = False
move_rem = False
if diff < 0 or neg_quant_qty: # found more than expected
vals['location_id'] = line.product_id.property_stock_inventory.id
vals['location_dest_id'] = line.location_id.id
vals['product_uom_qty'] = abs(diff) + neg_quant_qty
move_neg = moves.create(vals)
move_add = moves.create(vals)
if diff > 0 or neg_quant_qty:
vals['location_id'] = line.location_id.id
vals['location_dest_id'] = line.product_id.property_stock_inventory.id
vals['product_uom_qty'] = diff + neg_quant_qty
move_pos = moves.create(vals)
move_rem = moves.create(vals)

if move_pos:
if move_rem:
domain = [('qty', '>', 0.0), ('package_id', '=', line.package_id.id), ('lot_id', '=', line.prod_lot_id.id), ('location_id', '=', line.location_id.id)]
preferred_domain_list = [[('reservation_id', '=', False)], [('reservation_id.inventory_id', '!=', line.inventory_id.id)]]
quants = Quant.quants_get_preferred_domain(move_pos.product_qty, move_pos, domain=domain, preferred_domain_list=preferred_domain_list)
Quant.quants_reserve(quants, move_pos)
if move_neg and line.package_id:
move_neg.action_done()
move_neg.quant_ids.write({'package_id': line.package_id.id})
quants = Quant.search([('qty', '<', 0.0), ('product_id', '=', move_neg.product_id.id),
('location_id', '=', move_neg.location_dest_id.id), ('package_id', '!=', False)], limit=1)
quants = Quant.quants_get_preferred_domain(move_rem.product_qty, move_rem, domain=domain, preferred_domain_list=preferred_domain_list)
Quant.quants_reserve(quants, move_rem)
if move_add and line.package_id:
move_add.action_done()
move_add.quant_ids.write({'package_id': line.package_id.id})
quants = Quant.search([('qty', '<', 0.0), ('product_id', '=', move_add.product_id.id),
('location_id', '=', move_add.location_dest_id.id), ('package_id', '!=', False)], limit=1)
if quants:
for quant in move_neg.quant_ids:
if quant.location_id.id == move_neg.location_dest_id.id: #To avoid we take a quant that was reconcile already
quant._quant_reconcile_negative(move_neg)
for quant in move_add.quant_ids:
if quant.location_id.id == move_add.location_dest_id.id: #To avoid we take a quant that was reconcile already
quant._quant_reconcile_negative(move_add)
return moves
2 changes: 1 addition & 1 deletion addons/stock_account/views/product_views.xml
Expand Up @@ -50,7 +50,7 @@
groups="stock_account.group_inventory_valuation"/>
</div>
<div name="standard_price_uom" position="after">
<button string="Compute from average price" type="action"
<button string="Compute average price" type="action"
name="%(action_view_change_standard_price)d"
attrs="{'invisible':['|', '|', ('valuation','!=', 'real_time'), ('cost_method', 'not in', ['average']), ('product_variant_count', '>', 1)]}"
class="oe_link oe_read_only" colspan="2"
Expand Down

0 comments on commit 02424bc

Please sign in to comment.