diff --git a/inventory_withsqlite/__pycache__/models.cpython-39.pyc b/inventory_withsqlite/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000..4b134d8 Binary files /dev/null and b/inventory_withsqlite/__pycache__/models.cpython-39.pyc differ diff --git a/inventory_withsqlite/__pycache__/sales1.cpython-39.pyc b/inventory_withsqlite/__pycache__/sales1.cpython-39.pyc new file mode 100644 index 0000000..37ab680 Binary files /dev/null and b/inventory_withsqlite/__pycache__/sales1.cpython-39.pyc differ diff --git a/inventory_withsqlite/data/inventory.db b/inventory_withsqlite/data/inventory.db new file mode 100644 index 0000000..0ef6941 Binary files /dev/null and b/inventory_withsqlite/data/inventory.db differ diff --git a/inventory_withsqlite/main.py b/inventory_withsqlite/main.py new file mode 100644 index 0000000..df592ca --- /dev/null +++ b/inventory_withsqlite/main.py @@ -0,0 +1,48 @@ +import sqlite3 +import models + +def initialize(): + sql_products_create_table = """ + CREATE TABLE IF NOT EXISTS products ( + id integer PRIMARY KEY, + name text NOT NULL, + price real NOT NULL, + quantity int + ) + """ + sql_sales_create_table = """ + CREATE TABLE IF NOT EXISTS sales ( + id integer PRIMARY KEY, + product_id integer, + quantity integer, + created_date text NOT NULL, + FOREIGN KEY (product_id) REFERENCES products(id) + + ) + """ + connection = models.create_connection(models.database_file()) + if connection is not None: + models.create_table(connection, sql_products_create_table) + models.create_table(connection, sql_sales_create_table) + + connection.close() + + +if __name__ == '__main__': + initialize() + products = models.Product.get_all_products() + while True: + print("Enter product information") + product = models.Product.create_product_from_userinput() + product.save() + choice = input('Enter n to stop and y to continue') + if choice == 'n': + break + + + + + + + + \ No newline at end of file diff --git a/inventory_withsqlite/models.py b/inventory_withsqlite/models.py new file mode 100644 index 0000000..2372f47 --- /dev/null +++ b/inventory_withsqlite/models.py @@ -0,0 +1,96 @@ +import datetime +import sqlite3 +from sqlite3 import Error +from dataclasses import dataclass +from sqlite3.dbapi2 import Cursor + +@dataclass +class Product: + """ + This class represents the product + """ + id: int + name: str + price: float + quantity: int + + @staticmethod + def create_product_from_userinput(): + """ + This method will create product from the user input + Returns: + Product + """ + id = int(input('Enter Id: ')) + name = input('Enter Product Name: ') + price = float(input('Enter product price: ')) + quantity = int(input('Enter quanity: ')) + return Product(id=id, name=name,price=price,quantity=quantity) + + + def save(self): + """ + This statement will insert the product into the database + """ + insert_statement = f"INSERT into products (id, name, price, quantity) VALUES({self.id}, '{self.name}', {self.price}, {self.quantity})" + with create_connection(database_file()) as connection: + cursor = connection.cursor() + cursor.execute(insert_statement) + connection.commit() + + @staticmethod + def get_all_products(): + select_query = "SELECT * from products" + products = list() + with create_connection(database_file()) as connection: + cursor = connection.cursor() + for row in cursor.execute(select_query): + product = Product(row[0],row[1], row[2], row[3]) + products.append(product) + return products + + + + + + +@dataclass +class Sales: + """ + This class represents sales + """ + id: int + pid: int + quantity: int + price: float + createddate: str + +def database_file(): + """ + This function will return the database file location + """ + return 'data/inventory.db' + +def create_connection(db_file) -> sqlite3.Connection: + """ + This method connects to the database and returns the connection + """ + connection = None + try: + connection = sqlite3.connect(db_file) + print(sqlite3.version) + except Error as e: + print(e) + return connection + + +def create_table(connection: sqlite3.Connection, create_table_sql: str): + """ + This method will execute the sql for creating tables + """ + try: + cursor = connection.cursor() + cursor.execute(create_table_sql) + except Error as e: + print(e) + diff --git a/inventory_withsqlite/sales1.py b/inventory_withsqlite/sales1.py new file mode 100644 index 0000000..5288bc9 --- /dev/null +++ b/inventory_withsqlite/sales1.py @@ -0,0 +1,208 @@ +import datetime +import sqlite3 +from sqlite3 import Error +from dataclasses import dataclass +from sqlite3.dbapi2 import Cursor + + +@dataclass +class Sales: + """ + This class represents sales + """ + id: int + product_id: int + quantity: int + #price: float + created_date: str + + def __str__(self) -> str: + return f"{self.id},{self.product_id},{self.quantity},{self.created_date}" + + def header() -> str: + return "id, product_id, quantity, created_date" + + @staticmethod + def create_salesdata_from_userinput(): + """ + This method will creates salesdata from the user input + Returns: + Sales + """ + id = int(input('Enter Id: ')) + #name = input('Enter Product Name: ') + product_id = int(input('Enter product Id: ')) + quantity = int(input('Enter quanity: ')) + date_time1=str(get_datetime()) + return Sales(id=id,product_id=product_id,quantity=quantity,created_date=date_time1) + + @staticmethod + def get_all_sales(): + select_query = "SELECT * from sales" + sales = list() + with create_connection(database_file()) as connection: + cursor = connection.cursor() + for row in cursor.execute(select_query): + sale = Sales(row[0],row[1], row[2], row[3]) + sales.append(sale) + return sales + + """@staticmethod + def get_all_sales_id1(): + select_query = "SELECT * from products" + products_id = list() + with create_connection(database_file()) as connection: + cursor = connection.cursor() + for row in cursor.execute(select_query): + product =row[0] # + products_id.append(product) + #products_id_set=set(products_id) + return products_id""" + + def sell_reduction(pquantity,quantity,id): + """ + this method will reduce the quantity of item sold in inventory + """ + update_statement=f'UPDATE products SET quantity={pquantity-quantity} where id={id}' + with create_connection(database_file()) as connection: + cursor=connection.cursor() + cursor.execute(update_statement) + connection.commit() + + def save(self): + """ + This statement will insert the product into the database + Raises: + sqlite3.IntegrityError as entered product id already exists + """ + try : + insert_statement = f"INSERT into sales (id, product_id, quantity, created_date) VALUES({self.id}, {self.product_id}, {self.quantity}, '{self.created_date}')" + #Sales.quantity.sell_reduction(quantity=self.quantity) + with create_connection(database_file()) as connection: + cursor = connection.cursor() + cursor.execute(insert_statement) + connection.commit() + except Error as e: + print(e) + #print(f"Input Unique Id For Product Other Than In Given Id List {Sales.get_all_sales_id1()}") #My code + + + def get_product_quantity(id): #self id need to provide + select_query = f"SELECT quantity from products where id={id} " + products_quantity = list() + with create_connection(database_file()) as connection: + cursor = connection.cursor() + for row in cursor.execute(select_query): + obj=row[0] + print(obj) + return obj + #product =row[0] # + #products_id.append(product) + #products_id_set=set(products_id) + #return products_id + + + + + +def get_datetime(): + now=datetime.datetime.now() + return now.strftime('%d/%m/%Y %H:%M:%S') + +def database_file(): + """ + This function will return the database file location + """ + return 'data/inventory.db' + +def create_connection(db_file) -> sqlite3.Connection: + """ + This method connects to the database and returns the connection + """ + connection = None + try: + connection = sqlite3.connect(db_file) + #print(sqlite3.version) + except Error as e: + print(e) + return connection + + +def create_table(connection: sqlite3.Connection, create_table_sql: str): + """ + This method will execute the sql for creating tables + """ + try: + cursor = connection.cursor() + cursor.execute(create_table_sql) + except Error as e: + print(e) + + + +@dataclass +class Product: + """ + This class represents the product + """ + id: int + name: str + price: float + quantity: int + + def __str__(self) -> str: + return f"{self.id},{self.name},{self.price},{self.quantity}" + + def header() -> str: + return "id, name, price, quantity" + + + + + @staticmethod + def create_product_from_userinput(): + """ + This method will create product from the user input + Returns: + Product + """ + id = int(input('Enter Id: ')) + name = input('Enter Product Name: ') + price = float(input('Enter product price: ')) + quantity = int(input('Enter quanity: ')) + return Product(id=id, name=name,price=price,quantity=quantity) + + + + def update(self): + """ + this method will impliment update data to database + """ + update_statement=f" update products set name={self.name}, price={self.price}, quantity={self.quantity} " + with create_connection(database_file()) as connection: + cursor=connection.cursor() + cursor.execute(update_statement) + connection.commit() + + @staticmethod + def get_all_products(): + select_query = "SELECT * from products" + products = list() + with create_connection(database_file()) as connection: + cursor = connection.cursor() + for row in cursor.execute(select_query): + product = Product(row[0],row[1], row[2], row[3]) + products.append(product) + return products + + @staticmethod + def get_all_products_id(): + select_query = "SELECT * from products" + products_id = list() + with create_connection(database_file()) as connection: + cursor = connection.cursor() + for row in cursor.execute(select_query): + product =row[0] # + products_id.append(product) + #products_id_set=set(products_id) + return products_id diff --git a/inventory_withsqlite/smain.py b/inventory_withsqlite/smain.py new file mode 100644 index 0000000..d76ddf7 --- /dev/null +++ b/inventory_withsqlite/smain.py @@ -0,0 +1,78 @@ +from models import Sales +import sqlite3 +from sqlite3.dbapi2 import Error +import sales1 + +def initialize(): + #sql_products_create_table = """ + #CREATE TABLE IF NOT EXISTS products ( + # id integer PRIMARY KEY, + # name text NOT NULL, + # price real NOT NULL, + # quantity int + #) + #""" + sql_sales_create_table = """ + CREATE TABLE IF NOT EXISTS sales ( + id integer PRIMARY KEY, + product_id integer, + quantity integer, + created_date text NOT NULL, + FOREIGN KEY (product_id) REFERENCES products(id) + + ) + """ + connection = sales1.create_connection(sales1.database_file()) + if connection is not None: + #models.create_table(connection, sql_products_create_table) + sales1.create_table(connection, sql_sales_create_table) + + connection.close() + +def insert_salesdata(sales,ids): + """ + this method insert the salesdata into to table + """ + print("Enter product information") + sales = sales1.Sales.create_salesdata_from_userinput() + sales.save() + """if sales.id in ids: + print(f"Change the id as existing a product with same id {sales.id}.") + else: + sales.save()""" + +def display_details(sales): + """ + this ethod prints headers or column or attributes and values + """ + print(sales1.Sales.header()) + for sale in sales: + print(str(sale)) + +if __name__ == '__main__': + initialize() + sales = sales1.Sales.get_all_sales() + ids=[salerow.id for salerow in sales] + while True: + try: + menu_choice=int(input("Enter 1 for sales insert \n2 for display sales " )) + if menu_choice==1: + sale = sales1.Sales.create_salesdata_from_userinput() + #if sale.id in ids: + #print(f"Change the id as existing a product with same id {sale.id}.") + #continue + sale.save() + squantity=sale.quantity + id1=sale.id + print(sales1.Sales.get_product_quantity(id=id1)) + sales1.Sales.sell_reduction(pquantity=sales1.Sales.get_product_quantity(id=id1),quantity=squantity,id=id1) + + #sales1.Sales.sell_reduction + elif menu_choice==2: + display_details(sales) + + except Error as e: + print(e) + choice = input('Enter n to stop and y to continue') + if choice == 'n': + break \ No newline at end of file diff --git a/utils.py b/utils.py index 863c7fa..1f67b74 100644 --- a/utils.py +++ b/utils.py @@ -1,2 +1,3 @@ def sum_of_prime_numbers(max): - pass \ No newline at end of file + numbers=list(range(2,max+1)) + return sum(numbers) \ No newline at end of file