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