/// <summary>
    /// 到货单列表行事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();

        try
        {
            GridViewRow gvRow = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;
            BArriveDetailData arriveDetailModel = new BArriveDetailData();

            #region 重新收货

            if (e.CommandName == "receive")
            {
                int id = Convert.ToInt32(this.grid.DataKeys[gvRow.RowIndex]["id"]);

                arriveDetailModel = arriveDetailBB.GetModel(id);

                arriveDetailModel.isFinishReceive = false;//收货未完成

                arriveDetailBB.ModifyRecord(arriveDetailModel);

                return;
            }

            #endregion 重新收货
        }
        finally
        {
            arriveDetailBB.Dispose();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.StrWhere = "1=1";
            string id= Request.QueryString["id"];

            BArriveDetailBB arriveDetailBB = new BArriveDetailBB();
            BArriveBillBB arriveBillBB = new BArriveBillBB();
            DataSet ds = new DataSet();

            try
            {
                vBArriveBillData data = arriveBillBB.GetVModel(Convert.ToInt32(id));
                this.IdValue =  data.billNo;
                this.StrWhere = " arriveBillNo='" + data.billNo + "'";
                txtBillNo.Text = data.billNo;
                txtSupple.Text = data.custNm;
                txtarrageBillNo.Text = "PT" + data.billNo;
                txtarriveDt.Text = Convert.ToDateTime(data.arriveDt).ToString("yyyy-MM-dd");
                this.DtDetail = arriveDetailBB.GetVList(this.StrWhere).Tables[0];
            }
            finally
            {
                arriveDetailBB.Dispose();
            }

            this.BindGrid();
        }

        InitPageData();
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindGrid()
    {
        //throw new NotImplementedException();
        BArriveDetailBB saleBillBB = new BArriveDetailBB();
        DataSet ds = new DataSet();

        try
        {

            string strwhere = this.StrWhere;

            //销售订单编号
            if (!string.IsNullOrEmpty(this.txtSaleBillNo.Text))
            {
                strwhere += " and billNo like '%" + this.txtSaleBillNo.Text.Trim().Replace("'", "''").Trim() + "%'";
            }

            //客户
            if (!string.IsNullOrEmpty(this.txtMaterialNo.Text))
            {
                strwhere += " and (MaterialNo like '%" + this.txtMaterialNo.Text.Replace("'", "''").Trim()
                    + "%')";
            }

            //订单状态

           // ds = saleBillBB.GetVListChange(strwhere);
            ds = saleBillBB.GetVListChange(strwhere);
            this.grid.DataSource = ds.Tables[0];
            this.grid.DataBind();

            //赋值记录条数、页面总数
            this.Label3.Text = ds.Tables[0].Rows.Count.ToString();
            this.Label2.Text = this.grid.PageCount.ToString();
            this.currPage.Text = (this.grid.PageIndex + 1).ToString();
        }
        finally
        {
            saleBillBB.Dispose();
        }
    }
    /// <summary>
    /// 初始化数据
    /// </summary>
    private void InitData()
    {
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();
        BArrangeBillDetailBB arrangeBillDetailBB = new BArrangeBillDetailBB();

        try
        {
            #region 生成到货明细数据源

            //到货明细
            this.DtDetail = arriveDetailBB.GetVList(" arriveBillNo='" + this.ArriveBillNo + "'").Tables[0];

            #endregion 生成到货明细数据源

            #region 生成排托明细数据源

            //排托明细
            string strWhere = "arriveBillNo=@arriveBillNo";
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@arriveBillNo", this.ArriveBillNo) };
            DataSet ds = arrangeBillDetailBB.GetVList(strWhere, param);

            if (ds != null && ds.Tables.Count > 0)
            {
                this.DtResult = ds.Tables[0];

                this.DtResult.Columns.Add(new DataColumn("rowId", typeof(string)));
                this.DtResult.Columns.Add(new DataColumn("isdel", typeof(string)));
                this.DtResult.Columns.Add(new DataColumn("ischeck", typeof(string)));
                this.DtResult.Columns.Add(new DataColumn("hideAmount", typeof(string)));

                foreach (DataRow dr in this.DtResult.Rows)
                {
                    dr["rowId"] = Guid.NewGuid().ToString();
                    dr["isdel"] = "0";
                    dr["ischeck"] = "false";

                    #region 记录合计行

                    if (dr["boxNum"] != null && dr["boxNum"] != DBNull.Value)
                    {
                        //初始化时保存amount的原始值
                        dr["hideAmount"] = dr["boxNum"].ToString() == "" ? "0" : dr["boxNum"].ToString();
                    }
                    else
                    {
                        dr["hideAmount"] = "0";
                    }

                    #endregion
                }
            }

            #endregion 生成排托明细数据源
        }
        finally
        {
            arriveDetailBB.Dispose();
            arrangeBillDetailBB.Dispose();
        }
    }
    public void FinishTSArriveDetail(int arriveDetailId)
    {
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();
        SCommBB commBB = new SCommBB();

        try
        {
            BArriveDetailData arriveDetailModel = new BArriveDetailData();
            object obj = null;
            string strArriveBillNo = "", strFinanceBillNo = "", strMaterialNo = "";

            arriveDetailModel = arriveDetailBB.GetModel(arriveDetailId);
            arriveDetailModel.isFinishReceive = true;//收货完成

            arriveDetailBB.ModifyRecord(arriveDetailModel);

            //更改到货单明细的排托数量
            strArriveBillNo = arriveDetailModel.arriveBillNo;
            strFinanceBillNo = arriveDetailModel.financeBillNo;
            strMaterialNo = arriveDetailModel.materialNo;

            obj = commBB.ExecuteScalar("select count(1) from dbo.BArrangeBillBox where arriveBillNo='"
                + strArriveBillNo + "' and financeBillNo='" + strFinanceBillNo + "' and materialNo='" + strMaterialNo + "'");
            if (obj != null)
            {
                commBB.ExecuteSql("update dbo.BarriveDetail set boxNum=" + obj.ToString() + " where id=" + arriveDetailId.ToString());
            }
        }
        finally
        {
            arriveDetailBB.Dispose();
            commBB.Dispose();
        }
    }
    public bool UpdateArriveDetailWeight(int arriveDetailId, double firstBoxSingleWeight)
    {
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();

        try
        {
            BArriveDetailData arriveDetailModel = new BArriveDetailData();

            arriveDetailModel = arriveDetailBB.GetModel(arriveDetailId);
            arriveDetailModel.firstBoxSingleWeight = firstBoxSingleWeight;

            return arriveDetailBB.ModifyRecord(arriveDetailModel);
        }
        finally
        {
            arriveDetailBB.Dispose();
        }
    }
    public void SaveArriveBox(int arriveDetailId, string strArriveBillNo, string strFinanceBillNo, string strMaterialNo,
        string strBoxNo, string strPalletNo, int factNum, int isrtEmpId, string strWareLocatorNo)
    {
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();
        BArrangeBillBoxBB arrangeBillBoxBB = new BArrangeBillBoxBB();
        SCommBB commBB = new SCommBB();

        try
        {
            BArriveDetailData arriveDetailModel = new BArriveDetailData();
            BArrangeBillBoxData arrangeBillBoxModel = new BArrangeBillBoxData();
            DataTable dtWareLocator = new DataTable();

            //获取某库位信息
            dtWareLocator = this.GetWareLocatorInfo(strWareLocatorNo);

            //获取到货单明细实例
            arriveDetailModel = arriveDetailBB.GetModel(arriveDetailId);

            arrangeBillBoxModel.arriveBillNo = strArriveBillNo;//到货单号
            arrangeBillBoxModel.financeBillNo = strFinanceBillNo;//采购单号
            arrangeBillBoxModel.financeBillId = arriveDetailModel.financeBillId;//采购订单ID
            arrangeBillBoxModel.financeBillLineNum = arriveDetailModel.financeBillLineNum;//采购订单行号
            arrangeBillBoxModel.materialNo = strMaterialNo;//物料号
            arrangeBillBoxModel.boxNo = strBoxNo;//箱号
            arrangeBillBoxModel.palletNo = strPalletNo;//托盘号
            arrangeBillBoxModel.wareNo = dtWareLocator.Rows[0]["wareNo"].ToString();//库区
            arrangeBillBoxModel.wareLocatorNo = strWareLocatorNo;//库位
            arrangeBillBoxModel.isBoxArrange = false;//箱子是否已经排托
            arrangeBillBoxModel.isPalletUsing = false;//托盘是否占用
            arrangeBillBoxModel.acceptEmpId = isrtEmpId;//收货人
            arrangeBillBoxModel.acceptDt = System.DateTime.Now.ToString();//收货时间
            arrangeBillBoxModel.factNum = factNum;//收货数量

            arrangeBillBoxBB.AddRecord(arrangeBillBoxModel);

            //锁定库位
            commBB.ExecuteSql("update dbo.LWareLocator set isUsing=1 where wareLocatorNo='" + strWareLocatorNo + "'");
        }
        finally
        {
            arriveDetailBB.Dispose();
            arrangeBillBoxBB.Dispose();
            commBB.Dispose();
        }
    }
    public string SAPInStock(int arriveDetailId, int financeBillId, int financeBillLineNum, string strMaterialNo,
        double num, DateTime stockDt, DateTime arriveDt, string custNo, string custNm)
    {
        SapOnlineService.SapOnlineService sapService = new SapOnlineService.SapOnlineService();
        SCommBB commBB = new SCommBB();
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();

        try
        {
            SapOnlineService.POR1[] list = null;
            SapOnlineService.POR1 por1 = new SapOnlineService.POR1();
            BArriveDetailData arriveDetailModel = new BArriveDetailData();
            string strResult = "";

            //SAP入库
            //需要传递:采购订单号、行号、物料号、数量
            list = new SapOnlineService.POR1[1];

            por1.DocEntry = financeBillId;//采购订单号
            por1.LineNum = financeBillLineNum;//采购订单行号
            por1.ItemCode = strMaterialNo;//物料编号
            por1.Quantity = num;//数量

            list[0] = por1;
            strResult = sapService.Web_AddPDN_For_POR1(custNo, custNm, stockDt, "由WMS同步", stockDt, "由WMS同步", stockDt, list);
            // strResult = sapService.Web_AddPDN_For_POR1(por1.DocEntry, stockDt, "从仓库系统生成", arriveDt, list);

            if (strResult.Split('&')[0] == "1")
            {
                //更改到货中间表数据
                arriveDetailModel = arriveDetailBB.GetModel(arriveDetailId);

                arriveDetailModel.isSapInStock = true;//是否已提交SAP入库

                arriveDetailBB.ModifyRecord(arriveDetailModel);

                //更改物料箱的“是否已提交SAP入库”状态
                commBB.ExecuteSql(@"update dbo.BArrangeBillBox set isSapInStock=1 where isSapInStock=0
                                    and inStockDt is not null and arriveBillNo='" + arriveDetailModel.arriveBillNo
                                    + "' and financeBillId='" + financeBillId + "' and financeBillLineNum='" + financeBillLineNum
                                    + "' and materialNo='" + strMaterialNo + "'");
            }

            return strResult;
        }
        finally
        {
            sapService.Dispose();
            arriveDetailBB.Dispose();
            commBB.Dispose();
        }
    }
    public DataTable GetArriveDetail(int arriveDetailId)
    {
        BArriveDetailBB arriveDetailBB = new BArriveDetailBB();

        try
        {
            DataTable dt = new DataTable();

            dt = arriveDetailBB.GetVList("id=" + arriveDetailId.ToString()).Tables[0];
            return dt;
        }
        finally
        {
            arriveDetailBB.Dispose();
        }
    }
    /// <summary>
    /// 提交排托单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCommit_Click(object sender, EventArgs e)
    {
        BArriveDetailBB bb = new BArriveDetailBB();

        try
        {
            //获取选中的数据Id
            foreach (GridViewRow gvrow in this.grid.Rows)
            {
                CheckBox chkId = (CheckBox)gvrow.FindControl("chkId");
                if (chkId.Checked == true)
                {
                    string strBillNo = "";
                    DataSet ds = new DataSet();

                    strBillNo = chkId.ValidationGroup;

                    if (bb.ModifyRecordSC(strBillNo))
                    {
                        this.BindGrid();//重新绑定到货单列表
                        return;
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"更改生产到货单状态失败!\");", true);
                        return;
                    }

                }

            }

            this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"请选择一条记录!\");", true);
            return;
        }
        finally
        {
            bb.Dispose();

        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.StrWhere = "1=1";
            string id= Request.QueryString["id"];

            BArriveDetailBB arriveDetailBB = new BArriveDetailBB();
            TSBarrivebillBB arriveBillBB = new TSBarrivebillBB();
            DataSet ds = new DataSet();

            try
            {
                TSBarrivebillData data = arriveBillBB.GetModel(Convert.ToInt32(id));
                this.IdValue =  data.arriveBillNo;
                this.StrWhere = " arriveBillNo='" + data.arriveBillNo + "'";
                txtBillNo.Text = data.arriveBillNo;
                this.DtDetail = arriveDetailBB.GetTSVList(this.StrWhere).Tables[0];
            }
            finally
            {
                arriveDetailBB.Dispose();
            }

            this.BindGrid();
        }

        InitPageData();
    }