Example #1
0
        public ActionResult _CreatePost(PurchaseIndentCancelLineViewModel svm)
        {
            if (svm.PurchaseIndentCancelLineId <= 0)
            {
                ViewBag.LineMode = "Create";
            }
            else
            {
                ViewBag.LineMode = "Edit";
            }

            bool BeforeSave = true;

            try
            {
                if (svm.PurchaseIndentLineId <= 0)
                {
                    BeforeSave = PurchaseIndentCancelDocEvents.beforeLineSaveEvent(this, new PurchaseEventArgs(svm.PurchaseIndentCancelHeaderId, EventModeConstants.Add), ref db);
                }
                else
                {
                    BeforeSave = PurchaseIndentCancelDocEvents.beforeLineSaveEvent(this, new PurchaseEventArgs(svm.PurchaseIndentCancelHeaderId, 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.PurchaseIndentCancelLineId <= 0)
            {
                PurchaseIndentCancelLine s = new PurchaseIndentCancelLine();
                decimal balqty             = (from p in db.ViewPurchaseIndentBalance
                                              where p.PurchaseIndentLineId == svm.PurchaseIndentLineId
                                              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.PurchaseIndentLineId <= 0)
                {
                    ModelState.AddModelError("PurchaseIndentLineId", "The Purchase Indent field is required");
                }
                if (ModelState.IsValid && BeforeSave && !EventException)
                {
                    s.PurchaseIndentCancelHeaderId = svm.PurchaseIndentCancelHeaderId;
                    s.PurchaseIndentLineId         = svm.PurchaseIndentLineId;
                    s.Qty          = svm.Qty;
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.Sr           = _PurchaseIndentCancelLineService.GetMaxSr(s.PurchaseIndentCancelHeaderId);
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    //_PurchaseIndentCancelLineService.Create(s);
                    s.ObjectState = Model.ObjectState.Added;
                    db.PurchaseIndentCancelLine.Add(s);

                    PurchaseIndentCancelHeader temp2 = new PurchaseIndentCancelHeaderService(_unitOfWork).Find(s.PurchaseIndentCancelHeaderId);
                    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;
                        temp2.ObjectState  = Model.ObjectState.Modified;
                        db.PurchaseIndentCancelHeader.Add(temp2);
                        //new PurchaseIndentCancelHeaderService(_unitOfWork).Update(temp2);
                    }

                    try
                    {
                        PurchaseIndentCancelDocEvents.onLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseIndentCancelHeaderId, s.PurchaseIndentCancelLineId, 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();
                        //_unitOfWork.Save();
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        return(PartialView("_Create", svm));
                    }

                    try
                    {
                        PurchaseIndentCancelDocEvents.afterLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseIndentCancelHeaderId, s.PurchaseIndentCancelLineId, EventModeConstants.Add), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                    }


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


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

                PurchaseIndentCancelHeader temp = new PurchaseIndentCancelHeaderService(_unitOfWork).Find(svm.PurchaseIndentCancelHeaderId);


                int           status    = temp.Status;
                StringBuilder logstring = new StringBuilder();

                PurchaseIndentCancelLine s = _PurchaseIndentCancelLineService.Find(svm.PurchaseIndentCancelLineId);

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

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


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

                    s.ObjectState = Model.ObjectState.Modified;
                    db.PurchaseIndentCancelLine.Add(s);

                    //_PurchaseIndentCancelLineService.Update(s);

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

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

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

                    try
                    {
                        PurchaseIndentCancelDocEvents.onLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseIndentCancelHeaderId, s.PurchaseIndentCancelLineId, 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();
                        //_unitOfWork.Save();
                    }

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

                    try
                    {
                        PurchaseIndentCancelDocEvents.afterLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseIndentCancelHeaderId, s.PurchaseIndentCancelLineId, 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           = s.PurchaseIndentCancelHeaderId,
                        DocLineId       = s.PurchaseIndentCancelLineId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.DocNo,
                        xEModifications = Modifications,
                        DocDate         = temp.DocDate,
                        DocStatus       = temp.Status,
                    }));

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