|
|
|
|
@ -103,6 +103,7 @@ class Payment(PolymorphicModel): |
|
|
|
|
roistat_visit = models.PositiveIntegerField('Номер визита Roistat', null=True, editable=False) |
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True) |
|
|
|
|
update_at = models.DateTimeField(auto_now=True) |
|
|
|
|
bonus = models.ForeignKey('payment.UserBonus', null=True, on_delete=models.SET_NULL, related_name='purchase_payments') |
|
|
|
|
|
|
|
|
|
objects = PaymentManger() |
|
|
|
|
|
|
|
|
|
@ -111,6 +112,15 @@ class Payment(PolymorphicModel): |
|
|
|
|
verbose_name_plural = 'Платежи' |
|
|
|
|
ordering = ('created_at',) |
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
def add_months(cls, sourcedate, months=1): |
|
|
|
|
result = arrow.get(sourcedate, settings.TIME_ZONE).shift(months=months) |
|
|
|
|
if months == 1: |
|
|
|
|
if (sourcedate.month == 2 and sourcedate.day >= 28) or (sourcedate.day == 31 and result.day <= 30)\ |
|
|
|
|
or (sourcedate.month == 1 and sourcedate.day >= 29 and result.day == 28): |
|
|
|
|
result = result.replace(day=1, month=result.month + 1) |
|
|
|
|
return result.datetime |
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
def calc_amount(cls, payment=None, user=None, course=None, weekdays=None): |
|
|
|
|
if isinstance(payment, CoursePayment): |
|
|
|
|
@ -134,7 +144,6 @@ class Payment(PolymorphicModel): |
|
|
|
|
user=user, |
|
|
|
|
date_start__lte=now().date(), |
|
|
|
|
date_end__gte=now().date(), |
|
|
|
|
add_days=False, |
|
|
|
|
status__in=[ |
|
|
|
|
Pingback.PINGBACK_TYPE_REGULAR, |
|
|
|
|
Pingback.PINGBACK_TYPE_GOODWILL, |
|
|
|
|
@ -144,8 +153,8 @@ class Payment(PolymorphicModel): |
|
|
|
|
school_schedules_purchased = school_payments.annotate( |
|
|
|
|
joined_weekdays=Func(F('weekdays'), function='unnest', ) |
|
|
|
|
).values_list('joined_weekdays', flat=True).distinct() |
|
|
|
|
weekdays = set(map(int, weekdays)) - set(school_schedules_purchased) |
|
|
|
|
prev_school_payment = school_payments.last() |
|
|
|
|
weekdays = list(set(map(int, weekdays)) - set(school_schedules_purchased)) |
|
|
|
|
prev_school_payment = school_payments.filter(add_days=False).last() |
|
|
|
|
add_days = bool(prev_school_payment) |
|
|
|
|
else: |
|
|
|
|
add_days = False |
|
|
|
|
@ -173,6 +182,7 @@ class Payment(PolymorphicModel): |
|
|
|
|
'referral_bonus': referral_bonus, |
|
|
|
|
'referrer_bonus': referrer_bonus, |
|
|
|
|
'discount': discount, |
|
|
|
|
'weekdays': weekdays, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
def calc_commission(self): |
|
|
|
|
@ -197,10 +207,14 @@ class Payment(PolymorphicModel): |
|
|
|
|
def save(self, *args, **kwargs): |
|
|
|
|
amount_data = Payment.calc_amount(payment=self) |
|
|
|
|
self.amount = amount_data.get('amount') |
|
|
|
|
if isinstance(self, SchoolPayment): |
|
|
|
|
self.weekdays = amount_data.get('weekdays') |
|
|
|
|
super().save(*args, **kwargs) |
|
|
|
|
paid = self.status in [Pingback.PINGBACK_TYPE_REGULAR, Pingback.PINGBACK_TYPE_GOODWILL, |
|
|
|
|
Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED,] |
|
|
|
|
if isinstance(self, CoursePayment): |
|
|
|
|
author_balance = getattr(self, 'authorbalance', None) |
|
|
|
|
if not author_balance: |
|
|
|
|
if not author_balance and paid: |
|
|
|
|
AuthorBalance.objects.create( |
|
|
|
|
author=self.course.author, |
|
|
|
|
amount=self.amount, |
|
|
|
|
@ -210,9 +224,7 @@ class Payment(PolymorphicModel): |
|
|
|
|
author_balance.amount = self.amount |
|
|
|
|
author_balance.save() |
|
|
|
|
# Если юзер реферал и нет платежа, где применялась скидка |
|
|
|
|
if hasattr(self.user, 'referral') and not self.user.referral.payment\ |
|
|
|
|
and self.status in [Pingback.PINGBACK_TYPE_REGULAR, Pingback.PINGBACK_TYPE_GOODWILL, |
|
|
|
|
Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED,]: |
|
|
|
|
if hasattr(self.user, 'referral') and not self.user.referral.payment and paid: |
|
|
|
|
# Платеж - как сигнал, что скидка применилась |
|
|
|
|
self.user.referral.payment = self |
|
|
|
|
self.user.referral.save() |
|
|
|
|
@ -258,3 +270,5 @@ class UserBonus(models.Model): |
|
|
|
|
referral = models.ForeignKey('user.Referral', on_delete=models.SET_NULL, null=True) |
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True) |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
ordering = ('created_at',) |
|
|
|
|
|