public HttpResponseBase UpdateCbjobstaid()
        {
            int result = 0;
            int resulttwo = 0;
            string json = string.Empty;
            CbjobDetailQuery cbjobQuery = new CbjobDetailQuery();
            if (!string.IsNullOrEmpty(Request.Params["jobnumber"].Trim()))
            {
                cbjobQuery.cb_jobid = Request.Params["jobnumber"].Trim();//獲取工作編號
            }
            DateTime time = DateTime.MinValue;
            if (DateTime.TryParse(Request.Params["starttime"].ToString(), out time))
            {
                cbjobQuery.StartDate = time;
            }
            if (DateTime.TryParse(Request.Params["endtime"].ToString(), out time))
            {
                cbjobQuery.EndDate = time.AddDays(1);
            }
            cbjobQuery.create_datetime = DateTime.Now;       //修改時間
            cbjobQuery.create_user = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_id;//修改用戶
            try
            {
                _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);
                _istockMgr = new IstockChangeMgr(mySqlConnectionString);
                resulttwo = _istockMgr.insertistocksome(cbjobQuery);
                result = _cbjobMgr.UpdateCbjobstaid(cbjobQuery);

                if (result > 0)
                {
                    if (resulttwo > 0)
                    {
                        json = "{success:true,msg:'" + result + "'}";//返回json數據
                    }
                    else
                    {
                        json = "{success:false,msg:'" + result + "'}";//返回json數據
                    }
                }
                else if (result == -1)
                {
                    json = "{success:true,msg:-1}";//返回json數據
                }
                else if (result == -2)
                {
                    json = "{success:true,msg:-2}";//返回json數據
                }
                else if (result == -3)
                {
                    json = "{success:true,msg:-3}";//返回json數據
                }

            }
            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,totalCount:0,data:[]}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        public HttpResponseBase FupanComplete()
        {
            int result = 0;
            string json = string.Empty;
            CbjobDetailQuery cbjobQuery = new CbjobDetailQuery();
            if (!string.IsNullOrEmpty(Request.Params["jobnumber"].Trim()))
            {
                cbjobQuery.cb_jobid = Request.Params["jobnumber"].Trim();//獲取工作編號
            }
            cbjobQuery.create_datetime = DateTime.Now;       //創建時間
            cbjobQuery.create_user = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_id;
            try
            {
                _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);
                result = _cbjobMgr.FupanComplete(cbjobQuery);
                if (result == -1)
                {
                    json = "{success:true,msg:-1}";//返回json數據
                }
                else if (result == -2)
                {
                    json = "{success:true,msg:-2}";//返回json數據
                }
                else if (result == -3)
                {
                    json = "{success:true,msg:-3}";//返回json數據
                }
                else if (result > 0)
                {

                    json = "{success:true,msg:1}";//返回json數據
                }
            }
            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,totalCount:0,data:[]}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        public HttpResponseBase DeleteCbjobmessage()
        {
            int result = 0;
            string json = string.Empty;
            CbjobDetailQuery cbjobQuery = new CbjobDetailQuery();
            if (!string.IsNullOrEmpty(Request.Params["searchcontent"].Trim()))
            {
                cbjobQuery.cb_jobid = Request.Params["searchcontent"].Trim();
            }
            try
            {
                _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);
                result = _cbjobMgr.DeleteCbjobmessage(cbjobQuery);
                if (result == -2)
                {
                    json = "{success:true,msg:-2}";
                }
                else if (result == -1)
                {
                    json = "{success:true,msg:-1}";
                }
                else if (result > 0)
                {

                    json = "{success:true,msg:1}";//返回json數據

                }
            }
            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,totalCount:0,data:[]}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        public HttpResponseBase GetMessage()
        {
            string json = string.Empty;

            CbjobDetailQuery cbjQuery = new CbjobDetailQuery();
            cbjQuery.Start = Convert.ToInt32(Request.Params["start"] ?? "0");//用於分頁的變量
            cbjQuery.Limit = Convert.ToInt32(Request.Params["limit"] ?? "25");//用於分頁的變量
            if (!string.IsNullOrEmpty(Request.Params["searchcontent"].Trim()))
            {
                cbjQuery.searchcontent = Request.Params["searchcontent"];
            }
            if (!string.IsNullOrEmpty(Request.Params["newid"].Trim()))
            {
                cbjQuery.cb_newid = Convert.ToInt32(Request.Params["newid"].Trim());
            }
            try
            {
                List<CbjobDetailQuery> store = new List<CbjobDetailQuery>();
                _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);
                int totalCount = 0;
                store = _cbjobMgr.GetMessage(cbjQuery, out totalCount);
                IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
                //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式     
                timeConverter.DateTimeFormat = "yyyy-MM-dd";
                json = "{success:true,'msg':'user',totalCount:" + totalCount + ",data:" + JsonConvert.SerializeObject(store, Formatting.Indented, timeConverter) + "}";//返回json數據
            }
            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,totalCount:0,data:[]}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
        }
        /// <summary>
        /// 匯出盤點工作PDF
        /// </summary>
        public void CountBookPDF()
        {
            PdfHelper pdf = new PdfHelper();
            List<string> pdfList = new List<string>();
            float[] arrColWidth = new float[] { 25, 60, 40, 40, 55,40, 140, 50, 40, 50};
            string newFileName = string.Empty;
            string newName = string.Empty;
            string json = string.Empty;
            BaseFont bf = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bf, 8, iTextSharp.text.Font.UNDERLINE, iTextSharp.text.BaseColor.RED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 12, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0));//黑  
            string filename = "盤點工作" + DateTime.Now.ToString("yyyyMMddHHmmss");
            Document document = new Document(PageSize.A4.Rotate());
            string newPDFName = Server.MapPath(excelPath) + filename;
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(newPDFName, FileMode.Create));
            document.Open();
            int index = 0;
            CbjobMasterQuery query = new CbjobMasterQuery();
            List<CbjobMasterQuery> cbjobList = new List<CbjobMasterQuery>();
            query.IsPage = false;

            if (!string.IsNullOrEmpty(Request.Params["rowIDs"]))
            {
                query.row_id_IN = Request.Params["rowIDs"].TrimEnd(',');

            }
            _CbjobMasterMgr = new CbjobMasterMgr(mySqlConnectionString);
            int totalCount = 0;
            cbjobList = _CbjobMasterMgr.GetjobMaster(query, out  totalCount);
            DataTable _dtBody = new DataTable();
            _dtBody.Columns.Add("編號", typeof(string));
            _dtBody.Columns.Add("條碼", typeof(string));
            _dtBody.Columns.Add("料位", typeof(string));
            _dtBody.Columns.Add("現有庫存", typeof(string));
            _dtBody.Columns.Add("盤點數量", typeof(string));
            _dtBody.Columns.Add("效期控制", typeof(string));
            _dtBody.Columns.Add("品名", typeof(string));
            _dtBody.Columns.Add("規格", typeof(string));
            _dtBody.Columns.Add("細項編號", typeof(string));
            _dtBody.Columns.Add("備註", typeof(string));
            PdfPTable ptablefoot = new PdfPTable(10);

                if (cbjobList.Count > 0)
                {
                    _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);


                    for (int i = 0; i < cbjobList.Count; i++)
                    {
                        CbjobDetail cbdetail = new CbjobDetail();
                        cbdetail.cb_jobid = cbjobList[i].cbjob_id;
                        DataTable _dtdetail = _cbjobMgr.GetDetailTable(cbdetail);
                        _dtBody.Rows.Clear();


                        #region 標頭
                        #region 標頭
                        PdfPTable ptable = new PdfPTable(10);
                        ptable.WidthPercentage = 100;//表格寬度
                        ptable.SetTotalWidth(arrColWidth);
                        PdfPCell cell = new PdfPCell();
                        cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.Colspan = 10;
                        cell.DisableBorderSide(1);
                        cell.DisableBorderSide(4);
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.Colspan = 4;
                        cell.DisableBorderSide(1);
                        cell.DisableBorderSide(2);
                        // cell.DisableBorderSide(4);
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("      盤點薄" + "-" + cbdetail.cb_jobid, new iTextSharp.text.Font(bf, 18)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;
                        cell.Colspan = 3;
                        cell.DisableBorderSide(1);
                        cell.DisableBorderSide(2);
                        cell.DisableBorderSide(4);
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.Colspan = 3;
                        cell.DisableBorderSide(1);
                        cell.DisableBorderSide(2);
                        cell.DisableBorderSide(4);
                        // cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.Colspan = 10;
                        cell.DisableBorderSide(1);
                        cell.DisableBorderSide(2);
                        // cell.DisableBorderSide(4);
                        //cell.DisableBorderSide(8);
                        ptable.AddCell(cell);


                        #endregion
                        #region 表頭
                        cell = new PdfPCell(new Phrase("編號", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("條碼", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);



                        cell = new PdfPCell(new Phrase("料位", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("現有庫存", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("盤點數量", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);
                        cell = new PdfPCell(new Phrase("效期控制", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);
                        cell = new PdfPCell(new Phrase("品名", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);

                        cell = new PdfPCell(new Phrase("規格", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);
                        cell = new PdfPCell(new Phrase("細項編號", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        cell.DisableBorderSide(8);
                        ptable.AddCell(cell);
                        cell = new PdfPCell(new Phrase("備註", new iTextSharp.text.Font(bf, 12)));
                        cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                        ptable.AddCell(cell);
                        #endregion
                        #endregion

                        if (_dtdetail.Rows.Count > 0)
                        {
                            _IiupcMgr = new IupcMgr(mySqlConnectionString);
                            int a = 0;
                            foreach (DataRow rows in _dtdetail.Rows)
                            {
                                a++;
                                string upc_id = string.Empty;
                                #region 取條碼

                                List<IupcQuery> list = new List<IupcQuery>();
                                IupcQuery iupc_query = new IupcQuery();
                                if (!string.IsNullOrEmpty(rows["item_id"].ToString()))
                                {
                                    uint item_id = uint.Parse(rows["item_id"].ToString());
                                    iupc_query.item_id = item_id;
                                    iupc_query.upc_type_flg = "1";
                                    list = _IiupcMgr.GetIupcByType(iupc_query);
                                    if (list.Count > 0)
                                    {
                                        upc_id = list[0].upc_id;
                                    }
                                    else
                                    {
                                        iupc_query.upc_type_flg = "3";
                                        list = _IiupcMgr.GetIupcByType(iupc_query);
                                        if (list.Count > 0)
                                        {
                                            upc_id = list[0].upc_id;
                                        }
                                        else
                                        {
                                            iupc_query.upc_type_flg = "2";
                                            list = _IiupcMgr.GetIupcByType(iupc_query);
                                            if (list.Count > 0)
                                            {
                                                upc_id = list[0].upc_id;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    upc_id = " ";
                                }
                                #endregion
                                DataRow row = _dtBody.NewRow();
                                row["編號"] = a;
                                row["條碼"] = upc_id;

                                row["料位"] = rows["loc_id"];
                                row["現有庫存"] = rows["prod_qty"];
                                row["盤點數量"] = "";
                                row["效期控制"] = string.IsNullOrEmpty(rows["pwy_dte_ctl"].ToString()) ? "否" : (rows["pwy_dte_ctl"].ToString() == "Y" ? "是" : "否");
                                row["品名"] = rows["product_name"];
                                row["規格"] = rows["spec_title_1"];
                                row["細項編號"] = rows["item_id"];
                                row["備註"] = " ";
                                _dtBody.Rows.Add(row);
                            }
                        }
                        newFileName = newPDFName + "_part" + index++ + "." + "pdf";
                        pdf.ExportDataTableToPDF(_dtBody, false, newFileName, arrColWidth, ptable, ptablefoot, "", "", 10, uint.Parse(_dtBody.Rows.Count.ToString()));/*第一7是列,第二個是行*/
                        pdfList.Add(newFileName);
                    }



                }
            if (_dtBody.Rows.Count == 0)
            {
                #region 標頭
                #region 標頭
                PdfPTable ptable = new PdfPTable(10);
                ptable.WidthPercentage = 100;//表格寬度
                ptable.SetTotalWidth(arrColWidth);
                PdfPCell cell = new PdfPCell();
                cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.Colspan = 10;
                cell.DisableBorderSide(1);
                cell.DisableBorderSide(4);
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.Colspan = 4;
                cell.DisableBorderSide(1);
                cell.DisableBorderSide(2);
                // cell.DisableBorderSide(4);
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("      盤點薄", new iTextSharp.text.Font(bf, 18)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;
                cell.Colspan = 3;
                cell.DisableBorderSide(1);
                cell.DisableBorderSide(2);
                cell.DisableBorderSide(4);
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.Colspan = 3;
                cell.DisableBorderSide(1);
                cell.DisableBorderSide(2);
                cell.DisableBorderSide(4);
                // cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.Colspan = 10;
                cell.DisableBorderSide(1);
                cell.DisableBorderSide(2);
                // cell.DisableBorderSide(4);
                //cell.DisableBorderSide(8);
                ptable.AddCell(cell);


                #endregion
                #region 表頭
                cell = new PdfPCell(new Phrase("編號", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("條碼", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);



                cell = new PdfPCell(new Phrase("料位", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("現有庫存", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("盤點數量", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase("效期控制", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase("品名", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("規格", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase("細項編號", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase("備註", new iTextSharp.text.Font(bf, 12)));
                cell.VerticalAlignment = Element.ALIGN_LEFT;//字體水平居左
                ptable.AddCell(cell);
                #endregion
                #endregion
                document = new Document(PageSize.A4.Rotate());
                if (!document.IsOpen())
                {
                    document.Open();
                }
                cell = new PdfPCell(new Phrase(" ", font));
                cell.Colspan = 4;
                cell.VerticalAlignment = Element.ALIGN_CENTER;//字體水平居左
                cell.DisableBorderSide(8);
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase("此工作代號無數據!", font));
                cell.Colspan = 3;
                cell.DisableBorderSide(4);
                cell.VerticalAlignment = Element.ALIGN_CENTER;//字體水平居左
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase(" ", font));
                cell.Colspan = 3;
                cell.VerticalAlignment = Element.ALIGN_CENTER;//字體水平居左
                //cell.DisableBorderSide(8);
                ptable.AddCell(cell);


                // document.Add(ptable);
                //document.Add(ptablefoot); 
                newFileName = newPDFName + "_part" + index++ + "." + "pdf";
                pdf.ExportDataTableToPDF(_dtBody, false, newFileName, arrColWidth, ptable, ptablefoot, "", "", 10, uint.Parse(_dtBody.Rows.Count.ToString()));/*第一7是列,第二個是行*/
                pdfList.Add(newFileName);

            }
            newFileName = newPDFName + "." + "pdf";
            pdf.MergePDF(pdfList, newFileName);

            Response.Clear();
            Response.Charset = "gb2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attach-ment;filename=" + filename + ".pdf");
            Response.WriteFile(newFileName);
        }
        /// <summary>
        /// 生成盤點工作
        /// </summary>
        /// <returns></returns>
        public HttpResponseBase GetCountBook2()
        {
            // DataSet.Tables["XX"].Columns["xx"].ColumnName = "NewColumnName";
            string json = string.Empty;
            IinvdQuery m = new IinvdQuery();
            CbjobMaster cm = new CbjobMaster();
            CbjobDetail cd = new CbjobDetail();
            IinvdMgr iinvdMgr = new IinvdMgr(mySqlConnectionString);
            _IiupcMgr = new IupcMgr(mySqlConnectionString);
            string cbjob_id = "PC";// +DateTime.Now.ToString("yyyyMMddHHmmss");

            try
            {
                #region 條件
                if (!string.IsNullOrEmpty(Request.Params["startIloc"].Trim()))
                {//料位開始
                    m.startIloc = Request.Params["startIloc"].Trim().ToUpper();
                }
                int length = m.startIloc.Length;
                switch (length)
                {
                    case 0:
                        cbjob_id = cbjob_id + "AA";
                        break;
                    case 1:
                        cbjob_id = cbjob_id+ m.startIloc+"A";
                        break;
                    default:
                        cbjob_id = cbjob_id + m.startIloc.Substring(0, 2);
                        break;
                }
                if (!string.IsNullOrEmpty(Request.Params["endIloc"].Trim()))
                {
                    m.endIloc = Request.Params["endIloc"].Trim() + "Z";
                    m.endIloc = m.endIloc.ToUpper();
                }
                length = m.endIloc.Length;
                switch (length)
                {
                    case 0:
                        cbjob_id = cbjob_id + "ZZ";
                        break;
                    case 1:
                        cbjob_id = cbjob_id +m.endIloc +"Z";
                        break;
                    default:
                        cbjob_id = cbjob_id + m.endIloc.Substring(0, 2);
                        break;
                }
               
                if (!string.IsNullOrEmpty(Request.Params["level"]))//層數
                {//層數選擇
                    m.lot_no = Request.Params["level"].ToString().ToUpper();
                }
                if (!string.IsNullOrEmpty(Request.Params["sort"]))//排序
                {//排序方式
                    m.Sort = Request.Params["sort"];
                    if (m.Sort == "0" && !string.IsNullOrEmpty(Request.Params["firstsd"]))
                    {
                        m.Firstsd = Request.Params["firstsd"];
                    }
                }
                if (!string.IsNullOrEmpty(m.Firstsd))
                {
                    if (m.Firstsd == "0")
                    {
                        cbjob_id += "S";//單
                    }
                    else
                    {
                        cbjob_id += "M";//雙
                    }
                }
                else 
                {
                    cbjob_id += "Z";//不分
                } 
                cbjob_id += DateTime.Now.ToString("yyyyMMddHHmmss");
                if (!string.IsNullOrEmpty(Request.Params["vender"]))
                {//vender
                    m.vender = Request.Params["vender"].ToString().ToUpper();
                }
                if (!string.IsNullOrEmpty(Request.Params["prepaid"]))
                {
                    m.prepaid = int.Parse(Request.Params["prepaid"]);
                }
                #endregion
                DataTable dt = iinvdMgr.getVentory(m);


                if (dt.Rows.Count > 0)
                {
                    #region 往cbjob_master裡面插入一條數據
                    StringBuilder sb = new StringBuilder();
                    _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);
                    _cbMasterMgr = new CbjobMasterMgr(mySqlConnectionString);
                    cm.cbjob_id = cbjob_id;
                    cm.create_datetime = DateTime.Now;
                    cm.sta_id = "CNT";
                    cm.create_user = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_id;
                    sb.Append(_cbMasterMgr.Insertsql(cm));
                    #endregion

                    #region 修改iinvd數據,往cbjob_detail循環插入數據
                    int a = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(dt.Rows[i]["row_id"].ToString()))
                        {
                            cd.cb_jobid = cm.cbjob_id;
                            cd.cb_newid = a + 1;
                            cd.chang_user = cm.create_user;
                            cd.change_datetime = DateTime.Now;
                            cd.create_datetime = DateTime.Now;
                            cd.create_user = cm.create_user;
                            cd.status = 1;
                            cd.iinvd_id = int.Parse(dt.Rows[i]["row_id"].ToString());
                            sb.AppendFormat("set sql_safe_updates = 0; UPDATE iinvd set st_qty=prod_qty where row_id='{0}'; set sql_safe_updates = 1;", cd.iinvd_id);
                            sb.Append(_cbjobMgr.insertsql(cd));
                            a++;
                        }
                    }
                    #endregion
                
                    _cbjobMgr.InsertSql(sb.ToString());
                    json = "{success:true,msg:" + 1 + "}";
                }
                else
                {
                    json = "{success:true,msg:" + 2 + "}";
                    #region 應用
		 
	
                    //Response.Clear();
                    //this.Response.Write("沒有數據<br/>");
                    #endregion

                }
               
            }
            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,msg:" + 0 + "}";
            }
            this.Response.Clear();
            this.Response.Write(json);
            this.Response.End();
            return this.Response;
         
        }
        public HttpResponseBase GetCountBook()
        {
            // DataSet.Tables["XX"].Columns["xx"].ColumnName = "NewColumnName";
            string json = string.Empty;
            IinvdQuery m = new IinvdQuery();
            CbjobMaster cm = new CbjobMaster();
            CbjobDetail cd = new CbjobDetail();
            IinvdMgr iinvdMgr = new IinvdMgr(mySqlConnectionString);
            _IiupcMgr = new IupcMgr(mySqlConnectionString);
            string cbjob_id = "PC" + DateTime.Now.ToString("yyyyMMddHHmmss");
            try
            {
                #region 條件
                if (!string.IsNullOrEmpty(Request.Params["startIloc"]))
                {//料位開始
                    m.startIloc = Request.Params["startIloc"].ToUpper();
                }
                if (!string.IsNullOrEmpty(Request.Params["endIloc"]))
                {
                    m.endIloc = Request.Params["endIloc"] + "Z";
                    m.endIloc = m.endIloc.ToUpper();
                }
                if (!string.IsNullOrEmpty(Request.Params["level"]))//層數
                {//層數選擇
                    m.lot_no = Request.Params["level"].ToString().ToUpper();
                }
                if (!string.IsNullOrEmpty(Request.Params["sort"]))//排序
                {//排序方式
                    m.Sort = Request.Params["sort"];
                    if (m.Sort == "0" && !string.IsNullOrEmpty(Request.Params["firstsd"]))
                    {
                        m.Firstsd = Request.Params["firstsd"];
                    }
                }
                if (!string.IsNullOrEmpty(Request.Params["vender"]))
                {//vender
                    m.vender = Request.Params["vender"].ToString().ToUpper();
                }
                if (!string.IsNullOrEmpty(Request.Params["prepaid"]))
                {
                    m.prepaid = int.Parse(Request.Params["prepaid"]);
                }
                #endregion
                DataTable dt = iinvdMgr.getproduct(m);
                #region 列名
                DataTable dtCountBook = new DataTable();
                dtCountBook.Columns.Add("編號", typeof(String));
                dtCountBook.Columns.Add("料位", typeof(String));
                dtCountBook.Columns.Add("狀態", typeof(String));
                dtCountBook.Columns.Add("商品細項編號", typeof(String));
                dtCountBook.Columns.Add("庫存", typeof(String));
                dtCountBook.Columns.Add("成本", typeof(String));
                dtCountBook.Columns.Add("商品名稱", typeof(String));
                dtCountBook.Columns.Add("商品規格", typeof(String));
                dtCountBook.Columns.Add("是否買斷", typeof(String));
                dtCountBook.Columns.Add("製造日期", typeof(String));
                dtCountBook.Columns.Add("保存期限", typeof(String));
                dtCountBook.Columns.Add("有效日期", typeof(String));
                dtCountBook.Columns.Add("是否有效期控制", typeof(String));
                dtCountBook.Columns.Add("是否即期", typeof(String));
                dtCountBook.Columns.Add("是否過期", typeof(String));
                dtCountBook.Columns.Add("允出天數", typeof(String));
                dtCountBook.Columns.Add("允收天數", typeof(String));
                dtCountBook.Columns.Add("國際條碼", typeof(String));
                dtCountBook.Columns.Add("最新店內碼", typeof(String));
                #endregion

                if (dt.Rows.Count > 0)
                {
                    #region 往cbjob_master裡面插入一條數據
                    StringBuilder sb = new StringBuilder();
                    _cbjobMgr = new CbjobDetailMgr(mySqlConnectionString);
                    _cbMasterMgr = new CbjobMasterMgr(mySqlConnectionString);
                    cm.cbjob_id = cbjob_id;
                    cm.create_datetime = DateTime.Now;
                    cm.sta_id = "CNT";
                    cm.create_user = (System.Web.HttpContext.Current.Session["caller"] as Caller).user_id;
                    sb.Append(_cbMasterMgr.Insertsql(cm));
                    #endregion
                    #region 修改iinvd數據,往cbjob_detail循環插入數據
                    int a = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(dt.Rows[i]["row_id"].ToString()))
                        {
                            cd.cb_jobid = cm.cbjob_id;
                            cd.cb_newid = a + 1;
                            cd.chang_user = cm.create_user;
                            cd.change_datetime = DateTime.Now;
                            cd.create_datetime = DateTime.Now;
                            cd.create_user = cm.create_user;
                            cd.status = 1;
                            cd.iinvd_id = int.Parse(dt.Rows[i]["row_id"].ToString());
                            sb.AppendFormat("set sql_safe_updates = 0; UPDATE iinvd set st_qty=prod_qty where row_id='{0}'; set sql_safe_updates = 1;", cd.iinvd_id);
                            sb.Append(_cbjobMgr.insertsql(cd));
                            a++;
                        }
                    }
                    _cbjobMgr.InsertSql(sb.ToString());
                    #endregion
                    m.cb_jobid = cm.cbjob_id;//賦值給m
                    //dt = iinvdMgr.CountBook(m);
                    int bh = 1;
                    string loc = "";
                    foreach (DataRow item2 in dt.Rows)
                    {

                        if (item2["loc_id"].ToString() != loc)
                        {
                            if (!string.IsNullOrEmpty(item2["row_id"].ToString()))
                            {
                                #region 讀取iinvd裡面的數據
                                m.loc_id = item2["loc_id"].ToString();
                                DataTable Invdt = iinvdMgr.GetIinvdCountBook(m);
                                foreach (DataRow item in Invdt.Rows)
                                {
                                    DataRow dr = dtCountBook.NewRow();
                                    dr[0] = bh;
                                    dr[1] = item["loc_id"];
                                    dr[2] = item["lsta_id"];
                                    if (!string.IsNullOrEmpty(item["item_id"].ToString()))
                                    {
                                        dr[3] = item["item_id"];
                                    }
                                    if (!string.IsNullOrEmpty(item["prod_qty"].ToString()))
                                    {
                                        dr[4] = item["prod_qty"];
                                    }
                                    else
                                    {
                                        dr[4] = "0";
                                    }
                                    if (!string.IsNullOrEmpty(item["product_id"].ToString()))
                                    {
                                        dr[5] = iinvdMgr.Getcost(item["product_id"].ToString());
                                    }
                                    dr[6] = item["product_name"];
                                    dr[7] = GetProductSpec(item["item_id"].ToString());
                                    dr[8] = item["prepaid"].ToString() == "0" ? "否" : "是";
                                    DateTime md;
                                    if (DateTime.TryParse(item["made_date"].ToString(), out md))
                                    {
                                        dr[9] = DateTime.Parse(item["made_date"].ToString()).ToString("yyyy/MM/dd");
                                    }
                                    dr[10] = item["cde_dt_incr"];
                                    DateTime cdate;
                                    if (DateTime.TryParse(item["cde_dt"].ToString(), out cdate))
                                    {
                                        dr[11] = cdate.ToString("yyyy/MM/dd");
                                    }
                                    int shp = 0;
                                    if (item["pwy_dte_ctl"].ToString() == "Y" && Int32.TryParse(item["cde_dt_shp"].ToString(), out shp))//表示是有效期控管的商品
                                    {
                                        dr[12] = "Y";
                                        if (cdate.AddDays(-shp) <= DateTime.Now && cdate >= DateTime.Now)
                                        {
                                            dr[13] = "Y";
                                        }
                                        else
                                        {
                                            dr[13] = "N";
                                        }
                                        if (cdate < DateTime.Now)
                                        {
                                            dr[14] = "Y";
                                        }
                                        else
                                        {
                                            dr[14] = "N";
                                        }
                                        dr[15] = item["cde_dt_shp"];//允出天數
                                        dr[16] = item["cde_dt_var"];
                                    }
                                    else if (item["pwy_dte_ctl"].ToString() == "N")
                                    {
                                        dr[12] = "N";
                                        dr[13] = "";
                                        dr[14] = "";
                                        dr[15] = 0;//允出天數
                                        dr[16] = 0;
                                    }
                                    else
                                    {
                                        dr[12] = "";
                                        dr[13] = "";
                                        dr[14] = "";
                                        dr[15] = 0;//允出天數
                                        dr[16] = 0;
                                    }
                                    dr[17] = " " + _IiupcMgr.Getupc(item["item_id"].ToString(), "1");
                                    dr[18] = " " + _IiupcMgr.Getupc(item["item_id"].ToString(), "2");
                                    dtCountBook.Rows.Add(dr);
                                    bh++;
                                }
                                #endregion
                            }
                            else
                            {
                                #region iinvd沒有的料位信息從iplas表查出
                                DataRow dr = dtCountBook.NewRow();
                                m.loc_id = item2["loc_id"].ToString();
                                DataTable dt1 = iinvdMgr.GetIplasCountBook(m);
                                foreach (DataRow item1 in dt1.Rows)
                                {
                                    dr[0] = bh;
                                    dr[1] = item2["loc_id"];
                                    dr[2] = item1["lsta_id"];
                                    if (!string.IsNullOrEmpty(item1["item_id"].ToString()))
                                    {
                                        dr[3] = item1["item_id"];
                                    }
                                    if (!string.IsNullOrEmpty(item1["prod_qty"].ToString()))
                                    {
                                        dr[4] = item1["prod_qty"];
                                    }
                                    else
                                    {
                                        dr[4] = "0";
                                    }
                                    if (!string.IsNullOrEmpty(item1["product_id"].ToString()))
                                    {
                                        dr[5] = iinvdMgr.Getcost(item1["product_id"].ToString());
                                    }
                                    else
                                    {
                                        dr[5] = "0";
                                    }
                                    dr[6] = item1["product_name"];
                                    dr[7] = GetProductSpec(item1["item_id"].ToString());
                                    dr[8] = item1["prepaid"].ToString() == "0" ? "否" : "是";
                                    dr[9] = "";
                                    dr[10] = item1["cde_dt_incr"];
                                    dr[11] = "";
                                    if (item1["pwy_dte_ctl"].ToString() == "Y")//表示是有效期控管的商品
                                    {
                                        dr[12] = "Y";
                                        dr[13] = "N";
                                        dr[14] = "N";
                                        dr[15] = item1["cde_dt_shp"];//允出天數
                                        dr[16] = item1["cde_dt_var"];
                                    }
                                    else if (item1["pwy_dte_ctl"].ToString() == "N")
                                    {
                                        dr[12] = "N";
                                        dr[13] = "";
                                        dr[14] = "";
                                        dr[15] = 0;//允出天數
                                        dr[16] = 0;
                                    }
                                    else
                                    {
                                        dr[12] = "";
                                        dr[13] = "";
                                        dr[14] = "";
                                        dr[15] = 0;//允出天數
                                        dr[16] = 0;
                                    }
                                    dr[17] = " " + _IiupcMgr.Getupc(item1["item_id"].ToString(), "1");
                                    dr[18] = " " + _IiupcMgr.Getupc(item1["item_id"].ToString(), "2");
                                    dtCountBook.Rows.Add(dr);
                                    bh++;
                                }
                                #endregion
                            }
                            loc = item2["loc_id"].ToString();
                        }
                    }
                    string fileName = "盤點簿" + cm.cbjob_id.Substring(2, 14) + ".xls";
                    String str = "盤點簿報表-" + cm.cbjob_id;
                    MemoryStream ms = ExcelHelperXhf.ExportDT(dtCountBook, str);
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.BinaryWrite(ms.ToArray());
                }
                else
                {
                    Response.Clear();
                    this.Response.Write("沒有數據<br/>");
                }
            }
            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);
            }
            return this.Response;
        }