+member id
[burette/remembership.git] / remembership.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # Remembership module for OpenERP, Overload membership module
5 # Copyright (C) 2012 L'Heureux Cyclage (<http://www.heureux-cyclage.org>) Ludovic CHEVALIER
6 #
7 # This file is a part of Remembership
8 #
9 # Remembership is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Remembership is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23
24 from osv import osv
25 from osv import fields
26
27 class Partner(osv.osv):
28 _inherit = 'res.partner'
29
30 _columns = {
31 'member_ident': fields.char('Member identifier', size=64, readonly=True),
32 }
33
34 def create_membership_invoice(self, cr, uid, ids, product_id=None, datas=None, context=None):
35 """ Create Customer Invoice of Membership for partners.
36 @param datas: datas has dictionary value which consist Id of Membership product and Cost Amount of Membership.
37 datas = {'membership_product_id': None, 'amount': None}
38 """
39 invoice_obj = self.pool.get('account.invoice')
40 invoice_line_obj = self.pool.get('account.invoice.line')
41 invoice_tax_obj = self.pool.get('account.invoice.tax')
42 product_id = product_id or datas.get('membership_product_id', False)
43 amount = datas.get('amount', 0.0)
44 invoice_list = []
45 if type(ids) in (int, long,):
46 ids = [ids]
47 for partner in self.browse(cr, uid, ids, context=context):
48 account_id = partner.property_account_receivable and partner.property_account_receivable.id or False
49 fpos_id = partner.property_account_position and partner.property_account_position.id or False
50 addr = self.address_get(cr, uid, [partner.id], ['invoice'])
51 if partner.free_member:
52 raise osv.except_osv(_('Error !'),
53 _("Partner is a free Member."))
54 if not addr.get('invoice', False):
55 raise osv.except_osv(_('Error !'),
56 _("Partner doesn't have an address to make the invoice."))
57 quantity = 1
58 line_value = {
59 'product_id': product_id,
60 }
61
62 line_dict = invoice_line_obj.product_id_change(cr, uid, {},
63 product_id, False, quantity, '', 'out_invoice', partner.id, fpos_id, price_unit=amount, context=context)
64 line_value.update(line_dict['value'])
65 line_value['price_unit'] = amount
66 if line_value.get('invoice_line_tax_id', False):
67 tax_tab = [(6, 0, line_value['invoice_line_tax_id'])]
68 line_value['invoice_line_tax_id'] = tax_tab
69
70 invoice_id = invoice_obj.create(cr, uid, {
71 'partner_id': partner.id,
72 'address_invoice_id': addr.get('invoice', False),
73 'account_id': account_id,
74 'fiscal_position': fpos_id or False
75 }, context=context)
76 line_value['invoice_id'] = invoice_id
77 invoice_line_id = invoice_line_obj.create(cr, uid, line_value, context=context)
78 invoice_obj.write(cr, uid, invoice_id, {'invoice_line': [(6, 0, [invoice_line_id])]}, context=context)
79 invoice_list.append(invoice_id)
80 if line_value['invoice_line_tax_id']:
81 tax_value = invoice_tax_obj.compute(cr, uid, invoice_id).values()
82 for tax in tax_value:
83 invoice_tax_obj.create(cr, uid, tax, context=context)
84 if partner.member_ident:
85 mbr_id = partner.member_ident
86 else:
87 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
88 #recompute the membership_state of those partners
89 self.pool.get('res.partner').write(cr, uid, ids, {'member_ident': mbr_id})
90 return invoice_list
91
92 Partner()
93
94
95 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: