1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from datetime import datetime
23 from dateutil.relativedelta import relativedelta
24 from openerp.osv import fields
25 from openerp.osv import osv
26 from openerp.tools.translate import _
28 class procurement_order(osv.osv):
29 _inherit = 'procurement.order'
31 'bom_id': fields.many2one('mrp.bom', 'BoM', ondelete='cascade', select=True),
32 'property_ids': fields.many2many('mrp.property', 'procurement_property_rel', 'procurement_id','property_id', 'Properties'),
33 'production_id': fields.many2one('mrp.production', 'Manufacturing Order'),
36 def _prepare_order_line_procurement(self, cr, uid, order, line, move_id, date_planned, context=None):
37 result = super(procurement_order, self)._prepare_order_line_procurement(cr, uid, order, line, move_id, date_planned, context)
38 result['property_ids'] = [(6, 0, [x.id for x in line.property_ids])]
41 def check_produce_product(self, cr, uid, procurement, context=None):
42 ''' Depict the capacity of the procurement workflow to produce products (not services)'''
45 def check_bom_exists(self, cr, uid, ids, context=None):
46 """ Finds the bill of material for the product from procurement order.
47 @return: True or False
49 for procurement in self.browse(cr, uid, ids, context=context):
50 product = procurement.product_id
51 properties = [x.id for x in procurement.property_ids]
52 bom_id = self.pool.get('mrp.bom')._bom_find(cr, uid, procurement.product_id.id, procurement.product_uom.id, properties)
54 cr.execute('update procurement_order set message=%s where id=%s', (_('No BoM defined for this product !'), procurement.id))
55 for (id, name) in self.name_get(cr, uid, procurement.id):
56 message = _("Procurement '%s' has an exception: 'No BoM defined for this product !'") % name
57 self.message_post(cr, uid, [procurement.id], body=message, context=context)
61 def check_conditions_confirm2wait(self, cr, uid, ids):
62 """ condition on the transition to go from 'confirm' activity to 'confirm_wait' activity """
63 res = super(procurement_order, self).check_conditions_confirm2wait(cr, uid, ids)
64 return res and not self.get_phantom_bom_id(cr, uid, ids)
66 def get_phantom_bom_id(self, cr, uid, ids, context=None):
67 for procurement in self.browse(cr, uid, ids, context=context):
68 if procurement.move_id and procurement.move_id.product_id.supply_method=='produce' \
69 and procurement.move_id.product_id.procure_method=='make_to_order':
70 phantom_bom_id = self.pool.get('mrp.bom').search(cr, uid, [
71 ('product_id', '=', procurement.move_id.product_id.id),
72 ('bom_id', '=', False),
73 ('type', '=', 'phantom')])
77 def action_produce_assign_product(self, cr, uid, ids, context=None):
78 """ This is action which call from workflow to assign production order to procurements
81 procurement_obj = self.pool.get('procurement.order')
82 res = procurement_obj.make_mo(cr, uid, ids, context=context)
84 return len(res) and res[0] or 0
86 def make_mo(self, cr, uid, ids, context=None):
87 """ Make Manufacturing(production) order from procurement
88 @return: New created Production Orders procurement wise
91 company = self.pool.get('res.users').browse(cr, uid, uid, context).company_id
92 production_obj = self.pool.get('mrp.production')
93 move_obj = self.pool.get('stock.move')
94 procurement_obj = self.pool.get('procurement.order')
95 for procurement in procurement_obj.browse(cr, uid, ids, context=context):
96 res_id = procurement.move_id.id
97 newdate = datetime.strptime(procurement.date_planned, '%Y-%m-%d %H:%M:%S') - relativedelta(days=procurement.product_id.produce_delay or 0.0)
98 newdate = newdate - relativedelta(days=company.manufacturing_lead)
99 produce_id = production_obj.create(cr, uid, {
100 'origin': procurement.origin,
101 'product_id': procurement.product_id.id,
102 'product_qty': procurement.product_qty,
103 'product_uom': procurement.product_uom.id,
104 'product_uos_qty': procurement.product_uos and procurement.product_uos_qty or False,
105 'product_uos': procurement.product_uos and procurement.product_uos.id or False,
106 'location_src_id': procurement.location_id.id,
107 'location_dest_id': procurement.location_id.id,
108 'bom_id': procurement.bom_id and procurement.bom_id.id or False,
109 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
110 'move_prod_id': res_id,
111 'company_id': procurement.company_id.id,
114 res[procurement.id] = produce_id
115 self.write(cr, uid, [procurement.id], {'state': 'running', 'production_id': produce_id})
116 bom_result = production_obj.action_compute(cr, uid,
117 [produce_id], properties=[x.id for x in procurement.property_ids])
118 production_obj.signal_button_confirm(cr, uid, [produce_id])
120 move_obj.write(cr, uid, [res_id],
121 {'location_id': procurement.location_id.id})
122 self.production_order_create_note(cr, uid, ids, context=context)
125 def production_order_create_note(self, cr, uid, ids, context=None):
126 for procurement in self.browse(cr, uid, ids, context=context):
127 body = _("Manufacturing Order <em>%s</em> created.") % ( procurement.production_id.name,)
128 self.message_post(cr, uid, [procurement.id], body=body, context=context)
131 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: