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.Properties; 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 Point_Of_Sale_Managment { public partial class SaleCancellation : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); bool soldToCustomer = false; public SaleCancellation() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); } private void SaleCancellation_Load(object sender, EventArgs e) { } private void SaleCancellation_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { this.Close(); } } private void TextBox1_TextChanged(object sender, EventArgs e) { getReceipt(textBox1.Text); if (dataGridView1.Rows.Count > 0) { btnCancel.Enabled = true; } else { btnCancel.Enabled = false; } } public void cancelSales(string transactionKey) { SqlTransaction transaction; cn.Open(); transaction = cn.BeginTransaction(); try { cm = new SqlCommand("Update tblCart set status = @status where branchID = @branch and transno = @transno", cn); cm.Parameters.AddWithValue("@status", "CANCELLED"); cm.Parameters.AddWithValue("@transno", transactionKey); cm.Parameters.AddWithValue("@branch", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); cm = new SqlCommand("Insert into tblCancelledTransactions (transno,dateCancelled,cancelledBy,branchID,countID) " + "values (@transno,@dateCancelled,@cancelledBy,@branchID,@countID)", cn); cm.Parameters.AddWithValue("@dateCancelled", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@transno", transactionKey); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@cancelledBy", MainLogin.login_user); cm.Parameters.AddWithValue("@countID", transactionKey + Settings.Default.BranchID + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Transaction = transaction; cm.ExecuteNonQuery(); if (soldToCustomer) { cm = new SqlCommand("Declare @debit decimal(19,2) = Case when (Select paid from CreditPurchases where receiptID = @transaction and branchID = @branchID) is not null then " + "(Select paid from CreditPurchases where receiptID = @transaction and branchID = @branchID) else " + "(Select SUM(total) from tblCart where transno = @transaction and branchID = @branchID) end " + "Declare @customerID varchar(50) = (Select customerID from tblCustomerPurchases where transactionID = @transaction and branchID = @branchID) " + "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) "+ "Delete from tblCustomerPurchases where transactionID = @transaction and branchID = @branchID and customerID = @customerID", cn); cm.Parameters.AddWithValue("@transaction", transactionKey); cm.Parameters.AddWithValue("@transactionID", DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@credit", 0); cm.Parameters.AddWithValue("@comments", "ReceiptID with transaction ID : "+ transactionKey + " was cancelled"); cm.Parameters.AddWithValue("@date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@countID", transactionKey + Settings.Default.BranchID + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Transaction = transaction; cm.ExecuteNonQuery(); } foreach (DataGridViewRow row in dataGridView1.Rows) { cm = new SqlCommand("Declare @baseunit varchar(150) = (Select baseUnit 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 pcode = @pcode and unitCode = @unit and branchID = @branchID) end " + "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); cm.Parameters.AddWithValue("@quantity", row.Cells[3].Value.ToString()); cm.Parameters.AddWithValue("@unit", row.Cells[7].Value.ToString()); cm.Parameters.AddWithValue("@pcode", row.Cells[5].Value.ToString()); cm.Parameters.AddWithValue("@date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", transactionKey + Settings.Default.BranchID + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Transaction = transaction; cm.ExecuteNonQuery(); } transaction.Commit(); cn.Close(); string title = "Cancelled"; string message = "Receipt with transaction number " + transactionKey + " has been cancelled successfully"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } catch (Exception ex) { transaction.Rollback(); cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); } } public void getReceipt(string key) { int i = 1; try { dataGridView1.Rows.Clear(); cn.Open(); cm = new SqlCommand("Select tblProduct.product_name, tblCart.price,tblCart.quantity,tblCart.total,date,tblCart.id,un.unitname,unit " + "from tblCart Inner Join tblProduct On tblProduct.pcode = tblCart.id Inner Join UnitOfMeasure un On un.unitCode = tblCart.unit and un.unitCode = tblProduct.baseUnit " + "where tblCart.branchID = @branch and transno = @transaction and tblCart.status <> 'CANCELLED' " + "Select tblProduct.product_name, tblCart.price, tblCart.quantity / PAU.[quantity/unit], tblCart.total, date, tblCart.id, un.unitname, unit " + "from tblCart Inner Join tblProduct On tblProduct.pcode = tblCart.id Inner Join UnitOfMeasure un On un.unitCode = tblCart.unit " + "Inner Join ProductAltUnit PAU on PAU.unitCode = tblCart.unit and PAU.pcode = tblCart.id where tblCart.branchID = @branch " + "and transno = @transaction and tblCart.status <> 'CANCELLED' " + "Select Count(Distinct customerID) from tblCustomerPurchases where transactionID = @transaction", cn); cm.Parameters.AddWithValue("@transaction", key); cm.Parameters.AddWithValue("@branch", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { dateP.Value = Convert.ToDateTime(dr[4].ToString()); dataGridView1.Rows.Add(i, dr[0].ToString().ToUpper(), dr[1].ToString().ToUpper(), dr[2].ToString(), dr[6].ToString(), dr[3].ToString(), dr[5].ToString(), dr[7].ToString()); i++; } dr.NextResult(); while (dr.Read()) { dateP.Value = Convert.ToDateTime(dr[4].ToString()); dataGridView1.Rows.Add(i, dr[0].ToString().ToUpper(), dr[1].ToString().ToUpper(), dr[2].ToString(), dr[6].ToString(), dr[3].ToString(), dr[5].ToString(), dr[7].ToString()); i++; } dr.NextResult(); dr.Read(); int result = int.Parse(dr[0].ToString()); if (result == 1) soldToCustomer = true; dr.Close(); cn.Close(); } catch(Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); } } private void TextBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 8) { //accepts backspace } else if ((e.KeyChar < 48) || (e.KeyChar > 57)) //ascii code 48-57 between 0-9 { e.Handled = true; } } private void BtnCancel_Click(object sender, EventArgs e) { string title = "Confirm Cancellation"; string message = "Are you sure you would like to cancel this transaction ?"; Confirmation confirmation = new Confirmation(title, message); confirmation.BringToFront(); confirmation.ShowDialog(); if (confirmation.DialogResult == DialogResult.Yes) { if (Settings.Default.cancellationConfirmation) { Prompt promptCancel = new Prompt(); promptCancel.BringToFront(); promptCancel.ShowDialog(); if ((promptCancel.DialogResult == DialogResult.OK) && (promptCancel.confirmed)) { cancelSales(textBox1.Text); dataGridView1.Rows.Clear(); textBox1.Text = ""; } } else { cancelSales(textBox1.Text); dataGridView1.Rows.Clear(); textBox1.Text = ""; } } } 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.Transparent; } } }