using BarcodeLib; using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Company_Setup; using BiskLog_Point_Of_Sale.Dialogs; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.POSDialogs; using BiskLog_Point_Of_Sale.Properties; using Microsoft.Reporting.WinForms; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Threading.Tasks; using System.Windows.Forms; namespace Point_Of_Sale_Managment { public partial class SalesPOS : Form { List cart = new List(); SqlConnection cn = new SqlConnection(); SqlCommand cm, cm1; DatabaseConn databasecon = new DatabaseConn(); SqlDataReader dr; int i = 1; string currency; decimal subtotal; decimal discount, vat; public static decimal mainTotal; public static List receiptClasses = new List(); public static List receiptClasses2 = new List(); string printLocation = ""; string printerReceiptDefault = Settings.Default.ReceiptPrinter; Form form = null; string invoiceid; CustomerClass customerO; bool printReceipt = false; public SalesPOS(Form f = null, List invoicecode = null, string invoiceid = null, CustomerClass customer = null) { InitializeComponent(); this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; this.WindowState = FormWindowState.Maximized; lblDate.Text = DateTime.Now.ToLongDateString(); cn = new SqlConnection(databasecon.MyConnection()); KeyPreview = true; currency = Settings.Default.currrencyCode + " "; discount = decimal.Parse("0.00"); if (customer != null) { customerO = customer; lblAddress.Text = customerO.address; lblTel.Text = customerO.telephone; lblName.Text = customerO.firstname; lblMail.Text = customerO.email; groupBox1.Visible = true; usualPanel.Visible = false; customerSalesPanel.Visible = true; } if (Settings.Default.AutomaticallyTax) { vat = decimal.Parse(Settings.Default.TaxRate.ToString()); } else { vat = decimal.Parse("0.0"); } if (f != null) { form = f; } if (invoicecode != null) { this.invoiceid = invoiceid; Initializing initializing = new Initializing(invoicecode); initializing.BringToFront(); initializing.ShowDialog(); if (initializing.DialogResult == DialogResult.OK) { newTransaction(); foreach (InvoicePOS pOS in initializing.getInvoiceList) { decimal price = decimal.Parse(pOS.totalprice.ToString()); int quantity = int.Parse(pOS.quantity.ToString()); decimal cost = price / quantity; dataGridView1.Rows.Add(i, pOS.productCode.ToString(), pOS.productname.ToString(), Settings.Default.currrencyCode + " " + cost, quantity, pOS.unitName, Settings.Default.currrencyCode + " " + price, pOS.unitCode, pOS.distinctiveCode); i++; } checkTotal(); } } } private void btnClose_Click(object sender, EventArgs e) { if (form != null) { form.Show(); } this.Close(); } public bool newTransaction() { if (Settings.Default.pinOnEveryTransaction) { Prompt transPrompt = new Prompt(); transPrompt.BringToFront(); transPrompt.ShowDialog(); if ((transPrompt.DialogResult == DialogResult.OK) && (transPrompt.confirmed)) { GetTransNo(); txtSearch.Enabled = true; txtSearch.Focus(); dataGridView1.Rows.Clear(); return true; } else { return false; } } else { GetTransNo(); txtSearch.Enabled = true; txtSearch.Focus(); dataGridView1.Rows.Clear(); return true; } } public Image getBarcodeImage(string Bcode) { Barcode barcodeAPI = new Barcode(); Color foreColor = Color.Black; Color backColor = Color.Transparent; return barcodeAPI.Encode(TYPE.UPCA, Bcode, foreColor, backColor, 290, 120); } public int makePayment(decimal balance, decimal tendered) { string transactionT = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); lblTransno.Invoke(new Action(() => { MemoryStream ms = new MemoryStream(); getBarcodeImage(lblTransno.Text).Save(ms, ImageFormat.Png); byte[] imageBytes = ms.ToArray(); string barcodeStringImage = Convert.ToBase64String(imageBytes); receiptClasses.Clear(); receiptClasses2.Clear(); bottom bottomReceipt = new bottom(lblTax.Text, lblTransno.Text, MainLogin.login_user, balance, tendered, discount.ToString(), lblDate.Text, LBLsubtotal.Text, lblTotalCol.Text, Form1.bCompanyName, Form1.branchName, Form1.branchLocation, Form1.branchTel, Form1.tin, Form1.vatno, barcodeStringImage); receiptClasses2.Add(bottomReceipt); GetTransNo(); })); cn.Open(); SqlTransaction tn = cn.BeginTransaction(); try { dataGridView1.Invoke(new Action(() => { foreach (DataGridViewRow row in dataGridView1.Rows) { cm = new SqlCommand("Declare @baseunit varchar(150) = (Select baseUnit from tblProduct where pcode = @pcode) " + "Declare @costprice decimal(19, 2) = (Select costprice from tblProduct where pcode = @pcode) " + "Declare @dquantity int = Case when @baseUnit = @unit then 1 * @quantity else " + "(Select [quantity/unit] * @quantity as quantity from ProductAltUnit where distinctiveCode = @distinctive and branchID = @branchID) end " + "Insert into tblCart(transno, id, quantity, date, price, cashier, status, total, countID, branchID, unit, costprice) " + "Values(@transno, @id, @dquantity, @date, @price, @cashier, @status, @total, @countID, @branchID, @unit, @costprice) " + "Declare @newQuantity int = (Select MAX(quantity) - MIN(@dquantity) from tblInventory where pcode = @pcode and branchID = @branchID) " + "Update tblInventory set quantity = @newQuantity where pcode = @pcode and branchID = @branchID " + "Insert into tblInventoryEntries(pcode, quantity, date, countID, branchID) values (@pcode, @newQuantity, @date, @countID, @branchID)", cn, tn); cm.Parameters.AddWithValue("@transno", lblTransno.Text); cm.Parameters.AddWithValue("@id", row.Cells[1].Value.ToString()); cm.Parameters.AddWithValue("@quantity", row.Cells[4].Value.ToString()); cm.Parameters.AddWithValue("@price", row.Cells[3].Value.ToString().Substring(currency.Length)); cm.Parameters.AddWithValue("@total", row.Cells[6].Value.ToString().Substring(currency.Length)); cm.Parameters.AddWithValue("@date", transactionT); cm.Parameters.AddWithValue("@status", "SOLD"); cm.Parameters.AddWithValue("@unit", row.Cells[7].Value.ToString()); cm.Parameters.AddWithValue("@distinctive", row.Cells[8].Value.ToString()); cm.Parameters.AddWithValue("@cashier", MainLogin.login_user); cm.Parameters.AddWithValue("@branchID", Form1.branch); cm.Parameters.AddWithValue("@countID", MainLogin.login_user + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff") + row.Index); cm.Parameters.AddWithValue("@pcode", row.Cells[1].Value.ToString()); cm.ExecuteNonQuery(); //cm1 = new SqlCommand("Declare @newQuantity int = (Select MAX(quantity) - MIN(@quantity) from tblInventory where pcode = @pcode and branchID = @branchID) " + // "Update tblInventory set quantity = @newQuantity where pcode = @pcode and branchID = @branchID " + // "Insert into tblInventoryEntries(pcode, quantity, date, countID, branchID) values " + // "(@pcode,@newQuantity,@date,@countID,@branchID)", cn, tn); //cm1.Parameters.AddWithValue("@quantity", int.Parse(row.Cells[4].Value.ToString())); //cm1.Parameters.AddWithValue("@branchID", Form1.branch); //cm1.Parameters.AddWithValue("@date", transactionT); //cm1.Parameters.AddWithValue("@countID", Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff") + row.Index); //cm1.ExecuteNonQuery(); ReceiptObject receipt = new ReceiptObject(row.Cells[2].Value.ToString(), decimal.Parse(row.Cells[3].Value.ToString().Substring(currency.Length)), int.Parse(row.Cells[4].Value.ToString()), decimal.Parse(row.Cells[6].Value.ToString().Substring(currency.Length))); receiptClasses.Add(receipt); } if (discount > 0) { cm1 = new SqlCommand("Insert into tblDiscountLogs (receiptID,cashier,date,discount,branchID,countID) values (@receiptID,@cashier,@date,@discount,@branchID,@countID)", cn, tn); cm1.Parameters.AddWithValue("@receiptID", lblTransno.Text); cm1.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm1.Parameters.AddWithValue("@cashier", MainLogin.login_user); cm1.Parameters.AddWithValue("@date", transactionT); cm1.Parameters.AddWithValue("@discount", discount); cm1.Parameters.AddWithValue("@countID", MainLogin.login_user + lblTransno.Text + transactionT); cm1.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(invoiceid)) { cm1 = new SqlCommand("Update tblInvoice set status = @status where invoiceID = @invoiceID and branchID = @branchID ", cn, tn); cm1.Parameters.AddWithValue("@invoiceID", invoiceid); cm1.Parameters.AddWithValue("@branchID", Form1.branch); cm1.Parameters.AddWithValue("@status", "SOLD"); cm1.ExecuteNonQuery(); } if (customerO != null) { cm1 = new SqlCommand("Insert into tblCustomerPurchases (customerID,transactionID,branchID,countID) values(@customerID,@transactionID,@branchID,@countID)", cn, tn); cm1.Parameters.AddWithValue("@customerID", customerO.id); cm1.Parameters.AddWithValue("@branchID", Form1.branch); cm1.Parameters.AddWithValue("@countID", DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm1.Parameters.AddWithValue("@transactionID", lblTransno.Text); cm1.ExecuteNonQuery(); } tn.Commit(); cn.Close(); GetTransNo(); if (printReceipt) { if (printerReceiptDefault == "Microsoft XPS Document Writer" || printerReceiptDefault == "Microsoft Print to PDF") { FolderBrowserDialog folder = new FolderBrowserDialog(); DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK && !String.IsNullOrEmpty(folder.SelectedPath)) { printLocation = folder.SelectedPath; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\Receipt.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("ReceiptReport", receiptClasses)); report.DataSources.Add(new ReportDataSource("ReceiptRest", receiptClasses2)); ReceiptConfig.PrintToPrinter(report, printLocation, printerReceiptDefault); } } else { printLocation = ""; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\Receipt.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("ReceiptReport", receiptClasses)); report.DataSources.Add(new ReportDataSource("ReceiptRest", receiptClasses2)); ReceiptConfig.PrintToPrinter(report, printLocation, printerReceiptDefault); } } dataGridView1.Rows.Clear(); lblTransno.Text = "000000000000000"; txtSearch.Enabled = true; txtSearch.Focus(); receiptClasses2.Clear(); receiptClasses.Clear(); customer.Text = ""; lblName.Text = ""; customerO = null; customer.Visible = false; usualPanel.Visible = false; customerSalesPanel.Visible = true; })); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); tn.Rollback(); cn.Close(); return 0; } } private void GetTransNo() { try { i = 1; subtotal = decimal.Parse("00.00"); lbldiscount.Text = "0"; LBLsubtotal.Text = "0.00"; string sdate = DateTime.Now.ToString("yyyyMMdd"); string transno; int count; cn.Open(); cm = new SqlCommand("Select top 1 transno from tblCart where transno like '" + sdate + "%' order by transno desc", cn); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows) { transno = dr[0].ToString(); count = int.Parse(transno.Substring(8, 4)); lblTransno.Text = sdate + (count + 1); } else { transno = sdate + "1001"; lblTransno.Text = transno; } dr.Close(); cn.Close(); lblTax.Text = vat.ToString() + "%"; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); lblTransno.Text = String.Format("{0:d9}", (DateTime.Now.Ticks / 10) % 10000000); } } private void btnNew_Click(object sender, EventArgs e) { newTransaction(); clearCustomer(); } private void txtSearch_Click(object sender, EventArgs e) { } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex != -1) { string colName = dataGridView1.Columns[e.ColumnIndex].Name; if (colName.Equals("Delete")) { int r = dataGridView1.CurrentCell.RowIndex; string productcode = dataGridView1.Rows[r].Cells[1].Value.ToString(); string title = "Remove item"; string message = "Are you show you would like to remove " + dataGridView1.Rows[r].Cells[2].Value.ToString() + " from the cart ?"; Confirmation Confirmation = new Confirmation(title, message); Confirmation.BringToFront(); Confirmation.ShowDialog(); if (Confirmation.DialogResult == DialogResult.Yes) { dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); if (dataGridView1.Rows.Count == 0) { discount = 0; lbldiscount.Text = "0"; } checkTotal(); cart.Remove(productcode); } } else { string productcode = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); string name = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); int quantity = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString()); string unit = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString(); string total = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString(); string selling = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); decimal price = decimal.Parse(selling.Substring(currency.Length)); decimal totalCost = decimal.Parse(total.Substring(currency.Length)); AlterWindow alterWindow = new AlterWindow(productcode, name, quantity, unit, price, totalCost); alterWindow.BringToFront(); alterWindow.ShowDialog(); if (alterWindow.DialogResult == DialogResult.OK) { dataGridView1.Rows[e.RowIndex].Cells[4].Value = alterWindow.quantity; dataGridView1.Rows[e.RowIndex].Cells[3].Value = currency + alterWindow.sellingprice; dataGridView1.Rows[e.RowIndex].Cells[6].Value = currency + alterWindow.totalPrice; } checkTotal(); txtSearch.Focus(); } } else { txtSearch.Focus(); } } private void txtSearch_TextChanged(object sender, EventArgs e) { try { if (txtSearch.Text == string.Empty) { return; } else { cn.Open(); cm = new SqlCommand("Select tblProduct.pcode,price,product_name,uni.unitname as unit,tblProduct.baseUnit from tblProduct " + "Inner Join tblInventory On tblInventory.pcode = tblProduct.pcode Inner Join UnitOfMeasure uni On uni.unitCode = tblProduct.baseUnit where " + "barcode = @barcode and tblInventory.branchID = @branch and tblInventory.quantity != 0 " + "Select tblProduct.pcode,PAU.[price/unit] as price,product_name,uni.unitname,PAU.unitCode as unit,distinctiveCode from ProductAltUnit PAU " + "Inner Join tblProduct On tblProduct.pcode = PAU.pcode Inner Join UnitOfMeasure uni On uni.unitCode = PAU.unitCode Inner Join tblInventory ti " + "On ti.pcode = PAU.pcode where PAU.unitBarcode = @barcode and ti.branchID = @branch and ti.quantity != 0 ", cn); cm.Parameters.AddWithValue("@barcode", txtSearch.Text); cm.Parameters.AddWithValue("@branch", Form1.branch); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows) { loadProductAutomatic(dr[0].ToString(), dr[2].ToString(), dr[1].ToString(), 1, dr[3].ToString(), dr[4].ToString(), dr[0].ToString()); txtSearch.Text = ""; dr.Close(); cn.Close(); } if (!dr.HasRows) { dr.NextResult(); dr.Read(); if (dr.HasRows) { loadProductAutomatic(dr[0].ToString(), dr[2].ToString(), dr[1].ToString(), 1, dr[3].ToString(), dr[4].ToString(), dr[5].ToString()); txtSearch.Text = ""; dr.Close(); cn.Close(); } } else { string title = "Product unavailable"; string message = "Product is supposed to be out of stock or product does not exist"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); txtSearch.Text = ""; dr.Close(); cn.Close(); } } } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.Message); } } private void BTNitem_Click(object sender, EventArgs e) { if (lblTransno.Text == "000000000000000") { if (newTransaction()) { AddProducttoCart producttoCart = new AddProducttoCart(this); producttoCart.BringToFront(); producttoCart.ShowDialog(); } } else { AddProducttoCart producttoCart = new AddProducttoCart(this); producttoCart.BringToFront(); producttoCart.ShowDialog(); } } public void loadProductManual(string id, string productName, string price, int quantity, string unitname, string unitCode, string distinctive) { decimal cost = decimal.Parse(price.Substring(currency.Length)); decimal total = cost * quantity; if (!cart.Contains(id) && !id.Equals(distinctive) && !cart.Contains(distinctive)) { dataGridView1.Rows.Add(i, id, productName, price, quantity, unitname, currency + total, unitCode, distinctive); cart.Add(distinctive); i++; } else if (!cart.Contains(id) && id.Equals(distinctive) && !cart.Contains(distinctive)) { dataGridView1.Rows.Add(i, id, productName, price, quantity, unitname, currency + total, unitCode, distinctive); cart.Add(id); i++; } else if (!id.Equals(distinctive) && !cart.Contains(distinctive)) { dataGridView1.Rows.Add(i, id, productName, price, quantity, unitname, currency + total, unitCode, distinctive); cart.Add(distinctive); i++; } else { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[1].Value.ToString().Equals(id) && row.Cells[8].Value.ToString().Equals(distinctive)) { int total_quantity = int.Parse(row.Cells[4].Value.ToString()); decimal total_bill = decimal.Parse(row.Cells[6].Value.ToString().Substring(currency.Length)); total_bill += total; total_quantity += quantity; row.Cells[4].Value = total_quantity; row.Cells[6].Value = currency + " " + total_bill; } } } checkTotal(); if (Settings.Default.BeepOnAdd) { Console.Beep(); } } public void loadProductAutomatic(string id, string productName, string price, int quantity, string unitname, string unitCode, string distinctive) { decimal cost = decimal.Parse(price); decimal total = cost * quantity; if (!cart.Contains(id) && !id.Equals(distinctive) && !cart.Contains(distinctive)) { dataGridView1.Rows.Add(i, id, productName, currency + price, quantity, unitname, currency + total, unitCode, distinctive); cart.Add(distinctive); i++; } else if (!cart.Contains(id) && id.Equals(distinctive) && !cart.Contains(distinctive)) { dataGridView1.Rows.Add(i, id, productName, currency + price, quantity, unitname, currency + total, unitCode, distinctive); cart.Add(id); i++; } else if (!id.Equals(distinctive) && !cart.Contains(distinctive)) { dataGridView1.Rows.Add(i, id, productName, currency + price, quantity, unitname, currency + total, unitCode, distinctive); cart.Add(distinctive); i++; } else { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[1].Value.ToString().Equals(id) && row.Cells[8].Value.ToString().Equals(distinctive)) { int total_quantity = int.Parse(row.Cells[4].Value.ToString()); decimal total_bill = decimal.Parse(row.Cells[6].Value.ToString().Substring(currency.Length)); total_bill += total; total_quantity += quantity; row.Cells[4].Value = total_quantity; row.Cells[6].Value = currency + " " + total_bill; } } } checkTotal(); if (Settings.Default.BeepOnAdd) { Console.Beep(); } } public void checkTotal() { decimal Ttotal = decimal.Parse("00.00"); foreach (DataGridViewRow rows in dataGridView1.Rows) { Ttotal += decimal.Parse(rows.Cells[6].Value.ToString().Substring(currency.Length)); } LBLsubtotal.Text = Ttotal.ToString(); decimal thiscou = decimal.Parse((Ttotal * discount / 100).ToString()); decimal vatr = (Ttotal * vat / 100); mainTotal = decimal.Parse((Ttotal + vatr - thiscou).ToString()); lblTotalCol.Text = currency + " " + mainTotal.ToString(); } private void SalesPOS_Load(object sender, EventArgs e) { if (customerO != null) { this.customer.Text = "Customer Name : " + customerO.firstname; this.customer.Visible = true; if (!newTransaction()) { this.Close(); } } lblTax.Text = vat + "%"; cashierName.Text = MainLogin.login_user; timer1.Start(); } private void btnSettle_Click(object sender, EventArgs e) { printReceipt = false; if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { settlePayment payment = new settlePayment(this); payment.BringToFront(); payment.ShowDialog(); lblTotalCol.Text = "00.00"; } cart.Clear(); } private void SalesPOS_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F1) { newTransaction(); clearCustomer(); } else if (e.KeyCode == Keys.F2) { if (lblTransno.Text == "000000000000000") { if (newTransaction()) { AddProducttoCart producttoCart = new AddProducttoCart(this); producttoCart.BringToFront(); producttoCart.ShowDialog(); } } else { AddProducttoCart producttoCart = new AddProducttoCart(this); producttoCart.BringToFront(); producttoCart.ShowDialog(); } } else if (e.KeyCode == Keys.F3) { if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can add a discount"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { Discount discount = new Discount(this); discount.BringToFront(); discount.ShowDialog(); } } else if (e.KeyCode == Keys.F7) { printReceipt = false; if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { if (customerO == null) { settlePayment payment = new settlePayment(this); payment.BringToFront(); payment.ShowDialog(); } else { checkTotal(); decimal bal = decimal.Parse(lblBal.Text.Substring(currency.Length)); CustomerSettlePayment payment = new CustomerSettlePayment(this, bal); payment.BringToFront(); payment.ShowDialog(); clearCustomer(); lblTotalCol.Text = "00.00"; } } cart.Clear(); } else if (e.KeyCode == Keys.F11) { printReceipt = true; if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { if (customerO == null) { settlePayment payment = new settlePayment(this); payment.BringToFront(); payment.ShowDialog(); } else { checkTotal(); decimal bal = decimal.Parse(lblBal.Text.Substring(currency.Length)); CustomerSettlePayment payment = new CustomerSettlePayment(this, bal); payment.BringToFront(); payment.ShowDialog(); clearCustomer(); lblTotalCol.Text = "00.00"; } } cart.Clear(); } else if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt) { string message = "Are you sure you would like to leave ?"; string title = "Confirmation"; Confirmation noAction = new Confirmation(title, message); noAction.BringToFront(); noAction.ShowDialog(); if (noAction.DialogResult == DialogResult.Yes) { this.Close(); } } else if (e.KeyCode == Keys.F5) { //only authorized users cancel SaleCancellation cancellation = new SaleCancellation(); cancellation.BringToFront(); cancellation.ShowDialog(); } else if (e.KeyCode == Keys.F6) { DailySales sales = new DailySales(); sales.BringToFront(); sales.ShowDialog(); } else if (e.KeyCode == Keys.F9) { CustomerBill customer = new CustomerBill(); customer.BringToFront(); customer.ShowDialog(); if (customer.DialogResult == DialogResult.OK) { customerO = new CustomerClass(); customerO = customer.customer; lblAddress.Text = customer.address; lblTel.Text = customer.telephone; lblName.Text = customer.name; lblMail.Text = customer.email; this.customer.Text = "Customer Name : " + customerO.firstname; this.customer.Visible = true; } } else if (e.KeyCode == Keys.F10) { string message = "Are you sure you would like to cancel this sale ?"; string title = "Confirmation"; Confirmation noAction = new Confirmation(title, message); noAction.BringToFront(); noAction.ShowDialog(); if (noAction.DialogResult == DialogResult.Yes) { dataGridView1.Rows.Clear(); newTransaction(); checkTotal(); } } } public void LoadDiscount(decimal dis) { discount = dis; lbldiscount.Text = discount.ToString() + "%"; checkTotal(); } private void BtnDiscount_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can add a discount"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { Discount discount = new Discount(this); discount.BringToFront(); discount.ShowDialog(); } } private void Label5_Click(object sender, EventArgs e) { string message = "Are you sure you would like to leave ?"; string title = "Confirmation"; Confirmation noAction = new Confirmation(title, message); noAction.BringToFront(); noAction.ShowDialog(); if (noAction.DialogResult == DialogResult.Yes) { this.Close(); } } private void MagicButton2_Click(object sender, EventArgs e) { string message = "Are you sure you would like to cancel this sale ?"; string title = "Confirmation"; Confirmation noAction = new Confirmation(title, message); noAction.BringToFront(); noAction.ShowDialog(); if (noAction.DialogResult == DialogResult.Yes) { customer.Text = ""; customer.Visible = false; dataGridView1.Rows.Clear(); newTransaction(); checkTotal(); } } private void Label5_MouseEnter(object sender, EventArgs e) { label5.BackColor = Color.Crimson; } private void Label5_MouseLeave(object sender, EventArgs e) { label5.BackColor = Color.FromArgb(2, 0, 46); } private void Label3_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } private void Label3_MouseEnter(object sender, EventArgs e) { label3.BackColor = Color.FromArgb(10, 10, 56); } private void Timer1_Tick(object sender, EventArgs e) { timeStamp.Text = DateTime.Now.ToString("HH:mm:ss"); } private void BtnCancel_Click(object sender, EventArgs e) { //only authorized users cancel SaleCancellation cancellation = new SaleCancellation(); cancellation.BringToFront(); cancellation.ShowDialog(); } private void magicButton3_Click(object sender, EventArgs e) { printReceipt = true; if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { settlePayment payment = new settlePayment(this); payment.BringToFront(); payment.ShowDialog(); lblTotalCol.Text = "00.00"; } cart.Clear(); } private void button19_Click(object sender, EventArgs e) { CustomerBill customer = new CustomerBill(); customer.BringToFront(); customer.ShowDialog(); if (customer.DialogResult == DialogResult.OK) { customerO = new CustomerClass(); customerO = customer.customer; lblAddress.Text = customer.address; lblTel.Text = customer.telephone; lblName.Text = customer.name; lblMail.Text = customer.email; this.customer.Text = "Customer Name : " + customerO.firstname; this.customer.Visible = true; } } public int loadBal() { try { lblBal.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", customerO.id); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows) { decimal figure = decimal.Parse(dr[0].ToString()); lblBal.Text = currency + figure; if (figure < 0) { lblBal.ForeColor = Color.Crimson; } else { lblBal.ForeColor = Color.DarkGreen; } } else { lblBal.Text = currency + "00.00"; lblBal.ForeColor = Color.DarkGreen; } dr.Close(); cn.Close(); })); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); return 0; } } public void clearCustomer() { lblName.Text = ""; lblBal.Text = ""; customerO = null; groupBox1.Visible = false; usualPanel.Visible = true; customerSalesPanel.Visible = false; this.customer.Text = ""; this.customer.Visible = false; } private async void lblName_TextChanged(object sender, EventArgs e) { if (!String.IsNullOrEmpty(lblName.Text)) { groupBox1.Visible = true; usualPanel.Visible = false; customerSalesPanel.Visible = true; this.customer.Text = "Customer Name : " + customerO.firstname; this.customer.Visible = true; Task task = new Task(loadBal); task.Start(); await task; } else { clearCustomer(); } } public int creditSale(decimal balance, decimal tendered) { string transactionT = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); decimal totalBill = 0; lblTransno.Invoke(new Action(() => { totalBill = decimal.Parse(lblTotalCol.Text.Substring(Settings.Default.currrencyCode.Length)); MemoryStream ms = new MemoryStream(); getBarcodeImage(lblTransno.Text).Save(ms, ImageFormat.Png); byte[] imageBytes = ms.ToArray(); string barcodeStringImage = Convert.ToBase64String(imageBytes); receiptClasses.Clear(); receiptClasses2.Clear(); bottom bottomReceipt = new bottom(lblTax.Text, lblTransno.Text, MainLogin.login_user, balance, tendered, discount.ToString(), lblDate.Text, LBLsubtotal.Text, lblTotalCol.Text, Form1.bCompanyName, Form1.branchName, Form1.branchLocation, Form1.branchTel, customerO.firstname, Form1.vatno, barcodeStringImage); receiptClasses2.Add(bottomReceipt); GetTransNo(); })); 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", customerO.id); 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(); cn.Close(); cn.Open(); SqlTransaction tn = cn.BeginTransaction(); try { dataGridView1.Invoke(new Action(() => { string status = ""; string comment = ""; if (tendered < totalBill && balance > 0) { status = "CREDIT"; comment = "ITEMS SOLD TO CUSTOMER ON CREDIT"; cm1 = new SqlCommand("Insert into CreditPurchases (customerID,receiptID,date,paid,totalBill,status,branchID) " + "values (@customerID,@receiptID,@date,@paid,@totalBill,@status,@branchID)", cn, tn); cm1.Parameters.AddWithValue("@customerID", customerO.id); cm1.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm1.Parameters.AddWithValue("@receiptID", lblTransno.Text); cm1.Parameters.AddWithValue("@date", transactionT); cm1.Parameters.AddWithValue("@paid", totalBill - balance); cm1.Parameters.AddWithValue("@totalBill", lblTotalCol.Text.Substring(Settings.Default.currrencyCode.Length)); cm1.Parameters.AddWithValue("@status", "unsettled"); cm1.ExecuteNonQuery(); } else if (tendered > totalBill) { decimal afterPayment = tendered - totalBill; status = "SOLD"; comment = "ITEMS SOLD TO CUSTOMER"; PreviousSettlement.settlement(cn, customerO.id, afterPayment, receiptList, tn); } else { status = "SOLD"; comment = "ITEMS SOLD TO CUSTOMER"; } foreach (DataGridViewRow row in dataGridView1.Rows) { cm = new SqlCommand("Declare @baseunit varchar(150) = (Select baseUnit from tblProduct where pcode = @pcode) " + "Declare @costprice decimal(19, 2) = (Select costprice from tblProduct where pcode = @pcode) " + "Declare @dquantity int = Case when @baseUnit = @unit then 1 * @quantity else " + "(Select [quantity/unit] * @quantity as quantity from ProductAltUnit where distinctiveCode = @distinctive and branchID = @branchID) end " + "Insert into tblCart(transno, id, quantity, date, price, cashier, status, total, countID, branchID, unit, costprice) " + "Values(@transno, @id, @dquantity, @date, @price, @cashier, @status, @total, @countID, @branchID, @unit, @costprice) " + "Declare @newQuantity int = (Select MAX(quantity) - MIN(@dquantity) from tblInventory where pcode = @pcode and branchID = @branchID) " + "Update tblInventory set quantity = @newQuantity where pcode = @pcode and branchID = @branchID " + "Insert into tblInventoryEntries(pcode, quantity, date, countID, branchID) values (@pcode, @newQuantity, @date, @countID, @branchID)", cn, tn); cm.Parameters.AddWithValue("@transno", lblTransno.Text); cm.Parameters.AddWithValue("@id", row.Cells[1].Value.ToString()); cm.Parameters.AddWithValue("@quantity", row.Cells[4].Value.ToString()); cm.Parameters.AddWithValue("@price", row.Cells[3].Value.ToString().Substring(currency.Length)); cm.Parameters.AddWithValue("@total", row.Cells[6].Value.ToString().Substring(currency.Length)); cm.Parameters.AddWithValue("@date", transactionT); cm.Parameters.AddWithValue("@status", status); cm.Parameters.AddWithValue("@unit", row.Cells[7].Value.ToString()); cm.Parameters.AddWithValue("@distinctive", row.Cells[8].Value.ToString()); cm.Parameters.AddWithValue("@cashier", MainLogin.login_user); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", MainLogin.login_user + Settings.Default.BranchID + DateTime.Now.ToString("yyyyMMddHHmmssfff") + row.Index); cm.Parameters.AddWithValue("@pcode", row.Cells[1].Value.ToString()); cm.ExecuteNonQuery(); ReceiptObject receipt = new ReceiptObject(row.Cells[2].Value.ToString(), decimal.Parse(row.Cells[3].Value.ToString().Substring(currency.Length)), int.Parse(row.Cells[4].Value.ToString()), decimal.Parse(row.Cells[6].Value.ToString().Substring(currency.Length))); receiptClasses.Add(receipt); } if (discount > 0) { cm1 = new SqlCommand("Insert into tblDiscountLogs (receiptID,cashier,date,discount,branchID,countID) values " + "(@receiptID,@cashier,@date,@discount,@branchID,@countID)", cn, tn); cm1.Parameters.AddWithValue("@receiptID", lblTransno.Text); cm1.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm1.Parameters.AddWithValue("@cashier", MainLogin.login_user); cm1.Parameters.AddWithValue("@date", transactionT); cm1.Parameters.AddWithValue("@discount", discount); cm1.Parameters.AddWithValue("@countID", MainLogin.login_user + lblTransno.Text + transactionT); cm1.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(invoiceid)) { cm1 = new SqlCommand("Update tblInvoice set status = @status where invoiceID = @invoiceID and branchID = @branchID ", cn, tn); cm1.Parameters.AddWithValue("@invoiceID", invoiceid); cm1.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm1.Parameters.AddWithValue("@status", "SOLD"); cm1.ExecuteNonQuery(); } 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", customerO.id); cm.Parameters.AddWithValue("@transactionID", lblTransno.Text); cm.Parameters.AddWithValue("@date", transactionT); cm.Parameters.AddWithValue("@debit", tendered); cm.Parameters.AddWithValue("@credit", lblTotalCol.Text.Substring(Settings.Default.currrencyCode.Length)); cm.Parameters.AddWithValue("@comments", comment); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", MainLogin.login_user + lblTransno.Text + transactionT); cm.ExecuteNonQuery(); cm1 = new SqlCommand("Insert into tblCustomerPurchases (customerID,transactionID,branchID,countID) values(@customerID,@transactionID,@branchID,@countID)", cn, tn); cm1.Parameters.AddWithValue("@customerID", customerO.id); cm1.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm1.Parameters.AddWithValue("@countID", DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm1.Parameters.AddWithValue("@transactionID", lblTransno.Text); cm1.ExecuteNonQuery(); tn.Commit(); cn.Close(); GetTransNo(); if (printReceipt) { if (printerReceiptDefault == "Microsoft XPS Document Writer" || printerReceiptDefault == "Microsoft Print to PDF") { FolderBrowserDialog folder = new FolderBrowserDialog(); DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK && !String.IsNullOrEmpty(folder.SelectedPath)) { printLocation = folder.SelectedPath; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\CreditReceipt.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("ReceiptReport", receiptClasses)); report.DataSources.Add(new ReportDataSource("ReceiptRest", receiptClasses2)); ReceiptConfig.PrintToPrinter(report, printLocation, printerReceiptDefault); } } else { printLocation = ""; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\CreditReceipt.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("ReceiptReport", receiptClasses)); report.DataSources.Add(new ReportDataSource("ReceiptRest", receiptClasses2)); ReceiptConfig.PrintToPrinter(report, printLocation, printerReceiptDefault); } } dataGridView1.Rows.Clear(); lblTransno.Text = "000000000000000"; txtSearch.Enabled = true; txtSearch.Focus(); receiptClasses2.Clear(); receiptClasses.Clear(); })); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); tn.Rollback(); cn.Close(); return 0; } } private async void magicButton7_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { checkTotal(); string titleM = "Action Required !!!"; string messageM = "Would you like a receipt for this transaction ?"; Confirmation confirmation = new Confirmation(titleM, messageM); confirmation.BringToFront(); confirmation.ShowDialog(); if (confirmation.DialogResult == DialogResult.Yes) { printReceipt = true; } else { printReceipt = false; } decimal balance = decimal.Parse(lblTotalCol.Text.Substring(Settings.Default.currrencyCode.Length)); decimal bal = decimal.Parse(lblBal.Text.Substring(Settings.Default.currrencyCode.Length)); if (bal > 0) { balance -= bal; if (balance < 0) balance *= 0; } Task task = new Task(() => creditSale(balance, 0)); task.Start(); int result = await task; if (result == 1) { string title = "Transaction Successful"; string message = "Billed has been credited to " + lblName.Text + "'s account successfully"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); clearCustomer(); lblTotalCol.Text = "00.00"; cart.Clear(); } else { string title = "Error Occurred"; string message = "An error occurred while billing customer"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } } private void magicButton6_Click(object sender, EventArgs e) { printReceipt = false; if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { checkTotal(); decimal bal = decimal.Parse(lblBal.Text.Substring(currency.Length)); CustomerSettlePayment payment = new CustomerSettlePayment(this, bal); payment.BringToFront(); payment.ShowDialog(); clearCustomer(); lblTotalCol.Text = "00.00"; } cart.Clear(); dataGridView1.Rows.Clear(); } private void magicButton5_Click(object sender, EventArgs e) { printReceipt = true; if (dataGridView1.Rows.Count == 0) { string title = "Cart empty"; string message = "Add items to cart before you can settle payment"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { checkTotal(); decimal bal = decimal.Parse(lblBal.Text.Substring(currency.Length)); CustomerSettlePayment payment = new CustomerSettlePayment(this, bal); payment.BringToFront(); payment.ShowDialog(); clearCustomer(); lblTotalCol.Text = "00.00"; } cart.Clear(); } private void BtnDaily_Click(object sender, EventArgs e) { DailySales sales = new DailySales(); sales.BringToFront(); sales.ShowDialog(); } private void lbldiscount_TextChanged(object sender, EventArgs e) { checkTotal(); } } }