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 ##############################################################################
25 from osv
import fields
27 class Partner(osv
.osv
):
28 _inherit
= 'res.partner'
31 'member_ident': fields
.char('Member identifier', size
=64, readonly
=True),
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}
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)
45 if type(ids
) in (int, long,):
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."))
59 'product_id': product_id
,
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
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
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()
83 invoice_tax_obj
.create(cr
, uid
, tax
, context
=context
)
84 if partner
.member_ident
:
85 mbr_id
= partner
.member_ident
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
})
95 class account_invoice_line(osv
.osv
):
96 _inherit
='account.invoice.line'
98 def write(self
, cr
, uid
, ids
, vals
, context
=None):
99 """Overrides orm write method
101 member_line_obj
= self
.pool
.get('membership.membership_line')
102 res
= super(account_invoice_line
, self
).write(cr
, uid
, ids
, vals
, context
=context
)
103 for line
in self
.browse(cr
, uid
, ids
, context
=context
):
104 if line
.invoice_id
.type == 'out_invoice':
105 ml_ids
= member_line_obj
.search(cr
, uid
, [('account_invoice_line', '=', line
.id)], context
=context
)
106 if line
.product_id
and line
.product_id
.membership
and not ml_ids
:
107 # Product line has changed to a membership product
108 date_from
= line
.product_id
.membership_date_from
109 date_to
= line
.product_id
.membership_date_to
110 if line
.invoice_id
.date_invoice
> date_from
and line
.invoice_id
.date_invoice
< date_to
:
111 date_from
= line
.invoice_id
.date_invoice
112 member_line_obj
.create(cr
, uid
, {
113 'partner': line
.invoice_id
.partner_id
.id,
114 'membership_id': line
.product_id
.id,
115 'member_price': line
.price_unit
,
116 'date': time
.strftime('%Y-%m-%d'),
117 'date_from': date_from
,
119 'account_invoice_line': line
.id,
121 if line
.product_id
and not line
.product_id
.membership
and ml_ids
:
122 # Product line has changed to a non membership product
123 member_line_obj
.unlink(cr
, uid
, ml_ids
, context
=context
)
126 account_invoice_line()
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: