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; 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; SqlTransaction transaction; int total = 0; ProductsMainHolder mainHolder; public StockForm(ProductsMainHolder productsMain, string code = null, string oldquantity = 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; } 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; 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("Select tblInventory.pcode,pdesc,tblInventory.quantity from tblProduct Inner Join tblInventory On tblInventory.pcode =" + "tblProduct.pcode where (tblProduct.pdesc like '%" + txtSearch.Text + "%') or (tblProduct.product_name like '%" + txtSearch.Text + "%') or" + " (tblProduct.pcode like '%" + txtSearch.Text + "%') order by tblInventory.quantity asc", cn); dr = cm.ExecuteReader(); while (dr.Read()) { i++; dataGridView1.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].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 >= 3) { 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(); txtAdd.Text = ""; txtRef.Text = ""; } 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 { oldqty = int.Parse(oldval); newqty = int.Parse(newval); upqty = oldqty + newqty; string Messenger = "Are you sure you want to add " + newqty + " items to " + productCode + " ?"; 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(); transaction = cn.BeginTransaction(); cm = new SqlCommand("Insert into tbStock (refno,pcode,qty,sdate,stockinby,branchID,countID) values " + "(@refno,@pcode,@qty,@sdate,@stockinby,@branchID,@countID)", cn); cm.Parameters.AddWithValue("@refno", txtRef.Text); cm.Parameters.AddWithValue("@pcode", productCode); cm.Parameters.AddWithValue("@qty", upqty); cm.Parameters.AddWithValue("@sdate", datePicka.Value); cm.Parameters.AddWithValue("@stockinby", txtIN.Text); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@countID", productCode + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Transaction = transaction; cm.ExecuteNonQuery(); cm1 = new SqlCommand("Update tblInventory set quantity = @quantity where branchID = @branchID and pcode = @pcode " + "Insert into tblInventoryEntries(pcode, quantity, date, countID, branchID) values (@pcode,@quantity,@date,@countID,@branchID)", cn); cm1.Parameters.AddWithValue("@pcode", productCode); cm1.Parameters.AddWithValue("@date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm1.Parameters.AddWithValue("@branchID", Form1.branch); cm1.Parameters.AddWithValue("@countID", productCode + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm1.Parameters.AddWithValue("@quantity", upqty); cm1.Transaction = transaction; cm1.ExecuteNonQuery(); transaction.Commit(); cn.Close(); LoadStock(); LoadProducts(); })); return 1; } else { return 2; } } catch { cn.Close(); return 0; } } public int LoadStock() { try { dataGridView2.Invoke(new Action(() => { int i = 0; dataGridView2.Rows.Clear(); cn.Open(); cm = new SqlCommand("Select TOP (100) refno,tblProduct.pcode,tblProduct.product_name,tblProduct.pdesc,qty,sdate,stockinby from tbStock Inner " + "Join tblProduct On tblProduct.pcode = tbStock.pcode where tblProduct.branchID = @branch order by sdate desc", cn); cm.Parameters.AddWithValue("@branch", Settings.Default.BranchID); dr = cm.ExecuteReader(); while (dr.Read()) { i++; dataGridView2.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), Convert.ToDateTime(dr[5].ToString()).ToLongDateString(), dr[6].ToString()); } dr.Close(); cn.Close(); dataGridView2.ClearSelection(); })); return 1; } 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 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(); } } }