public HttpResponseBase SaveIinvd()
        {
            string json = "{success:false,message:'系統異常'}";
            try
            {
                int temp = 0;
                if (int.TryParse(Request.Params["prod_qty"], out temp))
                {
                    if (temp > 0)
                    {
                        IinvdQuery iinvd = new IinvdQuery();
                        if (!string.IsNullOrEmpty(Request.Params["loc_id"]))
                        {
                            iinvd.plas_loc_id = Request.Params["loc_id"];
                        }
                        if (!string.IsNullOrEmpty(Request.Params["item_id"]))
                        {
                            if (int.TryParse(Request.Params["item_id"], out temp))
                            {
                                iinvd.item_id = uint.Parse(Request.Params["item_id"]);
                            }
                        }

                        #region 判斷是否指定主料位
                        IlocQuery ilocquery = new IlocQuery();
                        _IiplasMgr = new IplasMgr(mySqlConnectionString);
                        IplasQuery iplasquery = new IplasQuery();
                        iplasquery.item_id = iinvd.item_id;
                        IIlocImplMgr ilocMgr = new IlocMgr(mySqlConnectionString);
                        IplasDao iplasdao = new IplasDao(mySqlConnectionString);
                        int total = 0;
                        ilocquery.loc_id = iinvd.plas_loc_id;
                        ilocquery.lcat_id = "0";
                        ilocquery.lsta_id = "";
                        ilocquery.IsPage = false;
                        List<IlocQuery> listiloc = ilocMgr.GetIocList(ilocquery, out total);
                        if (listiloc.Count > 0)
                        {
                            string lcat_id = listiloc.Count == 0 ? "" : listiloc[0].lcat_id;
                            if (lcat_id == "S")
                            {
                                string item_id = iplasdao.Getlocid(ilocquery.loc_id);
                                if (item_id == "")
                                {
                                    Iplas iplas = new Iplas();
                                    if (int.TryParse(Request.Params["item_id"], out temp))
                                    {
                                        iplas.item_id = uint.Parse(Request.Params["item_id"]);
                                        if (_IiplasMgr.IsTrue(iplas) == "false")
                                        {
                                            json = "{success:false,message:'不存在該商品編號'}";
                                            this.Response.Clear();
                                            this.Response.Write(json);
                                            this.Response.End();
                                            return this.Response;
                                        }
                                        if (_IiplasMgr.GetIplasid(iplasquery) > 0)
                                        {
                                            json = "{success:false,message:'此商品主料位非該料位'}";
                                            this.Response.Clear();
                                            this.Response.Write(json);
                                            this.Response.End();
                                            return this.Response;
                                        }
                                        Iloc iloc = new Iloc();
                                        iloc.loc_id = iinvd.plas_loc_id;
                                        if (_IiplasMgr.GetLocCount(iloc) <= 0)
                                        {
                                            json = "{success:false,message:'該料位已鎖定或被指派'}";
                                            this.Response.Clear();
                                            this.Response.Write(json);
                                            this.Response.End();
                                            return this.Response;
                                        }
                                        iplas.loc_id = iloc.loc_id;
                                        iplas.loc_stor_cse_cap = 100;
                                        iplas.create_user = (Session["caller"] as Caller).user_id;
                                        iplas.create_dtim = DateTime.Now;
                                        iplas.change_user = (Session["caller"] as Caller).user_id;
                                        iplas.change_dtim = DateTime.Now;
                                        _IiplasMgr.InsertIplas(iplas);
                                    }
                                }
                            }
                        }
                        #endregion
                        iinvd.create_user = (Session["caller"] as Caller).user_id;
                        iinvd.create_dtim = DateTime.Now;
                        iinvd.change_user = iinvd.create_user;
                        iinvd.change_dtim = iinvd.create_dtim;
                        iinvd.ista_id = "A";
                        if (!string.IsNullOrEmpty(Request.Params["loc_id"]))
                        {
                            iinvd.plas_loc_id = Request.Params["loc_id"];
                        }
                        int change_prod_qty = int.Parse(Request.Params["prod_qty"]);
                        iinvd.prod_qty = change_prod_qty;
                        if (!string.IsNullOrEmpty(Request.Params["st_qty"]))
                        {
                            if (int.TryParse(Request.Params["st_qty"], out temp))
                            {
                                iinvd.st_qty = int.Parse(Request.Params["st_qty"]);
                            }
                        }
                        if (!string.IsNullOrEmpty(Request.Params["item_id"]))
                        {
                            if (int.TryParse(Request.Params["item_id"], out temp))
                            {
                                iinvd.item_id = uint.Parse(Request.Params["item_id"]);
                            }
                        }
                        DateTime date = DateTime.Now;
                        if (DateTime.TryParse(Request.Params["datetimepicker1"], out date))
                        {
                            iinvd.made_date = date;
                        }
                        else
                        {
                            iinvd.made_date = DateTime.Now;
                        }
                        _iinvd = new IinvdMgr(mySqlConnectionString);
                        if (Request.Params["pwy_dte_ctl"] == "Y")
                        {
                            iinvd.pwy_dte_ctl = "Y";
                            IProductExtImplMgr productExt = new ProductExtMgr(mySqlConnectionString);
                            int Cde_dt_incr = productExt.GetCde_dt_incr((int)iinvd.item_id);
                            iinvd.cde_dt = date.AddDays(Cde_dt_incr);
                        }
                        else
                        {
                            iinvd.cde_dt = DateTime.Now;
                        }
                        iinvd.prod_qtys = _iinvd.GetProd_qty(Convert.ToInt32(iinvd.item_id), iinvd.plas_loc_id, "", iinvd.row_id.ToString());
                        IialgQuery iialg = new IialgQuery();
                        iialg.cde_dt = iinvd.cde_dt;
                        int prod_qty = 0;// _iinvd.GetProd_qty((int)iinvd.item_id, iinvd.plas_loc_id, "", "");
                        int row = _iinvd.GetIinvdCount(iinvd);
                        if (row > 1)
                        {
                            prod_qty = row - 1;
                            json = "{success:true}";
                        }
                        else
                        {
                            if (_iinvd.Insert(iinvd) == 1)
                            {
                                json = "{success:true}";
                            }
                        }
                        
                        iialg.qty_o = prod_qty;
                        iialg.loc_id = iinvd.plas_loc_id;
                        iialg.item_id = iinvd.item_id;
                        iialg.iarc_id = "循環盤點";
                        iialg.adj_qty = change_prod_qty;
                        iialg.create_dtim = DateTime.Now;
                        iialg.create_user = iinvd.create_user;
                        iialg.type = 2;
                        iialg.doc_no = "C" + DateTime.Now.ToString("yyyyMMddHHmmss");
                        iialg.made_dt = iinvd.made_date;
                        iialg.cde_dt = iinvd.cde_dt;
                        _iialgMgr = new IialgMgr(mySqlConnectionString);
                        _iialgMgr.insertiialg(iialg);

                        IstockChangeQuery istock = new IstockChangeQuery();
                        istock.sc_trans_id = iialg.doc_no;
                        istock.item_id = iinvd.item_id;
                        istock.sc_istock_why = 2;
                        istock.sc_trans_type = 2;
                        istock.sc_num_old = iinvd.prod_qtys;
                        istock.sc_num_chg = iialg.adj_qty;
                        istock.sc_num_new = _iinvd.GetProd_qty((int)iinvd.item_id, iinvd.plas_loc_id,"N","");
                        istock.sc_time = iinvd.create_dtim;
                        istock.sc_user = iinvd.create_user;
                        istock.sc_note = "循環盤點";
                        IstockChangeMgr istockMgr = new IstockChangeMgr(mySqlConnectionString);
                        istockMgr.insert(istock);
                    }
                    else
                    {
                        json = "{success:false,message:'庫存不能小於1'}";
                    }
                }
                else
                {                  
                    json = "{success:false,message:'庫存請輸入數字'}";
                }
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
                json = "{success:false}";

            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        public ActionResult IinvdCheck()
        {
            _IlocMgr = new IlocMgr(mySqlConnectionString);
            StringBuilder strHtml = new StringBuilder();
            string loc_id = Request.Params["loc_id"];
            IlocQuery ilocquery=new IlocQuery();
            ilocquery.loc_id = loc_id;
            loc_id = _IlocMgr.GetIlocCount(ilocquery);
            ViewBag.loc_id = loc_id;
            ViewBag.lcat_id = "";
            ViewBag.upc_id = "";
            ViewBag.iplas = "";
            _iinvd = new IinvdMgr(mySqlConnectionString);
            if (loc_id != "")
            {
                List<IinvdQuery> list = _iinvd.GetIinvdList(loc_id);
                IupcQuery iupc = new IupcQuery();
                _IiupcMgr = new IupcMgr(mySqlConnectionString);
                if (list.Count > 0)
                {
                    ViewBag.product_name = list[0].product_name;
                    ViewBag.spec = list[0].spec;
                    ViewBag.item_id = list[0].item_id.ToString()                                        ;
                    ViewBag.pwy_dte_ctl = list[0].pwy_dte_ctl;
                    iupc.item_id=uint.Parse(ViewBag.item_id);
                    List<IupcQuery> listiupc=new List<IupcQuery>();
                    if (iupc.item_id!=0)
                    {
                        listiupc = _IiupcMgr.GetIupcByItemID(iupc);
                    }
                    if (listiupc.Count > 0)
                    {
                        ViewBag.upc_id = listiupc[0].upc_id;
                    }

                    if (list[0].pwy_dte_ctl == "Y")
                    {
                        ViewBag.count = list.Count;
                    }
                    else
                    {
                        int prod_qty = 0;
                        for (int i = 0; i < list.Count; i++)
                        {
                            prod_qty += list[i].prod_qty;
                        }
                        list[0].prod_qty = prod_qty;
                        ViewBag.count = 1;
                    }
                    ViewBag.data = list;

                    IlocQuery lsta_id = _IlocMgr.GetIlocLsta_id(loc_id);
                    Iloc iloc = new Iloc();
                    if (lsta_id != null)
                    {
                        if (lsta_id.lsta_id == "F")
                        {
                            iloc.row_id = lsta_id.row_id;
                            iloc.lsta_id = "A";
                            iloc.change_dtim = DateTime.Now;
                            iloc.change_user = (Session["caller"] as Caller).user_id;
                            _IlocMgr.UpdateIlocLock(iloc);
                        }
                    } 
                }
                else {
                    ViewBag.product_name ="";
                    ViewBag.spec = "";
                    ViewBag.item_id = "此料位暫無商品";
                    IinvdQuery iinvd = new IinvdQuery();
                    iinvd.prod_qty = 0;
                    list.Add(iinvd);
                    ViewBag.pwy_dte_ctl = Request.Params["pwy_dte_ctl"]; ;
                    ViewBag.count = 0;
                    ViewBag.data = list;

                    IIlocImplMgr ilocMgr = new IlocMgr(mySqlConnectionString);
                    IplasDao iplas = new IplasDao(mySqlConnectionString);
                    int total = 0;
                    ilocquery.loc_id = loc_id;
                    ilocquery.lcat_id = "0";
                    ilocquery.lsta_id = "";
                    ilocquery.IsPage = false;
                    List<IlocQuery> listiloc = ilocMgr.GetIocList(ilocquery, out total);
                    if (listiloc.Count > 0)
                    {
                        string lcat_id =listiloc.Count==0?"": listiloc[0].lcat_id;
                        if (lcat_id == "S")
                        {
                            string item_id = iplas.Getlocid(loc_id);
                            if (item_id != "")
                            {
                                ViewBag.item_id = item_id;
                                iupc.item_id = uint.Parse(ViewBag.item_id);
                                List<IupcQuery> listiupc = _IiupcMgr.GetIupcByItemID(iupc);
                                if (listiupc.Count > 0)
                                {
                                    ViewBag.upc_id = listiupc[0].upc_id;
                                }
                                DataTable table = iplas.GetProduct(item_id);
                                if (table.Rows.Count > 0)
                                {
                                    ViewBag.product_name = table.Rows[0]["product_name"];
                                    ViewBag.spec = table.Rows[0]["spec"];
                                    ViewBag.lcat_id = lcat_id;
                                }
                                DataTable tablePwy = _iinvd.Getprodubybar(item_id);
                                if (tablePwy.Rows.Count > 0)
                                {
                                    ViewBag.pwy_dte_ctl = tablePwy.Rows[0]["pwy_dte_ctl"].ToString();
                                }
                            }
                            else
                            {
                                IlocQuery lsta_id = _IlocMgr.GetIlocLsta_id(loc_id);
                                Iloc iloc = new Iloc();
                                if (lsta_id != null)
                                {
                                    if (lsta_id.lsta_id != "H")
                                    {
                                        iloc.row_id = lsta_id.row_id;
                                        iloc.lsta_id = "F";
                                        iloc.change_dtim = DateTime.Now;
                                        iloc.change_user = (Session["caller"] as Caller).user_id;
                                        _IlocMgr.UpdateIlocLock(iloc);
                                    }
                                } 
                                ViewBag.iplas = "false";
                                ViewBag.pwy_dte_ctl = "N";
                            }
                        }
                        else {
                            IlocQuery lsta_id = _IlocMgr.GetIlocLsta_id(loc_id);
                            Iloc iloc = new Iloc();
                            if(lsta_id!=null)
                            {
                                if(lsta_id.lsta_id!="H")
                                {
                                    iloc.row_id=lsta_id.row_id;
                                    iloc.lsta_id = "F";
                                    iloc.change_dtim = DateTime.Now;
                                    iloc.change_user = (Session["caller"] as Caller).user_id;
                                    _IlocMgr.UpdateIlocLock(iloc);
                                }
                            }
                            ViewBag.pwy_dte_ctl = "N";
                        }
                    }
                }
                if (ViewBag.pwy_dte_ctl==null)
                {
                    ViewBag.pwy_dte_ctl = "";
                }
            }
            return View();
        }