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.Delivery { public partial class FreeLoading : Form { SqlConnection cn; SqlDataReader dr; SqlCommand cm; DatabaseConn conn = new DatabaseConn(); List listed = new List(); int i = 1; public FreeLoading() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); holding.Left = (ClientSize.Width - holding.Width) / 2; holding.Top = (ClientSize.Height - holding.Height) / 2; updateBox.Left = (ClientSize.Width - updateBox.Width) / 2; } private int pullTrucks() { try { cn.Open(); cm = new SqlCommand("Select SUM(TP.price * TI.quantity),TT.truckID,TT.licensePlate from tblTruckInventory TI " + "Inner Join tblProduct TP On TP.pcode = TI.pcode Inner Join tblTrucks TT On TT.truckID = TI.truckID where TP.branchID = " + "@branchID and TI.pcode in (Select TI.pcode from tblTruckInventory TI Inner Join tblProduct TP On TP.pcode = TI.pcode where " + "TP.branchID = @branchID) group by TT.truckID,TT.licensePlate", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { TruckItem truckItem = new TruckItem(); truckItem.Text = dr[2].ToString(); truckItem.Value = dr[1].ToString(); truckItem.worth = Settings.Default.currrencyCode + "0.00 "; trucks.Invoke(new Action(() => trucks.Items.Add(truckItem))); } cn.Close(); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private int LoadInventory2() { string car = ""; trucks.Invoke(new Action(() => car = (trucks.SelectedItem as TruckItem).Value.ToString())); try { orders.Invoke(new Action(() => { orders.Rows.Clear(); i = 1; cn.Open(); cm = new SqlCommand("Select SUM(TP.price * TI.quantity) as worth,TP.pcode,TP.product_name,TI.quantity,u.unitname,baseUnit from tblTruckInventory TI " + "Inner Join tblProduct TP On TP.pcode = TI.pcode Inner Join UnitOfMeasure u On u.unitCode = TI.unit where TI.truckID = @truckID and TP.branchID =" + " @branchID and TP.baseUnit = TI.unit and TI.pcode in (Select TI.pcode from tblTruckInventory TI Inner Join tblProduct TP On TP.pcode = TI.pcode where TP.branchID = @branchID and " + "TI.pcode like '%" + txtSearch.Text + "%') " + "group by TP.pcode,TP.product_name,TI.quantity,u.unitname,baseUnit " + "Select SUM(PAU.[price/unit] * TI.quantity) as worth,PAU.pcode,TP.product_name,TI.quantity,u.unitname,PAU.unitCode from tblTruckInventory TI " + "Inner Join ProductAltUnit PAU On PAU.pcode = TI.pcode Inner Join tblProduct TP On PAU.pcode = TP.pcode Inner Join UnitOfMeasure u On u.unitCode = TI.unit " + "where TI.truckID = @truckID and PAU.branchID = @branchID and PAU.unitCode = TI.unit and TI.pcode in (Select TI.pcode from tblTruckInventory TI Inner Join " + "tblProduct TP On TP.pcode = TI.pcode where TP.branchID = @branchID and " + "TI.pcode like '%" + txtSearch.Text + "%') group by PAU.pcode, TP.product_name, TI.quantity, u.unitname, PAU.unitCode", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@truckID", car); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { orders.Rows.Add(i, dr[1].ToString().ToUpper(), dr[2].ToString().ToUpper(), dr[3].ToString().ToUpper(), dr[4].ToString(), Settings.Default.currrencyCode + " " + dr[0].ToString().ToUpper(), dr[5].ToString()); i++; if (!listed.Contains(dr[1].ToString())) { listed.Add(dr[1].ToString()); } } dr.NextResult(); while (dr.Read()) { orders.Rows.Add(i, dr[1].ToString().ToUpper(), dr[2].ToString().ToUpper(), dr[3].ToString().ToUpper(), dr[4].ToString(), Settings.Default.currrencyCode + " " + dr[0].ToString().ToUpper(), dr[5].ToString()); i++; if (!listed.Contains(dr[1].ToString())) { listed.Add(dr[1].ToString()); } } orders.ClearSelection(); cn.Close(); })); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private int LoadInventory() { decimal total = 0; string car = ""; trucks.Invoke(new Action(() => car = (trucks.SelectedItem as TruckItem).Value.ToString())); try { orders.Invoke(new Action(() => { orders.Rows.Clear(); i = 1; cn.Open(); cm = new SqlCommand("Select SUM(TP.price * TI.quantity) as worth,TP.pcode,TP.product_name,TI.quantity,u.unitname,baseUnit from tblTruckInventory TI " + "Inner Join tblProduct TP On TP.pcode = TI.pcode Inner Join UnitOfMeasure u On u.unitCode = TI.unit where TI.truckID = @truckID and TP.branchID =" + " @branchID and TP.baseUnit = TI.unit and TI.pcode in (Select TI.pcode from tblTruckInventory TI Inner Join tblProduct TP On TP.pcode = TI.pcode where TP.branchID = @branchID and " + "TI.pcode like '%" + txtSearch.Text + "%') " + "group by TP.pcode,TP.product_name,TI.quantity,u.unitname,baseUnit " + "Select SUM(PAU.[price/unit] * TI.quantity) as worth,PAU.pcode,TP.product_name,TI.quantity,u.unitname,PAU.unitCode from tblTruckInventory TI " + "Inner Join ProductAltUnit PAU On PAU.pcode = TI.pcode Inner Join tblProduct TP On PAU.pcode = TP.pcode Inner Join UnitOfMeasure u On u.unitCode = TI.unit " + "where TI.truckID = @truckID and PAU.branchID = @branchID and PAU.unitCode = TI.unit and TI.pcode in (Select TI.pcode from tblTruckInventory TI Inner Join " + "tblProduct TP On TP.pcode = TI.pcode where TP.branchID = @branchID and " + "TI.pcode like '%" + txtSearch.Text + "%') group by PAU.pcode, TP.product_name, TI.quantity, u.unitname, PAU.unitCode", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@truckID", car); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { orders.Rows.Add(i, dr[1].ToString().ToUpper(), dr[2].ToString().ToUpper(), dr[3].ToString().ToUpper(), dr[4].ToString(), Settings.Default.currrencyCode + " " + dr[0].ToString().ToUpper(), dr[5].ToString()); total += decimal.Parse(dr[0].ToString()); i++; if (!listed.Contains(dr[1].ToString())) { listed.Add(dr[1].ToString()); } } dr.NextResult(); while (dr.Read()) { orders.Rows.Add(i, dr[1].ToString().ToUpper(), dr[2].ToString().ToUpper(), dr[3].ToString().ToUpper(), dr[4].ToString(), Settings.Default.currrencyCode + " " + dr[0].ToString().ToUpper(), dr[5].ToString()); total += decimal.Parse(dr[0].ToString()); i++; if (!listed.Contains(dr[1].ToString())) { listed.Add(dr[1].ToString()); } } orders.ClearSelection(); cn.Close(); networth.Text = Settings.Default.currrencyCode + " " + total.ToString(); })); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private async void FreeLoading_Load(object sender, EventArgs e) { Task loadTrucks = new Task(pullTrucks); holding.Visible = true; loadTrucks.Start(); await loadTrucks; holding.Visible = false; } private async void Trucks_TextChanged(object sender, EventArgs e) { if (!String.IsNullOrEmpty(trucks.Text)) { addProducts.Enabled = true; } else { addProducts.Enabled = false; } truckID.Text = (trucks.SelectedItem as TruckItem).Value.ToString().ToUpper(); networth.Text = (trucks.SelectedItem as TruckItem).worth.ToString().ToUpper(); Task task = new Task(LoadInventory); holding.Visible = true; task.Start(); int result = await task; if (result == 0) { string title = "An Error Occurred"; string message = "An error occurred while getting truck's inventory, please try again"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } holding.Visible = false; } private void Orders_CellClick(object sender, DataGridViewCellEventArgs e) { try { if (e.RowIndex != -1) { string car = ""; trucks.Invoke(new Action(() => car = (trucks.SelectedItem as TruckItem).Value.ToString())); string colName = orders.Columns[e.ColumnIndex].Name; string id = orders.Rows[e.RowIndex].Cells[1].Value.ToString(); string pname = orders.Rows[e.RowIndex].Cells[2].Value.ToString(); string unit = orders.Rows[e.RowIndex].Cells[4].Value.ToString(); string unitCode = orders.Rows[e.RowIndex].Cells[6].Value.ToString(); int current = int.Parse(orders.Rows[e.RowIndex].Cells[3].Value.ToString()); switch (colName) { case "stockup": TruckStockUp stockUp = new TruckStockUp(pcode: id, truckID: car, name: pname, unit: unit, unitCode: unitCode); stockUp.BringToFront(); stockUp.ShowDialog(); string title = ""; string message = ""; switch (stockUp.DialogResult) { case DialogResult.OK: title = "Success"; message = "Product restocked successfully"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); int oldquantity = int.Parse(orders.Rows[e.RowIndex].Cells[3].Value.ToString()); int restocked = stockUp.quantity; int newvalue = oldquantity + restocked; orders.Rows[e.RowIndex].Cells[3].Value = newvalue.ToString(); orders.Rows[e.RowIndex].Cells[5].Value = stockUp.total; break; case DialogResult.Abort: title = "Error Occurred"; message = "An error occurred while trying to update product stock"; NoAction noAction1 = new NoAction(title, message); noAction1.BringToFront(); noAction1.ShowDialog(); break; case DialogResult.Ignore: title = "Operation not allowed"; message = "Sorry you do not have enough quantity of this product in the branch's inventory to complete " + "this operation"; NoAction noAction2 = new NoAction(title, message); noAction2.BringToFront(); noAction2.ShowDialog(); break; default: break; } break; case "deduct": TruckStockDown stockDown = new TruckStockDown(pcode: id, truckID: car, name: pname, current: current, unit: unit, unitCode: unitCode); stockDown.BringToFront(); stockDown.ShowDialog(); string titled = ""; string messaged = ""; switch (stockDown.DialogResult) { case DialogResult.OK: titled = "Success"; messaged = "Products offloaded successfully"; NoAction noAction = new NoAction(titled, messaged); noAction.BringToFront(); noAction.ShowDialog(); int oldquantity = int.Parse(orders.Rows[e.RowIndex].Cells[3].Value.ToString()); int restocked = stockDown.quantity; int newvalue = oldquantity - restocked; orders.Rows[e.RowIndex].Cells[3].Value = newvalue.ToString(); orders.Rows[e.RowIndex].Cells[5].Value = stockDown.total; break; case DialogResult.Abort: titled = "Error Occurred"; messaged = "An error occurred while trying to update product stock"; NoAction noAction1 = new NoAction(titled, messaged); noAction1.BringToFront(); noAction1.ShowDialog(); break; case DialogResult.Cancel: titled = "Operation not allowed"; messaged = "Operation could not be completed since the quantity to be offloaded was greater than the actual " + "number available in truck's inventory"; NoAction noAction2 = new NoAction(titled, messaged); noAction2.BringToFront(); noAction2.ShowDialog(); break; default: break; } break; default: break; } } } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); } } private async void txtSearch_TextChanged(object sender, EventArgs e) { truckID.Text = (trucks.SelectedItem as TruckItem).Value.ToString().ToUpper(); networth.Text = (trucks.SelectedItem as TruckItem).worth.ToString().ToUpper(); Task task = new Task(LoadInventory2); holding.Visible = true; task.Start(); int result = await task; if (result == 0) { string title = "An Error Occurred"; string message = "An error occurred while getting truck's inventory, please try again"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } holding.Visible = false; } //public void loadProductManual(string productName, int quantity, decimal price, string id, string unitname, string unitcode, string distinctive) //{ // if (!listed.Contains(id) && !id.Equals(distinctive) && !listed.Contains(distinctive)) // { // orders.Rows.Add(i, productName, quantity, unitname, currency + price, id, unitcode, distinctive); // listed.Add(distinctive); // i++; // } // else if (!listed.Contains(id) && id.Equals(distinctive) && !listed.Contains(distinctive)) // { // orders.Rows.Add(i, productName, quantity, unitname, currency + price, id, unitcode, distinctive); // listed.Add(id); // i++; // } // else if (!id.Equals(distinctive) && !listed.Contains(distinctive)) // { // orders.Rows.Add(i, productName, quantity, unitname, currency + price, id, unitcode, distinctive); // listed.Add(distinctive); // i++; // } // else // { // foreach (DataGridViewRow row in orders.Rows) // { // if (row.Cells[5].Value.ToString().Equals(id) && row.Cells[7].Value.ToString().Equals(distinctive)) // { // int total_quantity = int.Parse(row.Cells[2].Value.ToString()); // decimal total_bill = decimal.Parse(row.Cells[4].Value.ToString().Substring(currency.Length)); // total_quantity += quantity; // row.Cells[2].Value = total_quantity; // row.Cells[4].Value = currency + " " + (total_bill * total_quantity); // } // } // } //} public void loadProductManual(string productName, int quantity, decimal price, string id, string unitname, string unitcode, string distinctive) { // itemTotal += price; if (!listed.Contains(id) && !id.Equals(distinctive) && !listed.Contains(distinctive)) { orders.Rows.Add(i, productName, quantity, unitname, Settings.Default.currrencyCode + " " + price, id, unitcode, distinctive); listed.Add(distinctive); i++; } else if (!listed.Contains(id) && id.Equals(distinctive) && !listed.Contains(distinctive)) { orders.Rows.Add(i, productName, quantity, unitname, Settings.Default.currrencyCode + " " + price, id, unitcode, distinctive); listed.Add(id); i++; } else if (!id.Equals(distinctive) && !listed.Contains(distinctive)) { orders.Rows.Add(i, productName, quantity, unitname, Settings.Default.currrencyCode + " " + price, id, unitcode, distinctive); listed.Add(distinctive); i++; } else { foreach (DataGridViewRow row in orders.Rows) { if (row.Cells[5].Value.ToString().Equals(id, StringComparison.OrdinalIgnoreCase) && row.Cells[7].Value.ToString().Equals(distinctive)) { int quantityO = quantity + int.Parse(row.Cells[3].Value.ToString()); string old = row.Cells[5].Value.ToString().Substring(Settings.Default.currrencyCode.Length); decimal newPrice = decimal.Parse(old) + price; row.Cells[3].Value = quantityO.ToString(); row.Cells[5].Value = Settings.Default.currrencyCode + " " + newPrice.ToString(); } } } } private void addProducts_Click(object sender, EventArgs e) { string truckID = (trucks.SelectedItem as TruckItem).Value.ToString(); AddProductTruck addProductTruck = new AddProductTruck(this, truckID); addProductTruck.BringToFront(); addProductTruck.ShowDialog(); } } }