public ActionResult _CreatePost(ProductConsumptionLineViewModel svm)
        {
            if (ModelState.IsValid)
            {
                if (svm.BomDetailId == 0)
                {
                    BomDetail bomdetail = new BomDetail();

                    bomdetail.BaseProductId = svm.BaseProductId;
                    bomdetail.BatchQty      = 1;
                    //bomdetail.ProcessId = new ProcessService(_unitOfWork).Find(ProcessConstants.Silai).ProcessId;
                    bomdetail.BaseProcessId = new ProcessService(_unitOfWork).Find(ProcessConstants.Manufacturing).ProcessId;
                    bomdetail.ProductId     = svm.ProductId;
                    bomdetail.ProcessId     = svm.ProcessId;
                    bomdetail.Dimension1Id  = svm.Dimension1Id;
                    bomdetail.Dimension2Id  = svm.Dimension2Id;
                    bomdetail.Dimension3Id  = svm.Dimension3Id;
                    bomdetail.Dimension4Id  = svm.Dimension4Id;
                    bomdetail.Qty           = svm.Qty;
                    bomdetail.MBQ           = svm.MBQ;
                    bomdetail.StdCost       = svm.StdCost;
                    bomdetail.StdTime       = svm.StdTime;

                    bomdetail.CreatedDate  = DateTime.Now;
                    bomdetail.ModifiedDate = DateTime.Now;
                    bomdetail.CreatedBy    = User.Identity.Name;
                    bomdetail.ModifiedBy   = User.Identity.Name;
                    bomdetail.ObjectState  = Model.ObjectState.Added;
                    _BomDetailService.Create(bomdetail);


                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }
                    return(RedirectToAction("_Create", new { id = svm.BaseProductId }));
                }
                else
                {
                    BomDetail     bomdetail = _BomDetailService.Find(svm.BomDetailId);
                    StringBuilder logstring = new StringBuilder();

                    bomdetail.BaseProductId = svm.BaseProductId;
                    bomdetail.BatchQty      = 1;
                    bomdetail.BaseProcessId = new ProcessService(_unitOfWork).Find(ProcessConstants.Manufacturing).ProcessId;
                    bomdetail.ProductId     = svm.ProductId;
                    bomdetail.ProcessId     = svm.ProcessId;
                    bomdetail.Dimension1Id  = svm.Dimension1Id;
                    bomdetail.Dimension2Id  = svm.Dimension2Id;
                    bomdetail.Dimension3Id  = svm.Dimension3Id;
                    bomdetail.Dimension4Id  = svm.Dimension4Id;
                    bomdetail.Qty           = svm.Qty;
                    bomdetail.MBQ           = svm.MBQ;
                    bomdetail.StdCost       = svm.StdCost;
                    bomdetail.StdTime       = svm.StdTime;


                    bomdetail.ModifiedDate = DateTime.Now;
                    bomdetail.ModifiedBy   = User.Identity.Name;
                    bomdetail.ObjectState  = Model.ObjectState.Modified;
                    _BomDetailService.Update(bomdetail);


                    //Saving the Activity Log
                    ActivityLog al = new ActivityLog()
                    {
                        ActivityType = (int)ActivityTypeContants.Modified,
                        DocId        = bomdetail.BomDetailId,
                        CreatedDate  = DateTime.Now,
                        Narration    = logstring.ToString(),
                        CreatedBy    = User.Identity.Name,
                        //DocTypeId = new DocumentTypeService(_unitOfWork).FindByName(TransactionDocCategoryConstants.BomDetail).DocumentTypeId,
                    };
                    new ActivityLogService(_unitOfWork).Create(al);
                    //End of Saving the Activity Log


                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }
                    return(Json(new { success = true }));
                }
            }

            PrepareViewBag(svm);
            return(PartialView("_Create", svm));
        }
        public ActionResult Create(ProductConsumptionHeaderViewModel svm)
        {
            Product SProd = new ProductService(_unitOfWork).Find(svm.ProductId);

            if (ModelState.IsValid)
            {
                if (svm.BaseProductId == 0)
                {
                    Product product = new Product();



                    if (SProd.ProductName.Length > 16)
                    {
                        product.ProductCode = SProd.ProductName.ToString().Substring(0, 16) + "-Bom";
                    }
                    else
                    {
                        product.ProductCode = SProd.ProductName.ToString().Substring(0, SProd.ProductName.Length) + "-Bom";
                    }

                    product.ProductName        = SProd.ProductName + "-Bom";
                    product.ProductGroupId     = new ProductGroupService(_unitOfWork).Find(ProductGroupConstants.Bom).ProductGroupId;
                    product.DivisionId         = (int)System.Web.HttpContext.Current.Session["DivisionId"];
                    product.IsActive           = true;
                    product.ReferenceDocTypeId = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.Product).DocumentTypeId;
                    product.ReferenceDocId     = svm.ProductId;


                    product.CreatedDate  = DateTime.Now;
                    product.ModifiedDate = DateTime.Now;
                    product.CreatedBy    = User.Identity.Name;
                    product.ModifiedBy   = User.Identity.Name;
                    product.ObjectState  = Model.ObjectState.Added;
                    _ProductService.Create(product);



                    var ExistingRec = _BomDetailService.GetExistingBaseProduct(SProd.ProductId);
                    if (ExistingRec != null)
                    {
                        ExistingRec.ProductId   = product.ProductId;
                        ExistingRec.CreatedDate = DateTime.Now;
                        ExistingRec.ModifiedBy  = User.Identity.Name;
                        _BomDetailService.Update(ExistingRec);
                    }
                    else
                    {
                        BomDetail bomdetail = new BomDetail();
                        bomdetail.BaseProductId  = SProd.ProductId;
                        bomdetail.BatchQty       = 1;
                        bomdetail.ConsumptionPer = 100;
                        bomdetail.ProcessId      = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                        bomdetail.ProductId      = product.ProductId;
                        bomdetail.Qty            = 1;

                        bomdetail.CreatedDate  = DateTime.Now;
                        bomdetail.ModifiedDate = DateTime.Now;
                        bomdetail.CreatedBy    = User.Identity.Name;
                        bomdetail.ModifiedBy   = User.Identity.Name;
                        bomdetail.ObjectState  = Model.ObjectState.Added;
                        _BomDetailService.Create(bomdetail);
                    }



                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(View(svm));
                    }

                    //return RedirectToAction("Create").Success("Data saved successfully");
                    return(RedirectToAction("Edit", new { id = product.ProductId }).Success("Data saved Successfully"));
                }
                else
                {
                    Product product = _ProductService.Find(svm.BaseProductId);

                    if (SProd.ProductName.Length > 16)
                    {
                        product.ProductCode = SProd.ProductName.ToString().Substring(0, 16) + "-Bom";
                    }
                    else
                    {
                        product.ProductCode = SProd.ProductName.ToString().Substring(0, SProd.ProductName.Length) + "-Bom";
                    }


                    product.ProductName        = SProd.ProductName + "-Bom";
                    product.ReferenceDocTypeId = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.Product).DocumentTypeId;
                    product.ReferenceDocId     = svm.ProductId;
                    product.ModifiedBy         = User.Identity.Name;
                    product.ModifiedDate       = DateTime.Now;

                    StringBuilder logstring = new StringBuilder();

                    _ProductService.Update(product);



                    ////Saving Activity Log::
                    ActivityLog al = new ActivityLog()
                    {
                        ActivityType = (int)ActivityTypeContants.Modified,
                        DocId        = svm.BaseProductId,
                        Narration    = logstring.ToString(),
                        CreatedDate  = DateTime.Now,
                        CreatedBy    = User.Identity.Name,
                        //DocTypeId = new DocumentTypeService(_unitOfWork).FindByName(TransactionDocCategoryConstants.ProcessSequence).DocumentTypeId,
                    };
                    new ActivityLogService(_unitOfWork).Create(al);
                    //End of Saving ActivityLog

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(View("Create", svm));
                    }
                    return(RedirectToAction("Index").Success("Data saved successfully"));
                }
            }
            PrepareViewBag();
            return(View(svm));
        }
        public ActionResult _CreatePost(DesignConsumptionLineViewModel svm)
        {
            if (ModelState.IsValid)
            {
                FinishedProduct product = new FinishedProduct();
                if (svm.BaseProductId == 0)
                {
                    ProductQuality Quality = new ProductQualityService(_unitOfWork).Find(svm.QualityName);

                    string ConsumptionProductName = "";
                    if (svm.ColourName != "" && svm.ColourName != null)
                    {
                        ConsumptionProductName = svm.DesignName.ToString().Trim() + "-" + svm.ColourName.ToString().Trim() + "-Bom";
                    }
                    else
                    {
                        ConsumptionProductName = svm.DesignName.ToString().Trim() + "-Bom";
                    }


                    int ProductGroupId = new ProductGroupService(_unitOfWork).Find(svm.DesignName).ProductGroupId;

                    product.ProductCode        = ConsumptionProductName;
                    product.ProductName        = ConsumptionProductName;
                    product.ProductGroupId     = new ProductGroupService(_unitOfWork).Find(ProductGroupConstants.Bom).ProductGroupId;
                    product.DivisionId         = (int)System.Web.HttpContext.Current.Session["DivisionId"];
                    product.IsActive           = true;
                    product.ReferenceDocTypeId = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.ProductGroup).DocumentTypeId;
                    product.ReferenceDocId     = ProductGroupId;
                    product.StandardWeight     = svm.Weight;
                    product.CreatedDate        = DateTime.Now;
                    product.ModifiedDate       = DateTime.Now;
                    product.CreatedBy          = User.Identity.Name;
                    product.ModifiedBy         = User.Identity.Name;
                    product.ObjectState        = Model.ObjectState.Added;

                    product.IsSample         = false;
                    product.ProductQualityId = Quality.ProductQualityId;
                    new ProductService(_unitOfWork).Create(product);


                    ProductGroup ProductGroup = new ProductGroupService(_unitOfWork).Find(svm.DesignName);
                    Colour       Colour       = new ColourService(_unitOfWork).Find(svm.ColourName);
                    if (ProductGroup != null && Colour != null)
                    {
                        var ProductList = (from p in db.FinishedProduct
                                           where p.ProductGroupId == ProductGroup.ProductGroupId && p.ColourId == Colour.ColourId
                                           select new
                        {
                            ProductId = p.ProductId
                        }).ToList();

                        foreach (var item in ProductList)
                        {
                            BomDetail bomdetail = new BomDetail();

                            bomdetail.BaseProductId  = item.ProductId;
                            bomdetail.BatchQty       = 1;
                            bomdetail.ConsumptionPer = 100;
                            bomdetail.ProcessId      = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                            bomdetail.BaseProcessId  = svm.BaseProcessId;
                            bomdetail.ProductId      = product.ProductId;
                            bomdetail.Qty            = 1;

                            bomdetail.CreatedDate  = DateTime.Now;
                            bomdetail.ModifiedDate = DateTime.Now;
                            bomdetail.CreatedBy    = User.Identity.Name;
                            bomdetail.ModifiedBy   = User.Identity.Name;
                            bomdetail.ObjectState  = Model.ObjectState.Added;
                            _BomDetailService.Create(bomdetail);
                        }
                    }


                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.DesignColourConsumption).DocumentTypeId,
                        DocId        = product.ProductId,
                        ActivityType = (int)ActivityTypeContants.Added,
                    }));
                }
                else
                {
                    //product = new FinishedProductService(_unitOfWork).Find(svm.BaseProductId);
                    product.ProductId = svm.BaseProductId;
                }

                if (svm.BomDetailId == 0)
                {
                    BomDetail bomdetail = new BomDetail();

                    bomdetail.BaseProductId  = svm.BaseProductId;
                    bomdetail.BatchQty       = 1;
                    bomdetail.ConsumptionPer = svm.ConsumptionPer;
                    bomdetail.Dimension1Id   = svm.Dimension1Id;
                    bomdetail.ProcessId      = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                    bomdetail.BaseProcessId  = svm.BaseProcessId;
                    bomdetail.ProductId      = svm.ProductId;
                    bomdetail.Qty            = svm.Qty;

                    bomdetail.CreatedDate  = DateTime.Now;
                    bomdetail.ModifiedDate = DateTime.Now;
                    bomdetail.CreatedBy    = User.Identity.Name;
                    bomdetail.ModifiedBy   = User.Identity.Name;
                    bomdetail.ObjectState  = Model.ObjectState.Added;
                    _BomDetailService.Create(bomdetail);


                    if (bomdetail.BaseProductId == bomdetail.ProductId)
                    {
                        PrepareViewBag(svm);
                        //return View(svm).Danger(DataValidationMsg);
                        ModelState.AddModelError("", "Invalid Product is Selected!");
                        return(PartialView("_Create", svm));
                    }
                    try
                    {
                        _unitOfWork.Save();
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.DesignConsumption).DocumentTypeId,
                        DocId        = bomdetail.BomDetailId,
                        ActivityType = (int)ActivityTypeContants.Added,
                    }));



                    if (svm.ContentType == "Main Contents")
                    {
                        return(RedirectToAction("_CreateMainContentForBaseProduct", new { id = product.ProductId }));
                    }
                    else if (svm.ContentType == "OverTuft Contents")
                    {
                        return(RedirectToAction("_CreateOverTuftContentForBaseProduct", new { id = product.ProductId }));
                    }
                    else
                    {
                        return(RedirectToAction("_CreateOtherContentForBaseProduct", new { id = product.ProductId }));
                    }
                }
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    BomDetail bomdetail = _BomDetailService.Find(svm.BomDetailId);

                    BomDetail ExRec = Mapper.Map <BomDetail>(bomdetail);

                    bomdetail.BaseProductId  = svm.BaseProductId;
                    bomdetail.BatchQty       = 1;
                    bomdetail.ConsumptionPer = svm.ConsumptionPer;
                    bomdetail.Dimension1Id   = svm.Dimension1Id;
                    bomdetail.ProductId      = svm.ProductId;
                    bomdetail.Qty            = svm.Qty;


                    bomdetail.ModifiedDate = DateTime.Now;
                    bomdetail.ModifiedBy   = User.Identity.Name;
                    bomdetail.ObjectState  = Model.ObjectState.Modified;
                    _BomDetailService.Update(bomdetail);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = bomdetail,
                    });
                    XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);
                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.DesignConsumption).DocumentTypeId,
                        DocId           = bomdetail.BomDetailId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        xEModifications = Modifications,
                    }));

                    return(Json(new { success = true }));
                }
            }

            PrepareViewBag(svm);
            return(PartialView("_Create", svm));
        }
        public ActionResult _CreatePost(ProductConsumptionLineViewModel svm)
        {
            if (ModelState.IsValid)
            {
                if (svm.BomDetailId == 0)
                {
                    BomDetail bomdetail = new BomDetail();

                    bomdetail.BaseProductId  = svm.BaseProductId;
                    bomdetail.BatchQty       = 1;
                    bomdetail.ConsumptionPer = svm.ConsumptionPer;
                    bomdetail.Dimension1Id   = svm.Dimension1Id;
                    bomdetail.ProcessId      = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                    bomdetail.ProductId      = svm.ProductId;
                    bomdetail.Qty            = svm.Qty;

                    bomdetail.CreatedDate  = DateTime.Now;
                    bomdetail.ModifiedDate = DateTime.Now;
                    bomdetail.CreatedBy    = User.Identity.Name;
                    bomdetail.ModifiedBy   = User.Identity.Name;
                    bomdetail.ObjectState  = Model.ObjectState.Added;
                    _BomDetailService.Create(bomdetail);


                    if (bomdetail.BaseProductId == bomdetail.ProductId)
                    {
                        //return View(svm).Danger(DataValidationMsg);
                        ModelState.AddModelError("", "Invalid Product is Selected!");
                        return(PartialView("_Create", svm));
                    }

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }


                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.ProductConsumption).DocumentTypeId,
                        DocId        = bomdetail.BomDetailId,
                        ActivityType = (int)ActivityTypeContants.Added,
                    }));

                    return(RedirectToAction("_Create", new { id = svm.BaseProductId }));
                }
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();
                    BomDetail bomdetail             = _BomDetailService.Find(svm.BomDetailId);

                    BomDetail ExRec = Mapper.Map <BomDetail>(bomdetail);

                    bomdetail.BaseProductId  = svm.BaseProductId;
                    bomdetail.BatchQty       = 1;
                    bomdetail.ConsumptionPer = svm.ConsumptionPer;
                    bomdetail.Dimension1Id   = svm.Dimension1Id;
                    bomdetail.ProductId      = svm.ProductId;
                    bomdetail.Qty            = svm.Qty;


                    bomdetail.ModifiedDate = DateTime.Now;
                    bomdetail.ModifiedBy   = User.Identity.Name;
                    bomdetail.ObjectState  = Model.ObjectState.Modified;
                    _BomDetailService.Update(bomdetail);


                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = bomdetail,
                    });
                    XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.ProductConsumption).DocumentTypeId,
                        DocId           = bomdetail.BomDetailId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        xEModifications = Modifications,
                    }));

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