Commit dfbddd07 by Yvon Kerdoncuff

mostly #7524 and #7413 so that "cancellations" of shift and shift_templates work…

mostly #7524 and #7413 so that "cancellations" of shift and shift_templates work fine especially when adding/changing shift
parent 6e18b9a3
......@@ -525,25 +525,19 @@ def delete_shift_registration(request):
is_connected_user = CagetteUser.are_credentials_ok(request)
if is_connected_user is True:
data = json.loads(request.body.decode())
member_id = int(data["member_id"])
shift_registration_id = int(data["shift_registration_id"])
shift_is_makeup = data["shift_is_makeup"]
cancellation_description = data["cancellation_description"]
# Note: 'upcoming_registration_count' in res.partner won't change because the _compute method
# in odoo counts canceled shift registrations.
m = CagetteShift()
res["cancel_shift"] = m.cancel_shift([shift_registration_id], origin='bdm', description=cancellation_description)
if shift_is_makeup is True:
fields = {
'name': "Admin BDM (annulation de rattrapage par une annulation de présence) - " + cancellation_description,
'shift_id': False,
'type': data["member_shift_type"],
'partner_id': member_id,
'point_qty': 1
}
res["update_counter"] = m.update_counter_event(fields)
res["cancel_shift"] = m.cancel_shift(
[shift_registration_id],
cancellation_origin='Bdm',
cancellation_description=cancellation_description,
# Display message for odoo in case cancelled shift is a makeup and there is point to add
counter_event_name="Admin BDM (annulation de rattrapage par une annulation de présence) - " + cancellation_description
)
response = JsonResponse(res, safe=False)
else:
......@@ -620,11 +614,8 @@ def shift_subscription(request):
else:
# Unselect makeups on target shift_template
# (they would prevent new subscription)
api.execute(
'shift.registration',
'unselect',
curated_data["makeups_reg_on_target_shift_template"]
)
cs = CagetteShift()
cs.unselect_makeup(curated_data["makeups_reg_on_target_shift_template"])
# Perform soft unsubscribe that only unselect
# makeups of shift_template to cancel (and
# not makeups that are not linked to it)
......
......@@ -184,7 +184,7 @@ function delete_shift_registration(shift_registration_id) {
$.ajax({
type: 'POST',
url: "/shifts/cancel_shift",
url: "/shifts/cancel_shift_case_extra_shift_done",
dataType:"json",
data: tData,
timeout: 3000,
......
......@@ -295,7 +295,9 @@ class CagetteShift(models.Model):
return listeTicket[0]['shift_ticket_ids'][0]
def set_shift(self, data):
"""Shift registration"""
"""Shift registration.
Decrement makeups_to_do if shift has to be a makeup.
Handle partner already registered on this shift case."""
st_r_id = False
try:
shift_type = "standard"
......@@ -318,7 +320,24 @@ class CagetteShift(models.Model):
res = self.reopen_shift(data)
if res:
st_r_id = True
return st_r_id
partner_id = int(data['idPartner'])
res_decrement = False
if st_r_id:
# decrement makeups_to_do
if data['is_makeup'] is True:
try:
# Ok if makeups_to_do go under 0, until unselect_makeup is called,
# (this is needed when set_shift is called from change_shift)
res_decrement = self.o_api.update(
'res.partner',
[partner_id],
{"makeups_to_do": self.get_member_makeups_to_do(partner_id) - 1}
)
except Exception as e:
coop_logger.error("Decrement makeups to do : %s, %s", str(e), str(data))
return st_r_id, res_decrement
def affect_shift(self, data):
"""Affect shift to partner, his associate or both"""
......@@ -349,13 +368,22 @@ class CagetteShift(models.Model):
coop_logger.error("Model affect shift nobody found : %s", str(cond))
return response
def cancel_shift(self, idsRegisteur, origin='memberspace', description=''):
"""Annule un shift"""
fieldsDatas = { "related_shift_state": 'cancel',
"origin": origin,
"state": 'cancel',
"cancellation_description": description}
return self.o_api.update('shift.registration', idsRegisteur, fieldsDatas)
def cancel_shift(self, idsReg, cancellation_origin='', cancellation_description='', counter_event_name=''):
"""Annule un shift. Si le shift est un rattrapage, ajoute 1 point au compteur."""
fieldsDatas = {
"cancellation_origin": cancellation_origin,
"cancellation_description": cancellation_description,
"counter_event_name": counter_event_name
}
return self.o_api.execute('shift.registration', 'cancel', idsReg, fieldsDatas)
def unselect_makeup(self, idsReg, cancellation_origin='', cancellation_description=''):
"""Déchoisit un rattrapage."""
fieldsDatas = {
"cancellation_origin": cancellation_origin,
"cancellation_description": cancellation_description,
}
return self.o_api.execute('shift.registration', 'unselect_makeup', idsReg, fieldsDatas)
def reopen_shift(self, data):
"""Use when a member select a shift he has canceled before"""
......@@ -515,18 +543,6 @@ class CagetteShift(models.Model):
fields = ['makeups_to_do']
return self.o_api.search_read('res.partner', cond, fields)[0]["makeups_to_do"]
def decrement_makeups_to_do(self, partner_id):
""" Decrements partners makeups to do if > 0 """
makeups_to_do = self.get_member_makeups_to_do(partner_id)
if makeups_to_do > 0:
makeups_to_do -= 1
f = { "makeups_to_do": makeups_to_do }
return self.o_api.update('res.partner', [partner_id], f)
else:
return "makeups already at 0"
def member_can_have_delay(self, partner_id):
""" Can a member have a delay? """
answer = False
......
......@@ -15,7 +15,7 @@ urlpatterns = [
url(r'^change_shift', views.change_shift),
url(r'^affect_shift', views.affect_shift),
url(r'^add_shift', views.add_shift),
url(r'^cancel_shift', views.cancel_shift),
url(r'^cancel_shift', views.cancel_shift_case_extra_shift_done),
url(r'^request_delay', views.request_delay),
url(r'^reset_members_positive_points', views.reset_members_positive_points)
]
......@@ -219,14 +219,19 @@ def change_shift(request):
st_r_id = False
#Insertion du nouveau shift
try:
st_r_id = cs.set_shift(data)
st_r_id, res_decrement = cs.set_shift(data)
except Exception as e:
coop_logger.error("Change shift : %s, %s", str(e), str(data))
if st_r_id:
listRegister = [int(request.POST['idRegister'])]
reg_ids = [int(request.POST['idRegister'])]
# Annule l'ancien shift
response = cs.cancel_shift(listRegister)
c_o = "Espace membre",
c_d = "Déplacement de shift"
if data['is_makeup']:
cs.unselect_makeup(reg_ids, cancellation_origin=c_o, cancellation_description=c_d)
else:
cs.cancel_shift(reg_ids, cancellation_origin=c_o, cancellation_description=c_d)
response = {'result': True}
else:
......@@ -304,25 +309,14 @@ def add_shift(request):
#Insertion du nouveau shift
st_r_id = False
try:
st_r_id = cs.set_shift(data)
st_r_id, res_decrement = cs.set_shift(data)
except Exception as e:
coop_logger.error("Add shift : %s, %s", str(e), str(data))
if st_r_id:
response = {'result': True}
response = {'result': True, 'decrement_makeups' : res_decrement}
else:
response = {'result': False}
# decrement makeups_to_do
response["decrement_makeups"] = False
if data['is_makeup'] is True:
try:
res_decrement = cs.decrement_makeups_to_do(int(request.POST['idPartner']))
except Exception as e:
coop_logger.error("Decrement makeups to do : %s, %s", str(e), str(data))
if res_decrement:
response["decrement_makeups"] = res_decrement
else:
response = {'result': False}
return JsonResponse(response)
......@@ -331,7 +325,7 @@ def add_shift(request):
else:
return HttpResponseForbidden()
def cancel_shift(request):
def cancel_shift_case_extra_shift_done(request):
""" Annule une présence à un shift """
if 'verif_token' in request.POST:
partner_id = int(request.POST.get('idPartner'))
......@@ -343,7 +337,13 @@ def cancel_shift(request):
try:
# decrement extra_shift_done if param exists
if 'extra_shift_done' in request.POST:
response = cs.cancel_shift(listRegister, origin='memberspace extra shift done')
cs.cancel_shift(
listRegister,
cancellation_origin='Espace membre',
cancellation_description='Suite service fait à deux',
#To print in counter event in the case when shift is a makeup
counter_event_name='Suppression d\'un rattrapage depuis l\'espace membre suite à un service fait à deux.'
)
target = int(request.POST["extra_shift_done"]) - 1
# extra security
......@@ -352,10 +352,11 @@ def cancel_shift(request):
cm = CagetteMember(partner_id)
cm.update_extra_shift_done(target)
else:
response = cs.cancel_shift(listRegister)
return JsonResponse({"res" : 'response'})
return JsonResponse({"res": 'response'})
return JsonResponse({"error" : "Cette méthode ne peut être appelée que pour annuler"
"un service suite à un service fait à deux"}, status=500)
except Exception as e:
return JsonResponse({"error" : str(e)}, status=500)
else:
......
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