|
|
|
|
@ -4,6 +4,7 @@ import logging |
|
|
|
|
import requests |
|
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
from django.core.mail import EmailMessage |
|
|
|
|
from django.db import IntegrityError |
|
|
|
|
from django.db.models import Q |
|
|
|
|
from django.http import HttpResponse, HttpResponseForbidden |
|
|
|
|
from django.shortcuts import redirect |
|
|
|
|
@ -40,40 +41,38 @@ class BillListView(APIView): |
|
|
|
|
def post(self, request): |
|
|
|
|
if request.user.is_authenticated and (request.user.groups.filter(name__in=['managers','lead_managers']).exists() |
|
|
|
|
or request.user.is_superuser): |
|
|
|
|
bill = request.JSON.get('bill') |
|
|
|
|
user = get_user_model().objects.get(email=request.JSON.get('user')) |
|
|
|
|
opener = get_user_model().objects.get(email=request.JSON.get('opener')) |
|
|
|
|
description = request.JSON.get('description', None) |
|
|
|
|
comment = request.JSON.get('comment', None) |
|
|
|
|
course_token = request.JSON.get('course_token', None) |
|
|
|
|
|
|
|
|
|
if bill: |
|
|
|
|
user = get_user_model().objects.get(email=bill['user']) |
|
|
|
|
opener = get_user_model().objects.get(email=bill['opener']) |
|
|
|
|
description = bill['description'] |
|
|
|
|
comment = bill['comment'] |
|
|
|
|
course_token = bill['course_token'] |
|
|
|
|
if course_token is None: |
|
|
|
|
return Response("Идентификатор курса не передан", status=400) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
bill_obj = Bill.objects.get(user=user, course_token=course_token) |
|
|
|
|
except Bill.DoesNotExist: |
|
|
|
|
try: |
|
|
|
|
bill_obj = Bill.objects.get(user=user, course_token=course_token) |
|
|
|
|
except Bill.DoesNotExist: |
|
|
|
|
bill_obj = Bill.objects.create(user=user, course_token=course_token) |
|
|
|
|
except IntegrityError: |
|
|
|
|
return Response("У пользователя уже есть счёт на этот курс", status=400) |
|
|
|
|
|
|
|
|
|
bill_obj.opener = opener |
|
|
|
|
bill_obj.description = description |
|
|
|
|
bill_obj.comment = comment |
|
|
|
|
bill_obj.save() |
|
|
|
|
|
|
|
|
|
return Response(BillSerializer(bill_obj).data, status=200) |
|
|
|
|
bill_obj.opener = bill_obj.opener if opener is None else opener |
|
|
|
|
bill_obj.description = bill_obj.description if description is None else description |
|
|
|
|
bill_obj.comment = bill_obj.comment if comment is None else comment |
|
|
|
|
bill_obj.save() |
|
|
|
|
|
|
|
|
|
return Response("Bill not set", status=400) |
|
|
|
|
return Response(bill_obj.id, status=200) |
|
|
|
|
|
|
|
|
|
return Response("Course detail access only for manager users", status=403) |
|
|
|
|
return Response("Ошибка доступа, возможно вы разлогинились из другой вкладки браузера", status=403) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvoiceDetailView(APIView): |
|
|
|
|
renderer_classes = (JSONRenderer,) |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def delete(request): |
|
|
|
|
invoice_id = request.JSON.get('invoice_id', None) |
|
|
|
|
if invoice_id is None: |
|
|
|
|
return Response("invoice_id must be set", status=400) |
|
|
|
|
def delete(request, invoice_id): |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
i = Invoice.objects.get(id=invoice_id) |
|
|
|
|
@ -84,48 +83,66 @@ class InvoiceDetailView(APIView): |
|
|
|
|
|
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
@transaction_decorator |
|
|
|
|
def post(self, request): |
|
|
|
|
@staticmethod |
|
|
|
|
def post(request, invoice_id): |
|
|
|
|
if request.user.is_authenticated and (request.user.groups.filter(name__in=['managers','lead_managers']).exists() |
|
|
|
|
or request.user.is_superuser): |
|
|
|
|
invoice_data = request.JSON.get('invoice', None) |
|
|
|
|
if invoice_data is None: |
|
|
|
|
return Response("Invoice mast be set", status=400) |
|
|
|
|
invoice_id = int(invoice_id) |
|
|
|
|
bill_id = request.JSON.get('bill', None) |
|
|
|
|
is_open = request.JSON.get('is_open', None) |
|
|
|
|
method = request.JSON.get('method', None) |
|
|
|
|
status = request.JSON.get('status', None) |
|
|
|
|
price = request.JSON.get('price', None) |
|
|
|
|
real_price = request.JSON.get('real_price', None) |
|
|
|
|
|
|
|
|
|
if bill_id is None: |
|
|
|
|
return Response("Не передан id счёта", status=400) |
|
|
|
|
|
|
|
|
|
if is_open is None or method is None or status is None or price is None: |
|
|
|
|
return Response("Не передан один из пораметров is_open, method, status, price", status=400) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
bill = Bill.objects.get(id=invoice_data.pop('bill')) |
|
|
|
|
except (Bill.DoesNotExist, KeyError): |
|
|
|
|
return Response('Bill id must be set', status=400) |
|
|
|
|
bill = Bill.objects.get(id=bill_id) |
|
|
|
|
except Bill.DoesNotExist: |
|
|
|
|
return Response('Не найден счёт с id=%s' % bill_id, status=404) |
|
|
|
|
|
|
|
|
|
invoice_data['method'] = get_real_name(elem=invoice_data['method'], array=Invoice.BILL_METHOD) |
|
|
|
|
invoice_data['status'] = get_real_name(elem=invoice_data['status'], array=Invoice.BILL_METHOD) |
|
|
|
|
method = get_real_name(elem=method[0], array=Invoice.BILL_METHOD) |
|
|
|
|
status = get_real_name(elem=status[0], array=Invoice.BILL_STATUSES) |
|
|
|
|
|
|
|
|
|
if bill.check_validate() and invoice_data['is_open']: |
|
|
|
|
if bill.check_validate(invoice_id) and is_open: |
|
|
|
|
return Response("Уже есть платёж открывающий курс", status=400) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
invoice = Invoice.objects.get(id=invoice_data['id']) |
|
|
|
|
except (Invoice.DoesNotExist, KeyError): |
|
|
|
|
invoice = Invoice.objects.get(id=invoice_id) |
|
|
|
|
except Invoice.DoesNotExist: |
|
|
|
|
if not invoice_id == 0: |
|
|
|
|
return Response("Платёж не найден", status=404) |
|
|
|
|
|
|
|
|
|
if bill.check_pay(): |
|
|
|
|
return Response( |
|
|
|
|
"Нельзя добавить новый платёж, так как один из платежей по счёту уже оплачен", status=400) |
|
|
|
|
invoice = Invoice.objects.create(**invoice_data) |
|
|
|
|
invoice = Invoice.objects.create( |
|
|
|
|
bill=bill, |
|
|
|
|
method=method, |
|
|
|
|
status=status, |
|
|
|
|
is_open=is_open, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if invoice.status == "F": |
|
|
|
|
return Response(InvoiceSerializer(invoice).data, status=200) |
|
|
|
|
|
|
|
|
|
invoice.real_price = None |
|
|
|
|
invoice.method = invoice_data['method'] |
|
|
|
|
invoice.status = invoice_data['status'] |
|
|
|
|
invoice.method = method |
|
|
|
|
invoice.status = status |
|
|
|
|
if invoice.status == "F": |
|
|
|
|
invoice.real_price = invoice_data['real_price'] |
|
|
|
|
invoice.real_price = invoice.real_price if real_price is None else real_price |
|
|
|
|
|
|
|
|
|
if bill.check_pay() and (invoice.price < invoice_data['price']): |
|
|
|
|
if bill.check_pay() and (invoice.price < price): |
|
|
|
|
return Response("""Нельзя менять стоимость по счёту в большую сторону, |
|
|
|
|
когда один из платежей оплачен""", status=400) |
|
|
|
|
|
|
|
|
|
invoice.price = invoice_data['price'] |
|
|
|
|
invoice.is_open = invoice_data['is_open'] |
|
|
|
|
invoice.price = price |
|
|
|
|
invoice.is_open = is_open |
|
|
|
|
|
|
|
|
|
if invoice.method == 'Y' and invoice.yandex_pay is None: |
|
|
|
|
yandex_pay = Payment.objects.create( |
|
|
|
|
@ -196,7 +213,9 @@ class FindBillView(APIView): |
|
|
|
|
|
|
|
|
|
if key: |
|
|
|
|
res = Bill.objects.filter( |
|
|
|
|
Q(opener__email__contains=key.lower()) | Q(user__email__contains=key.lower()) |
|
|
|
|
Q(opener__email__contains=key.lower()) |
|
|
|
|
| Q(user__email__contains=key.lower()) |
|
|
|
|
| Q(id__contains=key) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
|