Example #1
0
    def confirm(self):
        """
        Confirm deposit, defreeze account and transfer money
        """
        if self.status != Deposit.STATUS_REQUESTED_CREATING:
            return False

        bank_confirmation = BankSystemProxy.deposit_creation(
            self.client.id, self.template.id, self.duration,
            float(self.percentage), float(self.amount.amount), self.currency)
        if not bank_confirmation:
            self.reject('Отказано банком.')
            return False

        self.is_active = True
        self.status = Deposit.STATUS_ACTIVE
        account = Account.objects.get(pk=self.source_account_id)
        money_in_byn = FinanceSettings.get_instance().exchange_rates[self.currency] * \
                       float(self.amount.amount)
        account.status = Account.STATUS_ACTIVE
        account.get_money(money_in_byn)
        account.save()
        self.save()
        Transaction.objects.create(
            client=self.client,
            product=self,
            info='Депозит открыт.',
            type=Transaction.TYPE_DEPOSIT_CONFIRM_CREATE)
        Contract.objects.create(client=self.client, product=self)
        return True
Example #2
0
    def confirm(self):
        if self.status != self.STATUS_REQUESTED:
            return False
        bank_confirmation = BankSystemProxy.credit_create(
            self.client.id, self.template.id, float(self.residue.amount),
            self.duration, float(self.annual_percentage_rate))
        if not bank_confirmation:
            self.reject('Отказано банком')
            return False

        if self.money_destination == Credit.INTO_ACCOUNT:
            account = Account.objects.get(pk=self.target_account_id)
            account.confirm()
            account.put_money(self.residue.amount)
            account.save()

        Transaction.objects.create(client=self.client,
                                   product=self,
                                   info='Заявка на кредит одобрена.',
                                   type=Transaction.TYPE_CREDIT_CONFIRM)

        Contract.objects.create(client=self.client, product=self)
        self.is_active = True
        self.status = Credit.STATUS_OPENED
        self.save()
        return True
Example #3
0
 def close_confirm(self):
     if self.status != self.STATUS_REQUESTED_CLOSING:
         return False
     bank_confirmation = BankSystemProxy.deposit_closing(self.id)
     if not bank_confirmation:
         self.reject('Отказано банком.')
         return False
     cur_date = datetime.date.today()
     end_date = self.start_date + relativedelta(months=self.duration)
     if self.next_capitalize_term < min(
             end_date, cur_date):  # last capitalize, if need
         self._update_amount()
     money_in_byn = FinanceSettings.get_instance().exchange_rates[
         self.currency] * float(self.amount.amount)
     account = Account.objects.get(pk=self.target_account_id)
     account.put_money(money_in_byn)
     self.status = self.STATUS_CLOSED
     self.save()
     Transaction.objects.create(
         client=self.client,
         product=self,
         info='Депозит закрыт. Деньги в размере {} BYN переведены на счет {}'
         .format(money_in_byn, self.target_account_id),
         type=Transaction.TYPE_DEPOSIT_CONFIRM_CLOSE)
     return True
Example #4
0
    def assign(cls, client, account_number):
        """
        assign account by account_number to client
        """
        if Account.objects.filter(number=account_number):
            account = Account.objects.filter(number=account_number).first()
            if account.status == Account.STATUS_CLOSED:
                account.status = Account.STATUS_ACTIVE
                account.save()
            else:
                return False, 'Этот счет уже используется.'
        else:
            res, money_amount = BankSystemProxy.account_assign_to_user(
                client, account_number)
            if not res:
                return False, 'Отказано банком. Проверте счет.'

            account = Account.objects.create(number=account_number,
                                             is_active=True,
                                             residue=money_amount,
                                             status=Account.STATUS_ACTIVE,
                                             client=client)
            account.save()

        info_text = 'Счет успешно привязан.'
        Transaction.objects.create(client=client,
                                   product=account,
                                   info=info_text,
                                   type=Transaction.TYPE_ACCOUNT_ASSIGNED)
        return True, info_text
Example #5
0
    def create_online(cls,
                      client,
                      template,
                      money_amount,
                      duration,
                      account_id=None):
        bank_confirmation = BankSystemProxy.credit_create(
            client.id, template.id, money_amount, duration,
            float(template.annual_percentage_rate))
        if not bank_confirmation:
            return False, 'Отказано банком'

        success, scoring_result = client.get_scoring()
        if not success:
            return False, scoring_result
        elif scoring_result['level'] != 'success':
            return False, 'Ваша заявка отклонена кредитным скорингом. ' \
                          'У вас недостаточная платежеспособность'

        monthly_pay = cls._min_monthly_pay(money_amount,
                                           template.annual_percentage_rate,
                                           duration)
        if account_id is None:
            account = Account.create(True, client, 0)
            account_id = account.id

        credit = cls.objects.create(
            is_active=True,
            client=client,
            template=template,
            total_sum=0,
            residue=money_amount,
            current_penalty=0,
            start_date=datetime.date.today() + relativedelta(days=1),
            duration=duration,
            next_payment_term=datetime.date.today() + relativedelta(months=1),
            status=cls.STATUS_OPENED,
            annual_percentage_rate=template.annual_percentage_rate,
            fine_percentage=template.fine_percentage,
            minimum_monthly_pay=monthly_pay,
            current_month_pay=0,
            current_month_percents=0,
            method_of_ensuring=CreditTemplate.ENSURING_FINE,
            money_destination=cls.INTO_ACCOUNT,
            target_account_id=account_id)
        credit.save()

        target_account = Account.objects.get(pk=account_id)
        target_account.put_money(money_amount)
        target_account.save()

        info_text = 'Открыт онлайн кредит кредит.'
        Transaction.objects.create(client=client,
                                   product=credit,
                                   info=info_text,
                                   type=Transaction.TYPE_CREDIT_OPENED)
        return True, credit
