1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # Remembership module for OpenERP, Overload membership module
5 # Copyright (C) 2012 L'Heureux Cyclage (<http://www.heureux-cyclage.org>) Ludovic CHEVALIER
7 # This file is a part of Remembership
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.
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.
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/>.
22 ##############################################################################
27 from osv
import fields
29 class Partner(osv
.osv
):
30 _inherit
= 'res.partner'
33 'member_ident': fields
.char('Member identifier', size
=64, readonly
=True),
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}
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)
47 if type(ids
) in (int, long,):
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."))
61 'product_id': product_id
,
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
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
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()
85 invoice_tax_obj
.create(cr
, uid
, tax
, context
=context
)
86 if partner
.member_ident
:
87 mbr_id
= partner
.member_ident
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
})
97 class account_invoice_line(osv
.osv
):
98 _inherit
='account.invoice.line'
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
})
109 account_invoice_line()
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: