[IMP]:mrp:removed tabs
[odoo/odoo.git] / addons / mrp / mrp.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 osv import osv, fields
24 import decimal_precision as dp
25 from tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP
26 from tools.translate import _
27 import netsvc
28 import time
29 import tools
30
31
32 #----------------------------------------------------------
33 # Work Centers
34 #----------------------------------------------------------
35 # capacity_hour : capacity per hour. default: 1.0.
36 #          Eg: If 5 concurrent operations at one time: capacity = 5 (because 5 employees)
37 # unit_per_cycle : how many units are produced for one cycle
38
39 class mrp_workcenter(osv.osv):
40     _name = 'mrp.workcenter'
41     _description = 'Work Center'
42     _inherits = {'resource.resource':"resource_id"}
43     _columns = {
44         'note': fields.text('Description', help="Description of the Work Center. Explain here what's a cycle according to this Work Center."),
45         'capacity_per_cycle': fields.float('Capacity per Cycle', help="Number of operations this Work Center can do in parallel. If this Work Center represents a team of 5 workers, the capacity per cycle is 5."),
46         'time_cycle': fields.float('Time for 1 cycle (hour)', help="Time in hours for doing one cycle."),
47         'time_start': fields.float('Time before prod.', help="Time in hours for the setup."),
48         'time_stop': fields.float('Time after prod.', help="Time in hours for the cleaning."),
49         'costs_hour': fields.float('Cost per hour', help="Specify Cost of Work Center per hour."),
50         'costs_hour_account_id': fields.many2one('account.analytic.account', 'Hour Account', domain=[('type','<>','view')],
51             help="Complete this only if you want automatic analytic accounting entries on production orders."),
52         'costs_cycle': fields.float('Cost per cycle', help="Specify Cost of Work Center per cycle."),
53         'costs_cycle_account_id': fields.many2one('account.analytic.account', 'Cycle Account', domain=[('type','<>','view')],
54             help="Complete this only if you want automatic analytic accounting entries on production orders."),
55         'costs_journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal'),
56         'costs_general_account_id': fields.many2one('account.account', 'General Account', domain=[('type','<>','view')]),
57         'resource_id': fields.many2one('resource.resource','Resource', ondelete='cascade', required=True),
58         'product_id': fields.many2one('product.product','Work Center Product', help="Fill this product to track easily your production costs in the analytic accounting."),
59     }
60     _defaults = {
61         'capacity_per_cycle': 1.0,
62         'resource_type': 'material',
63      }
64
65     def on_change_product_cost(self, cr, uid, ids, product_id, context=None):
66         value = {}
67
68         if product_id:
69             cost = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
70             value = {'costs_hour': cost.standard_price}
71         return {'value': value}
72
73 mrp_workcenter()
74
75
76 class mrp_routing(osv.osv):
77     """
78     For specifying the routings of Work Centers.
79     """
80     _name = 'mrp.routing'
81     _description = 'Routing'
82     _columns = {
83         'name': fields.char('Name', size=64, required=True),
84         'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the routing without removing it."),
85         'code': fields.char('Code', size=8),
86
87         'note': fields.text('Description'),
88         'workcenter_lines': fields.one2many('mrp.routing.workcenter', 'routing_id', 'Work Centers'),
89
90         'location_id': fields.many2one('stock.location', 'Production Location',
91             help="Keep empty if you produce at the location where the finished products are needed." \
92                 "Set a location if you produce at a fixed location. This can be a partner location " \
93                 "if you subcontract the manufacturing operations."
94         ),
95         'company_id': fields.many2one('res.company', 'Company'),
96     }
97     _defaults = {
98         'active': lambda *a: 1,
99         'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'mrp.routing', context=context)
100     }
101 mrp_routing()
102
103 class mrp_routing_workcenter(osv.osv):
104     """
105     Defines working cycles and hours of a Work Center using routings.
106     """
107     _name = 'mrp.routing.workcenter'
108     _description = 'Work Center Usage'
109     _columns = {
110         'workcenter_id': fields.many2one('mrp.workcenter', 'Work Center', required=True),
111         'name': fields.char('Name', size=64, required=True),
112         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of routing Work Centers."),
113         'cycle_nbr': fields.float('Number of Cycles', required=True,
114             help="Number of iterations this work center has to do in the specified operation of the routing."),
115         'hour_nbr': fields.float('Number of Hours', required=True, help="Time in hours for this Work Center to achieve the operation of the specified routing."),
116         'routing_id': fields.many2one('mrp.routing', 'Parent Routing', select=True, ondelete='cascade',
117              help="Routing indicates all the Work Centers used, for how long and/or cycles." \
118                 "If Routing is indicated then,the third tab of a production order (Work Centers) will be automatically pre-completed."),
119         'note': fields.text('Description'),
120         'company_id': fields.related('routing_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
121     }
122     _defaults = {
123         'cycle_nbr': lambda *a: 1.0,
124         'hour_nbr': lambda *a: 0.0,
125     }
126 mrp_routing_workcenter()
127
128 class mrp_bom(osv.osv):
129     """
130     Defines bills of material for a product.
131     """
132     _name = 'mrp.bom'
133     _description = 'Bill of Material'
134     _inherit = ['mail.thread']
135
136     def _child_compute(self, cr, uid, ids, name, arg, context=None):
137         """ Gets child bom.
138         @param self: The object pointer
139         @param cr: The current row, from the database cursor,
140         @param uid: The current user ID for security checks
141         @param ids: List of selected IDs
142         @param name: Name of the field
143         @param arg: User defined argument
144         @param context: A standard dictionary for contextual values
145         @return:  Dictionary of values
146         """
147         result = {}
148         if context is None:
149             context = {}
150         bom_obj = self.pool.get('mrp.bom')
151         bom_id = context and context.get('active_id', False) or False
152         cr.execute('select id from mrp_bom')
153         if all(bom_id != r[0] for r in cr.fetchall()):
154             ids.sort()
155             bom_id = ids[0]
156         bom_parent = bom_obj.browse(cr, uid, bom_id, context=context)
157         for bom in self.browse(cr, uid, ids, context=context):
158             if (bom_parent) or (bom.id == bom_id):
159                 result[bom.id] = map(lambda x: x.id, bom.bom_lines)
160             else:
161                 result[bom.id] = []
162             if bom.bom_lines:
163                 continue
164             ok = ((name=='child_complete_ids') and (bom.product_id.supply_method=='produce'))
165             if (bom.type=='phantom' or ok):
166                 sids = bom_obj.search(cr, uid, [('bom_id','=',False),('product_id','=',bom.product_id.id)])
167                 if sids:
168                     bom2 = bom_obj.browse(cr, uid, sids[0], context=context)
169                     result[bom.id] += map(lambda x: x.id, bom2.bom_lines)
170
171         return result
172
173     def _compute_type(self, cr, uid, ids, field_name, arg, context=None):
174         """ Sets particular method for the selected bom type.
175         @param field_name: Name of the field
176         @param arg: User defined argument
177         @return:  Dictionary of values
178         """
179         res = dict.fromkeys(ids, False)
180         for line in self.browse(cr, uid, ids, context=context):
181             if line.type == 'phantom' and not line.bom_id:
182                 res[line.id] = 'set'
183                 continue
184             if line.bom_lines or line.type == 'phantom':
185                 continue
186             if line.product_id.supply_method == 'produce':
187                 if line.product_id.procure_method == 'make_to_stock':
188                     res[line.id] = 'stock'
189                 else:
190                     res[line.id] = 'order'
191         return res
192
193     _columns = {
194         'name': fields.char('Name', size=64, required=True),
195         'code': fields.char('Reference', size=16),
196         'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the bills of material without removing it."),
197         'type': fields.selection([('normal','Normal BoM'),('phantom','Sets / Phantom')], 'BoM Type', required=True,
198                                  help= "If a sub-product is used in several products, it can be useful to create its own BoM. "\
199                                  "Though if you don't want separated production orders for this sub-product, select Set/Phantom as BoM type. "\
200                                  "If a Phantom BoM is used for a root product, it will be sold and shipped as a set of components, instead of being produced."),
201         'method': fields.function(_compute_type, string='Method', type='selection', selection=[('',''),('stock','On Stock'),('order','On Order'),('set','Set / Pack')]),
202         'date_start': fields.date('Valid From', help="Validity of this BoM or component. Keep empty if it's always valid."),
203         'date_stop': fields.date('Valid Until', help="Validity of this BoM or component. Keep empty if it's always valid."),
204         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of bills of material."),
205         'position': fields.char('Internal Reference', size=64, help="Reference to a position in an external plan."),
206         'product_id': fields.many2one('product.product', 'Product', required=True),
207         'product_uos_qty': fields.float('Product UOS Qty'),
208         'product_uos': fields.many2one('product.uom', 'Product UOS', help="Product UOS (Unit of Sale) is the unit of measurement for the invoicing and promotion of stock."),
209         'product_qty': fields.float('Product Qty', required=True, digits_compute=dp.get_precision('Product Unit of Measure')),
210         'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', required=True, help="Unit of Measure (Unit of Measure) is the unit of measurement for the inventory control"),
211         'product_rounding': fields.float('Product Rounding', help="Rounding applied on the product quantity."),
212         'product_efficiency': fields.float('Manufacturing Efficiency', required=True, help="A factor of 0.9 means a loss of 10% within the production process."),
213         'bom_lines': fields.one2many('mrp.bom', 'bom_id', 'BoM Lines'),
214         'bom_id': fields.many2one('mrp.bom', 'Parent BoM', ondelete='cascade', select=True),
215         'routing_id': fields.many2one('mrp.routing', 'Routing', help="The list of operations (list of work centers) to produce the finished product. The routing is mainly used to compute work center costs during operations and to plan future loads on work centers based on production planning."),
216         'property_ids': fields.many2many('mrp.property', 'mrp_bom_property_rel', 'bom_id','property_id', 'Properties'),
217         'revision_ids': fields.one2many('mrp.bom.revision', 'bom_id', 'BoM Revisions'),
218         'child_complete_ids': fields.function(_child_compute, relation='mrp.bom', string="BoM Hierarchy", type='many2many'),
219         'company_id': fields.many2one('res.company','Company',required=True),
220     }
221     _defaults = {
222         'active': lambda *a: 1,
223         'product_efficiency': lambda *a: 1.0,
224         'product_qty': lambda *a: 1.0,
225         'product_rounding': lambda *a: 0.0,
226         'type': lambda *a: 'normal',
227         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'mrp.bom', context=c),
228     }
229     _order = "sequence"
230     _sql_constraints = [
231         ('bom_qty_zero', 'CHECK (product_qty>0)',  'All product quantities must be greater than 0.\n' \
232             'You should install the mrp_subproduct module if you want to manage extra products on BoMs !'),
233     ]
234
235     def _check_recursion(self, cr, uid, ids, context=None):
236         level = 100
237         while len(ids):
238             cr.execute('select distinct bom_id from mrp_bom where id IN %s',(tuple(ids),))
239             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
240             if not level:
241                 return False
242             level -= 1
243         return True
244
245     def _check_product(self, cr, uid, ids, context=None):
246         all_prod = []
247         boms = self.browse(cr, uid, ids, context=context)
248         def check_bom(boms):
249             res = True
250             for bom in boms:
251                 if bom.product_id.id in all_prod:
252                     res = res and False
253                 all_prod.append(bom.product_id.id)
254                 lines = bom.bom_lines
255                 if lines:
256                     res = res and check_bom([bom_id for bom_id in lines if bom_id not in boms])
257             return res
258         return check_bom(boms)
259
260     _constraints = [
261         (_check_recursion, 'Error ! You cannot create recursive BoM.', ['parent_id']),
262         (_check_product, 'BoM line product should not be same as BoM product.', ['product_id']),
263     ]
264
265     def onchange_product_id(self, cr, uid, ids, product_id, name, context=None):
266         """ Changes UoM and name if product_id changes.
267         @param name: Name of the field
268         @param product_id: Changed product_id
269         @return:  Dictionary of changed values
270         """
271         if product_id:
272             prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
273             return {'value': {'name': prod.name, 'product_uom': prod.uom_id.id}}
274         return {}
275
276     def _bom_find(self, cr, uid, product_id, product_uom, properties=[]):
277         """ Finds BoM for particular product and product uom.
278         @param product_id: Selected product.
279         @param product_uom: Unit of measure of a product.
280         @param properties: List of related properties.
281         @return: False or BoM id.
282         """
283         cr.execute('select id from mrp_bom where product_id=%s and bom_id is null order by sequence', (product_id,))
284         ids = map(lambda x: x[0], cr.fetchall())
285         max_prop = 0
286         result = False
287         for bom in self.pool.get('mrp.bom').browse(cr, uid, ids):
288             prop = 0
289             for prop_id in bom.property_ids:
290                 if prop_id.id in properties:
291                     prop += 1
292             if (prop > max_prop) or ((max_prop == 0) and not result):
293                 result = bom.id
294                 max_prop = prop
295         return result
296
297     def _bom_explode(self, cr, uid, bom, factor, properties=[], addthis=False, level=0, routing_id=False):
298         """ Finds Products and Work Centers for related BoM for manufacturing order.
299         @param bom: BoM of particular product.
300         @param factor: Factor of product UoM.
301         @param properties: A List of properties Ids.
302         @param addthis: If BoM found then True else False.
303         @param level: Depth level to find BoM lines starts from 10.
304         @return: result: List of dictionaries containing product details.
305                  result2: List of dictionaries containing Work Center details.
306         """
307         routing_obj = self.pool.get('mrp.routing')
308         factor = factor / (bom.product_efficiency or 1.0)
309         factor = rounding(factor, bom.product_rounding)
310         if factor < bom.product_rounding:
311             factor = bom.product_rounding
312         result = []
313         result2 = []
314         phantom = False
315         if bom.type == 'phantom' and not bom.bom_lines:
316             newbom = self._bom_find(cr, uid, bom.product_id.id, bom.product_uom.id, properties)
317
318             if newbom:
319                 res = self._bom_explode(cr, uid, self.browse(cr, uid, [newbom])[0], factor*bom.product_qty, properties, addthis=True, level=level+10)
320                 result = result + res[0]
321                 result2 = result2 + res[1]
322                 phantom = True
323             else:
324                 phantom = False
325         if not phantom:
326             if addthis and not bom.bom_lines:
327                 result.append(
328                 {
329                     'name': bom.product_id.name,
330                     'product_id': bom.product_id.id,
331                     'product_qty': bom.product_qty * factor,
332                     'product_uom': bom.product_uom.id,
333                     'product_uos_qty': bom.product_uos and bom.product_uos_qty * factor or False,
334                     'product_uos': bom.product_uos and bom.product_uos.id or False,
335                 })
336             routing = (routing_id and routing_obj.browse(cr, uid, routing_id)) or bom.routing_id or False
337             if routing:
338                 for wc_use in routing.workcenter_lines:
339                     wc = wc_use.workcenter_id
340                     d, m = divmod(factor, wc_use.workcenter_id.capacity_per_cycle)
341                     mult = (d + (m and 1.0 or 0.0))
342                     cycle = mult * wc_use.cycle_nbr
343                     result2.append({
344                         'name': tools.ustr(wc_use.name) + ' - '  + tools.ustr(bom.product_id.name),
345                         'workcenter_id': wc.id,
346                         'sequence': level+(wc_use.sequence or 0),
347                         'cycle': cycle,
348                         'hour': float(wc_use.hour_nbr*mult + ((wc.time_start or 0.0)+(wc.time_stop or 0.0)+cycle*(wc.time_cycle or 0.0)) * (wc.time_efficiency or 1.0)),
349                     })
350             for bom2 in bom.bom_lines:
351                 res = self._bom_explode(cr, uid, bom2, factor, properties, addthis=True, level=level+10)
352                 result = result + res[0]
353                 result2 = result2 + res[1]
354         return result, result2
355
356     def copy_data(self, cr, uid, id, default=None, context=None):
357         if default is None:
358             default = {}
359         bom_data = self.read(cr, uid, id, [], context=context)
360         default.update({'name': bom_data['name'] + ' ' + _('Copy'), 'bom_id':False})
361         return super(mrp_bom, self).copy_data(cr, uid, id, default, context=context)
362
363     def create(self, cr, uid, vals, context=None):
364         obj_id = super(mrp_bom, self).create(cr, uid, vals, context=context)
365         self.create_send_note(cr, uid, [obj_id], context=context)
366         return obj_id
367
368     def create_send_note(self, cr, uid, ids, context=None):
369         prod_obj = self.pool.get('product.product')
370         for obj in self.browse(cr, uid, ids, context=context):
371             for prod in prod_obj.browse(cr, uid, [obj.product_id], context=context):
372                 self.message_append_note(cr, uid, [obj.id], body=_("Bill of Material has been <b>created</b> for <em>%s</em> product.") % (prod.id.name_template), context=context)
373         return True
374
375 mrp_bom()
376
377 class mrp_bom_revision(osv.osv):
378     _name = 'mrp.bom.revision'
379     _description = 'Bill of Material Revision'
380     _columns = {
381         'name': fields.char('Modification name', size=64, required=True),
382         'description': fields.text('Description'),
383         'date': fields.date('Modification Date'),
384         'indice': fields.char('Revision', size=16),
385         'last_indice': fields.char('last indice', size=64),
386         'author_id': fields.many2one('res.users', 'Author'),
387         'bom_id': fields.many2one('mrp.bom', 'BoM', select=True),
388     }
389
390     _defaults = {
391         'author_id': lambda x, y, z, c: z,
392         'date': fields.date.context_today,
393     }
394
395 mrp_bom_revision()
396
397 def rounding(f, r):
398     import math
399     if not r:
400         return f
401     return math.ceil(f / r) * r
402
403 class mrp_production(osv.osv):
404     """
405     Production Orders / Manufacturing Orders
406     """
407     _name = 'mrp.production'
408     _description = 'Manufacturing Order'
409     _date_name  = 'date_planned'
410     _inherit = ['ir.needaction_mixin', 'mail.thread']
411
412     def _production_calc(self, cr, uid, ids, prop, unknow_none, context=None):
413         """ Calculates total hours and total no. of cycles for a production order.
414         @param prop: Name of field.
415         @param unknow_none:
416         @return: Dictionary of values.
417         """
418         result = {}
419         for prod in self.browse(cr, uid, ids, context=context):
420             result[prod.id] = {
421                 'hour_total': 0.0,
422                 'cycle_total': 0.0,
423             }
424             for wc in prod.workcenter_lines:
425                 result[prod.id]['hour_total'] += wc.hour
426                 result[prod.id]['cycle_total'] += wc.cycle
427         return result
428
429     def _production_date_end(self, cr, uid, ids, prop, unknow_none, context=None):
430         """ Finds production end date.
431         @param prop: Name of field.
432         @param unknow_none:
433         @return: Dictionary of values.
434         """
435         result = {}
436         for prod in self.browse(cr, uid, ids, context=context):
437             result[prod.id] = prod.date_planned
438         return result
439
440     def _production_date(self, cr, uid, ids, prop, unknow_none, context=None):
441         """ Finds production planned date.
442         @param prop: Name of field.
443         @param unknow_none:
444         @return: Dictionary of values.
445         """
446         result = {}
447         for prod in self.browse(cr, uid, ids, context=context):
448             result[prod.id] = prod.date_planned[:10]
449         return result
450
451     _columns = {
452         'name': fields.char('Reference', size=64, required=True),
453         'origin': fields.char('Source Document', size=64, help="Reference of the document that generated this production order request."),
454         'priority': fields.selection([('0','Not urgent'),('1','Normal'),('2','Urgent'),('3','Very Urgent')], 'Priority', select=True),
455
456         'product_id': fields.many2one('product.product', 'Product', required=True, readonly=True, states={'draft':[('readonly',False)]}),
457         'product_qty': fields.float('Product Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True, states={'draft':[('readonly',False)]}, readonly=True),
458         'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', required=True, states={'draft':[('readonly',False)]}, readonly=True),
459         'product_uos_qty': fields.float('Product UoS Qty', states={'draft':[('readonly',False)]}, readonly=True),
460         'product_uos': fields.many2one('product.uom', 'Product UoS', states={'draft':[('readonly',False)]}, readonly=True),
461
462         'location_src_id': fields.many2one('stock.location', 'Raw Materials Location', required=True,
463             readonly=True, states={'draft':[('readonly',False)]}, help="Location where the system will look for components."),
464         'location_dest_id': fields.many2one('stock.location', 'Finished Products Location', required=True,
465             readonly=True, states={'draft':[('readonly',False)]}, help="Location where the system will stock the finished products."),
466
467         'date_planned_end': fields.function(_production_date_end, type='date', string='Scheduled End Date'),
468         'date_planned_date': fields.function(_production_date, type='date', string='Scheduled Date'),
469         'date_planned': fields.datetime('Scheduled Date', required=True, select=1),
470         'date_start': fields.datetime('Start Date', select=True),
471         'date_finished': fields.datetime('End Date', select=True),
472
473         'bom_id': fields.many2one('mrp.bom', 'Bill of Material', domain=[('bom_id','=',False)], readonly=True, states={'draft':[('readonly',False)]}),
474         'routing_id': fields.many2one('mrp.routing', string='Routing', on_delete='set null', readonly=True, states={'draft':[('readonly',False)]}, help="The list of operations (list of work centers) to produce the finished product. The routing is mainly used to compute work center costs during operations and to plan future loads on work centers based on production plannification."),
475         'picking_id': fields.many2one('stock.picking', 'Picking List', readonly=True, ondelete="restrict",
476             help="This is the Internal Picking List that brings the finished product to the production plan"),
477         'move_prod_id': fields.many2one('stock.move', 'Product Move', readonly=True),
478         'move_lines': fields.many2many('stock.move', 'mrp_production_move_ids', 'production_id', 'move_id', 'Products to Consume', domain=[('state','not in', ('done', 'cancel'))], states={'done':[('readonly',True)]}),
479         'move_lines2': fields.many2many('stock.move', 'mrp_production_move_ids', 'production_id', 'move_id', 'Consumed Products', domain=[('state','in', ('done', 'cancel'))]),
480         'move_created_ids': fields.one2many('stock.move', 'production_id', 'Products to Produce', domain=[('state','not in', ('done', 'cancel'))], states={'done':[('readonly',True)]}),
481         'move_created_ids2': fields.one2many('stock.move', 'production_id', 'Produced Products', domain=[('state','in', ('done', 'cancel'))]),
482         'product_lines': fields.one2many('mrp.production.product.line', 'production_id', 'Scheduled goods'),
483         'workcenter_lines': fields.one2many('mrp.production.workcenter.line', 'production_id', 'Work Centers Utilisation'),
484         'state': fields.selection([('draft','New'),('cancel','Cancelled'),('picking_except', 'Picking Exception'),('confirmed','Waiting Goods'),('ready','Ready to Produce'),('in_production','Production Started'),('done','Done')],'Status', readonly=True,
485                                     help='When the production order is created the state is set to \'Draft\'.\n If the order is confirmed the state is set to \'Waiting Goods\'.\n If any exceptions are there, the state is set to \'Picking Exception\'.\
486                                     \nIf the stock is available then the state is set to \'Ready to Produce\'.\n When the production gets started then the state is set to \'In Production\'.\n When the production is over, the state is set to \'Done\'.'),
487         'hour_total': fields.function(_production_calc, type='float', string='Total Hours', multi='workorder', store=True),
488         'cycle_total': fields.function(_production_calc, type='float', string='Total Cycles', multi='workorder', store=True),
489         'user_id':fields.many2one('res.users', 'Responsible'),
490         'company_id': fields.many2one('res.company','Company',required=True),
491     }
492     _defaults = {
493         'priority': lambda *a: '1',
494         'state': lambda *a: 'draft',
495         'date_planned': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
496         'product_qty':  lambda *a: 1.0,
497         'name': lambda x, y, z, c: x.pool.get('ir.sequence').get(y, z, 'mrp.production') or '/',
498         'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'mrp.production', context=c),
499     }
500     _sql_constraints = [
501         ('name_uniq', 'unique(name, company_id)', 'Reference must be unique per Company!'),
502     ]
503     _order = 'priority desc, date_planned asc';
504
505     def _check_qty(self, cr, uid, ids, context=None):
506         for order in self.browse(cr, uid, ids, context=context):
507             if order.product_qty <= 0:
508                 return False
509         return True
510
511     _constraints = [
512         (_check_qty, 'Order quantity cannot be negative or zero!', ['product_qty']),
513     ]
514
515     def create(self, cr, uid, vals, context=None):
516         obj_id = super(mrp_production, self).create(cr, uid, vals, context=context)
517         self.create_send_note(cr, uid, [obj_id], context=context)
518         return obj_id
519
520     def unlink(self, cr, uid, ids, context=None):
521         for production in self.browse(cr, uid, ids, context=context):
522             if production.state not in ('draft', 'cancel'):
523                 raise osv.except_osv(_('Invalid action !'), _('Cannot delete a manufacturing order in state \'%s\'') % production.state)
524         return super(mrp_production, self).unlink(cr, uid, ids, context=context)
525
526     def copy(self, cr, uid, id, default=None, context=None):
527         if default is None:
528             default = {}
529         default.update({
530             'name': self.pool.get('ir.sequence').get(cr, uid, 'mrp.production'),
531             'move_lines' : [],
532             'move_lines2' : [],
533             'move_created_ids' : [],
534             'move_created_ids2' : [],
535             'product_lines' : [],
536             'picking_id': False
537         })
538         return super(mrp_production, self).copy(cr, uid, id, default, context)
539
540     def location_id_change(self, cr, uid, ids, src, dest, context=None):
541         """ Changes destination location if source location is changed.
542         @param src: Source location id.
543         @param dest: Destination location id.
544         @return: Dictionary of values.
545         """
546         if dest:
547             return {}
548         if src:
549             return {'value': {'location_dest_id': src}}
550         return {}
551
552     def product_id_change(self, cr, uid, ids, product_id, context=None):
553         """ Finds UoM of changed product.
554         @param product_id: Id of changed product.
555         @return: Dictionary of values.
556         """
557         if not product_id:
558             return {'value': {
559                 'product_uom': False,
560                 'bom_id': False,
561                 'routing_id': False
562             }}
563         bom_obj = self.pool.get('mrp.bom')
564         product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
565         bom_id = bom_obj._bom_find(cr, uid, product.id, product.uom_id and product.uom_id.id, [])
566         routing_id = False
567         if bom_id:
568             bom_point = bom_obj.browse(cr, uid, bom_id, context=context)
569             routing_id = bom_point.routing_id.id or False
570         result = {
571             'product_uom': product.uom_id and product.uom_id.id or False,
572             'bom_id': bom_id,
573             'routing_id': routing_id
574         }
575         return {'value': result}
576
577     def bom_id_change(self, cr, uid, ids, bom_id, context=None):
578         """ Finds routing for changed BoM.
579         @param product: Id of product.
580         @return: Dictionary of values.
581         """
582         if not bom_id:
583             return {'value': {
584                 'routing_id': False
585             }}
586         bom_point = self.pool.get('mrp.bom').browse(cr, uid, bom_id, context=context)
587         routing_id = bom_point.routing_id.id or False
588         result = {
589             'routing_id': routing_id
590         }
591         return {'value': result}
592
593     def action_picking_except(self, cr, uid, ids):
594         """ Changes the state to Exception.
595         @return: True
596         """
597         self.write(cr, uid, ids, {'state': 'picking_except'})
598         return True
599
600     def action_compute(self, cr, uid, ids, properties=[], context=None):
601         """ Computes bills of material of a product.
602         @param properties: List containing dictionaries of properties.
603         @return: No. of products.
604         """
605         results = []
606         bom_obj = self.pool.get('mrp.bom')
607         uom_obj = self.pool.get('product.uom')
608         prod_line_obj = self.pool.get('mrp.production.product.line')
609         workcenter_line_obj = self.pool.get('mrp.production.workcenter.line')
610         for production in self.browse(cr, uid, ids):
611             cr.execute('delete from mrp_production_product_line where production_id=%s', (production.id,))
612             cr.execute('delete from mrp_production_workcenter_line where production_id=%s', (production.id,))
613             bom_point = production.bom_id
614             bom_id = production.bom_id.id
615             if not bom_point:
616                 bom_id = bom_obj._bom_find(cr, uid, production.product_id.id, production.product_uom.id, properties)
617                 if bom_id:
618                     bom_point = bom_obj.browse(cr, uid, bom_id)
619                     routing_id = bom_point.routing_id.id or False
620                     self.write(cr, uid, [production.id], {'bom_id': bom_id, 'routing_id': routing_id})
621
622             if not bom_id:
623                 raise osv.except_osv(_('Error'), _("Couldn't find a bill of material for this product."))
624             factor = uom_obj._compute_qty(cr, uid, production.product_uom.id, production.product_qty, bom_point.product_uom.id)
625             res = bom_obj._bom_explode(cr, uid, bom_point, factor / bom_point.product_qty, properties, routing_id=production.routing_id.id)
626             results = res[0]
627             results2 = res[1]
628             for line in results:
629                 line['production_id'] = production.id
630                 prod_line_obj.create(cr, uid, line)
631             for line in results2:
632                 line['production_id'] = production.id
633                 workcenter_line_obj.create(cr, uid, line)
634         return len(results)
635
636     def action_cancel(self, cr, uid, ids, context=None):
637         """ Cancels the production order and related stock moves.
638         @return: True
639         """
640         if context is None:
641             context = {}
642         move_obj = self.pool.get('stock.move')
643         for production in self.browse(cr, uid, ids, context=context):
644             if production.state == 'confirmed' and production.picking_id.state not in ('draft', 'cancel'):
645                 raise osv.except_osv(
646                     _('Could not cancel manufacturing order !'),
647                     _('You must first cancel related internal picking attached to this manufacturing order.'))
648             if production.move_created_ids:
649                 move_obj.action_cancel(cr, uid, [x.id for x in production.move_created_ids])
650             move_obj.action_cancel(cr, uid, [x.id for x in production.move_lines])
651         self.write(cr, uid, ids, {'state': 'cancel'})
652         self.action_cancel_send_note(cr, uid, ids, context)
653         return True
654
655     def action_ready(self, cr, uid, ids, context=None):
656         """ Changes the production state to Ready and location id of stock move.
657         @return: True
658         """
659         move_obj = self.pool.get('stock.move')
660         self.write(cr, uid, ids, {'state': 'ready'})
661
662         for (production_id,name) in self.name_get(cr, uid, ids):
663             production = self.browse(cr, uid, production_id)
664             if production.move_prod_id:
665                 move_obj.write(cr, uid, [production.move_prod_id.id],
666                         {'location_id': production.location_dest_id.id})
667             self.action_ready_send_note(cr, uid, [production_id], context)
668         return True
669
670     def action_production_end(self, cr, uid, ids, context=None):
671         """ Changes production state to Finish and writes finished date.
672         @return: True
673         """
674         for production in self.browse(cr, uid, ids):
675             self._costs_generate(cr, uid, production)
676         write_res = self.write(cr, uid, ids, {'state': 'done', 'date_finished': time.strftime('%Y-%m-%d %H:%M:%S')})
677         self.action_done_send_note(cr, uid, ids, context)
678         return write_res
679
680     def test_production_done(self, cr, uid, ids):
681         """ Tests whether production is done or not.
682         @return: True or False
683         """
684         res = True
685         for production in self.browse(cr, uid, ids):
686             if production.move_lines:
687                 res = False
688
689             if production.move_created_ids:
690                 res = False
691         return res
692
693     def _get_subproduct_factor(self, cr, uid, production_id, move_id=None, context=None):
694         """ Compute the factor to compute the qty of procucts to produce for the given production_id. By default,
695             it's always equal to the quantity encoded in the production order or the production wizard, but if the
696             module mrp_subproduct is installed, then we must use the move_id to identify the product to produce
697             and its quantity.
698         :param production_id: ID of the mrp.order
699         :param move_id: ID of the stock move that needs to be produced. Will be used in mrp_subproduct.
700         :return: The factor to apply to the quantity that we should produce for the given production order.
701         """
702         return 1
703
704     def action_produce(self, cr, uid, production_id, production_qty, production_mode, context=None):
705         """ To produce final product based on production mode (consume/consume&produce).
706         If Production mode is consume, all stock move lines of raw materials will be done/consumed.
707         If Production mode is consume & produce, all stock move lines of raw materials will be done/consumed
708         and stock move lines of final product will be also done/produced.
709         @param production_id: the ID of mrp.production object
710         @param production_qty: specify qty to produce
711         @param production_mode: specify production mode (consume/consume&produce).
712         @return: True
713         """
714         stock_mov_obj = self.pool.get('stock.move')
715         production = self.browse(cr, uid, production_id, context=context)
716
717         produced_qty = 0
718         for produced_product in production.move_created_ids2:
719             if (produced_product.scrapped) or (produced_product.product_id.id <> production.product_id.id):
720                 continue
721             produced_qty += produced_product.product_qty
722         if production_mode in ['consume','consume_produce']:
723             consumed_data = {}
724
725             # Calculate already consumed qtys
726             for consumed in production.move_lines2:
727                 if consumed.scrapped:
728                     continue
729                 if not consumed_data.get(consumed.product_id.id, False):
730                     consumed_data[consumed.product_id.id] = 0
731                 consumed_data[consumed.product_id.id] += consumed.product_qty
732
733             # Find product qty to be consumed and consume it
734             for scheduled in production.product_lines:
735
736                 # total qty of consumed product we need after this consumption
737                 total_consume = ((production_qty + produced_qty) * scheduled.product_qty / production.product_qty)
738
739                 # qty available for consume and produce
740                 qty_avail = scheduled.product_qty - consumed_data.get(scheduled.product_id.id, 0.0)
741
742                 if qty_avail <= 0.0:
743                     # there will be nothing to consume for this raw material
744                     continue
745
746                 raw_product = [move for move in production.move_lines if move.product_id.id==scheduled.product_id.id]
747                 if raw_product:
748                     # qtys we have to consume
749                     qty = total_consume - consumed_data.get(scheduled.product_id.id, 0.0)
750
751                     if qty > qty_avail:
752                         # if qtys we have to consume is more than qtys available to consume
753                         prod_name = scheduled.product_id.name_get()[0][1]
754                         raise osv.except_osv(_('Warning!'), _('You are going to consume total %s quantities of "%s".\nBut you can only consume up to total %s quantities.') % (qty, prod_name, qty_avail))
755                     if qty <= 0.0:
756                         # we already have more qtys consumed than we need
757                         continue
758
759                     raw_product[0].action_consume(qty, raw_product[0].location_id.id, context=context)
760
761         if production_mode == 'consume_produce':
762             # To produce remaining qty of final product
763             #vals = {'state':'confirmed'}
764             #final_product_todo = [x.id for x in production.move_created_ids]
765             #stock_mov_obj.write(cr, uid, final_product_todo, vals)
766             #stock_mov_obj.action_confirm(cr, uid, final_product_todo, context)
767             produced_products = {}
768             for produced_product in production.move_created_ids2:
769                 if produced_product.scrapped:
770                     continue
771                 if not produced_products.get(produced_product.product_id.id, False):
772                     produced_products[produced_product.product_id.id] = 0
773                 produced_products[produced_product.product_id.id] += produced_product.product_qty
774
775             for produce_product in production.move_created_ids:
776                 produced_qty = produced_products.get(produce_product.product_id.id, 0)
777                 subproduct_factor = self._get_subproduct_factor(cr, uid, production.id, produce_product.id, context=context)
778                 rest_qty = (subproduct_factor * production.product_qty) - produced_qty
779
780                 if rest_qty < production_qty:
781                     prod_name = produce_product.product_id.name_get()[0][1]
782                     raise osv.except_osv(_('Warning!'), _('You are going to produce total %s quantities of "%s".\nBut you can only produce up to total %s quantities.') % (production_qty, prod_name, rest_qty))
783                 if rest_qty > 0 :
784                     stock_mov_obj.action_consume(cr, uid, [produce_product.id], (subproduct_factor * production_qty), context=context)
785
786         for raw_product in production.move_lines2:
787             new_parent_ids = []
788             parent_move_ids = [x.id for x in raw_product.move_history_ids]
789             for final_product in production.move_created_ids2:
790                 if final_product.id not in parent_move_ids:
791                     new_parent_ids.append(final_product.id)
792             for new_parent_id in new_parent_ids:
793                 stock_mov_obj.write(cr, uid, [raw_product.id], {'move_history_ids': [(4,new_parent_id)]})
794
795         wf_service = netsvc.LocalService("workflow")
796         wf_service.trg_validate(uid, 'mrp.production', production_id, 'button_produce_done', cr)
797         return True
798
799     def _costs_generate(self, cr, uid, production):
800         """ Calculates total costs at the end of the production.
801         @param production: Id of production order.
802         @return: Calculated amount.
803         """
804         amount = 0.0
805         analytic_line_obj = self.pool.get('account.analytic.line')
806         for wc_line in production.workcenter_lines:
807             wc = wc_line.workcenter_id
808             if wc.costs_journal_id and wc.costs_general_account_id:
809                 value = wc_line.hour * wc.costs_hour
810                 account = wc.costs_hour_account_id.id
811                 if value and account:
812                     amount += value
813                     analytic_line_obj.create(cr, uid, {
814                         'name': wc_line.name + ' (H)',
815                         'amount': value,
816                         'account_id': account,
817                         'general_account_id': wc.costs_general_account_id.id,
818                         'journal_id': wc.costs_journal_id.id,
819                         'ref': wc.code,
820                         'product_id': wc.product_id.id,
821                         'unit_amount': wc_line.hour,
822                         'product_uom_id': wc.product_id.uom_id.id
823                     } )
824             if wc.costs_journal_id and wc.costs_general_account_id:
825                 value = wc_line.cycle * wc.costs_cycle
826                 account = wc.costs_cycle_account_id.id
827                 if value and account:
828                     amount += value
829                     analytic_line_obj.create(cr, uid, {
830                         'name': wc_line.name+' (C)',
831                         'amount': value,
832                         'account_id': account,
833                         'general_account_id': wc.costs_general_account_id.id,
834                         'journal_id': wc.costs_journal_id.id,
835                         'ref': wc.code,
836                         'product_id': wc.product_id.id,
837                         'unit_amount': wc_line.cycle,
838                         'product_uom_id': wc.product_id.uom_id.id
839                     } )
840         return amount
841
842     def action_in_production(self, cr, uid, ids, context=None):
843         """ Changes state to In Production and writes starting date.
844         @return: True
845         """
846         self.write(cr, uid, ids, {'state': 'in_production', 'date_start': time.strftime('%Y-%m-%d %H:%M:%S')})
847         self.action_in_production_send_note(cr, uid, ids, context)
848         return True
849
850     def test_if_product(self, cr, uid, ids):
851         """
852         @return: True or False
853         """
854         res = True
855         for production in self.browse(cr, uid, ids):
856             if not production.product_lines:
857                 if not self.action_compute(cr, uid, [production.id]):
858                     res = False
859         return res
860
861     def _get_auto_picking(self, cr, uid, production):
862         return True
863
864     def _make_production_line_procurement(self, cr, uid, production_line, shipment_move_id, context=None):
865         wf_service = netsvc.LocalService("workflow")
866         procurement_order = self.pool.get('procurement.order')
867         production = production_line.production_id
868         location_id = production.location_src_id.id
869         date_planned = production.date_planned
870         procurement_name = (production.origin or '').split(':')[0] + ':' + production.name
871         procurement_id = procurement_order.create(cr, uid, {
872                     'name': procurement_name,
873                     'origin': procurement_name,
874                     'date_planned': date_planned,
875                     'product_id': production_line.product_id.id,
876                     'product_qty': production_line.product_qty,
877                     'product_uom': production_line.product_uom.id,
878                     'product_uos_qty': production_line.product_uos and production_line.product_qty or False,
879                     'product_uos': production_line.product_uos and production_line.product_uos.id or False,
880                     'location_id': location_id,
881                     'procure_method': production_line.product_id.procure_method,
882                     'move_id': shipment_move_id,
883                     'company_id': production.company_id.id,
884                 })
885         wf_service.trg_validate(uid, procurement_order._name, procurement_id, 'button_confirm', cr)
886         return procurement_id
887
888     def _make_production_internal_shipment_line(self, cr, uid, production_line, shipment_id, parent_move_id, destination_location_id=False, context=None):
889         stock_move = self.pool.get('stock.move')
890         production = production_line.production_id
891         date_planned = production.date_planned
892         # Internal shipment is created for Stockable and Consumer Products
893         if production_line.product_id.type not in ('product', 'consu'):
894             return False
895         move_name = _('PROD: %s') % production.name
896         source_location_id = production.location_src_id.id
897         if not destination_location_id:
898             destination_location_id = source_location_id
899         return stock_move.create(cr, uid, {
900                         'name': move_name,
901                         'picking_id': shipment_id,
902                         'product_id': production_line.product_id.id,
903                         'product_qty': production_line.product_qty,
904                         'product_uom': production_line.product_uom.id,
905                         'product_uos_qty': production_line.product_uos and production_line.product_uos_qty or False,
906                         'product_uos': production_line.product_uos and production_line.product_uos.id or False,
907                         'date': date_planned,
908                         'move_dest_id': parent_move_id,
909                         'location_id': source_location_id,
910                         'location_dest_id': destination_location_id,
911                         'state': 'waiting',
912                         'company_id': production.company_id.id,
913                 })
914
915     def _make_production_internal_shipment(self, cr, uid, production, context=None):
916         ir_sequence = self.pool.get('ir.sequence')
917         stock_picking = self.pool.get('stock.picking')
918         routing_loc = None
919         pick_type = 'internal'
920         partner_id = False
921
922         # Take routing address as a Shipment Address.
923         # If usage of routing location is a internal, make outgoing shipment otherwise internal shipment
924         if production.bom_id.routing_id and production.bom_id.routing_id.location_id:
925             routing_loc = production.bom_id.routing_id.location_id
926             if routing_loc.usage <> 'internal':
927                 pick_type = 'out'
928             partner_id = routing_loc.partner_id and routing_loc.partner_id.id or False
929
930         # Take next Sequence number of shipment base on type
931         pick_name = ir_sequence.get(cr, uid, 'stock.picking.' + pick_type)
932
933         picking_id = stock_picking.create(cr, uid, {
934             'name': pick_name,
935             'origin': (production.origin or '').split(':')[0] + ':' + production.name,
936             'type': pick_type,
937             'move_type': 'one',
938             'state': 'auto',
939             'partner_id': partner_id,
940             'auto_picking': self._get_auto_picking(cr, uid, production),
941             'company_id': production.company_id.id,
942         })
943         production.write({'picking_id': picking_id}, context=context)
944         return picking_id
945
946     def _make_production_produce_line(self, cr, uid, production, context=None):
947         stock_move = self.pool.get('stock.move')
948         source_location_id = production.product_id.product_tmpl_id.property_stock_production.id
949         destination_location_id = production.location_dest_id.id
950         move_name = _('PROD: %s') + production.name
951         data = {
952             'name': move_name,
953             'date': production.date_planned,
954             'product_id': production.product_id.id,
955             'product_qty': production.product_qty,
956             'product_uom': production.product_uom.id,
957             'product_uos_qty': production.product_uos and production.product_uos_qty or False,
958             'product_uos': production.product_uos and production.product_uos.id or False,
959             'location_id': source_location_id,
960             'location_dest_id': destination_location_id,
961             'move_dest_id': production.move_prod_id.id,
962             'state': 'waiting',
963             'company_id': production.company_id.id,
964         }
965         move_id = stock_move.create(cr, uid, data, context=context)
966         production.write({'move_created_ids': [(6, 0, [move_id])]}, context=context)
967         return move_id
968
969     def _make_production_consume_line(self, cr, uid, production_line, parent_move_id, source_location_id=False, context=None):
970         stock_move = self.pool.get('stock.move')
971         production = production_line.production_id
972         # Internal shipment is created for Stockable and Consumer Products
973         if production_line.product_id.type not in ('product', 'consu'):
974             return False
975         move_name = _('PROD: %s') % production.name
976         destination_location_id = production.product_id.product_tmpl_id.property_stock_production.id
977         if not source_location_id:
978             source_location_id = production.location_src_id.id
979         move_id = stock_move.create(cr, uid, {
980             'name': move_name,
981             'date': production.date_planned,
982             'product_id': production_line.product_id.id,
983             'product_qty': production_line.product_qty,
984             'product_uom': production_line.product_uom.id,
985             'product_uos_qty': production_line.product_uos and production_line.product_uos_qty or False,
986             'product_uos': production_line.product_uos and production_line.product_uos.id or False,
987             'location_id': source_location_id,
988             'location_dest_id': destination_location_id,
989             'move_dest_id': parent_move_id,
990             'state': 'waiting',
991             'company_id': production.company_id.id,
992         })
993         production.write({'move_lines': [(4, move_id)]}, context=context)
994         return move_id
995
996     def action_confirm(self, cr, uid, ids, context=None):
997         """ Confirms production order.
998         @return: Newly generated Shipment Id.
999         """
1000         shipment_id = False
1001         wf_service = netsvc.LocalService("workflow")
1002         uncompute_ids = filter(lambda x:x, [not x.product_lines and x.id or False for x in self.browse(cr, uid, ids, context=context)])
1003         self.action_compute(cr, uid, uncompute_ids, context=context)
1004         for production in self.browse(cr, uid, ids, context=context):
1005             shipment_id = self._make_production_internal_shipment(cr, uid, production, context=context)
1006             produce_move_id = self._make_production_produce_line(cr, uid, production, context=context)
1007
1008             # Take routing location as a Source Location.
1009             source_location_id = production.location_src_id.id
1010             if production.bom_id.routing_id and production.bom_id.routing_id.location_id:
1011                 source_location_id = production.bom_id.routing_id.location_id.id
1012
1013             for line in production.product_lines:
1014                 consume_move_id = self._make_production_consume_line(cr, uid, line, produce_move_id, source_location_id=source_location_id, context=context)
1015                 shipment_move_id = self._make_production_internal_shipment_line(cr, uid, line, shipment_id, consume_move_id,\
1016                                  destination_location_id=source_location_id, context=context)
1017                 self._make_production_line_procurement(cr, uid, line, shipment_move_id, context=context)
1018
1019             wf_service.trg_validate(uid, 'stock.picking', shipment_id, 'button_confirm', cr)
1020             production.write({'state':'confirmed'}, context=context)
1021             self.action_confirm_send_note(cr, uid, [production.id], context);
1022         return shipment_id
1023
1024     def force_production(self, cr, uid, ids, *args):
1025         """ Assigns products.
1026         @param *args: Arguments
1027         @return: True
1028         """
1029         pick_obj = self.pool.get('stock.picking')
1030         pick_obj.force_assign(cr, uid, [prod.picking_id.id for prod in self.browse(cr, uid, ids)])
1031         return True
1032     
1033     # ---------------------------------------------------
1034     # OpenChatter methods and notifications
1035     # ---------------------------------------------------
1036
1037     def message_get_subscribers(self, cr, uid, ids, context=None):
1038         """ Override to add responsible user. """
1039         user_ids = super(mrp_production, self).message_get_subscribers(cr, uid, ids, context=context)
1040         for obj in self.browse(cr, uid, ids, context=context):
1041             if obj.user_id and not obj.user_id.id in user_ids:
1042                 user_ids.append(obj.user_id.id)
1043         return user_ids
1044
1045     def create_send_note(self, cr, uid, ids, context=None):
1046         self.message_append_note(cr, uid, ids, body=_("Manufacturing order has been <b>created</b>."), context=context)
1047         return True
1048
1049     def action_cancel_send_note(self, cr, uid, ids, context=None):
1050         message = _("Manufacturing order has been <b>canceled</b>.")
1051         self.message_append_note(cr, uid, ids, body=message, context=context)
1052         return True
1053
1054     def action_ready_send_note(self, cr, uid, ids, context=None):
1055         message = _("Manufacturing order is <b>ready to produce</b>.")
1056         self.message_append_note(cr, uid, ids, body=message, context=context)
1057         return True
1058
1059     def action_in_production_send_note(self, cr, uid, ids, context=None):
1060         message = _("Manufacturing order is <b>in production</b>.")
1061         self.message_append_note(cr, uid, ids, body=message, context=context)
1062         return True
1063
1064     def action_done_send_note(self, cr, uid, ids, context=None):
1065         message = _("Manufacturing order has been <b>done</b>.")
1066         self.message_append_note(cr, uid, ids, body=message, context=context)
1067         return True
1068
1069     def action_confirm_send_note(self, cr, uid, ids, context=None):
1070         for obj in self.browse(cr, uid, ids, context=context):
1071             # convert datetime field to a datetime, using server format, then
1072             # convert it to the user TZ and re-render it with %Z to add the timezone
1073             obj_datetime = fields.DT.datetime.strptime(obj.date_planned, DEFAULT_SERVER_DATETIME_FORMAT)
1074             obj_date_str = fields.datetime.context_timestamp(cr, uid, obj_datetime, context=context).strftime(DATETIME_FORMATS_MAP['%+'] + " (%Z)")
1075             message = _("Manufacturing order has been <b>confirmed</b> and is <b>scheduled</b> for the <em>%s</em>.") % (obj_date_str)
1076             self.message_append_note(cr, uid, [obj.id], body=message, context=context)
1077         return True
1078
1079
1080 mrp_production()
1081
1082 class mrp_production_workcenter_line(osv.osv):
1083     _name = 'mrp.production.workcenter.line'
1084     _description = 'Work Order'
1085     _order = 'sequence'
1086     _inherit = ['mail.thread']
1087
1088     _columns = {
1089         'name': fields.char('Work Order', size=64, required=True),
1090         'workcenter_id': fields.many2one('mrp.workcenter', 'Work Center', required=True),
1091         'cycle': fields.float('Number of Cycles', digits=(16,2)),
1092         'hour': fields.float('Number of Hours', digits=(16,2)),
1093         'sequence': fields.integer('Sequence', required=True, help="Gives the sequence order when displaying a list of work orders."),
1094         'production_id': fields.many2one('mrp.production', 'Production Order', select=True, ondelete='cascade', required=True),
1095     }
1096     _defaults = {
1097         'sequence': lambda *a: 1,
1098         'hour': lambda *a: 0,
1099         'cycle': lambda *a: 0,
1100     }
1101 mrp_production_workcenter_line()
1102
1103 class mrp_production_product_line(osv.osv):
1104     _name = 'mrp.production.product.line'
1105     _description = 'Production Scheduled Product'
1106     _columns = {
1107         'name': fields.char('Name', size=64, required=True),
1108         'product_id': fields.many2one('product.product', 'Product', required=True),
1109         'product_qty': fields.float('Product Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
1110         'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', required=True),
1111         'product_uos_qty': fields.float('Product UOS Quantity'),
1112         'product_uos': fields.many2one('product.uom', 'Product UOS'),
1113         'production_id': fields.many2one('mrp.production', 'Production Order', select=True),
1114     }
1115 mrp_production_product_line()
1116
1117 class product_product(osv.osv):
1118     _inherit = "product.product"
1119     _columns = {
1120         'bom_ids': fields.one2many('mrp.bom', 'product_id', 'Bill of Materials'),
1121     }
1122
1123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: