public ActionResult _Create(int Id, int sid, bool IsIndentBased) //Id ==>Sale Order Header Id
        {
            PurchaseQuotationHeader        H = _PurchaseQuotationHeaderService.Find(Id);
            PurchaseQuotationLineViewModel s = new PurchaseQuotationLineViewModel();

            //Getting Settings
            var settings = new PurchaseQuotationSettingService(_unitOfWork).GetPurchaseQuotationSettingForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            s.PurchQuotationSettings = Mapper.Map <PurchaseQuotationSetting, PurchaseQuotationSettingsViewModel>(settings);

            s.PurchaseQuotationHeaderId    = H.PurchaseQuotationHeaderId;
            s.PurchaseQuotationHeaderDocNo = H.DocNo;
            s.SupplierId = sid;
            s.DocTypeId  = H.DocTypeId;
            s.DivisionId = H.DivisionId;
            s.SiteId     = H.SiteId;
            s.CalculateDiscountOnRate = H.CalculateDiscountOnRate;
            ViewBag.LineMode          = "Create";
            PrepareViewBag(null);

            if (IsIndentBased == true)
            {
                return(PartialView("_CreateForIndent", s));
            }
            else
            {
                return(PartialView("_Create", s));
            }
        }
        public ActionResult _Detail(int id)
        {
            PurchaseQuotationLineViewModel temp = _PurchaseQuotationLineService.GetPurchaseQuotationLine(id);

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

            PurchaseQuotationHeader H = _PurchaseQuotationHeaderService.Find(temp.PurchaseQuotationHeaderId);

            PrepareViewBag(temp);

            //Getting Settings
            var settings = new PurchaseQuotationSettingService(_unitOfWork).GetPurchaseQuotationSettingForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            temp.PurchQuotationSettings = Mapper.Map <PurchaseQuotationSetting, PurchaseQuotationSettingsViewModel>(settings);
            return(PartialView("_Create", temp));
        }
        public ActionResult DeletePost(PurchaseQuotationLineViewModel vm)
        {
            bool BeforeSave = true;

            //try
            //{
            //    BeforeSave = PurchaseQuotationReceiveDocEvents.beforeLineDeleteEvent(this, new PurchaseEventArgs(vm.PurchaseQuotationHeaderId, vm.PurchaseQuotationLineId), 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>();

                PurchaseQuotationLine PurchaseQuotationLine = db.PurchaseQuotationLine.Find(vm.PurchaseQuotationLineId);

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

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

                //_PurchaseQuotationLineService.Delete(PurchaseQuotationLine);
                PurchaseQuotationHeader header = _PurchaseQuotationHeaderService.Find(PurchaseQuotationLine.PurchaseQuotationHeaderId);

                var chargeslist = (from p in db.PurchaseQuotationLineCharge
                                   where p.LineTableId == PurchaseQuotationLine.PurchaseQuotationLineId
                                   select p).ToList();


                if (chargeslist != null)
                {
                    foreach (var item in chargeslist)
                    {
                        item.ObjectState = Model.ObjectState.Deleted;
                        db.PurchaseQuotationLineCharge.Remove(item);
                        //new PurchaseQuotationLineChargeService(_unitOfWork).Delete(item.Id);
                    }
                }

                if (vm.footercharges != null)
                {
                    foreach (var item in vm.footercharges)
                    {
                        var footer = new PurchaseQuotationHeaderChargeService(_unitOfWork).Find(item.Id);
                        footer.Rate        = item.Rate;
                        footer.Amount      = item.Amount;
                        footer.ObjectState = Model.ObjectState.Modified;
                        db.PurchaseQuotationHeaderCharge.Add(footer);
                        //new PurchaseQuotationHeaderChargeService(_unitOfWork).Update(footer);
                    }
                }

                PurchaseQuotationLine.ObjectState = Model.ObjectState.Deleted;
                db.PurchaseQuotationLine.Remove(PurchaseQuotationLine);

                if (header.Status != (int)StatusConstants.Drafted)
                {
                    header.Status       = (int)StatusConstants.Modified;
                    header.ModifiedDate = DateTime.Now;
                    header.ModifiedBy   = User.Identity.Name;
                }
                header.ObjectState = Model.ObjectState.Modified;
                db.PurchaseQuotationHeader.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;
                    PrepareViewBag(null);
                    return(PartialView("_Create", vm));
                }

                //try
                //{
                //    PurchaseQuotationReceiveDocEvents.afterLineDeleteEvent(this, new PurchaseEventArgs(PurchaseQuotationLine.PurchaseQuotationHeaderId, PurchaseQuotationLine.PurchaseQuotationLineId), 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,
                    DocLineId       = PurchaseQuotationLine.PurchaseQuotationLineId,
                    ActivityType    = (int)ActivityTypeContants.Deleted,
                    DocNo           = header.DocNo,
                    xEModifications = Modifications,
                    DocDate         = header.DocDate,
                    DocStatus       = header.Status,
                }));
            }

            return(Json(new { success = true }));
        }
        private ActionResult _Delete(int id)
        {
            PurchaseQuotationLineViewModel temp = _PurchaseQuotationLineService.GetPurchaseQuotationLine(id);
            bool IndentBased = false;

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

            if (temp.PurchaseIndentLineId.HasValue)
            {
                var Indent = (from p in db.ViewPurchaseIndentBalance
                              where p.PurchaseIndentLineId == temp.PurchaseIndentLineId
                              select new { p.PurchaseIndentLineId, p.PurchaseIndentNo, p.BalanceQty }).FirstOrDefault();
                temp.PurchaseIndentDocNo  = Indent.PurchaseIndentNo;
                temp.PurchaseIndentLineId = Indent.PurchaseIndentLineId;
                temp.IndentBalanceQty     = Indent.BalanceQty;
                IndentBased = true;
            }

            PurchaseQuotationHeader H = _PurchaseQuotationHeaderService.Find(temp.PurchaseQuotationHeaderId);
            PrepareViewBag(temp);

            //Getting Settings
            var settings = new PurchaseQuotationSettingService(_unitOfWork).GetPurchaseQuotationSettingForDocument(H.DocTypeId, H.DivisionId, H.SiteId);

            temp.PurchQuotationSettings = Mapper.Map <PurchaseQuotationSetting, PurchaseQuotationSettingsViewModel>(settings);
            if (IndentBased == true)
            {
                return(PartialView("_CreateForIndent", temp));
            }
            else
            {
                return(PartialView("_Create", temp));
            }
        }
        //[ValidateAntiForgeryToken]
        public ActionResult _CreatePost(PurchaseQuotationLineViewModel svm)
        {
            PurchaseQuotationLine   s    = Mapper.Map <PurchaseQuotationLineViewModel, PurchaseQuotationLine>(svm);
            PurchaseQuotationHeader temp = _PurchaseQuotationHeaderService.Find(s.PurchaseQuotationHeaderId);

            if (svm.PurchaseQuotationLineId <= 0)
            {
                ViewBag.LineMode = "Create";
            }
            else
            {
                ViewBag.LineMode = "Edit";
            }

            if (svm.PurchQuotationSettings != null)
            {
                if (svm.PurchQuotationSettings.isMandatoryRate == true && svm.Rate <= 0)
                {
                    ModelState.AddModelError("Rate", "The Rate field is required");
                }
            }

            if (svm.Qty <= 0)
            {
                ModelState.AddModelError("Qty", "The Qty field is required");
            }

            bool BeforeSave = true;

            //try
            //{

            //    if (svm.PurchaseQuotationHeaderId <= 0)
            //        BeforeSave = PurchaseQuotationReceiveDocEvents.beforeLineSaveEvent(this, new PurchaseEventArgs(svm.PurchaseQuotationHeaderId, EventModeConstants.Add), ref db);
            //    else
            //        BeforeSave = PurchaseQuotationReceiveDocEvents.beforeLineSaveEvent(this, new PurchaseEventArgs(svm.PurchaseQuotationHeaderId, 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 (ModelState.IsValid && BeforeSave && !EventException)
            {
                int ProcessId = new ProcessService(_unitOfWork).Find(ProcessConstants.FullFinishing).ProcessId;
                if (svm.PurchaseQuotationLineId <= 0)
                {
                    s.DiscountPer  = svm.DiscountPer;
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.Sr           = _PurchaseQuotationLineService.GetMaxSr(s.PurchaseQuotationHeaderId);
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    s.ObjectState  = Model.ObjectState.Added;
                    db.PurchaseQuotationLine.Add(s);


                    if (svm.linecharges != null)
                    {
                        foreach (var item in svm.linecharges)
                        {
                            item.LineTableId   = s.PurchaseQuotationLineId;
                            item.PersonID      = temp.SupplierId;
                            item.HeaderTableId = s.PurchaseQuotationHeaderId;
                            item.ObjectState   = Model.ObjectState.Added;
                            db.PurchaseQuotationLineCharge.Add(item);
                            //new PurchaseQuotationLineChargeService(_unitOfWork).Create(item);
                        }
                    }

                    if (svm.footercharges != null)
                    {
                        foreach (var item in svm.footercharges)
                        {
                            if (item.Id > 0)
                            {
                                var footercharge = new PurchaseQuotationHeaderChargeService(_unitOfWork).Find(item.Id);
                                footercharge.Rate        = item.Rate;
                                footercharge.Amount      = item.Amount;
                                footercharge.ObjectState = Model.ObjectState.Modified;
                                db.PurchaseQuotationHeaderCharge.Add(footercharge);
                                //new PurchaseQuotationHeaderChargeService(_unitOfWork).Update(footercharge);
                            }

                            else
                            {
                                item.HeaderTableId = s.PurchaseQuotationHeaderId;
                                item.PersonID      = temp.SupplierId;
                                item.ObjectState   = Model.ObjectState.Added;
                                db.PurchaseQuotationHeaderCharge.Add(item);
                                //new PurchaseQuotationHeaderChargeService(_unitOfWork).Create(item);
                            }
                        }
                    }



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

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

                    //try
                    //{
                    //    PurchaseQuotationReceiveDocEvents.onLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseQuotationHeaderId, s.PurchaseQuotationLineId, 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;
                        PrepareViewBag(null);
                        return(PartialView("_Create", svm));
                    }
                    //try
                    //{
                    //    PurchaseQuotationReceiveDocEvents.afterLineSaveEvent(this, new PurchaseEventArgs(s.PurchaseQuotationHeaderId, s.PurchaseQuotationLineId, EventModeConstants.Add), ref db);
                    //}
                    //catch (Exception ex)
                    //{
                    //    string message = _exception.HandleException(ex);
                    //    TempData["CSEXCL"] += message;
                    //}

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

                    return(RedirectToAction("_Create", new { id = s.PurchaseQuotationHeaderId, sid = svm.SupplierId, IsIndentBased = (svm.PurchaseIndentLineId.HasValue && svm.PurchaseIndentLineId.Value > 0 ? true : false) }));
                }
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    PurchaseQuotationHeader header = _PurchaseQuotationHeaderService.Find(svm.PurchaseQuotationHeaderId);
                    int status = header.Status;
                    PurchaseQuotationLine temp1 = _PurchaseQuotationLineService.Find(svm.PurchaseQuotationLineId);

                    PurchaseQuotationLine ExRecI = new PurchaseQuotationLine();
                    ExRecI = Mapper.Map <PurchaseQuotationLine>(temp1);

                    temp1.DiscountPer = svm.DiscountPer;
                    temp1.Amount      = svm.Amount;
                    temp1.UnitConversionMultiplier = svm.UnitConversionMultiplier;
                    temp1.DealQty       = svm.DealQty;
                    temp1.DealUnitId    = svm.DealUnitId;
                    temp1.Qty           = svm.Qty;
                    temp1.Rate          = svm.Rate;
                    temp1.Remark        = svm.Remark;
                    temp1.Dimension1Id  = svm.Dimension1Id;
                    temp1.Dimension2Id  = svm.Dimension2Id;
                    temp1.Specification = svm.Specification;
                    temp1.LotNo         = svm.LotNo;
                    temp1.ModifiedDate  = DateTime.Now;
                    temp1.ModifiedBy    = User.Identity.Name;
                    temp1.ObjectState   = Model.ObjectState.Modified;
                    db.PurchaseQuotationLine.Add(temp1);
                    //_PurchaseQuotationLineService.Update(temp1);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRecI,
                        Obj   = temp1,
                    });

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

                        //new PurchaseQuotationHeaderService(_unitOfWork).Update(header);
                    }

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

                    if (svm.linecharges != null)
                    {
                        foreach (var item in svm.linecharges)
                        {
                            var productcharge = db.PurchaseQuotationLineCharge.Find(item.Id);

                            productcharge.Rate        = item.Rate;
                            productcharge.Amount      = item.Amount;
                            productcharge.ObjectState = Model.ObjectState.Modified;
                            db.PurchaseQuotationLineCharge.Add(productcharge);
                            //new PurchaseQuotationLineChargeService(_unitOfWork).Update(productcharge);
                        }
                    }


                    if (svm.footercharges != null)
                    {
                        foreach (var item in svm.footercharges)
                        {
                            var footercharge = db.PurchaseQuotationHeaderCharge.Find(item.Id);

                            footercharge.Rate        = item.Rate;
                            footercharge.Amount      = item.Amount;
                            footercharge.ObjectState = Model.ObjectState.Modified;
                            db.PurchaseQuotationHeaderCharge.Add(footercharge);
                            //new PurchaseQuotationHeaderChargeService(_unitOfWork).Update(footercharge);
                        }
                    }

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

                    //try
                    //{
                    //    PurchaseQuotationReceiveDocEvents.onLineSaveEvent(this, new PurchaseEventArgs(temp1.PurchaseQuotationHeaderId, temp1.PurchaseQuotationLineId, 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;
                        PrepareViewBag(null);
                        return(PartialView("_Create", svm));
                    }

                    //try
                    //{
                    //    PurchaseQuotationReceiveDocEvents.afterLineSaveEvent(this, new PurchaseEventArgs(temp1.PurchaseQuotationHeaderId, temp1.PurchaseQuotationLineId, 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           = temp1.PurchaseQuotationHeaderId,
                        DocLineId       = temp1.PurchaseQuotationLineId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.DocNo,
                        xEModifications = Modifications,
                        DocDate         = temp.DocDate,
                        DocStatus       = temp.Status,
                    }));

                    //End of Saving the Activity Log

                    return(Json(new { success = true }));
                }
            }
            PrepareViewBag(svm);
            return(PartialView("_Create", svm));
        }
 private void PrepareViewBag(PurchaseQuotationLineViewModel vm)
 {
     ViewBag.DeliveryUnitList = new UnitService(_unitOfWork).GetUnitList().ToList();
 }