From 86f08c091eea2a623ee6460562a750506cbdd585 Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Fri, 5 May 2017 15:26:42 +0200 Subject: [PATCH] [REF] stock: adapt scrap to new implementation --- addons/stock/models/stock_picking.py | 4 ++- addons/stock/models/stock_scrap.py | 42 +++++++++--------------- addons/stock/views/stock_scrap_views.xml | 2 +- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/addons/stock/models/stock_picking.py b/addons/stock/models/stock_picking.py index b848d05976c..8d7bc51e9e3 100644 --- a/addons/stock/models/stock_picking.py +++ b/addons/stock/models/stock_picking.py @@ -762,6 +762,8 @@ class Picking(models.Model): @api.multi def button_scrap(self): self.ensure_one() + # only stockable products are scrapeable + scrapeable_products = self.pack_operation_ids.mapped('product_id').filtered(lambda p: p.type == 'product') return { 'name': _('Scrap'), 'view_type': 'form', @@ -769,7 +771,7 @@ class Picking(models.Model): 'res_model': 'stock.scrap', 'view_id': self.env.ref('stock.stock_scrap_form_view2').id, 'type': 'ir.actions.act_window', - 'context': {'default_picking_id': self.id, 'product_ids': self.pack_operation_product_ids.mapped('product_id').ids}, + 'context': {'default_picking_id': self.id, 'product_ids': scrapeable_products.ids}, 'target': 'new', } diff --git a/addons/stock/models/stock_scrap.py b/addons/stock/models/stock_scrap.py index 8453a49b4e3..96ef8443196 100644 --- a/addons/stock/models/stock_scrap.py +++ b/addons/stock/models/stock_scrap.py @@ -78,21 +78,16 @@ class StockScrap(models.Model): @api.multi def do_scrap(self): for scrap in self: - moves = scrap._get_origin_moves() or self.env['stock.move'] move = self.env['stock.move'].create(scrap._prepare_move_values()) - quants = self.env['stock.quant'].quants_get_preferred_domain( - move.product_qty, move, - domain=[ - ('qty', '>', 0), - ('lot_id', '=', self.lot_id.id), - ('package_id', '=', self.package_id.id)], - preferred_domain_list=scrap._get_preferred_domain()) - if any([not x[0] for x in quants]): + quantity_in_stock = self.env['stock.quant']._get_quantity( + self.product_id, self.location_id, lot_id=self.lot_id, + package_id=self.package_id, owner_id=self.owner_id, strict=True + ) + if quantity_in_stock < move.product_qty: # FIXME: float compare raise UserError(_('You cannot scrap a move without having available stock for %s. You can correct it with an inventory adjustment.') % move.product_id.name) - self.env['stock.quant'].quants_reserve(quants, move) move.action_done() scrap.write({'move_id': move.id, 'state': 'done'}) - moves.recalculate_move_state() + # TODO: unreserve reserved return True def _prepare_move_values(self): @@ -106,24 +101,19 @@ class StockScrap(models.Model): 'location_id': self.location_id.id, 'scrapped': True, 'location_dest_id': self.scrap_location_id.id, - 'restrict_lot_id': self.lot_id.id, - 'restrict_partner_id': self.owner_id.id, + 'pack_operation_ids': [(0, 0, {'product_id': self.product_id.id, + 'product_uom_id': self.product_uom_id.id, + 'qty_done': self.scrap_qty, + 'location_id': self.location_id.id, + 'location_dest_id': self.scrap_location_id.id, + 'package_id': self.package_id.id, + 'owner_id': self.owner_id.id, + 'lot_id': self.lot_id.id, })], +# 'restrict_lot_id': self.lot_id.id, # FIXME: JCO i asked you x times, should we drop this or not???? +# 'restrict_partner_id': self.owner_id.id, 'picking_id': self.picking_id.id } - def _get_preferred_domain(self): - if not self.picking_id: - return [] - if self.picking_id.state == 'done': - preferred_domain = [('history_ids', 'in', self.picking_id.move_lines.filtered(lambda x: x.state == 'done')).ids] - preferred_domain2 = [('history_ids', 'not in', self.picking_id.move_lines.filtered(lambda x: x.state == 'done')).ids] - return [preferred_domain, preferred_domain2] - else: - preferred_domain = [('reservation_id', 'in', self.picking_id.move_lines.ids)] - preferred_domain2 = [('reservation_id', '=', False)] - preferred_domain3 = ['&', ('reservation_id', 'not in', self.picking_id.move_lines.ids), ('reservation_id', '!=', False)] - return [preferred_domain, preferred_domain2, preferred_domain3] - @api.multi def action_get_stock_picking(self): action = self.env.ref('stock.action_picking_tree_all').read([])[0] diff --git a/addons/stock/views/stock_scrap_views.xml b/addons/stock/views/stock_scrap_views.xml index fbd2684d80a..12e74eca51f 100644 --- a/addons/stock/views/stock_scrap_views.xml +++ b/addons/stock/views/stock_scrap_views.xml @@ -43,7 +43,7 @@ - +