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 osv import fields, osv
24 from tools.translate import _
26 class project_task_close(osv.osv_memory):
30 _name = "project.task.close"
31 _description = "Project Close Task"
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'),
40 def default_get(self, cr, uid, fields, context=None):
42 This function gets default values
46 record_id = context and context.get('active_id', False) or False
47 task_pool = self.pool.get('project.task')
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
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})
67 def send(self, cr, uid, ids, context=None):
71 task_pool = self.pool.get('project.task')
72 task_id = context.get('active_id', False)
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."))
81 elif data.partner_warn and not data.partner_email:
82 raise osv.except_osv(_('Error'), _("Please specify the email address of Customer."))
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.user_email:
88 from_adr = task.user_id.user_email
89 signature = task.user_id.signature
91 raise osv.except_osv(_('Error'), _("Couldn't send mail because your email address is not configured!"))
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,
104 header_str = (project.warn_header or '')
105 footer_str = (project.warn_footer or '')
106 header = (re.sub(r'\%\W*\(', '%(', header_str)) % val
107 footer = (re.sub(r'\%\W*\(', '%(', footer_str)) % val
109 raise osv.except_osv(_('Error'), _("Invlaid automatic variables used in project header or foooter."))
110 body = u'%s\n%s\n%s\n\n-- \n%s' % (header, data.description or '', footer, signature)
111 if data.manager_warn and data.manager_email:
112 to_adr.append(data.manager_email)
113 if data.partner_warn and data.partner_email:
114 to_adr.append(data.partner_email)
115 mail_id = tools.email_send(from_adr, to_adr, subject, tools.ustr(body), email_bcc=[from_adr])
117 raise osv.except_osv(_('Error'), _("Couldn't send mail! Check the email ids and smtp configuration settings"))
118 return {'type': 'ir.actions.act_window_close'}
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: