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