/// <summary>
        /// Create rental request rotation list
        /// </summary>
        /// <param name="item"></param>
        /// <response code="201">Rental Request Rotation List created</response>
        public virtual IActionResult RentalrequestrotationlistsPostAsync(RentalRequestRotationList item)
        {
            if (item != null)
            {
                AdjustRecord(item);

                bool exists = _context.RentalRequestRotationLists.Any(a => a.Id == item.Id);

                if (exists)
                {
                    _context.RentalRequestRotationLists.Update(item);
                }
                else
                {
                    // record not found
                    _context.RentalRequestRotationLists.Add(item);
                }

                // save the changes
                _context.SaveChanges();

                return(new ObjectResult(new HetsResponse(item)));
            }

            // no record to insert
            return(new ObjectResult(new HetsResponse("HETS-04", ErrorViewModel.GetDescription("HETS-04", _configuration))));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <response code="201">Project created</response>
        public virtual IActionResult RentalrequestrotationlistsPostAsync(RentalRequestRotationList item)
        {
            if (item != null)
            {
                AdjustRecord(item);

                var exists = _context.RentalRequestRotationLists.Any(a => a.Id == item.Id);
                if (exists)
                {
                    _context.RentalRequestRotationLists.Update(item);
                }
                else
                {
                    // record not found
                    _context.RentalRequestRotationLists.Add(item);
                }
                // Save the changes
                _context.SaveChanges();
                return(new ObjectResult(item));
            }
            else
            {
                return(new StatusCodeResult(400));
            }
        }
        /// <summary>
        /// Adjust the RentalRequestRotationList record
        /// </summary>
        /// <param name="item"></param>
        private void AdjustRRRLRecord(RentalRequestRotationList item)
        {
            if (item != null)
            {
                if (item.Equipment != null)
                {
                    item.Equipment = _context.Equipments.FirstOrDefault(a => a.Id == item.Equipment.Id);
                }

                if (item.RentalAgreement != null)
                {
                    item.RentalAgreement = _context.RentalAgreements.FirstOrDefault(a => a.Id == item.RentalAgreement.Id);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="id">id of Project to fetch</param>
        /// <param name="item"></param>
        /// <response code="200">OK</response>
        /// <response code="404">Project not found</response>
        public virtual IActionResult RentalrequestrotationlistsIdPutAsync(int id, RentalRequestRotationList item)
        {
            AdjustRecord(item);
            var exists = _context.RentalRequestRotationLists.Any(a => a.Id == id);

            if (exists && id == item.Id)
            {
                _context.RentalRequestRotationLists.Update(item);
                // Save the changes
                _context.SaveChanges();
                return(new ObjectResult(item));
            }
            else
            {
                // record not found
                return(new StatusCodeResult(404));
            }
        }
        /// <summary>
        /// Update rental request rotation list
        /// </summary>
        /// <param name="id">id of Rental Request Rotation List to update</param>
        /// <param name="item"></param>
        /// <response code="200">OK</response>
        /// <response code="404">Rental Request Rotation List not found</response>
        public virtual IActionResult RentalrequestrotationlistsIdPutAsync(int id, RentalRequestRotationList item)
        {
            AdjustRecord(item);

            bool exists = _context.RentalRequestRotationLists.Any(a => a.Id == id);

            if (exists && id == item.Id)
            {
                _context.RentalRequestRotationLists.Update(item);

                // save the changes
                _context.SaveChanges();

                return(new ObjectResult(new HetsResponse(item)));
            }

            // record not found
            return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
        }
        /// <summary>
        /// Get rental request rotation list by id
        /// </summary>
        /// <param name="id">id of Rental Request Rotation List to fetch</param>
        /// <response code="200">OK</response>
        /// <response code="404">Rental Request Rotation List not found</response>
        public virtual IActionResult RentalrequestrotationlistsIdGetAsync(int id)
        {
            bool exists = _context.RentalRequestRotationLists.Any(a => a.Id == id);

            if (exists)
            {
                RentalRequestRotationList result = _context.RentalRequestRotationLists
                                                   .Include(x => x.RentalAgreement)
                                                   .Include(x => x.Equipment)
                                                   .ThenInclude(r => r.EquipmentAttachments)
                                                   .Include(x => x.Equipment)
                                                   .ThenInclude(e => e.Owner)
                                                   .ThenInclude(c => c.PrimaryContact)
                                                   .First(a => a.Id == id);

                return(new ObjectResult(new HetsResponse(result)));
            }

            // record not found
            return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
        }
        /// <summary>
        /// Delete rental request rotation list
        /// </summary>
        /// <param name="id">id of Rental Request Rotation List to delete</param>
        /// <response code="200">OK</response>
        /// <response code="404">Rental Request Rotation List not found</response>
        public virtual IActionResult RentalrequestrotationlistsIdDeletePostAsync(int id)
        {
            bool exists = _context.RentalRequestRotationLists.Any(a => a.Id == id);

            if (exists)
            {
                RentalRequestRotationList item = _context.RentalRequestRotationLists.First(a => a.Id == id);

                if (item != null)
                {
                    _context.RentalRequestRotationLists.Remove(item);

                    // save the changes
                    _context.SaveChanges();
                }

                return(new ObjectResult(new HetsResponse(item)));
            }

            // record not found
            return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
        }
        private void BuildRentalRequestRotationList(RentalRequest item)
        {
            // validate input parameters
            if (item != null && item.LocalArea != null && item.DistrictEquipmentType != null && item.DistrictEquipmentType.EquipmentType != null)
            {
                int currentSortOrder = 1;

                item.RentalRequestRotationList = new List <RentalRequestRotationList>();

                // first get the localAreaRotationList.askNextBlock1 for the given local area.
                LocalAreaRotationList localAreaRotationList = _context.LocalAreaRotationLists
                                                              .Include(x => x.LocalArea)
                                                              .Include(x => x.AskNextBlock1)
                                                              .Include(x => x.AskNextBlock2)
                                                              .Include(x => x.AskNextBlockOpen)
                                                              .FirstOrDefault(x => x.LocalArea.Id == item.LocalArea.Id);

                int numberOfBlocks = item.DistrictEquipmentType.EquipmentType.NumberOfBlocks;

                for (int currentBlock = 1; currentBlock <= numberOfBlocks; currentBlock++)
                {
                    // start by getting the current set of equipment for the given equipment type
                    List <Equipment> blockEquipment = _context.Equipments
                                                      .Where(x => x.DistrictEquipmentType == item.DistrictEquipmentType && x.BlockNumber == currentBlock && x.LocalArea.Id == item.LocalArea.Id)
                                                      .OrderByDescending(x => x.Seniority)
                                                      .ToList();

                    int listSize = blockEquipment.Count;

                    // find the starting position.
                    int currentPosition = 0;

                    Equipment seeking = null;
                    if (localAreaRotationList != null)
                    {
                        switch (currentBlock)
                        {
                        case 1:
                            seeking = localAreaRotationList.AskNextBlock1;
                            break;

                        case 2:
                            seeking = localAreaRotationList.AskNextBlock2;
                            break;

                        case 3:
                            seeking = localAreaRotationList.AskNextBlockOpen;
                            break;
                        }
                    }

                    if (localAreaRotationList != null && seeking != null)
                    {
                        for (int i = 0; i < listSize; i++)
                        {
                            if (blockEquipment[i] != null && blockEquipment[i].Id == seeking.Id)
                            {
                                currentPosition = i;
                            }
                        }
                    }

                    // next pass sets the rotation list sort order.
                    for (int i = 0; i < listSize; i++)
                    {
                        RentalRequestRotationList rentalRequestRotationList = new RentalRequestRotationList();
                        rentalRequestRotationList.Equipment             = blockEquipment[currentPosition];
                        rentalRequestRotationList.CreateTimestamp       = DateTime.UtcNow;
                        rentalRequestRotationList.RotationListSortOrder = currentSortOrder;

                        item.RentalRequestRotationList.Add(rentalRequestRotationList);

                        currentPosition++;
                        currentSortOrder++;

                        if (currentPosition >= listSize)
                        {
                            currentPosition = 0;
                        }
                    }
                }
            }
        }
Example #9
0
        /// <summary>
        ///
        /// </summary>
        /// <remarks>Updates a rental request rotation list entry.  Side effect is the LocalAreaRotationList is also updated</remarks>
        /// <param name="id">id of RentalRequest to update</param>
        /// <param name="rentalRequestRotationListId">id of RentalRequestRotationList to update</param>
        /// <param name="item"></param>
        /// <response code="200">OK</response>
        /// <response code="404">RentalRequestRotationList not found</response>
        public virtual IActionResult RentalrequestsIdRentalrequestrotationlistRentalRequestRotationListIdPutAsync(int id, int rentalRequestRotationListId, RentalRequestRotationList item)
        {
            var result = "";

            return(new ObjectResult(result));
        }
Example #10
0
 public virtual IActionResult RentalrequestIdRotationListIdPut([FromRoute] int id, [FromBody] RentalRequestRotationList item)
 {
     return(_service.RentalrequestRotationListIdPutAsync(id, item));
 }
Example #11
0
 public virtual IActionResult RentalrequestrotationlistsPost([FromBody] RentalRequestRotationList item)
 {
     return(this._service.RentalrequestrotationlistsPostAsync(item));
 }
Example #12
0
        /// <summary>
        /// Create Rental Request Rotation List
        /// </summary>
        /// <param name="item"></param>
        private void BuildRentalRequestRotationList(RentalRequest item)
        {
            // validate input parameters
            if (item != null && item.LocalArea != null && item.DistrictEquipmentType != null && item.DistrictEquipmentType.EquipmentType != null)
            {
                int currentSortOrder = 1;

                item.RentalRequestRotationList = new List <RentalRequestRotationList>();

                // *******************************************************************************
                // get the number of blocks for this piece of equipment
                // (default blocks plus 1 (for the open block))
                // *******************************************************************************
                int numberOfBlocks = GetNumberOfBlocks(item);
                numberOfBlocks = numberOfBlocks + 1;

                // *******************************************************************************
                // get the equipment based on the current seniority list for the area
                // (and sort the results based on seniority)
                // *******************************************************************************
                for (int currentBlock = 1; currentBlock <= numberOfBlocks; currentBlock++)
                {
                    // start by getting the current set of equipment for the given equipment type
                    List <Equipment> blockEquipment = _context.Equipments
                                                      .Where(x => x.DistrictEquipmentType == item.DistrictEquipmentType &&
                                                             x.BlockNumber == currentBlock &&
                                                             x.LocalArea.Id == item.LocalArea.Id)
                                                      .OrderByDescending(x => x.Seniority)
                                                      .ToList();

                    int listSize = blockEquipment.Count;

                    // check if any items have "Active" contracts - and drop them from the list
                    for (int i = listSize - 1; i >= 0; i--)
                    {
                        bool agreementExists = _context.RentalAgreements
                                               .Any(x => x.EquipmentId == blockEquipment[i].Id &&
                                                    x.Status == "Active");

                        if (agreementExists)
                        {
                            blockEquipment.RemoveAt(i);
                        }
                    }

                    // update list size for sorting
                    listSize = blockEquipment.Count;

                    // sets the rotation list sort order
                    int currentPosition = 0;

                    for (int i = 0; i < listSize; i++)
                    {
                        RentalRequestRotationList rentalRequestRotationList =
                            new RentalRequestRotationList
                        {
                            Equipment             = blockEquipment[i],
                            CreateTimestamp       = DateTime.UtcNow,
                            RotationListSortOrder = currentSortOrder
                        };

                        item.RentalRequestRotationList.Add(rentalRequestRotationList);

                        currentPosition++;
                        currentSortOrder++;

                        if (currentPosition >= listSize)
                        {
                            currentPosition = 0;
                        }
                    }
                }

                // update the local area rotation list
                UpdateLocalAreaRotationList(item, numberOfBlocks);
            }
        }
        /// <summary>
        /// Integration test for EquipmentAttachments
        /// </summary>
        public async Task TestBasic()
        {
            string initialName = "InitialName";
            string changedName = "ChangedName";

            // first test the POST.
            var request = new HttpRequestMessage(HttpMethod.Post, "/api/rentalrequestrotationlists");

            // create a new object.
            RentalRequestRotationList rentalRequestRotationList = new RentalRequestRotationList();

            rentalRequestRotationList.Note = initialName;
            string jsonString = rentalRequestRotationList.ToJson();

            request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // parse as JSON.
            jsonString = await response.Content.ReadAsStringAsync();

            rentalRequestRotationList = JsonConvert.DeserializeObject <RentalRequestRotationList>(jsonString);
            // get the id
            var id = rentalRequestRotationList.Id;

            // change the name
            rentalRequestRotationList.Note = changedName;

            // now do an update.
            request         = new HttpRequestMessage(HttpMethod.Put, "/api/rentalrequestrotationlists/" + id);
            request.Content = new StringContent(rentalRequestRotationList.ToJson(), Encoding.UTF8, "application/json");
            response        = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // do a get.
            request  = new HttpRequestMessage(HttpMethod.Get, "/api/rentalrequestrotationlists/" + id);
            response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // parse as JSON.
            jsonString = await response.Content.ReadAsStringAsync();

            rentalRequestRotationList = JsonConvert.DeserializeObject <RentalRequestRotationList>(jsonString);

            // verify the change went through.
            Assert.Equal(rentalRequestRotationList.Note, changedName);

            // do a delete.
            request  = new HttpRequestMessage(HttpMethod.Post, "/api/rentalrequestrotationlists/" + id + "/delete");
            response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // should get a 404 if we try a get now.
            request  = new HttpRequestMessage(HttpMethod.Get, "/api/rentalrequestrotationlists/" + id);
            response = await _client.SendAsync(request);

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
Example #14
0
 /// <summary>
 /// Setup the test.
 /// </summary>
 public RentalRequestRotationListModelTests()
 {
     instance = new RentalRequestRotationList();
 }
Example #15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <response code="201">RentalRequestRotationList created</response>
        public virtual IActionResult RentalrequestrotationlistsPostAsync(RentalRequestRotationList item)
        {
            var result = "";

            return(new ObjectResult(result));
        }
        /// <summary>
        ///
        /// </summary>
        /// <remarks>Updates a rental request rotation list entry.  Side effect is the LocalAreaRotationList is also updated</remarks>
        /// <param name="id">id of RentalRequest to update</param>
        /// <param name="rentalRequestRotationListId">id of RentalRequestRotationList to update</param>
        /// <param name="item"></param>
        /// <response code="200">OK</response>
        /// <response code="404">RentalRequestRotationList not found</response>
        public virtual IActionResult RentalrequestsIdRentalrequestrotationlistRentalRequestRotationListIdPutAsync(int id, int rentalRequestRotationListId, RentalRequestRotationList item)
        {
            // update the rental request rotation list item.
            AdjustRRRLRecord(item);
            var exists = _context.RentalRequestRotationLists.Any(a => a.Id == rentalRequestRotationListId);

            if (exists && rentalRequestRotationListId == item.Id)
            {
                _context.RentalRequestRotationLists.Update(item);
                // Save the changes
                _context.SaveChanges();
                _context.Entry(item).State = EntityState.Detached;

                // now update the corresponding entry in the LocalAreaRotationList.
                _context.UpdateLocalAreaRotationList(item.Id);

                return(new ObjectResult(item));
            }
            else
            {
                // record not found
                return(new StatusCodeResult(404));
            }
        }
Example #17
0
        /// <summary>
        /// Update rental request rotation list
        /// </summary>
        /// <remarks>Updates a rental request rotation list entry</remarks>
        /// <param name="id">id of RentalRequest to update</param>
        /// <param name="item"></param>
        /// <response code="200">OK</response>
        /// <response code="404">RentalRequestRotationList not found</response>
        public virtual IActionResult RentalrequestRotationListIdPutAsync(int id, RentalRequestRotationList item)
        {
            // update the rental request rotation list item
            AdjustRentalRequestRotationListRecord(item);

            // check if we have the rental request and the rotation list is attached
            bool exists = _context.RentalRequests.Any(a => a.Id == id);

            if (!exists)
            {
                // record not found
                return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            // check if we have the rental request that is In Progress
            exists = _context.RentalRequests
                     .Any(a => a.Id == id &&
                          a.Status.Equals("In Progress", StringComparison.InvariantCultureIgnoreCase));

            if (!exists)
            {
                // record not found
                return(new ObjectResult(new HetsResponse("HETS-06", ErrorViewModel.GetDescription("HETS-06", _configuration))));
            }

            exists = _context.RentalRequests
                     .Any(a => a.Id == id &&
                          a.Status.Equals("In Progress", StringComparison.InvariantCultureIgnoreCase) &&
                          a.RentalRequestRotationList.Any(b => b.Id == item.Id));

            if (!exists)
            {
                // record not found
                return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            // ******************************************************************
            // update the rental request rotation list record
            // and count the "Yes"es
            // ******************************************************************
            RentalRequest rentalRequest = _context.RentalRequests
                                          .AsNoTracking()
                                          .Include(x => x.RentalRequestRotationList)
                                          .ThenInclude(e => e.Equipment)
                                          .Include(x => x.DistrictEquipmentType)
                                          .ThenInclude(d => d.EquipmentType)
                                          .Include(x => x.LocalArea)
                                          .First(a => a.Id == id);

            _context.Entry(rentalRequest).State = EntityState.Detached;

            // ******************************************************************
            // find the rotation list record to update
            // ******************************************************************
            int rotationListIndex = -1;

            for (int i = 0; i < rentalRequest.RentalRequestRotationList.Count; i++)
            {
                if (rentalRequest.RentalRequestRotationList[i].Id == item.Id)
                {
                    rotationListIndex = i;
                    break;
                }
            }

            // ******************************************************************
            // update the rental request rotation list record
            // ******************************************************************
            rentalRequest.RentalRequestRotationList[rotationListIndex] = item;

            // to do: fix the CreateTimestamp and UserId - coming in as nulls

            // ******************************************************************
            // can we "Complete" this rental request
            // (if the Yes or Forced Hires = Request.EquipmentCount)
            // ******************************************************************
            int countOfYeses          = 0;
            int equipmentRequestCount = rentalRequest.EquipmentCount;

            foreach (RentalRequestRotationList rotationList in rentalRequest.RentalRequestRotationList)
            {
                if (rotationList.OfferResponse != null &&
                    rotationList.OfferResponse.Equals("Yes", StringComparison.InvariantCultureIgnoreCase))
                {
                    countOfYeses = countOfYeses + 1;
                }
                else if (rotationList.IsForceHire != null &&
                         rotationList.IsForceHire == true)
                {
                    countOfYeses = countOfYeses + 1;
                }
            }

            if (countOfYeses >= equipmentRequestCount)
            {
                rentalRequest.Status = "Complete";
                rentalRequest.FirstOnRotationList = null;
            }

            // ******************************************************************
            // If the request is still In Progress:
            // 1. get the number of blocks for this equipment type
            // 2. set which rotation list record is currently "active"
            // ******************************************************************
            if (!rentalRequest.Status.Equals("Complete", StringComparison.InvariantCultureIgnoreCase))
            {
                int numberOfBlocks = GetNumberOfBlocks(rentalRequest);
                UpdateLocalAreaRotationList(rentalRequest, numberOfBlocks);
            }

            // ******************************************************************
            // save the changes - Rental Request
            // ******************************************************************
            _context.RentalRequests.Update(rentalRequest);
            _context.SaveChanges();

            return(new ObjectResult(new HetsResponse(item)));
        }