public AdminAgencyFeeModel GetAgencyFeesAdmin(string countryCode, int patentTypeId, string currencyCode = "")
        {
            var result           = new AdminAgencyFeeModel();
            var country          = context.Countries.Find(countryCode);
            var patentType       = context.PatentFeeTypes.Find(patentTypeId);
            var currencyCodeFind = currencyCode == "" ? country.DefaultCurrency : context.Currencies.Find(currencyCode)?.CurrencyCode;

            result.CountryCode     = country.Code;
            result.CountryName     = country.Name;
            result.CurrencyCode    = currencyCodeFind;
            result.PatentTypeId    = patentType.ID;
            result.PatentTypeName  = patentType.Name;
            result.PatentTypeYears = patentType.Years;
            var agenFees = GetAgencyFees(countryCode, patentTypeId).OrderBy(p => p.Year).ToList();

            result.ValidFrom = agenFees.Count != 0 ? agenFees.FirstOrDefault().ValidFrom : DateTime.Now;
            if (agenFees != null && agenFees.Count > 0)
            {
                result.AgencyFees = agenFees.Select(p => new AdminAgencyFee()
                {
                    Year      = p.Year,
                    Fee       = p.Fee,
                    ValidFrom = p.ValidFrom
                }).ToList();
            }
            else
            {
                result.AgencyFees = new List <AdminAgencyFee>(new AdminAgencyFee[result.PatentTypeYears]);
            }
            return(result);
        }
        public AdminAgencyFeeModel UpdateAgencyFeesAdmin(AdminAgencyFeeModel data)
        {
            var newPatentFees = data.AgencyFees.Select(p => new AgencyFee()
            {
                CountryCode  = data.CountryCode,
                CurrencyCode = data.CurrencyCode,
                PatentTypeId = (byte)data.PatentTypeId,
                Year         = (byte)p.Year,
                ValidFrom    = data.ValidFrom,
                Fee          = p.Fee
            });

            var agencyFees = GetAgencyFees(data.CountryCode, data.PatentTypeId, data.CurrencyCode);

            if (agencyFees != null && agencyFees.Any())
            {
                dbSet.RemoveRange(agencyFees);
            }
            dbSet.AddRange(newPatentFees);
            if (context.SaveChanges() > 0)
            {
                return(GetAgencyFeesAdmin(data.CountryCode, data.PatentTypeId, data.CurrencyCode));
            }
            return(null);
        }
        public ActionResult Index(AdminAgencyFeeModel model)
        {
            if (ModelState.IsValid)
            {
                //show save success message
                var result = uow.AgencyFeeRepository.UpdateAgencyFeesAdmin(model);
                if (result != null)
                {
                    AddAlert(AlertType.SUCCESS, "Save successfully");
                }
                else
                {
                    AddAlert(AlertType.DANGER, "Save failed");
                }
                return(RedirectToAction("Index"));
            }
            AddAlert(AlertType.DANGER, "Save failed");

            ViewBag.Countries   = uow.CountryRepository.GetAll();
            ViewBag.PatentTypes = uow.PatentTypeRepository.GetAll();
            ViewBag.Currencies  = uow.CurrencyRepository.GetAll();
            return(View(model));
        }