Пример #1
0
    def post(self):
        try:
            requestDict = request.get_json()
            if not requestDict:
                response = {'error': 'No input data provided'}
                return response, status.HTTP_400_BAD_REQUEST

            idClient = int(requestDict['idClient'])
            totalShares = int(requestDict['totalShares'])
            amount = float(requestDict['amount'])
            interestRate = float(requestDict['interestRate'])
            idCampaign = int(requestDict['idCampaign'])
            idLead = int(requestDict['idLead'])
            idShareType = int(requestDict['idShareType'])
            share = float(requestDict['share'])
            idAccount = int(requestDict['idAccount'])
            commission = float(requestDict['commission'])

            #Update boolean in client
            client = Client.query.get_or_404(idClient)
            if client.activeLoans == 1:
                response = {'error': 'El cliente tiene un préstamo activo'}
                return response, status.HTTP_400_BAD_REQUEST
            client.activeLoans = 0  # Esto deberia ser 0 (Soli)
            client.update()
            today = datetime.now() - timedelta(hours=5)

            #Calculo de tea y tem
            tea = interestRate
            tem = (((1 + (tea / 100))**(1 / 12)) - 1)
            month = today.month
            countExtraMonths = 0
            numberExtra = 0
            auxDate = today
            for i in range(totalShares):
                auxDate = auxDate + timedelta(days=30)
                monthAuxDate = auxDate.month
                if (monthAuxDate == 7 or monthAuxDate == 12):
                    countExtraMonths += 1

            if (idShareType == 2):
                numberExtra = countExtraMonths
            pot = totalShares + numberExtra
            auxTem = tem + 1
            shareBase = round(
                (amount * ((1 + tem)**(totalShares + numberExtra)) * tem) /
                (((1 + tem)**(totalShares + numberExtra)) - 1), 2)
            initialDebt = amount
            day = today
            totalAmortization = 0
            totalInterest = 0
            totalComission = commission * totalShares
            totalShare = 0

            today = datetime.now() - timedelta(hours=5)

            #Obteniendo campaign
            campaign = Campaign.query.get_or_404(idCampaign)

            #Minus en bank account
            bankAccount = BankAccount.query.get_or_404(campaign.idCurrency)
            bankAccount.balance = bankAccount.balance - amount
            bankAccount.update()

            #Plus in AccountClient
            account = Account.query.get_or_404(idAccount)
            account.balance = account.balance + amount
            account.update()

            #Insert in salesRecord
            salesRecord = SalesRecord(origin='Web',
                                      requestDate=datetime.now() -
                                      timedelta(hours=5),
                                      idRecordStatus=1,
                                      active=1,
                                      idClient=idClient,
                                      idProduct=2)
            salesRecord.add(salesRecord)
            db.session.flush()

            #Insert in loan
            loan = Loan(totalShares=totalShares,
                        amount=amount,
                        interestRate=interestRate,
                        idLead=idLead,
                        idClient=idClient,
                        idSalesRecord=salesRecord.id,
                        idShareType=idShareType,
                        active=1,
                        idAccount=idAccount,
                        share=share,
                        commission=commission)
            loan.add(loan)
            db.session.flush()

            #Insert in shares
            for i in range(totalShares):
                auxShareBase = shareBase
                interest = round(initialDebt * tem, 2)
                day = day + timedelta(days=30)
                if (idShareType == 2):
                    if (day.month == 7 or day.month == 12):
                        auxShareBase = round(shareBase * 2, 2)
                amortization = auxShareBase - interest
                if (i == totalShares - 1):
                    amortization = initialDebt
                feeAmount = amortization + commission + interest
                totalAmortization += amortization
                totalInterest += interest
                totalShare += feeAmount
                share = Share(initialBalance=initialDebt,
                              amortization=amortization,
                              interest=interest,
                              commission=commission,
                              feeAmount=feeAmount,
                              dueDate=day,
                              idLoan=loan.id,
                              shareNumber=i + 1)
                share.add(share)
                initialDebt = initialDebt - amortization

            #Insert in transaction
            transaction = Transaction(datetime=today,
                                      amount=amount,
                                      idAccount=idAccount,
                                      idBankAccount=campaign.idCurrency,
                                      active=1)
            transaction.add(transaction)

            lead = Lead.query.get_or_404(idLead)
            lead.active = 0
            lead.update()

            #Commit changes
            db.session.commit()

            regLoan = Loan.query.get(loan.id)
            d = {}
            d['idLoan'] = str(regLoan.id)
            d['totalShares'] = str(regLoan.totalShares)
            d['amount'] = str(regLoan.amount)
            d['interestRate'] = str(regLoan.interestRate)
            d['idClient'] = str(regLoan.idClient)
            d['idSalesRecord'] = str(regLoan.idSalesRecord)
            d['idShareType'] = str(regLoan.idShareType)
            d['active'] = str(regLoan.active)
            prospectiveClient = ProspectiveClient.query.get_or_404(
                client.idProspectiveClient)
            person = Person.query.get_or_404(prospectiveClient.idPerson)
            currency = Currency.query.get_or_404(campaign.idCurrency)
            sharesA = Share.query.filter_by(idLoan=loan.id)
            shares = []
            for sha in sharesA:
                e = sha.toJson()
                shares.append(e)
            from mailing import mail
            msg = Message("Tunke - Prestamo exitoso",
                          sender="*****@*****.**",
                          recipients=[prospectiveClient.email1])
            msg.body = 'Enhorabuena, tu prestamo se realizo satisfactoriamente'
            fullName = person.firstName + ' ' + person.fatherLastname
            accNumber = str(account.accountNumber)
            curName = str(currency.currencyName)
            amount = str(d['amount'])
            currencySymbol = str(currency.currencySymbol)
            msg.html = render_template('loans.html',
                                       name=fullName,
                                       accountNumber=accNumber,
                                       currency=curName,
                                       amount=amount)
            logging.debug('Rendered')
            rendered = render_template(
                'calendar.html',
                shares=shares,
                currencySymbol=currencySymbol,
                totalAmortization=str(round(totalAmortization, 2)),
                totalInterest=str(round(totalInterest, 2)),
                totalComission=str(round(totalComission, 2)),
                totalShare=str(round(totalShare, 2)))
            logging.debug('Pdfkit')
            pdf = pdfkit.from_string(rendered, False)
            logging.debug('PDF')
            msg.attach("Calendario.pdf", "application/pdf", pdf)
            mail.send(msg)
            return d, status.HTTP_201_CREATED

        except SQLAlchemyError as e:
            db.session.rollback()
            response = {'error': str(e)}
            return response, status.HTTP_400_BAD_REQUEST

        except Exception as e:
            db.session.rollback()
            response = {'error': str(e)}
            logging.debug(str(e))
            return response, status.HTTP_400_BAD_REQUEST
