Remplace all occurences of my_browse_rec.product_id.product_tmpl_id.field to my_brows...
[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 osv import fields
25 from osv import osv
26 from tools.translate import _
27 import netsvc
28
29 class procurement_order(osv.osv):
30     _inherit = 'procurement.order'
31     _columns = {
32         'bom_id': fields.many2one('mrp.bom', 'BoM', ondelete='cascade', select=True),
33         'property_ids': fields.many2many('mrp.property', 'procurement_property_rel', 'procurement_id','property_id', 'Properties'),
34     }
35     
36     def check_produce_product(self, cr, uid, procurement, context=[]):
37         """ Finds the bill of material for the product from procurement order.
38         @return: True or False
39         """
40         properties = [x.id for x in procurement.property_ids]
41         bom_id = self.pool.get('mrp.bom')._bom_find(cr, uid, procurement.product_id.id, procurement.product_uom.id, properties)
42         if not bom_id:
43             cr.execute('update procurement_order set message=%s where id=%s', (_('No BoM defined for this product !'), procurement.id))
44             for (id, name) in self.name_get(cr, uid, procurement.id):
45                 message = _("Procurement '%s' has an exception: 'No BoM defined for this product !'") % name
46                 self.message_append_note(cr, uid, [procurement.id], body=message, context=context)
47             return False
48         return True
49     
50     def get_phantom_bom_id(self, cr, uid, ids, context=None):
51         for procurement in self.browse(cr, uid, ids, context=context):
52             if procurement.move_id and procurement.move_id.product_id.supply_method=='produce' \
53                  and procurement.move_id.product_id.procure_method=='make_to_order':
54                     phantom_bom_id = self.pool.get('mrp.bom').search(cr, uid, [
55                         ('product_id', '=', procurement.move_id.product_id.id),
56                         ('bom_id', '=', False),
57                         ('type', '=', 'phantom')]) 
58                     return phantom_bom_id 
59         return False
60     
61     def action_produce_assign_product(self, cr, uid, ids, context=None):
62         """ This is action which call from workflow to assign production order to procurements
63         @return: True
64         """
65         procurement_obj = self.pool.get('procurement.order')
66         res = procurement_obj.make_mo(cr, uid, ids, context=context)
67         res = res.values()
68         return len(res) and res[0] or 0
69     
70     def make_mo(self, cr, uid, ids, context=None):
71         """ Make Manufacturing(production) order from procurement
72         @return: New created Production Orders procurement wise 
73         """
74         res = {}
75         company = self.pool.get('res.users').browse(cr, uid, uid, context).company_id
76         production_obj = self.pool.get('mrp.production')
77         move_obj = self.pool.get('stock.move')
78         wf_service = netsvc.LocalService("workflow")
79         procurement_obj = self.pool.get('procurement.order')
80         for procurement in procurement_obj.browse(cr, uid, ids, context=context):
81             res_id = procurement.move_id.id
82             newdate = datetime.strptime(procurement.date_planned, '%Y-%m-%d %H:%M:%S') - relativedelta(days=procurement.product_id.produce_delay or 0.0)
83             newdate = newdate - relativedelta(days=company.manufacturing_lead)
84             produce_id = production_obj.create(cr, uid, {
85                 'origin': procurement.origin,
86                 'product_id': procurement.product_id.id,
87                 'product_qty': procurement.product_qty,
88                 'product_uom': procurement.product_uom.id,
89                 'product_uos_qty': procurement.product_uos and procurement.product_uos_qty or False,
90                 'product_uos': procurement.product_uos and procurement.product_uos.id or False,
91                 'location_src_id': procurement.location_id.id,
92                 'location_dest_id': procurement.location_id.id,
93                 'bom_id': procurement.bom_id and procurement.bom_id.id or False,
94                 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
95                 'move_prod_id': res_id,
96                 'company_id': procurement.company_id.id,
97             })
98             res[procurement.id] = produce_id
99             self.write(cr, uid, [procurement.id], {'state': 'running'})
100             self.running_send_note(cr, uid, ids, context=context)
101             bom_result = production_obj.action_compute(cr, uid,
102                     [produce_id], properties=[x.id for x in procurement.property_ids])
103             wf_service.trg_validate(uid, 'mrp.production', produce_id, 'button_confirm', cr)
104             if res_id:
105                 move_obj.write(cr, uid, [res_id],
106                         {'location_id': procurement.location_id.id})
107         return res
108     
109 procurement_order()
110
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: