using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.Properties; using Point_Of_Sale_Managment; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BiskLog_Point_Of_Sale { public partial class AddCustomer : Form { Customers customersList; SqlConnection cn; SqlCommand cm; DatabaseConn conn = new DatabaseConn(); public AddCustomer(Customers customers, bool update = false, List id = null) { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); customersList = customers; if (update) { title.Text = "Edit Customer Details"; btnUpdate.Left = (ClientSize.Width - btnUpdate.Width) / 2; btnUpdate.Visible = true; btnNew.Visible = false; lblCustomerID.Text = id[0]; lblCustomerID.Visible = true; lblID.Visible = true; txtFirstname.Text = id[1]; txtSurname.Text = id[2]; txtAddress.Text = id[3]; txtEmail.Text = id[4]; txtPhone.Text = id[5]; txtTIN.Text = id[6]; } else { title.Text = "New Customer"; btnNew.Left = (ClientSize.Width - btnNew.Width) / 2; btnNew.Visible = true; } } private void Exit_Click(object sender, EventArgs e) { this.Close(); } private void Exit_MouseEnter(object sender, EventArgs e) { exit.BackColor = Color.Crimson; } private void Exit_MouseLeave(object sender, EventArgs e) { exit.BackColor = Color.FromArgb(20, 158, 132); } public int checkEmptyBox() { int total = 0; foreach (Control c in panel2.Controls) { if ((c is TextBox) && (!String.IsNullOrEmpty(c.Text))) { total++; } } if (String.IsNullOrEmpty(txtEmail.Text)) { total++; } return total; } public int SaveDetails() { try { cn.Open(); lblCustomerID.Invoke(new Action(() => { string number = String.Format("{0:d9}", (DateTime.Now.Ticks / 10) % 1000000000); lblCustomerID.Text = (txtFirstname.Text.Substring(0, 2) + number).ToUpper(); ; cm = new SqlCommand("Insert into tblCustomers (customerID,Firstname,Surname,address,email,telephone,dateAdded,branchID,status,tin,financialStatus,name_key1,name_key2) " + "values (@customerID,@Firstname,@Surname,@address,@email,@telephone,@dateAdded,@branchID,@status,@tin,@financialStatus,@name_key1,@name_key2)", cn); cm.Parameters.AddWithValue("@customerID", lblCustomerID.Text); cm.Parameters.AddWithValue("@Firstname", txtFirstname.Text); cm.Parameters.AddWithValue("@Surname", txtSurname.Text); cm.Parameters.AddWithValue("@address", txtAddress.Text); cm.Parameters.AddWithValue("@telephone", txtPhone.Text); cm.Parameters.AddWithValue("@dateAdded", DateTime.Now); cm.Parameters.AddWithValue("@email", txtEmail.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@status", "ACTIVE"); cm.Parameters.AddWithValue("@tin", txtTIN.Text); cm.Parameters.AddWithValue("@financialStatus", "not owing"); cm.Parameters.AddWithValue("@name_key1", txtFirstname.Text + " " + txtSurname.Text); cm.Parameters.AddWithValue("@name_key2", txtSurname.Text + " " + txtFirstname.Text); cm.ExecuteNonQuery(); })); cn.Close(); return 1; } catch { cn.Close(); return 0; } } public void lockBoxes() { foreach (Control box in panel2.Controls) { if (box is TextBox) { box.Enabled = false; } } } public void unlockBoxes() { foreach (Control box in panel2.Controls) { if (box is TextBox) { box.Enabled = true; } } } public void clearBoxes() { foreach (Control box in panel2.Controls) { if (box is TextBox) { box.Text = ""; } } } private async void BtnNew_Click(object sender, EventArgs e) { int checkedTotal = checkEmptyBox(); if (checkedTotal == 6) { Task task = new Task(SaveDetails); holding.Visible = true; lockBoxes(); btnNew.Enabled = false; task.Start(); int result = await task; if (result == 1) { string heading = "Registration Successful"; string body = "The new customer has been addded successfully to the system."; NoAction noAction = new NoAction(title: heading, message: body); noAction.BringToFront(); noAction.ShowDialog(); clearBoxes(); customersList.loadCustomerOutNOut(); } else { string heading = "Registration Failed"; string body = "An error occurred while adding the new customer to the system, please try again later."; NoAction noAction = new NoAction(title: heading, message: body); noAction.BringToFront(); noAction.ShowDialog(); } holding.Visible = false; btnNew.Enabled = true; unlockBoxes(); } else { string heading = "Operation Denied"; string body = "You must fill out all spaces to continue!!"; NoAction noAction = new NoAction(title: heading, message: body); noAction.BringToFront(); noAction.ShowDialog(); } } public int UpdateCustomers() { try { cn.Open(); lblCustomerID.Invoke(new Action(() => { cm = new SqlCommand("Update tblCustomers set Firstname = @firstname, Surname = @surname,address = @address, tin = @tin," + "telephone = @phone,email = @email where customerID = @customerNumber", cn); cm.Parameters.AddWithValue("@firstname", txtFirstname.Text); cm.Parameters.AddWithValue("@surname", txtSurname.Text); cm.Parameters.AddWithValue("@address", txtAddress.Text); cm.Parameters.AddWithValue("@tin", txtTIN.Text); cm.Parameters.AddWithValue("@phone", txtPhone.Text); cm.Parameters.AddWithValue("@email", txtEmail.Text); cm.Parameters.AddWithValue("@customerNumber", lblCustomerID.Text); cm.ExecuteNonQuery(); })); cn.Close(); return 1; } catch { cn.Close(); return 0; } } private async void BtnUpdate_Click(object sender, EventArgs e) { int checkedTotal = checkEmptyBox(); if (checkedTotal == 6) { Task task = new Task(UpdateCustomers); holding.Visible = true; lockBoxes(); btnUpdate.Enabled = false; task.Start(); int result = await task; if (result == 1) { string heading = "Update Successful"; string body = "Customer details has been updated successfully...."; NoAction noAction = new NoAction(title: heading, message: body); noAction.BringToFront(); noAction.ShowDialog(); customersList.loadCustomerOutNOut(); } else { string heading = "Update Failed"; string body = "An error occurred while updating customer details , please try again later."; NoAction noAction = new NoAction(title: heading, message: body); noAction.BringToFront(); noAction.ShowDialog(); } holding.Visible = false; btnUpdate.Enabled = true; unlockBoxes(); } else { string heading = "Operation Denied"; string body = "You must fill out all spaces to continue!!"; NoAction noAction = new NoAction(title: heading, message: body); noAction.BringToFront(); noAction.ShowDialog(); } } } }