def count(value, card, d_plan, transaction): try: parameters = json.loads(d_plan.parameters) except: return None if type(parameters) is not dict: return None value = float(value) if 'bonus_mechanism' in parameters: bonus_mechanism = parameters['bonus_mechanism'] else: return None if bonus_mechanism == 'bonus_cost': if 'bonus_cost' in parameters: bonus_cost = float(parameters['bonus_cost']) else: return None if bonus_mechanism == 'bonus_percent': if 'bonus_percent' in parameters: bonus_percent = float(parameters['bonus_percent']) else: return None if 'min_transaction' in parameters: min_transaction = float(parameters['min_transaction']) else: return None if 'bonus_lifetime' in parameters: bonus_lifetime = float(parameters['bonus_lifetime']) else: return None if 'round' in parameters: rounding = parameters['round'] else: return None if abs(value) < min_transaction: return card bonus = Bonus() bonus.date = transaction.date bonus.active_from = datetime.now() + relativedelta(days=d_plan.time_delay) bonus.card = card if bonus_mechanism == 'bonus_cost': bonus.value = custom_round((value / bonus_cost), rounding) elif bonus_mechanism == 'bonus_percent': bonus.value = custom_round((value * bonus_percent / 100), rounding) bonus.active_to = bonus.active_from + relativedelta( months=int(bonus_lifetime)) bonus.enabled = False bonus.transaction_id = transaction.pk trans = Transaction(org=card.org, card=card, date=datetime.now(), bonus_before=card.get_total_bonus(), doc_number=transaction.doc_number, session=transaction.session, sum=transaction.sum, shop=transaction.shop, workplace=transaction.workplace) if value < 0: trans.type = Operations.bonus_refund refund_bonus(card, transaction.base_doc_date, -bonus.value) trans.bonus_add = bonus.value trans.save() return card else: trans.type = Operations.bonus_add trans.bonus_add = bonus.value trans.save() task = Task(queue_date=datetime.now(), execution_date=datetime.now().date() + relativedelta(days=d_plan.time_delay), data=transaction.sum, card=card, operation="bonus", d_plan=d_plan, transaction=trans, org=card.org) task.save() bonus.task_id = task.pk bonus.save() return card
def rest_add_bonus(request, card_code): response = {} if request.method == 'PUT': user = UserCustom.objects.get(user_id__exact=request.user.pk) bonus_data = json.loads(request.body.decode()) if not user.administrator: response['status'] = 'error' response['message'] = "Недостаточно прав!" return JsonResponse(response, status=503) try: card = Card.objects.get(org_id__exact=user.org.pk, code__exact=card_code) except: response['status'] = 'error' response['message'] = "Карта с таким кодом не найдена!" return JsonResponse(response, status=400) try: bonus = Bonus() bonus.card = card try: bonus.active_from = datetime.fromtimestamp( bonus_data['active_from'] / 1000) except Exception as exc: bonus.active_from = bonus_data['active_from'] try: bonus.active_to = datetime.fromtimestamp( bonus_data['active_to'] / 1000) except: bonus.active_to = bonus_data['active_to'] bonus.value = float(bonus_data['value']) bonus.enabled = True bonus.save() trans = Transaction() trans.card = card trans.type = 'bonus_add' trans.date = datetime.now() trans.org = user.org trans.bonus_add = bonus.value trans.workplace = 'ВТИ-ДИСКОНТ' trans.doc_close_user = user.user.first_name + " " + user.user.last_name trans.save() except Exception as err: response['status'] = 'error' response['message'] = str(err) return JsonResponse(response, status=400) response['status'] = 'success' response['message'] = 'Бонусы добавлены' return JsonResponse(response, safe=False) if request.method == 'DELETE': user = UserCustom.objects.get(user_id__exact=request.user.pk) bonus_data = json.loads(request.body.decode()) try: card = Card.objects.get(org_id__exact=user.org.pk, code__exact=card_code) except: response['status'] = 'error' response['message'] = "Карта с таким кодом не найдена!" return JsonResponse(response, status=400) try: bonus = Bonus.objects.get(id__exact=int(bonus_data['id'])) bonus.delete() trans = Transaction() trans.card = card trans.type = 'bonus_refund' trans.date = datetime.now() trans.org = user.org trans.bonus_add = bonus.value trans.workplace = 'ВТИ-ДИСКОНТ' trans.doc_close_user = user.user.first_name + " " + user.user.last_name trans.save() except Exception as err: response['status'] = 'error' response['message'] = str(err) return JsonResponse(response, status=400) response['status'] = 'success' response['message'] = 'Бонусы удалены' return JsonResponse(response, safe=False)