[FIX] mail, BaseModel, portal_sale: fixes and improvements in the URL
[odoo/odoo.git] / addons / portal_sale / portal_sale.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2012 OpenERP S.A. <http://openerp.com>
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 import SUPERUSER_ID
23 from openerp.osv import osv, fields
24
25
26 class sale_order(osv.Model):
27     _inherit = 'sale.order'
28
29     # make the real method inheritable
30     _payment_block_proxy = lambda self, *a, **kw: self._portal_payment_block(*a, **kw)
31
32     _columns = {
33         'portal_payment_options': fields.function(_payment_block_proxy, type="html", string="Portal Payment Options"),
34     }
35
36     def _portal_payment_block(self, cr, uid, ids, fieldname, arg, context=None):
37         result = dict.fromkeys(ids, False)
38         payment_acquirer = self.pool['payment.acquirer']
39         for this in self.browse(cr, uid, ids, context=context):
40             if this.state not in ('draft', 'cancel') and not this.invoiced:
41                 result[this.id] = payment_acquirer.render_payment_block(
42                     cr, uid, this.name, this.amount_total, this.pricelist_id.currency_id.id,
43                     partner_id=this.partner_id.id, company_id=this.company_id.id, context=context)
44         return result
45
46     def action_quotation_send(self, cr, uid, ids, context=None):
47         '''  Override to use a modified template that includes a portal signup link '''
48         action_dict = super(sale_order, self).action_quotation_send(cr, uid, ids, context=context)
49         try:
50             template_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'portal_sale', 'email_template_edi_sale')[1]
51             # assume context is still a dict, as prepared by super
52             ctx = action_dict['context']
53             ctx['default_template_id'] = template_id
54             ctx['default_use_template'] = True
55         except Exception:
56             pass
57         return action_dict
58
59     def action_button_confirm(self, cr, uid, ids, context=None):
60         # fetch the partner's id and subscribe the partner to the sale order
61         assert len(ids) == 1
62         document = self.browse(cr, uid, ids[0], context=context)
63         partner = document.partner_id
64         if partner.id not in document.message_follower_ids:
65             self.message_subscribe(cr, uid, ids, [partner.id], context=context)
66         return super(sale_order, self).action_button_confirm(cr, uid, ids, context=context)
67
68     def get_signup_url(self, cr, uid, ids, context=None):
69         assert len(ids) == 1
70         document = self.browse(cr, uid, ids[0], context=context)
71         contex_signup = dict(context, signup_valid=True)
72         return self.pool['res.partner']._get_signup_url_for_action(
73             cr, uid, [document.partner_id.id], action='mail.action_mail_redirect',
74             model=self._name, res_id=document.id, context=contex_signup,
75         )[document.partner_id.id]
76
77     def get_formview_action(self, cr, uid, id, context=None):
78         user = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
79         if user.share:
80             document = self.browse(cr, uid, id, context=context)
81             action_xmlid = 'action_quotations_portal' if document.state in ('draft', 'sent') else 'action_orders_portal'
82             return self.pool['ir.actions.act_window'].for_xml_id(cr, uid, 'portal_sale', action_xmlid, context=context)
83         return super(sale_order, self).get_formview_action(cr, uid, id, context=context)
84
85
86 class account_invoice(osv.Model):
87     _inherit = 'account.invoice'
88
89     # make the real method inheritable
90     _payment_block_proxy = lambda self, *a, **kw: self._portal_payment_block(*a, **kw)
91
92     _columns = {
93         'portal_payment_options': fields.function(_payment_block_proxy, type="html", string="Portal Payment Options"),
94     }
95
96     def _portal_payment_block(self, cr, uid, ids, fieldname, arg, context=None):
97         result = dict.fromkeys(ids, False)
98         payment_acquirer = self.pool.get('payment.acquirer')
99         for this in self.browse(cr, uid, ids, context=context):
100             if this.type == 'out_invoice' and this.state not in ('draft', 'done') and not this.reconciled:
101                 result[this.id] = payment_acquirer.render_payment_block(
102                     cr, uid, this.number, this.residual, this.currency_id.id,
103                     partner_id=this.partner_id.id, company_id=this.company_id.id, context=context)
104         return result
105
106     def action_invoice_sent(self, cr, uid, ids, context=None):
107         '''  Override to use a modified template that includes a portal signup link '''
108         action_dict = super(account_invoice, self).action_invoice_sent(cr, uid, ids, context=context)
109         try:
110             template_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'portal_sale', 'email_template_edi_invoice')[1]
111             # assume context is still a dict, as prepared by super
112             ctx = action_dict['context']
113             ctx['default_template_id'] = template_id
114             ctx['default_use_template'] = True
115         except Exception:
116             pass
117         return action_dict
118
119     def invoice_validate(self, cr, uid, ids, context=None):
120         # fetch the partner's id and subscribe the partner to the invoice
121         for invoice in self.browse(cr, uid, ids, context=context):
122             partner = invoice.partner_id
123             if partner.id not in invoice.message_follower_ids:
124                 self.message_subscribe(cr, uid, [invoice.id], [partner.id], context=context)
125         return super(account_invoice, self).invoice_validate(cr, uid, ids, context=context)
126
127     def get_signup_url(self, cr, uid, ids, context=None):
128         assert len(ids) == 1
129         document = self.browse(cr, uid, ids[0], context=context)
130         contex_signup = dict(context, signup_valid=True)
131         return self.pool['res.partner']._get_signup_url_for_action(
132             cr, uid, [document.partner_id.id], action='mail.action_mail_redirect',
133             model=self._name, res_id=document.id, context=contex_signup,
134         )[document.partner_id.id]
135
136     def get_formview_action(self, cr, uid, id, context=None):
137         user = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
138         if user.share:
139             return self.pool['ir.actions.act_window'].for_xml_id(cr, uid, 'portal_sale', 'portal_action_invoices', context=context)
140         return super(sale_order, self).get_formview_action(cr, uid, id, context=context)
141
142
143 class mail_mail(osv.osv):
144     _inherit = 'mail.mail'
145
146     def _postprocess_sent_message(self, cr, uid, mail, context=None, mail_sent=True):
147         if mail_sent and mail.model == 'sale.order':
148             so_obj = self.pool.get('sale.order')
149             order = so_obj.browse(cr, uid, mail.res_id, context=context)
150             partner = order.partner_id
151             # Add the customer in the SO as follower
152             if partner.id not in order.message_follower_ids:
153                 so_obj.message_subscribe(cr, uid, [mail.res_id], [partner.id], context=context)
154             # Add all recipients of the email as followers
155             for p in mail.partner_ids:
156                 if p.id not in order.message_follower_ids:
157                     so_obj.message_subscribe(cr, uid, [mail.res_id], [p.id], context=context)
158         return super(mail_mail, self)._postprocess_sent_message(cr, uid, mail=mail, context=context, mail_sent=mail_sent)