Пример #2
0
def add_transaction():
    t = Transaction(request.form['amount'], request.form['reoccurs'],
            description=request.form['description'])
    Transaction.add(t)
    # adds a transaction
    pass
Пример #3
0
    def put(self, id):
        try:
            requestDict = request.get_json()
            if not requestDict:
                response = {'error': 'No input data provided'}
                return response, status.HTTP_400_BAD_REQUEST

            state = requestDict['state']

            salesRecord = SalesRecord.query.get_or_404(id)
            loan = Loan.query.filter_by(idSalesRecord=id).first()
            loan = loan.toJson()
            account = Account.query.get_or_404(loan['idAccount'])
            currency = Currency.query.get_or_404(account.idCurrency)
            currency = currency.toJson()
            aux = account.toJson()
            client = Client.query.get_or_404(loan['idClient'])
            prospectiveClient = ProspectiveClient.query.get_or_404(
                client.idProspectiveClient)
            bankAccount = BankAccount.query.get_or_404(aux['idCurrency'])
            if state == 1:  #aprobado
                salesRecord.idRecordStatus = 1
                salesRecord.requestDate = datetime.now() - timedelta(hours=5)
                account.balance = account.balance + loan['amount']
                bankAccount.balance = bankAccount.balance - loan['amount']
                bankAccount.update()
                transaction = Transaction(datetime=datetime.now() -
                                          timedelta(hours=5),
                                          amount=loan['amount'],
                                          idAccount=loan['idAccount'],
                                          idBankAccount=aux['idCurrency'],
                                          active=1)
                transaction.add(transaction)
                client.activeLoans = 0
            elif state == 2:
                salesRecord.idRecordStatus = 2
                salesRecord.requestDate = datetime.now() - timedelta(hours=5)
                client.activeLoans = 0

            salesRecord.update()
            client.update()
            account.update()
            db.session.commit()
            response = {'ok': 'Prestamo actualizado satisfactoriamente.'}

            from mailing import mail

            if state == 1:
                msg = Message("Tunke - Prestamo aprobado",
                              sender="*****@*****.**",
                              recipients=[prospectiveClient.email1])
                msg.body = 'Enhorabuena, tu prestamo fue aprobado'
                sharesA = Share.query.filter_by(idLoan=loan['idLoan'])
                shares = []
                totalAmortization = 0
                totalInterest = 0
                totalComission = 0
                totalShare = 0
                for sha in sharesA:
                    e = sha.toJson()
                    shares.append(e)
                    totalAmortization += e['amortization']
                    totalInterest += e['interest']
                    totalComission += e['commission']
                    totalShare += e['feeAmount']
                currencySymbol = currency['currencySymbol']
                rendered = render_template(
                    'calendar.html',
                    shares=shares,
                    currencySymbol=currencySymbol,
                    totalAmortization=str(round(totalAmortization, 2)),
                    totalInterest=str(round(totalInterest, 2)),
                    totalComission=str(round(totalComission, 2)),
                    totalShare=str(round(totalShare, 2)))
                pdf = pdfkit.from_string(rendered, False)
                msg.attach("Calendario.pdf", "application/pdf", pdf)
            elif state == 2:
                msg = Message("Tunke - Prestamo rechazado",
                              sender="*****@*****.**",
                              recipients=[prospectiveClient.email1])
                msg.body = 'Lo sentimos, tu prestamo fue rechazado. Acercarse a las oficinas para mas informacion'
            mail.send(msg)
            return response, status.HTTP_200_OK

        except SQLAlchemyError as e:
            db.session.rollback()
            response = {'error': str(e)}
            print(e)
            return response, status.HTTP_400_BAD_REQUEST

        except Exception as e:
            db.session.rollback()
            response = {'error': str(e)}
            print(e)
            return response, status.HTTP_400_BAD_REQUEST