public ActionResult PsrTargetBreakDownSave(List <PsrTargetBreak> psrTargetBreak) { List <tbld_Target_PSR_Details> psrTargetDetails = new List <tbld_Target_PSR_Details>(); if (psrTargetBreak != null) { int dbid = (int)Session["DBId"]; int tgtid = psrTargetBreak.Select(x => x.Tgtid).FirstOrDefault(); var dbTgt = Db.tbld_Target_Details.Where(x => x.db_id == dbid && x.target_id == tgtid); foreach (var item in psrTargetBreak) { foreach (var tgtitem in dbTgt) { tbld_Target_PSR_Details psrDetails = new tbld_Target_PSR_Details { sku_id = tgtitem.sku_id, target_id = tgtid, Pack_size = tgtitem.Pack_size, db_id = tgtitem.db_id, psr_id = item.PsrId, qtyinCS = Math.Floor((tgtitem.total_Qty / 100 * item.Contribution) / tgtitem.Pack_size), qtyinPS = Math.Floor((tgtitem.total_Qty / 100 * item.Contribution) % tgtitem.Pack_size), total_Qty = Math.Floor(tgtitem.total_Qty / 100 * item.Contribution) }; psrTargetDetails.Add(psrDetails); } } Db.tbld_Target_PSR_Details.RemoveRange(Db.tbld_Target_PSR_Details.Where(x => x.db_id == dbid && x.target_id == tgtid)); Db.SaveChanges(); Db.tbld_Target_PSR_Details.AddRange(psrTargetDetails); Db.SaveChanges(); } TempData["alertbox"] = "success"; TempData["alertboxMsg"] = " Target Break Down Successfully"; return(RedirectToAction("TargetBreakDownIndex")); }
public ActionResult UploadTargetbyPsr(HttpPostedFileBase fileUpload) { int targetid = 0; List <tbld_Target_PSR_Details> tbldTargetDetails = new List <tbld_Target_PSR_Details>(); if (fileUpload == null) { TempData["alertbox"] = "error"; TempData["alertboxMsg"] = " Target not uploaded"; return(RedirectToAction("Index")); } if (fileUpload.ContentType == "application/vnd.ms-excel" || fileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { string filename = fileUpload.FileName; if (filename.EndsWith(".xlsx")) { string targetpath = Server.MapPath("~/Upload/"); fileUpload.SaveAs(targetpath + filename); var pathToExcelFile = targetpath + filename; var excelFile = new ExcelQueryFactory(pathToExcelFile); var worksheetNames = excelFile.GetWorksheetNames(); string sheetName = worksheetNames.First(); var tgtList = from a in excelFile.Worksheet <TargetPsrDetailsVm>(sheetName) select a; foreach (var tgtListitem in tgtList) { tbld_Target_PSR_Details targetitem = new tbld_Target_PSR_Details() { target_id = tgtListitem.TargetId, db_id = tgtListitem.DbId, psr_id = tgtListitem.PsrId, sku_id = tgtListitem.SkuId, Pack_size = tgtListitem.PackSize, qtyinCS = tgtListitem.QtyinCs, qtyinPS = tgtListitem.QtyinPs, total_Qty = (tgtListitem.QtyinCs * tgtListitem.PackSize) + tgtListitem.QtyinPs }; targetid = tgtListitem.TargetId; tbldTargetDetails.Add(targetitem); Db.tbld_Target_PSR_Details.Add(targetitem); } var dbTargetDetails = (from a in tbldTargetDetails group a by new { a.target_id, a.db_id, a.sku_id, a.Pack_size, } into g select new tbld_Target_Details { target_id = g.Key.target_id, db_id = (int)g.Key.db_id, sku_id = (int)g.Key.sku_id, Pack_size = g.Key.Pack_size, qtyinCS = g.Sum(p => p.total_Qty) / g.Key.Pack_size, qtyinPS = g.Sum(p => p.total_Qty) % g.Key.Pack_size, total_Qty = g.Sum(p => p.total_Qty) }).ToList(); int tergetCount = Db.tbld_Target_PSR_Details.Count(x => x.target_id == targetid); if (tergetCount <= 0) { Db.tbld_Target_Details.AddRange(dbTargetDetails); if (tgtList.Count() == tbldTargetDetails.Count()) { Db.SaveChanges(); TempData["alertbox"] = "success"; TempData["alertboxMsg"] = " Target Upload Successfully"; return(RedirectToAction("Index")); } } else { TempData["alertbox"] = "error"; TempData["alertboxMsg"] = " Target Already uploaded"; return(RedirectToAction("Index")); } } } return(RedirectToAction("Index")); }