public ActionResult Delete(ReasonViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var DispatchWaybillHeader = _DispatchWaybillHeaderService.GetDispatchWaybillHeader(vm.id);

                ActivityLog al = new ActivityLog()
                {
                    ActivityType = (int)ActivityTypeContants.Deleted,
                    CreatedBy    = User.Identity.Name,
                    CreatedDate  = DateTime.Now,
                    DocId        = DispatchWaybillHeader.DispatchWaybillHeaderId,
                    UserRemark   = vm.Reason,
                    Narration    = "Sale Order is deleted with DocNo:" + DispatchWaybillHeader.DocNo,
                    DocTypeId    = new DocumentTypeService(_unitOfWork).FindByName(TransactionDocCategoryConstants.Packing).DocumentTypeId,
                    UploadDate   = DateTime.Now,
                };
                _ActivityLogService.Create(al);

                var dispatchwaybillline = new DispatchWaybillLineService(_unitOfWork).GetDispatchWaybillLineForHeaderId(vm.id);

                foreach (var item in dispatchwaybillline)
                {
                    new DispatchWaybillLineService(_unitOfWork).Delete(item.DispatchWaybillLineId);
                }

                new DispatchWaybillHeaderService(_unitOfWork).Delete(vm.id);

                try
                {
                    _unitOfWork.Save();
                }

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

                return(Json(new { success = true }));
            }
            return(PartialView("_Reason", vm));
        }
        public ActionResult Create(DispatchWaybillHeaderViewModel svm)
        {
            string DataValidationMsg = DataValidation(svm);

            if (DataValidationMsg != "")
            {
                PrepareViewBag(svm);
                PrepareViewBag(svm.DocTypeId);
                return(View(svm).Danger(DataValidationMsg));
            }

            #region DocTypeTimeLineValidation

            try
            {
                if (svm.DispatchWaybillHeaderId <= 0)
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(svm), DocumentTimePlanTypeConstants.Create, User.Identity.Name, out ExceptionMsg, out Continue);
                }
                else
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(svm), DocumentTimePlanTypeConstants.Modify, User.Identity.Name, out ExceptionMsg, out Continue);
                }
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                TimePlanValidation = false;
            }

            if (!TimePlanValidation)
            {
                TempData["CSEXC"] += ExceptionMsg;
            }

            #endregion

            if (ModelState.IsValid && (TimePlanValidation || Continue))
            {
                #region CreateRecord
                if (svm.DispatchWaybillHeaderId == 0)
                {
                    DispatchWaybillHeader DispatchWaybillHeader = Mapper.Map <DispatchWaybillHeaderViewModel, DispatchWaybillHeader>(svm);
                    DispatchWaybillHeader.CreatedDate  = DateTime.Now;
                    DispatchWaybillHeader.ModifiedDate = DateTime.Now;
                    DispatchWaybillHeader.CreatedBy    = User.Identity.Name;
                    DispatchWaybillHeader.ModifiedBy   = User.Identity.Name;
                    DispatchWaybillHeader.Status       = (int)StatusConstants.Drafted;

                    if (svm.DocTypeId == new DocumentTypeService(_unitOfWork).Find(TransactionDoctypeConstants.PreDispatchWaybill).DocumentTypeId)
                    {
                        DispatchWaybillHeader.IsPreCarriage = true;
                    }
                    else
                    {
                        DispatchWaybillHeader.IsPreCarriage = false;
                    }


                    _DispatchWaybillHeaderService.Create(DispatchWaybillHeader);


                    var routecities = new RouteLineService(_unitOfWork).GetRouteLineListForIndex(svm.RouteId).ToList();

                    foreach (var item in routecities)
                    {
                        DispatchWaybillLine DispatchWaybillLine = new DispatchWaybillLine();
                        DispatchWaybillLine.CityId       = item.CityId;
                        DispatchWaybillLine.CreatedDate  = DateTime.Now;
                        DispatchWaybillLine.ModifiedDate = DateTime.Now;
                        DispatchWaybillLine.CreatedBy    = User.Identity.Name;
                        DispatchWaybillLine.ModifiedBy   = User.Identity.Name;
                        new DispatchWaybillLineService(_unitOfWork).Create(DispatchWaybillLine);
                    }

                    try
                    {
                        _unitOfWork.Save();
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        PrepareViewBag(svm);
                        ViewBag.Mode = "Add";
                        return(View("Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = DispatchWaybillHeader.DocTypeId,
                        DocId        = DispatchWaybillHeader.DispatchWaybillHeaderId,
                        ActivityType = (int)ActivityTypeContants.Added,
                        DocNo        = DispatchWaybillHeader.DocNo,
                        DocDate      = DispatchWaybillHeader.DocDate,
                        DocStatus    = DispatchWaybillHeader.Status,
                    }));

                    return(RedirectToAction("Modify", new { id = DispatchWaybillHeader.DispatchWaybillHeaderId }).Success("Data saved Successfully"));
                }
                #endregion

                #region EditRecord
                else
                {
                    int routeid = _DispatchWaybillHeaderService.GetDispatchWaybillHeaderViewModel(svm.DispatchWaybillHeaderId).RouteId;

                    DispatchWaybillHeader DispatchWaybillHeader = Mapper.Map <DispatchWaybillHeaderViewModel, DispatchWaybillHeader>(svm);

                    int status = DispatchWaybillHeader.Status;

                    if (DispatchWaybillHeader.Status != (int)StatusConstants.Drafted && DispatchWaybillHeader.Status != (int)StatusConstants.Import)
                    {
                        DispatchWaybillHeader.Status = (int)StatusConstants.Modified;
                    }

                    DispatchWaybillHeader.Status       = (int)StatusConstants.Modified;
                    DispatchWaybillHeader.ModifiedDate = DateTime.Now;
                    DispatchWaybillHeader.ModifiedBy   = User.Identity.Name;
                    _DispatchWaybillHeaderService.Update(DispatchWaybillHeader);



                    if (routeid != svm.RouteId)
                    {
                        var dispatchwaybillline = new DispatchWaybillLineService(_unitOfWork).GetDispatchWaybillLineForHeaderId(svm.DispatchWaybillHeaderId);

                        foreach (var item in dispatchwaybillline)
                        {
                            new DispatchWaybillLineService(_unitOfWork).Delete(item.DispatchWaybillLineId);
                        }


                        var routecities = new RouteLineService(_unitOfWork).GetRouteLineListForIndex(svm.RouteId).ToList();

                        foreach (var item in routecities)
                        {
                            DispatchWaybillLine DispatchWaybillLine = new DispatchWaybillLine();
                            DispatchWaybillLine.DispatchWaybillHeaderId = DispatchWaybillHeader.DispatchWaybillHeaderId;
                            DispatchWaybillLine.CityId       = item.CityId;
                            DispatchWaybillLine.CreatedDate  = DateTime.Now;
                            DispatchWaybillLine.ModifiedDate = DateTime.Now;
                            DispatchWaybillLine.CreatedBy    = User.Identity.Name;
                            DispatchWaybillLine.ModifiedBy   = User.Identity.Name;
                            new DispatchWaybillLineService(_unitOfWork).Create(DispatchWaybillLine);
                        }
                    }

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        ViewBag.Mode       = "Edit";
                        return(View("Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = DispatchWaybillHeader.DocTypeId,
                        DocId        = DispatchWaybillHeader.DispatchWaybillHeaderId,
                        ActivityType = (int)ActivityTypeContants.Modified,
                        DocNo        = DispatchWaybillHeader.DocNo,
                        DocDate      = DispatchWaybillHeader.DocDate,
                        DocStatus    = DispatchWaybillHeader.Status,
                    }));

                    return(RedirectToAction("Index", new { id = svm.DocTypeId, IndexType = "" }).Success("Data saved successfully"));
                }
                #endregion
            }
            PrepareViewBag(svm);
            PrepareViewBag(svm.DocTypeId);
            ViewBag.Mode = "Add";
            return(View("Create", svm));
        }