public async Task <IActionResult> GetSalary([FromRoute] int month)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            List <SalaryClass>              salResult      = new List <SalaryClass>();
            IEnumerable <ContractSet>       contracts      = _context.ContractSet;
            IEnumerable <AppraiserContract> contrAppraiser = _context.AppraiserContract;
            IEnumerable <UserSetAppraiser>  appraisers     = _context.UserSetAppraiser;
            IEnumerable <UserSet>           users          = _context.UserSet;
            IEnumerable <SalarySettingsSet> settings       = _context.SalarySettingsSet;
            IEnumerable <ObjectSetFlat>     flats          = _context.ObjectSetFlat;
            IEnumerable <ObjectSetCar>      cars           = _context.ObjectSetCar;
            IEnumerable <ObjectSetParcel>   parcels        = _context.ObjectSetParcel;
            SalaryClass       sal    = new SalaryClass();
            ContractSet       cntr   = new ContractSet();
            AppraiserContract conApp = new AppraiserContract();
            UserSetAppraiser  appr   = new UserSetAppraiser();
            UserSet           usr    = new UserSet();
            ObjectSetFlat     flt    = new ObjectSetFlat();
            ObjectSetCar      car    = new ObjectSetCar();
            ObjectSetParcel   prc    = new ObjectSetParcel();

            DateTime currDate    = new DateTime();
            DateTime monthStart  = new DateTime();
            DateTime monthFinish = new DateTime();

            currDate = DateTime.Now;

            if (month != 0)
            {
                month       = month * (-1);
                currDate    = currDate.AddMonths(month);
                monthFinish = getLastDay(currDate);
            }
            else
            {
                monthFinish = currDate;
            }

            monthStart = new DateTime(currDate.Year, currDate.Month, 1);

            contracts = contracts.Where(cn => DateTime.Compare(cn.FinishDate, monthStart) >= 0 && DateTime.Compare(cn.FinishDate, monthFinish) <= 0);

            for (int i = 0; i < contracts.Count(); i++)
            {
                sal    = null;
                conApp = null;
                appr   = null;
                usr    = null;

                conApp = contrAppraiser.FirstOrDefault(c => c.ContractId == contracts.ElementAt(i).Id);
                appr   = appraisers.FirstOrDefault(a => a.Id == conApp.AppraiserId);
                usr    = users.FirstOrDefault(u => u.Id == appr.Id);

                flt = flats.FirstOrDefault(u => u.Id == contracts.ElementAt(i).ObjectId);
                car = cars.FirstOrDefault(u => u.Id == contracts.ElementAt(i).ObjectId);
                prc = parcels.FirstOrDefault(u => u.Id == contracts.ElementAt(i).ObjectId);

                sal = salResult.FirstOrDefault(s => s.Surname == usr.Surname);

                if (sal == null)
                {
                    sal = new SalaryClass();
                    if (flt != null)
                    {
                        sal.ContractsFlat.Add(contracts.ElementAt(i));
                    }
                    else
                    {
                        if (prc != null)
                        {
                            sal.ContractsParcel.Add(contracts.ElementAt(i));
                        }
                        else
                        {
                            sal.ContractsCar.Add(contracts.ElementAt(i));
                        }
                    }
                    sal.Surname    = usr.Surname;
                    sal.Name       = usr.Name;
                    sal.Patronymic = usr.Patronymic;
                    sal.Month      = month;

                    contracts.ElementAt(i).Client            = null;
                    contracts.ElementAt(i).AppraiserContract = null;
                    contracts.ElementAt(i).Object            = null;

                    salResult.Add(sal);
                }
                else
                {
                    salResult.Remove(sal);
                    if (flt != null)
                    {
                        contracts.ElementAt(i).Client            = null;
                        contracts.ElementAt(i).AppraiserContract = null;
                        contracts.ElementAt(i).Object            = null;
                        sal.ContractsFlat.Add(contracts.ElementAt(i));
                    }
                    else
                    {
                        if (prc != null)
                        {
                            contracts.ElementAt(i).Client            = null;
                            contracts.ElementAt(i).AppraiserContract = null;
                            contracts.ElementAt(i).Object            = null;
                            sal.ContractsParcel.Add(contracts.ElementAt(i));
                        }
                        else
                        {
                            contracts.ElementAt(i).Client            = null;
                            contracts.ElementAt(i).AppraiserContract = null;
                            contracts.ElementAt(i).Object            = null;
                            sal.ContractsCar.Add(contracts.ElementAt(i));
                        }
                    }
                    salResult.Add(sal);
                }
            }

            for (int i = 0; i < salResult.Count(); i++)
            {
                salResult.ElementAt(i).ContractsCount = salResult.ElementAt(i).ContractsCar.Count + salResult.ElementAt(i).ContractsFlat.Count + salResult.ElementAt(i).ContractsParcel.Count;

                foreach (ContractSet contr in salResult.ElementAt(i).ContractsFlat)
                {
                    salResult.ElementAt(i).Salary += contr.ContractSumm / 100 * settings.First().FlatPercent;
                }

                foreach (ContractSet contr in salResult.ElementAt(i).ContractsCar)
                {
                    salResult.ElementAt(i).Salary += contr.ContractSumm / 100 * settings.First().CarPercent;
                }

                foreach (ContractSet contr in salResult.ElementAt(i).ContractsParcel)
                {
                    salResult.ElementAt(i).Salary += contr.ContractSumm / 100 * settings.First().ParcelPercent;
                }

                salResult.ElementAt(i).Salary = Math.Round(salResult.ElementAt(i).Salary, MidpointRounding.ToEven);
            }

            if (salResult == null)
            {
                return(NotFound());
            }

            return(Ok(salResult));
        }
        public async Task <IActionResult> ToExcel([FromRoute] int month)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            List <SalaryClass>              salResult      = new List <SalaryClass>();
            IEnumerable <ContractSet>       contracts      = _context.ContractSet;
            IEnumerable <AppraiserContract> contrAppraiser = _context.AppraiserContract;
            IEnumerable <UserSetAppraiser>  appraisers     = _context.UserSetAppraiser;
            IEnumerable <UserSet>           users          = _context.UserSet;
            IEnumerable <SalarySettingsSet> settings       = _context.SalarySettingsSet;
            IEnumerable <ObjectSetFlat>     flats          = _context.ObjectSetFlat;
            IEnumerable <ObjectSetCar>      cars           = _context.ObjectSetCar;
            IEnumerable <ObjectSetParcel>   parcels        = _context.ObjectSetParcel;
            SalaryClass       sal    = new SalaryClass();
            ContractSet       cntr   = new ContractSet();
            AppraiserContract conApp = new AppraiserContract();
            UserSetAppraiser  appr   = new UserSetAppraiser();
            UserSet           usr    = new UserSet();
            ObjectSetFlat     flt    = new ObjectSetFlat();
            ObjectSetCar      car    = new ObjectSetCar();
            ObjectSetParcel   prc    = new ObjectSetParcel();

            DateTime currDate    = new DateTime();
            DateTime monthStart  = new DateTime();
            DateTime monthFinish = new DateTime();

            currDate = DateTime.Now;

            if (month != 0)
            {
                month       = month * (-1);
                currDate    = currDate.AddMonths(month);
                monthFinish = getLastDay(currDate);
            }
            else
            {
                monthFinish = currDate;
            }

            monthStart = new DateTime(currDate.Year, currDate.Month, 1);

            contracts = contracts.Where(cn => DateTime.Compare(cn.FinishDate, monthStart) >= 0 && DateTime.Compare(cn.FinishDate, monthFinish) <= 0);

            for (int i = 0; i < contracts.Count(); i++)
            {
                sal    = null;
                conApp = null;
                appr   = null;
                usr    = null;

                conApp = contrAppraiser.FirstOrDefault(c => c.ContractId == contracts.ElementAt(i).Id);
                appr   = appraisers.FirstOrDefault(a => a.Id == conApp.AppraiserId);
                usr    = users.FirstOrDefault(u => u.Id == appr.Id);

                flt = flats.FirstOrDefault(u => u.Id == contracts.ElementAt(i).ObjectId);
                car = cars.FirstOrDefault(u => u.Id == contracts.ElementAt(i).ObjectId);
                prc = parcels.FirstOrDefault(u => u.Id == contracts.ElementAt(i).ObjectId);

                sal = salResult.FirstOrDefault(s => s.Surname == usr.Surname);

                if (sal == null)
                {
                    sal = new SalaryClass();
                    if (flt != null)
                    {
                        sal.ContractsFlat.Add(contracts.ElementAt(i));
                    }
                    else
                    {
                        if (prc != null)
                        {
                            sal.ContractsParcel.Add(contracts.ElementAt(i));
                        }
                        else
                        {
                            sal.ContractsCar.Add(contracts.ElementAt(i));
                        }
                    }
                    sal.Surname    = usr.Surname;
                    sal.Name       = usr.Name;
                    sal.Patronymic = usr.Patronymic;
                    sal.Month      = month;

                    contracts.ElementAt(i).Client            = null;
                    contracts.ElementAt(i).AppraiserContract = null;
                    contracts.ElementAt(i).Object            = null;

                    salResult.Add(sal);
                }
                else
                {
                    salResult.Remove(sal);
                    if (flt != null)
                    {
                        contracts.ElementAt(i).Client            = null;
                        contracts.ElementAt(i).AppraiserContract = null;
                        contracts.ElementAt(i).Object            = null;
                        sal.ContractsFlat.Add(contracts.ElementAt(i));
                    }
                    else
                    {
                        if (prc != null)
                        {
                            contracts.ElementAt(i).Client            = null;
                            contracts.ElementAt(i).AppraiserContract = null;
                            contracts.ElementAt(i).Object            = null;
                            sal.ContractsParcel.Add(contracts.ElementAt(i));
                        }
                        else
                        {
                            contracts.ElementAt(i).Client            = null;
                            contracts.ElementAt(i).AppraiserContract = null;
                            contracts.ElementAt(i).Object            = null;
                            sal.ContractsCar.Add(contracts.ElementAt(i));
                        }
                    }
                    salResult.Add(sal);
                }
            }

            for (int i = 0; i < salResult.Count(); i++)
            {
                salResult.ElementAt(i).ContractsCount = salResult.ElementAt(i).ContractsCar.Count + salResult.ElementAt(i).ContractsFlat.Count + salResult.ElementAt(i).ContractsParcel.Count;

                foreach (ContractSet contr in salResult.ElementAt(i).ContractsFlat)
                {
                    salResult.ElementAt(i).Salary += contr.ContractSumm / 100 * settings.First().FlatPercent;
                }

                foreach (ContractSet contr in salResult.ElementAt(i).ContractsCar)
                {
                    salResult.ElementAt(i).Salary += contr.ContractSumm / 100 * settings.First().CarPercent;
                }

                foreach (ContractSet contr in salResult.ElementAt(i).ContractsParcel)
                {
                    salResult.ElementAt(i).Salary += contr.ContractSumm / 100 * settings.First().ParcelPercent;
                }
            }

            string[] monthArr = new string[] { "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" };

            var fileDownloadName = "Зарплата - " + monthArr[monthStart.Month - 1] + ".xlsx";

            using (var package = createExcelPackage(salResult))
            {
                package.SaveAs(new FileInfo(Path.Combine(@"C:\Users\user\Downloads", fileDownloadName)));
            }
            return(Ok());
        }