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