[membership_date2date] initial implementation
[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 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)
48
49 Partner()
50
51
52 class Product(osv.osv):
53 _inherit = 'product.product'
54
55 _columns = {
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.'),
58 }
59
60 Product()
61
62
63 class account_invoice_line(osv.osv):
64 _inherit = 'account.invoice.line'
65
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)]
76 , limit=1
77 , context=context)
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
89 ,'date_to' : date_to
90 }
91 , context=context)
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,
101 'date_to': date_to,
102 'account_invoice_line': line.id,
103 }, context=context)
104 else:
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")
109 else:
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)
112
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)
117 for i in partners:
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)
124 return res
125
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)
130
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)]
135 , limit=1
136 , context=context)
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
151 ,'date_to' : date_to
152 }
153 , context=context)
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,
167 'date_to': date_to,
168 'account_invoice_line': line.id,
169 }, context=context)
170 #Define member ident if it's necessary
171 for i in partners:
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})
175 return res
176
177 account_invoice_line()
178
179
180 class pos_make_payment(osv.osv_memory):
181 _inherit = 'pos.make.payment'
182
183 def check(self, cr, uid, ids, context=None):
184 """
185 Auto invoice orders. Temporarly solution before make partner members just with orders, without invoice.
186 """
187 res = super(pos_make_payment, self).check(cr, uid, ids, context=context)
188
189 order_obj = self.pool.get('pos.order')
190 order_obj.action_invoice(cr, uid, ids, context=context)
191
192 return res
193
194 pos_make_payment()
195
196 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: