public JsonResult CreateRequisition(IEnumerable <VMRequisitionItem> RequisitionItems, int ProjectId, int SiteId, DateTime RequisitionDate, string ReqNo, string remarks) { var result = new { flag = false, message = "Requisition saving error !" }; var flag = false; var RequisitionId = 0;; //if (RequisitionId == null) //{ // RequisitionId = 0; //} var planList = db.Proc_RequisitionMas.Where(x => x.Rcode.Trim() == ReqNo.Trim()).ToList(); if (planList.Count == 0) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { flag = false; Proc_RequisitionMas master = new Proc_RequisitionMas(); var newProj = (from procProject in db.ProcProject join site in db.ProjectSite on procProject.ProjectSiteId equals SiteId join project in db.Project on site.ProjectId equals ProjectId where project.Id == ProjectId select procProject); foreach (var i in newProj) { master.ProcProjectId = i.Id; } master.ReqDate = RequisitionDate; master.Rcode = ReqNo; master.Remarks = remarks; master.Status = "N"; db.Proc_RequisitionMas.Add(master); flag = db.SaveChanges() > 0; var getReqId = db.Proc_RequisitionMas.SingleOrDefault(x => x.Rcode == ReqNo); RequisitionId = getReqId.Id; foreach (var item in RequisitionItems) { Proc_RequisitionDet detail = new Proc_RequisitionDet(); detail.ItemId = item.ItemId; detail.Proc_RequisitionMasId = RequisitionId; detail.ReqQty = item.ReqQty; detail.CStockQty = item.CStockQty; detail.Brand = item.Brand; detail.Size = item.Size; detail.RequiredDate = item.RequiredDate; detail.Remarks = item.ItemRemarks; db.Proc_RequisitionDet.Add(detail); db.SaveChanges(); } dbContextTransaction.Commit(); if (flag == true) { result = new { flag = true, message = "Save Successful!" }; } } catch (Exception ex) { dbContextTransaction.Rollback(); result = new { flag = false, message = ex.Message }; } } } else { result = new { flag = false, message = "Req No. already exists!" }; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public JsonResult CreateRequisition(IEnumerable <VMRequisitionItem> RequisitionItems, int ProjectId, int SiteId, DateTime RequisitionDate, string ReqNo, string remarks) { var result = new { flag = false, message = "Requisition saving error !" }; var flag = false; var RequisitionId = 0;; //if (RequisitionId == null) //{ // RequisitionId = 0; //} var planList = db.Proc_RequisitionMas.Where(x => x.Rcode.Trim() == ReqNo.Trim()).ToList(); if (planList.Count == 0) { flag = false; Proc_RequisitionMas master = new Proc_RequisitionMas(); //master.ProcProjectId = ProjectId; var newProj = (from procProject in db.ProcProject join site in db.ProjectSite on procProject.ProjectSiteId equals SiteId join project in db.Project on site.ProjectId equals ProjectId where project.Id == ProjectId select procProject); foreach (var i in newProj) { master.ProcProjectId = i.Id; } master.ReqDate = RequisitionDate; master.Rcode = ReqNo; master.Remarks = remarks; master.Status = "N"; db.Proc_RequisitionMas.Add(master); flag = db.SaveChanges() > 0; var getReqId = db.Proc_RequisitionMas.SingleOrDefault(x => x.Rcode == ReqNo); RequisitionId = getReqId.Id; foreach (var item in RequisitionItems) { Proc_RequisitionDet detail = new Proc_RequisitionDet(); detail.ItemId = item.ItemId; detail.Proc_RequisitionMasId = RequisitionId; detail.ReqQty = item.ReqQty; detail.CStockQty = item.CStockQty; detail.Brand = item.Brand; detail.Size = item.Size; detail.RequiredDate = item.RequiredDate; detail.Remarks = item.ItemRemarks; db.Proc_RequisitionDet.Add(detail); db.SaveChanges(); } } //else //{ // var Proc_RequisitionMasId = db.Proc_RequisitionMas.Where(x => x.Rcode.Trim() == ReqNo.Trim()).SingleOrDefault(); // var master = db.Proc_RequisitionMas.Find(Proc_RequisitionMasId.Id); // //var prevProj = (from procProject in db.ProcProject // // join site in db.ProjectSite on procProject.ProjectSiteId equals site.Id // // join project in db.Project on site.ProjectId equals project.Id // // where project.Id == ProjectId // // select project).SingleOrDefault(); // //master.ProcProjectId = prevProj.Id; // master.ReqDate = RequisitionDate; // master.Rcode = ReqNo; // master.Remarks = remarks; // //master.Status = "A"; // db.Entry(master).State = EntityState.Modified; // //db.Proc_RequisitionMas.Add(master); // flag = db.SaveChanges() > 0; // var getReqId = db.Proc_RequisitionMas.SingleOrDefault(x => x.Rcode == ReqNo); // RequisitionId = getReqId.Id; // foreach (var item in RequisitionItems) // { // var check = db.Proc_RequisitionDet.SingleOrDefault(x => x.ProcRequisitionMasId == RequisitionId && x.ItemId == item.ItemId); // if (check == null) { // Proc_RequisitionDet detail = new Proc_RequisitionDet(); // detail.ItemId = item.ItemId; // detail.ProcRequisitionMasId = RequisitionId; // detail.ReqQty = item.ReqQty; // detail.CStockQty = item.CStockQty; // detail.Brand = item.Brand; // detail.Size = item.Size; // detail.RequiredDate = item.RequiredDate; // detail.Remarks = item.ItemRemarks; // db.Entry(detail).State = EntityState.Added; // //db.Proc_RequisitionDet.Add(detail); // db.SaveChanges(); // } // else // { // var Proc_RequisitionDet_Id = db.Proc_RequisitionDet.SingleOrDefault(x => x.ProcRequisitionMasId == RequisitionId && x.ItemId == item.ItemId); // var getItem = db.Proc_RequisitionDet.Find(Proc_RequisitionDet_Id.Id); // getItem.CStockQty = item.CStockQty; // getItem.Brand = item.Brand; // getItem.Size = item.Size; // getItem.RequiredDate = item.RequiredDate; // getItem.Remarks = item.ItemRemarks; // db.Entry(getItem).State = EntityState.Modified; // db.SaveChanges(); // } // } //} if (flag == true) { result = new { flag = true, message = "Save Successful!" }; } return(Json(result, JsonRequestBehavior.AllowGet)); }