[grouped] [date2date] : fix : account_invoice_line : write : date_from.
[burette/remembership.git] / remembership.py
index a8ae67c..b88034f 100644 (file)
@@ -32,21 +32,15 @@ from openerp.tools.translate import _
 class Partner(osv.osv):
     _inherit = 'res.partner'
 
 class Partner(osv.osv):
     _inherit = 'res.partner'
 
-    _columns = {
-        'member_ident': fields.char('Member identifier', size=64),
-        'associate_members': fields.one2many('res.partner', 'associate_member', 'Associate members', help='Members who are associated to this partner. If this member buy a membership, the associates members will be members too.'),
-    }
-
-    _sql_constraints = [
-        ('member_ident_uniq', 'unique(member_ident, company_id)', 'The member identifier must be unique !'),
-    ]
-
+    # XXX: copied from openerp/addons/membership/membership.py
     def _membership_date(self, cr, uid, ids, name, args, context=None):
         """Return  date of membership"""
         name = name[0]
         res = {}
         member_line_obj = self.pool.get('membership.membership_line')
     def _membership_date(self, cr, uid, ids, name, args, context=None):
         """Return  date of membership"""
         name = name[0]
         res = {}
         member_line_obj = self.pool.get('membership.membership_line')
+        print ("DEV: [remembership] [_membership_date]")
         for partner in self.browse(cr, uid, ids, context=context):
         for partner in self.browse(cr, uid, ids, context=context):
+            # XXX: commented part from openerp/addons/membership/membership.py
             #if partner.associate_member:
             #     partner_id = partner.associate_member.id
             #else:
             #if partner.associate_member:
             #     partner_id = partner.associate_member.id
             #else:
@@ -61,23 +55,92 @@ class Partner(osv.osv):
                 line_id = member_line_obj.search(cr, uid, [('partner', '=', partner_id),('date_cancel','=',False)],
                             limit=1, order='date_from', context=context)
                 if line_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:
-                        res[partner.id]['membership_start'] = member_line_obj.read(cr, uid, line_id[0],
-                                ['date_from'], context=context)['date_from']
+                    res[partner.id]['membership_start'] = member_line_obj.read(cr, uid, line_id[0],
+                            ['date_from'], context=context)['date_from']
+                    if partner.associate_member:
+                        print ("DEV: [_membership_date] [associate] date_from: %s" % str(res[partner.id]['membership_start']))
+                    else:
+                        print ("DEV: [_membership_date] date_from: %s" % str(res[partner.id]['membership_start']))
 
             if name == 'membership_stop':
                 line_id1 = member_line_obj.search(cr, uid, [('partner', '=', partner_id),('date_cancel','=',False)],
                             limit=1, order='date_to desc', context=context)
                 if line_id1:
 
             if name == 'membership_stop':
                 line_id1 = member_line_obj.search(cr, uid, [('partner', '=', partner_id),('date_cancel','=',False)],
                             limit=1, order='date_to desc', context=context)
                 if line_id1:
-                      res[partner.id]['membership_stop'] = member_line_obj.read(cr, uid, line_id1[0],
-                                ['date_to'], context=context)['date_to']
+                    res[partner.id]['membership_stop'] = member_line_obj.read(cr, uid, line_id1[0],
+                              ['date_to'], context=context)['date_to']
+                    if partner.associate_member:
+                        print ("DEV: [_membership_date] [associate] date_to: %s" % str(res[partner.id]['membership_stop']))
+                    else:
+                        print ("DEV: [_membership_date] date_to: %s" % str(res[partner.id]['membership_stop']))
 
             if name == 'membership_cancel':
                 if partner.membership_state == 'canceled':
 
             if name == 'membership_cancel':
                 if partner.membership_state == 'canceled':
-                    # NOTE: hmm, partner_id et pas partner.id ?
-                    line_id2 = member_line_obj.search(cr, uid, [('partner', '=', partner_id)], limit=1, order='date_cancel', context=context)
+                    line_id2 = member_line_obj.search(cr, uid, [('partner', '=', partner.id)], limit=1, order='date_cancel', context=context)
                     if line_id2:
                         res[partner.id]['membership_cancel'] = member_line_obj.read(cr, uid, line_id2[0], ['date_cancel'], context=context)['date_cancel']
         return res
                     if line_id2:
                         res[partner.id]['membership_cancel'] = member_line_obj.read(cr, uid, line_id2[0], ['date_cancel'], context=context)['date_cancel']
         return res
