|
|
|
|
@ -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 |
|
|
|
|
@ -12,6 +13,9 @@ from rest_framework.response import Response |
|
|
|
|
from rest_framework.views import APIView |
|
|
|
|
from yandex_money.models import Payment |
|
|
|
|
from django.conf import settings |
|
|
|
|
from django.core.mail import EmailMultiAlternatives |
|
|
|
|
from django.template.loader import render_to_string |
|
|
|
|
from django.utils.html import strip_tags |
|
|
|
|
|
|
|
|
|
from courses.models import Course |
|
|
|
|
from courses.api import CourseParamsApi |
|
|
|
|
@ -41,84 +45,144 @@ 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') |
|
|
|
|
children = request.JSON.get('children', []) |
|
|
|
|
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.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.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() |
|
|
|
|
|
|
|
|
|
for i in children: |
|
|
|
|
status = get_real_name(elem=i['status'], array=Invoice.BILL_STATUSES) |
|
|
|
|
return Response(bill_obj.id, status=200) |
|
|
|
|
|
|
|
|
|
return Response("Ошибка доступа, возможно вы разлогинились из другой вкладки браузера", status=403) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvoiceDetailView(APIView): |
|
|
|
|
renderer_classes = (JSONRenderer,) |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def delete(request, invoice_id): |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
invoice_id = i['id'] |
|
|
|
|
except KeyError: |
|
|
|
|
invoice_id = None |
|
|
|
|
i = Invoice.objects.get(id=invoice_id) |
|
|
|
|
if not i.status == "F" and not (i.bill.check_pay() and i.is_open): |
|
|
|
|
i.delete() |
|
|
|
|
except Invoice.DoesNotExist: |
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
@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_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) |
|
|
|
|
comment = request.JSON.get('comment', 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: |
|
|
|
|
if not invoice_id is None: |
|
|
|
|
invoice = Invoice.objects.get(id=i['id']) |
|
|
|
|
if invoice.status == "P" or invoice.status == status: |
|
|
|
|
continue |
|
|
|
|
else: |
|
|
|
|
raise Invoice.DoesNotExist |
|
|
|
|
bill = Bill.objects.get(id=bill_id) |
|
|
|
|
except Bill.DoesNotExist: |
|
|
|
|
return Response('Не найден счёт с id=%s' % bill_id, status=404) |
|
|
|
|
|
|
|
|
|
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(invoice_id) and is_open: |
|
|
|
|
return Response("Уже есть платёж открывающий курс", status=400) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
invoice = Invoice.objects.get(id=invoice_id) |
|
|
|
|
except Invoice.DoesNotExist: |
|
|
|
|
i['method'] = get_real_name(elem=i['method'], array=Invoice.BILL_METHOD) |
|
|
|
|
i['status'] = status |
|
|
|
|
i['bill'] = bill_obj |
|
|
|
|
i['yandex_pay'] = None |
|
|
|
|
invoice = Invoice.objects.create(**i) |
|
|
|
|
if not invoice_id == 0: |
|
|
|
|
return Response("Платёж не найден", status=404) |
|
|
|
|
|
|
|
|
|
if i['method'] == 'Y' and invoice.yandex_pay is None: |
|
|
|
|
if bill.check_pay(): |
|
|
|
|
return Response( |
|
|
|
|
"Нельзя добавить новый платёж, так как один из платежей по счёту уже оплачен", status=400) |
|
|
|
|
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 = method |
|
|
|
|
invoice.status = status |
|
|
|
|
|
|
|
|
|
if invoice.status == "F": |
|
|
|
|
invoice.real_price = invoice.real_price if real_price is None else real_price |
|
|
|
|
|
|
|
|
|
if bill.check_pay() and (invoice.price < price): |
|
|
|
|
return Response("""Нельзя менять стоимость по счёту в большую сторону, |
|
|
|
|
когда один из платежей оплачен""", status=400) |
|
|
|
|
|
|
|
|
|
invoice.price = price |
|
|
|
|
invoice.is_open = is_open |
|
|
|
|
invoice.comment = comment |
|
|
|
|
|
|
|
|
|
if invoice.method == 'Y' and invoice.yandex_pay is None: |
|
|
|
|
yandex_pay = Payment.objects.create( |
|
|
|
|
order_amount=i['price'], |
|
|
|
|
order_amount=invoice.price, |
|
|
|
|
shop_amount=0, |
|
|
|
|
customer_number=bill_obj.user.id, |
|
|
|
|
user=bill_obj.user, |
|
|
|
|
cps_email=bill_obj.user.email, |
|
|
|
|
customer_number=bill.user.id, |
|
|
|
|
user=bill.user, |
|
|
|
|
cps_email=bill.user.email, |
|
|
|
|
) |
|
|
|
|
invoice.yandex_pay = yandex_pay |
|
|
|
|
invoice.save() |
|
|
|
|
|
|
|
|
|
msg = EmailMessage( |
|
|
|
|
'Выставлен новый счёт.', |
|
|
|
|
'''Менеджер %s выставил счёт пользователю %s на курс "%s".''' |
|
|
|
|
% ( |
|
|
|
|
invoice.bill.opener.get_full_name(), |
|
|
|
|
invoice.bill.user.email, |
|
|
|
|
Course.objects.get(token=invoice.bill.course_token).title, |
|
|
|
|
), |
|
|
|
|
'robo@skillbox.ru', |
|
|
|
|
[invoice.bill.opener.email], |
|
|
|
|
bcc=['dmitry.dolya@skillbox.ru'], |
|
|
|
|
) |
|
|
|
|
context = { |
|
|
|
|
'user_email': invoice.bill.user.email, |
|
|
|
|
'opener_full_name': invoice.bill.opener.get_full_name(), |
|
|
|
|
'course_title': Course.objects.get(token=invoice.bill.course_token).title, |
|
|
|
|
'date': str(invoice.date), |
|
|
|
|
'price': invoice.price, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
msg.send() |
|
|
|
|
subject, to = 'Выставлен новый счёт', invoice.bill.opener.email |
|
|
|
|
|
|
|
|
|
res = { |
|
|
|
|
"bill": BillSerializer(bill_obj).data, |
|
|
|
|
"children": [InvoiceSerializer(i).data for i in bill_obj.invoice_set.all()], |
|
|
|
|
} |
|
|
|
|
html_content = render_to_string('mail/sales/back_set_bill.html', context) |
|
|
|
|
text_content = strip_tags(html_content) |
|
|
|
|
|
|
|
|
|
return Response(res, status=200) |
|
|
|
|
msg = EmailMultiAlternatives(subject, text_content, to=[to], bcc=['dmitry.dolya@skillbox.ru']) |
|
|
|
|
msg.attach_alternative(html_content, "text/html") |
|
|
|
|
msg.send() |
|
|
|
|
|
|
|
|
|
invoice.save() |
|
|
|
|
|
|
|
|
|
return Response("Bill not set", status=400) |
|
|
|
|
return Response(InvoiceSerializer(invoice).data, status=200) |
|
|
|
|
|
|
|
|
|
return Response("Course detail access only for manager users", status=403) |
|
|
|
|
return Response("Invoice detail access only for manager users", status=403) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BillDetailView(APIView): |
|
|
|
|
@ -159,7 +223,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: |
|
|
|
|
@ -346,16 +412,23 @@ class YandexAvisoView(APIView): |
|
|
|
|
'response': xml_res, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
context = { |
|
|
|
|
'user_email': pay.invoice.bill.user.email, |
|
|
|
|
'opener_full_name': pay.invoice.bill.opener.get_full_name(), |
|
|
|
|
'course_title': Course.objects.get(token=pay.invoice.bill.course_token).title, |
|
|
|
|
'date': str(pay.invoice.date), |
|
|
|
|
'price': pay.invoice.price, |
|
|
|
|
'finish_date': pay.performed_datetime, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
msg = EmailMessage( |
|
|
|
|
'Успешная оплата.', |
|
|
|
|
'''Пользователь "%s", перевёл %s рублей. Номер платежа в яндекс кассе %s''' |
|
|
|
|
% (pay.invoice.bill.user.email, str(pay.invoice.price), str(data['invoiceId'])), |
|
|
|
|
'robo@skillbox.ru', |
|
|
|
|
[pay.invoice.bill.opener.email], |
|
|
|
|
bcc=['dmitry.dolya@skillbox.ru', 'vera.procenko@skillbox.ru'], |
|
|
|
|
) |
|
|
|
|
subject, to = 'Счёт оплачен', pay.invoice.bill.opener.email |
|
|
|
|
|
|
|
|
|
html_content = render_to_string('mail/sales/pay_access.html', context) |
|
|
|
|
text_content = strip_tags(html_content) |
|
|
|
|
|
|
|
|
|
msg = EmailMultiAlternatives( |
|
|
|
|
subject, text_content, to=[to], bcc=['dmitry.dolya@skillbox.ru', 'vera.procenko@skillbox.ru']) |
|
|
|
|
msg.attach_alternative(html_content, "text/html") |
|
|
|
|
msg.send() |
|
|
|
|
|
|
|
|
|
if pay.invoice.rebilling_on: |
|
|
|
|
|