public ActionResult Reviewed(int Id, string IndexType, string UserRemark, string IsContinue)
        {
            ProdOrderCancelHeader pd = new ProdOrderCancelHeaderService(_unitOfWork).Find(Id);

            if (ModelState.IsValid)
            {
                pd.ReviewCount = (pd.ReviewCount ?? 0) + 1;
                pd.ReviewBy   += User.Identity.Name + ", ";

                _ProdOrderCancelHeaderService.Update(pd);

                _unitOfWork.Save();

                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId    = pd.DocTypeId,
                    DocId        = pd.ProdOrderCancelHeaderId,
                    ActivityType = (int)ActivityTypeContants.Reviewed,
                    UserRemark   = UserRemark,
                    DocNo        = pd.DocNo,
                    DocDate      = pd.DocDate,
                    DocStatus    = pd.Status,
                }));

                return(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }).Success("Record reviewed successfully."));
            }

            return(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }).Warning("Error in reviewing."));
        }
Ejemplo n.º 2
0
        public ActionResult DeletePost(ProdOrderCancelLineViewModel vm)
        {
            List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

            ProdOrderCancelLine SaleOrderLine = _ProdOrderCancelLineService.Find(vm.ProdOrderCancelLineId);

            LogList.Add(new LogTypeViewModel
            {
                ExObj = SaleOrderLine,
            });

            ProdOrderCancelHeader header = new ProdOrderCancelHeaderService(_unitOfWork).Find(SaleOrderLine.ProdOrderCancelHeaderId);
            int status = header.Status;

            new ProdOrderLineStatusService(_unitOfWork).UpdateProdQtyOnCancel(SaleOrderLine.ProdOrderLineId, SaleOrderLine.ProdOrderCancelLineId, header.DocDate, 0, ref db, false);

            _ProdOrderCancelLineService.Delete(vm.ProdOrderCancelLineId);

            if (header.Status != (int)StatusConstants.Drafted)
            {
                header.Status = (int)StatusConstants.Modified;
                new ProdOrderCancelHeaderService(_unitOfWork).Update(header);
            }
            XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

            try
            {
                _unitOfWork.Save();
            }

            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                return(PartialView("_Create", vm));
            }

            LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
            {
                DocTypeId       = header.DocTypeId,
                DocId           = header.ProdOrderCancelHeaderId,
                DocLineId       = SaleOrderLine.ProdOrderCancelLineId,
                ActivityType    = (int)ActivityTypeContants.Deleted,
                DocNo           = header.DocNo,
                xEModifications = Modifications,
                DocDate         = header.DocDate,
                DocStatus       = header.Status,
            }));

            return(Json(new { success = true }));
        }
Ejemplo n.º 3
0
        public ActionResult _Create(int Id) //Id ==>Prod Order Cancel Header Id
        {
            ProdOrderCancelHeader        Header = new ProdOrderCancelHeaderService(_unitOfWork).Find(Id);
            ProdOrderCancelLineViewModel svm    = new ProdOrderCancelLineViewModel();

            //Getting Settings
            var settings = new ProdOrderSettingsService(_unitOfWork).GetProdOrderSettingsForDocument(Header.DocTypeId, Header.DivisionId, Header.SiteId);

            svm.ProdOrderSettings = Mapper.Map <ProdOrderSettings, ProdOrderSettingsViewModel>(settings);

            svm.ProdOrderCancelHeaderId = Id;
            ViewBag.LineMode            = "Create";
            svm.DivisionId = Header.DivisionId;
            return(PartialView("_Create", svm));
        }
Ejemplo n.º 4
0
        public ActionResult _Detail(int id)
        {
            ProdOrderCancelLineViewModel temp = _ProdOrderCancelLineService.GetProdOrderCancelLine(id);

            if (temp == null)
            {
                return(HttpNotFound());
            }

            ProdOrderCancelHeader H = new ProdOrderCancelHeaderService(_unitOfWork).Find(temp.ProdOrderCancelHeaderId);
            //Getting Settings
            var settings = new ProdOrderSettingsService(_unitOfWork).GetProdOrderSettingsForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            temp.ProdOrderSettings = Mapper.Map <ProdOrderSettings, ProdOrderSettingsViewModel>(settings);


            return(PartialView("_Create", temp));
        }
