112b233bf1fa86c89fa1c44bf167eb9bb6dc8cf2
[odoo/odoo.git] / addons / stock / wizard / stock_return_picking.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import osv, fields
23 from openerp.tools.translate import _
24 import openerp.addons.decimal_precision as dp
25
26 class stock_return_picking_line(osv.osv_memory):
27     _name = "stock.return.picking.line"
28     _rec_name = 'product_id'
29
30     _columns = {
31         'product_id': fields.many2one('product.product', string="Product", required=True),
32         'quantity': fields.float("Quantity", digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
33         'wizard_id': fields.many2one('stock.return.picking', string="Wizard"),
34         'move_id': fields.many2one('stock.move', "Move"),
35         'lot_id': fields.many2one('stock.production.lot', 'Serial Number', help="Used to choose the lot/serial number of the product returned"),
36     }
37
38
39 class stock_return_picking(osv.osv_memory):
40     _name = 'stock.return.picking'
41     _description = 'Return Picking'
42     _columns = {
43         'product_return_moves': fields.one2many('stock.return.picking.line', 'wizard_id', 'Moves'),
44         'move_dest_exists': fields.boolean('Chained Move Exists', readonly=True, help="Technical field used to hide help tooltip if not needed"),
45     }
46
47     def default_get(self, cr, uid, fields, context=None):
48         """
49          To get default values for the object.
50          @param self: The object pointer.
51          @param cr: A database cursor
52          @param uid: ID of the user currently logged in
53          @param fields: List of fields for which we want default values
54          @param context: A standard dictionary
55          @return: A dictionary with default values for all field in ``fields``
56         """
57         result1 = []
58         if context is None:
59             context = {}
60         res = super(stock_return_picking, self).default_get(cr, uid, fields, context=context)
61         record_id = context and context.get('active_id', False) or False
62         uom_obj = self.pool.get('product.uom')
63         pick_obj = self.pool.get('stock.picking')
64         pick = pick_obj.browse(cr, uid, record_id, context=context)
65         quant_obj = self.pool.get("stock.quant")
66         chained_move_exist = False
67         if pick:
68             if pick.state != 'done':
69                 raise osv.except_osv(_('Warning!'), _("You may only return pickings that are Done!"))
70
71             for move in pick.move_lines:
72                 if move.move_dest_id:
73                     chained_move_exist = True
74                 #Sum the quants in that location that can be returned (they should have been moved by the moves that were included in the returned picking)
75                 qty = 0
76                 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)
77                 for quant in quant_obj.browse(cr, uid, quant_search, context=context):
78                     if not quant.reservation_id or quant.reservation_id.origin_returned_move_id.id != move.id:
79                         qty += quant.qty
80                 qty = uom_obj._compute_qty(cr, uid, move.product_id.uom_id.id, qty, move.product_uom.id)
81                 result1.append({'product_id': move.product_id.id, 'quantity': qty, 'move_id': move.id})
82
83             if len(result1) == 0:
84                 raise osv.except_osv(_('Warning!'), _("No products to return (only lines in Done state and not fully returned yet can be returned)!"))
85             if 'product_return_moves' in fields:
86                 res.update({'product_return_moves': result1})
87             if 'move_dest_exists' in fields:
88                 res.update({'move_dest_exists': chained_move_exist})
89         return res
90
91     def _create_returns(self, cr, uid, ids, context=None):
92         if context is None:
93             context = {}
94         record_id = context and context.get('active_id', False) or False
95         move_obj = self.pool.get('stock.move')
96         pick_obj = self.pool.get('stock.picking')
97         uom_obj = self.pool.get('product.uom')
98         data_obj = self.pool.get('stock.return.picking.line')
99         pick = pick_obj.browse(cr, uid, record_id, context=context)
100         data = self.read(cr, uid, ids[0], context=context)
101         returned_lines = 0
102
103         # Cancel assignment of existing chained assigned moves
104         moves_to_unreserve = []
105         for move in pick.move_lines:
106             to_check_moves = [move.move_dest_id]
107             while to_check_moves:
108                 current_move = to_check_moves.pop()
109                 if current_move.state not in ('done', 'cancel') and current_move.reserved_quant_ids:
110                     moves_to_unreserve.append(current_move.id)
111                 split_move_ids = move_obj.search(cr, uid, [('split_from', '=', current_move.id)], context=context)
112                 if split_move_ids:
113                     to_check_moves += move_obj.browse(cr, uid, split_move_ids, context=context)
114
115         if moves_to_unreserve:
116             move_obj.do_unreserve(cr, uid, moves_to_unreserve, context=context)
117             #break the link between moves in order to be able to fix them later if needed
118             move_obj.write(cr, uid, moves_to_unreserve, {'move_orig_ids': False}, context=context)
119
120         #Create new picking for returned products
121         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
122         new_picking = pick_obj.copy(cr, uid, pick.id, {
123             'move_lines': [],
124             'picking_type_id': pick_type_id,
125             'state': 'draft',
126             'origin': pick.name,
127         }, context=context)
128
129         for data_get in data_obj.browse(cr, uid, data['product_return_moves'], context=context):
130             move = data_get.move_id
131             if not move:
132                 raise osv.except_osv(_('Warning !'), _("You have manually created product lines, please delete them to proceed"))
133             new_qty = data_get.quantity
134             if new_qty:
135                 returned_lines += 1
136                 move_obj.copy(cr, uid, move.id, {
137                     'product_id': data_get.product_id.id,
138                     'product_uom_qty': new_qty,
139                     'product_uos_qty': uom_obj._compute_qty(cr, uid, move.product_uom.id, new_qty, move.product_uos.id),
140                     'picking_id': new_picking,
141                     'state': 'draft',
142                     'location_id': move.location_dest_id.id,
143                     'location_dest_id': move.location_id.id,
144                     'origin_returned_move_id': move.id,
145                     'procure_method': 'make_to_stock',
146                     'restrict_lot_id': data_get.lot_id.id,
147                 })
148
149         if not returned_lines:
150             raise osv.except_osv(_('Warning!'), _("Please specify at least one non-zero quantity."))
151
152         pick_obj.action_confirm(cr, uid, [new_picking], context=context)
153         pick_obj.action_assign(cr, uid, [new_picking], context)
154         return new_picking, pick_type_id
155
156     def create_returns(self, cr, uid, ids, context=None):
157         """
158          Creates return picking.
159          @param self: The object pointer.
160          @param cr: A database cursor
161          @param uid: ID of the user currently logged in
162          @param ids: List of ids selected
163          @param context: A standard dictionary
164          @return: A dictionary which of fields with values.
165         """
166         new_picking_id, pick_type_id = self._create_returns(cr, uid, ids, context=context)
167         # Override the context to disable all the potential filters that could have been set previously
168         ctx = {
169             'search_default_picking_type_id': pick_type_id,
170             'search_default_draft': False,
171             'search_default_assigned': False,
172             'search_default_confirmed': False,
173             'search_default_ready': False,
174             'search_default_late': False,
175             'search_default_available': False,
176         }
177         return {
178             'domain': "[('id', 'in', [" + str(new_picking_id) + "])]",
179             'name': _('Returned Picking'),
180             'view_type': 'form',
181             'view_mode': 'tree,form',
182             'res_model': 'stock.picking',
183             'type': 'ir.actions.act_window',
184             'context': ctx,
185         }
186
187
188 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: