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

« back to all changes in this revision

Viewing changes to cci_mission/wizard/create_invoice.py

bugfix in overlay creation system

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
5
#
 
6
# $Id: make_invoice.py 1070 2005-07-29 12:41:24Z nicoe $
 
7
#
 
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
 
13
# Service Company
 
14
#
 
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.
 
19
#
 
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.
 
24
#
 
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.
 
28
#
 
29
##############################################################################
 
30
 
 
31
import wizard
 
32
import netsvc
 
33
import pooler
 
34
 
 
35
from osv import fields, osv
 
36
 
 
37
form = """<?xml version="1.0"?>
 
38
<form string="Create invoices">
 
39
    <field name="inv_created"/>
 
40
    <newline />
 
41
    <field name="inv_rejected"/>
 
42
    <newline />
 
43
    <field name="inv_rej_reason" width="400"/>
 
44
</form>
 
45
"""
 
46
 
 
47
fields = {
 
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},
 
51
}
 
52
 
 
53
def _createInvoices(self, cr, uid, data, context={}):
 
54
    list_inv = []
 
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')
 
60
    inv_create = 0
 
61
    inv_reject = 0
 
62
    inv_rej_reason = ""
 
63
 
 
64
    for data in data_dossier:
 
65
        context.update({'pricelist': data.order_partner_id.property_product_pricelist.id})
 
66
        list = []
 
67
        value = []
 
68
        dict = {}
 
69
        address_contact = False
 
70
        address_invoice = False
 
71
        create_ids = []
 
72
        if data.invoice_id:
 
73
            inv_reject = inv_reject + 1
 
74
            inv_rej_reason += "ID "+str(data.id)+": Already Has an Invoice Linked. \n"
 
75
            continue
 
76
 
 
77
        if not data.to_bill:
 
78
            inv_reject = inv_reject + 1
 
79
            inv_rej_reason += "ID "+str(data.id)+": Cannot Be Billed. \n"
 
80
            continue
 
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"
 
93
            continue
 
94
 
 
95
        context.update({'date':data.date})
 
96
        inv_create = inv_create + 1
 
97
        for lines in data.product_ids :
 
98
 
 
99
            val = {}
 
100
            val['value']={}
 
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})
 
107
 
 
108
            value.append(val)
 
109
 
 
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
 
114
 
 
115
        for prod_id in list:
 
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 })
 
118
 
 
119
            force_member=force_non_member=False
 
120
            if current_model=='cci_missions.legalization':
 
121
                if data.member_price==1:
 
122
                    force_member=True
 
123
                else:
 
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})
 
130
 
 
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]})
 
133
 
 
134
            if prod_id == dict['original']:
 
135
                val['value'].update({'quantity' : data.quantity_original })
 
136
            else:
 
137
                val['value'].update({'quantity' : data.quantity_copies})
 
138
            value.append(val)
 
139
 
 
140
        for val in value:
 
141
            if val['value']['quantity']>0.00:
 
142
                tax_on_line = []
 
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'],
 
150
                        'discount': False,
 
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,
 
155
                    })
 
156
                else:
 
157
                    raise osv.except_osv('Input Error!','No Product Chosen')
 
158
                create_ids.append(inv_id)
 
159
        inv = {
 
160
            'name': data.name,
 
161
            'origin': data.name,
 
162
            'type': 'out_invoice',
 
163
            'reference': False,
 
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,
 
172
            }
 
173
        price = data.total
 
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)
 
179
 
 
180
        obj_dossier.write(cr, uid,data.id, {'invoice_id' : inv_id, 'invoiced_amount':price})
 
181
 
 
182
    return {'inv_created' : str(inv_create) , 'inv_rejected' : str(inv_reject) , 'invoice_ids':  list_inv, 'inv_rej_reason': inv_rej_reason}
 
183
 
 
184
 
 
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']
 
190
        return {
 
191
            'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
 
192
            'name': 'Invoices',
 
193
            'view_type': 'form',
 
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'
 
199
        }
 
200
 
 
201
    states = {
 
202
        'init' : {
 
203
            'actions' : [_createInvoices],
 
204
            'result' : {'type' : 'form' ,   'arch' : form,'fields' : fields,'state' : [('end','Ok'),('open','Open Invoice')]}
 
205
        },
 
206
        'open': {
 
207
            'actions': [],
 
208
            'result': {'type':'action', 'action':_open_invoice, 'state':'end'}
 
209
        }
 
210
    }
 
211
 
 
212
create_invoice("mission.create_invoice")
 
213
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
214