[MERGE] lp881356
[odoo/odoo.git] / addons / project_mrp / project_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 osv import fields, osv
23
24 class procurement_order(osv.osv):
25     _name = "procurement.order"
26     _inherit = "procurement.order"
27     _columns = {
28         'task_id': fields.many2one('project.task', 'Task'),
29         'sale_line_id': fields.many2one('sale.order.line', 'Sale order line')
30     }
31
32     def action_check_finished(self, cr, uid, ids):
33         res = super(procurement_order, self).action_check_finished(cr, uid, ids)
34         return res and self.check_task_done(cr, uid, ids)
35     
36     def check_task_done(self, cr, uid, ids, context=None):
37         """ Checks if task is done or not.
38         @return: True or False.
39         """
40         return all(proc.product_id.type != 'service' or (proc.task_id and proc.task_id.state in ('done', 'cancelled')) \
41                     for proc in self.browse(cr, uid, ids, context=context))
42
43     def check_produce_service(self, cr, uid, procurement, context=None):    
44         return True
45
46     def _convert_qty_company_hours(self, cr, uid, procurement, context=None):
47         product_uom = self.pool.get('product.uom')
48         company_time_uom_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id.id
49         if procurement.product_uom.id != company_time_uom_id:
50             planned_hours = product_uom._compute_qty(cr, uid, procurement.product_uom.id, procurement.product_qty, company_time_uom_id)
51         else:
52             planned_hours = procurement.product_qty
53         return planned_hours
54
55     def _get_project(self, cr, uid, procurement, context=None):
56         project_project = self.pool.get('project.project')
57         project = procurement.product_id.project_id
58         if not project and procurement.sale_line_id:
59             # find the project corresponding to the analytic account of the sale order
60             account = procurement.sale_line_id.order_id.project_id
61             project_ids = project_project.search(cr, uid, [('analytic_account_id', '=', account.id)])
62             projects = project_project.browse(cr, uid, project_ids, context=context)
63             project = projects and projects[0] or False
64         return project
65
66     def action_produce_assign_service(self, cr, uid, ids, context=None):
67         project_task = self.pool.get('project.task')
68         for procurement in self.browse(cr, uid, ids, context=context):
69             project = self._get_project(cr, uid, procurement, context=context)
70             planned_hours = self._convert_qty_company_hours(cr, uid, procurement, context=context)
71             task_id = project_task.create(cr, uid, {
72                 'name': '%s:%s' % (procurement.origin or '', procurement.product_id.name),
73                 'date_deadline': procurement.date_planned,
74                 'planned_hours':planned_hours,
75                 'remaining_hours': planned_hours,
76                 'user_id': procurement.product_id.product_manager.id,
77                 'notes': procurement.note,
78                 'procurement_id': procurement.id,
79                 'description': procurement.note,
80                 'project_id':  project and project.id or False,
81                 'company_id': procurement.company_id.id,
82             },context=context)
83             self.write(cr, uid, [procurement.id], {'task_id': task_id, 'state': 'running'}, context=context)
84         return task_id
85
86 procurement_order()
87
88 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: