[grouped_membership] [membership_date2date] [dev]: work in progress..
[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 import time
27 from datetime import datetime, date
28 from dateutil.relativedelta import relativedelta
29
30
31 class Partner(osv.osv):
32 _inherit = 'res.partner'
33
34 _columns = {
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.'),
37 }
38
39 _sql_constraints = [
40 ('member_ident_uniq', 'unique(member_ident, company_id)', 'The member identifier must be unique !'),
41 ]
42
43 def _membership_date(self, cr, uid, ids, name, args, context=None):
44 """Return date of membership"""
45 name = name[0]
46 res = {}
47 member_line_obj = self.pool.get('membership.membership_line')
48 for partner in self.browse(cr, uid, ids, context=context):
49 #if partner.associate_member:
50 # partner_id = partner.associate_member.id
51 #else:
52 # NOTE: hmm, ça devrait être toujours partner.id
53 partner_id = partner.id
54 res[partner.id] = {
55 'membership_start': False,
56 'membership_stop': False,
57 'membership_cancel': False
58 }
59 if name == 'membership_start':
60 line_id = member_line_obj.search(cr, uid, [('partner', '=', partner_id),('date_cancel','=',False)],
61 limit=1, order='date_from', context=context)
62 if line_id:
63 res[partner.id]['membership_start'] = member_line_obj.read(cr, uid, line_id[0],
64 ['date_from'], context=context)['date_from']
65
66 if name == 'membership_stop':
67 line_id1 = member_line_obj.search(cr, uid, [('partner', '=', partner_id),('date_cancel','=',False)],
68 limit=1, order='date_to desc', context=context)
69 if line_id1:
70 res[partner.id]['membership_stop'] = member_line_obj.read(cr, uid, line_id1[0],
71 ['date_to'], context=context)['date_to']
72
73 if name == 'membership_cancel':
74 if partner.membership_state == 'canceled':
75 # NOTE: hmm, partner_id et pas partner.id ?
76 line_id2 = member_line_obj.search(cr, uid, [('partner', '=', partner_id)], limit=1, order='date_cancel', context=context)
77 if line_id2:
78 res[partner.id]['membership_cancel'] = member_line_obj.read(cr, uid, line_id2[0], ['date_cancel'], context=context)['date_cancel']
79 return res
80
81 def create_membership_invoice(self, cr, uid, ids, product_id=None, datas=None, context=None):
82 # NOTE: use context to supply date_from to account_invoice_line.create
83 ctx = context and context.copy() or {}
84 ctx['date_from'] = datas.get('date_from', None)
85 return super(Partner, self).create_membership_invoice(cr, uid, ids, product_id=product_id, datas=datas, context=ctx)
86
87 Partner()
88
89
90 class Product(osv.osv):
91 _inherit = 'product.product'
92
93 _columns = {
94 'membership_grouped': fields.boolean('Grouped membership product', help='Check if it\'s a grouped membership product.'),
95 'membership_date2date': fields.boolean('Date to date membership product', required=False, help='Check if it\'s a date to date membership product.'),
96 }
97
98 Product()
99
100
101 class account_invoice_line(osv.osv):
102 _inherit = 'account.invoice.line'
103
104 def write(self, cr, uid, ids, vals, context=None):
105 member_line_obj = self.pool.get('membership.membership_line')
106 res = super(account_invoice_line, self).write(cr, uid, ids, vals, context=context)
107 for line in self.browse(cr, uid, ids, context=context):
108 if line.invoice_id.type == 'out_invoice':
109 ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
110 if line.product_id and line.product_id.membership:
111 member_line_id = member_line_obj.search(cr, uid
112 , [('partner' , '=', line.invoice_id.partner_id.id)
113 ,('account_invoice_line', '=', line.id)]
114 , limit=1
115 , context=context)
116 for member_line in member_line_obj.browse(cr, uid, member_line_id, context=context):
117 # NOTE: get member_line created in membership.account_invoice_line.create
118 date_from = member_line.date_from
119 date_to = member_line.date_to
120 if line.product_id.membership_date2date:
121 date_from = context['date_from'] and datetime.strptime(context['date_from'], "%Y-%m-%d") or date.today()
122 date_to = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
123 date_from = date_from.strftime("%Y-%m-%d")
124 date_to = date_to .strftime("%Y-%m-%d")
125 print ("DEV: [write] date_from: %s" % str(date_from))
126 print ("DEV: [write] date_to : %s" % str(date_to))
127 member_line_obj.write(cr, uid, member_line.id
128 , {'date_from': date_from
129 ,'date_to' : date_to
130 }
131 , context=context)
132 if line.product_id.membership_grouped:
133 if line.invoice_id.partner_id.associate_members:
134 for associate_member in line.invoice_id.partner_id.associate_members:
135 print ("DEV: [write] [associate] date_from: %s" % str(date_from))
136 print ("DEV: [write] [associate] date_to : %s" % str(date_to))
137 member_line_obj.create(cr, uid, {
138 'partner': associate_member.id,
139 'membership_id': line.product_id.id,
140 'member_price': line.price_unit,
141 'date': time.strftime('%Y-%m-%d'),
142 'date_from': date_from,
143 'date_to': date_to,
144 'account_invoice_line': line.id,
145 }, context=context)
146 else:
147 print("DEV: mettre une contrainte pour l'objet\
148 membership.membership_line interdisant les\
149 adhésions groupées reliées à des partenaires\
150 sans membres associés")
151 else:
152 associate_ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id), ('partner', '!=', line.invoice_id.partner_id.id)], context=context)
153 member_line_obj.unlink(cr, uid, associate_ml_ids, context=context)
154
155 #Define member ident if it's necessary
156 partners = [line.invoice_id.partner_id]
157 if line.invoice_id.partner_id.associate_members:
158 partners.extend(line.invoice_id.partner_id.associate_members)
159 for i in partners:
160 if not i.member_ident:
161 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
162 self.pool.get('res.partner').write(cr, uid, i.id, {'member_ident': mbr_id})
163 if line.product_id and not line.product_id.membership and ml_ids:
164 # Product line has changed to a non membership product
165 member_line_obj.unlink(cr, uid, ml_ids, context=context)
166 return res
167
168 def create(self, cr, uid, vals, context=None):
169 member_line_obj = self.pool.get('membership.membership_line')
170 res = super(account_invoice_line, self).create(cr, uid, vals, context=context)
171 line = self.browse(cr, uid, res, context=context)
172
173 if line.invoice_id.type == 'out_invoice' and line.product_id and line.product_id.membership:
174 member_line_id = member_line_obj.search(cr, uid
175 , [('partner' , '=', line.invoice_id.partner_id.id)
176 ,('account_invoice_line', '=', line.id)]
177 , limit=1
178 , context=context)
179 for member_line in member_line_obj.browse(cr, uid, member_line_id, context=context):
180 # NOTE: get member_line created in membership.account_invoice_line.create
181 date_from = member_line.date_from
182 date_to = member_line.date_to
183 if line.product_id.membership_date2date:
184 print "DEV: line.product_id.membership_date2date"
185 date_from = context['date_from'] and datetime.strptime(context['date_from'], "%Y-%m-%d") or date.today()
186 date_to = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
187 date_from = date_from.strftime("%Y-%m-%d")
188 date_to = date_to .strftime("%Y-%m-%d")
189 print ("DEV: [create] date_from: %s" % str(date_from))
190 print ("DEV: [create] date_to : %s" % str(date_to))
191 member_line_obj.write(cr, uid, member_line.id
192 , {'date_from': date_from
193 ,'date_to' : date_to
194 }
195 , context=context)
196 partners = [line.invoice_id.partner_id]
197 if line.product_id.membership_grouped and line.invoice_id.partner_id.associate_members:
198 partners.extend(line.invoice_id.partner_id.associate_members)
199 #Adding membership lines just for associate partners
200 for associate_member in line.invoice_id.partner_id.associate_members:
201 print ("DEV: [create] [associate] date_from: %s" % str(date_from))
202 print ("DEV: [create] [associate] date_to : %s" % str(date_to))
203 member_line_obj.create(cr, uid, {
204 'partner': associate_member.id,
205 'membership_id': line.product_id.id,
206 'member_price': line.price_unit,
207 'date': time.strftime('%Y-%m-%d'),
208 'date_from': date_from,
209 'date_to': date_to,
210 'account_invoice_line': line.id,
211 }, context=context)
212 #Define member ident if it's necessary
213 for i in partners:
214 if not i.member_ident:
215 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
216 self.pool.get('res.partner').write(cr, uid, i.id, {'member_ident': mbr_id})
217 return res
218
219 account_invoice_line()
220
221
222 class pos_make_payment(osv.osv_memory):
223 _inherit = 'pos.make.payment'
224
225 def check(self, cr, uid, ids, context=None):
226 """
227 Auto invoice orders. Temporarly solution before make partner members just with orders, without invoice.
228 """
229 res = super(pos_make_payment, self).check(cr, uid, ids, context=context)
230
231 order_obj = self.pool.get('pos.order')
232 order_obj.action_invoice(cr, uid, ids, context=context)
233
234 return res
235
236 pos_make_payment()
237
238 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: