[FIX] qweb loading concat mode only, increase cache age to one day
[odoo/odoo.git] / addons / project_mrp / project_mrp.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 import workflow
24
25
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'
32
33     _columns = {
34         'closed': fields.boolean('Close', help="Tasks in this stage are considered as closed."),
35     }
36
37     _defaults = {
38         'closed': False,
39     }
40
41
42 class project_task(osv.osv):
43     _name = "project.task"
44     _inherit = "project.task"
45     _columns = {
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'),
48     }
49
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)
54
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)
60             if stage.closed:
61                 self._validate_subflows(cr, uid, ids, context=context)
62         return res
63
64
65 class product_product(osv.osv):
66     _inherit = "product.product"
67     _columns = {
68         'project_id': fields.many2one('project.project', 'Project', ondelete='set null',)
69     }
70
71
72 class sale_order(osv.osv):
73     _inherit = 'sale.order'
74
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
79         return proc_data
80
81     def _picked_rate(self, cr, uid, ids, name, arg, context=None):
82         if not ids:
83             return {}
84         res_sale = {}
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()
93
94         if not sale_task_data:
95             return res
96
97         for id in ids:
98             res_sale[id] = {
99                 'number_of_done': 0,
100                 'total_no_task': 0,
101             }
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']
108
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']
113
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'])
119                 #add the 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'])
121         return res
122
123     _columns = {
124         'picked_rate': fields.function(_picked_rate, method=True, string='Picked', type='float'),
125     }
126