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 NewTruck : Form { public string licensePlate; public string truckID; public bool created = false; public bool ready = false; SqlConnection cn; SqlCommand cm; DatabaseConn conn = new DatabaseConn(); SqlDataReader dr; public NewTruck() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); } private void BtnExit_MouseEnter(object sender, EventArgs e) { btnExit.BackColor = Color.Crimson; } private void BtnExit_MouseLeave(object sender, EventArgs e) { btnExit.BackColor = Color.FromArgb(20, 158, 132); } private void BtnExit_Click(object sender, EventArgs e) { this.Close(); } private void License_TextChanged(object sender, EventArgs e) { licensePlate = trucklicense.Text; } public int loadDrivers() { try { cn.Open(); cm = new SqlCommand("Select driverID,firstname,surname from tblDrivers where branchID = @branchID and status = 'UNASSIGNED'", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { ComboboxItem combobox = new ComboboxItem(); combobox.Text = dr[1].ToString().ToUpper() + " " + dr[2].ToString().ToUpper(); combobox.Value = dr[0].ToString(); truckDrivers.Invoke(new Action (() => truckDrivers.Items.Add(combobox))); } dr.Close(); cn.Close(); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } public int addNewTruck() { string truckPlate = null, truckBrand = null, weight = null, driver = null; lbltruckID.Invoke(new Action(() => { try { truckID = lbltruckID.Text; truckPlate = trucklicense.Text; licensePlate = truckPlate; truckBrand = this.truckBrand.Text; weight = truckWeight.Text; driver = (truckDrivers.SelectedItem as ComboboxItem).Value.ToString(); } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); } })); if (!String.IsNullOrEmpty(truckID) && !String.IsNullOrEmpty(truckPlate) && !String.IsNullOrEmpty(truckBrand) && !String.IsNullOrEmpty(weight)) { cn.Open(); SqlTransaction transaction = cn.BeginTransaction(); try { if (!String.IsNullOrEmpty(driver)) { cm = new SqlCommand("Insert into tblTrucks (truckID,licensePlate,brand,driver,weight,branchID) values " + "(@truckID,@licensePlate,@brand,@driver,@weight,@branchID)", cn, transaction); cm.Parameters.AddWithValue("@truckID", truckID); cm.Parameters.AddWithValue("@licensePlate", truckPlate); cm.Parameters.AddWithValue("@brand", truckBrand); cm.Parameters.AddWithValue("@weight", weight); cm.Parameters.AddWithValue("@driver", driver); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); cm = new SqlCommand("Insert into tblTruck_DriverMapping (truckID,driverID,dateEntry,operation,countID) values " + "(@truckID,@driverID,@dateEntry,@operation,@countID)", cn, transaction); cm.Parameters.AddWithValue("@truckID", truckID); cm.Parameters.AddWithValue("@driverID", driver); cm.Parameters.AddWithValue("@dateEntry", DateTime.Now); cm.Parameters.AddWithValue("@operation", "DRIVER ASSIGNED TO TRUCK"); cm.Parameters.AddWithValue("@countID", DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.ExecuteNonQuery(); cm = new SqlCommand("Update tblDrivers set status = @status where driverID = @driverID", cn, transaction); cm.Parameters.AddWithValue("@driverID", driver); cm.Parameters.AddWithValue("@status", "ASSIGNED"); cm.ExecuteNonQuery(); ready = true; } else { cm = new SqlCommand("Insert into tblTrucks (truckID,licensePlate,brand,weight,branchID) values " + "(@truckID,@licensePlate,@brand,@weight,@branchID)", cn, transaction); cm.Parameters.AddWithValue("@truckID", truckID); cm.Parameters.AddWithValue("@licensePlate", truckPlate); cm.Parameters.AddWithValue("@brand", truckBrand); cm.Parameters.AddWithValue("@weight", weight); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); } transaction.Commit(); cn.Close(); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); transaction.Rollback(); ready = false; cn.Close(); return 0; } } else { return 2; } } private async void NewTruck_Load(object sender, EventArgs e) { Task task = new Task(loadDrivers); task.Start(); await task; lbltruckID.Text = String.Format("{0:d12}", (DateTime.Now.Ticks / 10) % 1000000000); } private async void BtnAdd_Click(object sender, EventArgs e) { ready = false; Task task = new Task(addNewTruck); btnAdd.Visible = false; holding.Visible = true; task.Start(); int result = await task; string title = null, message = null; switch (result) { case 1: if (ready) { title = "Success"; message = "Truck created successfully you can now assign delivery orders to this truck"; } else { title = "Success"; message = "Truck created successfully, before you can now assign delivery orders to this truck you must set a designated " + "driver for this truck"; } NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); created = true; this.DialogResult = DialogResult.OK; this.Close(); break; case 2: title = "Truck Not createdd"; message = "To add a new truck you must make sure all fields with red asterisks are filled"; NoAction noAction1 = new NoAction(title, message); noAction1.BringToFront(); noAction1.ShowDialog(); foreach (Control c in panel2.Controls) { if (c.Text == "*") { c.Visible = true; } } break; case 0: title = "Error occurred"; message = "An error occurred while creating truck, please try again later"; NoAction noAction2 = new NoAction(title, message); noAction2.BringToFront(); noAction2.ShowDialog(); break; } btnAdd.Visible = true; holding.Visible = false; } } }