[Identifiant] +numerotation depuis la facture
[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 import time
25
26 from osv import osv
27 from osv import fields
28
29 class Partner(osv.osv):
30 _inherit = 'res.partner'
31
32 _columns = {
33 'member_ident': fields.char('Member identifier', size=64, readonly=True),
34 }
35
36 def create_membership_invoice(self, cr, uid, ids, product_id=None, datas=None, context=None):
37 """ Create Customer Invoice of Membership for partners.
38 @param datas: datas has dictionary value which consist Id of Membership product and Cost Amount of Membership.
39 datas = {'membership_product_id': None, 'amount': None}
40 """
41 invoice_obj = self.pool.get('account.invoice')
42 invoice_line_obj = self.pool.get('account.invoice.line')
43 invoice_tax_obj = self.pool.get('account.invoice.tax')
44 product_id = product_id or datas.get('membership_product_id', False)
45 amount = datas.get('amount', 0.0)
46 invoice_list = []
47 if type(ids) in (int, long,):
48 ids = [ids]
49 for partner in self.browse(cr, uid, ids, context=context):
50 account_id = partner.property_account_receivable and partner.property_account_receivable.id or False
51 fpos_id = partner.property_account_position and partner.property_account_position.id or False
52 addr = self.address_get(cr, uid, [partner.id], ['invoice'])
53 if partner.free_member:
54 raise osv.except_osv(_('Error !'),
55 _("Partner is a free Member."))
56 if not addr.get('invoice', False):
57 raise osv.except_osv(_('Error !'),
58 _("Partner doesn't have an address to make the invoice."))
59 quantity = 1
60 line_value = {
61 'product_id': product_id,
62 }
63
64 line_dict = invoice_line_obj.product_id_change(cr, uid, {},
65 product_id, False, quantity, '', 'out_invoice', partner.id, fpos_id, price_unit=amount, context=context)
66 line_value.update(line_dict['value'])
67 line_value['price_unit'] = amount
68 if line_value.get('invoice_line_tax_id', False):
69 tax_tab = [(6, 0, line_value['invoice_line_tax_id'])]
70 line_value['invoice_line_tax_id'] = tax_tab
71
72 invoice_id = invoice_obj.create(cr, uid, {
73 'partner_id': partner.id,
74 'address_invoice_id': addr.get('invoice', False),
75 'account_id': account_id,
76 'fiscal_position': fpos_id or False
77 }, context=context)
78 line_value['invoice_id'] = invoice_id
79 invoice_line_id = invoice_line_obj.create(cr, uid, line_value, context=context)
80 invoice_obj.write(cr, uid, invoice_id, {'invoice_line': [(6, 0, [invoice_line_id])]}, context=context)
81 invoice_list.append(invoice_id)
82 if line_value['invoice_line_tax_id']:
83 tax_value = invoice_tax_obj.compute(cr, uid, invoice_id).values()
84 for tax in tax_value:
85 invoice_tax_obj.create(cr, uid, tax, context=context)
86 if partner.member_ident:
87 mbr_id = partner.member_ident
88 else:
89 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
90 #recompute the membership_state of those partners
91 self.pool.get('res.partner').write(cr, uid, ids, {'member_ident': mbr_id})
92 return invoice_list
93
94 Partner()
95
96
97 class account_invoice_line(osv.osv):
98 _inherit='account.invoice.line'
99
100 def create(self, cr, uid, vals, context=None):
101 result = super(account_invoice_line, self).create(cr, uid, vals, context=context)
102 line = self.browse(cr, uid, result, context=context)
103 if line.invoice_id.type == 'out_invoice':
104 if line.product_id and line.product_id.membership and not line.invoice_id.partner_id.member_ident:
105 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
106 self.pool.get('res.partner').write(cr, uid, line.invoice_id.partner_id.id, {'member_ident': mbr_id})
107 return result
108
109 account_invoice_line()
110
111
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: