From 369d22296594b55633de113bcb8ce9af0550672d Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Wed, 5 Feb 2014 18:24:44 +0100 Subject: [PATCH] [REF] some refactoring made during code review bzr revid: qdp-launchpad@openerp.com-20140205172444-4v7msetkkp7ypb7y --- addons/stock/stock.py | 22 ++++------- addons/stock/wizard/stock_return_picking.py | 39 +++++++------------ .../wizard/stock_return_picking_view.xml | 14 +++++-- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/addons/stock/stock.py b/addons/stock/stock.py index def6ed637ce..a79ded69f9a 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -26,7 +26,6 @@ import time from openerp.osv import fields, osv from openerp.tools.translate import _ -from openerp import tools from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT from openerp import SUPERUSER_ID import openerp.addons.decimal_precision as dp @@ -784,8 +783,8 @@ class stock_picking(osv.osv): '''This function prints the delivery order''' assert len(ids) == 1, 'This option should only be used for a single id at a time' datas = { - 'model': 'stock.picking', - 'ids': ids, + 'model': 'stock.picking', + 'ids': ids, } return {'type': 'ir.actions.report.xml', 'report_name': 'stock.picking.list', 'datas': datas, 'nodestroy': True} @@ -793,8 +792,8 @@ class stock_picking(osv.osv): '''This function prints the picking list''' assert len(ids) == 1, 'This option should only be used for a single id at a time' datas = { - 'model': 'stock.picking', - 'ids': ids, + 'model': 'stock.picking', + 'ids': ids, } return {'type': 'ir.actions.report.xml', 'report_name': 'stock.picking.list.internal', 'datas': datas, 'nodestroy': True} @@ -841,7 +840,6 @@ class stock_picking(osv.osv): self.do_prepare_partial(cr, uid, [pick.id], context=None) return True - def action_cancel(self, cr, uid, ids, context=None): for pick in self.browse(cr, uid, ids, context=context): ids2 = [move.id for move in pick.move_lines] @@ -1204,14 +1202,13 @@ class stock_production_lot(osv.osv): @return: A dictionary of values """ quant_obj = self.pool.get("stock.quant") - move_obj = self.pool.get("stock.move") quants = quant_obj.search(cr, uid, [('lot_id', 'in', ids)], context=context) moves = set() for quant in quant_obj.browse(cr, uid, quants, context=context): moves |= {move.id for move in quant.history_ids} if moves: - return { - 'domain': "[('id','in',["+','.join(map(str, list(moves)))+"])]", + return { + 'domain': "[('id','in',[" + ','.join(map(str, list(moves))) + "])]", 'name': _('Traceability'), 'view_mode': 'tree,form', 'view_type': 'form', @@ -1220,7 +1217,6 @@ class stock_production_lot(osv.osv): 'type': 'ir.actions.act_window', } return False - # ---------------------------------------------------- @@ -1396,7 +1392,7 @@ class stock_move(osv.osv): 'price_unit': fields.float('Unit Price', help="Technical field used to record the product cost set by the user during a picking confirmation (when costing method used is 'average price' or 'real'). Value given in company currency and in product uom."), # as it's a technical field, we intentionally don't provide the digits attribute 'company_id': fields.many2one('res.company', 'Company', required=True, select=True), - 'split_from': fields.many2one('stock.move', string="Move Split From", select=True), + 'split_from': fields.many2one('stock.move', string="Move Split From", help="Technical field used to track the origin of a split move, which can be useful in case of debug"), 'backorder_id': fields.related('picking_id', 'backorder_id', type='many2one', relation="stock.picking", string="Back Order of", select=True), 'origin': fields.char("Source"), 'procure_method': fields.selection([('make_to_stock', 'Make to Stock'), ('make_to_order', 'Make to Order')], 'Procurement Method', required=True, help="Make to Stock: When needed, the product is taken from the stock or we wait for replenishment. \nMake to Order: When needed, the product is purchased or produced."), @@ -1803,7 +1799,6 @@ class stock_move(osv.osv): self._putaway_check(cr, uid, ids, context=context) return self.write(cr, uid, ids, {'state': 'assigned'}) - def check_tracking(self, cr, uid, move, lot_id, context=None): """ Checks if serial number is assigned to stock move or not and raise an error if it had to. """ @@ -1839,6 +1834,7 @@ class stock_move(osv.osv): fallback_domain = prev_quant_ids and [('id', 'not in', prev_quant_ids)] or [] #we always keep the quants already assigned and try to find the remaining quantity on quants not assigned only main_domain = [('reservation_id', '=', False), ('qty', '>', 0)] + #if the move is returned from another, restrict the choice of quants to the ones that follow the returned move if move.origin_returned_move_id: main_domain += [('history_ids', 'in', move.origin_returned_move_id.id)] #first try to find quants based on specific domains given by linked operations @@ -1906,8 +1902,6 @@ class stock_move(osv.osv): pickings.add(move.picking_id.id) qty = move.product_qty main_domain = [('qty', '>', 0)] - if move.origin_returned_move_id: - main_domain += [('history_ids', 'in', move.origin_returned_move_id.id)] prefered_domain = [('reservation_id', '=', move.id)] fallback_domain = [('reservation_id', '=', False)] #first, process the move per linked operation first because it may imply some specific domains to consider diff --git a/addons/stock/wizard/stock_return_picking.py b/addons/stock/wizard/stock_return_picking.py index a1f2ae51e9a..95703679ccb 100644 --- a/addons/stock/wizard/stock_return_picking.py +++ b/addons/stock/wizard/stock_return_picking.py @@ -43,15 +43,13 @@ class stock_return_picking(osv.osv_memory): _description = 'Return Picking' _columns = { 'product_return_moves': fields.one2many('stock.return.picking.line', 'wizard_id', 'Moves'), - 'move_dest_exists': fields.boolean('Chained Move Exists', readonly = True), - 'move_dest_method': fields.selection([('nothing', 'Do Nothing'), ('cancel', 'Split and Cancel Next Moves => Do not receive returned products again'), - ('create', 'Split and Create New Original Move => Receive returned products again later')], - string="With Chained Moves"), + 'move_dest_exists': fields.boolean('Chained Move Exists', readonly=True, help="Technical field used to hide 'move_dest_method' and help tooltip if not needed"), + 'move_dest_method': fields.selection([('cancel', 'Cancel'), ('create', 'Recreate'), ('nothing', 'Fix Manually')], string="Chained Picking Resolution"), } _defaults = { 'move_dest_method': 'nothing', - } + } def default_get(self, cr, uid, fields, context=None): """ @@ -72,24 +70,18 @@ class stock_return_picking(osv.osv_memory): pick = pick_obj.browse(cr, uid, record_id, context=context) quant_obj = self.pool.get("stock.quant") move_obj = self.pool.get("stock.move") - move_dest = False + chained_move_exist = False if pick: if pick.state != 'done': raise osv.except_osv(_('Warning!'), _("You may only return pickings that are Done!")) - #Check if chained moves are not done already. In the mean time, sum the quants in that location for move in pick.move_lines: if move.move_dest_id: - move_dest = True - #Backorder moves - moves = move_obj.search(cr, uid, [('split_from', '=', move.move_dest_id.id)], context=context) - moves = [move.move_dest_id] + move_obj.browse(cr, uid, moves, context=context) - if all([x.state in ['done'] for x in moves]): - raise osv.except_osv(_('Warning!'), _('You can not reverse when the chained moves are done!')) - #Search quants - quant_search = quant_obj.search(cr, uid, ['&', '&', ('history_ids', 'in', move.id), ('qty', '>', 0.0), - ('location_id', 'child_of', move.location_dest_id.id)], context=context) + chained_move_exist = True + #Sum the quants in that location that can be returned (they must belong to the moves that were included in the returned picking) qty = 0 + quant_search = quant_obj.search(cr, uid, [('history_ids', 'in', move.id), ('qty', '>', 0.0), + ('location_id', 'child_of', move.location_dest_id.id)], context=context) for quant in quant_obj.browse(cr, uid, quant_search, context=context): if not quant.reservation_id or quant.reservation_id.origin_returned_move_id.id != move.id: qty += quant.qty @@ -100,7 +92,7 @@ class stock_return_picking(osv.osv_memory): if 'product_return_moves' in fields: res.update({'product_return_moves': result1}) if 'move_dest_exists' in fields: - res.update({'move_dest_exists': move_dest}) + res.update({'move_dest_exists': chained_move_exist}) return res def _create_returns(self, cr, uid, ids, context=None): @@ -114,15 +106,14 @@ class stock_return_picking(osv.osv_memory): pick = pick_obj.browse(cr, uid, record_id, context=context) data = self.read(cr, uid, ids[0], context=context) returned_lines = 0 + + # Cancel assignment of existing chained assigned moves + moves_to_unreserve = [x.move_dest_id.id for x in pick.move_lines if x.move_dest_id and x.move_dest_id.quant_ids] + if moves_to_unreserve: + move_obj.do_unreserve(cr, uid, moves_to_unreserve, context=context) #Create new picking for returned products pick_type_id = pick.picking_type_id.return_picking_type_id and pick.picking_type_id.return_picking_type_id.id or pick.picking_type_id.id - - # Cancel assignment of existing chained assigned moves - moves_to_unreserve = [x.move_dest_id.id for x in pick.move_lines if x.move_dest_id and x.move_dest_id.state == "assigned"] - if moves_to_unreserve: - move_obj.do_unreserve(cr, uid, moves_to_unreserve, context=context) - new_picking = pick_obj.copy(cr, uid, pick.id, { 'move_lines': [], 'picking_type_id': pick_type_id, @@ -137,7 +128,7 @@ class stock_return_picking(osv.osv_memory): new_qty = data_get.quantity if new_qty: returned_lines += 1 - return_move = move_obj.copy(cr, uid, move.id, { + move_obj.copy(cr, uid, move.id, { 'product_id': data_get.product_id.id, 'product_uom_qty': new_qty, 'product_uos_qty': uom_obj._compute_qty(cr, uid, move.product_uom.id, new_qty, move.product_uos.id), diff --git a/addons/stock/wizard/stock_return_picking_view.xml b/addons/stock/wizard/stock_return_picking_view.xml index 8504fdbc239..e383ba3c9aa 100644 --- a/addons/stock/wizard/stock_return_picking_view.xml +++ b/addons/stock/wizard/stock_return_picking_view.xml @@ -41,12 +41,19 @@ stock.return.picking
-