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; 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 TruckManagement : Form { Point drawingPoint = new Point(0, 0); Point pointerPoint = new Point(0, 0); SqlConnection cn; SqlCommand cm; SqlDataReader dr; SortedList truckWithD = new SortedList(); SortedList trucksWithoutD = new SortedList(); DatabaseConn conn = new DatabaseConn(); string selectedLicense; PictureBox currentSender = null; public TruckManagement() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); holding.Left = (ClientSize.Width - holding.Width) / 2; } private void ADDANEWTRUCKToolStripMenuItem_Click(object sender, EventArgs e) { newTruckAddition(); } public int loadTrucks() { try { cn.Open(); cm = new SqlCommand("Select truckID,licensePlate,driver from tblTrucks where branchID = @branchID", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { if (!String.IsNullOrEmpty(dr[2].ToString())) { truckWithD.Add(dr[0].ToString(), dr[1].ToString()); } else { trucksWithoutD.Add(dr[0].ToString(), dr[1].ToString()); } } dr.Close(); cn.Close(); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); return 0; } } public void newTruckAddition() { try { NewTruck newTruck = new NewTruck(); newTruck.BringToFront(); newTruck.ShowDialog(); if (newTruck.DialogResult == DialogResult.OK) { if (newTruck.created && newTruck.ready) { creation(newTruck.licensePlate, newTruck.truckID, mainpanel2); truckWithD.Add(newTruck.truckID, newTruck.licensePlate); } else if (newTruck.created) { trucksWithoutD.Add(newTruck.truckID, newTruck.licensePlate); Notcreation(newTruck.licensePlate, newTruck.truckID, mainpanel2); } } } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); } } public void creation(string license, string truckIdentity, Panel paneld = null) { var panel = new Panel(); panel.BackColor = Color.FromName("GradientActiveCaption"); panel.Size = new Size(170, 110); panel.Name = truckIdentity; var truckID = new Label(); truckID.Name = "Truck" + String.Format("{0:d12}", (DateTime.Now.Ticks / 10) % 1000000000); truckID.Text = license; truckID.AutoSize = true; truckID.ForeColor = Color.FromArgb(0, 52, 69); truckID.Font = new Font("Segoe UI", 12, FontStyle.Bold); truckID.Cursor = Cursors.Hand; truckID.Location = new Point(drawingPoint.X, drawingPoint.Y); panel.Controls.Add(truckID); var picture = new PictureBox(); picture.Name = "TruckImage" + String.Format("{0:d12}", (DateTime.Now.Ticks / 10) % 1000000000); ; picture.Cursor = Cursors.Hand; picture.Size = new Size(130, 90); picture.Location = new Point(drawingPoint.X - 5, drawingPoint.Y + 24); picture.Image = Resources.truck; picture.SizeMode = PictureBoxSizeMode.StretchImage; if (paneld == null) { picture.MouseClick += pictureTable2; panel.Controls.Add(picture); mainpanel.Controls.Add(panel); } else { picture.MouseClick += Picture_MouseClick; panel.Controls.Add(picture); panel.Location = pointerPoint; paneld.Controls.Add(panel); } } private void pictureTable2(object sender, MouseEventArgs e) { var cellPos = GetRowColIndex(mainpanel, mainpanel.PointToClient(Cursor.Position)); if (e.Button == MouseButtons.Right) { drawingPoint = new Point(MousePosition.X, MousePosition.Y); truckMenu.Show(drawingPoint); try { currentSender = sender as PictureBox; Control c = mainpanel.GetControlFromPosition(cellPos.Value.X, cellPos.Value.Y); selectedLicense = c.Name.ToString(); } catch { } } } public void Notcreation(string license, string truckIdentity, Panel paneld = null) { var panel = new Panel(); panel.Name = truckIdentity; panel.BackColor = Color.FromName("GradientActiveCaption"); panel.Size = new Size(170, 110); var truckID = new Label(); truckID.Name = "Truck" + String.Format("{0:d12}", (DateTime.Now.Ticks / 10) % 1000000000); truckID.Text = license; truckID.AutoSize = true; truckID.ForeColor = Color.FromArgb(0, 52, 69); truckID.Font = new Font("Segoe UI", 12, FontStyle.Bold); truckID.Cursor = Cursors.Hand; truckID.Location = new Point(drawingPoint.X, drawingPoint.Y); panel.Controls.Add(truckID); var picture = new PictureBox(); picture.Name = "TruckImage" + String.Format("{0:d12}", (DateTime.Now.Ticks / 10) % 1000000000); ; picture.Cursor = Cursors.Hand; picture.Size = new Size(130, 90); picture.Location = new Point(drawingPoint.X - 5, drawingPoint.Y + 24); picture.Image = Resources.truck2; picture.SizeMode = PictureBoxSizeMode.StretchImage; if (paneld == null) { picture.MouseClick += pictureTable1; panel.Controls.Add(picture); mainpanel.Controls.Add(panel); } else { picture.MouseClick += Picture_MouseClick1; panel.Controls.Add(picture); /* Calculations */ int x = 0, y = 0; if (this.Width > mainpanel2.Width) { x = (mainpanel2.Width / this.Width) * pointerPoint.X; } else { x = (this.Width / mainpanel2.Width) * pointerPoint.X; } if (this.Height > mainpanel2.Height) { y = (mainpanel2.Height / this.Height) * pointerPoint.Y; } else { y = (this.Height / mainpanel2.Height) * pointerPoint.Y; } panel.Location = new Point(x, y); mainpanel2.Controls.Add(panel); } } private void pictureTable1(object sender, MouseEventArgs e) { var cellPos = GetRowColIndex(mainpanel, mainpanel.PointToClient(Cursor.Position)); if (e.Button == MouseButtons.Right) { drawingPoint = new Point(MousePosition.X, MousePosition.Y); NOdRIVER.Show(drawingPoint); try { currentSender = sender as PictureBox; Control c = mainpanel.GetControlFromPosition(cellPos.Value.X, cellPos.Value.Y); selectedLicense = c.Name.ToString(); } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); } } } Point? GetRowColIndex(TableLayoutPanel tlp, Point point) { if (point.X > tlp.Width || point.Y > tlp.Height) return null; int w = tlp.Width; int h = tlp.Height; int[] widths = tlp.GetColumnWidths(); int i; for (i = widths.Length - 1; i >= 0 && point.X < w; i--) w -= widths[i]; int col = i + 1; int[] heights = tlp.GetRowHeights(); for (i = heights.Length - 1; i >= 0 && point.Y < h; i--) h -= heights[i]; int row = i + 1; return new Point(col, row); } private void Picture_MouseClick1(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { drawingPoint = new Point(MousePosition.X, MousePosition.Y); NOdRIVER.Show(drawingPoint); try { currentSender = sender as PictureBox; Control c = sender as Control; selectedLicense = c.Parent.Name; } catch { } } } private void Picture_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { drawingPoint = new Point(MousePosition.X, MousePosition.Y); truckMenu.Show(drawingPoint); try { currentSender = sender as PictureBox; Control c = sender as Control; selectedLicense = c.Parent.Name; } catch { } } } private async void TruckManagement_Load(object sender, EventArgs e) { mainpanel.Visible = false; mainpanel2.Visible = false; mainpanel.Controls.Clear(); mainpanel2.Controls.Clear(); Task task = new Task(loadTrucks); holding.Visible = true; task.Start(); int result = await task; if (result != 1) { string title = "Error occurred"; string message = "An error occurred while loading trucks, please try again later!!!"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { try { foreach (var truck in truckWithD) { creation(truck.Value, truck.Key); } foreach (var truck in trucksWithoutD) { Notcreation(truck.Value, truck.Key); } } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); } } holding.Visible = false; mainpanel.Dock = DockStyle.Top; mainpanel2.BringToFront(); mainpanel2.Dock = DockStyle.Fill; mainpanel.Visible = true; mainpanel2.Visible = true; } private void Mainpanel_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { pointerPoint = new Point(MousePosition.X, MousePosition.Y); mainMenu.Show(pointerPoint); mainpanel.Visible = true; } } private async void ToolStripMenuItem2_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(selectedLicense)) { string titleD = "Confirmation"; string messageD = "Are you sure you would like to remove this truck ?"; Confirmation confirmation = new Confirmation(titleD, messageD); confirmation.BringToFront(); confirmation.ShowDialog(); if (confirmation.DialogResult == DialogResult.Yes) { Task task = new Task(() => deleteTruck(selectedLicense)); holding.Visible = true; task.Start(); int result = await task; if (result == 1) { removeTruckPanelStyle(); } else { string title = "Error occurred"; string message = "An error occurred while trying to remove truck, please try again later!!"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } holding.Visible = false; } } currentSender = null; } public void removeTruckPanelStyle() { if (truckWithD.ContainsKey(selectedLicense)) { truckWithD.Remove(selectedLicense); Control c = mainpanel.Controls[selectedLicense]; mainpanel.Controls.Remove(c); c = mainpanel2.Controls[selectedLicense]; mainpanel2.Controls.Remove(c); } else if (trucksWithoutD.ContainsKey(selectedLicense)) { trucksWithoutD.Remove(selectedLicense); Control c = mainpanel2.Controls[selectedLicense]; mainpanel2.Controls.Remove(c); c = mainpanel.Controls[selectedLicense]; mainpanel.Controls.Remove(c); } } public int deleteTruck(string truck) { try { if (truckWithD.ContainsKey(selectedLicense)) { cn.Open(); cm = new SqlCommand("Declare @driverOFT nvarchar(50) = (Select driver from tblTrucks where truckID = @truckID where branchID = @branchID) " + "Update tblDrivers set status = 'UNASSIGNED' where driverID = @driverOFT and branchID = @branchID " + "Delete from tblTrucks where truckID = @truckID and branchID = @branchID " + "Insert Into tblTruck_DriverMapping(truckID, driverID, dateEntry, operation, countID) values " + "(@truckID, @driverOFT, @dateEntry, @operation, @countID) ", cn); cm.Parameters.AddWithValue("@truckID", truck); cm.Parameters.AddWithValue("@dateEntry", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")); cm.Parameters.AddWithValue("@operation", "TRUCK REMOVAL"); cm.Parameters.AddWithValue("@countID", Settings.Default.BranchID + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); //cm.Parameters.AddWithValue("@driverOFT", "NOT ASSIGNED"); cm.ExecuteNonQuery(); cn.Close(); } else if (trucksWithoutD.ContainsKey(selectedLicense)) { cn.Open(); cm = new SqlCommand("Delete from tblTrucks where truckID = @truckID and branchID = @branchID " + "Insert Into tblTruck_DriverMapping(truckID, driverID, dateEntry, operation, countID) values " + "(@truckID, @driverOFT, @dateEntry, @operation, @countID) ", cn); cm.Parameters.AddWithValue("@truckID", truck); cm.Parameters.AddWithValue("@dateEntry", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")); cm.Parameters.AddWithValue("@operation", "TRUCK REMOVAL"); cm.Parameters.AddWithValue("@countID", Settings.Default.BranchID + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@driverOFT", "NOT ASSIGNED"); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); cn.Close(); } return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } public int removeDriver(string truck) { try { cn.Open(); cm = new SqlCommand("Declare @driverOFT varchar(50) = (Select driver from tblTrucks where truckID = @truckID and branchID = @branchID) " + "Update tblDrivers set status = 'UNASSIGNED' where driverID = @driverOFT and branchID = @branchID " + "Update tblTrucks set driver = null where truckID = @truckID and branchID = @branchID " + "Insert Into tblTruck_DriverMapping(truckID, driverID, dateEntry, operation, countID) values " + "(@truckID, @driverOFT, @dateEntry, @operation, @countID) ", cn); cm.Parameters.AddWithValue("@truckID", truck); cm.Parameters.AddWithValue("@dateEntry", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff")); cm.Parameters.AddWithValue("@operation", "DRIVER RELEASED FROM TRUCK"); cm.Parameters.AddWithValue("@countID", Settings.Default.BranchID + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); cn.Close(); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private async void removeDRIVERToolStripMenuItem_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(selectedLicense)) { string titleC = "Confirmation"; string messageC = "Are you sure you would like to remove the designated driver for this truck ?"; Confirmation confirmation = new Confirmation(titleC, messageC); confirmation.BringToFront(); confirmation.ShowDialog(); if (confirmation.DialogResult == DialogResult.Yes) { Task task = new Task(() => removeDriver(selectedLicense)); holding.Visible = true; task.Start(); int result = await task; if (result == 1) { string title = "Success"; string message = "Driver unassigned from truck successfully"; NoAction action = new NoAction(title, message); action.BringToFront(); action.ShowDialog(); currentSender.Image = Resources.truck2; currentSender.MouseClick += Picture_MouseClick1; foreach (var value in truckWithD) { if (value.Key == selectedLicense) { trucksWithoutD.Add(selectedLicense, value.Value); truckWithD.Remove(selectedLicense); holding.Visible = false; return; } } } else { string title = "Error occurred"; string message = "An error occurred while unassigning driver from truck, please try again later !!"; NoAction action = new NoAction(title, message); action.BringToFront(); action.ShowDialog(); } holding.Visible = false; } } currentSender = null; } private void toolStripMenuItem1_Click(object sender, EventArgs e) { string title = "Confirmation"; string message = "Are you sure you would like to assign a new driver to this truck, the driver assigned would be the designated driver for this truck."; Confirmation confirmation = new Confirmation(title, message); confirmation.BringToFront(); confirmation.ShowDialog(); if (confirmation.DialogResult == DialogResult.Yes) { DriverAssignment assignment = new DriverAssignment(selectedLicense); assignment.BringToFront(); assignment.ShowDialog(); if (assignment.DialogResult == DialogResult.OK) { currentSender.Image = Resources.truck; currentSender.MouseClick += Picture_MouseClick; foreach (var value in trucksWithoutD) { if (value.Key == selectedLicense) { truckWithD.Add(selectedLicense, value.Value); trucksWithoutD.Remove(selectedLicense); return; } } } } currentSender = null; } private void INVENTORYToolStripMenuItem_Click(object sender, EventArgs e) { if (trucksWithoutD.ContainsKey(selectedLicense)) { TruckInventory truckInventory = new TruckInventory(selectedLicense); truckInventory.BringToFront(); truckInventory.ShowDialog(); } else if (truckWithD.ContainsKey(selectedLicense)) { TruckInventory truckInventory = new TruckInventory(selectedLicense, true); truckInventory.BringToFront(); truckInventory.ShowDialog(); } currentSender = null; } private void ASSIGNMENTSToolStripMenuItem_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(selectedLicense)) { if (trucksWithoutD.ContainsKey(selectedLicense)) { Assignments assignments = new Assignments(selectedLicense); assignments.BringToFront(); assignments.ShowDialog(); } else if (truckWithD.ContainsKey(selectedLicense)) { Assignments assignments = new Assignments(selectedLicense, true); assignments.BringToFront(); assignments.ShowDialog(); } } currentSender = null; } } }