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 import workflow
26 class ProjectTaskStageMrp(osv.Model):
27 """ Override project.task.type model to add a 'closed' boolean field allowing
28 to know that tasks in this stage are considered as closed. Indeed since
29 OpenERP 8.0 status is not present on tasks anymore, only stage_id. """
30 _name = 'project.task.type'
31 _inherit = 'project.task.type'
34 'closed': fields.boolean('Close', help="Tasks in this stage are considered as closed."),
42 class project_task(osv.osv):
43 _name = "project.task"
44 _inherit = "project.task"
46 'procurement_id': fields.many2one('procurement.order', 'Procurement', ondelete='set null'),
47 'sale_line_id': fields.related('procurement_id', 'sale_line_id', type='many2one', relation='sale.order.line', store=True, string='Sales Order Line'),
50 def _validate_subflows(self, cr, uid, ids, context=None):
51 for task in self.browse(cr, uid, ids):
52 if task.procurement_id:
53 workflow.trg_write(uid, 'procurement.order', task.procurement_id.id, cr)
55 def write(self, cr, uid, ids, values, context=None):
56 """ When closing tasks, validate subflows. """
57 res = super(project_task, self).write(cr, uid, ids, values, context=context)
58 if values.get('stage_id'):
59 stage = self.pool.get('project.task.type').browse(cr, uid, values.get('stage_id'), context=context)
61 self._validate_subflows(cr, uid, ids, context=context)
65 class product_product(osv.osv):
66 _inherit = "product.product"
68 'project_id': fields.many2one('project.project', 'Project', ondelete='set null',)
72 class sale_order(osv.osv):
73 _inherit = 'sale.order'
75 def _prepare_order_line_procurement(self, cr, uid, order, line, move_id, date_planned, context=None):
76 proc_data = super(sale_order, self)._prepare_order_line_procurement(cr,
77 uid, order, line, move_id, date_planned, context=context)
78 proc_data['sale_line_id'] = line.id
81 def _picked_rate(self, cr, uid, ids, name, arg, context=None):
85 res = super(sale_order, self)._picked_rate(cr, uid, ids, name, arg, context=context)
86 cr.execute('''select sol.order_id as sale_id, stage.closed as task_closed ,
87 t.id as task_id, sum(sol.product_uom_qty) as total
88 from project_task as t
89 left join sale_order_line as sol on sol.id = t.sale_line_id
90 left join project_task_type as stage on stage.id = t.stage_id
91 where sol.order_id in %s group by sol.order_id,stage.closed,t.id ''',(tuple(ids),))
92 sale_task_data = cr.dictfetchall()
94 if not sale_task_data:
102 #compute the sum of quantity for each SO
103 cr.execute('''select sol.order_id as sale_id, sum(sol.product_uom_qty) as total
104 from sale_order_line sol where sol.order_id in %s group by sol.order_id''',(tuple(ids),))
105 total_qtty_ref = cr.dictfetchall()
106 for item in total_qtty_ref:
107 res_sale[item['sale_id']]['number_of_stockable'] = item['total']
109 for item in sale_task_data:
110 res_sale[item['sale_id']]['total_no_task'] += item['total']
111 if item['task_closed']:
112 res_sale[item['sale_id']]['number_of_done'] += item['total']
114 for sale in self.browse(cr, uid, ids, context=context):
115 if 'number_of_stockable' in res_sale[sale.id]:
116 res_sale[sale.id]['number_of_stockable'] -= res_sale[sale.id]['total_no_task']
117 #adjust previously percentage because now we must also count the product of type service
118 res[sale.id] = res[sale.id] * float(res_sale[sale.id]['number_of_stockable']) / (res_sale[sale.id]['number_of_stockable'] + res_sale[sale.id]['total_no_task'])
120 res[sale.id] += res_sale[sale.id]['number_of_done'] * 100 / (res_sale[sale.id]['number_of_stockable'] + res_sale[sale.id]['total_no_task'])
124 'picked_rate': fields.function(_picked_rate, method=True, string='Picked', type='float'),