using BiskLog_Point_Of_Sale.Classes; 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.Products_Module { public partial class PriceAmendentForm : Form { SqlConnection cn; SqlCommand cm; DatabaseConn conn = new DatabaseConn(); SqlTransaction transaction; List products; public PriceAmendentForm(List products) { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); holding.Left = (ClientSize.Width - holding.Width) / 2; holding.Top = (ClientSize.Height - holding.Height) / 2; this.products = products; } private void exitBTN_Click(object sender, EventArgs e) { this.Close(); } private void exitBTN_MouseEnter(object sender, EventArgs e) { exitBTN.BackColor = Color.Crimson; } private void exitBTN_MouseLeave(object sender, EventArgs e) { exitBTN.BackColor = Color.Transparent; } private void PriceAmendentForm_Load(object sender, EventArgs e) { foreach (ProductAmendment product in products) { name.Text = product.productname; pcode.Text = product.productCode; txtDesc.Text = product.productDescription; if (String.IsNullOrEmpty(baseUnitCode.Text)) { baseName.Text = product.baseUnitName; baseCurrent.Text = product.baseUnitprice; baseUnitCode.Text = product.baseUnitCode; baseNew.Enabled = true; baseNew.Text = product.baseUnitprice; ; } if (String.IsNullOrEmpty(unit1Code.Text)) { unit1Code.Text = product.unitDistinctive; unit1Name.Text = product.unitName; unit1Current.Text = product.unitprice; unit1New.Enabled = true; unit1New.Text = product.unitprice; } else if (String.IsNullOrEmpty(unit2Code.Text)) { unit2Code.Text = product.unitDistinctive; unit2Name.Text = product.unitName; unit2Current.Text = product.unitprice; unit2New.Enabled = true; unit2New.Text = product.unitprice; } else if (String.IsNullOrEmpty(unit3Code.Text)) { unit3Code.Text = product.unitDistinctive; unit3Name.Text = product.unitName; unit3Current.Text = product.unitprice; unit3New.Enabled = true; unit3New.Text = product.unitprice; } else if (String.IsNullOrEmpty(unit4Code.Text)) { unit4Code.Text = product.unitDistinctive; unit4Name.Text = product.unitName; unit4Current.Text = product.unitprice; unit4New.Enabled = true; unit4New.Text = product.unitprice; } else if (String.IsNullOrEmpty(unit5Code.Text)) { unit5Code.Text = product.unitDistinctive; unit5Name.Text = product.unitName; unit5Current.Text = product.unitprice; unit5New.Enabled = true; unit5New.Text = product.unitprice; } } } private async void btnSave_Click(object sender, EventArgs e) { holding.Visible = true; Task task = new Task(amendPricing); task.Start(); int result = await task; if (result == 1) { this.DialogResult = DialogResult.OK; string Message = "You have successfully changed the pricing of " + name.Text; string Title = "Price changes successful"; NoAction action = new NoAction(message: Message, title: Title); action.BringToFront(); action.ShowDialog(); } else { string Message = "An error occurred while changing the price of " + name.Text + " please try again later"; string Title = "Error while making changes"; NoAction action = new NoAction(message: Message, title: Title); action.BringToFront(); action.ShowDialog(); } holding.Visible = false; } public int amendPricing() { try { baseUnitCode.Invoke(new Action(() => { cn.Open(); transaction = cn.BeginTransaction(); if (!String.IsNullOrEmpty(baseUnitCode.Text) && !String.IsNullOrEmpty(baseNew.Text)) { cm = new SqlCommand("Insert into tblPriceChanges (pcode,previous_price,current_price,change_date,branchID,countID) values " + "(@pcode,@previous_price,@current_price,@change_date,@branchID,@countID)", cn); cm.Parameters.AddWithValue("@pcode", pcode.Text); cm.Parameters.AddWithValue("@previous_price", baseCurrent.Text); cm.Parameters.AddWithValue("@current_price", baseNew.Text); cm.Parameters.AddWithValue("@change_date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", pcode.Text + Settings.Default.BranchID + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Transaction = transaction; cm.ExecuteNonQuery(); cm = new SqlCommand("Update tblProduct set price = @price where pcode = @pcode and branchID = @branchID", cn); cm.Parameters.AddWithValue("@pcode", pcode.Text); cm.Parameters.AddWithValue("@price", baseNew.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(unit1Code.Text) && !String.IsNullOrEmpty(unit1New.Text)) { cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn); cm.Parameters.AddWithValue("@distinctiveCode", unit1Code.Text); cm.Parameters.AddWithValue("@price", unit1New.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(unit2Code.Text) && !String.IsNullOrEmpty(unit2New.Text)) { cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn); cm.Parameters.AddWithValue("@distinctiveCode", unit2Code.Text); cm.Parameters.AddWithValue("@price", unit2New.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(unit3Code.Text) && !String.IsNullOrEmpty(unit3New.Text)) { cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn); cm.Parameters.AddWithValue("@distinctiveCode", unit3Code.Text); cm.Parameters.AddWithValue("@price", unit3New.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(unit4Code.Text) && !String.IsNullOrEmpty(unit4New.Text)) { cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn); cm.Parameters.AddWithValue("@distinctiveCode", unit4Code.Text); cm.Parameters.AddWithValue("@price", unit4New.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); } if (!String.IsNullOrEmpty(unit5Code.Text) && !String.IsNullOrEmpty(unit5New.Text)) { cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn); cm.Parameters.AddWithValue("@distinctiveCode", unit5Code.Text); cm.Parameters.AddWithValue("@price", unit5New.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Transaction = transaction; cm.ExecuteNonQuery(); } transaction.Commit(); cn.Close(); })); return 1; } catch (Exception ex) { transaction.Rollback(); cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private void baseNew_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 46) { // accepts . character } else if (e.KeyChar == 8) { //accepts backspace } else if ((e.KeyChar < 48) || (e.KeyChar > 57)) //ascii code 48-57 between 0-9 { e.Handled = true; } } } }