public ActionResult _FilterPostIndents(PurchaseQuotationLineFilterViewModel vm)
        {
            if (vm.PurchaseQuotationSettings.isMandatoryRate && (vm.Rate == null || vm.Rate == 0))
            {
                ModelState.AddModelError("", "Rate is mandatory");
                PrepareViewBag(null);
                return(PartialView("_IndentFilters", vm));
            }


            List <PurchaseQuotationLineViewModel> temp = _PurchaseQuotationLineService.GetPurchaseIndentForFilters(vm).ToList();
            PurchaseQuotationMasterDetailModel    svm  = new PurchaseQuotationMasterDetailModel();

            svm.PurchaseQuotationLineViewModel = temp;

            bool UnitConvetsionException = (from p in temp
                                            where p.UnitConversionException == true
                                            select p).Any();


            if (UnitConvetsionException)
            {
                ViewBag.UnitConversionException = UnitConvetsionException;

                string Products = string.Join(",", from p in temp
                                              where p.UnitConversionException == true
                                              select p.ProductName);


                ModelState.AddModelError("", "Unit Conversion are missing for few Products -" + Products);
            }

            PurchaseQuotationHeader Header = _PurchaseQuotationHeaderService.Find(vm.PurchaseQuotationHeaderId);

            PurchaseQuotationSetting Settings = new PurchaseQuotationSettingService(_unitOfWork).GetPurchaseQuotationSettingForDocument(Header.DocTypeId, Header.DivisionId, Header.SiteId);

            svm.PurchaseQuotationSettings = Mapper.Map <PurchaseQuotationSetting, PurchaseQuotationSettingsViewModel>(Settings);


            return(PartialView("_IndentResults", svm));
        }
        public ActionResult _ResultsPost(PurchaseQuotationMasterDetailModel vm)
        {
            List <HeaderChargeViewModel> HeaderCharges = new List <HeaderChargeViewModel>();
            List <LineChargeViewModel>   LineCharges   = new List <LineChargeViewModel>();
            int  pk               = 0;
            int  ReceiptLinepk    = 0;
            int  StockPk          = 0;
            int  Cnt              = 0;
            int  Serial           = _PurchaseQuotationLineService.GetMaxSr(vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId);
            bool HeaderChargeEdit = false;

            PurchaseQuotationHeader Header = _PurchaseQuotationHeaderService.Find(vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId);

            PurchaseQuotationSetting Settings = new PurchaseQuotationSettingService(_unitOfWork).GetPurchaseQuotationSettingForDocument(Header.DocTypeId, Header.DivisionId, Header.SiteId);

            int?MaxLineId = new PurchaseQuotationLineChargeService(_unitOfWork).GetMaxProductCharge(Header.PurchaseQuotationHeaderId, "Web.PurchaseQuotationLines", "PurchaseQuotationHeaderId", "PurchaseQuotationLineId");

            int PersonCount   = 0;
            int CalculationId = Settings.CalculationId;

            List <LineDetailListViewModel> LineList = new List <LineDetailListViewModel>();

            bool BeforeSave = true;

            //try
            //{
            //    BeforeSave = PurchaseQuotationReceiveDocEvents.beforeLineSaveBulkEvent(this, new PurchaseEventArgs(vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId), ref db);
            //}
            //catch (Exception ex)
            //{
            //    string message = _exception.HandleException(ex);
            //    TempData["CSEXCL"] += message;
            //    EventException = true;
            //}

            if (!BeforeSave)
            {
                ModelState.AddModelError("", "Validation failed before save");
            }

            if (ModelState.IsValid && BeforeSave && !EventException)
            {
                int ProcessId = new ProcessService(_unitOfWork).Find(ProcessConstants.FullFinishing).ProcessId;

                var IndentLineIds = vm.PurchaseQuotationLineViewModel.Select(m => m.PurchaseIndentLineId).ToArray();

                var IndentBalRecords = (from p in db.ViewPurchaseIndentBalance
                                        where IndentLineIds.Contains(p.PurchaseIndentLineId)
                                        select new
                {
                    LineId = p.PurchaseIndentLineId,
                    BalQty = p.BalanceQty,
                }).ToList();

                foreach (var item in vm.PurchaseQuotationLineViewModel)
                {
                    decimal balqty = IndentBalRecords.Where(m => m.LineId == item.PurchaseIndentLineId).FirstOrDefault().BalQty;

                    if (item.Qty > 0 && (Settings.isMandatoryRate == true ? item.Rate > 0 : 1 == 1) && item.Qty <= balqty)
                    {
                        PurchaseGoodsReceiptLine ReceiptLine = new PurchaseGoodsReceiptLine();
                        var PurchaseIndentLine = new PurchaseIndentLineService(_unitOfWork).Find(item.PurchaseIndentLineId.Value);


                        PurchaseQuotationLine line = new PurchaseQuotationLine();
                        line.PurchaseQuotationHeaderId = item.PurchaseQuotationHeaderId;
                        line.UnitConversionMultiplier  = item.UnitConversionMultiplier;
                        line.Rate = item.Rate;
                        line.PurchaseIndentLineId = item.PurchaseIndentLineId;
                        line.DiscountPer          = item.DiscountPer;
                        line.Qty                     = item.Qty;
                        line.DealUnitId              = item.DealUnitId;
                        line.DealQty                 = item.Qty * item.UnitConversionMultiplier;
                        line.Amount                  = (line.DealQty * item.Rate);
                        line.CreatedDate             = DateTime.Now;
                        line.ProductId               = PurchaseIndentLine.ProductId;
                        line.Dimension1Id            = PurchaseIndentLine.Dimension1Id;
                        line.Dimension2Id            = PurchaseIndentLine.Dimension2Id;
                        line.Specification           = PurchaseIndentLine.Specification;
                        line.Sr                      = Serial++;
                        line.ModifiedDate            = DateTime.Now;
                        line.CreatedBy               = User.Identity.Name;
                        line.ModifiedBy              = User.Identity.Name;
                        line.PurchaseQuotationLineId = pk;
                        line.ObjectState             = Model.ObjectState.Added;
                        //_PurchaseQuotationLineService.Create(line);
                        db.PurchaseQuotationLine.Add(line);


                        LineList.Add(new LineDetailListViewModel {
                            Amount = line.Amount, Rate = line.Rate, LineTableId = line.PurchaseQuotationLineId, HeaderTableId = item.PurchaseQuotationHeaderId, PersonID = Header.SupplierId
                        });

                        pk++;
                        StockPk++;
                    }
                }

                if (Header.Status != (int)StatusConstants.Drafted)
                {
                    Header.Status       = (int)StatusConstants.Modified;
                    Header.ModifiedBy   = User.Identity.Name;
                    Header.ModifiedDate = DateTime.Now;
                }

                Header.ObjectState = Model.ObjectState.Modified;
                db.PurchaseQuotationHeader.Add(Header);

                //new PurchaseGoodsReceiptHeaderService(_unitOfWork).Update(ReceiptHeader);

                new ChargesCalculationService(_unitOfWork).CalculateCharges(LineList, vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId, CalculationId, MaxLineId, out LineCharges, out HeaderChargeEdit, out HeaderCharges, "Web.PurchaseQuotationHeaderCharges", "Web.PurchaseQuotationLineCharges", out PersonCount, Header.DocTypeId, Header.SiteId, Header.DivisionId);

                //Saving Charges
                foreach (var item in LineCharges)
                {
                    PurchaseQuotationLineCharge PoLineCharge = Mapper.Map <LineChargeViewModel, PurchaseQuotationLineCharge>(item);
                    PoLineCharge.ObjectState = Model.ObjectState.Added;
                    db.PurchaseQuotationLineCharge.Add(PoLineCharge);
                    //new PurchaseQuotationLineChargeService(_unitOfWork).Create(PoLineCharge);
                }


                //Saving Header charges
                for (int i = 0; i < HeaderCharges.Count(); i++)
                {
                    if (!HeaderChargeEdit)
                    {
                        PurchaseQuotationHeaderCharge POHeaderCharge = Mapper.Map <HeaderChargeViewModel, PurchaseQuotationHeaderCharge>(HeaderCharges[i]);
                        POHeaderCharge.HeaderTableId = vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId;
                        POHeaderCharge.PersonID      = Header.SupplierId;
                        POHeaderCharge.ObjectState   = Model.ObjectState.Added;
                        db.PurchaseQuotationHeaderCharge.Add(POHeaderCharge);
                        //new PurchaseQuotationHeaderChargeService(_unitOfWork).Create(POHeaderCharge);
                    }
                    else
                    {
                        var footercharge = new PurchaseQuotationHeaderChargeService(_unitOfWork).Find(HeaderCharges[i].Id);
                        footercharge.Rate        = HeaderCharges[i].Rate;
                        footercharge.Amount      = HeaderCharges[i].Amount;
                        footercharge.ObjectState = Model.ObjectState.Modified;
                        db.PurchaseQuotationHeaderCharge.Add(footercharge);
                        //new PurchaseQuotationHeaderChargeService(_unitOfWork).Update(footercharge);
                    }
                }

                //try
                //{
                //    PurchaseQuotationReceiveDocEvents.onLineSaveBulkEvent(this, new PurchaseEventArgs(vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId), 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("_IndentResults", vm));
                }

                //try
                //{
                //    PurchaseQuotationReceiveDocEvents.afterLineSaveBulkEvent(this, new PurchaseEventArgs(vm.PurchaseQuotationLineViewModel.FirstOrDefault().PurchaseQuotationHeaderId), ref db);
                //}
                //catch (Exception ex)
                //{
                //    string message = _exception.HandleException(ex);
                //    TempData["CSEXC"] += message;
                //}

                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId    = Header.DocTypeId,
                    DocId        = Header.PurchaseQuotationHeaderId,
                    ActivityType = (int)ActivityTypeContants.MultipleCreate,
                    DocNo        = Header.DocNo,
                    DocDate      = Header.DocDate,
                    DocStatus    = Header.Status,
                }));

                return(Json(new { success = true }));
            }
            return(PartialView("_IndentResults", vm));
        }