Example #6
0
 def update_exchange_rates(self):
     rates = BankSystemProxy.get_currency_rates()
     if not rates:
         logger.info('Error while updating currency info')
     else:
         rates['BYN'] = 1.0
         self.exchange_rates = rates
         self.currencies = rates.keys()
         self.save()
Example #7
0
    def pay(self, payment):
        """
        Processes the payment for the credit.
        Steps:
            0. assert credit != CLOSED
            1. If credit FINED, then
                a. Try to pay penalty
                b. If penalty payed, then change credit status to OPENED, then continue
            2. If credit OPENED or PAYED_OFF(client should explicitly close credit), then
                Every payment goes to pay for current credit percents and for decrease residue, so
                a. Pay for percents
                b. Decrease residue
                c. Inc current_month_pay
        We can multiple payments during one month.
        """
        if self.status in Credit.INOPERABLE_STATUSES:
            return False

        bank_confirmation = BankSystemProxy.credit_pay(self.id, float(payment))
        if not bank_confirmation:
            return False

        payment = Decimal(payment)
        self.total_sum.amount += payment
        if self.status == Credit.STATUS_FINED:
            if self.current_penalty.amount <= payment:
                days_late = (datetime.date.today() -
                             self.next_payment_term).days
                profile = self.client.profile
                profile.treat_days_late(days_late)
                payment, self.current_penalty.amount = payment - self.current_penalty.amount, 0
                self.status = Credit.STATUS_OPENED
            else:
                self.current_penalty.amount -= payment
                payment = 0
        self.current_month_pay.amount += payment  # inc current_month_pay
        cur_percents = self.current_month_percents.amount
        cur_percents, payment = max(0, cur_percents - payment), max(
            0, payment - cur_percents)
        self.current_month_percents = cur_percents
        self.residue.amount -= payment  # can be < 0
        if self.residue.amount <= 0:
            self.status = Credit.STATUS_PAID_OFF
        Transaction.objects.create(
            client=self.client,
            product=self,
            info='Внесен платеж по кредиту в размере: {}.'.format(payment),
            type=Transaction.TYPE_CREDIT_PAY)
        self.save()
        return True
Example #8
0
    def put_money(self, contribution):
        """
        In BYN only
        """
        if self.status in Account.INOPERABLE_STATUSES:
            return False

        bank_confirmation = BankSystemProxy.account_put_money(self.id, float(contribution))
        if not bank_confirmation:
            return False

        Transaction.objects.create(client=self.client,
                                   product=self,
                                   info='Зачисление в размере {} BYN'.format(contribution),
                                   type=Transaction.TYPE_ACCOUNT_PUT_MONEY)
        self.residue.amount += Decimal(contribution)
        self.save()
        return True
Example #9
0
    def close(self):
        """
        User can close credit without confirmation, if credit is paid_off
        Attempts to close the credit. Throws exceptions if the attempt fails
        """
        if self.status != Credit.STATUS_PAID_OFF:
            return False

        bank_confirmation = BankSystemProxy.credit_close(self.id)
        if not bank_confirmation:
            return False

        self.status = Credit.STATUS_CLOSED
        self.save()

        Transaction.objects.create(client=self.client,
                                   product=self,
                                   info='Кредит закрыт.',
                                   type=Transaction.TYPE_CREDIT_CLOSE)

        return True
Example #10
0
    def get_money(self, required_quantity):
        """
        In BYN only
        """
        if self.status in Account.INOPERABLE_STATUSES:
            return False

        if self.residue.amount >= Decimal(required_quantity):
            self.residue.amount -= Decimal(required_quantity)
            self.save()

            bank_confirmation = BankSystemProxy.account_get_money(self.id,
                                                                  required_quantity)
            if not bank_confirmation:
                return False

            Transaction.objects.create(client=self.client,
                                       product=self,
                                       info='Снятие в размере {} BYN'.format(required_quantity),
                                       type=Transaction.TYPE_ACCOUNT_PUT_MONEY)
            return True
        return False
Example #11
0
    def confirm(self):
        """
        Confirm account
        """
        if self.status != self.STATUS_REQUESTED_CREATING:
            return False

        bank_confirmation = BankSystemProxy.account_creation(
            self.number, self.client.id, float(self.residue.amount))
        if not bank_confirmation:
            self.reject('Отклонено банком')
            return False

        self.is_active = True
        self.status = Account.STATUS_ACTIVE
        self.save()
        Transaction.objects.create(
            client=self.client,
            product=self,
            info='Счет подтвержден.',
            type=Transaction.TYPE_ACCOUNT_CONFIRM_CREATE)
        Contract.objects.create(client=self.client, product=self)
        return True