[FIX]: project task - set to done - pop up to small
[odoo/odoo.git] / addons / project / wizard / project_task_close.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
23 import tools
24 from tools.translate import _
25
26 class project_task_close(osv.osv_memory):
27     """
28     Close Task
29     """
30     _name = "project.task.close"
31     _description = "Project Close Task"
32     _columns = {
33         'manager_warn': fields.boolean("Warn Manager", help="Warn Manager by Email"),
34         'partner_warn': fields.boolean("Warn Customer", help="Warn Customer by Email"),
35         'manager_email': fields.char('Manager Email', size=128, help="Email Address of Project's Manager"),
36         'partner_email': fields.char('Customer Email', size=128, help="Email Address of Customer"),
37         'description': fields.text('Description'),
38     }
39
40     def default_get(self, cr, uid, fields, context=None):
41         """
42         This function gets default values
43         """
44         if context is None:
45             context = {}
46         record_id = context and context.get('active_id', False) or False
47         task_pool = self.pool.get('project.task')
48                 
49         res = super(project_task_close, self).default_get(cr, uid, fields, context=context)
50         task = task_pool.browse(cr, uid, record_id, context=context)
51         project = task.project_id
52         manager = project.user_id or False
53         partner = task.partner_id or task.project_id.partner_id
54         
55         if 'description' in fields:
56             res.update({'description': task.description or False})
57         if 'manager_warn' in fields:
58             res.update({'manager_warn': project.warn_manager or False})
59         if 'partner_warn' in fields:
60             res.update({'partner_warn': project.warn_customer or False})
61         if 'manager_email' in fields:
62             res.update({'manager_email': manager and manager.user_email or False})
63         if partner and len(partner.address) and 'partner_email' in fields:
64             res.update({'partner_email': partner.address[0].email})
65         return res
66
67     def send(self, cr, uid, ids, context=None):
68         if context is None:
69             context = {}
70         
71         task_pool = self.pool.get('project.task')
72         task_id = context.get('active_id', False)
73         if not task_id:
74             return {}
75         task = task_pool.browse(cr, uid, task_id, context=context)
76         for data in self.browse(cr, uid, ids, context=context):
77             # Send Warn Message by Email to Manager and Customer
78             if data.manager_warn and not data.manager_email:
79                 raise osv.except_osv(_('Error'), _("Please specify the email address of Project Manager."))
80
81             elif data.partner_warn and not data.partner_email:
82                 raise osv.except_osv(_('Error'), _("Please specify the email address of Customer."))
83
84             elif data.manager_warn or data.partner_warn:
85                 project = task.project_id
86                 subject = _("Task '%s' Closed") % task.name
87                 if task.user_id and task.user_id.address_id and task.user_id.address_id.email:
88                     from_adr = task.user_id.address_id.email
89                     signature = task.user_id.signature
90                 else:
91                     raise osv.except_osv(_('Error'), _("Couldn't send mail because your email address is not configured!"))
92                 val = {
93                         'name': task.name,
94                         'user_id': task.user_id.name,
95                         'task_id': "%d/%d" % (project.id, task.id),
96                         'date_start': task.date_start,
97                         'date_end': task.date_end,
98                         'state': task.state
99                 }
100
101                 to_adr = []
102                 header = (project.warn_header or '') % val
103                 footer = (project.warn_footer or '') % val
104                 body = u'%s\n%s\n%s\n\n-- \n%s' % (header, task.description, footer, signature)
105                 if data.manager_warn and data.manager_email:
106                     to_adr.append(data.manager_email)
107                 if data.partner_warn and data.partner_email:
108                     to_adr.append(data.partner_email)
109                 mail_id = tools.email_send(from_adr, to_adr, subject, tools.ustr(body), email_bcc=[from_adr])
110                 if not mail_id:
111                     raise osv.except_osv(_('Error'), _("Couldn't send mail! Check the email ids and smtp configuration settings"))
112         return {}
113
114 project_task_close()
115
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: