1
##############################################################################
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5
# $Id: make_invoice.py 1070 2005-07-29 12:41:24Z nicoe $
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
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.
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.
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.
28
##############################################################################
34
from osv import fields, osv
36
form = """<?xml version="1.0"?>
37
<form string="Create invoices">
38
<field name="inv_created"/>
40
<field name="inv_rejected"/>
42
<field name="inv_rej_reason" width="400"/>
47
'inv_created': {'string':'Invoice Created', 'type':'char', 'readonly':True},
48
'inv_rejected': {'string':'Invoice Rejected', 'type':'char', 'readonly':True},
49
'inv_rej_reason': {'string':'Error Messages', 'type':'text', 'readonly':True},
52
def _createInvoices(self, cr, uid, data, context={}):
54
pool_obj = pooler.get_pool(cr.dbname)
55
obj_dossier = pool_obj.get(data['model'])
56
current_model=data['model']
57
data_dossier = obj_dossier.browse(cr,uid,data['ids'])
58
obj_lines=pool_obj.get('account.invoice.line')
63
for data in data_dossier:
64
context.update({'pricelist': data.order_partner_id.property_product_pricelist.id})
68
address_contact = False
69
address_invoice = False
72
inv_reject = inv_reject + 1
73
inv_rej_reason += "ID "+str(data.id)+": Already Has an Invoice Linked. \n"
77
inv_reject = inv_reject + 1
78
inv_rej_reason += "ID "+str(data.id)+": Cannot Be Billed. \n"
80
for add in data.order_partner_id.address:
81
if add.type == 'contact':
82
address_contact = add.id
83
if add.type == 'invoice':
84
address_invoice = add.id
85
if (not address_contact) and (add.type == 'default'):
86
address_contact = add.id
87
if (not address_invoice) and (add.type == 'default'):
88
address_invoice = add.id
89
if not address_invoice:
90
inv_reject = inv_reject + 1
91
inv_rej_reason += "ID "+str(data.id)+": No Partner Address Defined on Billed Customer. \n"
94
context.update({'date':data.date})
95
inv_create = inv_create + 1
96
for lines in data.product_ids :
100
val['value'].update({'name':lines.name})
101
val['value'].update({'account_id':lines.account_id.id})
102
val['value'].update({'product_id' : lines.product_id.id })
103
val['value'].update({'quantity' : lines.quantity })
104
val['value'].update({'uos_id':lines.uos_id.id})
105
val['value'].update({'price_unit':lines.price_unit})
109
list.append(data.type_id.original_product_id.id)
110
dict['original'] = data.type_id.original_product_id.id
111
list.append(data.type_id.copy_product_id.id)
112
dict['copy'] = data.type_id.copy_product_id.id
115
val = obj_lines.product_id_change(cr, uid, [], prod_id,uom =False, partner_id=data.order_partner_id.id)
116
val['value'].update({'product_id' : prod_id })
118
force_member=force_non_member=False
119
if current_model=='cci_missions.legalization':
120
if data.member_price==1:
123
force_non_member=True
124
context.update({'partner_id':data.order_partner_id})
125
context.update({'force_member':force_member})
126
context.update({'force_non_member':force_non_member})
127
context.update({'value_goods':data.goods_value})
128
context.update({'date':data.date})
130
price=pool_obj.get('product.product')._product_price(cr, uid, [prod_id], False, False, context)
131
val['value'].update({'price_unit':price[prod_id]})
133
if prod_id == dict['original']:
134
val['value'].update({'quantity' : data.quantity_original })
136
val['value'].update({'quantity' : data.quantity_copies})
140
if val['value']['quantity']>0.00:
142
if val['value']['product_id'] != False:
143
tax_on_line = pool_obj.get('product.product').browse(cr,uid,val['value']['product_id']).taxes_id
144
inv_id =pool_obj.get('account.invoice.line').create(cr, uid, {
145
'name': val['value']['name'],
146
'account_id':val['value']['account_id'],
147
'price_unit': val['value']['price_unit'],
148
'quantity': val['value']['quantity'],
150
'uos_id': val['value']['uos_id'],
151
'product_id':val['value']['product_id'],
152
'invoice_line_tax_id': [(6,0,tax_on_line)],
153
'note':data.text_on_invoice,
156
raise osv.except_osv('Input Error!','No Product Chosen')
157
create_ids.append(inv_id)
161
'type': 'out_invoice',
163
'account_id': data.order_partner_id.property_account_receivable.id,
164
'partner_id': data.order_partner_id.id,
165
'address_invoice_id':address_invoice,
166
'address_contact_id':address_contact,
167
'invoice_line': [(6,0,create_ids)],
168
'currency_id' :data.order_partner_id.property_product_pricelist.currency_id.id,# 1,
169
'comment': data.text_on_invoice,
170
'payment_term':data.order_partner_id.property_payment_term.id,
173
inv_obj = pool_obj.get('account.invoice')
174
inv_id = inv_obj.create(cr, uid, inv)
175
list_inv.append(inv_id)
176
wf_service = netsvc.LocalService('workflow')
177
wf_service.trg_validate(uid, current_model, data.id, 'invoiced', cr)
179
obj_dossier.write(cr, uid,data.id, {'invoice_id' : inv_id, 'invoiced_amount':price})
181
return {'inv_created' : str(inv_create) , 'inv_rejected' : str(inv_reject) , 'invoice_ids': list_inv, 'inv_rej_reason': inv_rej_reason}
184
class create_invoice(wizard.interface):
185
def _open_invoice(self, cr, uid, data, context):
186
pool_obj = pooler.get_pool(cr.dbname)
187
model_data_ids = pool_obj.get('ir.model.data').search(cr,uid,[('model','=','ir.ui.view'),('name','=','invoice_form')])
188
resource_id = pool_obj.get('ir.model.data').read(cr,uid,model_data_ids,fields=['res_id'])[0]['res_id']
190
'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
193
'view_mode': 'tree,form',
194
'res_model': 'account.invoice',
195
'views': [(False,'tree'),(resource_id,'form')],
196
'context': "{'type':'out_invoice'}",
197
'type': 'ir.actions.act_window'
202
'actions' : [_createInvoices],
203
'result' : {'type' : 'form' , 'arch' : form,'fields' : fields,'state' : [('end','Ok'),('open','Open Invoice')]}
207
'result': {'type':'action', 'action':_open_invoice, 'state':'end'}
211
create_invoice("mission.create_invoice")