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