[merge]
[odoo/odoo.git] / addons / project / wizard / project_close_task.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 import time
23
24 from osv import fields, osv
25 from tools.translate import _
26 from tools import email_send as email
27
28 class project_close_task(osv.osv_memory):
29     """
30     Close Task
31     """
32     _name = "close.task"
33     _description = "Project Close Task"
34     _columns = {
35         'email': fields.char('E-Mail', size=64, help="Email Address"),
36         'description': fields.text('Description'),
37         }
38
39     def _get_email(self, cr, uid, context=None):
40         if context is None:
41             context = {}
42         email = ''
43         if 'task_id' in context:
44             task = self.pool.get('project.task').browse(cr, uid, context['task_id'])
45             partner_id = task.partner_id or task.project_id.partner_id
46             if partner_id and len(partner_id.address) and partner_id.address[0].email:
47                 email = partner_id.address[0].email
48         return email
49
50     def _get_desc(self, cr, uid, context=None):
51         if context is None:
52             context = {}
53         if 'task_id' in context:
54             task = self.pool.get('project.task').browse(cr, uid, context['task_id'])
55             return task.description or task.name
56         return ''
57
58     _defaults = {
59        'email': _get_email,
60        'description': _get_desc,
61                }
62     
63     def close(self, cr, uid, ids, context=None):
64         if 'task_id' in context:
65             self.pool.get('project.task').write(cr, uid, [context['task_id']], {'state': 'done', 'date_end':time.strftime('%Y-%m-%d %H:%M:%S'), 'remaining_hours': 0.0})
66         return {}
67
68     def confirm(self, cr, uid, ids, context=None):
69         if context is None:
70             context = {}
71         if not 'task_id' in context:
72             return {}
73         close_task = self.read(cr, uid, ids[0], [])
74         to_adr = close_task['email']
75         description = close_task['description']
76         
77         if 'task_id' in context:
78             task_obj = self.pool.get('project.task')
79             for task in task_obj.browse(cr, uid, [context['task_id']], context=context):
80                 project = task.project_id
81                 subject = "Task '%s' closed" % task.name
82                 if task.user_id and task.user_id.address_id and task.user_id.address_id.email:
83                     from_adr = task.user_id.address_id.email
84                     signature = task.user_id.signature
85                 else:
86                     raise osv.except_osv(_('Error'), _("Couldn't send mail because your email address is not configured!"))
87                 if to_adr:
88                     val = {
89                         'name': task.name,
90                         'user_id': task.user_id.name,
91                         'task_id': "%d/%d" % (project.id, task.id),
92                         'date_start': task.date_start,
93                         'date_end': task.date_end,
94                         'state': task.state
95                     }
96                     header = (project.warn_header or '') % val
97                     footer = (project.warn_footer or '') % val
98                     body = u'%s\n%s\n%s\n\n-- \n%s' % (header, description, footer, signature)
99                     email(from_adr, [to_adr], subject, body.encode('utf-8'), email_bcc=[from_adr])
100                     task_obj.write(cr, uid, [task.id], {'state': 'done', 'date_end':time.strftime('%Y-%m-%d %H:%M:%S'), 'remaining_hours': 0.0})
101                 else:
102                     raise osv.except_osv(_('Error'), _("Please specify the email address of partner."))
103         return {}
104
105 project_close_task()
106
107 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: