/// <summary> /// Create a new inventory record for an implement. /// </summary> /// <param name="details"></param> /// <param name="jobsiteId"></param> /// <param name="authUserId"></param> /// <param name="_context"></param> /// <returns>The ID of the created inventory record.</returns> public int InsertIntoInventory(ImplementDetails details, int jobsiteId, long authUserId, GETContext _context) { int result = -1; try { GET_INVENTORY newGETinInventory = new GET_INVENTORY(); newGETinInventory.get_auto = details.Id; newGETinInventory.jobsite_auto = jobsiteId; newGETinInventory.status_auto = (int)GETInterfaces.Enum.InventoryStatus.Ready_for_Use; newGETinInventory.modified_date = DateTime.Now; newGETinInventory.modified_user = (int)authUserId; newGETinInventory.ltd = (int)details.ImplementHoursAtSetup; newGETinInventory.workshop_auto = null; _context.GET_INVENTORY.Add(newGETinInventory); int _changesSaved = _context.SaveChanges(); if (_changesSaved > 0) { result = newGETinInventory.inventory_auto; } } catch (Exception ex1) { } return(result); }
/// <summary> /// Update an existing Inventory record for an implement. /// </summary> /// <param name="details"></param> /// <param name="jobsiteId"></param> /// <param name="authUserId"></param> /// <param name="_context"></param> /// <returns>The ID of the updated inventory record.</returns> public int UpdateInventoryRecord(ImplementDetails details, int jobsiteId, long authUserId, GETContext _context) { int result = -1; try { GET_INVENTORY inventoryRecord = _context.GET_INVENTORY.Where(w => w.get_auto == details.Id).FirstOrDefault(); if (inventoryRecord != null) { inventoryRecord.modified_date = DateTime.Now; inventoryRecord.modified_user = (int)authUserId; inventoryRecord.ltd = (int)details.ImplementHoursAtSetup; int _changesSaved = _context.SaveChanges(); if (_changesSaved > 0) { result = inventoryRecord.inventory_auto; } } } catch (Exception ex1) { } return(result); }
/// <summary> /// Update all components for an implement when the SMU is changed. /// </summary> /// <param name="_gContext"></param> /// <param name="GETAuto"></param> /// <param name="SMU_difference"></param> /// <param name="eventAuto"></param> /// <param name="eventDate"></param> /// <returns></returns> public bool updateComponentsForGET(GETContext _gContext, long GETAuto, int SMU_difference, long eventAuto, DateTime eventDate) { bool result = false; var GETComponents = _gContext.GET_COMPONENT .Where(gc => gc.get_auto == GETAuto && gc.active == true) .ToArray(); // Handle case when there are no components. if (GETComponents == null) { return(true); } else if (GETComponents.Length == 0) { return(true); } for (int n = 0; n < GETComponents.Count(); n++) { int GETComponentAuto = GETComponents[n].get_component_auto; var previousComponentEvent = findPreviousComponentEvent(_gContext, GETComponentAuto, eventDate); if (previousComponentEvent == null) { return(false); } int newLTD = (previousComponentEvent.ltd + SMU_difference); var componentEvent = createComponentEvent(GETComponentAuto, newLTD, eventAuto, 0); _gContext.GET_EVENTS_COMPONENT.Add(componentEvent); int _changesSavedComponentEvent = _gContext.SaveChanges(); if (_changesSavedComponentEvent == 0) { return(false); } else { result = true; } } return(result); }
/// <summary> /// Update all implements for an equipment when the SMU is changed. /// </summary> /// <param name="_gContext"></param> /// <param name="equipmentAuto"></param> /// <param name="SMU_difference"></param> /// <param name="eventAuto"></param> /// <param name="eventDate"></param> /// <returns></returns> public bool updateImplementsForEquipment(GETContext _gContext, long equipmentAuto, int SMU_difference, long eventAuto, DateTime eventDate) { bool result = false; var GETs = _gContext.GET .Where(g => g.equipmentid_auto == equipmentAuto && g.on_equipment == true) .ToArray(); // No implements to update, so just return back. if (GETs == null) { return(true); } else if (GETs.Length == 0) { return(true); } for (int n = 0; n < GETs.Count(); n++) { int GETAuto = GETs[n].get_auto; var previousImplementEvent = findPreviousImplementEvent(_gContext, GETAuto, eventDate); if (previousImplementEvent == null) { return(false); } int newLTD = (previousImplementEvent.ltd + SMU_difference); var implementEvent = createImplementEvent(GETAuto, newLTD, eventAuto); _gContext.GET_EVENTS_IMPLEMENT.Add(implementEvent); int _changesSavedImplementEvent = _gContext.SaveChanges(); if (_changesSavedImplementEvent == 0) { return(false); } else { result = updateComponentsForGET(_gContext, GETAuto, SMU_difference, eventAuto, eventDate); } } return(result); }
/// <summary> /// Update all implements and components with new SMU. /// </summary> /// <param name="_gContext"></param> /// <param name="equipmentAuto"></param> /// <param name="meter_reading"></param> /// <param name="userId"></param> /// <param name="eventDate"></param> /// <param name="comment"></param> /// <param name="cost"></param> /// <returns></returns> public long cascadeUpdateWithNewSMU(GETContext _gContext, long equipmentAuto, int meter_reading, long userId, DateTime eventDate, string comment, decimal cost) { long ERROR_CODE = -1; long result = ERROR_CODE; // Find the previous equipment event. var previousEquipmentEvent = findPreviousEquipmentEvent(_gContext, equipmentAuto, eventDate); if (previousEquipmentEvent == null) { return(ERROR_CODE); } int SMU_difference = meter_reading - previousEquipmentEvent.smu; int newLTD = (previousEquipmentEvent.ltd + SMU_difference); if (SMU_difference > 0) { // Create an event record. int actionAuto = (int)GETActionType.Equipment_SMU_Changed; var SMUChangedEvent = createGETEvent(userId, actionAuto, eventDate, comment, cost); _gContext.GET_EVENTS.Add(SMUChangedEvent); int _changesSaved = _gContext.SaveChanges(); if (_changesSaved > 0) { // Create an equipment event. var equipmentEvent = createEquipmentEvent(SMUChangedEvent.events_auto, equipmentAuto, meter_reading, newLTD); _gContext.GET_EVENTS_EQUIPMENT.Add(equipmentEvent); int _changesSavedEquipmentEvent = _gContext.SaveChanges(); if (_changesSavedEquipmentEvent > 0) { // Create implement events bool update_success = updateImplementsForEquipment(_gContext, equipmentAuto, SMU_difference, SMUChangedEvent.events_auto, eventDate); if (update_success) { result = SMUChangedEvent.events_auto; } else { // Failed to save Implement or Component events. SMUChangedEvent.recordStatus = 1; result = ERROR_CODE; } } else { // Failed to save Equipment event. SMUChangedEvent.recordStatus = 1; result = ERROR_CODE; } } else { // Failed to save new GET_EVENT. SMUChangedEvent.recordStatus = 1; result = ERROR_CODE; } } else if (SMU_difference == 0) { result = 0; } return(result); }
public int CreateNewMeasurementPoints(int get_auto, GETContext _context) { int result = 0; int _changesSaved = 0; // Check if there are any records already for measurement points. var existingRecords = _context.GET_COMPONENT .Where(w => w.get_auto == get_auto && w.active == true) .ToList(); // GET record var GT = _context.GET.Find(get_auto); long implement_auto = GT.implement_auto.Value; List <int> comparttypes = (from ic in _context.GET_IMPLEMENT_COMPARTTYPE join g in _context.GET on ic.implement_auto equals g.implement_auto where g.get_auto == get_auto select ic.comparttype_auto ).ToList(); List <string> schematics = _context.LU_IMPLEMENT.Find(implement_auto) .schematic_auto_multiple .Split('_') .ToList(); var measurementPoints = (from gsc in _context.GET_SCHEMATIC_COMPONENT join lc in _context.LU_COMPART_TYPE on gsc.comparttype_auto equals lc.comparttype_auto where comparttypes.Contains(gsc.comparttype_auto) && schematics.Contains(gsc.schematic_auto.ToString()) && gsc.active == true select new { Id = lc.comparttype, SchematicComponent = gsc.schematic_component_auto }).ToList(); // There are no records, i.e. this is setting up components for a new implement. if (existingRecords.Count == 0) { for (int i = 0; i < measurementPoints.Count; i++) { GET_COMPONENT gc = new GET_COMPONENT(); gc.get_auto = get_auto; gc.cmu = 0; gc.install_date = GT.created_date; gc.ltd_at_setup = (int)GT.impsetup_hours.Value; gc.req_measure = false; gc.initial_length = 0; gc.worn_length = 0; gc.price = 0; gc.part_no = ""; gc.active = true; gc.schematic_component_auto = measurementPoints[i].SchematicComponent; _context.GET_COMPONENT.Add(gc); } // Save changes try { _changesSaved = _context.SaveChanges(); if (_changesSaved > 0) { result = 1; } } catch (Exception ex1) { result = -1; } } return(result); }
public async Task <Tuple <int, string> > CreateNewImplementTemplate(NewImplementTemplateViewModel newImplementTemplate) { List <LU_COMPART_TYPE> components = new List <LU_COMPART_TYPE>(); // New components that need to be added to the database // Check if a template with this name exists for the same category and customer bool existingTemplate = _context.LU_IMPLEMENT.Where(i => i.implementdescription.ToLower() == newImplementTemplate.TemplateName.ToLower() && i.CustomerId == newImplementTemplate.CustomerId && i.implement_category_auto == (int)newImplementTemplate.ImplementCategory).Any(); if (existingTemplate) { return(Tuple.Create(-1, "A template with this name already exists. ")); } // Create new implement LU_IMPLEMENT template = new LU_IMPLEMENT { CustomerId = newImplementTemplate.CustomerId, implementdescription = newImplementTemplate.TemplateName, implement_category_auto = (int)newImplementTemplate.ImplementCategory, parentID = 0 }; _context.LU_IMPLEMENT.Add(template); // Create any new components that don't exist yet, and then add all new and existing components to list foreach (ImplementComponentTypeViewModel componentType in newImplementTemplate.ComponentTypes) { if (componentType.Id == 0) // If ID = 0 then it is a new component we need to create { // Generate a stupid second ID we don't need. string id = componentType.Name; if (id.Length > 10) { id = id.Substring(0, 10); } // GET-68 if (containsReservedName(id)) { return(Tuple.Create(-1, "Unable to create component as it contains a reserved keyword!")); } // Work out the component type category for GET or Dump Body short componentCategory; if (newImplementTemplate.ImplementCategory == ImplementCategory.GET) { componentCategory = (int)ComponentTypeCategory.GET; } else { componentCategory = (int)ComponentTypeCategory.DumpBody; } LU_COMPART_TYPE newComponent = new LU_COMPART_TYPE() { comparttypeid = id, comparttype = componentType.Name, _protected = false, system_auto = componentCategory }; components.Add(newComponent); _context.LU_COMPART_TYPE.Add(newComponent); } else // Add existing components to list, so we can map them to this implement template { components.Add(_context.LU_COMPART_TYPE.Where(c => c.comparttype_auto == componentType.Id).First()); } } try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to create template and new component types. Exception: " + e.Message)); } // If everything saved successfully, map all the components to the new template foreach (LU_COMPART_TYPE comp in components) { _context.GET_IMPLEMENT_COMPARTTYPE.Add(new GET_IMPLEMENT_COMPARTTYPE() { comparttype_auto = comp.comparttype_auto, implement_auto = template.implement_auto }); } try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to map new component types to the new template. Exception: " + e.Message)); } // Map the models to the template foreach (int modelId in newImplementTemplate.EquipmentModels) { _context.GET_IMPLEMENT_MAKE_MODEL.Add(new GET_IMPLEMENT_MAKE_MODEL() { implement_auto = template.implement_auto, model_auto = modelId }); } try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to map the new template to it's models. Exception: " + e.Message)); } return(Tuple.Create((int)template.implement_auto, "Template created successfully. ")); }