Exemplo n.º 1
0
def openManHinhThemHangHoa(mainScreen):
    def ham_xu_ly_them_hang_hoa(cboLoai):
        print(type(cboLoai))
        print("Loai dang chon: ", cboLoai.get())

    mhThemHangHoa = Toplevel(mainScreen)
    common.setWidgetCenter(mhThemHangHoa, 350, 300)

    mhThemHangHoa.title("Thêm hàng hóa")

    Label(mhThemHangHoa, text="Mã hàng hóa").grid(row=0, column=0)
    mahh = StringVar()
    Entry(mhThemHangHoa, textvariable=mahh).grid(row=0, column=1)

    Label(mhThemHangHoa, text="Tên hàng hóa").grid(row=1, column=0)
    tenhh = StringVar()
    Entry(mhThemHangHoa, textvariable=tenhh).grid(row=1, column=1)

    Label(mhThemHangHoa, text="Mô tả").grid(row=2, column=0)
    mota = StringVar()
    Entry(mhThemHangHoa, textvariable=mota).grid(row=2, column=1)
    # Text(mhThemHangHoa, height=5, textva =mota).grid(row=2, column=1)

    Label(mhThemHangHoa, text="Đơn giá").grid(row=3, column=0)
    dongia = IntVar()
    Entry(mhThemHangHoa, textvariable=dongia).grid(row=3, column=1)

    Label(mhThemHangHoa, text="SKU").grid(row=4, column=0)
    sku = StringVar()
    Entry(mhThemHangHoa, textvariable=sku).grid(row=4, column=1)

    # ComboBox Thành phố
    maloai = StringVar
    dsLoai = []
    sql = f"SELECT * FROM Loai ORDER BY TenLoai"
    conn = dbcommon.sql_connection(db_name)
    result = dbcommon.query_data(conn, sql)
    conn.close()
    for loai in result:
        dsLoai.append((loai[0], loai[1]))
    Label(mhThemHangHoa, text="Loại").grid(row=5, column=0)
    cboLoai = ttk.Combobox(mhThemHangHoa, values=dsLoai).grid(row=5, column=1)
    processInsertpro = partial(ham_xu_ly_them_hang_hoa, cboLoai)

    Button(mhThemHangHoa, text="Thêm hàng hóa",
           command=processInsertpro).grid(row=6, column=1)

    mhThemHangHoa.mainloop()
Exemplo n.º 2
0
 def xu_ly_them_loai(ten_loai):
     try:
         sql = f"SELECT * FROM Loai WHERE TenLoai='{ten_loai.get()}'"
         conn = dbcommon.sql_connection(db_name)
         result = dbcommon.query_data(conn, sql)
         print(result)
         if len(result) > 0:
             messagebox.showwarning(title="Thông báo",
                                    message=f"Đã có loại {ten_loai.get()}")
         else:
             sqlInsert = f"INSERT INTO Loai(TenLoai) VALUES('{ten_loai.get()}')"
             idloai = dbcommon.insert_and_get_inserted_id(conn, sqlInsert)
             message=f"Đã thêm loại ({idloai}, {ten_loai.get()})."\
                 + "\n Tiếp tục không?"
             traloi = messagebox.askyesno("Hỏi", message)
             if traloi == True:
                 ten_loai.set("")
             else:
                 mhThemLoai.destroy()
     except Exception as e:
         print(e)
     finally:
         if conn:
             conn.close()
Exemplo n.º 3
0

def them_moi_loai(connection, ten_loai):
    sql = f"INSERT INTO Loai(TenLoai) VALUES('{ten_loai}')"
    cursor = connection.cursor()
    cursor.execute(sql)
    connection.commit()
    return cursor.lastrowid  # Trả về id vừa tăng


def them_hang_hoa(connection, hang_hoa_obj):
    sql = """
        INSERT INTO HangHoa(MaHH,TenHH,MoTa,DonGia,SKU,MaLoai)
        VALUES(?,?,?,?,?,?) 
    """
    cursor = connection.cursor()
    cursor.execute(sql, hang_hoa_obj)
    connection.commit()


conn = dbcommon.sql_connection(database_name=DbName)
ma_loai = them_moi_loai(conn, "Điện tử")
print("Vừa thêm loại có mã: ", ma_loai)
hh1 = ('HH0001', 'Sony 32s1', 'Tivi Sony 32" Full HD', 11909, 'SKU0001',
       ma_loai)
hh2 = ('HH0002', 'Daikin 123', 'Máy lạnh Daikin 1HP Inverter', 12999,
       'SKU0010', ma_loai)
them_hang_hoa(conn, hh1)
them_hang_hoa(conn, hh2)
conn.close()
Exemplo n.º 4
0
# qlhanghoa_1_tao_bang.py
import dbcommon

DbName = "QLHangHoa.db"
sql_create_table_loai = """
    CREATE TABLE Loai(
        MaLoai integer PRIMARY KEY,
        TenLoai varchar(40)
    )        
    """
sql_create_table_hang_hoa = """
    CREATE TABLE HangHoa(
        MaHH char(6) PRIMARY KEY,
        TenHH varchar(40), 
        MoTa varchar(55),
        DonGia decimal(10,2),
        SKU varchar(15) NULL,
        MaLoai interger NULL,
        FOREIGN KEY (MaLoai) REFERENCES Loai(MaLoai)
    )        
    """

connection = dbcommon.sql_connection(DbName)
dbcommon.create_table(connection, sql_create_table_loai)
dbcommon.create_table(connection, sql_create_table_hang_hoa)

if (connection):
    connection.close()