private void PrepareViewBag(ExcessMaterialLineViewModel vm)
        {
            ViewBag.DeliveryUnitList = new UnitService(_unitOfWork).GetUnitList().ToList();
            ExcessMaterialHeaderViewModel H = new ExcessMaterialHeaderService(_unitOfWork, db).GetExcessMaterialHeader(vm.ExcessMaterialHeaderId);

            ViewBag.DocNo = H.DocTypeName + "-" + H.DocNo;
        }
        private ActionResult _Delete(int id)
        {
            ExcessMaterialLineViewModel temp = _ExcessMaterialLineService.GetExcessMaterialLine(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";
            }

            ExcessMaterialHeader H = new ExcessMaterialHeaderService(_unitOfWork, db).Find(temp.ExcessMaterialHeaderId);

            //Getting Settings
            var settings = new ExcessMaterialSettingsService(_unitOfWork, db).GetExcessMaterialSettingsForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            temp.ExcessMaterialSettings = Mapper.Map <ExcessMaterialSettings, ExcessMaterialSettingsViewModel>(settings);


            PrepareViewBag(temp);
            return(PartialView("_Create", temp));
        }
        public ActionResult _Create(int Id) //Id ==>Sale Order Header Id
        {
            ExcessMaterialHeader        H = new ExcessMaterialHeaderService(_unitOfWork, db).Find(Id);
            ExcessMaterialLineViewModel s = new ExcessMaterialLineViewModel();

            //Getting Settings
            var settings = new ExcessMaterialSettingsService(_unitOfWork, db).GetExcessMaterialSettingsForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            s.ExcessMaterialSettings = Mapper.Map <ExcessMaterialSettings, ExcessMaterialSettingsViewModel>(settings);
            s.ExcessMaterialHeaderId = H.ExcessMaterialHeaderId;
            ViewBag.Status           = H.Status;
            PrepareViewBag(s);
            if (!string.IsNullOrEmpty((string)TempData["CSEXCL"]))
            {
                ViewBag.CSEXCL     = TempData["CSEXCL"];
                TempData["CSEXCL"] = null;
            }
            ViewBag.LineMode = "Create";
            return(PartialView("_Create", s));
        }
        public ActionResult Reviewed(int Id, string IndexType, string UserRemark, string IsContinue)
        {
            bool BeforeSave = true;

            try
            {
                BeforeSave = ExcessMaterialDocEvents.beforeHeaderReviewEvent(this, new StockEventArgs(Id), ref context);
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
            }

            if (!BeforeSave)
            {
                TempData["CSEXC"] += "Falied validation before Review.";
            }

            ExcessMaterialHeader pd = new ExcessMaterialHeaderService(_unitOfWork, context).Find(Id);

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

                pd.ObjectState = Model.ObjectState.Modified;
                context.ExcessMaterialHeader.Add(pd);

                try
                {
                    ExcessMaterialDocEvents.onHeaderReviewEvent(this, new StockEventArgs(Id), ref context);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXC"] += message;
                }

                context.SaveChanges();

                try
                {
                    ExcessMaterialDocEvents.afterHeaderReviewEvent(this, new StockEventArgs(Id), ref context);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXC"] += message;
                }

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

                //SendEmail_POReviewd(Id);
                return(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }));
            }

            return(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }).Warning("Error in Reviewing."));
        }
        public ActionResult Submitted(int Id, string IndexType, string UserRemark, string IsContinue, string GenGatePass)
        {
            bool BeforeSave = true;

            try
            {
                BeforeSave = ExcessMaterialDocEvents.beforeHeaderSubmitEvent(this, new StockEventArgs(Id), ref context);
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                EventException     = true;
            }

            if (!BeforeSave)
            {
                TempData["CSEXC"] += "Falied validation before submit.";
            }

            ExcessMaterialHeader pd = new ExcessMaterialHeaderService(_unitOfWork, context).Find(Id);

            if (ModelState.IsValid && BeforeSave && !EventException)
            {
                if (User.Identity.Name == pd.ModifiedBy || UserRoles.Contains("Admin"))
                {
                    int ActivityType;

                    pd.Status    = (int)StatusConstants.Submitted;
                    ActivityType = (int)ActivityTypeContants.Submitted;

                    ExcessMaterialSettings Settings = new ExcessMaterialSettingsService(_unitOfWork, context).GetExcessMaterialSettingsForDocument(pd.DocTypeId, pd.DivisionId, pd.SiteId);

                    pd.ReviewBy    = null;
                    pd.ObjectState = Model.ObjectState.Modified;

                    context.ExcessMaterialHeader.Add(pd);

                    try
                    {
                        ExcessMaterialDocEvents.onHeaderSubmitEvent(this, new StockEventArgs(Id), ref context);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        EventException     = true;
                    }

                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }

                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        return(RedirectToAction("Index", new { id = pd.DocTypeId }));
                    }


                    try
                    {
                        ExcessMaterialDocEvents.afterHeaderSubmitEvent(this, new StockEventArgs(Id), ref context);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = pd.DocTypeId,
                        DocId        = pd.ExcessMaterialHeaderId,
                        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(RedirectToAction("Index", new { id = pd.DocTypeId, IndexType = IndexType }));
        }
        public ActionResult DeletePost(ExcessMaterialLineViewModel vm)
        {
            bool BeforeSave = true;

            try
            {
                BeforeSave = ExcessMaterialDocEvents.beforeLineDeleteEvent(this, new StockEventArgs(vm.ExcessMaterialHeaderId, vm.ExcessMaterialLineId), ref db);
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                EventException     = true;
            }

            if (!BeforeSave)
            {
                TempData["CSEXC"] += "Validation failed before delete.";
            }

            if (BeforeSave && !EventException)
            {
                int?ProdUid = 0;
                List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                //ExcessMaterialLine ExcessMaterialLine = _ExcessMaterialLineService.Find(vm.ExcessMaterialLineId);
                ExcessMaterialLine ExcessMaterialLine = (from p in db.ExcessMaterialLine
                                                         where p.ExcessMaterialLineId == vm.ExcessMaterialLineId
                                                         select p).FirstOrDefault();
                ExcessMaterialHeader header = new ExcessMaterialHeaderService(_unitOfWork, db).Find(ExcessMaterialLine.ExcessMaterialHeaderId);

                LogList.Add(new LogTypeViewModel
                {
                    ExObj = Mapper.Map <ExcessMaterialLine>(ExcessMaterialLine),
                });

                ProdUid = ExcessMaterialLine.ProductUidId;

                ExcessMaterialLine.ObjectState = Model.ObjectState.Deleted;
                db.ExcessMaterialLine.Remove(ExcessMaterialLine);

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


                //if (ProdUid != null && ProdUid > 0)
                //{
                //    ProductUidDetail ProductUidDetail = new ProductUidService(_unitOfWork).FGetProductUidLastValues((int)ProdUid, "ExcessMaterial Head-" + vm.ExcessMaterialHeaderId.ToString());

                //    ProductUid ProductUid = new ProductUidService(_unitOfWork).Find((int)ProdUid);

                //    ProductUid.LastTransactionDocDate = ProductUidDetail.LastTransactionDocDate;
                //    ProductUid.LastTransactionDocId = ProductUidDetail.LastTransactionDocId;
                //    ProductUid.LastTransactionDocNo = ProductUidDetail.LastTransactionDocNo;
                //    ProductUid.LastTransactionDocTypeId = ProductUidDetail.LastTransactionDocTypeId;
                //    ProductUid.LastTransactionPersonId = ProductUidDetail.LastTransactionPersonId;
                //    ProductUid.CurrenctGodownId = ProductUidDetail.CurrenctGodownId;
                //    ProductUid.CurrenctProcessId = ProductUidDetail.CurrenctProcessId;
                //    ProductUid.ObjectState = Model.ObjectState.Modified;
                //    db.ProductUid.Add(ProductUid);
                //}


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

                try
                {
                    ExcessMaterialDocEvents.onLineDeleteEvent(this, new StockEventArgs(ExcessMaterialLine.ExcessMaterialHeaderId, ExcessMaterialLine.ExcessMaterialLineId), ref db);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXCL"] += message;
                    EventException      = true;
                }

                try
                {
                    if (EventException)
                    {
                        throw new Exception();
                    }
                    db.SaveChanges();
                }

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

                try
                {
                    ExcessMaterialDocEvents.afterLineDeleteEvent(this, new StockEventArgs(ExcessMaterialLine.ExcessMaterialHeaderId, ExcessMaterialLine.ExcessMaterialLineId), ref db);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXC"] += message;
                }

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

            return(Json(new { success = true }));
        }
        public ActionResult _CreatePost(ExcessMaterialLineViewModel svm)
        {
            ExcessMaterialHeader temp = new ExcessMaterialHeaderService(_unitOfWork, db).Find(svm.ExcessMaterialHeaderId);
            ExcessMaterialLine   s    = Mapper.Map <ExcessMaterialLineViewModel, ExcessMaterialLine>(svm);

            bool BeforeSave = true;

            try
            {
                if (svm.ExcessMaterialLineId <= 0)
                {
                    BeforeSave = ExcessMaterialDocEvents.beforeLineSaveEvent(this, new StockEventArgs(svm.ExcessMaterialHeaderId, EventModeConstants.Add), ref db);
                }
                else
                {
                    BeforeSave = ExcessMaterialDocEvents.beforeLineSaveEvent(this, new StockEventArgs(svm.ExcessMaterialHeaderId, EventModeConstants.Edit), ref db);
                }
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXCL"] += message;
                EventException      = true;
            }

            if (!BeforeSave)
            {
                ModelState.AddModelError("", "Validation failed before save.");
            }

            if (svm.ProductId <= 0)
            {
                ModelState.AddModelError("ProductId", "The Product field is required");
            }

            if (svm.ExcessMaterialLineId <= 0)
            {
                ViewBag.LineMode = "Create";
            }
            else
            {
                ViewBag.LineMode = "Edit";
            }


            if (ModelState.IsValid && BeforeSave && !EventException)
            {
                if (svm.ExcessMaterialLineId <= 0)
                {
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    s.ProductUidId = svm.ProductUidId;
                    s.Sr           = _ExcessMaterialLineService.GetMaxSr(s.ExcessMaterialHeaderId);
                    s.ObjectState  = Model.ObjectState.Added;
                    db.ExcessMaterialLine.Add(s);

                    if (temp.Status != (int)StatusConstants.Drafted && temp.Status != (int)StatusConstants.Import)
                    {
                        temp.Status       = (int)StatusConstants.Modified;
                        temp.ModifiedBy   = User.Identity.Name;
                        temp.ModifiedDate = DateTime.Now;
                        db.ExcessMaterialHeader.Add(temp);
                    }

                    try
                    {
                        ExcessMaterialDocEvents.onLineSaveEvent(this, new StockEventArgs(s.ExcessMaterialHeaderId, s.ExcessMaterialLineId, EventModeConstants.Add), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        EventException      = true;
                    }

                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }

                        db.SaveChanges();
                    }

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

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

                    try
                    {
                        ExcessMaterialDocEvents.afterLineSaveEvent(this, new StockEventArgs(s.ExcessMaterialHeaderId, s.ExcessMaterialLineId, EventModeConstants.Add), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                    }

                    return(RedirectToAction("_Create", new { id = svm.ExcessMaterialHeaderId }));
                }


                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();
                    int status = temp.Status;
                    ExcessMaterialLine templine = _ExcessMaterialLineService.Find(s.ExcessMaterialLineId);

                    ExcessMaterialLine ExRec = new ExcessMaterialLine();
                    ExRec = Mapper.Map <ExcessMaterialLine>(templine);

                    templine.ProductId    = s.ProductId;
                    templine.ProductUidId = s.ProductUidId;
                    templine.Dimension1Id = s.Dimension1Id;
                    templine.Dimension2Id = s.Dimension2Id;
                    templine.Dimension3Id = s.Dimension3Id;
                    templine.Dimension4Id = s.Dimension4Id;
                    templine.LotNo        = s.LotNo;
                    templine.ProcessId    = s.ProcessId;
                    templine.ProductUidId = s.ProductUidId;
                    templine.Remark       = s.Remark;
                    templine.Qty          = s.Qty;
                    templine.Remark       = s.Remark;

                    templine.ModifiedDate = DateTime.Now;
                    templine.ModifiedBy   = User.Identity.Name;
                    templine.ObjectState  = Model.ObjectState.Modified;
                    db.ExcessMaterialLine.Add(templine);

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


                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = templine,
                    });


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

                    try
                    {
                        ExcessMaterialDocEvents.onLineSaveEvent(this, new StockEventArgs(s.ExcessMaterialHeaderId, templine.ExcessMaterialLineId, EventModeConstants.Edit), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        EventException      = true;
                    }

                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }
                        db.SaveChanges();
                    }

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

                    try
                    {
                        ExcessMaterialDocEvents.afterLineSaveEvent(this, new StockEventArgs(s.ExcessMaterialHeaderId, templine.ExcessMaterialLineId, EventModeConstants.Edit), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }

                    //Saving the Activity Log

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

                    //End of Saving the Activity Log

                    return(Json(new { success = true }));
                }
            }
            PrepareViewBag(svm);
            return(PartialView("_Create", svm));
        }