public ActionResult Update([DataSourceRequest] DataSourceRequest request, VmWarehouse warehouse) { var isEditable = CurrentUser.UserLevel == 3; if (!isEditable) { ModelState.AddModelError("Update", "Current user does not have permission to update warehouses."); } if (ModelState.IsValid) { var dbWarehouse = Db.Warehouses.Find(warehouse.WarehouseId); if (dbWarehouse != null) { dbWarehouse.Description = warehouse.Description; dbWarehouse.IsActive = warehouse.IsActive; try { Db.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("Update Warehouse", ServerError.GetErrorFromException(e).ExceptionMsg); } } } return(Json(new[] { warehouse }.ToDataSourceResult(request, ModelState))); }
public ActionResult Update([DataSourceRequest] DataSourceRequest request, VmTruck truck) { var isEditable = CurrentUser.UserLevel == 3; if (!isEditable) { ModelState.AddModelError("Update", "Current user does not have permission to update trucks."); } if (ModelState.IsValid) { var dbTruck = Db.Trucks.Find(truck.TruckId); if (dbTruck != null) { dbTruck.IsActive = truck.IsActive; dbTruck.TruckDescription = truck.TruckDescription; dbTruck.NextInspectionDate = truck.NextInspectionDate; dbTruck.NextMaintenanceDate = truck.NextMaintenanceDate; try { Db.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("Update truck", ServerError.GetErrorFromException(e).ExceptionMsg); } } } return(Json(new[] { truck }.ToDataSourceResult(request, ModelState))); }
public ActionResult Create([DataSourceRequest] DataSourceRequest request, VmWarehouse warehouse) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Create", "Current user does not have permission to create a warehouse."); } if (ModelState.IsValid) { try { var newDbWarehouse = new Warehouse() { WarehouseCode = warehouse.WarehouseCode, Description = warehouse.Description, IsActive = true }; Db.Warehouses.Add(newDbWarehouse); Db.SaveChanges(); warehouse.WarehouseId = newDbWarehouse.WarehouseId; } catch (Exception e) { ModelState.AddModelError("Create Warehouse", ServerError.GetErrorFromException(e).ExceptionMsg); } } return(Json(new[] { warehouse }.ToDataSourceResult(request, ModelState))); }
public ActionResult Update([DataSourceRequest] DataSourceRequest request, VmUserLogin userLogin) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Create", "Current user does not have permission to update a user."); } if (ModelState.IsValid) { var dbUserLogin = Db.UserLogins.Find(userLogin.UserLoginId); if (dbUserLogin != null) { dbUserLogin.Email = userLogin.Email; dbUserLogin.AccessLevelId = userLogin.AccessLevelId; dbUserLogin.UserName = userLogin.UserName; dbUserLogin.IsActive = userLogin.IsActive; try { Db.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("Update User Login", ServerError.GetErrorFromException(e).ExceptionMsg); } } } return(Json(new[] { userLogin }.ToDataSourceResult(request, ModelState))); }
public ActionResult DeleteOrder(VmKoOrder order) { try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Entry) { throw new ServerException("Save Order: User does not have permission to save an order"); } var dbOrder = Db.Orders.FirstOrDefault(o => o.OrderId == order.OrderId); if (dbOrder != null) { if (dbOrder.LoadId != null) { var load = Db.Loads.FirstOrDefault(l => l.LoadId == dbOrder.LoadId); if (load != null) { load.ModifiedByUserLoginId = CurrentUser.UserLoginId; load.ModifiedDate = DateTime.Now; } } Db.Orders.Remove(dbOrder); Db.SaveChanges(); } } catch (Exception e) { return(ReturnOrder(order, ServerError.GetErrorFromException(e).ExceptionMsg)); } return(ReturnOrder(order, $"Order# {order.OrderNumber} has been deleted")); }
public ActionResult Create([DataSourceRequest] DataSourceRequest request, VmTruck truck) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Create", "Current user does not have permission to create a truck."); } if (ModelState.IsValid) { try { var newDbTruck = new Truck() { NextInspectionDate = truck.NextInspectionDate, NextMaintenanceDate = truck.NextMaintenanceDate, TruckDescription = truck.TruckDescription, IsActive = true, }; Db.Trucks.Add(newDbTruck); Db.SaveChanges(); truck.TruckId = newDbTruck.TruckId; } catch (Exception e) { ModelState.AddModelError("Create Truck", ServerError.GetErrorFromException(e).ExceptionMsg); } } return(Json(new[] { truck }.ToDataSourceResult(request, ModelState))); }
public ActionResult Create([DataSourceRequest] DataSourceRequest request, VmUserLogin userLogin) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Create", "Current user does not have permission to create a user."); } if (ModelState.IsValid) { try { var returnMsgObj = new ObjectParameter("responseMessage", typeof(string)); var returnIntObj = new ObjectParameter("newId", typeof(int)); Db.uspAddUser(userLogin.UserId, userLogin.UserId, "TempPassword9", userLogin.Email, returnMsgObj, returnIntObj); var outMsg = returnMsgObj.Value.ToString(); if (outMsg != "Success") { ModelState.AddModelError("Create User Login", outMsg); } else { userLogin.UserLoginId = (int)returnIntObj.Value; } } catch (Exception e) { ModelState.AddModelError("Create User Login", ServerError.GetErrorFromException(e).ExceptionMsg); } } return(Json(new[] { userLogin }.ToDataSourceResult(request, ModelState))); }
public ActionResult Read([DataSourceRequest] DataSourceRequest request) { DataSourceResult userLogins; try { var query = from h in Db.UserLogins select h; userLogins = query.Select(l => new VmUserLogin() { AccessLevelId = l.AccessLevelId, Email = l.Email, UserName = l.UserName, UserId = l.UserId, UserLoginId = l.UserLoginId, IsActive = l.IsActive, }).ToDataSourceResult(request); } catch (Exception e) { userLogins = new DataSourceResult() { Errors = "UserLogins_Read error: " + ServerError.GetErrorFromException(e).ExceptionMsg }; } return(Json(userLogins, JsonRequestBehavior.AllowGet)); }
public ActionResult DeleteLoad(long loadId) { try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Dispatcher) { throw new ServerException("Delete Load: User does not have permission to delete a load"); } var dbLoad = Db.Loads.Find(loadId) ?? throw new InvalidOperationException(); foreach (var order in dbLoad.Orders) { order.LoadId = null; } Db.SaveChanges();//Remove orders from load Db.Loads.Remove(dbLoad); Db.SaveChanges(); } catch (Exception e) { return(ReturnLoad(new VmKoLoad(), ServerError.GetErrorFromException(e).ExceptionMsg)); } return(ReturnLoad(new VmKoLoad(), $"Load {loadId} has been deleted")); }
public ActionResult SaveScreenSize(VmKoScreenSize screenSize) { try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Entry) { throw new ServerException("Save Order: User does not have permission to save screen size"); } var dbRowSize = Db.UserSettings.FirstOrDefault(o => o.UserLoginId == CurrentUser.UserLoginId && o.SettingCode == "OrdersPageSize"); var dbColSize = Db.UserSettings.FirstOrDefault(o => o.UserLoginId == CurrentUser.UserLoginId && o.SettingCode == "LoadsVisible"); if (dbRowSize == null) { Db.UserSettings.Add(new DbContext.UserSetting() { SettingCode = "OrdersPageSize", SettingValue = screenSize.OrdersPageSize.ToString(), UserLoginId = CurrentUser.UserLoginId }); } else { dbRowSize.SettingValue = screenSize.OrdersPageSize.ToString(); } if (dbColSize == null) { Db.UserSettings.Add(new DbContext.UserSetting() { SettingCode = "LoadsVisible", SettingValue = screenSize.VisibleLoadCols.ToString(), UserLoginId = CurrentUser.UserLoginId }); } else { dbColSize.SettingValue = screenSize.VisibleLoadCols.ToString(); } Db.SaveChanges(); } catch (Exception e) { return(ReturnScreenSize(screenSize, ServerError.GetErrorFromException(e).ExceptionMsg)); } return(ReturnScreenSize(screenSize, "Screen Size# has been saved")); }
public ActionResult Delete(int id) { var rtnString = ""; var isEditable = CurrentUser.IsAdmin; if (!isEditable) { rtnString += "Current user does not have permission to delete a trailer."; } if (rtnString.IsNullOrEmpty()) { var dbTrailer = Db.Trailers.Find(id); if (dbTrailer != null) { //if (dbTrailer.Loads.Count > 0) //{ // rtnString += "This trailer cannot be deleted as they have been assigned to loads, set the trailer 'Inactive' instead."; //} //else //{ Db.Trailers.Remove(dbTrailer); try { Db.SaveChanges(); } catch (Exception e) { rtnString += ServerError.GetErrorFromException(e).ExceptionMsg; } //} } } if (rtnString.IsNullOrEmpty()) { rtnString += $"Trailer {id} has been deleted"; } var rtnJson = Json(rtnString.ToJsonString(), JsonRequestBehavior.AllowGet); return(rtnJson); }
public ActionResult Read([DataSourceRequest] DataSourceRequest request) { DataSourceResult trailers; try { trailers = GetDataSourceResults.GetTrailers(request); } catch (Exception e) { trailers = new DataSourceResult() { Errors = "Trucks_Read error: " + ServerError.GetErrorFromException(e).ExceptionMsg }; } return(Json(trailers, JsonRequestBehavior.AllowGet)); }
public ActionResult Read([DataSourceRequest] DataSourceRequest request) { DataSourceResult warehouses; try { warehouses = GetDataSourceResults.GetWarehouses(request); } catch (Exception e) { warehouses = new DataSourceResult() { Errors = "Warehouses_Read error: " + ServerError.GetErrorFromException(e).ExceptionMsg }; } return(Json(warehouses, JsonRequestBehavior.AllowGet)); }
public ActionResult CreateNewLoad(VmKoLoad load) { long newLoadId = 0; try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Dispatcher) { throw new ServerException("Create Load: User does not have permission to create a load"); } int?unitOfMeasure = null; var dbTrailer = Db.Trailers.Find(load.TrailerId); if (dbTrailer != null) { unitOfMeasure = dbTrailer.UnitOfMeasureId; } var dbLoad = new Load { CreatedByUserLoginId = CurrentUser.UserLoginId, DriverId = load.DriverId, LoadStatusId = 1, CreatedDate = DateTime.Now, LoadDate = load.LoadDate.Date, TrailerId = load.TrailerId, TruckId = load.TruckId, UnitOfMeasureId = unitOfMeasure }; Db.Loads.Add(dbLoad); Db.SaveChanges(); newLoadId = dbLoad.LoadId; } catch (Exception e) { return(ReturnLoad(load, ServerError.GetErrorFromException(e).ExceptionMsg)); } return(ReturnLoad(load, $"Load {newLoadId} has been saved")); }
public ActionResult ResetOrder(long orderId) { long oldLoadId = 0; var result = new VmDragDropResult(); try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Dispatcher) { throw new ServerException("Reset Order: User does not have permission to move orders."); } var dbOrder = Db.Orders.FirstOrDefault(o => o.OrderId == orderId); if (dbOrder != null) { oldLoadId = dbOrder.LoadId ?? 0; var previousLoad = Db.Loads.Find(oldLoadId); if (previousLoad != null) { previousLoad.ModifiedDate = DateTime.Now; previousLoad.ModifiedByUserLoginId = CurrentUser.UserLoginId; } dbOrder.LoadId = null; Db.SaveChanges(); result.LoadId = oldLoadId; result.OrderId = orderId; } } catch (Exception e) { result.Message = ServerError.GetErrorFromException(e).ExceptionMsg; return(Json(result.ToJsonString(), JsonRequestBehavior.AllowGet)); } result.Message = $"OrderId {orderId} has been removed from loadId {oldLoadId}"; return(Json(result.ToJsonString(), JsonRequestBehavior.AllowGet)); }
public ActionResult Create([DataSourceRequest] DataSourceRequest request, VmDriver driver) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Create", "Current user does not have permission to create a driver."); } if (ModelState.IsValid) { try { var newDbDriver = new Driver() { Email = driver.Email, FirstName = driver.FirstName, LastName = driver.LastName, IsActive = true, IsEmployee = driver.IsEmployee, LicenseExpireDate = driver.LicenseExpireDate, PhoneNumber = driver.PhoneNumber, TdgTrainingDate = driver.TdgTrainingDate, WhmisTrainingDate = driver.WhmisTrainingDate, }; Db.Drivers.Add(newDbDriver); Db.SaveChanges(); driver.DriverId = newDbDriver.DriverId; } catch (Exception e) { ModelState.AddModelError("Create Driver", ServerError.GetErrorFromException(e).ExceptionMsg); } } return(Json(new[] { driver }.ToDataSourceResult(request, ModelState))); }
public ActionResult Delete(int id) { var rtnString = ""; var isEditable = CurrentUser.IsAdmin; if (!isEditable) { rtnString += "Current user does not have permission to delete a driver."; } if (rtnString.IsNullOrEmpty()) { var dbDriver = Db.Drivers.Find(id); if (dbDriver != null) { Db.Drivers.Remove(dbDriver); try { Db.SaveChanges(); } catch (Exception e) { rtnString += ServerError.GetErrorFromException(e).ExceptionMsg; } } } if (rtnString.IsNullOrEmpty()) { rtnString += $"Driver {id} has been deleted"; } var rtnJson = Json(rtnString.ToJsonString(), JsonRequestBehavior.AllowGet); return(rtnJson); }
public ActionResult View_Read([DataSourceRequest] DataSourceRequest request, string viewName) { DataSourceResult result = null; try { switch (viewName) { case "Drivers": result = GetDataSourceResults.GetDrivers(request); break; case "Trucks": result = GetDataSourceResults.GetTrucks(request); break; case "Trailers": result = GetDataSourceResults.GetTrailers(request); break; case "Warehouses": result = GetDataSourceResults.GetWarehouses(request); break; } } catch (Exception e) { result = new DataSourceResult() { Errors = viewName + " read error: " + ServerError.GetErrorFromException(e).ExceptionMsg }; } var rtnObj = Json(result, JsonRequestBehavior.AllowGet); return(rtnObj); }
public ActionResult Update([DataSourceRequest] DataSourceRequest request, VmDriver driver) { var isEditable = CurrentUser.UserLevel == 3; if (!isEditable) { ModelState.AddModelError("Update", "Current user does not have permission to update drivers."); } if (ModelState.IsValid) { var dbDriver = Db.Drivers.Find(driver.DriverId); if (dbDriver != null) { dbDriver.IsActive = driver.IsActive; dbDriver.IsEmployee = driver.IsEmployee; dbDriver.LicenseExpireDate = driver.LicenseExpireDate; dbDriver.TdgTrainingDate = driver.TdgTrainingDate; dbDriver.WhmisTrainingDate = driver.WhmisTrainingDate; dbDriver.Email = driver.Email; dbDriver.PhoneNumber = driver.PhoneNumber; dbDriver.FirstName = driver.FirstName; dbDriver.LastName = driver.LastName; try { Db.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("Update Driver", ServerError.GetErrorFromException(e).ExceptionMsg); } } } return(Json(new[] { driver }.ToDataSourceResult(request, ModelState))); }
public ActionResult Create([DataSourceRequest] DataSourceRequest request, VmTrailer trailer) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Create", "Current user does not have permission to create a trailer."); } if (ModelState.IsValid) { try { var newDbTrailer = new Trailer() { TrailerTypeId = trailer.TrailerTypeId, TrailerDescription = trailer.TrailerDescription, CapacityKg = trailer.CapacityKg, Volume = trailer.Volume, AxlesDescription = trailer.AxlesDescription, UnitOfMeasureId = trailer.UnitOfMeasureId, IsActive = true, }; Db.Trailers.Add(newDbTrailer); Db.SaveChanges(); trailer.TrailerId = newDbTrailer.TrailerId; } catch (Exception e) { ModelState.AddModelError("Create Trailer", ServerError.GetErrorFromException(e).ExceptionMsg); } } return(Json(new[] { trailer }.ToDataSourceResult(request, ModelState))); }
public ActionResult Update([DataSourceRequest] DataSourceRequest request, VmTrailer trailer) { var isEditable = CurrentUser.IsAdmin; if (!isEditable) { ModelState.AddModelError("Update", "Current user does not have permission to update trailers."); } if (ModelState.IsValid) { var dbTrailer = Db.Trailers.Find(trailer.TrailerId); if (dbTrailer != null) { dbTrailer.IsActive = trailer.IsActive; dbTrailer.TrailerDescription = trailer.TrailerDescription; dbTrailer.AxlesDescription = trailer.AxlesDescription; dbTrailer.CapacityKg = trailer.CapacityKg; dbTrailer.TrailerTypeId = trailer.TrailerTypeId; dbTrailer.UnitOfMeasureId = trailer.UnitOfMeasureId; dbTrailer.Volume = trailer.Volume; try { Db.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("Update trailer", ServerError.GetErrorFromException(e).ExceptionMsg); } } } return(Json(new[] { trailer }.ToDataSourceResult(request, ModelState))); }
public ActionResult SaveEditedLoad(VmKoLoad load) { try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Dispatcher) { throw new ServerException($"Save Load: User does not have permission to save load {load.LoadId}"); } var dbLoad = Db.Loads.Find(load.LoadId); if (dbLoad == null) { throw new ServerException($"SaveEditedLoad: Cannot find load {load.LoadId}"); } dbLoad.DriverId = load.DriverId; dbLoad.TruckId = load.TruckId; dbLoad.LoadStatusId = load.LoadStatusId; dbLoad.LoadDate = load.LoadDate; var isChangeTrailer = dbLoad.TrailerId != load.TrailerId; var isReOrderItems = false; if (load.NewOrderSequence != null) { var currentOrderSequence = dbLoad.Orders.OrderBy(r => r.LoadSort).Select(o => o.OrderId).ToArray(); isReOrderItems = !currentOrderSequence.SequenceEqual(load.NewOrderSequence); } if (isReOrderItems) { var position = 1; foreach (var orderId in load.NewOrderSequence) { var dbOrder = dbLoad.Orders.FirstOrDefault(o => o.OrderId == orderId); if (dbOrder != null) { dbOrder.LoadSort = position; position++; } } } if (isChangeTrailer) { int?unitOfMeasure = null; var dbTrailer = Db.Trailers.Find(load.TrailerId); if (dbTrailer != null) { unitOfMeasure = dbTrailer.UnitOfMeasureId; } dbLoad.UnitOfMeasureId = unitOfMeasure; } dbLoad.TrailerId = load.TrailerId; dbLoad.ModifiedByUserLoginId = CurrentUser.UserLoginId; dbLoad.ModifiedDate = DateTime.Now; Db.SaveChanges(); } catch (Exception e) { return(ReturnLoad(load, ServerError.GetErrorFromException(e).ExceptionMsg)); } return(ReturnLoad(load, $"Load {load.LoadId} has been saved")); }
public ActionResult DropOrder(long orderId, long loadId) { var result = new VmDragDropResult() { LoadId = loadId, OrderId = orderId }; try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Dispatcher) { throw new ServerException("Drop Order: User does not have permission to move orders."); } var dbOrder = Db.Orders.FirstOrDefault(o => o.OrderId == orderId); if (dbOrder != null) { var isFromOrderList = dbOrder.LoadId == null; var isMovedFromAnotherLoad = !isFromOrderList && dbOrder.LoadId != loadId; if (isMovedFromAnotherLoad) { var previousLoad = Db.Loads.Find(dbOrder.LoadId); if (previousLoad != null) { previousLoad.ModifiedDate = DateTime.Now; previousLoad.ModifiedByUserLoginId = CurrentUser.UserLoginId; } } if (isFromOrderList || isMovedFromAnotherLoad) { var dbLoad = Db.Loads.Find(loadId); if (dbLoad != null) { if (dbLoad.LoadStatusId == 1) { dbOrder.LoadId = loadId; dbLoad.ModifiedDate = DateTime.Now; dbLoad.ModifiedByUserLoginId = CurrentUser.UserLoginId; var maxLoadSortNum = Db.Orders.Where(o => o.LoadId == dbOrder.LoadId).Max(o => o.LoadSort) ?? 0; dbOrder.LoadSort = maxLoadSortNum + 1; } } } Db.SaveChanges(); } } catch (Exception e) { result.Message = ServerError.GetErrorFromException(e).ExceptionMsg; return(Json(result.ToJsonString(), JsonRequestBehavior.AllowGet)); } result.Message = $"OrderId {orderId} has been moved to loadId {loadId}"; return(Json(result.ToJsonString(), JsonRequestBehavior.AllowGet)); }
public ActionResult SaveOrder(VmKoOrder order) { try { if (CurrentUser.UserLevel < (int)Enums.AccessLevel.Entry) { throw new ServerException("Save Order: User does not have permission to save an order"); } var dbOrder = Db.Orders.FirstOrDefault(o => o.OrderId == order.OrderId); if (dbOrder != null) { var isOnLoad = dbOrder.LoadId != null; if (isOnLoad) { var dbLoad = Db.Loads.Find(dbOrder.LoadId); if (dbLoad != null) { dbLoad.ModifiedDate = DateTime.Now; dbLoad.ModifiedByUserLoginId = CurrentUser.UserLoginId; } } dbOrder.CustAddress = order.CustomerAddress; dbOrder.CustomerName = order.CustomerName; dbOrder.OrderNumber = order.OrderNumber; dbOrder.DeliveryDate = order.DeliveryDate; dbOrder.PickupDate = order.PickupDate; dbOrder.Destination = order.Destination; dbOrder.WarehouseId = order.WarehouseId; dbOrder.IsDangerousGoods = order.IsDangerousGoods; dbOrder.IsCustomerPickup = order.IsCustomerPickup; dbOrder.Notes = order.Notes; dbOrder.Volume = order.Volume; dbOrder.UnitOfMeasureId = order.UnitOfMeasureId; dbOrder.WeightKg = order.WeightKg; dbOrder.ModifiedDate = DateTime.Now; dbOrder.ModifiedByUserLoginId = CurrentUser.UserLoginId; Db.SaveChanges(); } else {// add it;) var newOrder = new Order() { OrderNumber = order.OrderNumber, CreatedDate = DateTime.Now, CreatedByUserLoginId = CurrentUser.UserLoginId }; Db.Orders.Add(newOrder); Db.SaveChanges(); order.OrderId = newOrder.OrderId; order.LastChangeDate = newOrder.CreatedDate; order.UserName = CurrentUser.DisplayName; } } catch (Exception e) { return(ReturnOrder(order, ServerError.GetErrorFromException(e).ExceptionMsg)); } return(ReturnOrder(order, $"Order# {order.OrderNumber} has been saved")); }