def apiRemCardBonus(request, card_code, salt): t_type = Operations.bonus_reduce if request.method == 'POST': data = request.POST if ('key' in data) and ('session_key' in data): box = check_license(data['key'], data['session_key']) if box: try: org = box.co_unit.org except: return HttpResponse(status=403) org = None else: return HttpResponse(status=403) if org is not None: if org.is_active: try: if 'value' in data: trans = Transaction().create(data) trans.date = datetime.now() card = Card.objects.get(code=card_code, org=org.pk) trans.bonus_before = card.get_total_bonus() # пишем статистику trans.org = org trans.card = card trans.sum = 0 trans.bonus_reduce = float(data['value']) trans.type = t_type trans.save() value = float(data['value']) rem_bonus(card, value) return HttpResponse(data['value']) else: return HttpResponse(status='404') except ObjectDoesNotExist as e: return HttpResponse(status='404') else: return HttpResponse(status='503') else: return HttpResponse(status='503') else: return HttpResponse(status='503')
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)
def apiToCardFromService(request): if request.method == 'POST': data = request.POST if ('key' in data) and ('vtikeeper' in data): box = CashBox.get_by_key(data['key']) if box: try: org = box.co_unit.org except: org = None if org is not None: if org.is_active: try: if 'type' in data: if data['type'] == 'add': t_type = Operations.sell if 'value' in data and 'card' in data: trans = Transaction().create(data) trans.date = datetime.strptime( data['datetime'], "%d.%m.%Y %H:%M:%S") card = Card.objects.get(code=data['card'], org=org.pk) trans.bonus_before = card.get_total_bonus() d_plan = DiscountPlan.objects.get( org_id__exact=org.pk) algorithm = d_plan.algorithm card.accumulation += float(data['value']) card.last_transaction_date = datetime.now( ).date() card.save() # пишем статистику trans.org = org trans.card = card trans.sum = float(data['value']) trans.bonus_reduce = 0 trans.type = t_type try: trans.base_doc_date = parse( data['base_doc_date']) except: trans.base_doc_date = None trans.save() try: # Добавляем задание task = Task( queue_date=datetime.now(), execution_date=trans.date + relativedelta( days=d_plan.time_delay), data=data['value'], card=card, operation=identify_task_operation( card, d_plan), d_plan=d_plan, transaction=trans, org=card.org) task.save() except: pass return HttpResponse(data['value']) else: return HttpResponse(status='404') if data['type'] == 'rem': t_type = Operations.bonus_reduce if 'value' in data and 'card' in data: trans = Transaction().create(data) trans.date = datetime.strptime( data['datetime'], "%d.%m.%Y %H:%M:%S") card = Card.objects.get(code=data['card'], org=org.pk) trans.bonus_before = card.get_total_bonus() # пишем статистику trans.org = org trans.card = card trans.sum = 0 trans.bonus_reduce = float(data['value']) trans.type = t_type trans.save() value = float(data['value']) rem_bonus(card, value) return HttpResponse(data['value']) else: return HttpResponse(status='404') except ObjectDoesNotExist as e: return HttpResponse(status='404') else: return HttpResponse(status='503') else: return HttpResponse(status='503') else: return HttpResponse(status='503')
def apiAddAccumToCard(request, card_code, salt): t_type = Operations.sell if request.method == 'POST': data = request.POST if ('key' in data) and ('session_key' in data): box = check_license(data['key'], data['session_key']) if box: try: org = box.co_unit.org except: return HttpResponse(status=403) org = None else: return HttpResponse(status=403) if org is not None: if org.is_active: try: if 'value' in data: value = float(data['value']) trans = Transaction().create(data) trans.date = datetime.now() card = Card.objects.get(code=card_code, org=org.pk) trans.bonus_before = card.get_total_bonus() trans.org = org trans.card = card trans.sum = float(data['value']) trans.bonus_reduce = 0 trans.type = t_type try: trans.base_doc_date = parse( data['base_doc_date']) except: trans.base_doc_date = None d_plan = DiscountPlan.objects.get( org_id__exact=org.pk) algorithm = d_plan.algorithm card.accumulation += float(data['value']) card.last_transaction_date = datetime.now().date() card.save() if value < 0: t_type = Operations.refund trans.type = t_type _handler = __import__( 'core.lib.%s' % identify_task_operation(card, d_plan), globals(), locals(), ['count'], 0) card = _handler.count(value, card, d_plan, trans) card.save() trans.save() if value > 0: try: # Добавляем задание # task = Task(queue_date=datetime.now(), # execution_date= datetime.now().date() + relativedelta(days=d_plan.time_delay), # data=data['value'], # card=card, # operation=identify_task_operation(card, d_plan), # d_plan=d_plan, # transaction=trans, # org=card.org) # task.save() _handler = __import__( 'core.lib.%s' % identify_task_operation(card, d_plan), globals(), locals(), ['count'], 0) card = _handler.count( value, card, d_plan, trans) card.save() except: pass return HttpResponse(data['value']) else: return HttpResponse(status='404') except ObjectDoesNotExist as e: return HttpResponse(status='404') else: return HttpResponse(status='503') else: return HttpResponse(status='503') else: return HttpResponse(status='503')