Пример #1
0
        public virtual IActionResult RentalRequestsIdRotationListIdGet([FromRoute] int id)
        {
            bool exists = _context.HetRentalRequest.Any(a => a.RentalRequestId == id);

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

            // get the scoring rules
            SeniorityScoringRules scoringRules = new SeniorityScoringRules(_configuration);

            return(new ObjectResult(new HetsResponse(RentalRequestHelper.GetRecordWithRotationList(id, scoringRules, _context))));
        }
Пример #2
0
        public virtual IActionResult RentalRequestIdRotationListIdPut([FromRoute] int id, [FromBody] HetRentalRequestRotationList item)
        {
            // not found
            if (item == null)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            bool exists = _context.HetRentalRequest.Any(a => a.RentalRequestId == id);

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

            int?statusId = StatusHelper.GetStatusId(HetRentalRequest.StatusInProgress, "rentalRequestStatus", _context);

            if (statusId == null)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-23", ErrorViewModel.GetDescription("HETS-23", _configuration))));
            }

            // check if we have the rental request that is In Progress
            exists = _context.HetRentalRequest
                     .Any(a => a.RentalRequestId == id &&
                          a.RentalRequestStatusTypeId == statusId);

            // rental request must be "in progress"
            if (!exists)
            {
                return(new BadRequestObjectResult(new HetsResponse("HETS-06", ErrorViewModel.GetDescription("HETS-06", _configuration))));
            }

            // get rental request record
            HetRentalRequest request = _context.HetRentalRequest
                                       .Include(x => x.Project)
                                       .ThenInclude(x => x.District)
                                       .Include(x => x.LocalArea)
                                       .Include(x => x.HetRentalRequestRotationList)
                                       .ThenInclude(x => x.Equipment)
                                       .First(a => a.RentalRequestId == id);

            // get rotation list record
            HetRentalRequestRotationList requestRotationList = _context.HetRentalRequestRotationList
                                                               .FirstOrDefault(a => a.RentalRequestRotationListId == item.RentalRequestRotationListId);

            // not found
            if (requestRotationList == null)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            // update rotation list record
            int tempEquipmentId = item.Equipment.EquipmentId;

            requestRotationList.ConcurrencyControlNumber = item.ConcurrencyControlNumber;
            requestRotationList.EquipmentId           = tempEquipmentId;
            requestRotationList.IsForceHire           = item.IsForceHire;
            requestRotationList.AskedDateTime         = DateTime.UtcNow;
            requestRotationList.Note                  = item.Note;
            requestRotationList.OfferRefusalReason    = item.OfferRefusalReason;
            requestRotationList.OfferResponse         = item.OfferResponse;
            requestRotationList.OfferResponseDatetime = item.OfferResponseDatetime;
            requestRotationList.WasAsked              = item.WasAsked;
            requestRotationList.OfferResponseNote     = item.OfferResponseNote;

            // do we need to create or modify a Rental Agreement?
            if (item.IsForceHire == true ||
                item.OfferResponse.Equals("Yes", StringComparison.InvariantCultureIgnoreCase))
            {
                // get rental agreement record
                HetRentalAgreement rentalAgreement = _context.HetRentalAgreement
                                                     .FirstOrDefault(a => a.RentalAgreementId == item.RentalAgreementId);

                // create rental agreement if it doesn't exist
                if (rentalAgreement == null)
                {
                    // generate the rental agreement number
                    string agreementNumber = RentalAgreementHelper.GetRentalAgreementNumber(item.Equipment, _context);

                    // get user info - agreement city
                    User   user          = UserAccountHelper.GetUser(_context, _httpContext);
                    string agreementCity = user.AgreementCity;

                    int?rateTypeId = StatusHelper.GetRatePeriodId(HetRatePeriodType.PeriodHourly, _context);
                    if (rateTypeId == null)
                    {
                        return(new NotFoundObjectResult(new HetsResponse("HETS-24", ErrorViewModel.GetDescription("HETS-24", _configuration))));
                    }

                    rentalAgreement = new HetRentalAgreement
                    {
                        ProjectId        = request.ProjectId,
                        DistrictId       = request.Project.District.DistrictId,
                        EquipmentId      = tempEquipmentId,
                        Number           = agreementNumber,
                        RatePeriodTypeId = (int)rateTypeId,
                        AgreementCity    = agreementCity
                    };

                    // add overtime rates
                    List <HetProvincialRateType> overtime = _context.HetProvincialRateType.AsNoTracking()
                                                            .Where(x => x.Overtime)
                                                            .ToList();

                    // agreement overtime records (default overtime flag)
                    foreach (HetProvincialRateType rate in overtime)
                    {
                        // add the rate
                        HetRentalAgreementRate newAgreementRate = new HetRentalAgreementRate
                        {
                            Comment           = rate.Description,
                            ComponentName     = rate.RateType,
                            Overtime          = true,
                            Active            = rate.Active,
                            IsIncludedInTotal = rate.IsIncludedInTotal,
                            Rate = rate.Rate
                        };

                        if (rentalAgreement.HetRentalAgreementRate == null)
                        {
                            rentalAgreement.HetRentalAgreementRate = new List <HetRentalAgreementRate>();
                        }

                        rentalAgreement.HetRentalAgreementRate.Add(newAgreementRate);
                    }

                    _context.HetRentalAgreement.Add(rentalAgreement);
                }

                int?statusIdAgreement = StatusHelper.GetStatusId(HetRentalAgreement.StatusActive, "rentalAgreementStatus", _context);
                if (statusIdAgreement == null)
                {
                    return(new NotFoundObjectResult(new HetsResponse("HETS-23", ErrorViewModel.GetDescription("HETS-23", _configuration))));
                }

                // update rental agreement
                rentalAgreement.RentalAgreementStatusTypeId = (int)statusIdAgreement;
                rentalAgreement.DatedOn                     = DateTime.UtcNow;
                rentalAgreement.EstimateHours               = request.ExpectedHours;
                rentalAgreement.EstimateStartWork           = request.ExpectedStartDate;
                rentalAgreement.RentalRequestId             = request.RentalRequestId;
                rentalAgreement.RentalRequestRotationListId = requestRotationList.RentalRequestRotationListId;

                // have to save the agreement
                _context.SaveChanges();

                // relate the new rental agreement to the original rotation list record
                int tempRentalAgreementId = rentalAgreement.RentalAgreementId;
                requestRotationList.RentalAgreementId = tempRentalAgreementId;
                requestRotationList.RentalAgreement   = rentalAgreement;
            }

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

            foreach (HetRentalRequestRotationList rotationList in request.HetRentalRequestRotationList)
            {
                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)
            {
                int?statusIdComplete = StatusHelper.GetStatusId(HetRentalRequest.StatusComplete, "rentalRequestStatus", _context);
                if (statusIdComplete == null)
                {
                    return(new NotFoundObjectResult(new HetsResponse("HETS-23", ErrorViewModel.GetDescription("HETS-23", _configuration))));
                }

                request.RentalRequestStatusTypeId = (int)statusIdComplete;
                request.Status = "Complete";
                request.FirstOnRotationList = null;
            }

            // 1. get the number of blocks for this equipment type
            // 2. set which rotation list record is currently "active"
            int numberOfBlocks = EquipmentHelper.GetNumberOfBlocks(item.Equipment, _configuration);

            RentalRequestHelper.UpdateRotationList(request, numberOfBlocks, _context);

            // save the changes
            _context.SaveChanges();

            // get the scoring rules
            SeniorityScoringRules scoringRules = new SeniorityScoringRules(_configuration);

            return(new ObjectResult(new HetsResponse(RentalRequestHelper.GetRecordWithRotationList(id, scoringRules, _context))));
        }