public ActionResult CopyFromExisting()
        {
            CopyFromExistingProductConsumptionViewModel vm = new CopyFromExistingProductConsumptionViewModel();

            if (Session["ProductConsumptionFromPId"] != null)
            {
                vm.FromProductId = (int)Session["ProductConsumptionFromPId"];
            }
            return(PartialView("CopyFromExisting", vm));
        }
        public ActionResult CopyFromExisting(CopyFromExistingProductConsumptionViewModel vm)
        {
            if (ModelState.IsValid)
            {
                Session["ProductConsumptionFromPId"] = vm.FromProductId;
                Product NewProduct = new Product();
                Product prod       = new ProductService(_unitOfWork).Find(vm.ToProductId);

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

                NewProduct.ProductName        = prod.ProductName + "-Bom";
                NewProduct.ProductGroupId     = new ProductGroupService(_unitOfWork).Find(ProductGroupConstants.Bom).ProductGroupId;
                NewProduct.DivisionId         = (int)System.Web.HttpContext.Current.Session["DivisionId"];
                NewProduct.IsActive           = true;
                NewProduct.CreatedDate        = DateTime.Now;
                NewProduct.ModifiedDate       = DateTime.Now;
                NewProduct.CreatedBy          = User.Identity.Name;
                NewProduct.ModifiedBy         = User.Identity.Name;
                NewProduct.ReferenceDocTypeId = new DocumentTypeService(_unitOfWork).FindByName(MasterDocTypeConstants.Product).DocumentTypeId;
                NewProduct.ReferenceDocId     = prod.ProductId;
                NewProduct.ObjectState        = Model.ObjectState.Added;
                _ProductService.Create(NewProduct);


                var ExistingRec = _BomDetailService.GetExistingBaseProduct(prod.ProductId);
                if (ExistingRec != null)
                {
                    ExistingRec.ProductId   = NewProduct.ProductId;
                    ExistingRec.CreatedDate = DateTime.Now;
                    ExistingRec.ModifiedBy  = User.Identity.Name;
                    _BomDetailService.Update(ExistingRec);
                }
                else
                {
                    BomDetail bomdetail = new BomDetail();

                    bomdetail.BaseProductId  = prod.ProductId;
                    bomdetail.BatchQty       = 1;
                    bomdetail.ConsumptionPer = 100;
                    bomdetail.ProcessId      = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                    bomdetail.ProductId      = NewProduct.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);
                }

                IEnumerable <BomDetail> bomdetList = new BomDetailService(_unitOfWork).GetBomDetailList(vm.FromProductId);

                foreach (BomDetail item in bomdetList)
                {
                    BomDetail bomdet = new BomDetail();
                    bomdet.BaseProductId  = NewProduct.ProductId;
                    bomdet.BatchQty       = item.BatchQty;
                    bomdet.ConsumptionPer = item.ConsumptionPer;
                    bomdet.Dimension1Id   = item.Dimension1Id;
                    bomdet.ProcessId      = item.ProcessId;
                    bomdet.ProductId      = item.ProductId;
                    bomdet.Qty            = item.Qty;
                    bomdet.CreatedDate    = DateTime.Now;
                    bomdet.ModifiedDate   = DateTime.Now;
                    bomdet.CreatedBy      = User.Identity.Name;
                    bomdet.ModifiedBy     = User.Identity.Name;
                    bomdet.ObjectState    = Model.ObjectState.Added;
                    _BomDetailService.Create(bomdet);
                }


                try
                {
                    _unitOfWork.Save();
                }

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

                return(Json(new { success = true, Url = "/ProductConsumptionHeader/Edit/" + NewProduct.ProductId }));
            }

            return(PartialView("CopyFromExisting", vm));
        }