46558c734a8c00288171a95dd5476bd676c61176
[odoo/odoo.git] / addons / stock_location / stock_location.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 fields, osv
23 from datetime import *
24 from dateutil.relativedelta import relativedelta
25 from openerp.tools.translate import _
26
27 class stock_location_route(osv.osv):
28     _name = 'stock.location.route'
29     _description = "Inventory Routes"
30     _order = 'sequence'
31     _columns = {
32         'name': fields.char('Route Name', required=True),
33         'sequence': fields.integer('Sequence'),
34         'pull_ids': fields.one2many('procurement.rule', 'route_id', 'Pull Rules'),
35         'push_ids': fields.one2many('stock.location.path', 'route_id', 'Push Rules'),
36     }
37     _defaults = {
38         'sequence': lambda self,cr,uid,ctx: 0,
39     }
40
41 class stock_location_path(osv.osv):
42     _name = "stock.location.path"
43     _description = "Pushed Flows"
44     _columns = {
45         'name': fields.char('Operation', size=64),
46         'company_id': fields.many2one('res.company', 'Company'),
47         'route_id': fields.many2one('stock.location.route', 'Route'),
48
49         'product_id' : fields.many2one('product.product', 'Products', ondelete='cascade', select=1),
50
51         'location_from_id' : fields.many2one('stock.location', 'Source Location', ondelete='cascade', select=1, required=True),
52         'location_dest_id' : fields.many2one('stock.location', 'Destination Location', ondelete='cascade', select=1, required=True),
53         'delay': fields.integer('Delay (days)', help="Number of days to do this transition"),
54         'invoice_state': fields.selection([
55             ("invoiced", "Invoiced"),
56             ("2binvoiced", "To Be Invoiced"),
57             ("none", "Not Applicable")], "Invoice Status",
58             required=True,),
59         'picking_type': fields.selection([('out','Sending Goods'),('in','Getting Goods'),('internal','Internal')], 'Shipping Type', required=True, select=True, help="Depending on the company, choose whatever you want to receive or send products"),
60         'auto': fields.selection(
61             [('auto','Automatic Move'), ('manual','Manual Operation'),('transparent','Automatic No Step Added')],
62             'Automatic Move',
63             required=True, select=1,
64             help="This is used to define paths the product has to follow within the location tree.\n" \
65                 "The 'Automatic Move' value will create a stock move after the current one that will be "\
66                 "validated automatically. With 'Manual Operation', the stock move has to be validated "\
67                 "by a worker. With 'Automatic No Step Added', the location is replaced in the original move."
68         ),
69     }
70     _defaults = {
71         'auto': 'auto',
72         'delay': 1,
73         'invoice_state': 'none',
74         'picking_type': 'internal',
75     }
76     def _apply(self, cr, uid, rule, move, context=None):
77         move_obj = self.pool.get('stock.move')
78         newdate = (datetime.strptime(move.date, '%Y-%m-%d %H:%M:%S') + relativedelta(days=rule.delay or 0)).strftime('%Y-%m-%d')
79         if rule.auto=='transparent':
80             self.write(cr, uid, [move.id], {
81                 'date': newdate,
82                 'location_dest_id': rule.location_dest_id.id
83             })
84             vals = {}
85 # TODO journal_id was to be removed?
86 #             if route.journal_id:
87 #                 vals['stock_journal_id'] = route.journal_id.id
88             vals['type'] = rule.picking_type
89             if rule.location_dest_id.id<>move.location_dest_id.id:
90                 move_obj._push_apply(self, cr, uid, move.id, context)
91             return move.id
92         else:
93             move_id = move_obj.copy(cr, uid, move.id, {
94                 'location_id': move.location_dest_id.id,
95                 'location_dest_id': rule.location_dest_id.id,
96                 'date': time.strftime('%Y-%m-%d'),
97                 'company_id': rule.company_id.id,
98                 'date_expected': newdate,
99             })
100             move_obj.write(cr, uid, [move.id], {
101                 'move_dest_id': move_id,
102             })
103             move_obj.action_confirm(self, cr, uid, [move_id], context=None)
104             return move_id
105
106
107 class procurement_rule(osv.osv):
108     _inherit = 'procurement.rule'
109     _columns = {
110         'route_id': fields.many2one('stock.location.route', 'Route',
111             help="If route_id is False, the rule is global"),
112         'delay': fields.integer('Number of Hours'),
113         'procure_method': fields.selection([('make_to_stock','Make to Stock'),('make_to_order','Make to Order')], 'Procure Method', required=True, help="'Make to Stock': When needed, take from the stock or wait until re-supplying. 'Make to Order': When needed, purchase or produce for the procurement request."),
114         #'type_proc': fields.selection([('produce','Produce'),('buy','Buy'),('move','Move')], 'Type of Procurement', required=True),
115         'partner_address_id': fields.many2one('res.partner', 'Partner Address'),
116         'invoice_state': fields.selection([
117             ("invoiced", "Invoiced"),
118             ("2binvoiced", "To Be Invoiced"),
119             ("none", "Not Applicable")], "Invoice Status",
120             required=True,),
121     }
122     _defaults = {
123         'procure_method': 'make_to_stock',
124         'invoice_state': 'none',
125     }
126
127
128
129
130
131 class procurement_order(osv.osv):
132     _inherit = 'procurement.order'
133     
134     def _run_move_create(self, cr, uid, procurement, context=None):
135         d = super(procurement_order, self)._run_move_create(cr, uid, procurement, context=context)
136         if procurement.move_dest_id:
137             date = procurement.move_dest_id.date
138         else:
139             date = procurement.date_planned
140         procure_method = procurement.rule_id and procurement.rule_id.procure_method or 'make_to_stock',
141         newdate = (datetime.strptime(date, '%Y-%m-%d %H:%M:%S') - relativedelta(days=procurement.rule_id.delay or 0)).strftime('%Y-%m-%d %H:%M:%S')
142         d.update({
143             'date': newdate,
144             'procure_method': procure_method, 
145         })
146         return d
147
148     # TODO: implement using routes on products
149     def _find_suitable_rule(self, cr, uid, procurement, context=None):
150         res = False
151         if procurement.location_id:
152             rule_obj = self.pool.get('procurement.rule')
153             route_ids = [x.id for x in procurement.product_id.route_ids]
154             res = rule_obj.search(cr, uid, [('location_id', '=', procurement.location_id.id), ('route_id', 'in', route_ids), ], context=context)
155         return res and res[0] or super(procurement_order, self)._find_suitable_rule(cr, uid, procurement, context=context)
156
157 class product_putaway_strategy(osv.osv):
158     _name = 'product.putaway'
159     _description = 'Put Away Strategy'
160     _columns = {
161         'product_categ_id':fields.many2one('product.category', 'Product Category', required=True),
162         'location_id': fields.many2one('stock.location','Parent Location', help="Parent Destination Location from which a child bin location needs to be chosen", required=True), #domain=[('type', '=', 'parent')], 
163         'method': fields.selection([('empty', 'Empty'), ('fixed', 'Fixed Location')], "Method", required = True),
164     }
165
166 # TODO: move this on stock module
167
168 class product_removal_strategy(osv.osv):
169     _name = 'product.removal'
170     _description = 'Removal Strategy'
171     _order = 'sequence'
172     _columns = {
173         'product_categ_id': fields.many2one('product.removal', 'Category', required=True), 
174         'sequence': fields.integer('Sequence'),
175         'location_id': fields.many2one('stock.location', 'Locations', required=True),
176     }
177
178 class product_product(osv.osv):
179     _inherit = 'product.product'
180     _columns = {
181         'route_ids': fields.many2many('stock.location.route', 'stock_location_route_product', 'product_id', 'route_id', 'Routes'),
182     }
183
184 class product_category(osv.osv):
185     _inherit = 'product.category'
186     _columns = {
187         'route_ids': fields.many2many('stock.location.route', 'stock_location_route_categ', 'categ_id', 'route_id', 'Routes'),
188         #'removal_strategy_ids': fields.one2many('product.removal', 'product_categ_id', 'Removal Strategies'),
189         #'putaway_strategy_ids': fields.one2many('product.putaway', 'product_categ_id', 'Put Away Strategies'),
190     }
191
192
193 class stock_move_putaway(osv.osv):
194     _name = 'stock.move.putaway'
195     _description = 'Proposed Destination'
196     _columns = {
197         'move_id': fields.many2one('stock.move', required=True),
198         'location_id': fields.many2one('stock.location', 'Location', required=True),
199         'lot_id': fields.many2one('stock.production.lot', 'Lot'),
200         'quantity': fields.float('Quantity', required=True),
201     }
202
203 class stock_move(osv.osv):
204     _inherit = 'stock.move'
205     _columns = {
206         'cancel_cascade': fields.boolean('Cancel Cascade', help='If checked, when this move is cancelled, cancel the linked move too'),
207         'putaway_ids': fields.one2many('stock.move.putaway', 'move_id', 'Put Away Suggestions'), 
208     }
209         
210         
211     # TODO: reimplement this
212 #     def _pull_apply(self, cr, uid, moves, context):
213 #         # Create a procurement is MTO on stock.move
214 #         # Call _assign on procurement
215 #         for move in moves:
216 #             #If move is MTO, then you should create a procurement
217 #             #Search for original rule
218 #             #Then change procurement to stock
219 #             for route in move.product_id.route_ids:
220 #                 found = False
221 #                 for rule in route.pull_ids:
222 #                     if rule.location_id.id == move.location_dest_id.id and move.procure_method == "make_to_order":
223 #                         self._create_procurement(cr, uid, rule, move, context=context)
224 #                         found = True
225 #                         break
226 #                 if found: break
227 #         return True
228
229     def _push_apply(self, cr, uid, moves, context):
230         for move in moves:
231             for route in move.product_id.route_ids:
232                 found = False
233                 for rule in route.push_ids:
234                     if rule.location_from_id.id == move.location_dest_id.id:
235                         self.pool.get('stock.location.path')._apply(cr, uid, rule, move, context=context)
236                         found = True
237                         break
238                 if found: break
239         return True
240
241     # Create the stock.move.putaway records
242     def _putaway_apply(self,cr, uid, ids, context=None):
243         for move in self.browse(cr, uid, ids, context=context):
244             res = self.pool.get('stock.location').get_putaway_strategy(cr, uid, move.location_dest_id.id, move.product_id.id, context=context)
245             if res:
246                 raise 'put away strategies not implemented yet!'
247         return True
248
249     def action_assign(self, cr, uid, ids, context=None):
250        result = super(stock_move, self).action_assign(cr, uid, ids, context=context)
251        self._putaway_apply(cr, uid, ids, context=context)
252        return result
253
254     def action_confirm(self, cr, uid, ids, context=None):
255         result = super(stock_move, self).action_confirm(cr, uid, ids, context)
256         moves = self.browse(cr, uid, ids, context=context)
257         self._push_apply(cr, uid, moves, context=context)
258         return result
259
260 class stock_location(osv.osv):
261     _inherit = 'stock.location'
262     _columns = {
263         'removal_strategy_ids': fields.one2many('product.removal', 'location_id', 'Removal Strategies'),
264         'putaway_strategy_ids': fields.one2many('product.putaway', 'location_id', 'Put Away Strategies'),
265     }
266     def get_putaway_strategy(self, cr, uid, id, product_id, context=None):
267         product = self.pool.get("product.product").browse(cr, uid, product_id, context=context)
268         strats = self.pool.get('product.removal').search(cr, uid, [('location_id','=',id), ('product_categ_id','child_of', product.categ_id.id)], context=context)
269         return strats and strats[0] or None
270
271     def get_removal_strategy(self, cr, uid, location, product, context=None):
272         pr = self.pool.get('product.removal')
273         categ = product.categ_id
274         categs = [categ.id, False]
275         while categ.parent_id:
276             categ = categ.parent_id
277             categs.append(categ.id)
278
279         result = pr.search(cr,uid, [
280             ('location_id', '=', location.id),
281             ('product_categ_id', 'in', categs)
282         ], context=context)
283         if result:
284             return pr.browse(cr, uid, result[0], context=context)
285         return super(stock_location, self).get_removal_strategy(cr, uid, id, product, context=context)
286
287 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: