Example #1
0
        public ActionResult DeletePost(PurchaseIndentCancelLineViewModel vm)
        {
            bool BeforeSave = true;

            try
            {
                BeforeSave = PurchaseIndentCancelDocEvents.beforeLineDeleteEvent(this, new PurchaseEventArgs(vm.PurchaseIndentCancelHeaderId, vm.PurchaseIndentCancelLineId), 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)
            {
                List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                PurchaseIndentCancelLine IndentCancelLine = db.PurchaseIndentCancelLine.Find(vm.PurchaseIndentCancelLineId);

                try
                {
                    PurchaseIndentCancelDocEvents.onLineDeleteEvent(this, new PurchaseEventArgs(IndentCancelLine.PurchaseIndentCancelHeaderId, IndentCancelLine.PurchaseIndentCancelLineId), ref db);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXCL"] += message;
                    EventException      = true;
                }

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

                IndentCancelLine.ObjectState = Model.ObjectState.Deleted;
                db.PurchaseIndentCancelLine.Remove(IndentCancelLine);

                //_PurchaseIndentCancelLineService.Delete(vm.PurchaseIndentCancelLineId);
                PurchaseIndentCancelHeader header = new PurchaseIndentCancelHeaderService(_unitOfWork).Find(IndentCancelLine.PurchaseIndentCancelHeaderId);
                if (header.Status != (int)StatusConstants.Drafted)
                {
                    header.Status = (int)StatusConstants.Modified;
                    //new PurchaseIndentCancelHeaderService(_unitOfWork).Update(header);
                    header.ModifiedBy   = User.Identity.Name;
                    header.ModifiedDate = DateTime.Now;
                    header.ObjectState  = Model.ObjectState.Modified;
                    db.PurchaseIndentCancelHeader.Add(header);
                }

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

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

                    db.SaveChanges();
                    //_unitOfWork.Save();
                }

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

                try
                {
                    PurchaseIndentCancelDocEvents.afterLineDeleteEvent(this, new PurchaseEventArgs(IndentCancelLine.PurchaseIndentCancelHeaderId, IndentCancelLine.PurchaseIndentCancelLineId), ref db);
                }
                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    TempData["CSEXC"] += message;
                }


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

            return(Json(new { success = true }));
        }