def done(self): if self.status == self.STATUS_TYPES.BOOKED: self.status = self.STATUS_TYPES.DONE super(self.__class__, self).save() rooms.edit(roomid=self.scribblar_id, locked='1') from apps.profile.models import UserCreditMovement, Referral tutor = self.tutor tutor_profile = tutor.profile try: referral = Referral.objects.filter(user = tutor, used=False, activated=True).latest('id') referral.used = True referral.save() self.earning_fee = self.credit_fee self.universal_fee = 0 super(self.__class__, self).save() except Referral.DoesNotExist: user_discount = tutor_profile.get_active_discount() if user_discount: discount = user_discount.discount if discount.discount_percentage: diff = self.universal_fee * discount.discount_percentage self.universal_fee -= diff self.earning_fee += diff if discount.discount_fixed: diff = discount.discount_fixed if self.universal_fee - discount.discount_fixed >= 0 else self.universal_fee self.universal_fee -= diff self.earning_fee += diff super(self.__class__, self).save() user_discount.use() tutor_profile.income += self.earning_fee tutor_profile.income_without_commission += self.credit_fee tutor_profile.classes_given = tutor.classes_as_tutor.filter(status=self.STATUS_TYPES.DONE).count() tutor_profile.save() tutor.movements.create(type=UserCreditMovement.MOVEMENTS_TYPES.INCOME, credits=self.credit_fee, related_class=self) tutor_profile.send_notification(tutor_profile.NOTIFICATIONS_TYPES.INCOME, { 'class': self, 'student': self.student, 'tutor': tutor, }) for referral in Referral.objects.select_related().filter(Q(key=tutor_profile.referral_key) | Q(key=self.student.profile.referral_key), Q(used=False)): user = referral.user profile = user.profile count = user.referrals.filter(user=user, used=True).count() if count < 3: if profile.type == profile.TYPES.STUDENT or profile.type == profile.TYPES.UNDER16: profile.receive_referral(REFERRAL_CREDITS) referral.used = True elif profile.type == profile.TYPES.TUTOR: referral.activated = True else: referral.used = True referral.save()
def save(self, *args, **kwargs): tutor = self.tutor tutor_profile = tutor.profile is_new = not self.id if is_new: if tutor_profile.check_period(self.date, self.date.time(), (self.date + datetime.timedelta(minutes=self.duration)).time(), gtz=pytz.utc): self.credit_fee = self.subject_credits_per_hour * (self.duration / 60.0) self.earning_fee = self.credit_fee * (1 - UNIVERSAL_FEE) self.universal_fee = self.credit_fee * UNIVERSAL_FEE super(self.__class__, self).save(*args, **kwargs) else: super(self.__class__, self).save(*args, **kwargs) if self.scribblar_id: try: rooms.edit(roomid=self.scribblar_id, roomname='%s' % self.subject) except: self.create_scribblar_class()