[IMP] Query should pass through orm and as such the refreshes can be removed in the...
[odoo/odoo.git] / addons / mrp / procurement.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 datetime import datetime
23 from dateutil.relativedelta import relativedelta
24 from openerp.osv import osv, fields
25 from openerp.tools.translate import _
26 from openerp import SUPERUSER_ID
27
28 class procurement_rule(osv.osv):
29     _inherit = 'procurement.rule'
30
31     def _get_action(self, cr, uid, context=None):
32         return [('manufacture', _('Manufacture'))] + super(procurement_rule, self)._get_action(cr, uid, context=context)
33
34
35 class procurement_order(osv.osv):
36     _inherit = 'procurement.order'
37     _columns = {
38         'bom_id': fields.many2one('mrp.bom', 'BoM', ondelete='cascade', select=True),
39         'property_ids': fields.many2many('mrp.property', 'procurement_property_rel', 'procurement_id','property_id', 'Properties'),
40         'production_id': fields.many2one('mrp.production', 'Manufacturing Order'),
41     }
42
43     def propagate_cancel(self, cr, uid, procurement, context=None):
44         if procurement.rule_id.action == 'manufacture' and procurement.production_id:
45             self.pool.get('mrp.production').action_cancel(cr, uid, [procurement.production_id.id], context=context)
46         return super(procurement_order, self).propagate_cancel(cr, uid, procurement, context=context)
47
48     def _run(self, cr, uid, procurement, context=None):
49         if procurement.rule_id and procurement.rule_id.action == 'manufacture':
50             #make a manufacturing order for the procurement
51             return self.make_mo(cr, uid, [procurement.id], context=context)[procurement.id]
52         return super(procurement_order, self)._run(cr, uid, procurement, context=context)
53
54     def _check(self, cr, uid, procurement, context=None):
55         if procurement.production_id and procurement.production_id.state == 'done':  # TOCHECK: no better method? 
56             return True
57         return super(procurement_order, self)._check(cr, uid, procurement, context=context)
58
59     def check_bom_exists(self, cr, uid, ids, context=None):
60         """ Finds the bill of material for the product from procurement order.
61         @return: True or False
62         """
63         for procurement in self.browse(cr, uid, ids, context=context):
64             properties = [x.id for x in procurement.property_ids]
65             bom_id = self.pool.get('mrp.bom')._bom_find(cr, uid, product_id=procurement.product_id.id,
66                                                         properties=properties, context=context)
67             if not bom_id:
68                 return False
69         return True
70
71     def make_mo(self, cr, uid, ids, context=None):
72         """ Make Manufacturing(production) order from procurement
73         @return: New created Production Orders procurement wise
74         """
75         res = {}
76         company = self.pool.get('res.users').browse(cr, uid, uid, context).company_id
77         production_obj = self.pool.get('mrp.production')
78         bom_obj = self.pool.get('mrp.bom')
79         procurement_obj = self.pool.get('procurement.order')
80         for procurement in procurement_obj.browse(cr, uid, ids, context=context):
81             if self.check_bom_exists(cr, uid, [procurement.id], context=context):
82                 if procurement.bom_id:
83                     bom_id = procurement.bom_id.id
84                     routing_id = procurement.bom_id.routing_id.id
85                 else:
86                     properties = [x.id for x in procurement.property_ids]
87                     bom_id = bom_obj._bom_find(cr, uid, product_id=procurement.product_id.id,
88                                                properties=properties, context=context)
89                     bom = bom_obj.browse(cr, uid, bom_id, context=context)
90                     routing_id = bom.routing_id.id
91
92                 res_id = procurement.move_dest_id and procurement.move_dest_id.id or False
93                 newdate = datetime.strptime(procurement.date_planned, '%Y-%m-%d %H:%M:%S') - relativedelta(days=procurement.product_id.produce_delay or 0.0)
94                 newdate = newdate - relativedelta(days=company.manufacturing_lead)
95                 #create the MO as SUPERUSER because the current user may not have the rights to do it (mto product launched by a sale for example)
96                 produce_id = production_obj.create(cr, SUPERUSER_ID, {
97                     'origin': procurement.origin,
98                     'product_id': procurement.product_id.id,
99                     'product_qty': procurement.product_qty,
100                     'product_uom': procurement.product_uom.id,
101                     'product_uos_qty': procurement.product_uos and procurement.product_uos_qty or False,
102                     'product_uos': procurement.product_uos and procurement.product_uos.id or False,
103                     'location_src_id': procurement.location_id.id,
104                     'location_dest_id': procurement.location_id.id,
105                     'bom_id': bom_id,
106                     'routing_id': routing_id,
107                     'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
108                     'move_prod_id': res_id,
109                     'company_id': procurement.company_id.id,
110                 })
111
112                 res[procurement.id] = produce_id
113                 self.write(cr, uid, [procurement.id], {'production_id': produce_id})
114                 self.production_order_create_note(cr, uid, procurement, context=context)
115                 production_obj.action_compute(cr, uid, [produce_id], properties=[x.id for x in procurement.property_ids])
116                 production_obj.signal_workflow(cr, uid, [produce_id], 'button_confirm')
117             else:
118                 res[procurement.id] = False
119                 self.message_post(cr, uid, [procurement.id], body=_("No BoM exists for this product!"), context=context)
120         return res
121
122     def production_order_create_note(self, cr, uid, procurement, context=None):
123         body = _("Manufacturing Order <em>%s</em> created.") % (procurement.production_id.name,)
124         self.message_post(cr, uid, [procurement.id], body=body, context=context)
125
126
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: