using NFine.Application.WMS;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Code;
using System.Data.SqlClient;
using NFine.Data.Extensions;
using System.Data.OleDb;
using System.Configuration;
using ICS.Application.Entity;
using System.Xml;

namespace NFine.Web.Areas.WMS.Controllers
{
    public class WMSCreateItemLotController : ControllerBase
    {
        // GET: WMS/WMSCreateItemLot

        private WMSCreateItemLotApp App = new WMSCreateItemLotApp();
        public ActionResult CreateItemLot()
        {
            return View();
        }

        public ActionResult From()
        {
            return View();
        }
        public ActionResult FormMoPick()
        {
            return View();
        }
        public ActionResult ImportLot()
        {
            return View();
        }
        

        [HttpGet]
        public ActionResult SelectICSExtensionEnable()
        {
            var data = App.SelectICSExtensionEnable();
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetComplete()
        {
            var data = App.GetComplete();
            return Content(data.ToJson());
        }
        
         [HttpGet]
        public ActionResult GetWWComplete()
        {
            var data = App.GetWWComplete();
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJson(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetICSMOPick(Pagination pagination, string MODetailID,string workpoint)
        {
            DataTable ListData = App.GetICSMOPick(MODetailID, workpoint, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJson(string ApplyNegCode, string Sequence, string Type, Pagination pagination)
        {
            DataTable ListData = App.GetSubGridJson(ApplyNegCode, Sequence, Type, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonByCreate(string ApplyNegCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonByCreate(ApplyNegCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetVendorLotNo(string VenCode, string WorkPoint)
        {
            DataTable ListData = App.GetVendorLotNo(VenCode, WorkPoint);
            var JsonData = new
            {
                VendorLotNo = ListData.Rows[0][0].ToString(),
            };
            return Content(JsonData.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(string ApplyNegCode, string Sequence, string keyValue, string WorkPoint,string AMEnable)
        {
            int i = App.CreateItemLotNo(ApplyNegCode, Sequence, keyValue, WorkPoint, AMEnable);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }


        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteItemLot(string keyValue)
        {
            keyValue = keyValue.Substring(1, keyValue.Length - 2);
            //WorkPoint = WorkPoint.Substring(1, WorkPoint.Length - 2);
            string msg = App.DeleteItemLot(keyValue);
            if (string.IsNullOrWhiteSpace(msg))
            {
                return Success("删除成功!");
            }
            else
            {
                return Error(msg);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormWeiWai(string OApplyNegCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormWeiWai(OApplyNegCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //成品
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormChengPing(string MOCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormChengPing(MOCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }
        //销售退货
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormXiaoShou(string SDNCode, string Sequence, string keyValue, string WorkPoint, string AMEnable)
        {
            int i = App.SubmitFormXiaoShou(SDNCode, Sequence, keyValue, WorkPoint, AMEnable);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }
        //其他
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormQiTa(string InCode, string Sequence, string keyValue, string WorkPoint, string AMEnable)
        {
            int i = App.SubmitFormQiTa(InCode, Sequence, keyValue, WorkPoint, AMEnable);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        /// <summary>
        /// 打印数据参数:服务器的URL+打印的文件名,转化为Base64编码
        /// </summary>
        protected string strPrintData;
        /// <summary>
        /// 标识是否安装了控件
        /// </summary>
        protected bool bIsInstallPrintControl = true;
        /// <summary>
        /// 打印控件的Cookie值
        /// </summary>
        protected string strPrintControlCookie = "";

        /// <summary>
        /// 获取Url中去掉文件名的路径
        /// </summary>
        /// <returns></returns>
        private string GetUrlPath()
        {
            string strUrl = Request.Url.ToString();
            int iEnd = strUrl.LastIndexOf("/");
            strUrl = strUrl.Substring(0, iEnd + 1);

            return strUrl;
        }


        //打印
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult PrintItemLot(string keyValue, string WorkPoint, string Type)
        {
            // string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            keyValue = keyValue.Substring(1, keyValue.Length - 2);
            WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            string strPrintFileName = Server.MapPath("/ReportFile/") + "纷纷无法b_成品条码.fr3";
            PrintJson pJson = new PrintJson(Server.MapPath("./PrintTemp"), strPrintFileName);
            pJson.CheckRegister("苏州智合诚信息科技有限公司", "56DD3B4C172D0D140841CAC98A58A819F4E28EDA5D6E45711DDD64F6A439F68B6A7870CD7DAFD69A919CB870207FE4BB206F92BE3D53C221B019E0797E739EBA4");  //注册信息
            //pJson.CheckRegister("注册姓名", "8ECCCD6A1302DFEE1A6456A5D");  //注册信息
            //pJson.AddPrintParam("ShopName", "测试酒楼");
            //pJson.AddPrintParam("PrintDepositAdd", "说明:本单据为贵客押金收取凭证,盖章有效。退房时请出示,遗失者自负,请妥善保存。退房时间为12:00时,延时退房18:00时以前按半天房费收取,18:00时以后算全天房价。押金单有效期为一个月,过期作废。       贵重物品请交前台寄存,未寄存丢失自负。      谢谢!");
            SetCookieAndURL(pJson);// 设置控件调用的Cookie值,判断是否安装了打印控件
            string strSql = "";
            if (Type == "1")
            {
                strSql = @"select a.LotNo, c.ApplyNegCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSMOApplyNegDetail c on b.TransCode=c.ApplyNegCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "2")
            {
                strSql = @"select a.LotNo, c.OApplyNegCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSOApplyNegDetail c on b.TransCode=c.OApplyNegCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "3")
            {
                strSql = @"select a.LotNo, c.MOCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSMO c on b.TransCode=c.MOCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "5")
            {
                strSql = @"select a.LotNo, c.InCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSOtherIn c on b.TransCode=c.InCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "6")
            {
                strSql = @"select a.LotNo, c.ReturnCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSReturn c on b.TransCode=c.ReturnCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "7")
            {
                strSql = @"select a.LotNo, c.DNCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSDeliveryNotice c on b.TransCode=c.DNCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "8")
            {
                strSql = @"select a.LotNo, c.ODNCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSODeliveryNotice c on b.TransCode=c.ODNCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "9")
            {
                strSql = @"	select a.LotNo, c.RCVCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSManufactureReceive c on b.TransCode=c.RCVCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint

where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "10")
            {
                strSql = @"select a.LotNo, c.DNCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSDeliveryNotice c on b.TransCode=c.DNCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            if (Type == "11")
            {
                strSql = @"select a.LotNo, c.ODNCode,a.InvCode,d.InvName,d.InvStd,C.Quantity,D.InvUnit
FROM dbo.ICSInventoryLot a 
left join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSODeliveryNotice c on b.TransCode=c.ODNCode and b.TransSequence=c.Sequence and b.WorkPoint=c.WorkPoint
left join ICSInventory d on a.InvCode=d.InvCode and a.WorkPoint=d.WorkPoint
where  a.ID IN (" + keyValue.TrimEnd(',') + ") and a.WorkPoint in ('" + WorkPoint.Trim(',') + "')";
            }
            //string strSql = @"Select * From CashLog";
            DataTable dtCashLog = DbHelper.ExecuteTable(DbHelper.ConnectionString, CommandType.Text, strSql, true, null);
            // string strPrintTempFile = pJson.ShowReport(dtCashLog); //产生JSON文件内容
            string strPrintTempFile = pJson.ShowReport(dtCashLog); //导出PDF文件

            //把服务器的URL + 此文件名 传递给控件,由控件下载还原数据进行打印
            string strServerURL = GetUrlPath() + "PrintTemp/";
            string strData = strServerURL + strPrintTempFile;
            strPrintData = PrintFunction.EnBase64(strData);
            var JsonData = new
            {
                strPrintData_1 = strPrintData,
                bIsInstallPrintControl_1 = bIsInstallPrintControl,
                strPrintControlCookie_1 = strPrintControlCookie
            };
            //多站点
            string sql = @"UPDATE dbo.ICSInventoryLot 
                            SET PrintTimes=ISNULL(PrintTimes,0)+1,
                            LastPrintUser='',
                            LastPrintTime=GETDATE()
                            WHERE ID IN (" + keyValue.TrimEnd(',') + ") and WorkPoint in ('" + WorkPoint.TrimEnd(',') + "')";
            SqlHelper.ExecuteNonQuery(sql);
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 设置控件调用的Cookie值,判断是否安装了打印控件
        /// </summary>
        /// <param name="pJson"></param>
        private void SetCookieAndURL(PrintJson pJson)
        {
            bIsInstallPrintControl = false;
            strPrintControlCookie = "";
            HttpCookie pCookieInstall = Request.Cookies["InstallPrintControl"];
            if (pCookieInstall != null)
            { //Cookie存在
                strPrintControlCookie = pCookieInstall.Value.ToString();

                //以Cookie值查找在数据表中是否存在
                string strSql = @"Select * From sys_SRM_CheckInstall Where Cookie = @Cookie";
                SqlParameter[] pmcCookie = { new SqlParameter("Cookie", strPrintControlCookie) };
                using (SqlDataReader drCookie = DbHelper.ExecuteReader(DbHelper.ConnectionString, CommandType.Text, strSql, pmcCookie))
                {
                    if (drCookie.Read())
                    { //标识为已经安装
                        bIsInstallPrintControl = true;
                    }
                    drCookie.Close();
                }
                //更新Cookie的保存时间
                pCookieInstall.Expires = DateTime.Now.AddYears(10);
                Response.SetCookie(pCookieInstall);
            }
            else
            {//Cookie不存在,则新建Cookie
                strPrintControlCookie = System.Guid.NewGuid().ToString();
                pCookieInstall = new HttpCookie("InstallPrintControl", strPrintControlCookie);
                pCookieInstall.Expires = DateTime.Now.AddYears(10);
                Response.Cookies.Add(pCookieInstall);
            }

            string strUrl = Server.MapPath("/PrintTemp/") + "IsCheckInstall";

            pJson.SetCookieAndURL(strPrintControlCookie, strUrl);
        }



        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonWeiWai(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonWeiWai(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonWeiWai(string OApplyNegCode, string Sequence, Pagination pagination)
        {
            DataTable ListData = App.GetSubGridJsonWeiWai(OApplyNegCode, Sequence, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        /// <summary>
        /// 点击生成条码查询
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonWeiWaiByCreate(string OApplyNegCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonWeiWaiByCreate(OApplyNegCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonChengPing(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonChengPing(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(成品)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonChengPingByCreate(string MOCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonChengPingByCreate(MOCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,    
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonXiaoShou(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonXiaoShou(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonQiTa(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonQiTa(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(销售退货)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonXiaoShouByCreate(string SDNCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonXiaoShouByCreate(SDNCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(其他入库)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonQiTaByCreate(string InCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonQiTaByCreate(InCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //归还  
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonGuiHuan(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonGuiHuan(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //审核到货单 
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonSHDH(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonSHDH(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //审核到货单 
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonWWSHDH(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonWWSHDH(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //审核到货单 
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonKLCPRK(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonKLCPRK(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        /// <summary>
        /// 点击生成条码查询(审核到货单)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonSHDHByCreate(string DNCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonSHDHByCreate(DNCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(审核委外到货单)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonWWSHDHByCreate(string ODNCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonWWSHDHByCreate(ODNCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(审核委外到货单)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonKLCPRKByCreate(string RCVCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonKLCPRKByCreate(RCVCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(归还)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonGuiHuanByCreate(string ReturnCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonGuiHuanByCreate(ReturnCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //归还
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormGuiHuan(string ReturnCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormGuiHuan(ReturnCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //审核到货单
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormSHDH(string DNCode, string Sequence, string keyValue, string WorkPoint,string AMEnable)
        {
            int i = App.SubmitFormSHDH(DNCode, Sequence, keyValue, WorkPoint, AMEnable);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }
        //审核委外到货单
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormWWSHDH(string ODNCode, string Sequence, string keyValue, string WorkPoint, string AMEnable)
        {
            int i = App.SubmitFormWWSHDH(ODNCode, Sequence, keyValue, WorkPoint, AMEnable);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }
        //开立成品入库
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormKLCPRK(string RCVCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormKLCPRK(RCVCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }


        //委外拒收单 
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonWWJSD(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonWWJSD(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }
        //拒收单  
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonJSD(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonJSD(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //审核到货单
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormJSD(string DNCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormJSD(DNCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }
        //审核委外到货单
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormWWJSD(string ODNCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormWWJSD(ODNCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //领料申请退料
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonLLSQTL(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonLLSQTL(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(领料申请退料)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonJSDByCreate(string DNCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonJSDByCreate(DNCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 点击生成条码查询(领料申请退料)
        /// </summary>

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonWWJSDByCreate(string ODNCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonWWJSDByCreate(ODNCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonLLSQDByCreate(string ApplyNegCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonLLSQDByCreate(ApplyNegCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        //领料申请退料生成条码
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormLLSQTL(string ApplyNegCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormLLSQTL(ApplyNegCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }


        //材料出库退料
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonCLCK(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonCLCK(queryJson, ref pagination);
            var JsonData = new

            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        //材料出库退料
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonCLCKByCreate(string ApplyNegCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonCLCKByCreate(ApplyNegCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //材料出库退料生成条码
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormCLCKT(string ApplyNegCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormCLCKT(ApplyNegCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //委外领料
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonWWLLTL(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonWWLLTL(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //委外领料
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonWWLLByCreate(string OApplyNegCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonWWLLByCreate(OApplyNegCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //委外领料申请退料
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormWWLL(string OApplyNegCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormWWLL(OApplyNegCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //委外材料出库
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonWWCLCK(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonWWCLCK(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        //委外材料出库
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonWWCLByCreate(string OApplyNegCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonWWCLByCreate(OApplyNegCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormWWCL(string OApplyNegCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormWWCL(OApplyNegCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }



        //返工工单
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonFGGD(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonFGGD(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonFGGDCreate(string FGGDMOCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonFGGDCreate(FGGDMOCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormFGGD(string FGGDMOCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormFGGD(FGGDMOCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetQiSetNum(string keyValue)
        {
            var rows = App.GetQiSetNum(keyValue);
    
            return Content(rows.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetLoadShow()
        {
            DataTable ListData = App.GetLoadShow();
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFreeEnble()
        {
            DataTable ListData = App.GetFreeEnble();
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }




    

        [HttpPost]
        public void StatementExportAll(string Type, string ID)
        {
            //ID = ID.Substring(0, ID.Length - 2);
            DataTable dt = App.StatementExportAll(Type,ID);
            AsposeCell.Export(dt);
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonBHGTM(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonBHGTM(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        //[HttpGet]
        //[HandlerAjaxOnly]
        public ActionResult GetSubGridJsonBHGTM(string LotNo,  string Type, Pagination pagination)
        {
            DataTable ListData = App.GetSubGridJsonBHGTM(LotNo, Type, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        //审核到货单
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormBHGTM(string LotNo, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormBHGTM(LotNo,  keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }


        
   [HttpGet]
        public ActionResult SelGDLX()
        {
            var data = App.SelGDLX();
            return Content(data.ToJson());
        }


        [HttpGet]
        public ActionResult GetInvBatchEnable(string InvCode)
        {
            var data = App.GetInvBatchEnable(InvCode);
            return Content(data.ToJson());
        }



        
        //副产品
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonFCP(Pagination pagination, string queryJson)
        {
            DataTable ListData = App.GetGridJsonFCP(queryJson, ref pagination);
            var JsonData = new
            {
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSubGridJsonFCPCreate(string FCPMOCode, string Sequence, string WorkPoint)
        {
            DataTable ListData = App.GetSubGridJsonFCPCreate(FCPMOCode, Sequence, WorkPoint);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormFCP(string FCPMOCode, string Sequence, string keyValue, string WorkPoint)
        {
            int i = App.SubmitFormFCP(FCPMOCode, Sequence, keyValue, WorkPoint);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }



        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGeneratedNum(string Code, string Sequence, string Type,string thisCreateQty)
        {
            bool Flag = App.GetGeneratedNum(Code, Sequence, Type, thisCreateQty);
            var JsonData = new
            {
                Flag = Flag,
            };
            return Content(JsonData.ToJson());
        }



        //到货单一键生成条码
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormSHDHALL( string keyValue)
        {
            int i = App.SubmitFormSHDHALL( keyValue);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //委外到货单一键生成条码
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormWWSHDHALL( string keyValue)
        {
            int i = App.SubmitFormWWSHDHALL( keyValue);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }

        //开立成品入库(一键生成)
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitFormKLCPRKALL( string keyValue)
        {
            int i = App.SubmitFormKLCPRKALL( keyValue);
            if (i > 0)
            {
                return Success("生成成功!");
            }
            else
            {
                return Error("生成失败!");
            }
        }




        [HttpGet]
        public ActionResult SelectICSColumnEnableForLotEnable()
        {
            var data = App.SelectICSColumnEnableForLotEnable();
            return Content(data.ToJson());
        }



        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetLoadShowForColumn()
        {
            DataTable ListData = App.GetLoadShowForColumn();
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetAMEnable()
        {
            string ListData = App.GetAMEnable();
            var data = new
            {
                rows = ListData,
            };
            return Content(data.ToJson());
        }


        //查询配置-计量
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult SeachMeasure()
        {
            DataTable ListData = App.SeachMeasure();
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        


        //查询配置-开启栏位
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult SeachLableofDisable()
        {
            DataTable ListData = App.SeachLableofDisable();
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }



        
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult SeachAmountEnablebyInvCode(string InvCode)
        {
            DataTable ListData = App.SeachAmountEnablebyInvCode(InvCode);
            var JsonData = new
            {
                rows = ListData,
            };
            return Content(JsonData.ToJson());
        }




        [HttpPost]
        /// <summary>
        /// 文件上传到本地
        /// </summary>
        public string UploadFile()
        {
            try
            {
                //string str_Year = Request.Form["txt_Year"];
                //String UPLoadType = Request.Form["UPLoadType"];
                HttpFileCollection hpFiles = System.Web.HttpContext.Current.Request.Files;
                if (hpFiles != null && hpFiles.Count > 0)
                {

                    string IsXls = System.IO.Path.GetExtension(hpFiles[0].FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
                    if (IsXls != ".xls" && IsXls != ".xlsx")
                    {

                        return "只可以选择Excel(.xls .xlsx)文件";//当选择的不是Excel文件时,返回
                    }
                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss")  + IsXls;             //获取Execle文件名 DateTime日期函数
                    string savePath = System.Web.HttpContext.Current.Server.MapPath("~\\File\\UPLoadFile\\" + filename);//Server.MapPath 获得虚拟服务器相对路径

                    int iLen = hpFiles[0].ContentLength;
                    if (Directory.Exists(savePath)) return "文件已存在";
                    byte[] bData = new byte[iLen];
                    hpFiles[0].InputStream.Read(bData, 0, iLen);


                    FileStream newFile = new FileStream(savePath, FileMode.OpenOrCreate);
                    newFile.Write(bData, 0, bData.Length);
                    newFile.Flush();
                    int _FileSizeTemp = hpFiles[0].ContentLength;

                    newFile.Close();
                    newFile.Dispose();
                    //bool del = false;
                    string mess = "";
                    mess = App.LotBindCode(savePath);
                    if (System.IO.File.Exists(savePath))//删除文件
                    {

                        System.IO.File.Delete(savePath);
                    }
                    return mess;

                }
                else
                {
                    return "获取文件失败";
                }
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }
        }














    }
}