[IMP] project_mrp: Rename the file
[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 osv import fields, osv, orm
23
24 import tools
25
26 class procurement_order(osv.osv):
27     _name = "procurement.order"
28     _inherit = "procurement.order"
29     _columns = {
30         'task_id': fields.many2one('project.task', 'Task')
31     }
32
33     def action_produce_assign_service(self, cr, uid, ids, context=None):
34         if context is None:
35             context = {}
36         for procurement in self.browse(cr, uid, ids):
37             sline = self.pool.get('sale.order.line')
38             product_obj=self.pool.get('product.product')
39             content = ''
40             sale_order = self.pool.get('sale.order')
41             so_ref =  procurement.name.split(':')[0]
42             order_ids = sale_order.search(cr, uid, [('name', '=', so_ref)], context)
43
44             if order_ids:
45                 sale_ids = sale_order.read(cr, uid, order_ids[0], ['order_line'], context=context)['order_line']
46             else:
47                 so_ref =  procurement.origin.split(':')[0]
48                 sale_ids = sline.search(cr, uid, [('procurement_id', '=',procurement.id)], context)
49             l = None
50             project_id = None
51             analytic_account_id = False
52             partner_id = False
53
54             for line in sline.browse(cr, uid, sale_ids, context=context):
55                 content += (line.notes or '')
56                 l = line
57                 partner_id = line.order_id.partner_id.id
58                 if line.order_id.project_id:
59                     analytic_account_id = line.order_id.project_id.id
60                     partner_id = line.order_id.partner_id.id
61                     content+="\n\n"+line.order_id.project_id.complete_name
62                     break
63
64             # Creating a project for task.Project is created from Procurement.
65             project_obj = self.pool.get('project.project')
66             proj_name = tools.ustr(so_ref)
67             product_project_id =procurement.product_id.project_id and procurement.product_id.project_id.id or False
68             proj_exist_id = project_obj.search(cr, uid, [('name', '=', proj_name)], context=context)
69             if  not proj_exist_id:
70                 project_id = project_obj.create(cr, uid, {'name': proj_name, 'partner_id': partner_id,'parent_id':product_project_id})
71             else:
72                 project_id = proj_exist_id[0]
73
74             self.write(cr, uid, [procurement.id], {'state': 'running'})
75
76             name_task = ('','')
77             planned_hours=0.0
78             if procurement.product_id.type == 'service':
79                 proc_name = procurement.name
80                 if procurement.origin == proc_name:
81                     proc_name = procurement.product_id.name
82
83                 name_task = (procurement.origin, proc_name or '')
84             else:
85                 name_task = (procurement.product_id.name or procurement.origin, procurement.name or '')
86             planned_hours= procurement.product_id.sale_delay +procurement.product_id. produce_delay   
87             task_id = self.pool.get('project.task').create(cr, uid, {
88                 'name': '%s:%s' % name_task,
89                 'date_deadline': procurement.date_planned,
90                 'planned_hours':planned_hours,
91                 'remaining_hours': planned_hours,
92                 'user_id': procurement.product_id.product_manager.id,
93                 'notes': "b"+(l and l.order_id.note or ''),
94                 'procurement_id': procurement.id,
95                 'description': content,
96                 'date_deadline': procurement.date_planned,
97                 'state': 'draft',
98                 'partner_id': l and l.order_id.partner_id.id or False,
99                 'company_id': procurement.company_id.id,
100                 'project_id': project_id,
101             },context=context)
102             self.write(cr, uid, [procurement.id],{'task_id':task_id}) 
103             product_obj.write(cr,uid,[procurement.product_id.id],{'project_id':project_id})
104         return task_id
105
106 procurement_order()
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: