[FIX] web: when filtering out context, also strip *_view_ref keys
[odoo/odoo.git] / addons / sale / wizard / sale_make_invoice.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20
21 from openerp.osv import fields, osv
22 from openerp.tools.translate import _
23
24 class sale_make_invoice(osv.osv_memory):
25     _name = "sale.make.invoice"
26     _description = "Sales Make Invoice"
27     _columns = {
28         'grouped': fields.boolean('Group the invoices', help='Check the box to group the invoices for the same customers'),
29         'invoice_date': fields.date('Invoice Date'),
30     }
31     _defaults = {
32         'grouped': False,
33         'invoice_date': fields.date.context_today,
34     }
35
36     def view_init(self, cr, uid, fields_list, context=None):
37         if context is None:
38             context = {}
39         record_id = context and context.get('active_id', False)
40         order = self.pool.get('sale.order').browse(cr, uid, record_id, context=context)
41         if order.state == 'draft':
42             raise osv.except_osv(_('Warning!'), _('You cannot create invoice when sales order is not confirmed.'))
43         return False
44
45     def make_invoices(self, cr, uid, ids, context=None):
46         order_obj = self.pool.get('sale.order')
47         mod_obj = self.pool.get('ir.model.data')
48         act_obj = self.pool.get('ir.actions.act_window')
49         newinv = []
50         if context is None:
51             context = {}
52         data = self.read(cr, uid, ids)[0]
53         for sale_order in order_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
54             if sale_order.state != 'manual':
55                 raise osv.except_osv(_('Warning!'), _("You shouldn't manually invoice the following sale order %s") % (sale_order.name))
56
57         order_obj.action_invoice_create(cr, uid, context.get(('active_ids'), []), data['grouped'], date_invoice=data['invoice_date'])
58
59         for o in order_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
60             for i in o.invoice_ids:
61                 newinv.append(i.id)
62
63         result = mod_obj.get_object_reference(cr, uid, 'account', 'action_invoice_tree1')
64         id = result and result[1] or False
65         result = act_obj.read(cr, uid, [id], context=context)[0]
66         result['domain'] = "[('id','in', [" + ','.join(map(str, newinv)) + "])]"
67
68         return result
69
70
71 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: