passing modules in GPL-3
[odoo/odoo.git] / addons / membership / wizard / invoice_membership.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22 from osv import fields
23 import wizard
24 import pooler
25 import time
26
27 def _invoice_membership(self, cr, uid, data, context):
28     partner_ids = data['ids']
29     product_id = data['form']['product']
30
31     pool = pooler.get_pool(cr.dbname)
32
33     cr.execute('''
34             SELECT partner_id, id, type
35             FROM res_partner_address
36             WHERE partner_id IN (%s)
37             ''' % ','.join([str(id) for id in partner_ids])
38             )
39     fetchal = cr.fetchall()
40     if not fetchal:
41         raise wizard.except_wizard('Error !', 'No Address defined for this partner')
42     partner_address_ids = {}
43     for x in range(len(fetchal)):
44         pid = fetchal[x][0]
45         id = fetchal[x][1]
46         type = fetchal[x][2]
47
48         if partner_address_ids.has_key(pid) and partner_address_ids[pid]['type'] == 'invoice':
49             continue
50         partner_address_ids[pid] = {'id': id, 'type': type}
51
52
53     invoice_list= []
54     invoice_obj = pool.get('account.invoice')
55     partner_obj = pool.get('res.partner')
56     product_obj = pool.get('product.product')
57     invoice_line_obj = pool.get(('account.invoice.line'))
58     invoice_tax_obj = pool.get(('account.invoice.tax'))
59     product = product_obj.read(cr, uid, product_id, ['uom_id'])
60
61     for partner_id in partner_ids:
62         account_id = partner_obj.read(cr, uid, partner_id, ['property_account_receivable'])['property_account_receivable'][0]
63         invoice_id = invoice_obj.create(cr, uid, {
64             'partner_id' : partner_id,
65             'address_invoice_id': partner_address_ids[partner_id]['id'],
66             'account_id': account_id,
67             }
68         )
69
70         line_value =  {
71             'invoice_id' : invoice_id,
72             'product_id' : product_id,
73             }
74
75         quantity = 1
76
77         line_dict = invoice_line_obj.product_id_change(cr, uid, {}, product_id, product['uom_id'][0], quantity, '', 'out_invoice', partner_id, context=context)
78
79         line_value.update(line_dict['value'])
80         if line_value['invoice_line_tax_id']:
81             tax_tab = [(6, 0, line_value['invoice_line_tax_id'])]
82             line_value['invoice_line_tax_id'] = tax_tab
83         else:
84             print "no tax"
85         invoice_line_id = invoice_line_obj.create(cr, uid, line_value)
86         invoice_list.append(invoice_id)
87         if line_value['invoice_line_tax_id']:
88             invoice_obj.write(cr, uid, [invoice_id], {'tax_line':tax_tab})
89             tax_value = invoice_tax_obj.compute(cr, uid, invoice_id).values()[0]
90             invoice_tax_obj.create(cr, uid, tax_value)
91
92     value = {
93             'domain': [
94                 ('id', 'in', invoice_list),
95                 ],
96             'name': 'Membership invoice',
97             'view_type': 'form',
98             'view_mode': 'tree,form',
99             'res_model': 'account.invoice',
100             'type': 'ir.actions.act_window',
101         }
102
103     return value
104
105 wizard_arch= """<?xml version="1.0"?>
106 <form string="Choose invoice details">
107     <field
108         name="product"
109         domain="[('membership','=','True')]"
110         context="product='membership_product'"
111         />
112 </form>"""
113
114
115
116 class wizard_invoice_membership(wizard.interface):
117
118     states = {
119
120         'init' : {
121             'actions' : [],
122             'result' : {
123                 'type' : 'form',
124                 'arch' : wizard_arch,
125                 'fields' : {
126                         'product': {
127                             'string': 'Membership product',
128                             'type': 'many2one',
129                             'relation': 'product.product',
130                             'required': True
131                         },
132                 },
133                 'state' : [('end', 'Cancel'),('ok', 'Confirm') ]}
134         },
135
136         'ok' : {
137             'actions' : [],
138             'result' : {'type' : 'action',
139                         'action': _invoice_membership,
140                         'state' : 'end'
141             },
142         },
143
144     }
145
146 wizard_invoice_membership("wizard_invoice_membership")
147
148 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
149