~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to cci_mission/wizard/create_invoice.py

  • Committer: Jay vora
  • Date: 2008-06-09 08:43:58 UTC
  • Revision ID: jvo@tinyerp.com-c8b7029321d4f9d451745e462547a609739e1c2c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 pooler
 
33
 
 
34
from osv import fields, osv
 
35
 
 
36
form = """<?xml version="1.0"?>
 
37
<form string="Create invoices">
 
38
        <field name="inv_created"/>
 
39
        <newline />
 
40
        <field name="inv_rejected"/>
 
41
        <newline />
 
42
        <field name="inv_rej_reason" width="400"/>
 
43
</form>
 
44
"""
 
45
 
 
46
fields = {
 
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},
 
50
}
 
51
 
 
52
def _createInvoices(self, cr, uid, data, context={}):
 
53
        list_inv = []
 
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')
 
59
        inv_create = 0
 
60
        inv_reject = 0
 
61
        inv_rej_reason = ""
 
62
 
 
63
        for data in data_dossier:
 
64
                context.update({'pricelist': data.order_partner_id.property_product_pricelist.id})
 
65
                list = []
 
66
                value = []
 
67
                dict = {}
 
68
                address_contact = False
 
69
                address_invoice = False
 
70
                create_ids = []
 
71
                if data.invoice_id:
 
72
                        inv_reject = inv_reject + 1
 
73
                        inv_rej_reason += "ID "+str(data.id)+": Already Has an Invoice Linked. \n"
 
74
                        continue
 
75
 
 
76
                if not data.to_bill:
 
77
                        inv_reject = inv_reject + 1
 
78
                        inv_rej_reason += "ID "+str(data.id)+": Cannot Be Billed. \n"
 
79
                        continue
 
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"
 
92
                        continue
 
93
 
 
94
                context.update({'date':data.date})
 
95
                inv_create = inv_create + 1
 
96
                for lines in data.product_ids :
 
97
 
 
98
                        val = {}
 
99
                        val['value']={}
 
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})
 
106
 
 
107
                        value.append(val)
 
108
 
 
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
 
113
 
 
114
                for prod_id in list:
 
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 })
 
117
 
 
118
                        force_member=force_non_member=False
 
119
                        if current_model=='cci_missions.legalization':
 
120
                                if data.member_price==1:
 
121
                                        force_member=True
 
122
                                else:
 
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})
 
129
 
 
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]})
 
132
 
 
133
                        if prod_id == dict['original']:
 
134
                                val['value'].update({'quantity' : data.quantity_original })
 
135
                        else:
 
136
                                val['value'].update({'quantity' : data.quantity_copies})
 
137
                        value.append(val)
 
138
 
 
139
                for val in value:
 
140
                        if val['value']['quantity']>0.00:
 
141
                                tax_on_line = []
 
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'],
 
149
                                                'discount': False,
 
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,
 
154
                                        })
 
155
                                else:
 
156
                                        raise osv.except_osv('Input Error!','No Product Chosen')
 
157
                                create_ids.append(inv_id)
 
158
                inv = {
 
159
                        'name': data.name,
 
160
                        'origin': data.name,
 
161
                        'type': 'out_invoice',
 
162
                        'reference': False,
 
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,
 
171
                        }
 
172
                price = data.total
 
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)
 
178
 
 
179
                obj_dossier.write(cr, uid,data.id, {'invoice_id' : inv_id, 'invoiced_amount':price})
 
180
 
 
181
        return {'inv_created' : str(inv_create) , 'inv_rejected' : str(inv_reject) , 'invoice_ids':  list_inv, 'inv_rej_reason': inv_rej_reason}
 
182
 
 
183
 
 
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']
 
189
                return {
 
190
                        'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
 
191
                        'name': 'Invoices',
 
192
                        'view_type': 'form',
 
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'
 
198
                }
 
199
 
 
200
        states = {
 
201
                'init' : {
 
202
                        'actions' : [_createInvoices],
 
203
                        'result' : {'type' : 'form' ,   'arch' : form,'fields' : fields,'state' : [('end','Ok'),('open','Open Invoice')]}
 
204
                },
 
205
                'open': {
 
206
                        'actions': [],
 
207
                        'result': {'type':'action', 'action':_open_invoice, 'state':'end'}
 
208
                }
 
209
        }
 
210
 
 
211
create_invoice("mission.create_invoice")