ee17cb133829354e55930789be608aa7c3aeaedb
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 from datetime
import datetime
, date
28 from dateutil
.relativedelta
import relativedelta
31 class Partner(osv
.osv
):
32 _inherit
= 'res.partner'
35 'member_ident': fields
.char('Member identifier', size
=64),
36 'associate_members': fields
.one2many('res.partner', 'associate_member', 'Associate members', help='Members who are associated to this partner.'),
40 ('member_ident_uniq', 'unique(member_ident, company_id)', 'The member identifier must be unique !'),
43 def create_membership_invoice(self
, cr
, uid
, ids
, product_id
=None, datas
=None, context
=None):
44 # NOTE: use context to supply date_from to account_invoice_line.create
45 ctx
= context
and context
.copy() or {}
46 ctx
['date_from'] = datas
.get('date_from', None)
47 return super(Partner
, self
).create_membership_invoice(cr
, uid
, ids
, product_id
=product_id
, datas
=datas
, context
=ctx
)
52 class Product(osv
.osv
):
53 _inherit
= 'product.product'
56 'membership_grouped': fields
.boolean('Grouped membership product', help='Check if it\'s a grouped membership product.'),
57 'membership_date2date': fields
.boolean('Date to date membership product', required
=False, help='Check if it\'s a date to date membership product.'),
63 class account_invoice_line(osv
.osv
):
64 _inherit
= 'account.invoice.line'
66 def write(self
, cr
, uid
, ids
, vals
, context
=None):
67 member_line_obj
= self
.pool
.get('membership.membership_line')
68 res
= super(account_invoice_line
, self
).write(cr
, uid
, ids
, vals
, context
=context
)
69 for line
in self
.browse(cr
, uid
, ids
, context
=context
):
70 if line
.invoice_id
.type == 'out_invoice':
71 ml_ids
= member_line_obj
.search(cr
, uid
, [('account_invoice_line', '=', line
.id)], context
=context
)
72 if line
.product_id
and line
.product_id
.membership
:
73 member_line_id
= member_line_obj
.search(cr
, uid
74 , [('partner' , '=', line
.invoice_id
.partner_id
.id)
75 ,('account_invoice_line', '=', line
.id)]
78 for member_line
in member_line_obj
.browse(cr
, uid
, member_line_id
, context
=context
):
79 # NOTE: get member_line created in membership.account_invoice_line.create
80 date_from
= member_line
.date_from
81 date_to
= member_line
.date_to
82 if line
.product_id
.membership_date2date
:
83 date_from
= context
['date_from'] and datetime
.strptime(context
['date_from'], "%Y-%m-%d") or date
.today()
84 date_to
= date_from
+ relativedelta(months
= +12) # TODO: parameterize this delta?
85 date_from
= date_from
.strftime("%Y-%m-%d")
86 date_to
= date_to
.strftime("%Y-%m-%d")
87 member_line_obj
.write(cr
, uid
, member_line
.id
88 , {'date_from': date_from
92 if line
.product_id
.membership_grouped
:
93 if line
.invoice_id
.partner_id
.associate_members
:
94 for associate_member
in line
.invoice_id
.partner_id
.associate_members
:
95 member_line_obj
.create(cr
, uid
, {
96 'partner': associate_member
.id,
97 'membership_id': line
.product_id
.id,
98 'member_price': line
.price_unit
,
99 'date': time
.strftime('%Y-%m-%d'),
100 'date_from': date_from
,
102 'account_invoice_line': line
.id,
105 print("DEV: mettre une contrainte pour l'objet\
106 membership.membership_line interdisant les\
107 adhésions groupées reliées à des partenaires\
108 sans membres associés")
110 associate_ml_ids
= member_line_obj
.search(cr
, uid
, [('account_invoice_line', '=', line
.id), ('partner', '!=', line
.invoice_id
.partner_id
.id)], context
=context
)
111 member_line_obj
.unlink(cr
, uid
, associate_ml_ids
, context
=context
)
113 #Define member ident if it's necessary
114 partners
= [line
.invoice_id
.partner_id
]
115 if line
.invoice_id
.partner_id
.associate_members
:
116 partners
.extend(line
.invoice_id
.partner_id
.associate_members
)
118 if not i
.member_ident
:
119 mbr_id
= self
.pool
.get('ir.sequence').get(cr
, uid
, 'member_ident')
120 self
.pool
.get('res.partner').write(cr
, uid
, i
.id, {'member_ident': mbr_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 def create(self
, cr
, uid
, vals
, context
=None):
127 member_line_obj
= self
.pool
.get('membership.membership_line')
128 res
= super(account_invoice_line
, self
).create(cr
, uid
, vals
, context
=context
)
129 line
= self
.browse(cr
, uid
, res
, context
=context
)
131 if line
.invoice_id
.type == 'out_invoice' and line
.product_id
and line
.product_id
.membership
:
132 member_line_id
= member_line_obj
.search(cr
, uid
133 , [('partner' , '=', line
.invoice_id
.partner_id
.id)
134 ,('account_invoice_line', '=', line
.id)]
137 for member_line
in member_line_obj
.browse(cr
, uid
, member_line_id
, context
=context
):
138 # NOTE: get member_line created in membership.account_invoice_line.create
139 date_from
= member_line
.date_from
140 date_to
= member_line
.date_to
141 if line
.product_id
.membership_date2date
:
142 print "DEV: line.product_id.membership_date2date"
143 date_from
= context
['date_from'] and datetime
.strptime(context
['date_from'], "%Y-%m-%d") or date
.today()
144 date_to
= date_from
+ relativedelta(months
= +12) # TODO: parameterize this delta?
145 date_from
= date_from
.strftime("%Y-%m-%d")
146 date_to
= date_to
.strftime("%Y-%m-%d")
147 print ("DEV: date_from: %s" % str(date_from
))
148 print ("DEV: date_to : %s" % str(date_to
))
149 member_line_obj
.write(cr
, uid
, member_line
.id
150 , {'date_from': date_from
154 partners
= [line
.invoice_id
.partner_id
]
155 if line
.product_id
.membership_grouped
and line
.invoice_id
.partner_id
.associate_members
:
156 partners
.extend(line
.invoice_id
.partner_id
.associate_members
)
157 #Adding membership lines just for associate partners
158 for associate_member
in line
.invoice_id
.partner_id
.associate_members
:
159 print ("DEV: [associate] date_from: %s" % str(date_from
))
160 print ("DEV: [associate] date_to : %s" % str(date_to
))
161 member_line_obj
.create(cr
, uid
, {
162 'partner': associate_member
.id,
163 'membership_id': line
.product_id
.id,
164 'member_price': line
.price_unit
,
165 'date': time
.strftime('%Y-%m-%d'),
166 'date_from': date_from
,
168 'account_invoice_line': line
.id,
170 #Define member ident if it's necessary
172 if not i
.member_ident
:
173 mbr_id
= self
.pool
.get('ir.sequence').get(cr
, uid
, 'member_ident')
174 self
.pool
.get('res.partner').write(cr
, uid
, i
.id, {'member_ident': mbr_id
})
177 account_invoice_line()
180 class pos_make_payment(osv
.osv_memory
):
181 _inherit
= 'pos.make.payment'
183 def check(self
, cr
, uid
, ids
, context
=None):
185 Auto invoice orders. Temporarly solution before make partner members just with orders, without invoice.
187 res
= super(pos_make_payment
, self
).check(cr
, uid
, ids
, context
=context
)
189 order_obj
= self
.pool
.get('pos.order')
190 order_obj
.action_invoice(cr
, uid
, ids
, context
=context
)
196 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: