2013-02-16 08:32:02 +00:00
|
|
|
from brie.config import ldap_config
|
2014-01-24 03:48:51 +00:00
|
|
|
from brie.model.ldap import *
|
2013-09-10 22:28:40 +00:00
|
|
|
import datetime
|
2013-02-16 08:32:02 +00:00
|
|
|
|
|
|
|
class Residences:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_dn_by_name(user_session, name):
|
|
|
|
result = user_session.ldap_bind.search_first(ldap_config.liste_residence_dn, "(cn=" + name + ")")
|
|
|
|
|
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
#end if
|
|
|
|
|
|
|
|
return result.uniqueMember.first()
|
|
|
|
#end def
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_name_by_dn(user_session, dn):
|
|
|
|
result = user_session.ldap_bind.search_first(ldap_config.liste_residence_dn, "(uniqueMember=" + dn + ")")
|
|
|
|
|
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
#end if
|
|
|
|
|
|
|
|
return result.cn.first()
|
|
|
|
#end def
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_residences(user_session):
|
|
|
|
return user_session.ldap_bind.search(ldap_config.liste_residence_dn, "(objectClass=groupOfUniqueNames)")
|
|
|
|
#end def
|
|
|
|
#end class
|
2013-09-10 22:28:40 +00:00
|
|
|
|
|
|
|
class CotisationComputes:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def current_year():
|
|
|
|
now = datetime.datetime.now()
|
2014-01-01 23:11:34 +00:00
|
|
|
if now.month > 8:
|
2013-09-10 22:28:40 +00:00
|
|
|
return now.year + 1
|
|
|
|
|
|
|
|
return now.year
|
|
|
|
#end def
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_available_months(start, end, paid_months = []):
|
|
|
|
next_months_available = []
|
|
|
|
|
|
|
|
if start > 12 or end > 12:
|
|
|
|
raise Exception("invalid start or end")
|
|
|
|
|
|
|
|
if start > 8 and end > 8:
|
|
|
|
next_months_available = range(start, end + 1)
|
2014-01-07 00:22:00 +00:00
|
|
|
elif start <= 8 and end < 9:
|
|
|
|
next_months_available = range(start, end + 1)
|
2013-09-10 22:28:40 +00:00
|
|
|
elif start > 8:
|
|
|
|
next_months_available = range(start, 13) + range(1, end + 1 )
|
|
|
|
else:
|
|
|
|
raise Exception("invalid start and end")
|
|
|
|
#end if
|
|
|
|
|
|
|
|
if paid_months == []:
|
|
|
|
return next_months_available
|
|
|
|
|
|
|
|
print next_months_available
|
|
|
|
available_months = [
|
|
|
|
month
|
|
|
|
for month in next_months_available
|
|
|
|
if month not in paid_months
|
|
|
|
]
|
|
|
|
|
|
|
|
return available_months
|
|
|
|
|
|
|
|
#end def
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_valid_month(month):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
if now.month > 8:
|
|
|
|
return (month >= now.month and month < 13) or (month >= 1 and month < 9)
|
|
|
|
else:
|
|
|
|
return month >= now.month and month < 9
|
|
|
|
#end if
|
|
|
|
#end def
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def price_to_pay(year_price, month_price, already_paid, number_months_to_pay):
|
2013-02-16 08:32:02 +00:00
|
|
|
|
2013-09-10 22:28:40 +00:00
|
|
|
months_price = number_months_to_pay * month_price
|
|
|
|
print "already paid : " + str(already_paid)
|
|
|
|
print "months price : " + str(months_price)
|
2013-10-01 14:08:35 +00:00
|
|
|
if already_paid + months_price > year_price:
|
|
|
|
months_price = max(0, year_price - already_paid)
|
2013-09-10 22:28:40 +00:00
|
|
|
|
|
|
|
return months_price
|
|
|
|
#end def
|
|
|
|
|
2013-09-29 17:51:10 +00:00
|
|
|
@staticmethod
|
2014-01-24 03:48:51 +00:00
|
|
|
def anniversary_from_ldap_items(ldap_cotisations):
|
2013-09-29 17:51:10 +00:00
|
|
|
result = []
|
2014-02-15 15:35:40 +00:00
|
|
|
months = []
|
2014-03-04 10:58:47 +00:00
|
|
|
SORT_ORDER = {9: 0, 10: 1, 11: 2, 12: 3, 1: 4, 2: 5, 3: 6, 4: 7, 5: 8, 6: 9, 7: 10, 8: 11}
|
2013-09-29 17:51:10 +00:00
|
|
|
for cotisation in ldap_cotisations:
|
2014-03-04 10:58:47 +00:00
|
|
|
cotisation_months = []
|
2013-09-29 17:51:10 +00:00
|
|
|
anniversary_data = cotisation.get("x-time").first()
|
|
|
|
anniversary_datetime = datetime.datetime.strptime(anniversary_data,
|
|
|
|
"%Y-%m-%d %H:%M:%S.%f")
|
|
|
|
for month in cotisation.get("x-validMonth").all():
|
2014-01-24 03:48:51 +00:00
|
|
|
months.append(int(month))
|
2014-03-04 10:58:47 +00:00
|
|
|
cotisation_months.append(int(month))
|
2013-09-29 17:51:10 +00:00
|
|
|
#end for
|
2014-03-04 10:58:47 +00:00
|
|
|
cotisation_months.sort(key=lambda val: SORT_ORDER[val])
|
|
|
|
result.append((anniversary_datetime, cotisation_months))
|
2013-09-29 17:51:10 +00:00
|
|
|
#end for
|
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
anniversary = 0
|
|
|
|
# tri par ordre d'inscription
|
2013-09-29 17:51:10 +00:00
|
|
|
result = sorted(result)
|
2014-03-04 10:58:47 +00:00
|
|
|
previousMonth = -1
|
|
|
|
months.sort(key=lambda val: SORT_ORDER[val])
|
|
|
|
#on scanne chaque cotisation
|
|
|
|
for resultat in result:
|
|
|
|
#si on n'est pas la premiere cotisation et que les cotisations sont sans interruptions (pas de mois manquants)
|
|
|
|
#la date anniversaire reste la meme
|
|
|
|
if previousMonth != -1 and ( (resultat[1][0] == 1 and previousMonth == 12) or (resultat[1][0] == previousMonth + 1) ):
|
|
|
|
previousMonth = resultat[1][-1]
|
|
|
|
continue;
|
|
|
|
#sinon on recalcule la date anniversaire
|
|
|
|
else :
|
|
|
|
previousMonth = resultat[1][-1]
|
|
|
|
#end if
|
|
|
|
anniversary_day = resultat[0].day
|
2014-01-24 03:48:51 +00:00
|
|
|
anniversary_month = months[-1] + 1
|
|
|
|
if anniversary_month == 13:
|
|
|
|
anniversary_month = 1
|
|
|
|
if anniversary_month > 9:
|
2014-03-04 10:58:47 +00:00
|
|
|
anniversary_year = resultat[0].year
|
2014-01-24 03:48:51 +00:00
|
|
|
else :
|
2014-03-04 10:58:47 +00:00
|
|
|
anniversary_year = resultat[0].year + 1
|
2014-01-24 03:48:51 +00:00
|
|
|
anniversary = datetime.datetime.strptime(str(anniversary_year) + "-" + str(anniversary_month) + "-1 0:0", "%Y-%m-%d %H:%M") + datetime.timedelta(days=(anniversary_day - 1))
|
2014-03-04 10:58:47 +00:00
|
|
|
#end for
|
2014-01-24 03:48:51 +00:00
|
|
|
return anniversary
|
|
|
|
#end def
|
|
|
|
|
2014-03-04 10:58:47 +00:00
|
|
|
@staticmethod
|
|
|
|
# fonction de renvoi de la date anniversaire qui est la date actuelle au cas ou il n'a pas cotise depuis 30 jours, sinon date anniversaire actuelle
|
|
|
|
def generate_new_anniversary_from_ldap_items(ldap_cotisations):
|
|
|
|
anniversary = CotisationComputes.anniversary_from_ldap_items(ldap_cotisations)
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
if anniversary == 0 or (now - anniversary).days > 30:
|
|
|
|
return now
|
|
|
|
else :
|
|
|
|
return anniversary
|
|
|
|
#end if
|
|
|
|
#end def
|
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
@staticmethod
|
|
|
|
# old = SDF or no cotisation this year
|
2014-03-13 01:10:50 +00:00
|
|
|
def is_old_member(member_dn, user_session, residence_dn, cotisations = None):
|
|
|
|
if cotisations is None:
|
|
|
|
current_year = CotisationComputes.current_year()
|
|
|
|
cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year)
|
|
|
|
#end if
|
|
|
|
return Room.get_by_member_dn(user_session, residence_dn, member_dn) == None or cotisations == []
|
2014-01-24 03:48:51 +00:00
|
|
|
#end def
|
|
|
|
|
2014-03-03 18:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
# no cotisation for 2 years
|
|
|
|
def is_member_to_delete(member, user_session, residence_dn):
|
|
|
|
current_year = CotisationComputes.current_year()
|
|
|
|
cotisations_this_year = Cotisation.cotisations_of_member(user_session, member.dn, current_year)
|
|
|
|
cotisations_previous_year = Cotisation.cotisations_of_member(user_session, member.dn, current_year - 1)
|
|
|
|
return cotisations_this_year == [] and cotisations_previous_year == []
|
|
|
|
#end def
|
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
@staticmethod
|
|
|
|
# 7 days grace period
|
2014-05-30 16:05:14 +00:00
|
|
|
def is_cotisation_paid(member_dn, user_session, residence_dn, cotisations = None, anniversary = None, verification_old_member = True):
|
|
|
|
if verification_old_member and CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
return False
|
2014-03-13 01:10:50 +00:00
|
|
|
if cotisations is None:
|
|
|
|
current_year = CotisationComputes.current_year()
|
|
|
|
#end if
|
2014-01-24 03:48:51 +00:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
2014-03-13 01:10:50 +00:00
|
|
|
if cotisations is None:
|
|
|
|
cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year)
|
|
|
|
#end if
|
2014-05-30 16:05:14 +00:00
|
|
|
|
|
|
|
if anniversary is None:
|
|
|
|
anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations)
|
|
|
|
#end if
|
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
delta = (now - anniversary)
|
|
|
|
return delta.days <= 7
|
|
|
|
#end def
|
2013-09-29 17:51:10 +00:00
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
@staticmethod
|
|
|
|
# less than a month late but more than a week
|
2014-05-30 16:05:14 +00:00
|
|
|
def is_cotisation_late(member_dn, user_session, residence_dn, cotisations = None, anniversary = None, verification_old_member = True):
|
|
|
|
if verification_old_member and CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
return False
|
2014-03-13 01:10:50 +00:00
|
|
|
if cotisations is None:
|
|
|
|
current_year = CotisationComputes.current_year()
|
|
|
|
#end if
|
2014-01-24 03:48:51 +00:00
|
|
|
now = datetime.datetime.now()
|
2013-09-29 17:51:10 +00:00
|
|
|
|
2014-03-13 01:10:50 +00:00
|
|
|
if cotisations is None:
|
|
|
|
cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year)
|
|
|
|
#end if
|
2014-05-30 16:05:14 +00:00
|
|
|
|
|
|
|
if anniversary is None:
|
|
|
|
anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations)
|
|
|
|
#end if
|
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
delta = (now - anniversary)
|
|
|
|
#print("[DEBUG] cotisation en retard pour l'utilisateur "+ member.dn +" now="+ str(now) +" anniversary="+ str(anniversary) +" delta="+ str(delta))
|
|
|
|
return delta.days <= 30 and delta.days > 7
|
2013-09-29 17:51:10 +00:00
|
|
|
#end def
|
2014-01-24 03:48:51 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
# more than a month late
|
2014-05-30 16:05:14 +00:00
|
|
|
def is_no_cotisation(member_dn, user_session, residence_dn, cotisations = None, anniversary = None, verification_old_member = True):
|
|
|
|
if verification_old_member and CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
return False
|
2014-03-13 01:10:50 +00:00
|
|
|
if cotisations is None:
|
|
|
|
current_year = CotisationComputes.current_year()
|
|
|
|
#end if
|
2014-01-24 03:48:51 +00:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
2014-03-13 01:10:50 +00:00
|
|
|
if cotisations is None:
|
|
|
|
cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year)
|
|
|
|
#end if
|
2014-05-30 16:05:14 +00:00
|
|
|
|
|
|
|
if anniversary is None:
|
|
|
|
anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations)
|
|
|
|
#end if
|
|
|
|
|
2014-01-24 03:48:51 +00:00
|
|
|
delta = (now - anniversary)
|
|
|
|
return delta.days > 30
|
|
|
|
#end def
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def members_status_from_residence(user_session, residence_dn):
|
|
|
|
members = Member.get_all(user_session, residence_dn)
|
|
|
|
|
|
|
|
old_members = []
|
|
|
|
cotisation_paid_members = []
|
|
|
|
cotisation_late_members = []
|
|
|
|
no_cotisation_members = []
|
|
|
|
for member in members:
|
2014-03-13 01:10:50 +00:00
|
|
|
current_year = CotisationComputes.current_year()
|
|
|
|
cotisations = Cotisation.cotisations_of_member(user_session, member.dn, current_year)
|
|
|
|
if CotisationComputes.is_old_member(member.dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
old_members.append(member)
|
2014-03-13 01:10:50 +00:00
|
|
|
elif CotisationComputes.is_cotisation_paid(member.dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
cotisation_paid_members.append(member)
|
2014-03-13 01:10:50 +00:00
|
|
|
elif CotisationComputes.is_cotisation_late(member.dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
cotisation_late_members.append(member)
|
|
|
|
#print("[DEBUG] cotisation en retard pour l'utilisateur "+ member.dn)
|
2014-03-13 01:10:50 +00:00
|
|
|
elif CotisationComputes.is_no_cotisation(member.dn, user_session, residence_dn, cotisations):
|
2014-01-24 03:48:51 +00:00
|
|
|
no_cotisation_members.append(member)
|
|
|
|
else:
|
2014-01-24 21:55:07 +00:00
|
|
|
print "DEBUG : member with weird status !"
|
2014-01-24 03:48:51 +00:00
|
|
|
#end if
|
|
|
|
|
|
|
|
#end for
|
2014-01-24 21:55:07 +00:00
|
|
|
return dict(old_members=old_members, cotisation_paid_members=cotisation_paid_members, cotisation_late_members=cotisation_late_members, no_cotisation_members=no_cotisation_members)
|
2014-01-24 03:48:51 +00:00
|
|
|
#end def
|
|
|
|
|
2014-03-13 01:10:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def members_status_from_list_cotisations(user_session, residence_dn, cotisations):
|
|
|
|
members_dict = dict()
|
|
|
|
for cotisation in cotisations:
|
|
|
|
cotisation_dn = cotisation.dn.split(",")
|
|
|
|
member_dn = ""
|
|
|
|
for i in range(3, len(cotisation_dn)):
|
|
|
|
if i != 3:
|
|
|
|
member_dn += ","
|
|
|
|
#end if
|
|
|
|
member_dn += cotisation_dn[i]
|
|
|
|
#end for
|
|
|
|
if not member_dn in members_dict:
|
|
|
|
members_dict[member_dn] = [cotisation]
|
|
|
|
else:
|
|
|
|
members_dict[member_dn].append(cotisation)
|
|
|
|
#end if
|
|
|
|
#end for
|
|
|
|
|
2014-05-30 16:05:14 +00:00
|
|
|
print str(datetime.datetime.now()) + "members_status_from_list_cotisations:" + str(len(members_dict))
|
|
|
|
|
2014-03-13 01:10:50 +00:00
|
|
|
old_members = []
|
|
|
|
cotisation_paid_members = []
|
|
|
|
cotisation_late_members = []
|
|
|
|
no_cotisation_members = []
|
2014-05-30 16:05:14 +00:00
|
|
|
|
|
|
|
#on va verifier en un block quels sont les old_members
|
|
|
|
for member_dn, cotisations in members_dict.iteritems():
|
|
|
|
old_members.append(member_dn)
|
|
|
|
#end for
|
|
|
|
rooms = Room.get_rooms(user_session, residence_dn)
|
|
|
|
for room in rooms:
|
|
|
|
#pour chaque chambre, on check si il a un proprietaire, et si ce dernier a des cotisations
|
|
|
|
if room.has("x-memberIn") and room.get("x-memberIn").first() in members_dict and room.get("x-memberIn").first() in old_members:
|
|
|
|
#si oui, bah on le retire des old_members
|
|
|
|
old_members.remove(room.get("x-memberIn").first())
|
|
|
|
#endif
|
|
|
|
#endfor
|
|
|
|
|
|
|
|
|
2014-03-13 01:10:50 +00:00
|
|
|
for member_dn, cotisations in members_dict.iteritems():
|
2014-05-30 16:05:14 +00:00
|
|
|
anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations)
|
|
|
|
if CotisationComputes.is_cotisation_paid(member_dn, user_session, residence_dn, cotisations, anniversary, False):
|
2014-03-13 01:10:50 +00:00
|
|
|
cotisation_paid_members.append(member_dn)
|
2014-05-30 16:05:14 +00:00
|
|
|
elif CotisationComputes.is_cotisation_late(member_dn, user_session, residence_dn, cotisations, anniversary, False):
|
2014-03-13 01:10:50 +00:00
|
|
|
cotisation_late_members.append(member_dn)
|
|
|
|
#print("[DEBUG] cotisation en retard pour l'utilisateur "+ member.dn)
|
2014-05-30 16:05:14 +00:00
|
|
|
elif CotisationComputes.is_no_cotisation(member_dn, user_session, residence_dn, cotisations, anniversary, False):
|
2014-03-13 01:10:50 +00:00
|
|
|
no_cotisation_members.append(member_dn)
|
|
|
|
else:
|
|
|
|
print "DEBUG : member with weird status !"
|
|
|
|
#end if
|
|
|
|
|
|
|
|
#end for
|
|
|
|
return dict(old_members=old_members, cotisation_paid_members=cotisation_paid_members, cotisation_late_members=cotisation_late_members, no_cotisation_members=no_cotisation_members)
|
|
|
|
#end def
|
|
|
|
|
2013-09-10 22:28:40 +00:00
|
|
|
#end class
|