Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Subscription(models.Model):
- CHOICES_MOUNTH = (
- ('1', '1 month'),
- ('2', '2 month'),
- ('3', '3 month'),
- )
- user = models.ForeignKey(Patient, on_delete=models.SET_NULL, null = True)
- subscription_date = models.DateTimeField("Дата оформления подписки", default=datetime.now(), db_index=True)
- duration = models.CharField(max_length=12, choices=CHOICES_MOUNTH , default="1 month")
- cost = models.IntegerField('Стоимость услуги')
- subscription_end_date = models.DateTimeField("Конец подписки", default=datetime.now(), db_index=True)
- status = models.BooleanField("Статуc подписки : ", default = False)
- def __str__(self):
- return self.duration
- class HistoryBalance(models.Model):
- amount = models.IntegerField('Сумма операции')
- current_amount = models.IntegerField('Сумма текущего счёта')
- write_off = models.BooleanField('Снята ли блокировка с текущей суммы', default=False)
- billing_account = models.ForeignKey(BillingAccount, verbose_name= 'Пользовател', on_delete=models.CASCADE)
- datetime_create = models.DateTimeField('Дата создания операции',default=datetime.now(), db_index=True)
- class BillingAccount(models.Model):
- user = models.ForeignKey(Patient, on_delete=models.SET_NULL, null = True)
- datetime_create = models.DateTimeField('Дата создания аккаунта',default=datetime.now(), db_index=True)
- amount = models.IntegerField('Cумма на счету', default=0, null=False)
- def paymant_operation(self, amount):
- history = HistoryBalance.objects.create(
- current_amount = self.amount,
- amount = amount,
- write_off = True,
- billing_account = self
- )
- self.amount = self.amount - amount
- self.save()
- def check_status_amount(pacient_id):
- """Проверка суммы для списания со счёта !"""
- block_amount = Block_amount.objects.filter(write_off = False, billing_account__user = pacient_id)
- return block_amount.exists()
- def block_amount(self, amount):
- """Блокирование суммы для дальнейшего списания"""
- Block_amount.objects.create(billing_account = self, amount = amount, write_off = False)
- def take_away_block_amount(self, id):
- """отмена заблокированной суммы !"""
- block_amount = Block_amount.objects.get(id = id)
- block_amount.write_off = True
- block_amount.save()
- def get_summ_in_account(self):
- return self.amount
- class Block_amount(models.Model):
- amount = models.IntegerField('Сумма для блокировки аккаунта')
- write_off = models.BooleanField('Снята ли блокировка с текущей суммы', default=False)
- billing_account = models.ForeignKey(BillingAccount, verbose_name= 'Пользовател с заблокированной суммой', on_delete=models.CASCADE)
- datetime_block = models.DateTimeField('Дата создания аккаунта',default=datetime.now(), db_index=True)
- @receiver(post_save, sender=Patient)
- def save_or_create_account(sender, instance, created, **kwargs):
- if created:
- BillingAcount.objects.create(user=instance)
- else:
- try:
- instance.billingacount.save()
- except ObjectDoesNotExist:
- BillingAcount.objects.create(user=instance)
Add Comment
Please, Sign In to add comment