Advertisement
MaximTakkaTo

Untitled

Apr 8th, 2021
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import sqlite3
  2. import time
  3. from datetime import date
  4.  
  5. class CreditPayments:
  6.  
  7.     def __init__(self):
  8.         # connect to DB
  9.         # save cursor object to self property
  10.         self.conn = sqlite3.connect("mydatabase.db")
  11.         self.cursor = self.conn.cursor()
  12.        
  13.     #method convert date from MM.yyyy in unix format
  14.     def get_timestamp(self, x):
  15.         # Calc date in unix format
  16.         date_arr = x.split(".")
  17.         d = date(int(date_arr[1]), int(date_arr[0]), 1)
  18.         unixtime = time.mktime(d.timetuple())
  19.         return unixtime
  20.    
  21.     #method getRestOfCredit
  22.     def getRestOfCredit(self, id, date):
  23.         # get summ of credit
  24.         # query payments from DB where deal_id=id AND pdate<=date
  25.         # calculate rest of credit and return it
  26.         sql = "SELECT SUM(summ) FROM payment WHERE deal_id = ? AND pdate <= ?"
  27.         self.cursor.execute(sql, [(id), (self.get_timestamp(date))])
  28.         payment = self.cursor.fetchone()[0]
  29.         return self.getSummOfCredit(id) - payment
  30.    
  31.     def getAllRestOfCredit(self, id):
  32.         return self.getRestOfCredit(id, "12.2018")
  33.  
  34.     #method getSummOfCredit
  35.     def getSummOfCredit(self, id):
  36.         # query summ of credit from DB where id=id
  37.         # return it
  38.         sql = "SELECT summ FROM credit WHERE id = ?"
  39.         self.cursor.execute(sql, [id])
  40.         return self.cursor.fetchone()[0]
  41.    
  42.     def deleteCredit(self, id):
  43.         sql = "DELETE FROM payment WHERE deal_id = ?"
  44.         self.cursor.execute(sql, [id])
  45.         sql = "DELETE FROM credit WHERE id = ?"
  46.         self.cursor.execute(sql, [id])
  47.         self.conn.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement