public async Task <IActionResult> Details(int id) { var reports = new Mamreq(); if (User.IsInRole("dataentry")) { reports = await _context.Mamreq.SingleOrDefaultAsync(m => m.Rid == id && m.UserName == User.Identity.Name); } if (User.IsInRole("administrator")) { var user = await _userManager.FindByNameAsync(User.Identity.Name); IQueryable <Mamreq> query = _context.Mamreq.Where(m => m.Rid == id); if (user.TenantId != 1) { query = _context.Mamreq.Where(m => m.Rid == id && m.Tenant.Equals(user.TenantId)); } else { query = _context.Mamreq.Where(m => m.Rid == id); } reports = await query.SingleOrDefaultAsync(); } if (reports == null) { return(NotFound()); } return(View("samdetail", reports)); }
public async Task <IActionResult> Create([Bind("ProvCode,ImpCode,Year,Month,ReqBy,numMonth")] mreqvm req) { if (ModelState.IsValid) { Mamreq report = new Mamreq(); report.UserName = User.Identity.Name; report.UpdateDate = DateTime.Now; var user = await _userManager.FindByNameAsync(User.Identity.Name); report.Tenant = user.TenantId; var nmrs = new List <MamreqVm>(); var mams = new List <MamreqVm>(); var nmrs2 = new List <MamreqVm>(); var mams2 = new List <MamreqVm>(); report.ReqBy = req.ReqBy; int year = req.Year; var numMonth = new List <int>(); var numMonth2 = new List <int>(); int year2 = 0; int month2 = 0; month2 = req.numMonth; while (req.numMonth != 0) { if (req.Month - req.numMonth > 0) { numMonth.Add(req.Month - req.numMonth); } else { switch (req.Month - req.numMonth) { case 0: numMonth.Add(12); break; case -1: numMonth.Add(11); break; case -2: numMonth.Add(10); break; case -3: numMonth.Add(9); break; } } req.numMonth = req.numMonth - 1; } if (numMonth.Contains(12)) { year2 = req.Year - 1; foreach (var item in numMonth.ToList()) { if (item == 12 || item == 11 || item == 10 || item == 9) { numMonth.Remove(item); } numMonth2.Add(item); } } string[] prov = req.ProvCode.Split('-'); nmrs = _context.Nmr.Where(m => m.Year == year && numMonth.Contains(m.Month)) .Where(m => m.Implementer == req.ImpCode && m.Facility.DistNavigation.ProvCode == prov[1].Trim()) .Select(m => new MamreqVm() { FacType = m.Facility.FacilityTypeNavigation.FacType, }).ToList(); mams = _context.TblMam.Where(m => m.Nmr.Year == year && numMonth.Contains(m.Nmr.Month)) .Where(m => m.Nmr.Implementer == req.ImpCode && m.Nmr.Facility.DistNavigation.ProvCode == prov[1].Trim()) .Select(m => new MamreqVm() { Muac12 = m.Muac12.GetValueOrDefault(), Muac23 = m.Muac23.GetValueOrDefault(), Zscore23 = m.Zscore23.GetValueOrDefault(), refIn = m.ReferIn.GetValueOrDefault(), AgeGroup = m.Mam.AgeGroup, }).ToList(); nmrs2 = _context.Nmr.Where(m => m.Year == year2 && numMonth2.Contains(m.Month)) .Where(m => m.Implementer == req.ImpCode && m.Facility.DistNavigation.ProvCode == prov[1].Trim()) .Select(m => new MamreqVm() { FacType = m.Facility.FacilityTypeNavigation.FacType, }).ToList(); mams2 = _context.TblMam.Where(m => m.Nmr.Year == year2 && numMonth2.Contains(m.Nmr.Month)) .Where(m => m.Nmr.Implementer == req.ImpCode && m.Nmr.Facility.DistNavigation.ProvCode == prov[1].Trim()) .Select(m => new MamreqVm() { Muac12 = m.Muac12.GetValueOrDefault(), Muac23 = m.Muac23.GetValueOrDefault(), Zscore23 = m.Zscore23.GetValueOrDefault(), refIn = m.ReferIn.GetValueOrDefault(), AgeGroup = m.Mam.AgeGroup, }).ToList(); mams.AddRange(mams2); nmrs.AddRange(nmrs2); if (nmrs.Any()) { report.Ph = (short)nmrs.Where(m => m.FacType.ToLower().Contains("h2")).Select(m => m.FacType).Distinct().Count(); report.Dh = (short)nmrs.Where(m => m.FacType.ToLower().Contains("h3")).Select(m => m.FacType).Distinct().Count(); report.Chc = (short)nmrs.Where(m => m.FacType.ToLower().Contains("chc")).Select(m => m.FacType).Distinct().Count(); report.Shc = (short)nmrs.Where(m => m.FacType.ToLower().Contains("sch")).Select(m => m.FacType).Distinct().Count(); report.Mht = (short)nmrs.Where(m => m.FacType.ToLower().Contains("mht")).Select(m => m.FacType).Distinct().Count(); report.Bhc = (short)nmrs.Where(m => m.FacType.ToLower().Contains("bhc")).Select(m => m.FacType).Distinct().Count(); } report.ProvCode = prov[0]; report.ImpCode = req.ImpCode; report.Year = req.Year; report.Month = (short)month2; report.ReqYear = req.Year; report.ReqMonth = (short)req.Month; _context.Mamreq.Add(report); await _context.SaveChangesAsync(); var mamitems = _context.TlkpFstock.Where(m => m.Zarib > 0).ToList(); if (mamitems.Any()) { foreach (var item in mamitems) { if (item.Zarib > 0 && mams.Any()) { if (item.Item.ToLower().Contains("supercereal")) { var row = new MamreqDetails(); row.SupplyId = item.StockId; row.Rid = report.Rid; row.NoOfBenificiaries = mams.Where(m => m.AgeGroup.ToLower().Contains("women")).Sum(m => m.Muac12 + m.Muac23 + m.Zscore23 + m.refIn); row.FormName = "WOMEN"; _context.MamreqDetails.Add(row); } if (item.Item.ToLower().Contains("rusf")) { var row2 = new MamreqDetails(); row2.SupplyId = item.StockId; row2.Rid = report.Rid; row2.NoOfBenificiaries = mams.Where(m => m.AgeGroup.ToLower().Contains("children")).Sum(m => m.Muac12 + m.Muac23 + m.Zscore23 + m.refIn); row2.FormName = "Children"; _context.MamreqDetails.Add(row2); } } } await _context.SaveChangesAsync(); } return(RedirectToAction("index")); } List <Provinces> Provinces = _context.Provinces.ToList(); foreach (var Province in Provinces) { Province.ProvName = Province.ProvName + "-" + Province.ProvCode; } ViewData["ProvList"] = new SelectList(Provinces, "ProvName", "ProvName"); var implementers = (from imp in _context.Implementers select imp).ToList(); ViewData["Implementers"] = new SelectList(implementers, "ImpCode", "ImpAcronym"); return(View(req)); }