0% found this document useful (0 votes)
1 views

Payroll program

Nil

Uploaded by

Nivedha S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Payroll program

Nil

Uploaded by

Nivedha S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

-- Create a new database

CREATE DATABASE payroll_system;

-- Use the new database


USE payroll_system;

-- Create the 'emp' table to store employee details


CREATE TABLE emp (
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(50),
age INT,
address VARCHAR(100),
dob DATE,
phone_number VARCHAR(15)
);

-- Create the 'salary' table to store salary details


CREATE TABLE salary (
emp_id INT,
designation VARCHAR(50),
basic_salary FLOAT,
da FLOAT,
hra FLOAT,
pf FLOAT,
net_salary FLOAT,
FOREIGN KEY (emp_id) REFERENCES emp(emp_id)
);
```

Python Code

import mysql.connector
import tkinter as tk
from tkinter import messagebox

# Function to connect to MySQL database


def connect_to_db():
return mysql.connector.connect(
host="localhost", # Replace with your host
user="root", # Replace with your MySQL username
password="password123", # Replace with your MySQL password
database="payroll_system"
)
# Form1: Main Menu
def main_menu():
root = tk.Tk()
root.title("Payroll System")

# Button to open Employee form


add_employee_btn = tk.Button(root, text="Add Employee", width=20,
command=open_employee_form)
add_employee_btn.pack(pady=10)

# Button to open Salary form


add_salary_btn = tk.Button(root, text="Add Salary", width=20, command=open_salary_form)
add_salary_btn.pack(pady=10)

# Button to exit the application


exit_btn = tk.Button(root, text="Exit", width=20, command=root.quit)
exit_btn.pack(pady=10)

root.mainloop()

# Form2: Employee Management


def open_employee_form():
employee_form = tk.Toplevel()
employee_form.title("Employee Form")

# Labels and Entries


tk.Label(employee_form, text="Employee Name:").grid(row=0, column=0)
tk.Label(employee_form, text="Age:").grid(row=1, column=0)
tk.Label(employee_form, text="Address:").grid(row=2, column=0)
tk.Label(employee_form, text="DOB (YYYY-MM-DD):").grid(row=3, column=0)
tk.Label(employee_form, text="Phone Number:").grid(row=4, column=0)

emp_name_entry = tk.Entry(employee_form)
emp_name_entry.grid(row=0, column=1)
emp_age_entry = tk.Entry(employee_form)
emp_age_entry.grid(row=1, column=1)
emp_address_entry = tk.Entry(employee_form)
emp_address_entry.grid(row=2, column=1)
emp_dob_entry = tk.Entry(employee_form)
emp_dob_entry.grid(row=3, column=1)
emp_phone_entry = tk.Entry(employee_form)
emp_phone_entry.grid(row=4, column=1)

def add_employee():
emp_name = emp_name_entry.get()
emp_age = emp_age_entry.get()
emp_address = emp_address_entry.get()
emp_dob = emp_dob_entry.get()
emp_phone = emp_phone_entry.get()

if emp_name and emp_age and emp_address and emp_dob and emp_phone:


try:
conn = connect_to_db()
cursor = conn.cursor()
cursor.execute("INSERT INTO emp (emp_name, age, address, dob, phone_number)
VALUES (%s, %s, %s, %s, %s)",
(emp_name, emp_age, emp_address, emp_dob, emp_phone))
conn.commit()
messagebox.showinfo("Success", "Employee Added Successfully!")
conn.close()
except mysql.connector.Error as err:
messagebox.showerror("Error", f"Error: {err}")
else:
messagebox.showerror("Error", "Please fill all fields!")

# Add Employee Button


add_employee_btn = tk.Button(employee_form, text="Add Employee",
command=add_employee)
add_employee_btn.grid(row=5, column=0, columnspan=2)

employee_form.mainloop()

# Form3: Salary Management


def open_salary_form():
salary_form = tk.Toplevel()
salary_form.title("Salary Form")

# Labels and Entries


tk.Label(salary_form, text="Employee ID:").grid(row=0, column=0)
tk.Label(salary_form, text="Designation:").grid(row=1, column=0)
tk.Label(salary_form, text="Basic Salary:").grid(row=2, column=0)

emp_id_entry = tk.Entry(salary_form)
emp_id_entry.grid(row=0, column=1)
emp_designation_entry = tk.Entry(salary_form)
emp_designation_entry.grid(row=1, column=1)
basic_salary_entry = tk.Entry(salary_form)
basic_salary_entry.grid(row=2, column=1)
def calculate_salary():
try:
basic_salary = float(basic_salary_entry.get())
da = basic_salary * 0.07 # DA = 7% of Basic Salary
hra = basic_salary * 0.10 # HRA = 10% of Basic Salary
pf = basic_salary * 0.05 # PF = 5% of Basic Salary
net_salary = basic_salary + da + hra - pf # Net Salary calculation

return da, hra, pf, net_salary


except ValueError:
messagebox.showerror("Error", "Please enter valid numeric values for salary!")

def add_salary():
emp_id = emp_id_entry.get()
emp_designation = emp_designation_entry.get()

if emp_id and emp_designation:


da, hra, pf, net_salary = calculate_salary()

try:
conn = connect_to_db()
cursor = conn.cursor()
cursor.execute("INSERT INTO salary (emp_id, designation, basic_salary, da, hra, pf,
net_salary) VALUES (%s, %s, %s, %s, %s, %s, %s)",
(emp_id, emp_designation, float(basic_salary_entry.get()), da, hra, pf,
net_salary))
conn.commit()
messagebox.showinfo("Success", "Salary Added Successfully!")
conn.close()
except mysql.connector.Error as err:
messagebox.showerror("Error", f"Error: {err}")
else:
messagebox.showerror("Error", "Please fill all fields!")

# Add Salary Button


add_salary_btn = tk.Button(salary_form, text="Add Salary", command=add_salary)
add_salary_btn.grid(row=3, column=0, columnspan=2)

salary_form.mainloop()

# Start the application


if __name__ == "__main__":
main_menu()

You might also like