[MERGE] merged branch that corrects the decimal precision on fields. Courtesy of...
[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 osv import fields, osv
22 from tools.translate import _
23 import netsvc
24
25 class sale_make_invoice(osv.osv_memory):
26     _name = "sale.make.invoice"
27     _description = "Sales Make Invoice"
28     _columns = {
29         'grouped': fields.boolean('Group the invoices', help='Check the box to group the invoices for the same customers'),
30         'invoice_date': fields.date('Invoice Date'),
31     }
32     _defaults = {
33         'grouped': False
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         order_obj.action_invoice_create(cr, uid, context.get(('active_ids'), []), data['grouped'], date_inv = data['invoice_date'])
54         wf_service = netsvc.LocalService("workflow")
55         for id in context.get(('active_ids'), []):
56             wf_service.trg_validate(uid, 'sale.order', id, 'manual_invoice', cr)
57
58         for o in order_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
59             for i in o.invoice_ids:
60                 newinv.append(i.id)
61
62         result = mod_obj.get_object_reference(cr, uid, 'account', 'action_invoice_tree1')
63         id = result and result[1] or False
64         result = act_obj.read(cr, uid, [id], context=context)[0]
65         result['domain'] = "[('id','in', ["+','.join(map(str,newinv))+"])]"
66
67         return result
68
69 sale_make_invoice()
70
71 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: