using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using BiskLog_Point_Of_Sale.Properties; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.Products_Module; using BiskLog_Point_Of_Sale.Company_Setup; using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.CustomControls; namespace Point_Of_Sale_Managment { public partial class StockForm : Form { SqlConnection cn; SqlCommand cm, cm1; DatabaseConn databasecon = new DatabaseConn(); SqlDataReader dr; int newqty, oldqty, upqty; string oldval, newval = "0"; string productCode, baseunitName, productName; SqlTransaction transaction; int total = 0; ProductsMainHolder mainHolder; List productUnits; string messageRestock = ""; public StockForm(ProductsMainHolder productsMain, string code = null, string oldquantity = null, string productname = null, string baseunit = null) { InitializeComponent(); mainHolder = productsMain; if (!String.IsNullOrEmpty(code)) { productCode = code; oldval = oldquantity; txtRef.Text = getReference(); } cn = new SqlConnection(databasecon.MyConnection()); holding1.Left = (ClientSize.Width - holding1.Width) / 2; holding2.Left = (ClientSize.Width - holding2.Width) / 2; HandleCreated += StockForm_HandleCreated; if (!String.IsNullOrEmpty(baseunit)) baseunitName = baseunit.ToUpper(); productName = productname; } private void StockForm_HandleCreated(object sender, EventArgs e) { } private async void StockForm_Load(object sender, EventArgs e) { txtIN.Text = MainLogin.login_user; Task task = new Task(() => { LoadProducts(); //LoadStock(); return 1; }); holding1.Visible = true; holding2.Visible = true; task.Start(); await task; lblSelectedProduct.Text = productName; holding1.Visible = false; holding2.Visible = false; } public int LoadProducts() { try { dataGridView1.Invoke(new Action(() => { int i = 0; dataGridView1.Rows.Clear(); cn.Open(); cm = new SqlCommand("EXEC usp_productList_restockForm @filter = @searchkey", cn); cm.AddParametersWithValues("@searchkey", txtSearch.Text); dr = cm.ExecuteReader(); while (dr.Read()) { i++; dataGridView1.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr["BaseUnit"].ToString()); } dr.Close(); cn.Close(); dataGridView1.ClearSelection(); })); return 1; } catch { cn.Close(); return 0; } } private async void btnSave_Click(object sender, EventArgs e) { holding2.Visible = true; //if (total >= 2) //{ Task task = new Task(StockUpdate); task.Start(); int result = await task; if (result == 1) { NoAction action = new NoAction("Operation Completed Successfully", "You have successfully update the stock entry"); action.BringToFront(); action.ShowDialog(); Form1.printerDialog = null; dataGridView1.ClearSelection(); txtRef.Text = ""; clearUnitControls(); } else if (result == 0) { NoAction action = new NoAction("Operation failed", "There was an error while trying to update the stcok entry!!"); action.BringToFront(); action.ShowDialog(); Form1.printerDialog = null; } //} //else //{ // NoAction action = new NoAction("Operation denied", "To be able to restock items, you must fill all entries!!"); // action.BringToFront(); // action.ShowDialog(); // Form1.printerDialog = null; //} holding2.Visible = false; } public int StockUpdate() { try { int newstock = getQuantity(); string Messenger = "Are you sure you want to add "+ messageRestock+ " of " + lblSelectedProduct.Text + " to your inventory ?"; string heading = "Adding item"; Confirmation Confirmation = new Confirmation(message: Messenger, title: heading); Confirmation.BringToFront(); Confirmation.ShowDialog(); if (Confirmation.DialogResult == DialogResult.Yes) { txtRef.Invoke(new Action(() => { cn.Open(); cm = new SqlCommand("EXEC usp_restock_products @reference = @reference1,@productCode = @productCode1, @branchID = @branchID1,@quantity = @quantity1," + "@stockdate = @stockdate1,@stockinby = @stockinby1,@countID = @countID1", cn); cm.Parameters.AddWithValue("@reference1", txtRef.Text); cm.Parameters.AddWithValue("@productCode1", productCode); cm.Parameters.AddWithValue("@quantity1", newstock); cm.Parameters.AddWithValue("@stockdate1", datePicka.Value); cm.Parameters.AddWithValue("@stockinby1", txtIN.Text); cm.Parameters.AddWithValue("@branchID1", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID1", productCode + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.ExecuteNonQuery(); cn.Close(); lblSelectedProduct.Text = ""; LoadProducts(); })); return 1; } else { return 2; } } catch { cn.Close(); return 0; } } public string getReference() { return productCode.Substring(0, 3).ToUpper() + DateTime.Now.ToString("/yyyy/MM/dd/HH/mm/ss/fff"); } private async void txtSearch_TextChanged(object sender, EventArgs e) { Task task = new Task(LoadProducts); holding1.Visible = true; task.Start(); await task; holding1.Visible = false; } private void TxtRef_TextChanged(object sender, EventArgs e) { lockSave(); } private void TxtAdd_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 async void lblSelectedProduct_TextChanged(object sender, EventArgs e) { await clearUnitControls(); await getUnits(); } private void TxtIN_TextChanged(object sender, EventArgs e) { lockSave(); } public void lockSave() { foreach (Control box in panel5.Controls) { if (box is TextBox) { if (String.IsNullOrEmpty(box.Text)) { total--; btnSave.Enabled = false; } else { total++; btnSave.Enabled = true; } } } } //private void txtAdd_TextChanged(object sender, EventArgs e) //{ // lockSave(); // if (!String.IsNullOrEmpty(txtAdd.Text)) // { // newval = txtAdd.Text; // } //} private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { oldval = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); productCode = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); txtRef.Text = getReference(); baseunitName = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString(); lblSelectedProduct.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); } public async Task getUnits() { productUnits = new List(); cn.Open(); try { SqlDataReader dr; SqlCommand sqlCommand = new SqlCommand($"EXEC usp_getproduct_units @productCode = @pcode1,@branchID = @branchID1", cn); sqlCommand.Parameters.AddWithValue("@pcode1", productCode); sqlCommand.Parameters.AddWithValue("@branchID1", Form1.branch); sqlCommand.ExecuteNonQuery(); dr = sqlCommand.ExecuteReader(); while (dr.Read()) { ProductUnits productUnits = new ProductUnits { baseUnit = dr["baseUnit"].ToString(), costPerUnit = decimal.Parse(dr["costPerUnit"].ToString()), quantityPerUnit = int.Parse(dr["quantityPerUnit"].ToString()), unitname = dr["unitname"].ToString() }; this.productUnits.Add(productUnits); } cn.Close(); } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); } setUnitProperty(this.productUnits); } private async Task setUnitProperty(List units) { if (!String.IsNullOrEmpty(baseunitName)) { UnitControl basecontrol = new UnitControl(); basecontrol.unitLabel.Text = baseunitName.ToUpper(); unitPanels.Controls.Add(basecontrol); } foreach (ProductUnits unit in units) { UnitControl control = new UnitControl(); control.unitLabel.Text = unit.unitname.ToUpper(); unitPanels.Controls.Add(control); } } private async Task clearUnitControls() { unitPanels.Controls.Clear(); messageRestock = ""; } private int getQuantity() { messageRestock = ""; int totalBase = 0; foreach (Control control in unitPanels.Controls) { if (control is UnitControl unit) { string unitID = unit.unitLabel.Text; int value = (int)unit.unitValue.Value; if (value > 0) messageRestock = messageRestock + $"{value} {unitID.ToLower()}(s) "; if (unitID != baseunitName) { int rate = this.productUnits.Where(u => u.unitname.ToLower() == unitID.ToLower()).Select(u => u.quantityPerUnit).FirstOrDefault(); totalBase += value * rate; } else { totalBase += value; } } } return totalBase; } } }