[Grouped] OK for change membership products
[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
28
29 class Partner(osv.osv):
30 _inherit = 'res.partner'
31
32 _columns = {
33 'member_ident': fields.char('Member identifier', size=64),
34 }
35
36 _sql_constraints = [
37 ('member_ident_uniq', 'unique(member_ident, company_id)', 'The member identifier must be unique !'),
38 ]
39
40 Partner()
41
42
43 class Product(osv.osv):
44 _inherit = 'product.product'
45
46 _columns = {
47 'membership_grouped': fields.boolean('Grouped membership product', help='Check if it\'s a grouped membership product.'),
48 'membership_date2date': fields.boolean('Date to date membership product', required=False, help='Check if it\'s a date to date membership product.'),
49 }
50
51 Product()
52
53
54 class account_invoice_line(osv.osv):
55 _inherit = 'account.invoice.line'
56
57
58 def write(self, cr, uid, ids, vals, context=None):
59 member_line_obj = self.pool.get('membership.membership_line')
60 res = super(account_invoice_line, self).write(cr, uid, ids, vals, context=context)
61 for line in self.browse(cr, uid, ids, context=context):
62 if line.invoice_id.type == 'out_invoice':
63 ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
64 if line.product_id and line.product_id.membership:
65 date_from = line.product_id.membership_date_from
66 date_to = line.product_id.membership_date_to
67 if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
68 date_from = line.invoice_id.date_invoice
69 if line.product_id.membership_grouped:
70 if line.invoice_id.partner_id.associate_members:
71 for associate_member in line.invoice_id.partner_id.associate_members:
72 member_line_obj.create(cr, uid, {
73 'partner': associate_member.id,
74 'membership_id': line.product_id.id,
75 'member_price': line.price_unit,
76 'date': time.strftime('%Y-%m-%d'),
77 'date_from': date_from,
78 'date_to': date_to,
79 'account_invoice_line': line.id,
80 }, context=context)
81 else:
82 print("DEV: mettre une contrainte pour l'objet\
83 membership.membership_line interdisant les\
84 adhésions groupées reliées à des partenaires\
85 sans membres associés")
86 else:
87 associate_ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id), ('partner', '!=', line.invoice_id.partner_id.id)], context=context)
88 member_line_obj.unlink(cr, uid, associate_ml_ids, context=context)
89
90 #Define member ident if it's necessary
91 partners = [line.invoice_id.partner_id]
92 if line.invoice_id.partner_id.associate_members:
93 partners.extend(line.invoice_id.partner_id.associate_members)
94 for i in partners:
95 if not i.member_ident:
96 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
97 self.pool.get('res.partner').write(cr, uid, i.id, {'member_ident': mbr_id})
98 if line.product_id and not line.product_id.membership and ml_ids:
99 # Product line has changed to a non membership product
100 member_line_obj.unlink(cr, uid, ml_ids, context=context)
101 return res
102
103 def create(self, cr, uid, vals, context=None):
104 member_line_obj = self.pool.get('membership.membership_line')
105 res = super(account_invoice_line, self).create(cr, uid, vals, context=context)
106 line = self.browse(cr, uid, res, context=context)
107
108 if line.invoice_id.type == 'out_invoice' and line.product_id and line.product_id.membership:
109 date_from = line.product_id.membership_date_from
110 date_to = line.product_id.membership_date_to
111 if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
112 date_from = line.invoice_id.date_invoice
113 partners = [line.invoice_id.partner_id]
114 if line.product_id.membership_grouped and line.invoice_id.partner_id.associate_members:
115 partners.extend(line.invoice_id.partner_id.associate_members)
116 #Adding membership lines just for associate partners
117 for associate_member in line.invoice_id.partner_id.associate_members:
118 member_line_obj.create(cr, uid, {
119 'partner': associate_member.id,
120 'membership_id': line.product_id.id,
121 'member_price': line.price_unit,
122 'date': time.strftime('%Y-%m-%d'),
123 'date_from': date_from,
124 'date_to': date_to,
125 'account_invoice_line': line.id,
126 }, context=context)
127 #Define member ident if it's necessary
128 for i in partners:
129 if not i.member_ident:
130 mbr_id = self.pool.get('ir.sequence').get(cr, uid, 'member_ident')
131 self.pool.get('res.partner').write(cr, uid, i.id, {'member_ident': mbr_id})
132 return res
133
134 account_invoice_line()
135
136
137 class pos_make_payment(osv.osv_memory):
138 _inherit = 'pos.make.payment'
139
140 def check(self, cr, uid, ids, context=None):
141 """
142 Auto invoice orders. Temporarly solution before make partner members just with orders, without invoice.
143 """
144 res = super(pos_make_payment, self).check(cr, uid, ids, context=context)
145
146 order_obj = self.pool.get('pos.order')
147 order_obj.action_invoice(cr, uid, ids, context=context)
148
149 return res
150
151 pos_make_payment()
152
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: