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'),
30 'sale_line_id': fields.many2one('sale.order.line', 'Sales order line')
33 def _is_procurement_task(self, cr, uid, procurement, context=None):
34 return procurement.product_id.type == 'service' and procurement.product_id.auto_create_task or False
36 def _assign(self, cr, uid, procurement, context=None):
37 res = super(procurement_order, self)._assign(cr, uid, procurement, context=context)
39 #if there isn't any specific procurement.rule defined for the product, we may want to create a task
40 if self._is_procurement_task(cr, uid, procurement, context=context):
44 def _run(self, cr, uid, procurement, context=None):
45 if self._is_procurement_task(cr, uid, procurement, context=context) and not procurement.task_id:
46 #create a task for the procurement
47 return self._create_service_task(cr, uid, procurement, context=context)
48 return super(procurement_order, self)._run(cr, uid, procurement, context=context)
50 def _check(self, cr, uid, procurement, context=None):
51 if self._is_procurement_task(cr, uid, procurement, context=context):
52 return procurement.task_id and procurement.task_id.stage_id.closed or False
53 return super(procurement_order, self)._check(cr, uid, procurement, context=context)
55 def _convert_qty_company_hours(self, cr, uid, procurement, context=None):
56 product_uom = self.pool.get('product.uom')
57 company_time_uom_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id
58 if procurement.product_uom.id != company_time_uom_id.id and procurement.product_uom.category_id.id == company_time_uom_id.category_id.id:
59 planned_hours = product_uom._compute_qty(cr, uid, procurement.product_uom.id, procurement.product_qty, company_time_uom_id.id)
61 planned_hours = procurement.product_qty
64 def _get_project(self, cr, uid, procurement, context=None):
65 project_project = self.pool.get('project.project')
66 project = procurement.product_id.project_id
67 if not project and procurement.sale_line_id:
68 # find the project corresponding to the analytic account of the sales order
69 account = procurement.sale_line_id.order_id.project_id
70 project_ids = project_project.search(cr, uid, [('analytic_account_id', '=', account.id)])
71 projects = project_project.browse(cr, uid, project_ids, context=context)
72 project = projects and projects[0] or False
75 def _create_service_task(self, cr, uid, procurement, context=None):
76 project_task = self.pool.get('project.task')
77 project = self._get_project(cr, uid, procurement, context=context)
78 planned_hours = self._convert_qty_company_hours(cr, uid, procurement, context=context)
79 task_id = project_task.create(cr, uid, {
80 'name': '%s:%s' % (procurement.origin or '', procurement.product_id.name),
81 'date_deadline': procurement.date_planned,
82 'planned_hours': planned_hours,
83 'remaining_hours': planned_hours,
84 'partner_id': procurement.sale_line_id and procurement.sale_line_id.order_id.partner_id.id or False,
85 'user_id': procurement.product_id.product_manager.id,
86 'procurement_id': procurement.id,
87 'description': procurement.name + '\n',
88 'project_id': project and project.id or False,
89 'company_id': procurement.company_id.id,
91 self.write(cr, uid, [procurement.id], {'task_id': task_id}, context=context)
92 self.project_task_create_note(cr, uid, [procurement.id], context=context)
95 def project_task_create_note(self, cr, uid, ids, context=None):
96 for procurement in self.browse(cr, uid, ids, context=context):
97 body = _("Task created")
98 self.message_post(cr, uid, [procurement.id], body=body, context=context)
99 if procurement.sale_line_id and procurement.sale_line_id.order_id:
100 procurement.sale_line_id.order_id.message_post(body=body)
103 class ProjectTaskStageMrp(osv.Model):
104 """ Override project.task.type model to add a 'closed' boolean field allowing
105 to know that tasks in this stage are considered as closed. Indeed since
106 OpenERP 8.0 status is not present on tasks anymore, only stage_id. """
107 _name = 'project.task.type'
108 _inherit = 'project.task.type'
111 'closed': fields.boolean('Close', help="Tasks in this stage are considered as closed."),
119 class project_task(osv.osv):
120 _name = "project.task"
121 _inherit = "project.task"
123 'procurement_id': fields.many2one('procurement.order', 'Procurement', ondelete='set null'),
124 'sale_line_id': fields.related('procurement_id', 'sale_line_id', type='many2one', relation='sale.order.line', store=True, string='Sales Order Line'),
127 def _validate_subflows(self, cr, uid, ids, context=None):
128 proc_obj = self.pool.get("procurement.order")
129 for task in self.browse(cr, uid, ids, context=context):
130 if task.procurement_id:
131 proc_obj.check(cr, uid, [task.procurement_id.id], context=context)
133 def write(self, cr, uid, ids, values, context=None):
134 """ When closing tasks, validate subflows. """
135 res = super(project_task, self).write(cr, uid, ids, values, context=context)
136 if values.get('stage_id'):
137 stage = self.pool.get('project.task.type').browse(cr, uid, values.get('stage_id'), context=context)
139 self._validate_subflows(cr, uid, ids, context=context)
142 class product_product(osv.osv):
143 _inherit = "product.product"
145 'project_id': fields.many2one('project.project', 'Project', ondelete='set null',),
146 '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"),
150 class sale_order_line(osv.osv):
151 _inherit = 'sale.order.line'
153 def need_procurement(self, cr, uid, ids, context=None):
154 #when sale is installed alone, there is no need to create procurements, but with project_mrp
155 #we must create a procurement for each service that has the auto_create_task boolean set to True.
156 for line in self.browse(cr, uid, ids, context=context):
157 if line.product_id and line.product_id.type == 'service' and line.product_id.auto_create_task:
159 return super(sale_order_line, self).need_procurement(cr, uid, ids, context=context)