ac8b2d634f20828766e031f0bbc1d62fb5c8ccd7
[odoo/odoo.git] / addons / event / wizard / make_invoice.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import wizard
24 import netsvc
25 import pooler
26
27 from osv import fields, osv
28 form = """<?xml version="1.0"?>
29 <form string="Create Invoices">
30     <newline />
31     <field name="inv_created"/>
32     <newline />
33     <field name="inv_rejected"/>
34     <newline />
35     <field name="inv_rej_reason" width="400"/>
36 </form>
37 """
38 fields = {
39     'inv_created': {'string':'Invoice Created', 'type':'char', 'readonly':True},
40     'inv_rejected': {'string':'Invoice Rejected', 'type':'char', 'readonly':True},
41     'inv_rej_reason': {'string':'Error Messages', 'type':'text', 'readonly':True},
42 }
43
44 def _makeInvoices(self, cr, uid, data, context):
45     invoices = {}
46     invoice_ids = []
47     create_ids=[]
48     tax_ids=[]
49     pool_obj=pooler.get_pool(cr.dbname)
50
51     inv_create = 0
52     inv_reject = 0
53     inv_rej_reason = ""
54     list_inv = []
55     obj_event_reg=pool_obj.get('event.registration')
56     data_event_reg=obj_event_reg.browse(cr,uid,data['ids'])
57     obj_lines=pool_obj.get('account.invoice.line')
58
59     for reg in data_event_reg:
60         if reg.state=='draft':
61             inv_reject = inv_reject + 1
62             inv_rej_reason += "ID "+str(reg.id)+": Invoice cannot be created if the registration is in draft state. \n"
63             continue
64         if (not reg.tobe_invoiced):
65             inv_reject = inv_reject + 1
66             inv_rej_reason += "ID "+str(reg.id)+": Registration is set as 'Cannot be invoiced'. \n"
67             continue
68         if reg.invoice_id:
69             inv_reject = inv_reject + 1
70             inv_rej_reason += "ID "+str(reg.id)+": Registration already has an invoice linked. \n"
71             continue
72         if not reg.event_id.product_id:
73             inv_reject = inv_reject + 1
74             inv_rej_reason += "ID "+str(reg.id)+": Event related doesn't have any product defined. \n"
75             continue
76         if not reg.partner_invoice_id:
77             inv_reject = inv_reject + 1
78             inv_rej_reason += "ID "+str(reg.id)+": Registration doesn't have any partner to invoice. \n"
79             continue
80         else:
81             val_invoice = pool_obj.get('account.invoice').onchange_partner_id(cr, uid, [], 'out_invoice', reg.partner_invoice_id.id, False, False)
82             val_invoice['value'].update({'partner_id': reg.partner_invoice_id.id})
83             partner_address_id = val_invoice['value']['address_invoice_id']
84
85         if not partner_address_id:
86             inv_reject = inv_reject + 1
87             inv_rej_reason += "ID "+str(reg.id)+": Registered partner doesn't have an address to make the invoice. \n"
88             continue
89
90         inv_create = inv_create + 1
91         value=obj_lines.product_id_change(cr, uid, [], reg.event_id.product_id.id,uom =False, partner_id=reg.partner_invoice_id.id, fposition_id=reg.partner_invoice_id.property_account_position.id)
92         data_product=pool_obj.get('product.product').browse(cr,uid,[reg.event_id.product_id.id])
93         for tax in data_product[0].taxes_id:
94             tax_ids.append(tax.id)
95
96         vals = value['value']
97         c_name = reg.contact_id and ('-' + pool_obj.get('res.partner.contact').name_get(cr, uid, [reg.contact_id.id])[0][1]) or ''
98         vals.update({
99             'name': reg.invoice_label + c_name,
100             'price_unit': reg.unit_price,
101             'quantity': reg.nb_register,
102             'product_id':reg.event_id.product_id.id,
103             'invoice_line_tax_id': [(6,0,tax_ids)],
104         })
105         inv_line_ids = obj_event_reg._create_invoice_lines(cr, uid, [reg.id], vals)
106         val_invoice['value'].update({
107             'origin': reg.invoice_label,
108             'reference': False,
109             'invoice_line': [(6,0,[inv_line_ids])],
110             'comment': "",
111         })
112
113         inv_obj = pool_obj.get('account.invoice')
114         inv_id = inv_obj.create(cr, uid, val_invoice['value'])
115         list_inv.append(inv_id)
116         obj_event_reg.write(cr, uid,reg.id, {'invoice_id' : inv_id,'state':'done'})
117         obj_event_reg._history(cr, uid,[reg.id], 'Invoiced', history=True)
118
119     return {'inv_created' : str(inv_create) , 'inv_rejected' : str(inv_reject) , 'invoice_ids':  list_inv, 'inv_rej_reason': inv_rej_reason}
120
121 class make_invoice(wizard.interface):
122
123     def _list_invoice(self, cr, uid, data, context):
124         pool_obj = pooler.get_pool(cr.dbname)
125         model_data_ids = pool_obj.get('ir.model.data').search(cr,uid,[('model','=','ir.ui.view'),('name','=','invoice_form')])
126         resource_id = pool_obj.get('ir.model.data').read(cr,uid,model_data_ids,fields=['res_id'])[0]['res_id']
127         return {
128             'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
129             'name': 'Invoices',
130             'view_type': 'form',
131             'view_mode': 'tree,form',
132             'res_model': 'account.invoice',
133             'views': [(False,'tree'),(resource_id,'form')],
134             'context': "{'type':'out_invoice'}",
135             'type': 'ir.actions.act_window'
136         }
137
138     states = {
139         'init' : {
140                'actions' : [_makeInvoices],
141                'result': {'type': 'form', 'arch': form, 'fields': fields, 'state':[('end','Ok'),('open','Open')]}
142             },
143
144         'open': {
145             'actions': [],
146             'result': {'type':'action', 'action':_list_invoice, 'state':'end'}
147         }
148
149     }
150
151 make_invoice("event.reg_make_invoice")
152 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
153