コード例 #1
0
        /// <summary>
        /// Get rental agreement
        /// </summary>
        /// <param name="district"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetRentalAgreementNumber(HetDistrict district, DbAppContext context)
        {
            int    currentCount = 0;
            string result       = "";

            // validate item
            if (district?.DistrictId != null)
            {
                DateTime currentTime = DateTime.UtcNow;

                int fiscalYear = currentTime.Year;

                // fiscal year always ends in March
                if (currentTime.Month > 3)
                {
                    fiscalYear++;
                }

                int districtNumber = district.MinistryDistrictId;
                int districtId     = district.DistrictId;

                DateTime fiscalYearStart = new DateTime(fiscalYear - 1, 1, 1);

                // count the number of rental agreements in the system (for this district)
                HetRentalAgreement agreement = context.HetRentalAgreement.AsNoTracking()
                                               .Include(x => x.Project.District)
                                               .OrderBy(x => x.RentalAgreementId)
                                               .LastOrDefault(x => x.DistrictId == districtId &&
                                                              x.AppCreateTimestamp >= fiscalYearStart &&
                                                              x.Number.Contains("-D"));

                if (agreement != null)
                {
                    string temp = agreement.Number;
                    temp = temp.Substring(temp.LastIndexOf('-') + 1);
                    int.TryParse(temp, out currentCount);
                }

                currentCount++;

                // format of the Rental Agreement number is YYYY-#-#### (new for blank agreements)
                result = fiscalYear + "-D" + districtNumber + "-" + currentCount.ToString("D4");
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Map data
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="oldObject"></param>
        /// <param name="serviceArea"></param>
        /// <param name="systemId"></param>
        private static void CopyToInstance(DbAppContext dbContext, ImportModels.ServiceArea oldObject,
                                           ref HetServiceArea serviceArea, string systemId)
        {
            try
            {
                if (serviceArea == null)
                {
                    serviceArea = new HetServiceArea();
                }

                if (oldObject.Service_Area_Id <= 0)
                {
                    return;
                }

                serviceArea.ServiceAreaId         = oldObject.Service_Area_Id;
                serviceArea.MinistryServiceAreaId = oldObject.Service_Area_Id;
                serviceArea.Name = oldObject.Service_Area_Desc.Trim();

                // remove " CA" from Service Area Names
                if (serviceArea.Name.EndsWith(" CA"))
                {
                    serviceArea.Name = serviceArea.Name.Replace(" CA", "");
                }

                // service area number
                if (oldObject.Service_Area_Cd != null)
                {
                    serviceArea.AreaNumber = int.Parse(oldObject.Service_Area_Cd);
                }

                // get the district for this service area
                int tempServiceAreaId = GetServiceAreaId(serviceArea.Name);

                if (tempServiceAreaId > 0)
                {
                    HetDistrict district = dbContext.HetDistrict.AsNoTracking()
                                           .FirstOrDefault(x => x.MinistryDistrictId == tempServiceAreaId);

                    if (district != null)
                    {
                        serviceArea.DistrictId = district.DistrictId;
                    }
                }

                if (oldObject.FiscalStart != null)
                {
                    serviceArea.FiscalStartDate = DateTime.ParseExact(oldObject.FiscalStart.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                }

                if (oldObject.FiscalEnd != null)
                {
                    serviceArea.FiscalEndDate = DateTime.ParseExact(oldObject.FiscalEnd.Trim().Substring(0, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
                }

                // Address, Phone, etc.
                serviceArea.Address = oldObject.Address.Trim();

                if (serviceArea.Address.Length > 255)
                {
                    serviceArea.Address = serviceArea.Address.Substring(1, 255);
                }

                serviceArea.Phone = oldObject.Phone.Trim();

                if (serviceArea.Phone.Length > 50)
                {
                    serviceArea.Phone = serviceArea.Phone.Substring(1, 50);
                }

                serviceArea.Phone = ImportUtility.FormatPhone(serviceArea.Phone);

                serviceArea.Fax = oldObject.Fax.Trim();

                if (serviceArea.Fax.Length > 50)
                {
                    serviceArea.Fax = serviceArea.Fax.Substring(1, 50);
                }

                serviceArea.Fax = ImportUtility.FormatPhone(serviceArea.Fax);

                serviceArea.SupportingDocuments = oldObject.Sup_Docs.Trim();

                if (serviceArea.SupportingDocuments.Length > 500)
                {
                    serviceArea.SupportingDocuments = serviceArea.SupportingDocuments.Substring(1, 500);
                }

                serviceArea.AppCreateUserid        = systemId;
                serviceArea.AppCreateTimestamp     = DateTime.UtcNow;
                serviceArea.AppLastUpdateUserid    = systemId;
                serviceArea.AppLastUpdateTimestamp = DateTime.UtcNow;

                dbContext.HetServiceArea.Add(serviceArea);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("***Error*** - Service Area Id: " + oldObject.Service_Area_Id);
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
コード例 #3
0
        public virtual IActionResult UsersIdPut([FromRoute] int id, [FromBody] HetUser item)
        {
            if (item == null || id != item.UserId)
            {
                // not found
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            bool exists = _context.HetUser.Any(x => x.UserId == id);

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

            // get record
            HetUser user = _context.HetUser
                           .Include(x => x.District)
                           .Include(x => x.HetUserDistrict)
                           .First(x => x.UserId == id);

            // validate that user id is unique
            // HETS-1033 - Post Live: Add validation on User ID while editing a user
            string smUserId = item.SmUserId?.Trim().ToLower();

            HetUser existingUser = _context.HetUser.AsNoTracking()
                                   .FirstOrDefault(x => x.SmUserId.ToLower() == smUserId && x.UserId != user.UserId);

            if (existingUser != null)
            {
                return(new BadRequestObjectResult(new HetsResponse("HETS-38", ErrorViewModel.GetDescription("HETS-38", _configuration))));
            }

            user.ConcurrencyControlNumber = item.ConcurrencyControlNumber;
            user.Active        = item.Active;
            user.Email         = item.Email;
            user.GivenName     = item.GivenName;
            user.Surname       = item.Surname;
            user.SmUserId      = item.SmUserId;
            user.AgreementCity = item.AgreementCity;

            if (item.District != null)
            {
                bool districtExists = _context.HetDistrict.Any(x => x.DistrictId == item.District.DistrictId);

                if (districtExists)
                {
                    HetDistrict district = _context.HetDistrict
                                           .Include(x => x.Region)
                                           .First(x => x.DistrictId == item.District.DistrictId);

                    user.DistrictId = district.DistrictId;

                    // check if we need to add this to the User District List too
                    bool userDistrictExists = false;

                    foreach (HetUserDistrict userDistrict in user.HetUserDistrict)
                    {
                        if (userDistrict.DistrictId == item.District.DistrictId)
                        {
                            userDistrictExists = true;
                            break;
                        }
                    }

                    // if not found - then add it!
                    if (!userDistrictExists)
                    {
                        HetUserDistrict newUserDistrict = new HetUserDistrict
                        {
                            UserId     = item.UserId,
                            DistrictId = district.DistrictId
                        };

                        if (user.HetUserDistrict == null)
                        {
                            user.HetUserDistrict      = new List <HetUserDistrict>();
                            newUserDistrict.IsPrimary = true;
                        }

                        user.HetUserDistrict.Add(newUserDistrict);
                    }
                }
            }

            // save changes
            _context.SaveChanges();

            // get updated user record and return to UI
            return(new ObjectResult(new HetsResponse(UserHelper.GetRecord(id, _context))));
        }
コード例 #4
0
        /// <summary>
        /// Annual Rollover
        /// </summary>
        /// <param name="context"></param>
        /// <param name="districtId"></param>
        /// <param name="seniorityScoringRules"></param>
        /// <param name="connectionString"></param>
        public static void AnnualRolloverJob(PerformContext context, int districtId, string seniorityScoringRules, string connectionString)
        {
            try
            {
                // open a connection to the database
                DbAppContext dbContext = new DbAppContext(connectionString);

                // get processing rules
                SeniorityScoringRules scoringRules = new SeniorityScoringRules(seniorityScoringRules);

                // update progress bar
                IProgressBar progress = context.WriteProgressBar();
                context.WriteLine("Starting Annual Rollover Job - District #" + districtId);

                progress.SetValue(0);

                // validate district id
                HetDistrict district = dbContext.HetDistrict.AsNoTracking()
                                       .FirstOrDefault(x => x.DistrictId == districtId);

                if (district == null)
                {
                    context.WriteLine("District not found");
                    progress.SetValue(100);
                    return;
                }

                // get status record - and ensure we're active
                HetDistrictStatus status = GetRecord(districtId, dbContext);

                if (status == null)
                {
                    context.WriteLine("District Status not found");
                    progress.SetValue(100);
                    return;
                }

                // get equipment status
                int?statusId = StatusHelper.GetStatusId(HetEquipment.StatusApproved, "equipmentStatus", dbContext);
                if (statusId == null)
                {
                    context.WriteLine("Equipment Status not found");
                    progress.SetValue(100);
                    return;
                }

                // determine the "Rollover Date" (required for testing)
                DateTime rolloverDate;

                if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3)
                {
                    if (status.NextFiscalYear == null)
                    {
                        status.NextFiscalYear = DateTime.UtcNow.Year;
                    }

                    rolloverDate = new DateTime((int)status.NextFiscalYear, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
                }
                else
                {
                    if (status.CurrentFiscalYear == null)
                    {
                        status.CurrentFiscalYear = DateTime.UtcNow.Year;
                    }

                    rolloverDate = new DateTime((int)status.CurrentFiscalYear, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
                }

                // get all district equipment types
                List <HetDistrictEquipmentType> equipmentTypes = dbContext.HetDistrictEquipmentType.AsNoTracking()
                                                                 .Include(x => x.EquipmentType)
                                                                 .Where(x => x.DistrictId == districtId).ToList();

                // get all local areas
                List <HetLocalArea> localAreas = dbContext.HetLocalArea.AsNoTracking()
                                                 .Where(a => a.ServiceArea.DistrictId == districtId).ToList();

                // update status - job is kicked off
                int localAreaCompleteCount = 0;
                int equipmentCompleteCount = 0;

                UpdateStatusKickoff(dbContext, status, localAreaCompleteCount, equipmentCompleteCount);

                // process all local areas and equipment types
                foreach (HetLocalArea localArea in localAreas.WithProgress(progress))
                {
                    // re-open the connection
                    dbContext = new DbAppContext(connectionString);

                    if (localArea.Name != null)
                    {
                        context.WriteLine("Local Area: " + localArea.Name);
                    }
                    else
                    {
                        context.WriteLine("Local Area ID: " + localArea.LocalAreaId);
                    }

                    // reset equipment counter
                    equipmentCompleteCount = 0;

                    foreach (HetDistrictEquipmentType equipmentType in equipmentTypes)
                    {
                        // it this a dump truck?
                        bool isDumpTruck = equipmentType.EquipmentType.IsDumpTruck;

                        // get rules for scoring and seniority block
                        int seniorityScoring = isDumpTruck ? scoringRules.GetEquipmentScore("DumpTruck") : scoringRules.GetEquipmentScore();
                        int blockSize        = isDumpTruck ? scoringRules.GetBlockSize("DumpTruck") : scoringRules.GetBlockSize();
                        int totalBlocks      = isDumpTruck ? scoringRules.GetTotalBlocks("DumpTruck") : scoringRules.GetTotalBlocks();

                        using (DbAppContext etContext = new DbAppContext(connectionString))
                        {
                            List <HetEquipment> data = etContext.HetEquipment
                                                       .Include(x => x.LocalArea)
                                                       .Include(x => x.DistrictEquipmentType.EquipmentType)
                                                       .Where(x => x.EquipmentStatusTypeId == statusId &&
                                                              x.LocalAreaId == localArea.LocalAreaId &&
                                                              x.DistrictEquipmentTypeId == equipmentType.DistrictEquipmentTypeId)
                                                       .ToList();

                            foreach (HetEquipment equipment in data)
                            {
                                // rollover the year
                                equipment.ServiceHoursThreeYearsAgo = equipment.ServiceHoursTwoYearsAgo;
                                equipment.ServiceHoursTwoYearsAgo   = equipment.ServiceHoursLastYear;
                                equipment.ServiceHoursLastYear      = EquipmentHelper.GetYtdServiceHours(equipment.EquipmentId, dbContext, rolloverDate);
                                equipment.CalculateYearsOfService(DateTime.UtcNow);

                                // blank out the override reason
                                equipment.SeniorityOverrideReason = "";

                                // update the seniority score
                                equipment.CalculateSeniority(seniorityScoring);

                                etContext.HetEquipment.Update(equipment);
                                etContext.SaveChanges();
                            }
                        }

                        // now update the rotation list
                        using (DbAppContext abContext = new DbAppContext(connectionString))
                        {
                            int localAreaId     = localArea.LocalAreaId;
                            int equipmentTypeId = equipmentType.DistrictEquipmentTypeId;

                            SeniorityListHelper.AssignBlocks(localAreaId, equipmentTypeId, blockSize, totalBlocks, abContext);
                        }

                        // increment counters and update status
                        equipmentCompleteCount++;
                        UpdateStatus(dbContext, status, localAreaCompleteCount, equipmentCompleteCount);
                    }

                    // increment counters and update status
                    localAreaCompleteCount++;
                    UpdateStatus(dbContext, status, localAreaCompleteCount, equipmentCompleteCount);
                    if (status.ProgressPercentage != null)
                    {
                        progress.SetValue((int)status.ProgressPercentage);
                    }
                }

                // done!
                UpdateStatusComplete(dbContext, status, localAreaCompleteCount, equipmentCompleteCount);
                progress.SetValue(100);

                // **********************************************************
                // regenerate Owner Secret Keys for this district
                // **********************************************************
                dbContext = new DbAppContext(connectionString);

                context.WriteLine("");
                context.WriteLine("Generate New Secret Keys - District #" + districtId);
                progress = context.WriteProgressBar();

                progress.SetValue(0);
                // get records
                List <HetOwner> owners = dbContext.HetOwner.AsNoTracking()
                                         .Where(x => x.BusinessId == null &&
                                                x.LocalArea.ServiceArea.DistrictId == districtId)
                                         .ToList();

                int i          = 0;
                int ownerCount = owners.Count;

                foreach (HetOwner owner in owners)
                {
                    i++;
                    string key = SecretKeyHelper.RandomString(8, owner.OwnerId);

                    string temp = owner.OwnerCode;

                    if (string.IsNullOrEmpty(temp))
                    {
                        temp = SecretKeyHelper.RandomString(4, owner.OwnerId);
                    }

                    key = temp + "-" + (rolloverDate.Year + 1) + "-" + key;

                    // get owner and update
                    HetOwner ownerRecord = dbContext.HetOwner.First(x => x.OwnerId == owner.OwnerId);
                    ownerRecord.SharedKey = key;
                    dbContext.HetOwner.Update(ownerRecord);

                    decimal tempProgress = Convert.ToDecimal(i) / Convert.ToDecimal(ownerCount);
                    tempProgress = tempProgress * 100;
                    int percentComplete = Convert.ToInt32(tempProgress);

                    if (percentComplete < 1)
                    {
                        percentComplete = 1;
                    }
                    if (percentComplete > 99)
                    {
                        percentComplete = 100;
                    }

                    progress.SetValue(percentComplete);
                }

                // save remaining updates - done!
                dbContext.SaveChangesForImport();
                progress.SetValue(100);
                context.WriteLine("Generate New Secret Keys - Done");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }