Пример #1
0
        public ActionResult Post(PurchaseOrderAmendmentHeaderViewModel vm)
        {
            bool BeforeSave = true;

            PurchaseOrderAmendmentHeader pt = AutoMapper.Mapper.Map <PurchaseOrderAmendmentHeaderViewModel, PurchaseOrderAmendmentHeader>(vm);

            #region BeforeSave
            try
            {
                if (vm.PurchaseOrderAmendmentHeaderId <= 0)
                {
                    BeforeSave = PurchaseOrderAmendmentDocEvents.beforeHeaderSaveEvent(this, new PurchaseEventArgs(vm.PurchaseOrderAmendmentHeaderId, EventModeConstants.Add), ref db);
                }
                else
                {
                    BeforeSave = PurchaseOrderAmendmentDocEvents.beforeHeaderSaveEvent(this, new PurchaseEventArgs(vm.PurchaseOrderAmendmentHeaderId, EventModeConstants.Edit), ref db);
                }
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                EventException     = true;
            }


            if (!BeforeSave)
            {
                TempData["CSEXC"] += "Failed validation before save";
            }
            #endregion

            #region DocTypeTimeLineValidation

            try
            {
                if (vm.PurchaseOrderAmendmentHeaderId <= 0)
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(vm), DocumentTimePlanTypeConstants.Create, User.Identity.Name, out ExceptionMsg, out Continue);
                }
                else
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(vm), 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 && BeforeSave && !EventException && (TimePlanValidation || Continue))
            {
                #region CreateRecord
                if (vm.PurchaseOrderAmendmentHeaderId <= 0)
                {
                    pt.Status = (int)StatusConstants.Drafted;

                    _PurchaseOrderAmendmentHeaderService.Create(pt, User.Identity.Name);

                    try
                    {
                        PurchaseOrderAmendmentDocEvents.onHeaderSaveEvent(this, new PurchaseEventArgs(pt.PurchaseOrderAmendmentHeaderId, EventModeConstants.Add), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        EventException     = true;
                    }

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

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

                    try
                    {
                        PurchaseOrderAmendmentDocEvents.afterHeaderSaveEvent(this, new PurchaseEventArgs(pt.PurchaseOrderAmendmentHeaderId, EventModeConstants.Add), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }

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

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

                #region EditRecord
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    PurchaseOrderAmendmentHeader temp = _PurchaseOrderAmendmentHeaderService.Find(pt.PurchaseOrderAmendmentHeaderId);


                    PurchaseOrderAmendmentHeader ExRec = new PurchaseOrderAmendmentHeader();
                    ExRec = Mapper.Map <PurchaseOrderAmendmentHeader>(temp);


                    int status = temp.Status;

                    if (temp.Status != (int)StatusConstants.Drafted)
                    {
                        temp.Status = (int)StatusConstants.Modified;
                    }

                    temp.DocDate    = pt.DocDate;
                    temp.DocNo      = pt.DocNo;
                    temp.DocTypeId  = pt.DocTypeId;
                    temp.Remark     = pt.Remark;
                    temp.SupplierId = pt.SupplierId;

                    _PurchaseOrderAmendmentHeaderService.Update(temp, User.Identity.Name);

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



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

                    try
                    {
                        PurchaseOrderAmendmentDocEvents.onHeaderSaveEvent(this, new PurchaseEventArgs(temp.PurchaseOrderAmendmentHeaderId, EventModeConstants.Edit), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        EventException     = true;
                    }

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

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

                    try
                    {
                        PurchaseOrderAmendmentDocEvents.afterHeaderSaveEvent(this, new PurchaseEventArgs(pt.PurchaseOrderAmendmentHeaderId, EventModeConstants.Edit), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }

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

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