Ejemplo n.º 5
0
        private ActionResult _Delete(int id)
        {
            ProdOrderCancelLineViewModel temp = _ProdOrderCancelLineService.GetProdOrderCancelLine(id);

            if (temp == null)
            {
                return(HttpNotFound());
            }

            #region DocTypeTimeLineValidation
            try
            {
                TimePlanValidation = DocumentValidation.ValidateDocumentLine(new DocumentUniqueId {
                    LockReason = temp.LockReason
                }, User.Identity.Name, out ExceptionMsg, out Continue);
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXCL"] += message;
                TimePlanValidation  = false;
            }

            if (!TimePlanValidation)
            {
                TempData["CSEXCL"] += ExceptionMsg;
            }
            #endregion

            if ((TimePlanValidation || Continue))
            {
                ViewBag.LineMode = "Delete";
            }

            ProdOrderCancelHeader H = new ProdOrderCancelHeaderService(_unitOfWork).Find(temp.ProdOrderCancelHeaderId);
            //Getting Settings
            var settings = new ProdOrderSettingsService(_unitOfWork).GetProdOrderSettingsForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            temp.ProdOrderSettings = Mapper.Map <ProdOrderSettings, ProdOrderSettingsViewModel>(settings);


            return(PartialView("_Create", temp));
        }
        public ActionResult Submitted(int Id, string IndexType, string UserRemark, string IsContinue)
        {
            ProdOrderCancelHeader pd = new ProdOrderCancelHeaderService(_unitOfWork).Find(Id);
            int ActivityType;

            if (ModelState.IsValid)
            {
                if (User.Identity.Name == pd.ModifiedBy || UserRoles.Contains("Admin"))
                {
                    pd.Status    = (int)StatusConstants.Submitted;
                    ActivityType = (int)ActivityTypeContants.Submitted;
                    pd.ReviewBy  = null;

                    _ProdOrderCancelHeaderService.Update(pd);
                    _unitOfWork.Save();

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = pd.DocTypeId,
                        DocId        = pd.ProdOrderCancelHeaderId,
                        ActivityType = ActivityType,
                        UserRemark   = UserRemark,
                        DocNo        = pd.DocNo,
                        DocDate      = pd.DocDate,
                        DocStatus    = pd.Status,
                    }));

                    return(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }).Success("Record submitted successfully."));
                }
                else
                {
                    return(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }).Warning("Record can be submitted by user " + pd.ModifiedBy + " only."));
                }
            }

            return(View());
        }
        public ActionResult DeleteConfirmed(ReasonViewModel vm)
        {
            if (ModelState.IsValid)
            {
                List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                db.Configuration.AutoDetectChangesEnabled = false;
                var temp = _MaterialPlanCancelHeaderService.Find(vm.id);

                int status = temp.Status;

                LogList.Add(new LogTypeViewModel
                {
                    ExObj = temp,
                });

                var MaterialPlanCancelline = new MaterialPlanCancelLineService(_unitOfWork).GetMaterialPlanCancelForDelete(vm.id).ToList();

                List <ProdOrderCancelLine>      ProdOrderCancelLines      = new List <ProdOrderCancelLine>();
                List <PurchaseIndentCancelLine> PurchaseIndentCancelLines = new List <PurchaseIndentCancelLine>();


                foreach (var item in MaterialPlanCancelline)
                {
                    //Deleting MaterialPlanCancelforSaleOrder
                    var MaterialPlanCancelForSaleOrder = new MaterialPlanCancelForSaleOrderService(_unitOfWork).GetMaterialPlanCancelForSaleOrderForMaterialPlanCancelline(item.MaterialPlanCancelLineId).ToList();
                    foreach (var item2 in MaterialPlanCancelForSaleOrder)
                    {
                        LogList.Add(new LogTypeViewModel
                        {
                            ExObj = item2,
                        });

                        item2.ObjectState = Model.ObjectState.Deleted;
                        //new MaterialPlanCancelForSaleOrderService(_unitOfWork).Delete(item2);
                        db.MaterialPlanCancelForSaleOrder.Attach(item2);
                        db.MaterialPlanCancelForSaleOrder.Remove(item2);
                    }

                    //Deleting MaterialPlanCancelForProdOrderLine
                    var MaterialPlanCancelForProdOrderLine = new MaterialPlanCancelForProdOrderLineService(_unitOfWork).GetMaterialPlanCancelForProdORderForMaterialPlanCancel(item.MaterialPlanCancelLineId).ToList();
                    foreach (var item2 in MaterialPlanCancelForProdOrderLine)
                    {
                        LogList.Add(new LogTypeViewModel
                        {
                            ExObj = item2,
                        });

                        item2.ObjectState = Model.ObjectState.Deleted;
                        //new MaterialPlanCancelForProdOrderLineService(_unitOfWork).Delete(item2.MaterialPlanCancelForProdOrderLineId);
                        db.MaterialPlanCancelForProdOrderLine.Attach(item2);
                        db.MaterialPlanCancelForProdOrderLine.Remove(item2);
                    }

                    //new MaterialPlanCancelLineService(_unitOfWork).Delete(item.MaterialPlanCancelLineId);

                    //MaterialPlanCancelLine Si = (MaterialPlanCancelLine) item;



                    ProdOrderCancelLines = new ProdOrderCancelLineService(_unitOfWork).GetProdOrderCancelLineForMaterialPlanCancel(item.MaterialPlanCancelLineId).ToList();

                    foreach (var item2 in ProdOrderCancelLines)
                    {
                        LogList.Add(new LogTypeViewModel
                        {
                            ExObj = item2,
                        });

                        item2.ObjectState = Model.ObjectState.Deleted;
                        db.ProdOrderCancelLine.Attach(item2);
                        db.ProdOrderCancelLine.Remove(item2);
                    }

                    PurchaseIndentCancelLines = new PurchaseIndentCancelLineService(_unitOfWork).GetPurchaseIndentCancelLineForMaterialPlanCancel(item.MaterialPlanCancelLineId).ToList();

                    foreach (var item2 in PurchaseIndentCancelLines)
                    {
                        LogList.Add(new LogTypeViewModel
                        {
                            ExObj = item2,
                        });

                        item2.ObjectState = Model.ObjectState.Deleted;
                        db.PurchaseIndentCancelLine.Attach(item2);
                        db.PurchaseIndentCancelLine.Remove(item2);
                    }



                    MaterialPlanCancelLine MPL = new MaterialPlanCancelLineService(_unitOfWork).Find(item.MaterialPlanCancelLineId);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = MPL,
                    });

                    MPL.ObjectState = Model.ObjectState.Deleted;

                    db.MaterialPlanCancelLine.Attach(MPL);
                    db.MaterialPlanCancelLine.Remove(MPL);
                }

                //Deleting MaterialPlanCancelForProdORder
                var MaterialPlanCancelForProdOrder = new MaterialPlanCancelForProdOrderService(_unitOfWork).GetMaterialPlanCancelForProdORderForMaterialPlanCancel(vm.id).ToList();
                foreach (var item2 in MaterialPlanCancelForProdOrder)
                {
                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = item2,
                    });

                    item2.ObjectState = Model.ObjectState.Deleted;
                    //new MaterialPlanCancelForProdOrderService(_unitOfWork).Delete(item2);
                    db.MaterialPlanCancelForProdOrder.Attach(item2);
                    db.MaterialPlanCancelForProdOrder.Remove(item2);
                }



                ProdOrderCancelHeader ProdOrderCancelHeaders = new ProdOrderCancelHeaderService(_unitOfWork).GetProdOrderCancelForMaterialPlan(vm.id);


                LogList.Add(new LogTypeViewModel
                {
                    ExObj = ProdOrderCancelHeaders,
                });

                ProdOrderCancelHeaders.ObjectState = Model.ObjectState.Deleted;
                // new ProdOrderHeaderService(_unitOfWork).Delete(item2.ProdOrderHeaderId);
                db.ProdOrderCancelHeader.Attach(ProdOrderCancelHeaders);
                db.ProdOrderCancelHeader.Remove(ProdOrderCancelHeaders);



                temp.ObjectState = Model.ObjectState.Deleted;
                // _MaterialPlanCancelHeaderService.Delete(temp);
                db.MaterialPlanCancelHeader.Attach(temp);
                db.MaterialPlanCancelHeader.Remove(temp);

                XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

                try
                {
                    // _unitOfWork.Save();
                    db.SaveChanges();
                    db.Configuration.AutoDetectChangesEnabled = true;
                }

                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXC"] += message;
                    return(PartialView("_Reason", vm));
                }

                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId       = temp.DocTypeId,
                    DocId           = temp.MaterialPlanCancelHeaderId,
                    ActivityType    = (int)ActivityTypeContants.Deleted,
                    UserRemark      = vm.Reason,
                    DocNo           = temp.DocNo,
                    xEModifications = Modifications,
                    DocDate         = temp.DocDate,
                    DocStatus       = temp.Status,
                }));

                return(Json(new { success = true }));
            }
            return(PartialView("_Reason", vm));
        }
Ejemplo n.º 8
0
        public ActionResult _ResultsPost(ProdOrderCancelMasterDetailModel vm)
        {
            Dictionary <int, decimal> LineStatus = new Dictionary <int, decimal>();
            var Header = new ProdOrderCancelHeaderService(_unitOfWork).Find(vm.ProdOrderCancelViewModels.FirstOrDefault().ProdOrderCancelHeaderId);

            if (ModelState.IsValid)
            {
                foreach (var item in vm.ProdOrderCancelViewModels)
                {
                    decimal balqty = (from p in db.ViewProdOrderBalance
                                      where p.ProdOrderLineId == item.ProdOrderLineId
                                      select p.BalanceQty).FirstOrDefault();

                    if (item.Qty > 0 && item.Qty <= balqty)
                    {
                        ProdOrderCancelLine line = new ProdOrderCancelLine();

                        line.ProdOrderCancelHeaderId = item.ProdOrderCancelHeaderId;
                        line.ProdOrderLineId         = item.ProdOrderLineId;
                        line.Qty          = item.Qty;
                        line.CreatedDate  = DateTime.Now;
                        line.ModifiedDate = DateTime.Now;
                        line.CreatedBy    = User.Identity.Name;
                        line.ModifiedBy   = User.Identity.Name;
                        LineStatus.Add(line.ProdOrderLineId, line.Qty);
                        _ProdOrderCancelLineService.Create(line);
                    }
                }

                new ProdOrderLineStatusService(_unitOfWork).UpdateProdQtyCancelMultiple(LineStatus, Header.DocDate);

                if (Header.Status != (int)StatusConstants.Drafted && Header.Status != (int)StatusConstants.Import)
                {
                    Header.Status       = (int)StatusConstants.Modified;
                    Header.ModifiedBy   = User.Identity.Name;
                    Header.ModifiedDate = DateTime.Now;
                    Header.ObjectState  = Model.ObjectState.Modified;
                }

                new ProdOrderCancelHeaderService(_unitOfWork).Update(Header);

                try
                {
                    _unitOfWork.Save();
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXCL"] += message;
                    return(PartialView("_Results", vm));
                }

                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId    = Header.DocTypeId,
                    DocId        = Header.ProdOrderCancelHeaderId,
                    ActivityType = (int)ActivityTypeContants.MultipleCreate,
                    DocNo        = Header.DocNo,
                    DocDate      = Header.DocDate,
                    DocStatus    = Header.Status,
                }));

                return(Json(new { success = true }));
            }
            return(PartialView("_Results", vm));
        }