+    # XXX: copied from openerp/addons/membership/membership.py
+    def _get_partner_id(self, cr, uid, ids, context=None):
+        member_line_obj = self.pool.get('membership.membership_line')
+        res_obj =  self.pool.get('res.partner')
+        data_inv = member_line_obj.browse(cr, uid, ids, context=context)
+        list_partner = []
+        for data in data_inv:
+            list_partner.append(data.partner.id)
+        ids2 = list_partner
+        while ids2:
+            ids2 = res_obj.search(cr, uid, [('associate_member', 'in', ids2)], context=context)
+            list_partner += ids2
+        return list_partner
+    # XXX: copied from openerp/addons/membership/membership.py
+    def _get_invoice_partner(self, cr, uid, ids, context=None):
+        inv_obj = self.pool.get('account.invoice')
+        res_obj = self.pool.get('res.partner')
+        data_inv = inv_obj.browse(cr, uid, ids, context=context)
+        list_partner = []
+        for data in data_inv:
+            list_partner.append(data.partner_id.id)
+        ids2 = list_partner
+        while ids2:
+            ids2 = res_obj.search(cr, uid, [('associate_member', 'in', ids2)], context=context)
+            list_partner += ids2
+        return list_partner
+
+    _columns = {
+        'member_ident': fields.char('Member identifier', size=64),
+        'associate_members': fields.one2many('res.partner', 'associate_member', 'Associate members', help='Members who are associated to this partner.'),
+        # XXX: copied from openerp/addons/membership/membership.py
+        'membership_start': fields.function(
+                    _membership_date, multi = 'membership_start',
+                    string = 'Membership Start Date', type = 'date',
+                    store = {
+                        'account.invoice': (_get_invoice_partner, ['state'], 10),
+                        'membership.membership_line': (_get_partner_id, ['state'], 10, ),
+                        'res.partner': (lambda self, cr, uid, ids, c={}: ids, ['free_member'], 10)
+                    }, help="Date from which membership becomes active."),
+        # XXX: copied from openerp/addons/membership/membership.py
+        'membership_stop': fields.function(
+                    _membership_date,
+                    string = 'Membership End Date', type='date', multi='membership_stop',
+                    store = {
+                        'account.invoice': (_get_invoice_partner, ['state'], 10),
+                        'membership.membership_line': (_get_partner_id, ['state'], 10),
+                        'res.partner': (lambda self, cr, uid, ids, c={}: ids, ['free_member'], 10)
+                    }, help="Date until which membership remains active."),
+        # XXX: copied from openerp/addons/membership/membership.py
+        'membership_cancel': fields.function(
+                    _membership_date,
+                    string = 'Cancel Membership Date', type='date', multi='membership_cancel',
+                    store = {
+                        'account.invoice': (_get_invoice_partner, ['state'], 11),
+                        'membership.membership_line': (_get_partner_id, ['state'], 10),
+                        'res.partner': (lambda self, cr, uid, ids, c={}: ids, ['free_member'], 10)
+                    }, help="Date on which membership has been cancelled"),
+    }
+
+    _sql_constraints = [
+        ('member_ident_uniq', 'unique(member_ident, company_id)', 'The member identifier must be unique !'),
+    ]
 
     def create_membership_invoice(self, cr, uid, ids, product_id=None, datas=None, context=None):
         # NOTE: use context to supply date_from to account_invoice_line.create
 
     def create_membership_invoice(self, cr, uid, ids, product_id=None, datas=None, context=None):
         # NOTE: use context to supply date_from to account_invoice_line.create
@@ -119,7 +182,10 @@ class account_invoice_line(osv.osv):
                         date_from = member_line.date_from
                         date_to   = member_line.date_to
                         if line.product_id.membership_date2date:
                         date_from = member_line.date_from
                         date_to   = member_line.date_to
                         if line.product_id.membership_date2date:
