# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'order1.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
import mysql.connector as conn
mydb = conn.connect(host="localhost",user="root",password="root",database="shop")
mycur=mydb.cursor(buffered=True)


class Ui_MainWindow(object):
    def __init__(self,cname='nishant',cid=10):
        super().__init__()
        self.cname=cname
        self.cid=cid
        
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(97, 255, 160))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(240, 48, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(97, 255, 160))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(240, 48, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(240, 48, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(240, 48, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        MainWindow.setPalette(palette)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.submit_details = QtWidgets.QPushButton(self.centralwidget)
        self.submit_details.setGeometry(QtCore.QRect(310, 350, 181, 101))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(25, 177, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(199, 188, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(25, 177, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(199, 188, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(25, 177, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
        self.submit_details.setPalette(palette)
        self.submit_details.setFlat(False)
        self.submit_details.setObjectName("submit_details")
        self.submit_details.clicked.connect(self.sd)
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(30, 70, 761, 161))
        self.widget.setObjectName("widget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(24)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.get_product_id = QtWidgets.QSpinBox(self.widget)
        self.get_product_id.setMinimumSize(QtCore.QSize(0, 25))
        self.get_product_id.setAutoFillBackground(True)
        self.get_product_id.setObjectName("get_product_id")
        self.horizontalLayout.addWidget(self.get_product_id)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(22)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.get_product_quantity = QtWidgets.QSpinBox(self.widget)
        self.get_product_quantity.setObjectName("get_product_quantity")
        self.horizontalLayout_2.addWidget(self.get_product_quantity)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        
        
        self.get_product_id.setMaximum(2)
        self.get_product_id.setMinimum(1)
        self.get_product_quantity.setMinimum(0)
        self.get_product_quantity.setMaximum(10)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.submit_details.setText(_translate("MainWindow", "Submit"))
        self.label.setText(_translate("MainWindow", "Enter product id :-"))
        self.label_2.setText(_translate("MainWindow", "Enter Quantity :-"))


    def sd(self):
        pid1=self.get_product_id.value()
        pq1=self.get_product_quantity.value()
        
        mycur.execute("select pid from orders where customerid = {} ".format(self.cid))
        b=mycur.fetchall()
        loki=[]
        for c in b:
            loki.append(int(c[0]))
            
        if int(pid1) in loki:
            mycur.execute("select stock from product where productid={}".format(int(pid1)))
            alma=mycur.fetchone()
            
            cvx=alma[0]
            print(cvx)
            if cvx<int(pq1):
                print("warning out of Stock")
                 
                 
            
            
            else:
                mycur.execute("select quantity from orders where pid={}".format(int(pid1)))
                qw=mycur.fetchone()
                asd=qw[0]
                new1=int(pq1)+int(asd)
                mycur.execute("update orders set quantity={} where pid={}".format(int(new1),int(pid1)))
                mycur.execute("select stock from product where productid={}".format(int(pid1)))
                tree=mycur.fetchone()
                llk=tree[0]
                mnop=llk-pq1
                mycur.execute("update product set stock = {} where productid = {}".format(mnop,int(pid1)))
                
                
                        
                
                
                
                mydb.commit()
        
        else:
            mycur.execute("select stock from product where productid={}".format(int(pid1)))
            alma=mycur.fetchone()
            
            cvx=alma[0]
            if cvx<pq1:
                print("warning out of stock")
            
            else:
        
                mycur.execute("insert into orders values({},'{}',{},{})".format(int(self.cid),self.cname,int(pid1),int(pq1)))
                
                mycur.execute("select stock from product where productid={}".format(int(pid1)))
                opi=mycur.fetchone()
                op2=opi[0]
                
                op3=op2-pq1
                mycur.execute("update product set stock = {} where productid = {}".format(op3,int(pid1)))
                mydb.commit()
       


        

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
