class Inversion(db.Model): """ class to create an inversion """ __tablename__ = 'inversions' id = db.Column(db.String(60), primary_key=True, default=uuid.uuid4()) budget = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) #rappi_points = db.Column(db.Integer, nullable=False) user_id = db.Column(db.String(60), db.ForeignKey('users.id'), nullable=False) def __repr__(self): """Return a representacion""" return '<Inversion {}: created at {}>'.format(self.budget, self.created_at) def save(self): """Save the new element""" db.session.add(self) db.session.commit()
class CommonFound(db.Model): """Create the found""" __tablename__ = "commonfound" id = db.Column(db.Integer, primary_key=True) total_money = db.Column(db.Integer, nullable=False, default=0) debt_money = db.Column(db.Integer, nullable=False, default=0) created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) profits = db.Column(db.Integer, default=0) def __init__(self): """Create the Found""" self.total_money = 0 self.debt_money = 0 def __repr__(self): """Send the information""" return '<Commond Found: Money = {}, Debt = {} Last Update = {}>'.format( self.total_money, self.debt_money, self.updated_at) def add_inversion(self, money): """Add the manoy of an inversion""" # add in DB self.total_money += money self.update_at = datetime.utcnow() db.session.add(self) db.session.commit() # Put the history in a file date = datetime.utcnow() goodDate = date.strftime('%d-%m-%Y') try: with open("found_history.csv") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') except FileNotFoundError: with open("found_history.csv", "w", newline="", encoding="utf-8") as csv_file: writer = csv.writer(csv_file) writer.writerow(["Change Date", "Total Money", "Debt Money", "Type Change", "Change Value"]) with open("found_history.csv", "a", newline="", encoding="utf-8") as csv_file: writer = csv.writer(csv_file) writer.writerow([goodDate, self.total_money, self.debt_money, "Inversion", money]) def put_debt(self, money): """Minus the debt""" self.total_money -= money self.debt_money += money self.updated_at = datetime.utcnow() db.session.add(self) db.session.commit()
class Admin(db.Model): """Class to make the querys to the database""" __tablename__ = 'admins' id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(60), nullable=False) last_name = db.Column(db.String(60), nullable=False) username = db.Column(db.String(255), unique=True, nullable=False) pwd = db.Column(db.String(255), nullable=False) credentials = db.Column(db.Integer, default=1) def __init__(self, first_name, last_name, username, pwd): """Set the columns in atributes""" self.first_name = first_name self.last_name = last_name self.username = username self.pwd = pwd def save(self): """Save the new object into the data base""" db.session.add(self) db.session.commit()
class User(db.Model): """Class to make the querys to the database""" __tablename__ = 'users' id = db.Column(db.String(60), primary_key=True) created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) first_name = db.Column(db.String(60), nullable=False) last_name = db.Column(db.String(60), nullable=False) email = db.Column(db.String(255), unique=True, nullable=False) pwd = db.Column(db.String(255), nullable=False) document = db.Column(db.String(25), nullable=False) phone = db.Column(db.String(25), nullable=False) reg_cod = db.Column(db.String(6), nullable=True) validated = db.Column(db.Boolean) profits = db.Column( db.Integer, default=0) # Create a funtion that make the payment to the profits inversions = db.relationship('Inversion', cascade='all,delete-orphan', backref='owner') debts = db.relationship('Debt', cascade='all,delete-orphan', backref='owner') def __init__(self, first_name, last_name, email, document, phone, pwd, keyGen=''): """Set the columns in atributes""" self.id = str(uuid4()) self.first_name = first_name self.last_name = last_name self.email = email self.document = document self.phone = phone self.pwd = pwd self.reg_cod = keyGen self.validated = False def __repr__(self): """Return a good representation""" dictionary = { 'first_name': self.first_name, 'last_name': self.last_name, 'email': self.email, 'pwd': self.pwd, 'reg_cod': self.reg_cod, 'validated': self.validated } return str(dictionary) def save(self): """Save the new object into the data base""" db.session.add(self) db.session.commit() def update(self, phone): """Update the information""" self.phone = phone
class Debt(db.Model): """Class to create debt to a rappitender""" __tablename__ = "debts" id = db.Column(db.String(60), primary_key=True, default=uuid.uuid4()) debt = db.Column(db.Integer, nullable=False) # Total de la deuda realtive_debt = db.Column(db.Float, default=0) #Deuda disminuida a capital interest_rate = db.Column(db.Float, default=0) # Tasa de interes fee_payment = db.Column(db.Integer, default=0) # Cantidad de cuotas pactadas fee_value = db.Column(db.Float, default=0) # Valor de la cuota actual_fee_payment = db.Column(db.Integer, default=0) # Cuotas pagadas #fee_state = db.Column(db.Boolean) capital_pay = db.Column(db.Float, default=0) interest_pay = db.Column(db.Float, default=0) reason = db.Column(db.String(500)) # Razon de la peticion del prestamo state = db.Column(db.String(20), nullable=False, default="In progress") # Estado del prestamo created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) user_id = db.Column(db.String(60), db.ForeignKey('users.id'), nullable=False) def save(self): """Save the new element""" db.session.add(self) db.session.commit() def update_pay(self, payment): """Update the mountto pay""" if payment == self.fee_value: self.interest_pay = self.interest_rate / self.realtive_debt print("Este es el interes", self.interest_pay) print(self.fee_value) self.capital_pay = self.fee_value - self.interest_pay print(self.capital_pay) self.realtive_debt -= self.capital_pay self.updated_at = datetime.utcnow() self.actual_fee_payment += 1 else: return "La el valor cuota es incorrecta" def confirmation(self, confirm, rate=0, fee_payment=0): """Put the state and the interes""" if confirm == "postulated": self.state = "Postulated" self.interest_rate = rate self.fee_payment = fee_payment goodRate = rate / 100 # Valor de la cuota (formula de interes compuesto) self.fee_value = (goodRate * self.debt) / (1 - (pow(1 + goodRate, (self.fee_payment * -1)))) print(self.fee_value) if confirm == "rejected": self.state = "Rejected" if confirm == "accepted": self.realtive_debt = self.debt self.state = "Accepted" if confirm == "payed": self.state = "Payed"