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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
25 class procurement_order(osv.osv):
26 _name = "procurement.order"
27 _inherit = "procurement.order"
29 'task_id': fields.many2one('project.task', 'Task', copy=False),
32 def _is_procurement_task(self, cr, uid, procurement, context=None):
33 return procurement.product_id.type == 'service' and procurement.product_id.auto_create_task or False
35 def _assign(self, cr, uid, procurement, context=None):
36 res = super(procurement_order, self)._assign(cr, uid, procurement, context=context)
38 #if there isn't any specific procurement.rule defined for the product, we may want to create a task
39 if self._is_procurement_task(cr, uid, procurement, context=context):
43 def _run(self, cr, uid, procurement, context=None):
44 if self._is_procurement_task(cr, uid, procurement, context=context) and not procurement.task_id:
45 #create a task for the procurement
46 return self._create_service_task(cr, uid, procurement, context=context)
47 return super(procurement_order, self)._run(cr, uid, procurement, context=context)
49 def _check(self, cr, uid, procurement, context=None):
50 if self._is_procurement_task(cr, uid, procurement, context=context):
51 return procurement.task_id and procurement.task_id.stage_id.closed or False
52 return super(procurement_order, self)._check(cr, uid, procurement, context=context)
54 def _convert_qty_company_hours(self, cr, uid, procurement, context=None):
55 product_uom = self.pool.get('product.uom')
56 company_time_uom_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id
57 if procurement.product_uom.id != company_time_uom_id.id and procurement.product_uom.category_id.id == company_time_uom_id.category_id.id:
58 planned_hours = product_uom._compute_qty(cr, uid, procurement.product_uom.id, procurement.product_qty, company_time_uom_id.id)
60 planned_hours = procurement.product_qty
63 def _get_project(self, cr, uid, procurement, context=None):
64 project_project = self.pool.get('project.project')
65 project = procurement.product_id.project_id
66 if not project and procurement.sale_line_id:
67 # find the project corresponding to the analytic account of the sales order
68 account = procurement.sale_line_id.order_id.project_id
69 project_ids = project_project.search(cr, uid, [('analytic_account_id', '=', account.id)])
70 projects = project_project.browse(cr, uid, project_ids, context=context)
71 project = projects and projects[0] or False
74 def _create_service_task(self, cr, uid, procurement, context=None):
75 project_task = self.pool.get('project.task')
76 project = self._get_project(cr, uid, procurement, context=context)
77 planned_hours = self._convert_qty_company_hours(cr, uid, procurement, context=context)
78 task_id = project_task.create(cr, uid, {
79 'name': '%s:%s' % (procurement.origin or '', procurement.product_id.name),
80 'date_deadline': procurement.date_planned,
81 'planned_hours': planned_hours,
82 'remaining_hours': planned_hours,
83 'partner_id': procurement.sale_line_id and procurement.sale_line_id.order_id.partner_id.id or procurement.partner_dest_id.id,
84 'user_id': procurement.product_id.product_manager.id,
85 'procurement_id': procurement.id,
86 'description': procurement.name + '\n',
87 'project_id': project and project.id or False,
88 'company_id': procurement.company_id.id,
90 self.write(cr, uid, [procurement.id], {'task_id': task_id}, context=context)
91 self.project_task_create_note(cr, uid, [procurement.id], context=context)
94 def project_task_create_note(self, cr, uid, ids, context=None):
95 for procurement in self.browse(cr, uid, ids, context=context):
96 body = _("Task created")
97 self.message_post(cr, uid, [procurement.id], body=body, context=context)
98 if procurement.sale_line_id and procurement.sale_line_id.order_id:
99 procurement.sale_line_id.order_id.message_post(body=body)
102 class ProjectTaskStageMrp(osv.Model):
103 """ Override project.task.type model to add a 'closed' boolean field allowing
104 to know that tasks in this stage are considered as closed. Indeed since
105 OpenERP 8.0 status is not present on tasks anymore, only stage_id. """
106 _name = 'project.task.type'
107 _inherit = 'project.task.type'
110 'closed': fields.boolean('Close', help="Tasks in this stage are considered as closed."),
118 class project_task(osv.osv):
119 _name = "project.task"
120 _inherit = "project.task"
122 'procurement_id': fields.many2one('procurement.order', 'Procurement', ondelete='set null'),
123 'sale_line_id': fields.related('procurement_id', 'sale_line_id', type='many2one', relation='sale.order.line', store=True, string='Sales Order Line'),
126 def _validate_subflows(self, cr, uid, ids, context=None):
127 proc_obj = self.pool.get("procurement.order")
128 for task in self.browse(cr, uid, ids, context=context):
129 if task.procurement_id:
130 proc_obj.check(cr, uid, [task.procurement_id.id], context=context)
132 def write(self, cr, uid, ids, values, context=None):
133 """ When closing tasks, validate subflows. """
134 res = super(project_task, self).write(cr, uid, ids, values, context=context)
135 if values.get('stage_id'):
136 stage = self.pool.get('project.task.type').browse(cr, uid, values.get('stage_id'), context=context)
138 self._validate_subflows(cr, uid, ids, context=context)
141 class product_template(osv.osv):
142 _inherit = "product.template"
144 'project_id': fields.many2one('project.project', 'Project', ondelete='set null',),
145 'auto_create_task': fields.boolean('Create Task Automatically', help="Thick this option if you want to create a task automatically each time this product is sold"),
148 class product_product(osv.osv):
149 _inherit = "product.product"
151 def need_procurement(self, cr, uid, ids, context=None):
152 for product in self.browse(cr, uid, ids, context=context):
153 if product.type == 'service' and product.auto_create_task:
155 return super(product_product, self).need_procurement(cr, uid, ids, context=context)