Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Hans Christian S.

Purino

BIT34
using System;
using System.Windows.Forms;
using MongoDB.Driver;
using MongoDB.Bson;

namespace EA5_Purino
{
public partial class Form1 : Form
{
private const double DailyWages = 580;
private const double SSS = 500;
private const double Pagibig = 250;
private const double OTPayExtraHour = 150.00;

private readonly MongoClient _client;


private readonly IMongoDatabase _database;
private readonly IMongoCollection<BsonDocument> _collection;
public Form1()
{
InitializeComponent();
_client = new MongoClient("mongodb://localhost:27017");
_database = _client.GetDatabase("HansCompany");
_collection = _database.GetCollection<BsonDocument>("employees");
}

private void label10_Click(object sender, EventArgs e)


{
}
private string GenerateEmployeeId()
{

Random rnd = new Random();


return string.Join("", Enumerable.Range(0, 9).Select(_ =>
rnd.Next(10).ToString()));
}
private void btncompute_Click(object sender, EventArgs e)
{

try
{

string lastName = txtlastname.Text;


string firstName = txtfirstname.Text;
string middleInitial = txtmiddleInitial.Text;
int daysWorked = int.Parse(txtdays.Text);
int otHours = int.Parse(txtOtHours.Text);

string employeeId = GenerateEmployeeId();

lbllname.Text = lastName;
lblfname.Text = firstName;
lblmiddleinitial.Text = middleInitial;

lblempnum.Text = employeeId;

double basicSalary = daysWorked * DailyWages;


double deduction = SSS + Pagibig;
double overtimePay = otHours * OTPayExtraHour;
double grossSalary = basicSalary + overtimePay;
double netSalary = grossSalary - deduction;

txtempid.Text = employeeId;

txtbasicpay.Text = basicSalary.ToString("C", new


System.Globalization.CultureInfo("en-PH"));
txtOTpay.Text = overtimePay.ToString("C", new
System.Globalization.CultureInfo("en-PH"));
txtgross.Text = grossSalary.ToString("C", new
System.Globalization.CultureInfo("en-PH"));
txtNet.Text = netSalary.ToString("C", new
System.Globalization.CultureInfo("en-PH"));

BsonDocument employee = new BsonDocument


{
{ "employeeID", employeeId },
{ "LastName", lastName },
{ "FirstName", firstName },
{ "MiddleInitial", middleInitial },
{ "BasicSalary", basicSalary },
{ "TotalDeduction", deduction },
{ "OvertimePay", overtimePay },
{ "GrossSalary", grossSalary },
{ "NetSalary", netSalary },
{ "DaysWorked", daysWorked },
{ "OTHours", otHours }

};

var filter = Builders<BsonDocument>.Filter.Eq("LastName", lastName)


&
Builders<BsonDocument>.Filter.Eq("FirstName",
firstName);
var existingEmployee = _collection.Find(filter).FirstOrDefault();
if (existingEmployee != null)
{
_collection.ReplaceOne(filter, employee);
MessageBox.Show("Employee already exists in the database.");
}
else
{
_collection.InsertOne(employee);
}
}
catch (FormatException)
{
MessageBox.Show("Please enter valid numeric values for days worked
and OT hours.");
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}

private void btncheck_Click(object sender, EventArgs e)


{
try
{

string employeeId = txtempid.Text;

var filter = Builders<BsonDocument>.Filter.Eq("employeeID",


employeeId);
var existingEmployee = _collection.Find(filter).FirstOrDefault();

if (existingEmployee != null)
{
MessageBox.Show("Employee already exists in the database.");

}
else
{
MessageBox.Show("Employee does not exist in the database.");

txtlastname.Text = "";
txtfirstname.Text = "";
txtmiddleInitial.Text = "";
txtdays.Text = "";
txtOtHours.Text = "";
txtempid.Text = "";
txtbasicpay.Text = "";
txtOTpay.Text = "";
txtgross.Text = "";
txtNet.Text = "";

lbllname.Text = "";
lblfname.Text = "";
lblmiddleinitial.Text = "";
lblempnum.Text="";
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}
}

private void btnupdate_Click(object sender, EventArgs e)


{
try
{

string lastName = txtlastname.Text;


string firstName = txtfirstname.Text;
string middleInitial = txtmiddleInitial.Text;
int daysWorked = int.Parse(txtdays.Text);
int otHours = int.Parse(txtOtHours.Text);

lbllname.Text = lastName;
lblfname.Text = firstName;
lblmiddleinitial.Text = middleInitial;

double basicSalary = daysWorked * DailyWages;


double deduction = SSS + Pagibig;
double overtimePay = otHours * OTPayExtraHour;
double grossSalary = basicSalary + overtimePay;
double netSalary = grossSalary - deduction;

string employeeId = txtempid.Text;

var filter = Builders<BsonDocument>.Filter.Eq("employeeID",


employeeId);
var update = Builders<BsonDocument>.Update
.Set("LastName", lastName)
.Set("FirstName", firstName)
.Set("MiddleInitial", middleInitial)
.Set("DaysWorked", daysWorked)
.Set("OTHours", otHours)
.Set("BasicSalary", basicSalary)
.Set("TotalDeduction", deduction)
.Set("OvertimePay", overtimePay)
.Set("GrossSalary", grossSalary)
.Set("NetSalary", netSalary);

_collection.UpdateOne(filter, update);

txtempid.Text = employeeId;
txtlastname.Text = lastName;
txtfirstname.Text = firstName;
txtmiddleInitial.Text = middleInitial;
txtdays.Text = daysWorked.ToString();
txtOtHours.Text = otHours.ToString();
txtbasicpay.Text = basicSalary.ToString("C", new
System.Globalization.CultureInfo("en-PH"));
txtOTpay.Text = overtimePay.ToString("C", new
System.Globalization.CultureInfo("en-PH"));
txtgross.Text = grossSalary.ToString("C", new
System.Globalization.CultureInfo("en-PH"));
txtNet.Text = netSalary.ToString("C", new
System.Globalization.CultureInfo("en-PH"));

MessageBox.Show("Employee data updated successfully.");


}
catch (FormatException)
{
MessageBox.Show("Please enter valid numeric values for days worked
and OT hours.");
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}
}
}

You might also like