-                            date_from = context['date_from'] and datetime.strptime(context['date_from'], "%Y-%m-%d") or date.today()
+                            date_from = ('date_from' in context
+                              and context['date_from']
+                              and datetime.strptime(context['date_from'], "%Y-%m-%d")
+                              or date.today())
                             date_to   = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
                             date_from = date_from.strftime("%Y-%m-%d")
                             date_to   = date_to  .strftime("%Y-%m-%d")
                             date_to   = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
                             date_from = date_from.strftime("%Y-%m-%d")
                             date_to   = date_to  .strftime("%Y-%m-%d")
@@ -132,26 +198,31 @@ class account_invoice_line(osv.osv):
                           , context=context)
                     if line.product_id.membership_grouped:
                         if line.invoice_id.partner_id.associate_members:
                           , context=context)
                     if line.product_id.membership_grouped:
                         if line.invoice_id.partner_id.associate_members:
-                            for associate_member in line.invoice_id.partner_id.associate_members:
+                            associate_member_line_ids = member_line_obj.search(cr, uid
+                              , [ ('account_invoice_line', '=', line.id)
+                                , ('partner', '!=', line.invoice_id.partner_id.id)
+                                ]
+                              , context=context)
+                            for associate_member_line in member_line_obj.browse(cr, uid, associate_member_line_ids, context=context):
                                 print ("DEV: [write] [associate] date_from: %s" % str(date_from))
                                 print ("DEV: [write] [associate] date_to  : %s" % str(date_to))
                                 print ("DEV: [write] [associate] date_from: %s" % str(date_from))
                                 print ("DEV: [write] [associate] date_to  : %s" % str(date_to))
-                                member_line_obj.create(cr, uid, {
-                                    'partner': associate_member.id,
-                                    'membership_id': line.product_id.id,
-                                    'member_price': line.price_unit,
-                                    'date': time.strftime('%Y-%m-%d'),
-                                    'date_from': date_from,
-                                    'date_to': date_to,
-                                    'account_invoice_line': line.id,
-                                }, context=context)
+                                member_line_obj.write(cr, uid, associate_member_line.id
+                                  , {'date_from': date_from
+                                    ,'date_to'  : date_to
+                                    }
+                                  , context=context)
                         else:
                             print("DEV: mettre une contrainte pour l'objet\
                                   membership.membership_line interdisant les\
                                   adhésions groupées reliées à des partenaires\
                                   sans membres associés")
                     else:
                         else:
                             print("DEV: mettre une contrainte pour l'objet\
                                   membership.membership_line interdisant les\
                                   adhésions groupées reliées à des partenaires\
                                   sans membres associés")
                     else:
-                        associate_ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id), ('partner', '!=', line.invoice_id.partner_id.id)], context=context)
-                        member_line_obj.unlink(cr, uid, associate_ml_ids, context=context)
+                        associate_member_line_ids = member_line_obj.search(cr, uid
+                          , [ ('account_invoice_line', '=', line.id)
+                            , ('partner', '!=', line.invoice_id.partner_id.id)
+                            ]
+                          , context=context)
+                        member_line_obj.unlink(cr, uid, associate_member_line_ids, context=context)
 
                     #Define member ident if it's necessary
                     partners = [line.invoice_id.partner_id]
 
                     #Define member ident if it's necessary
                     partners = [line.invoice_id.partner_id]
@@ -182,8 +253,10 @@ class account_invoice_line(osv.osv):
                 date_from = member_line.date_from
                 date_to   = member_line.date_to
                 if line.product_id.membership_date2date:
                 date_from = member_line.date_from
                 date_to   = member_line.date_to
                 if line.product_id.membership_date2date:
-                    print "DEV: line.product_id.membership_date2date"
-                    date_from = context['date_from'] and datetime.strptime(context['date_from'], "%Y-%m-%d") or date.today()
+                    date_from = ('date_from' in context
+                      and context['date_from']
+                      and datetime.strptime(context['date_from'], "%Y-%m-%d")
+                      or date.today())
                     date_to   = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
                     date_from = date_from.strftime("%Y-%m-%d")
                     date_to   = date_to  .strftime("%Y-%m-%d")
                     date_to   = date_from + relativedelta(months = +12) # TODO: parameterize this delta?
                     date_from = date_from.strftime("%Y-%m-%d")
                     date_to   = date_to  .strftime("%Y-%m-%d")