// GET: /ProductUidHeader/Edit/5
        public ActionResult Edit(int id, string PrevAction, string PrevController)
        {
            if (PrevAction != null)
            {
                ViewBag.Redirect = PrevAction;
            }

            ProductUidHeaderIndexViewModel svm = _ProductUidHeaderService.GetProductUidHeaderIndexViewModel(id);

            PrepareViewBag(svm);
            if (svm == null)
            {
                return(HttpNotFound());
            }
            return(View("Create", svm));
        }
        // GET: /PurchaseOrderHeader/Delete/5

        public ActionResult Delete(int id, string PrevAction, string PrevController)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ProductUidHeaderIndexViewModel ProductUidHeader = _ProductUidHeaderService.GetProductUidHeaderIndexViewModel(id);

            if (ProductUidHeader == null)
            {
                return(HttpNotFound());
            }
            ReasonViewModel rvm = new ReasonViewModel()
            {
                id = id,
            };

            return(PartialView("_Reason", rvm));
        }
        // GET: /ProductUidHeader/Create

        public ActionResult Create()
        {
            ProductUidHeaderIndexViewModel p = new ProductUidHeaderIndexViewModel();

            p.GenDocTypeId = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.ProductUid).DocumentTypeId;
            p.GenDocDate   = DateTime.Now;
            p.GenDocNo     = new ProductUidHeaderService(_unitOfWork).FGetNewDocNo("GenDocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".ProductUidHeaders", p.GenDocTypeId, p.GenDocDate);
            ViewBag.Mode   = "Add";



            string PendingToPrintProductUidHeaderIdsSessionVarName = "ProductUid_PendingToPrintProductUidHeaderIds" + User.Identity.Name.ToString().ToString();

            if (System.Web.HttpContext.Current.Session[PendingToPrintProductUidHeaderIdsSessionVarName] != null)
            {
                p.PrintProductUidHeaderIds = System.Web.HttpContext.Current.Session[PendingToPrintProductUidHeaderIdsSessionVarName].ToString();
            }


            return(View("Create", p));
        }
        public ActionResult HeaderPost(ProductUidHeaderIndexViewModel svm)
        {
            string GodownName  = "";
            string ProcessName = "";

            ViewBag.Mode = "Add";

            if (svm.Qty == 0)
            {
                ModelState.AddModelError("", "Please enter qty.");
                PrepareViewBag(svm);
                return(View("Create", svm));
            }

            if (svm.GenPersonId == 0 || svm.GenPersonId == null)
            {
                ModelState.AddModelError("", "Please enter Person.");
                PrepareViewBag(svm);
                return(View("Create", svm));
            }

            if (svm.ProductId == 0 || svm.ProductId == null)
            {
                ModelState.AddModelError("", "Please enter Product.");
                PrepareViewBag(svm);
                return(View("Create", svm));
            }

            if (ModelState.IsValid)
            {
                if (svm.ProductUidHeaderId == 0)
                {
                    if (svm.GodownId != null && svm.GodownId != 0)
                    {
                        GodownName = new GodownService(_unitOfWork).Find((int)svm.GodownId).GodownName;
                    }

                    if (svm.ProcessId != null && svm.ProcessId != 0)
                    {
                        ProcessName = new ProcessService(_unitOfWork).Find((int)svm.ProcessId).ProcessName;
                    }



                    ProductUidHeader ProdUidHeader = new ProductUidHeader();
                    //ProdUidHeader.GenDocNo = svm.GenDocNo;
                    ProdUidHeader.ProductId    = svm.ProductId;
                    ProdUidHeader.GenDocTypeId = svm.GenDocTypeId;
                    ProdUidHeader.GenDocDate   = svm.GenDocDate;
                    ProdUidHeader.GenPersonId  = svm.GenPersonId;
                    ProdUidHeader.GenDocNo     = new ProductUidHeaderService(_unitOfWork).FGetNewDocNo("GenDocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".ProductUidHeaders", svm.GenDocTypeId, svm.GenDocDate);
                    ProdUidHeader.CreatedDate  = DateTime.Now;
                    ProdUidHeader.ModifiedDate = DateTime.Now;
                    ProdUidHeader.CreatedBy    = User.Identity.Name;
                    ProdUidHeader.ModifiedBy   = User.Identity.Name;

                    ProdUidHeader.GenRemark = "Godown : " + GodownName + ", Process : " + ProcessName;


                    ProdUidHeader.ObjectState = Model.ObjectState.Added;
                    context.ProductUidHeader.Add(ProdUidHeader);

                    List <string> uids = new List <string>();

                    using (SqlConnection sqlConnection = new SqlConnection((string)System.Web.HttpContext.Current.Session["DefaultConnectionString"]))
                    {
                        sqlConnection.Open();

                        int TypeId = (int)svm.GenDocTypeId;

                        SqlCommand Totalf = new SqlCommand("SELECT * FROM Web.FGenerateBarcodeList ( " + TypeId + ", " + svm.Qty + ")", sqlConnection);

                        SqlDataReader Reader = (Totalf.ExecuteReader());
                        while (Reader.Read())
                        {
                            uids.Add((string)Reader.GetValue(0));
                        }
                    }



                    foreach (string UidItem in uids)
                    {
                        ProductUid ProdUid = new ProductUid();

                        ProdUid.ProductUidHeaderId = ProdUidHeader.ProductUidHeaderId;
                        ProdUid.ProductUidName     = UidItem;
                        ProdUid.ProductId          = ProdUidHeader.ProductId;
                        ProdUid.IsActive           = true;
                        ProdUid.CreatedBy          = User.Identity.Name;
                        ProdUid.CreatedDate        = DateTime.Now;
                        ProdUid.ModifiedBy         = User.Identity.Name;
                        ProdUid.ModifiedDate       = DateTime.Now;
                        //ProdUid.GenLineId = item.JobOrderLineId;
                        //ProdUid.GenDocId = pd.JobOrderHeaderId;
                        ProdUid.GenDocNo                 = ProdUidHeader.GenDocNo;
                        ProdUid.GenDocTypeId             = ProdUidHeader.GenDocTypeId;
                        ProdUid.GenDocDate               = ProdUidHeader.GenDocDate;
                        ProdUid.GenPersonId              = ProdUidHeader.GenPersonId;
                        ProdUid.CurrenctProcessId        = svm.ProcessId;
                        ProdUid.CurrenctGodownId         = svm.GodownId;
                        ProdUid.Status                   = ProductUidStatusConstants.Generated;
                        ProdUid.LastTransactionDocNo     = ProdUidHeader.GenDocNo;
                        ProdUid.LastTransactionDocTypeId = ProdUidHeader.GenDocTypeId;
                        ProdUid.LastTransactionDocDate   = ProdUidHeader.GenDocDate;
                        ProdUid.LastTransactionPersonId  = ProdUidHeader.GenPersonId;
                        //ProdUid.LastTransactionLineId = item.JobOrderLineId;
                        //ProdUid.ProductUIDId = count;
                        ProdUid.ObjectState = Model.ObjectState.Added;
                        //new ProductUidService(_unitOfWork).Create(ProdUid);
                        context.ProductUid.Add(ProdUid);
                    }


                    try
                    {
                        context.SaveChanges();
                    }

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

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

                    ReAssignSessionVariables(ProdUidHeader.ProductUidHeaderId);
                    //return RedirectToAction("Edit", new { id = ProdUidHeader.ProductUidHeaderId }).Success("Data saved Successfully");
                    return(RedirectToAction("Create").Success("Data saved Successfully"));
                }
            }
            PrepareViewBag(svm);
            return(View("Create", svm));
        }
 private void PrepareViewBag(ProductUidHeaderIndexViewModel s)
 {
 }