[FIX] project: on project template duplication, keep original tasks stages
[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 from openerp 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         'invoice_date': fields.date.context_today,
35     }
36
37     def view_init(self, cr, uid, fields_list, context=None):
38         if context is None:
39             context = {}
40         record_id = context and context.get('active_id', False)
41         order = self.pool.get('sale.order').browse(cr, uid, record_id, context=context)
42         if order.state == 'draft':
43             raise osv.except_osv(_('Warning!'), _('You cannot create invoice when sales order is not confirmed.'))
44         return False
45
46     def make_invoices(self, cr, uid, ids, context=None):
47         order_obj = self.pool.get('sale.order')
48         mod_obj = self.pool.get('ir.model.data')
49         act_obj = self.pool.get('ir.actions.act_window')
50         wf_service = netsvc.LocalService("workflow")
51         newinv = []
52         if context is None:
53             context = {}
54         data = self.read(cr, uid, ids)[0]
55         for sale_order in order_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
56             if sale_order.state != 'manual':
57                 raise osv.except_osv(_('Warning!'), _("You shouldn't manually invoice the following sale order %s") % (sale_order.name))
58
59         order_obj.action_invoice_create(cr, uid, context.get(('active_ids'), []), data['grouped'], date_invoice=data['invoice_date'])
60         orders = order_obj.browse(cr, uid, context.get(('active_ids'), []), context=context)
61         for o in orders:
62             for i in o.invoice_ids:
63                 newinv.append(i.id)
64         # Dummy call to workflow, will not create another invoice but bind the new invoice to the subflow
65         for id in [o.id for o in orders if o.order_policy == 'manual']:
66             wf_service.trg_validate(uid, 'sale.order', id, 'manual_invoice', cr)
67         result = mod_obj.get_object_reference(cr, uid, 'account', 'action_invoice_tree1')
68         id = result and result[1] or False
69         result = act_obj.read(cr, uid, [id], context=context)[0]
70         result['domain'] = "[('id','in', [" + ','.join(map(str, newinv)) + "])]"
71
72         return result
73
74 sale_make_invoice()
75
76 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: