From b0aed8053b2becab01e74f186a2c4f1285716d57 Mon Sep 17 00:00:00 2001 From: vrushabhendra661 Date: Wed, 18 Aug 2021 17:07:42 +0530 Subject: [PATCH] added iventory application with sqlite --- .../__pycache__/models.cpython-39.pyc | Bin 0 -> 4412 bytes .../__pycache__/sales1.cpython-39.pyc | Bin 0 -> 6389 bytes inventory_withsqlite/data/inventory.db | Bin 0 -> 12288 bytes inventory_withsqlite/main.py | 48 ++++ inventory_withsqlite/models.py | 96 ++++++++ inventory_withsqlite/sales1.py | 208 ++++++++++++++++++ inventory_withsqlite/smain.py | 78 +++++++ utils.py | 3 +- 8 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 inventory_withsqlite/__pycache__/models.cpython-39.pyc create mode 100644 inventory_withsqlite/__pycache__/sales1.cpython-39.pyc create mode 100644 inventory_withsqlite/data/inventory.db create mode 100644 inventory_withsqlite/main.py create mode 100644 inventory_withsqlite/models.py create mode 100644 inventory_withsqlite/sales1.py create mode 100644 inventory_withsqlite/smain.py diff --git a/inventory_withsqlite/__pycache__/models.cpython-39.pyc b/inventory_withsqlite/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4b134d85215ee37aab15c9d72a9c3a932d58e361 GIT binary patch literal 4412 zcmaJ^U31&U72O3u5~4_ojz1<&8h4vEiRr3}?Q}Yg#?H7_6i2Pgdi;?z9A!|53t7;B zAiV%>3nl$fdHUAr4@f*Cy=2DInLhQ`;I-2S_b2$N=Pp2yl%sCZZ~kF2sh=mp^EydOQU9gCT?!viv9xQ zF2dRp_PJUh$_}$;}BcXR8ls8abLbvCorGfIhUTQxYx?$|cdoQth<;6o4b~*Mq`BA-sYYkWQKFo;i zvhT2QlsqquyfnXKFDh7^(SFUBUs|e+;ffwsHkkz&Kx{=`u>F!LP=>w!YI}8acuX>-vTum3Fot*dOvLM8k zfg})H78Atu4vv~89!<+To+~|d2KCA0jmhL$*WBDR9bu*CzJ^N7VlMeU{$r|v`ePm& zsF}LW(EXOK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u`SawEZ5}8`#+Vl99Z75;k~yyVXo>$DwS< zanh>eAmJJYRIfR3n;Yak$i8I1>p1E(rb(9^#|^_iWD9y3VXz54sI%x&l%6RPCODt^ zyV+4!=cxUCGCw7=LPn68ZbjF~yE;LjEHkQqJ$BB? zc!UvAZ{UhnVNOWyDVe9_o=QsYDcPswo|66YH6|(VwbRl)H}In51Fb>Fr{och(EU2X zbj$&~Myat*k~@o8gps6eq(mQ6&t)=K$Xq2usH#uMXb^u%wc~*K6*|7f71dy7fZ>XI z-hhEl?ivlBroYK&)17P&Lmlz5j3N6}m`e}@1waw>yV}wE!Q>Rl^2bH8z9P4du5{{0 z64)J@-+*COCb*P)%|#`0R*{f>jvJgxo-|^1fKQS`)W2roGLj}^?=z%K__|EaAVr=g zNPKKOGms$Zk`g4XYDTg#-Dw_g^5}}MsQE-lxS_;H7JVvZUs%&Htl!Z_Sa=T$ODlPY zq7A7Z!?es!MQ-U#L7&u=doy@wHdCw#acQS9%i;!a7^GWT2U8HjC9~gNe$;ak1Q{#kECz7Q8#w;Ud z<8q$vXhL8)VjG#3UZWpA6nd2z=?R=^5S;>JWy-Dgj)8sLB~VWFRiW>nO>&D n`moV7Gixg-oZ8niMFjP*I8rfEBKr2I83u1Yz*V)&&HDcUcoz7I literal 0 HcmV?d00001 diff --git a/inventory_withsqlite/__pycache__/sales1.cpython-39.pyc b/inventory_withsqlite/__pycache__/sales1.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..37ab680a8be61e45816081519d223ced11c738f0 GIT binary patch literal 6389 zcmc&&OLN@D5yk*m>~gtWQ4+04c5F``w6roqSx)7}SdJ{2w&ceaV@h_s#T2sK0nJtI zO9N0cQA=H@YvOZksfx-WRXXOJdwvA2Il1x=c*@s30QNyDvQJqOjlp1MF#YuReESON@_-xai0mqowAyJBveJsJ#-th^c+-F^^vVk zsCo3v@fs)9DXejd*QhG7Zq-hIi#=KOnn(((TK6>?GpZRiTb*VY;=#MVqr<*#(WvpC zgNLiQ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z>XU zAq#1@3~1_?sQ)67Wg>49`2~>?C~=FBiQ^BcKl~v`*>)Y`pk$Zqs#A4rQJ%wfrc^GS z;*l*3%6}9DDP%An(LN$1u?GUyfpqr3R)w#kufdAk%z2DnUi>YB(x;634c zB)uHb-^By69T8U}NRhIdJ&~fWz56y$7sVY=*BK&PO;K0u*$-@>F5d}tIdie&-{+fV zj?-Y8W)Y#BLRi`CDO5RBq~IMA)ls^UuT!tNNijv*q?it1%+l;*PEu5xoZ=XNx(sH< zq{A|i=(~q2tbkaqD{N7lWKHl3e6mHznEgntxx@(U;xn){3pPiQ zO_+$5OYIt@=A=?XUY|?NdEl_raOYUXb~;?d_d_c1h06Kqc1szQ@-w{T7T$d0b!3S*FG6w*{UJ1Crs zxKklbh4pkHU79GQX964tq=odXDWe+|^wD7e71wiUb198>Rh@>fFC@h^QxeCov+1z; z9Vw&9#!_J9QH;-tOVGy=uoajr>xjP);!wOIA$-TOS(CA4Rr4y^`aL29dHMvADaR#H z{VOixxFSB0GW7il+;fyW5IePTF@4_p!MSI+|kDYW2~Uf^!0tK*_4NTdHn)58!womHGL`T z5ERPW7fkQKSdbAM3R)`i_4^?3D~g8tEJ#w=>hznDCeY-UQ+iBZPGKZnZK_)R8>VH| zDm;to)p#-8&hr19If37VMr28Bk6UOQ$;T-Vr+nOfT!cb!mN1rw7l2w+!FqWfUOYAr z|HDL@r-vxS6OkYe}{(M%{ zXA}R4(Z`4$9euG+h)~n+a literal 0 HcmV?d00001 diff --git a/inventory_withsqlite/data/inventory.db b/inventory_withsqlite/data/inventory.db new file mode 100644 index 0000000000000000000000000000000000000000..0ef694195e56faad6b96144bf9374b8e6dde778d GIT binary patch literal 12288 zcmeI%O-jQ+6bJB`L@5-suFR_NHev%JR4-sHQz?z9rWNd}#Aeh$zf7l~%jyL@l85jR z9>YmUnhJK|((->~KA0IY^M1QYI@eC9B;8M^T84+X5Op9)kFm%0@_Tz@H z^w)JwcHDa3(X+nip=o9aZveZ1O}yc*3oV4yD7hN*LJ7 zBNgq%0WVnPGJh>6xn`@f*7%WeHu{I8$Np&Tv8Pcukfie){*u$G2o>%4Zq7?zOg{(+ zKmY;|fB*y_009U<00Izz00jQIz&_t*hb9lFkCU;a>Ph_Ff`5Jl@%>-_^hZDd0uX=z U1Rwwb2tWV=5P$##Ah5v#-{XjafdBvi literal 0 HcmV?d00001 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