Ejemplo n.º 9
0
        public ActionResult _CreatePost(ProdOrderCancelLineViewModel svm)
        {
            if (svm.ProdOrderCancelLineId <= 0)
            {
                ViewBag.LineMode = "Create";
            }
            else
            {
                ViewBag.LineMode = "Edit";
            }

            if (svm.ProdOrderCancelLineId <= 0)
            {
                ProdOrderCancelLine s      = new ProdOrderCancelLine();
                decimal             balqty = (from p in db.ViewProdOrderBalance
                                              where p.ProdOrderLineId == svm.ProdOrderLineId
                                              select p.BalanceQty).FirstOrDefault();
                if (balqty < svm.Qty)
                {
                    ModelState.AddModelError("Qty", "Qty Exceeding Balance Qty");
                }
                if (svm.Qty == 0)
                {
                    ModelState.AddModelError("Qty", "Please Check Qty");
                }
                if (svm.ProdOrderLineId == 0)
                {
                    ModelState.AddModelError("ProdOrderLineId", "The Prod Order field is required");
                }
                if (ModelState.IsValid)
                {
                    s.ProdOrderCancelHeaderId = svm.ProdOrderCancelHeaderId;
                    s.ProdOrderLineId         = svm.ProdOrderLineId;
                    s.Qty          = svm.Qty;
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    _ProdOrderCancelLineService.Create(s);

                    ProdOrderCancelHeader temp2 = new ProdOrderCancelHeaderService(_unitOfWork).Find(s.ProdOrderCancelHeaderId);

                    new ProdOrderLineStatusService(_unitOfWork).UpdateProdQtyOnCancel(s.ProdOrderLineId, s.ProdOrderCancelLineId, temp2.DocDate, s.Qty, ref db, false);


                    if (temp2.Status != (int)StatusConstants.Drafted && temp2.Status != (int)StatusConstants.Import)
                    {
                        temp2.Status       = (int)StatusConstants.Modified;
                        temp2.ModifiedBy   = User.Identity.Name;
                        temp2.ModifiedDate = DateTime.Now;
                    }
                    new ProdOrderCancelHeaderService(_unitOfWork).Update(temp2);

                    try
                    {
                        _unitOfWork.Save();
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        return(PartialView("_Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = temp2.DocTypeId,
                        DocId        = temp2.ProdOrderCancelHeaderId,
                        DocLineId    = s.ProdOrderCancelLineId,
                        ActivityType = (int)ActivityTypeContants.Added,
                        DocNo        = temp2.DocNo,
                        DocDate      = temp2.DocDate,
                        DocStatus    = temp2.Status,
                    }));

                    return(RedirectToAction("_Create", new { id = s.ProdOrderCancelHeaderId }));
                }
                return(PartialView("_Create", svm));
            }
            else
            {
                List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                ProdOrderCancelHeader temp = new ProdOrderCancelHeaderService(_unitOfWork).Find(svm.ProdOrderCancelHeaderId);
                int           status       = temp.Status;
                StringBuilder logstring    = new StringBuilder();

                ProdOrderCancelLine s = _ProdOrderCancelLineService.Find(svm.ProdOrderCancelLineId);

                ProdOrderCancelLine ExRec = new ProdOrderCancelLine();
                ExRec = Mapper.Map <ProdOrderCancelLine>(s);

                decimal balqty = (from p in db.ViewProdOrderBalance
                                  where p.ProdOrderLineId == svm.ProdOrderLineId
                                  select p.BalanceQty).FirstOrDefault();
                if (balqty + s.Qty < svm.Qty)
                {
                    ModelState.AddModelError("Qty", "Qty Exceeding Balance Qty");
                }


                if (ModelState.IsValid)
                {
                    if (svm.Qty > 0)
                    {
                        s.Qty          = svm.Qty;
                        s.ModifiedBy   = User.Identity.Name;
                        s.ModifiedDate = DateTime.Now;
                    }

                    _ProdOrderCancelLineService.Update(s);

                    new ProdOrderLineStatusService(_unitOfWork).UpdateProdQtyOnCancel(s.ProdOrderLineId, s.ProdOrderCancelLineId, temp.DocDate, s.Qty, ref db, false);

                    temp.Status = (int)StatusConstants.Modified;
                    new ProdOrderCancelHeaderService(_unitOfWork).Update(temp);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = s,
                    });
                    XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);
                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        return(PartialView("_Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = temp.DocTypeId,
                        DocId           = s.ProdOrderCancelHeaderId,
                        DocLineId       = s.ProdOrderCancelLineId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.DocNo,
                        xEModifications = Modifications,
                        DocDate         = temp.DocDate,
                        DocStatus       = temp.Status,
                    }));

                    return(Json(new { success = true }));
                }
                return(PartialView("_Create", svm));
            }
        }
        public ActionResult _ResultsPost(MaterialPlanCancelLineListViewModel vm)
        {
            MaterialPlanCancelHeader header = new MaterialPlanCancelHeaderService(_unitOfWork).Find(vm.MaterialPlanCancelLineViewModel.FirstOrDefault().MaterialPlanCancelHeaderId);

            MaterialPlanSettings Setting = new MaterialPlanSettingsService(_unitOfWork).GetMaterialPlanSettingsForDocument(header.DocTypeId, header.DivisionId, header.SiteId);

            if (ModelState.IsValid)
            {
                var ProductIds = vm.MaterialPlanCancelLineViewModel.Select(m => m.ProductId).ToArray();

                List <MaterialPlanCancelLineViewModel> Line = new List <MaterialPlanCancelLineViewModel>();

                int i      = 0;
                int si     = 0;
                int linePk = 0;
                int Cnt    = 0;

                var mPlanLineIds = vm.MaterialPlanCancelLineViewModel.Where(m => m.Qty > 0 && m.Qty <= m.BalanceQty).Select(m => m.MaterialPlanLineId).ToList();


                //var mPlanRecords = db.ViewMaterialPlanForSaleOrderBalance.AsNoTracking().Where(m => mPlanLineIds.Contains(m.MaterialPlanLineId.Value)).ToList();
                //Changed because ViewMaterialPlanForSaleOrderBalance is not required.
                var mPlanRecords = db.MaterialPlanForSaleOrder.AsNoTracking().Where(m => mPlanLineIds.Contains(m.MaterialPlanLineId.Value)).ToList();

                foreach (var item in vm.MaterialPlanCancelLineViewModel.Where(m => m.Qty > 0 && m.Qty <= m.BalanceQty))
                {
                    MaterialPlanCancelLine cline = new MaterialPlanCancelLine();
                    cline.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                    cline.CreatedBy          = User.Identity.Name;
                    cline.CreatedDate        = DateTime.Now;
                    cline.MaterialPlanLineId = item.MaterialPlanLineId;
                    cline.CreatedBy          = User.Identity.Name;
                    cline.CreatedDate        = DateTime.Now;
                    cline.ModifiedBy         = User.Identity.Name;
                    cline.ModifiedDate       = DateTime.Now;
                    cline.ObjectState        = Model.ObjectState.Added;
                    cline.Qty    = item.Qty;
                    cline.Remark = item.Remark;
                    cline.Sr     = i++;
                    cline.MaterialPlanCancelLineId = linePk++;
                    cline.ObjectState = Model.ObjectState.Added;
                    db.MaterialPlanCancelLine.Add(cline);

                    SqlParameter SqlParameterMaterialPlanLineId = new SqlParameter("@MaterialPlanLineId", cline.MaterialPlanLineId);
                    SqlParameter SqlParameterQty = new SqlParameter("@Qty", cline.Qty);

                    IEnumerable <MaterialPlanForSaleOrderFifo> MaterialPlanForSaleOrderFifo = db.Database.SqlQuery <MaterialPlanForSaleOrderFifo>("" + ConfigurationManager.AppSettings["DataBaseSchema"] + ".sp_GetMaterialPlanForSaleOrderFifo @MaterialPlanLineId, @Qty", SqlParameterMaterialPlanLineId, SqlParameterQty).ToList();


                    //foreach (var detailSo in mPlanRecords.Where(m => m.MaterialPlanLineId == item.MaterialPlanLineId).ToList())
                    foreach (var detailSo in MaterialPlanForSaleOrderFifo)
                    {
                        MaterialPlanCancelForSaleOrder cso = new MaterialPlanCancelForSaleOrder();
                        cso.CreatedBy    = User.Identity.Name;
                        cso.CreatedDate  = DateTime.Now;
                        cso.ModifiedBy   = User.Identity.Name;
                        cso.ModifiedDate = DateTime.Now;
                        cso.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                        cso.MaterialPlanCancelLineId   = cline.MaterialPlanCancelLineId;
                        cso.MaterialPlanForSaleOrderId = detailSo.MaterialPlanForSaleOrderId;
                        cso.Qty         = detailSo.Qty;
                        cso.Sr          = si++;
                        cso.ObjectState = Model.ObjectState.Added;
                        db.MaterialPlanCancelForSaleOrder.Add(cso);
                    }


                    var MaterialPlanLine        = new MaterialPlanLineService(_unitOfWork).Find(item.MaterialPlanLineId);
                    int ProdOrderCancelHeaderId = 0;
                    if (MaterialPlanLine.ProdPlanQty > 0)
                    {
                        ProdOrderCancelHeader ExistingProdOrderCancel = new ProdOrderCancelHeaderService(_unitOfWork).GetProdOrderCancelForMaterialPlan(header.MaterialPlanCancelHeaderId);
                        ProdOrderCancelHeader ProdOrderCancelHeader   = new ProdOrderCancelHeader();

                        if (ExistingProdOrderCancel == null && Cnt == 0)
                        {
                            ProdOrderCancelHeader.CreatedBy   = User.Identity.Name;
                            ProdOrderCancelHeader.CreatedDate = DateTime.Now;
                            ProdOrderCancelHeader.DivisionId  = header.DivisionId;
                            ProdOrderCancelHeader.DocDate     = header.DocDate;
                            ProdOrderCancelHeader.DocNo       = header.DocNo;
                            ProdOrderCancelHeader.DocTypeId   = Setting.DocTypeProductionOrderId.Value;
                            ProdOrderCancelHeader.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                            ProdOrderCancelHeader.ModifiedBy   = User.Identity.Name;
                            ProdOrderCancelHeader.ModifiedDate = DateTime.Now;
                            ProdOrderCancelHeader.Remark       = header.Remark;
                            ProdOrderCancelHeader.SiteId       = header.SiteId;
                            ProdOrderCancelHeader.Status       = (int)StatusConstants.System;
                            ProdOrderCancelHeader.ObjectState  = Model.ObjectState.Added;
                            db.ProdOrderCancelHeader.Add(ProdOrderCancelHeader);
                            ProdOrderCancelHeaderId = ProdOrderCancelHeader.ProdOrderCancelHeaderId;

                            Cnt = Cnt + 1;
                        }
                        else
                        {
                            if (ExistingProdOrderCancel == null)
                            {
                                ProdOrderCancelHeaderId = ProdOrderCancelHeader.ProdOrderCancelHeaderId;
                            }
                            else
                            {
                                ProdOrderCancelHeaderId = ExistingProdOrderCancel.ProdOrderCancelHeaderId;
                            }
                        }


                        var ProdOrderLine          = new ProdOrderLineService(_unitOfWork).GetProdOrderLineForMaterialPlan(item.MaterialPlanLineId);
                        int ProdOrderCancelLineKey = 0;
                        ProdOrderCancelLine ProdOrderCancelLine = new ProdOrderCancelLine();
                        ProdOrderCancelLine.ProdOrderCancelLineId    = linePk++;
                        ProdOrderCancelLine.CreatedBy                = User.Identity.Name;
                        ProdOrderCancelLine.CreatedDate              = DateTime.Now;
                        ProdOrderCancelLine.ProdOrderLineId          = ProdOrderLine.FirstOrDefault().ProdOrderLineId;
                        ProdOrderCancelLine.ModifiedBy               = User.Identity.Name;
                        ProdOrderCancelLine.ModifiedDate             = DateTime.Now;
                        ProdOrderCancelLine.ProdOrderCancelHeaderId  = ProdOrderCancelHeaderId;
                        ProdOrderCancelLine.MaterialPlanCancelLineId = cline.MaterialPlanCancelLineId;
                        ProdOrderCancelLine.Qty = item.Qty;
                        ProdOrderCancelLine.ProdOrderCancelLineId = ProdOrderCancelLineKey--;
                        ProdOrderCancelLine.ObjectState           = Model.ObjectState.Added;
                        db.ProdOrderCancelLine.Add(ProdOrderCancelLine);
                    }


                    if (MaterialPlanLine.PurchPlanQty > 0)
                    {
                        PurchaseIndentCancelHeader ExistingPurchaseIndentCancel = new PurchaseIndentCancelHeaderService(_unitOfWork).GetPurchaseIndentCancelForMaterialPlan(header.MaterialPlanCancelHeaderId);

                        if (ExistingPurchaseIndentCancel == null)
                        {
                            PurchaseIndentCancelHeader PurchaseIndentCancelHeader = new PurchaseIndentCancelHeader();

                            PurchaseIndentCancelHeader.CreatedBy   = User.Identity.Name;
                            PurchaseIndentCancelHeader.CreatedDate = DateTime.Now;
                            PurchaseIndentCancelHeader.DivisionId  = header.DivisionId;
                            PurchaseIndentCancelHeader.DocDate     = header.DocDate;
                            PurchaseIndentCancelHeader.DocNo       = header.DocNo;
                            PurchaseIndentCancelHeader.DocTypeId   = Setting.DocTypeProductionOrderId.Value;
                            PurchaseIndentCancelHeader.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                            PurchaseIndentCancelHeader.ModifiedBy   = User.Identity.Name;
                            PurchaseIndentCancelHeader.ModifiedDate = DateTime.Now;
                            PurchaseIndentCancelHeader.Remark       = header.Remark;
                            PurchaseIndentCancelHeader.SiteId       = header.SiteId;
                            PurchaseIndentCancelHeader.Status       = (int)StatusConstants.System;
                            PurchaseIndentCancelHeader.ObjectState  = Model.ObjectState.Added;
                            db.PurchaseIndentCancelHeader.Add(PurchaseIndentCancelHeader);
                        }


                        var PurchaseIndentLine          = new PurchaseIndentLineService(_unitOfWork).GetPurchaseIndentLineForMaterialPlan(item.MaterialPlanLineId);
                        int PurchaseIndentCancelLineKey = 0;
                        PurchaseIndentCancelLine PurchaseIndentCancelLine = new PurchaseIndentCancelLine();
                        PurchaseIndentCancelLine.CreatedBy                    = User.Identity.Name;
                        PurchaseIndentCancelLine.CreatedDate                  = DateTime.Now;
                        PurchaseIndentCancelLine.PurchaseIndentLineId         = PurchaseIndentLine.FirstOrDefault().PurchaseIndentLineId;
                        PurchaseIndentCancelLine.ModifiedBy                   = User.Identity.Name;
                        PurchaseIndentCancelLine.ModifiedDate                 = DateTime.Now;
                        PurchaseIndentCancelLine.PurchaseIndentCancelHeaderId = ExistingPurchaseIndentCancel.PurchaseIndentCancelHeaderId;
                        PurchaseIndentCancelLine.MaterialPlanCancelLineId     = cline.MaterialPlanCancelLineId;
                        PurchaseIndentCancelLine.Qty = item.Qty;
                        PurchaseIndentCancelLine.PurchaseIndentCancelLineId = PurchaseIndentCancelLineKey--;
                        PurchaseIndentCancelLine.ObjectState = Model.ObjectState.Added;
                        db.PurchaseIndentCancelLine.Add(PurchaseIndentCancelLine);
                    }
                }

                try
                {
                    db.SaveChanges();
                }

                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    ModelState.AddModelError("", message);
                    return(PartialView("_Results", vm));
                }

                return(Json(new { success = true }));
            }
            return(PartialView("_Results", vm));
        }
        public ActionResult _ResultsPostProduction(MaterialPlanCancelLineListViewModel vm)
        {
            MaterialPlanCancelHeader header = new MaterialPlanCancelHeaderService(_unitOfWork).Find(vm.MaterialPlanCancelLineViewModel.FirstOrDefault().MaterialPlanCancelHeaderId);

            MaterialPlanSettings Setting = new MaterialPlanSettingsService(_unitOfWork).GetMaterialPlanSettingsForDocument(header.DocTypeId, header.DivisionId, header.SiteId);

            if (ModelState.IsValid)
            {
                var ProductIds = vm.MaterialPlanCancelLineViewModel.Select(m => m.ProductId).ToArray();

                List <MaterialPlanCancelLineViewModel> Line = new List <MaterialPlanCancelLineViewModel>();

                int i        = 0;
                int si       = 0;
                int linePk   = 0;
                int poLinePK = 0;

                var mPlanLineIds = vm.MaterialPlanCancelLineViewModel.Where(m => m.Qty > 0 && m.Qty == m.BalanceQty).Select(m => m.MaterialPlanLineId).ToList();

                var mPlanProdOrderLineRecords = db.ViewMaterialPlanForProdOrderLineBalance.AsNoTracking().Where(m => mPlanLineIds.Contains(m.MaterialPlanLineId.Value)).ToList();

                var mPlanProdOrderIds = mPlanProdOrderLineRecords.Select(m => m.MaterialPlanForProdOrderId).Distinct().ToList();

                var mPlanProdOrderRecords = db.ViewMaterialPlanForProdOrderBalance.AsNoTracking().Where(m => mPlanProdOrderIds.Contains(m.MaterialPlanForProdOrderId)).ToList();


                foreach (var item in vm.MaterialPlanCancelLineViewModel.Where(m => m.Qty > 0 && m.Qty == m.BalanceQty))
                {
                    MaterialPlanCancelLine cline = new MaterialPlanCancelLine();
                    cline.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                    cline.CreatedBy          = User.Identity.Name;
                    cline.CreatedDate        = DateTime.Now;
                    cline.MaterialPlanLineId = item.MaterialPlanLineId;
                    cline.CreatedBy          = User.Identity.Name;
                    cline.CreatedDate        = DateTime.Now;
                    cline.ModifiedBy         = User.Identity.Name;
                    cline.ModifiedDate       = DateTime.Now;
                    cline.ObjectState        = Model.ObjectState.Added;
                    cline.Qty    = item.Qty;
                    cline.Remark = item.Remark;
                    cline.Sr     = i++;
                    cline.MaterialPlanCancelLineId = linePk++;
                    cline.ObjectState = Model.ObjectState.Added;
                    db.MaterialPlanCancelLine.Add(cline);

                    var poLineRecords = mPlanProdOrderLineRecords.Where(m => m.MaterialPlanLineId == item.MaterialPlanLineId).ToList();

                    var ProdOrder = mPlanProdOrderRecords.Where(m => m.MaterialPlanForProdOrderId == poLineRecords.Select(t => t.MaterialPlanForProdOrderId).FirstOrDefault()).FirstOrDefault();

                    MaterialPlanCancelForProdOrder poLine = new MaterialPlanCancelForProdOrder();
                    poLine.CreatedBy   = User.Identity.Name;
                    poLine.CreatedDate = DateTime.Now;
                    poLine.MaterialPlanCancelHeaderId       = header.MaterialPlanCancelHeaderId;
                    poLine.MaterialPlanLineId               = cline.MaterialPlanLineId;
                    poLine.MaterialPlanCancelForProdOrderId = poLinePK++;
                    poLine.ModifiedDate = DateTime.Now;
                    poLine.ModifiedBy   = User.Identity.Name;
                    poLine.ObjectState  = Model.ObjectState.Added;
                    poLine.Qty          = ProdOrder.BalanceQty;
                    poLine.Sr           = cline.Sr;
                    poLine.ObjectState  = Model.ObjectState.Added;
                    db.MaterialPlanCancelForProdOrder.Add(poLine);


                    foreach (var detailSo in poLineRecords)
                    {
                        MaterialPlanCancelForProdOrderLine cso = new MaterialPlanCancelForProdOrderLine();
                        cso.MaterialPlanCancelLineId         = cline.MaterialPlanCancelLineId;
                        cso.MaterialPlanCancelForProdOrderId = poLine.MaterialPlanCancelForProdOrderId;
                        cso.Qty         = detailSo.BalanceQty;
                        cso.Sr          = si++;
                        cso.ObjectState = Model.ObjectState.Added;
                        db.MaterialPlanCancelForProdOrderLine.Add(cso);
                    }


                    var MaterialPlanLine = new MaterialPlanLineService(_unitOfWork).Find(item.MaterialPlanLineId);

                    if (MaterialPlanLine.ProdPlanQty > 0)
                    {
                        ProdOrderCancelHeader ExistingProdOrderCancel = new ProdOrderCancelHeaderService(_unitOfWork).GetProdOrderCancelForMaterialPlan(header.MaterialPlanCancelHeaderId);

                        if (ExistingProdOrderCancel == null)
                        {
                            ProdOrderCancelHeader ProdOrderCancelHeader = new ProdOrderCancelHeader();

                            ProdOrderCancelHeader.CreatedBy   = User.Identity.Name;
                            ProdOrderCancelHeader.CreatedDate = DateTime.Now;
                            ProdOrderCancelHeader.DivisionId  = header.DivisionId;
                            ProdOrderCancelHeader.DocDate     = header.DocDate;
                            ProdOrderCancelHeader.DocNo       = header.DocNo;
                            ProdOrderCancelHeader.DocTypeId   = Setting.DocTypeProductionOrderId.Value;
                            ProdOrderCancelHeader.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                            ProdOrderCancelHeader.ModifiedBy   = User.Identity.Name;
                            ProdOrderCancelHeader.ModifiedDate = DateTime.Now;
                            ProdOrderCancelHeader.Remark       = header.Remark;
                            ProdOrderCancelHeader.SiteId       = header.SiteId;
                            ProdOrderCancelHeader.Status       = (int)StatusConstants.System;
                            ProdOrderCancelHeader.ObjectState  = Model.ObjectState.Added;
                            db.ProdOrderCancelHeader.Add(ProdOrderCancelHeader);
                        }


                        var ProdOrderLine          = new ProdOrderLineService(_unitOfWork).GetProdOrderLineForMaterialPlan(item.MaterialPlanLineId);
                        int ProdOrderCancelLineKey = 0;
                        ProdOrderCancelLine ProdOrderCancelLine = new ProdOrderCancelLine();
                        ProdOrderCancelLine.CreatedBy               = User.Identity.Name;
                        ProdOrderCancelLine.CreatedDate             = DateTime.Now;
                        ProdOrderCancelLine.ProdOrderLineId         = ProdOrderLine.FirstOrDefault().ProdOrderLineId;
                        ProdOrderCancelLine.ModifiedBy              = User.Identity.Name;
                        ProdOrderCancelLine.ModifiedDate            = DateTime.Now;
                        ProdOrderCancelLine.ProdOrderCancelHeaderId = ExistingProdOrderCancel.ProdOrderCancelHeaderId;
                        ProdOrderCancelLine.Qty = item.Qty;
                        ProdOrderCancelLine.ProdOrderCancelLineId = ProdOrderCancelLineKey--;
                        ProdOrderCancelLine.ObjectState           = Model.ObjectState.Added;
                        db.ProdOrderCancelLine.Add(ProdOrderCancelLine);
                    }


                    if (MaterialPlanLine.PurchPlanQty > 0)
                    {
                        PurchaseIndentCancelHeader ExistingPurchaseIndentCancel = new PurchaseIndentCancelHeaderService(_unitOfWork).GetPurchaseIndentCancelForMaterialPlan(header.MaterialPlanCancelHeaderId);

                        if (ExistingPurchaseIndentCancel == null)
                        {
                            PurchaseIndentCancelHeader PurchaseIndentCancelHeader = new PurchaseIndentCancelHeader();

                            PurchaseIndentCancelHeader.CreatedBy   = User.Identity.Name;
                            PurchaseIndentCancelHeader.CreatedDate = DateTime.Now;
                            PurchaseIndentCancelHeader.DivisionId  = header.DivisionId;
                            PurchaseIndentCancelHeader.DocDate     = header.DocDate;
                            PurchaseIndentCancelHeader.DocNo       = header.DocNo;
                            PurchaseIndentCancelHeader.DocTypeId   = Setting.DocTypeProductionOrderId.Value;
                            PurchaseIndentCancelHeader.MaterialPlanCancelHeaderId = header.MaterialPlanCancelHeaderId;
                            PurchaseIndentCancelHeader.ModifiedBy   = User.Identity.Name;
                            PurchaseIndentCancelHeader.ModifiedDate = DateTime.Now;
                            PurchaseIndentCancelHeader.Remark       = header.Remark;
                            PurchaseIndentCancelHeader.SiteId       = header.SiteId;
                            PurchaseIndentCancelHeader.Status       = (int)StatusConstants.System;
                            PurchaseIndentCancelHeader.ObjectState  = Model.ObjectState.Added;
                            db.PurchaseIndentCancelHeader.Add(PurchaseIndentCancelHeader);
                        }


                        var PurchaseIndentLine          = new PurchaseIndentLineService(_unitOfWork).GetPurchaseIndentLineForMaterialPlan(item.MaterialPlanLineId);
                        int PurchaseIndentCancelLineKey = 0;
                        PurchaseIndentCancelLine PurchaseIndentCancelLine = new PurchaseIndentCancelLine();
                        PurchaseIndentCancelLine.CreatedBy                    = User.Identity.Name;
                        PurchaseIndentCancelLine.CreatedDate                  = DateTime.Now;
                        PurchaseIndentCancelLine.PurchaseIndentLineId         = PurchaseIndentLine.FirstOrDefault().PurchaseIndentLineId;
                        PurchaseIndentCancelLine.ModifiedBy                   = User.Identity.Name;
                        PurchaseIndentCancelLine.ModifiedDate                 = DateTime.Now;
                        PurchaseIndentCancelLine.PurchaseIndentCancelHeaderId = ExistingPurchaseIndentCancel.PurchaseIndentCancelHeaderId;
                        PurchaseIndentCancelLine.Qty = item.Qty;
                        PurchaseIndentCancelLine.PurchaseIndentCancelLineId = PurchaseIndentCancelLineKey--;
                        PurchaseIndentCancelLine.ObjectState = Model.ObjectState.Added;
                        db.PurchaseIndentCancelLine.Add(PurchaseIndentCancelLine);
                    }
                }

                try
                {
                    db.SaveChanges();
                }

                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    ModelState.AddModelError("", message);
                    return(PartialView("_Results", vm));
                }

                return(Json(new { success = true }));
            }
            return(PartialView("_Results", vm));
        }