public ActionResult CreateEdit(int?id, int?policyId, string returnUrl) { InvoiceItemViewModel vm; if (id != null) //Edit { vm = Uow.Invoices.GetAll() .Where(i => i.Id == (int)id) .Include(i => i.Particulars) .ProjectTo <InvoiceItemViewModel>() .FirstOrDefault(); if (vm.DueDate.Year < 1980) { vm.DueDate = DateTime.Now; } if (!(vm.StatusName.ToLower() == "pending" || vm.StatusName.ToLower() == "rejected")) { //Invoice has been approved. Premium and Endorsement Number are fixed at this point. var premiumFromParticular = vm.Particulars.Where(p => p.ParticularTypeName.ToLower() == "premium") .FirstOrDefault(); if (premiumFromParticular != null) { vm.Premium = premiumFromParticular.ParticularAmount; } //For Particular Readonly Values vm.IsReadOnlyState = true; } else { //Invoice not yet approved. Endorsement Number can still be modified. var policy = Uow.Policies.GetAll() .Where(p => p.Id == vm.PolicyId) .Include(p => p.Endorsements) .FirstOrDefault(); if (policy != null && policy.Endorsements != null && policy.Endorsements.Count > 0) { var latestEndorsementNumber = policy.Endorsements .OrderByDescending(e => e.EffectiveDate) .FirstOrDefault() .EndorsementNumber; vm.EndorsementNumber = latestEndorsementNumber; } //For Particular Readonly Values vm.IsReadOnlyState = false; } if (vm.StatusName.ToLower() == "unpaid") { vm.PaidDate = DateTime.Now; } var policies = Uow.Policies.GetAll() .Where(p => p.ClientId == vm.ClientId) .ProjectTo <ClientPoliciesListModel>() .ToList(); vm.Policies = policies; vm.Clients = ListProviderSvc.GetClientsWithPoliciesSimpleList(); vm.IsFromPolicy = false; } else //Create { vm = new InvoiceItemViewModel { StatusName = "New", IssueDate = DateTime.Now, DueDate = DateTime.Now.AddMonths(1), IsFromPolicy = false }; if (policyId != null) { vm.IsFromPolicy = true; vm.Policies = Uow.Policies.GetAll() .Where(p => p.Id == policyId) .ProjectTo <ClientPoliciesListModel>() .ToList(); vm.PolicyId = vm.Policies[0].Id; vm.CompanyName = vm.Policies[0].CompanyName; vm.EndorsementNumber = vm.Policies[0].LatestEndorsementNumber; vm.Premium = vm.Policies[0].Premium; vm.Rebate = vm.Policies[0].Rebate; var item = Uow.Policies.GetAll() .Where(p => p.Id == policyId) .Select(p => p.Client) .FirstOrDefault(); var client = new ClientSimple { Id = item.Id, Email = item.Email, AccountNumber = item.AccountNumber }; if (item.IsOrganization) { client.ClientName = item.OrganizationName; } else { client.ClientName = item.LastName + ", " + item.FirstName; } vm.Clients = new List <ClientSimple>(); vm.Clients.Add(client); vm.ClientId = client.Id; vm.ClientName = client.ClientName; vm.AccountNumber = client.AccountNumber; } else { vm.Clients = ListProviderSvc.GetClientsWithPoliciesSimpleList(); } } vm.ReturnUrl = returnUrl; vm.ParticularTypesList = ListProviderSvc.GetParticularTypes(); return(View(vm)); }