1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
27 from osv import fields, osv
28 form = """<?xml version="1.0"?>
29 <form string="Create Invoices">
31 <field name="inv_created"/>
33 <field name="inv_rejected"/>
35 <field name="inv_rej_reason" width="400"/>
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},
44 def _makeInvoices(self, cr, uid, data, context):
49 pool_obj=pooler.get_pool(cr.dbname)
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')
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"
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"
69 inv_reject = inv_reject + 1
70 inv_rej_reason += "ID "+str(reg.id)+": Registration already has an invoice linked. \n"
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"
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"
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']
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"
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)
97 c_name = reg.contact_id and ('-' + pool_obj.get('res.partner.contact').name_get(cr, uid, [reg.contact_id.id])[0][1]) or ''
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)],
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,
109 'invoice_line': [(6,0,[inv_line_ids])],
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)
119 return {'inv_created' : str(inv_create) , 'inv_rejected' : str(inv_reject) , 'invoice_ids': list_inv, 'inv_rej_reason': inv_rej_reason}
121 class make_invoice(wizard.interface):
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']
128 'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
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'
140 'actions' : [_makeInvoices],
141 'result': {'type': 'form', 'arch': form, 'fields': fields, 'state':[('end','Ok'),('open','Open')]}
146 'result': {'type':'action', 'action':_list_invoice, 'state':'end'}
151 make_invoice("event.reg_make_invoice")
152 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: