1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
6
# $Id: make_invoice.py 1070 2005-07-29 12:41:24Z nicoe $
8
# WARNING: This program as such is intended to be used by professional
9
# programmers who take the whole responsability of assessing all potential
10
# consequences resulting from its eventual inadequacies and bugs
11
# End users who are looking for a ready-to-use solution with commercial
12
# garantees and support are strongly adviced to contract a Free Software
15
# This program is Free Software; you can redistribute it and/or
16
# modify it under the terms of the GNU General Public License
17
# as published by the Free Software Foundation; either version 2
18
# of the License, or (at your option) any later version.
20
# This program is distributed in the hope that it will be useful,
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
# GNU General Public License for more details.
25
# You should have received a copy of the GNU General Public License
26
# along with this program; if not, write to the Free Software
27
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
##############################################################################
35
from osv import fields, osv
37
form = """<?xml version="1.0"?>
38
<form string="Create invoices">
39
<field name="inv_created"/>
41
<field name="inv_rejected"/>
43
<field name="inv_rej_reason" width="400"/>
48
'inv_created': {'string':'Invoice Created', 'type':'char', 'readonly':True},
49
'inv_rejected': {'string':'Invoice Rejected', 'type':'char', 'readonly':True},
50
'inv_rej_reason': {'string':'Error Messages', 'type':'text', 'readonly':True},
53
def _createInvoices(self, cr, uid, data, context={}):
55
pool_obj = pooler.get_pool(cr.dbname)
56
obj_dossier = pool_obj.get(data['model'])
57
current_model=data['model']
58
data_dossier = obj_dossier.browse(cr,uid,data['ids'])
59
obj_lines=pool_obj.get('account.invoice.line')
64
for data in data_dossier:
65
context.update({'pricelist': data.order_partner_id.property_product_pricelist.id})
69
address_contact = False
70
address_invoice = False
73
inv_reject = inv_reject + 1
74
inv_rej_reason += "ID "+str(data.id)+": Already Has an Invoice Linked. \n"
78
inv_reject = inv_reject + 1
79
inv_rej_reason += "ID "+str(data.id)+": Cannot Be Billed. \n"
81
for add in data.order_partner_id.address:
82
if add.type == 'contact':
83
address_contact = add.id
84
if add.type == 'invoice':
85
address_invoice = add.id
86
if (not address_contact) and (add.type == 'default'):
87
address_contact = add.id
88
if (not address_invoice) and (add.type == 'default'):
89
address_invoice = add.id
90
if not address_invoice:
91
inv_reject = inv_reject + 1
92
inv_rej_reason += "ID "+str(data.id)+": No Partner Address Defined on Billed Customer. \n"
95
context.update({'date':data.date})
96
inv_create = inv_create + 1
97
for lines in data.product_ids :
101
val['value'].update({'name':lines.name})
102
val['value'].update({'account_id':lines.account_id.id})
103
val['value'].update({'product_id' : lines.product_id.id })
104
val['value'].update({'quantity' : lines.quantity })
105
val['value'].update({'uos_id':lines.uos_id.id})
106
val['value'].update({'price_unit':lines.price_unit})
110
list.append(data.type_id.original_product_id.id)
111
dict['original'] = data.type_id.original_product_id.id
112
list.append(data.type_id.copy_product_id.id)
113
dict['copy'] = data.type_id.copy_product_id.id
116
val = obj_lines.product_id_change(cr, uid, [], prod_id,uom =False, partner_id=data.order_partner_id.id)
117
val['value'].update({'product_id' : prod_id })
119
force_member=force_non_member=False
120
if current_model=='cci_missions.legalization':
121
if data.member_price==1:
124
force_non_member=True
125
context.update({'partner_id':data.order_partner_id})
126
context.update({'force_member':force_member})
127
context.update({'force_non_member':force_non_member})
128
context.update({'value_goods':data.goods_value})
129
context.update({'date':data.date})
131
price=pool_obj.get('product.product')._product_price(cr, uid, [prod_id], False, False, context)
132
val['value'].update({'price_unit':price[prod_id]})
134
if prod_id == dict['original']:
135
val['value'].update({'quantity' : data.quantity_original })
137
val['value'].update({'quantity' : data.quantity_copies})
141
if val['value']['quantity']>0.00:
143
if val['value']['product_id'] != False:
144
tax_on_line = pool_obj.get('product.product').browse(cr,uid,val['value']['product_id']).taxes_id
145
inv_id =pool_obj.get('account.invoice.line').create(cr, uid, {
146
'name': val['value']['name'],
147
'account_id':val['value']['account_id'],
148
'price_unit': val['value']['price_unit'],
149
'quantity': val['value']['quantity'],
151
'uos_id': val['value']['uos_id'],
152
'product_id':val['value']['product_id'],
153
'invoice_line_tax_id': [(6,0,tax_on_line)],
154
'note':data.text_on_invoice,
157
raise osv.except_osv('Input Error!','No Product Chosen')
158
create_ids.append(inv_id)
162
'type': 'out_invoice',
164
'account_id': data.order_partner_id.property_account_receivable.id,
165
'partner_id': data.order_partner_id.id,
166
'address_invoice_id':address_invoice,
167
'address_contact_id':address_contact,
168
'invoice_line': [(6,0,create_ids)],
169
'currency_id' :data.order_partner_id.property_product_pricelist.currency_id.id,# 1,
170
'comment': data.text_on_invoice,
171
'payment_term':data.order_partner_id.property_payment_term.id,
174
inv_obj = pool_obj.get('account.invoice')
175
inv_id = inv_obj.create(cr, uid, inv)
176
list_inv.append(inv_id)
177
wf_service = netsvc.LocalService('workflow')
178
wf_service.trg_validate(uid, current_model, data.id, 'invoiced', cr)
180
obj_dossier.write(cr, uid,data.id, {'invoice_id' : inv_id, 'invoiced_amount':price})
182
return {'inv_created' : str(inv_create) , 'inv_rejected' : str(inv_reject) , 'invoice_ids': list_inv, 'inv_rej_reason': inv_rej_reason}
185
class create_invoice(wizard.interface):
186
def _open_invoice(self, cr, uid, data, context):
187
pool_obj = pooler.get_pool(cr.dbname)
188
model_data_ids = pool_obj.get('ir.model.data').search(cr,uid,[('model','=','ir.ui.view'),('name','=','invoice_form')])
189
resource_id = pool_obj.get('ir.model.data').read(cr,uid,model_data_ids,fields=['res_id'])[0]['res_id']
191
'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
194
'view_mode': 'tree,form',
195
'res_model': 'account.invoice',
196
'views': [(False,'tree'),(resource_id,'form')],
197
'context': "{'type':'out_invoice'}",
198
'type': 'ir.actions.act_window'
203
'actions' : [_createInvoices],
204
'result' : {'type' : 'form' , 'arch' : form,'fields' : fields,'state' : [('end','Ok'),('open','Open Invoice')]}
208
'result': {'type':'action', 'action':_open_invoice, 'state':'end'}
212
create_invoice("mission.create_invoice")
213
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: