using BiskLog_Point_Of_Sale.Cashier_Module; using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Company_Setup; using BiskLog_Point_Of_Sale.Customer; using BiskLog_Point_Of_Sale.Delivery; using BiskLog_Point_Of_Sale.POSDialogs; 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 Customers : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; SqlTransaction tn; DatabaseConn conn = new DatabaseConn(); string firstnameC, surnameC, selectedID, currency; Form1 form; CashierMain cashier; decimal figure = 0; public Customers(Form1 form = null, CashierMain cashier = null) { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); holding.Left = (ClientSize.Width - holding.Width) / 2; panelHo.Left = (panel14.Width - panelHo.Width) / 2; panelHo.Top = (panel14.Height - panelHo.Height) / 2; currency = Settings.Default.currrencyCode + " "; if (form != null) { this.form = form; } else { this.cashier = cashier; } } private void Button1_Click(object sender, EventArgs e) { AddCustomer addCustomer = new AddCustomer(this); addCustomer.BringToFront(); addCustomer.ShowDialog(); } private void BtnEdit_Click(object sender, EventArgs e) { List details = new List(); details.Add(lblCustomerID.Text); details.Add(firstnameC); details.Add(surnameC); details.Add(customerAddress.Text); details.Add(customerEmail.Text); details.Add(customerPhone.Text); details.Add(lbltaxID.Text); AddCustomer addCustomer = new AddCustomer(this, true, details); addCustomer.BringToFront(); addCustomer.ShowDialog(); } public async void loadCustomerOutNOut() { Task task = new Task(loadCustomers); holding.Visible = true; task.Start(); await task; holding.Visible = false; } public int loadCustomers() { try { int i = 1; dataGridView1.Invoke(new Action(() => dataGridView1.Rows.Clear())); cn.Open(); cm = new SqlCommand("Select firstname,surname,address,telephone,tin,customerID,email from tblCustomers where status = @status and branchID = @branchID" + " and (Firstname like '%" + searchBox.Text + "%' or Surname like '%" + searchBox.Text + "%' or customerID like '%" + searchBox.Text + "%')", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@status", "ACTIVE"); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { dataGridView1.Invoke(new Action(() => dataGridView1.Rows.Add(i, dr[0].ToString().ToUpper() + " " + dr[1].ToString().ToUpper(), dr[2].ToString().ToUpper(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString(), dr[0].ToString().ToUpper(), dr[1].ToString().ToUpper()))); i++; } dr.Close(); cn.Close(); dataGridView1.Invoke(new Action(() => dataGridView1.ClearSelection())); return 1; } catch { cn.Close(); return 0; } } public int loadBal() { try { balText.Invoke(new Action(() => { cn.Open(); cm = new SqlCommand("Select top(1) balance from CustomerAccounts where branchID = @branchID and customerID = @customer " + "order by date desc", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@customer", selectedID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows) { figure = decimal.Parse(dr[0].ToString()); balText.Text = "Account Balance : " + currency + figure; if (figure < 0) { balText.ForeColor = Color.Crimson; } else { balText.ForeColor = Color.DarkGreen; } } else { balText.Text = "Account Balance : " + currency + "00.00"; balText.ForeColor = Color.DarkGreen; figure = 0; } dr.Close(); cn.Close(); })); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); return 0; } } private async void Customers_Load(object sender, EventArgs e) { Task task = new Task(loadCustomers); holding.Visible = true; task.Start(); await task; holding.Visible = false; } private async void SearchBox_TextChanged(object sender, EventArgs e) { Task task = new Task(loadCustomers); holding.Visible = true; task.Start(); await task; holding.Visible = false; } private void Sell_Click(object sender, EventArgs e) { CustomerClass customer = new CustomerClass(); customer.firstname = lblName.Text; customer.id = lblCustomerID.Text; customer.address = customerAddress.Text; customer.email = customerEmail.Text; customer.telephone = customerPhone.Text; if (form != null) { form.MoveTOPOS(customer); } else { cashier.POS(customer: customer); } } private async void magicButton1_Click(object sender, EventArgs e) { AccountPayment accountPayment = new AccountPayment(figure); accountPayment.BringToFront(); accountPayment.ShowDialog(); if (accountPayment.DialogResult == DialogResult.OK && accountPayment.deposit) { decimal deposit = accountPayment.payment; decimal balance = figure; Task task = new Task(() => beginDeposit(deposit, balance)); task.Start(); int result = await task; if (result == 1) { Task task1 = new Task(loadBal); task1.Start(); await task1; } }else if(accountPayment.DialogResult == DialogResult.OK && accountPayment.withdrawal) { decimal withdraw = accountPayment.payment; Task task = new Task(() => beginWithdrawal(withdraw)); task.Start(); int result = await task; if (result == 1) { Task task1 = new Task(loadBal); task1.Start(); await task1; } } } private void customerHistory_Click(object sender, EventArgs e) { CustomerHistory customer = new CustomerHistory(lblCustomerID.Text); customer.BringToFront(); customer.ShowDialog(); } private void schDelivery_Click(object sender, EventArgs e) { CustomerClass person = new CustomerClass(); person.firstname = firstnameC.ToUpper(); person.surname = surnameC.ToUpper(); person.address = customerAddress.Text; person.id = lblCustomerID.Text; person.email = customerEmail.Text; person.telephone = customerPhone.Text; NewDelivery delivery = new NewDelivery(person); delivery.BringToFront(); delivery.ShowDialog(); } public int beginDeposit(decimal depositAmt, decimal currentBal) { try { cn.Open(); SortedList receiptList = new SortedList(); SqlDataReader dr; SqlCommand cm; cm = new SqlCommand("Select receiptID,(totalBill - paid) as remaining from CreditPurchases where status = @status and " + "customerID = @customerID and branchID = @branchID", cn); cm.Parameters.AddWithValue("@customerID", selectedID); cm.Parameters.AddWithValue("@status", "unsettled"); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { string receiptID = dr[0].ToString(); decimal remaining1 = decimal.Parse(dr[1].ToString()); receiptList.Add(receiptID, remaining1); } dr.Close(); tn = cn.BeginTransaction(); cm = new SqlCommand("Declare @total decimal(19,2) = Case when (Select SUM(debit - credit) from CustomerAccounts where branchID = @branchID " + "and customerID = @customerID) is null then 0 else (Select SUM(debit - credit) from CustomerAccounts where branchID = @branchID and customerID = @customerID) end " + "set @total = (@total + @debit) - @credit " + "Insert into CustomerAccounts(customerID, transactionID, date, debit, credit, balance, comments, branchID, countID) " + "Values(@customerID, @transactionID, @date, @debit, @credit, @total, @comments, @branchID, @countID)", cn, tn); cm.Parameters.AddWithValue("@customerID", selectedID); cm.Parameters.AddWithValue("@transactionID", DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Parameters.AddWithValue("@date", DateTime.Now); cm.Parameters.AddWithValue("@debit", depositAmt); cm.Parameters.AddWithValue("@credit", 0); cm.Parameters.AddWithValue("@comments", "Payment to account"); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", MainLogin.login_user + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.ExecuteNonQuery(); if (depositAmt + currentBal > 0) PreviousSettlement.settlement(cn, selectedID, depositAmt + currentBal, receiptList, tn); tn.Commit(); cn.Close(); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); return 0; } } public int beginWithdrawal(decimal WithdrawAmt) { try { cn.Open(); SortedList receiptList = new SortedList(); SqlDataReader dr; SqlCommand cm; tn = cn.BeginTransaction(); cm = new SqlCommand("Declare @total decimal(19,2) = Case when (Select SUM(debit - credit) from CustomerAccounts where branchID = @branchID " + "and customerID = @customerID) is null then 0 else (Select SUM(debit - credit) from CustomerAccounts where branchID = @branchID and customerID = @customerID) end " + "set @total = (@total + @debit) - @credit " + "Insert into CustomerAccounts(customerID, transactionID, date, debit, credit, balance, comments, branchID, countID) " + "Values(@customerID, @transactionID, @date, @debit, @credit, @total, @comments, @branchID, @countID)", cn, tn); cm.Parameters.AddWithValue("@customerID", selectedID); cm.Parameters.AddWithValue("@transactionID", DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Parameters.AddWithValue("@date", DateTime.Now); cm.Parameters.AddWithValue("@debit", 0); cm.Parameters.AddWithValue("@credit", WithdrawAmt); cm.Parameters.AddWithValue("@comments", "Payment to customer"); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", MainLogin.login_user + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.ExecuteNonQuery(); tn.Commit(); cn.Close(); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); return 0; } } private async void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex != -1) { lblName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); customerAddress.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); customerPhone.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); lblCustomerID.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString(); lbltaxID.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString(); customerEmail.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString(); firstnameC = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString(); surnameC = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString(); selectedID = lblCustomerID.Text; sell.Visible = true; btnEdit.Visible = true; customerHistory.Visible = true; paymentBTN.Visible = true; if (form != null) { // schDelivery.Visible = true; } Task task = new Task(loadBal); task.Start(); await task; } } } }