public ActionResult _Detail(int id)
        {
            SaleOrderCancelLineViewModel temp = _SaleOrderCancelLineService.GetSaleOrderCancelLine(id);

            if (temp == null)
            {
                return(HttpNotFound());
            }

            return(PartialView("_Create", temp));
        }
        public ActionResult DeletePost(SaleOrderCancelLineViewModel vm)
        {
            List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

            SaleOrderCancelLine SaleOrderLine = _SaleOrderCancelLineService.Find(vm.SaleOrderCancelLineId);

            LogList.Add(new LogTypeViewModel
            {
                ExObj = SaleOrderLine,
            });

            SaleOrderCancelHeader header = new SaleOrderCancelHeaderService(_unitOfWork).Find(SaleOrderLine.SaleOrderCancelHeaderId);

            new SaleOrderLineStatusService(_unitOfWork).UpdateSaleQtyOnCancel(SaleOrderLine.SaleOrderLineId, SaleOrderLine.SaleOrderCancelLineId, header.DocDate, 0);

            _SaleOrderCancelLineService.Delete(vm.SaleOrderCancelLineId);

            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;
                new SaleOrderCancelHeaderService(_unitOfWork).Update(header);
            }
            XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

            try
            {
                _unitOfWork.Save();
            }

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

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

            return(Json(new { success = true }));
        }
        public ActionResult _Create(int Id, int bid) //Id ==>Sale Order Header Id
        {
            SaleOrderCancelLineViewModel svm = new SaleOrderCancelLineViewModel();
            var header = new SaleOrderCancelHeaderService(_unitOfWork).Find(Id);

            svm.SaleOrderCancelHeaderId = Id;
            svm.BuyerId = bid;
            var settings = new SaleOrderSettingsService(_unitOfWork).GetSaleOrderSettings(header.DocTypeId, header.DivisionId, header.SiteId);

            svm.SaleOrderSettings    = Mapper.Map <SaleOrderSettings, SaleOrderSettingsViewModel>(settings);
            svm.DocumentTypeSettings = new DocumentTypeSettingsService(_unitOfWork).GetDocumentTypeSettingsForDocument(header.DocTypeId);

            ViewBag.LineMode = "Create";
            return(PartialView("_Create", svm));
        }
        public ActionResult _Delete(int id)
        {
            SaleOrderCancelLineViewModel temp = _SaleOrderCancelLineService.GetSaleOrderCancelLineForEdit(id);


            SaleOrderCancelHeader Header = new SaleOrderCancelHeaderService(_unitOfWork).Find(temp.SaleOrderCancelHeaderId);

            var settings = new SaleOrderSettingsService(_unitOfWork).GetSaleOrderSettings(Header.DocTypeId, Header.DivisionId, Header.SiteId);

            temp.SaleOrderSettings    = Mapper.Map <SaleOrderSettings, SaleOrderSettingsViewModel>(settings);
            temp.DocumentTypeSettings = new DocumentTypeSettingsService(_unitOfWork).GetDocumentTypeSettingsForDocument(Header.DocTypeId);

            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";
            }

            return(PartialView("_Create", temp));
        }
        public ActionResult _CreatePost(SaleOrderCancelLineViewModel svm)
        {
            SaleOrderCancelHeader temp2 = new SaleOrderCancelHeaderService(_unitOfWork).Find(svm.SaleOrderCancelHeaderId);

            if (svm.SaleOrderCancelLineId <= 0)
            {
                SaleOrderCancelLine s      = new SaleOrderCancelLine();
                decimal             balqty = (from p in db.ViewSaleOrderBalanceForCancellation
                                              where p.SaleOrderLineId == svm.SaleOrderLineId
                                              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 (ModelState.IsValid)
                {
                    s.Remark = svm.LineRemark;
                    s.SaleOrderCancelHeaderId = svm.SaleOrderCancelHeaderId;
                    s.SaleOrderLineId         = svm.SaleOrderLineId ?? 0;
                    s.Qty          = svm.Qty;
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    _SaleOrderCancelLineService.Create(s);

                    new SaleOrderLineStatusService(_unitOfWork).UpdateSaleQtyOnCancel(s.SaleOrderLineId, s.SaleOrderCancelLineId, temp2.DocDate, s.Qty);


                    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;
                    }

                    new SaleOrderCancelHeaderService(_unitOfWork).Update(temp2);

                    try
                    {
                        _unitOfWork.Save();
                    }

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

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

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

                SaleOrderCancelHeader temp = new SaleOrderCancelHeaderService(_unitOfWork).Find(svm.SaleOrderCancelHeaderId);

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

                SaleOrderCancelLine s = _SaleOrderCancelLineService.Find(svm.SaleOrderCancelLineId);

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

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


                if (ModelState.IsValid)
                {
                    if (svm.Qty > 0)
                    {
                        //Tracking the Modification::

                        if (status == (int)StatusConstants.Approved)
                        {
                            logstring.Append("Editing SaleOrderCancelLineId No:" + svm.SaleOrderCancelLineId + "::\n");
                            if ((s.Remark != svm.Remark))
                            {
                                logstring.Append(s.Remark + " Remark Modified to " + svm.Remark + "::\n");
                            }
                            if ((s.Qty != svm.Qty))
                            {
                                logstring.Append(s.Qty + " Qty Modified to " + svm.Qty + "::\n");
                            }
                        }
                        //Ennd of Checking Modifications

                        s.Remark       = svm.LineRemark;
                        s.Qty          = svm.Qty;
                        s.ModifiedBy   = User.Identity.Name;
                        s.ModifiedDate = DateTime.Now;

                        new SaleOrderLineStatusService(_unitOfWork).UpdateSaleQtyOnCancel(s.SaleOrderLineId, s.SaleOrderCancelLineId, temp.DocDate, s.Qty);
                    }

                    _SaleOrderCancelLineService.Update(s);

                    if (temp.Status != (int)StatusConstants.Drafted && temp.Status != (int)StatusConstants.Import)
                    {
                        temp.Status       = (int)StatusConstants.Modified;
                        temp.ModifiedDate = DateTime.Now;
                        temp.ModifiedBy   = User.Identity.Name;
                    }
                    new SaleOrderCancelHeaderService(_unitOfWork).Update(temp);

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

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

                    try
                    {
                        _unitOfWork.Save();
                    }

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

                    //SAving the Activity Log::

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

                    //End Of Saving Activity Log


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