eb01efd89b55a467d1ba0a48c5999059284202c3
[odoo/odoo.git] / addons / sale / wizard / wizard_sale_line_invoice.py
1 ##############################################################################
2 #
3 # Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id: make_invoice.py 1070 2005-07-29 12:41:24Z nicoe $
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 import wizard
31 import netsvc
32 import ir
33 import pooler
34
35 invoice_form = """<?xml version="1.0"?>
36 <form string="Create invoices">
37         <separator colspan="4" string="Do you really want to create the invoices ?" />
38         <field name="grouped" />
39 </form>
40 """
41
42 invoice_fields = {
43         'grouped' : {'string':'Group the invoices', 'type':'boolean', 'default': lambda *a: False}
44 }
45
46 def _makeInvoices(self, cr, uid, data, context):
47         pool = pooler.get_pool(cr.dbname)
48         res = False
49         factures = {}
50
51         #TODO: merge with sale.py/make_invoice
52         def make_invoice(order, lines):
53                 a = order.partner_id.property_account_receivable[0]
54                 inv = {
55                         'name': order.name,
56                         'origin': 'SO:'+str(order.id)+':'+order.name,
57                         'reference': "P%dSO%d" % (order.partner_id.id, order.id),
58                         'account_id': a,
59                         'partner_id': order.partner_id.id,
60                         'address_invoice_id': order.partner_invoice_id.id,
61                         'address_contact_id': order.partner_invoice_id.id,
62                         'project_id': order.project_id.id,
63                         'invoice_line': [(6,0,lines)],
64                         'currency_id' : order.pricelist_id.currency_id.id,
65                 }
66                 inv_id = pool.get('account.invoice').create(cr, uid, inv, {'type' : 'customer_invoice'})
67                 return inv_id
68
69         for line in pool.get('sale.order.line').browse(cr,uid,data['ids']):
70                 if not line.invoiced:
71                         if not line.order_id.id in factures:
72                                 factures[line.order_id.id] = []
73                         line_id = pool.get('sale.order.line').invoice_line_create(cr, uid, [line.id])
74                         for lid in line_id:
75                                 factures[line.order_id.id].append((line, lid))
76                         pool.get('sale.order.line').write(cr, uid, [line.id], {'invoiced':True})
77
78         for result in factures.values():
79                 order = result[0][0].order_id
80                 il = map(lambda x: x[1], result)
81                 res = make_invoice(order, il)
82                 cr.execute('insert into sale_order_invoice_rel (order_id,invoice_id) values (%d,%d)', (order.id, res))
83         return {}
84
85 class line_make_invoice(wizard.interface):
86         states = {
87                 'init' : {
88                         'actions' : [],
89                         'result' : {'type' : 'form', 'arch' : invoice_form, 'fields' : invoice_fields, 'state' : [('end', 'Cancel'),('invoice', 'Create invoices') ]}
90                 },
91                 'invoice' : {
92                         'actions' : [_makeInvoices],
93                         'result' : {'type':'state', 'state':'end'}
94                 },
95         }
96 line_make_invoice("sale.order.line.make_invoice")