Commit 72ed5ca6 by Yvon Kerdoncuff

#7325 : use suppleant_member_id instead of parent_id or is_associated_people to…

#7325 : use suppleant_member_id instead of parent_id or is_associated_people to perform binom checks
parent 578b0c52
...@@ -578,10 +578,12 @@ def delete_shift_template_registration(request): ...@@ -578,10 +578,12 @@ def delete_shift_template_registration(request):
if res["set_done"]: if res["set_done"]:
""" Delete pair(s?) of partner if it is a parent to improve statistics (#4810) """ """ Delete pair(s?) of partner if it is a parent to improve statistics (#4810) """
api = OdooAPI() api = OdooAPI()
associated_members = api.search_read('res.partner', [['parent_id', '=', partner_id]], ['id']) associated_members = api.search_read('res.partner', [['parent_id', '=', partner_id]], ['id', 'suppleant_member_id'])
for am in associated_members: for am in associated_members:
data = {"child": {"id": am["id"]}, "gone": ["parent", "child"]} # Do not call delete_pair_core if contact is not a binom
delete_pair_core(data) if am['suppleant_member_id']:
data = {"child": {"id": am["id"]}, "gone": ["parent", "child"]}
delete_pair_core(data)
except Exception as e: except Exception as e:
res["error"] = str(e) res["error"] = str(e)
...@@ -707,6 +709,7 @@ def get_member_info(request, id): ...@@ -707,6 +709,7 @@ def get_member_info(request, id):
'current_template_name', 'current_template_name',
'shift_type', 'shift_type',
'parent_id', 'parent_id',
'suppleant_member_id',
'is_associated_people', 'is_associated_people',
'parent_name', 'parent_name',
"makeups_to_do", "makeups_to_do",
...@@ -716,8 +719,9 @@ def get_member_info(request, id): ...@@ -716,8 +719,9 @@ def get_member_info(request, id):
member = api.search_read('res.partner', cond, fields) member = api.search_read('res.partner', cond, fields)
if member: if member:
member = member[0] member = member[0]
parent = None
if member['parent_id']: if member['parent_id']:
if not member['suppleant_member_id']:
return JsonResponse({"message": "Accès non supporté (rattaché non suppléant)."}, status=404)
res_parent = api.search_read('res.partner', [['id', '=', int(member['parent_id'][0])]], ['barcode_base', 'email']) res_parent = api.search_read('res.partner', [['id', '=', int(member['parent_id'][0])]], ['barcode_base', 'email'])
if res_parent: if res_parent:
parent = res_parent[0] parent = res_parent[0]
...@@ -796,28 +800,33 @@ def create_pair(request): ...@@ -796,28 +800,33 @@ def create_pair(request):
"final_standard_points", "final_standard_points",
"final_ftop_points", "final_ftop_points",
'shift_type' 'shift_type'
'parent_id'])[0] 'parent_id',
'suppleant_member_id'])[0]
case = isPartnerInvolvedInBinom(api, child)
errors = [] errors = []
if child['nb_associated_people'] > 0: if case == "MEMBER_SUPPLEANT_OF_BINOM":
# le membre est déjà titulaire d'un binôme errors.append("Le suppléant sélectionné est déjà suppléant d'un binôme.")
errors.append("Le membre suppléant sélectionné est titulaire d'un bînome") elif case == "CONTACT_OF_MEMBER_SUPPLEANT_OF_BINOM":
errors.append("Le suppléant sélectionné est déjà (le contact d'un membre) suppléant d'un binôme.")
elif case == "TITULAIRE_OF_BINOM":
errors.append("Le suppléant sélectionné est déjà titulaire d'un binôme.")
# le membre suppléant fait parti du commité? # le membre suppléant fait parti du commité?
if child['current_template_name'] == "Services des comités": if child['current_template_name'] == "Services des comités":
errors.append("Le membre suppléant séléctionné fait parti du comité") errors.append("Le membre suppléant séléctionné fait parti du comité")
# Verifier que le suppléant n'est pas déjà en binôme soit titulaire soit suppléant
for m in api.search_read('res.partner', [['email', '=', child['email']]]): case = isPartnerInvolvedInBinom(api, parent)
if m['is_associated_people']:
errors.append('Le membre suppléant est déjà en bînome') errors = []
if m['child_ids']: if case == "MEMBER_SUPPLEANT_OF_BINOM":
errors.append("Le membre suppléant sélectionné est titulaire d'un binôme") errors.append("Le titulaire sélectionné est déjà suppléant d'un binôme.")
# le membre titulaire a déjà un/des suppléants? elif case == "CONTACT_OF_MEMBER_SUPPLEANT_OF_BINOM":
if parent['nb_associated_people'] >= 1: errors.append("Le titulaire sélectionné est déjà (le contact d'un membre) suppléant d'un binôme.")
# On récupère le/s suppléant(s) elif case == "TITULAIRE_OF_BINOM":
associated_members = api.search_read('res.partner', [['parent_id', '=', parent_id]], ['id', 'age']) errors.append("Le titulaire sélectionné est déjà titulaire d'un binôme.")
# le suppléant est un mineur?
for m in associated_members:
if m['age'] > 18:
errors.append("Le membre titulaire sélectionné a déjà un suppléant")
if errors: if errors:
return JsonResponse({"errors": errors}, status=409) return JsonResponse({"errors": errors}, status=409)
...@@ -925,6 +934,27 @@ def create_pair(request): ...@@ -925,6 +934,27 @@ def create_pair(request):
return JsonResponse({"message": "Method Not Allowed"}, status=405) return JsonResponse({"message": "Method Not Allowed"}, status=405)
def isPartnerInvolvedInBinom(api, partner):
# First make sure the selected partner "is not already a suppleant"
# Let's be prudent : cover both case of partner being suppleant member,
# and case of partner being the contact of a suppleant member
contact_of_partner_suppleant = api.search_read('res.partner', [['suppleant_member_id', '=', partner['id']]])
if contact_of_partner_suppleant:
# partner has a suppleant contact therefore it is a suppleant member
return "MEMBER_SUPPLEANT_OF_BINOM"
if partner['parent_id'] and partner['suppleant_member_id']:
# partner is the contact of a suppleant member
return "CONTACT_OF_MEMBER_SUPPLEANT_OF_BINOM"
# Second make sure the selected partner is not a titulaire
children_of_partner = api.search_read('res.partner', [['parent_id', '=', partner['id']]])
suppleant_of_partner = [x for x in children_of_partner if x['suppleant_member_id']]
if suppleant_of_partner:
# partner is titulaire
return "TITULAIRE_OF_BINOM"
return None
def delete_pair(request): def delete_pair(request):
""" """
Administration des binômes membres Administration des binômes membres
......
...@@ -203,6 +203,7 @@ class CagetteMember(models.Model): ...@@ -203,6 +203,7 @@ class CagetteMember(models.Model):
if getattr(settings, 'ALLOW_NON_MEMBER_TO_CONNECT', False) is False: if getattr(settings, 'ALLOW_NON_MEMBER_TO_CONNECT', False) is False:
cond.append('|') cond.append('|')
cond.append(['is_member', '=', True]) cond.append(['is_member', '=', True])
# TODO : consider replacing is_associated_people by suppleant_member_id to exclude mineurs rattachés
cond.append(['is_associated_people', '=', True]) cond.append(['is_associated_people', '=', True])
fields = ['name', 'email', 'birthdate_date', 'create_date', 'cooperative_state', 'is_associated_people', 'barcode_base'] fields = ['name', 'email', 'birthdate_date', 'create_date', 'cooperative_state', 'is_associated_people', 'barcode_base']
...@@ -212,6 +213,7 @@ class CagetteMember(models.Model): ...@@ -212,6 +213,7 @@ class CagetteMember(models.Model):
if (res and len(res) >= 1): if (res and len(res) >= 1):
coop_id = None coop_id = None
hashed_password = None hashed_password = None
# TODO : add comment to explain why there is a loop here
for item in res: for item in res:
coop = item coop = item
if 'hashed_password' in item: if 'hashed_password' in item:
...@@ -219,6 +221,7 @@ class CagetteMember(models.Model): ...@@ -219,6 +221,7 @@ class CagetteMember(models.Model):
if item["birthdate_date"] is not False: if item["birthdate_date"] is not False:
coop_birthdate = item['birthdate_date'] coop_birthdate = item['birthdate_date']
coop_state = item['cooperative_state'] coop_state = item['cooperative_state']
# TODO : consider replacing is_associated_people check by suppleant_member_id to exclude mineurs rattachés
if item["is_associated_people"] == True: if item["is_associated_people"] == True:
coop_id = item['id'] coop_id = item['id']
...@@ -460,18 +463,12 @@ class CagetteMember(models.Model): ...@@ -460,18 +463,12 @@ class CagetteMember(models.Model):
def is_associated(id_parent): def is_associated(id_parent):
api = OdooAPI() api = OdooAPI()
cond = [['parent_id', '=', int(id_parent)]] cond = [['parent_id', '=', int(id_parent)]]
fields = ['id','name','parent_id','birthdate_date'] fields = ['id','name','parent_id','birthdate_date','suppleant_member_id']
res = api.search_read('res.partner', cond, fields, 10, 0, 'id DESC') res = api.search_read('res.partner', cond, fields, 10, 0, 'id DESC')
already_have_adult_associated = False
for partner in res: for partner in res:
birthdate = partner['birthdate_date'] if partner['suppleant_member_id']:
if(birthdate): return True
today = date.today() return False
date1 = datetime.datetime.strptime(birthdate, "%Y-%m-%d")
age = today.year - date1.year - ((today.month, today.day) < (date1.month, date1.day))
if age > 17 :
already_have_adult_associated = True
return already_have_adult_associated
@staticmethod @staticmethod
def finalize_coop_creation(post_data): def finalize_coop_creation(post_data):
...@@ -901,6 +898,7 @@ class CagetteMember(models.Model): ...@@ -901,6 +898,7 @@ class CagetteMember(models.Model):
cond = [['name', 'ilike', str(key)]] cond = [['name', 'ilike', str(key)]]
cond.append('|') cond.append('|')
cond.append(['is_member', '=', True]) cond.append(['is_member', '=', True])
#TODO : replace is_associated_people check by suppleant_member_id check to exclude mineurs rattachés
if search_type != 'members' and search_type != 'envelops': if search_type != 'members' and search_type != 'envelops':
cond.append(['is_associated_people', '=', True]) cond.append(['is_associated_people', '=', True])
else: else:
...@@ -1044,14 +1042,14 @@ class CagetteMember(models.Model): ...@@ -1044,14 +1042,14 @@ class CagetteMember(models.Model):
res = {} res = {}
c = [["parent_id", "=", self.id]] c = [["parent_id", "=", self.id]]
f = ["id", "name", "barcode_base"] f = ["id", "name", "barcode_base", 'suppleant_member_id']
res = self.o_api.search_read('res.partner', c, f) res = self.o_api.search_read('res.partner', c, f)
try: for partner in res:
return res[0] if partner['suppleant_member_id']:
except: return partner
return None return None
def update_member_makeups(self, member_data): def update_member_makeups(self, member_data):
api = OdooAPI() api = OdooAPI()
...@@ -1431,10 +1429,11 @@ class CagetteMembers(models.Model): ...@@ -1431,10 +1429,11 @@ class CagetteMembers(models.Model):
@staticmethod @staticmethod
def get_attached_members(): def get_attached_members():
api = OdooAPI() api = OdooAPI()
cond = [['is_associated_people','=', True]] cond = [['is_associated_people', '=', True]]
fields = ['id', 'name', 'parent_name'] fields = ['id', 'name', 'parent_name', 'suppleant_member_id']
res = api.search_read('res.partner', cond, fields) res = api.search_read('res.partner', cond, fields)
return res # Exclude mineurs rattachés
return [x for x in res if res['suppleant_member_id']]
class CagetteUser(models.Model): class CagetteUser(models.Model):
......
...@@ -218,7 +218,7 @@ def home(request): ...@@ -218,7 +218,7 @@ def home(request):
if partnerData['cooperative_state'] == "unsubscribed": if partnerData['cooperative_state'] == "unsubscribed":
coop_can_change_shift_template = False coop_can_change_shift_template = False
if getattr(settings, 'ASSOCIATE_PEOPLE_CAN_CHANGE_SHIFT_TEMPLE_REGISTRATION', False) is False: if getattr(settings, 'ASSOCIATE_PEOPLE_CAN_CHANGE_SHIFT_TEMPLE_REGISTRATION', False) is False:
if partnerData['is_associated_people'] is True: if partnerData['suppleant_member_id']:
coop_can_change_shift_template = False coop_can_change_shift_template = False
context = { context = {
'title': 'Espace Membres', 'title': 'Espace Membres',
......
...@@ -181,11 +181,11 @@ class CagetteShift(models.Model): ...@@ -181,11 +181,11 @@ class CagetteShift(models.Model):
'cooperative_state', 'final_standard_point', 'create_date', 'cooperative_state', 'final_standard_point', 'create_date',
'final_ftop_point', 'shift_type', 'leave_ids', 'makeups_to_do', 'barcode_base', 'final_ftop_point', 'shift_type', 'leave_ids', 'makeups_to_do', 'barcode_base',
'street', 'street2', 'zip', 'city', 'mobile', 'phone', 'function', 'email', 'street', 'street2', 'zip', 'city', 'mobile', 'phone', 'function', 'email',
'is_associated_people', 'parent_id', 'extra_shift_done'] 'is_associated_people', 'parent_id', 'suppleant_member_id', 'extra_shift_done']
partnerData = self.o_api.search_read('res.partner', cond, fields, 1) partnerData = self.o_api.search_read('res.partner', cond, fields, 1)
if partnerData: if partnerData:
partnerData = partnerData[0] partnerData = partnerData[0]
if partnerData['is_associated_people']: if partnerData['suppleant_member_id']:
cond = [['id', '=', partnerData['parent_id'][0]]] cond = [['id', '=', partnerData['parent_id'][0]]]
fields = ['create_date', 'makeups_to_do', 'date_delay_stop', 'extra_shift_done'] fields = ['create_date', 'makeups_to_do', 'date_delay_stop', 'extra_shift_done']
parentData = self.o_api.search_read('res.partner', cond, fields, 1) parentData = self.o_api.search_read('res.partner', cond, fields, 1)
...@@ -205,7 +205,7 @@ class CagetteShift(models.Model): ...@@ -205,7 +205,7 @@ class CagetteShift(models.Model):
self.o_api.update('res.partner', [id], {'makeups_to_do': partnerData['makeups_to_do']}) self.o_api.update('res.partner', [id], {'makeups_to_do': partnerData['makeups_to_do']})
except Exception as e: except Exception as e:
coop_logger.error("update res.partner.makeups_to_do %s", str(e)) coop_logger.error("update res.partner.makeups_to_do %s", str(e))
if partnerData['is_associated_people']: if partnerData['suppleant_member_id']:
cond = [['partner_id.id', '=', partnerData['parent_id'][0]]] cond = [['partner_id.id', '=', partnerData['parent_id'][0]]]
else: else:
cond = [['partner_id.id', '=', id]] cond = [['partner_id.id', '=', id]]
...@@ -366,10 +366,10 @@ class CagetteShift(models.Model): ...@@ -366,10 +366,10 @@ class CagetteShift(models.Model):
# partner_id can be 'associated_people' one, which is never use as shift partner_id reference # partner_id can be 'associated_people' one, which is never use as shift partner_id reference
# So, let's first retrieved data about the res.partner involved # So, let's first retrieved data about the res.partner involved
cond = [['id', '=', int(data['idPartner'])]] cond = [['id', '=', int(data['idPartner'])]]
fields = ['parent_id'] fields = ['parent_id', 'suppleant_member_id']
partner = self.o_api.search_read('res.partner', cond, fields, 1) partner = self.o_api.search_read('res.partner', cond, fields, 1)
if partner: if partner:
if partner[0]['parent_id']: if partner[0]['suppleant_member_id']:
partner_id = partner[0]['parent_id'][0] partner_id = partner[0]['parent_id'][0]
else: else:
partner_id = int(data['idPartner']) partner_id = int(data['idPartner'])
...@@ -690,8 +690,9 @@ class CagetteServices(models.Model): ...@@ -690,8 +690,9 @@ class CagetteServices(models.Model):
for m in s['members']: for m in s['members']:
mids.append(m['partner_id'][0]) mids.append(m['partner_id'][0])
cond = [['parent_id', 'in', mids]] cond = [['parent_id', 'in', mids]]
fields = ['id', 'parent_id', 'name','barcode_base'] fields = ['id', 'parent_id', 'name', 'barcode_base', 'suppleant_member_id']
associated = api.search_read('res.partner', cond, fields) attached = api.search_read('res.partner', cond, fields)
associated = [x for x in attached if x['suppleant_member_id']]
if len(associated) > 0: if len(associated) > 0:
for m in s['members']: for m in s['members']:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment