diff --git a/apps/course/templates/course/course.html b/apps/course/templates/course/course.html index 5b651689..69f6e5df 100644 --- a/apps/course/templates/course/course.html +++ b/apps/course/templates/course/course.html @@ -29,16 +29,15 @@ Редактировать {% endif %} {% if course.author != request.user and not paid and course.price %} - {% if pending %}ОЖИДАЕТСЯ ПОДТВЕРЖДЕНИЕ ОПЛАТЫ{% else %}КУПИТЬ КУРС{% endif %} {% endif %} @@ -159,12 +158,14 @@ >УРОКИ {% else %} - УРОКИ {% if not paid and course.price %} @@ -323,16 +324,15 @@ {% endif %} {% if course.author != request.user and not paid and course.price %} - {% if pending %}ОЖИДАЕТСЯ ПОДТВЕРЖДЕНИЕ ОПЛАТЫ{% else %}КУПИТЬ КУРС{% endif %} {% endif %} diff --git a/apps/payment/models.py b/apps/payment/models.py index b59269c9..0d406323 100644 --- a/apps/payment/models.py +++ b/apps/payment/models.py @@ -84,6 +84,11 @@ class PaymentManger(PolymorphicManager): class Payment(PolymorphicModel): + PW_PAID_STATUSES = [ + Pingback.PINGBACK_TYPE_REGULAR, + Pingback.PINGBACK_TYPE_GOODWILL, + Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, + ] PW_STATUS_CHOICES = ( (Pingback.PINGBACK_TYPE_REGULAR, 'regular',), (Pingback.PINGBACK_TYPE_GOODWILL, 'goodwill',), @@ -125,6 +130,10 @@ class Payment(PolymorphicModel): def calc_amount(cls, payment=None, user=None, course=None, date_start=None, weekdays=None): date_start = date_start or now().date() date_end = Payment.add_months(date_start, 1) + price = 0 + discount = 0 + referral_bonus = 0 + referrer_bonus = 0 if isinstance(payment, CoursePayment): course = payment.course user = payment.user @@ -132,14 +141,12 @@ class Payment(PolymorphicModel): user = payment.user weekdays = payment.weekdays date_start = payment.date_start - price = 0 - referral_bonus = 0 - referrer_bonus = 0 if hasattr(user, 'referral') and not user.referral.payment: referral_bonus = user.referral.bonus referrer_bonus = user.referral.referrer_bonus - discount = 0 - if course: + if payment and payment.is_paid(): + price = payment.amount + elif course: price = course.price else: if user: @@ -191,9 +198,15 @@ class Payment(PolymorphicModel): 'weekdays': weekdays, } + # TODO? change to property? def calc_commission(self): return self.amount * config.SERVICE_COMMISSION / 100 + # TODO change to property + def is_paid(self): + return self.status in self.PW_PAID_STATUSES + + # TODO? delete ? change to property def is_deliverable(self): return self.status in [ Pingback.PINGBACK_TYPE_REGULAR, @@ -201,9 +214,11 @@ class Payment(PolymorphicModel): Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, ] + # TODO change to property def is_under_review(self): return self.status == Pingback.PINGBACK_TYPE_RISK_UNDER_REVIEW + # TODO change to property def is_cancelable(self): return self.status in [ Pingback.PINGBACK_TYPE_NEGATIVE, @@ -211,19 +226,15 @@ class Payment(PolymorphicModel): ] def save(self, *args, **kwargs): - paid = self.status in [Pingback.PINGBACK_TYPE_REGULAR, Pingback.PINGBACK_TYPE_GOODWILL, - Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED,] amount_data = Payment.calc_amount(payment=self) - print('amount_data', amount_data) if self.status is None and not self.bonus: - print(123) self.amount = amount_data.get('amount') if isinstance(self, SchoolPayment): self.weekdays = amount_data.get('weekdays') super().save(*args, **kwargs) - if isinstance(self, CoursePayment) and paid: + if isinstance(self, CoursePayment) and self.is_paid(): author_balance = getattr(self, 'authorbalance', None) - if not author_balance : + if not author_balance: AuthorBalance.objects.create( author=self.course.author, amount=self.amount, @@ -233,7 +244,7 @@ class Payment(PolymorphicModel): author_balance.amount = self.amount author_balance.save() # Если юзер реферал и нет платежа, где применялась скидка - if hasattr(self.user, 'referral') and not self.user.referral.payment and paid: + if hasattr(self.user, 'referral') and not self.user.referral.payment and self.is_paid(): # Платеж - как сигнал, что скидка применилась self.user.referral.payment = self self.user.referral.save() diff --git a/apps/payment/views.py b/apps/payment/views.py index 5663df39..6be7734f 100644 --- a/apps/payment/views.py +++ b/apps/payment/views.py @@ -58,6 +58,7 @@ class CourseBuyView(TemplateView): template_name = 'payment/paymentwall_widget.html' def get(self, request, pk=None, *args, **kwargs): + use_bonuses = request.GET.get('use_bonuses') host = urlsplit(self.request.META.get('HTTP_REFERER')) host = str(host[0]) + '://' + str(host[1]) course = Course.objects.get(id=pk) @@ -70,9 +71,22 @@ class CourseBuyView(TemplateView): course=course, roistat_visit=roistat_visit, ) + if use_bonuses: + if request.user.bonus >= course_payment.amount: + bonus = UserBonus.objects.create(amount= -course_payment.amount, user=request.user, payment=course_payment) + course_payment.amount = 0 + course_payment.status = Pingback.PINGBACK_TYPE_REGULAR + else: + bonus = UserBonus.objects.create(amount= -request.user.bonus, user=request.user, + payment=course_payment) + course_payment.amount -= request.user.bonus + course_payment.bonus = bonus + course_payment.save() + if course_payment.is_paid(): + return redirect(reverse_lazy('course_payment_success', args=[course.id])) product = Product( f'course_{course_payment.id}', - course.price, + course_payment.amount, 'RUB', f'Курс "{course.title}"', ) @@ -156,7 +170,7 @@ class SchoolBuyView(TemplateView): school_payment.amount -= request.user.bonus school_payment.bonus = bonus school_payment.save() - if school_payment.status == Pingback.PINGBACK_TYPE_REGULAR: + if school_payment.is_paid(): return redirect(reverse_lazy('payment-success')) product = Product( f'school_{school_payment.id}', diff --git a/apps/school/templates/blocks/schedule.html b/apps/school/templates/blocks/schedule.html index ab97055f..15b129fb 100644 --- a/apps/school/templates/blocks/schedule.html +++ b/apps/school/templates/blocks/schedule.html @@ -5,7 +5,7 @@
Расписание
- {% for school_schedule in school_schedules %} + {% for school_schedule in school_schedules_sorted %} {% include 'blocks/schedule_item.html' with school_schedule=school_schedule live_lesson=school_schedule.current_live_lesson purchased=True %} {% endfor %}
diff --git a/apps/school/templates/blocks/schedule_purchased.html b/apps/school/templates/blocks/schedule_purchased.html index 5d1a5827..eb7cc8e2 100644 --- a/apps/school/templates/blocks/schedule_purchased.html +++ b/apps/school/templates/blocks/schedule_purchased.html @@ -6,9 +6,11 @@