[MODULE] +last membership product field in res_partner object
[burette/etudesetchantiersidf.git] / etudesetchantiersidf.py
diff --git a/etudesetchantiersidf.py b/etudesetchantiersidf.py
new file mode 100644 (file)
index 0000000..1f40102
--- /dev/null
@@ -0,0 +1,79 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    etudesetchantiersidf module for OpenERP, Custom module for Étude et
+#    Chantiers île-de-France
+#    Copyright (C) 2014-2015 etudesetchantiersidf
+#    (<http://etudesetchantiersiledefrance.unarec.org/>)
+#
+#    This file is a part of etudesetchantiersidf
+#
+#    etudesetchantiersidf is free software: you can redistribute it and/or
+#    modify it under the terms of the GNU General Public License as published
+#    by the Free Software Foundation, either version 3 of the License, or (at
+#    your option) any later version.
+#
+#    etudesetchantiersidf is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from openerp.osv import osv
+from openerp.osv import orm
+from openerp.osv import fields
+
+
+class Partner(osv.osv):
+    '''Partner'''
+    _inherit = 'res.partner'
+    '''
+    def _get_invoice_partner(self, cr, uid, ids, context=None):
+        res = super(Partner, self)._get_invoice_partner(cr, uid, ids, context=context)
+        return res
+
+    def _get_partner_id(self, cr, uid, ids, context=None):
+        res = super(Partner, self)._get_partner_id(cr, uid, ids, context=context)
+        return res
+    '''
+
+    def _get_last_mb_product(self, cr, uid, ids, name, args, context=None):
+        """Return the last membership product buy by a user"""
+        name=name[0]
+        res ={}
+        member_line_obj = self.pool.get('membership.membership_line')
+        for partner in self.browse(cr, uid, ids, context=context):
+            partner_id = partner.id
+            line_id = member_line_obj.search(cr, uid, [
+                ('partner', '=', partner_id),
+                ('date_cancel', '=', False)
+            ], limit=1, order='date_from', context=context)
+            if line_id:
+                product_id = member_line_obj.read(cr, uid, line_id[0], [name], context=context)[name][0]
+                res[partner.id] = {name: product_id}
+        return res
+
+
+    _columns = {
+        'membership_id': fields.function(
+            _get_last_mb_product, multi='membership_id',
+            string='Last membership product',
+            type='many2one',
+            obj='product.product',
+            store=True,
+            ),
+    }
+'''
+store = {
+    'account.invoice': (_get_invoice_partner, ['state'], 10),
+    'membership.membership_line': (_get_partner_id, ['state'], 10, )
+    }
+'''
+
+Partner()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: