using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using Sybase.DataWindow;
using TXSoft.Common;
using TXSoft.ExtPB;
using TXSoft.DataStore;
using A8ERP;
using System.Text.RegularExpressions;
using A8erp.Cache;

namespace A8ERP.DiaoBo.Base
{
    public partial class W_DiaoBo_Base
    {

        protected string dw_yw_cg_list = string.Empty;
        protected string dw_yw_cg_top = string.Empty;
        protected string dw_yw_cg_mx = string.Empty;
        protected string tableNameHz = string.Empty;
        protected string tableNameMx = string.Empty;
        protected string flag = "DB";
        protected string cg_fs_dwChild = string.Empty;
        protected string dw_yw_cg_top_title = string.Empty; // 入（退）标题
        protected string dw_yw_cg_top_title_init_djlx = string.Empty;// 入（退）默认值 
        protected bool isHaveKc = true;
        protected string curFormPzh = "";
        protected string curQTPzh = "";
        
        public W_DiaoBo_Base()
        {

        }

        public override void OnLoad()
        {

            base.OnLoad();
            #region 外来凭证打开
            if (Request["curFormPzh"] != null)
            {
                curFormPzh = Request["curFormPzh"].ToString();
            }
            this.SetParm("curFormPzh", curFormPzh);
            #endregion

            if (Request["curQTPzh"] != null)
            {
                curQTPzh = Request["curQTPzh"].ToString();
                this.SetParm("curQTPzh", curQTPzh);
            } 
            this.SetParm("tableNameHz", tableNameHz);
            this.SetParm("tableNameMx", tableNameMx);

            if (string.IsNullOrEmpty(dw_yw_cg_list))
            {
                dw_yw_cg_list = "dw_yw_db_ddhz_list";
            }
            if (string.IsNullOrEmpty(dw_yw_cg_top))
            {
                dw_yw_cg_top = "dw_yw_db_ddmx_top";
            }
            if (string.IsNullOrEmpty(dw_yw_cg_mx))
            {
                dw_yw_cg_mx = "dw_yw_db_ddmx_list";
            }
            if (string.IsNullOrEmpty(cg_fs_dwChild))
            {
                cg_fs_dwChild = "CG_DDFS";
            }
            
            string curYhbh = A8ERP.AppService.GetUserID();
            this.SetParm("curYhbh", curYhbh);
            this.SetParm("curJdbh", A8ERP.AppService.GetJdbh());

            dw_hz.DataStore.DataWindowObject = dw_yw_cg_list;
            dw_mx_top.DataStore.DataWindowObject = dw_yw_cg_top;
            dw_mx.DataStore.DataWindowObject = dw_yw_cg_mx;
            base.Dw_UserPerMission = dw_mx;
            #region 每页显示条数&查询天数
            string curDayAndPageSize = DBHelp.GetDayAndPageSize(this.FunctionID,"DB");

            string curPageSize = curDayAndPageSize.Split('#')[1];
            this.dw_hz.PageSize = Convert.ToInt32(curPageSize);

            // 查询天数
            string seldays = curDayAndPageSize.Split('#')[0];
            this.SetParm("seldays", seldays.ToString());

            #endregion

            if (!string.IsNullOrEmpty(dw_yw_cg_top_title))
            {
                this.SetParm("dw_yw_cg_top_title_value", dw_yw_cg_top_title);
            }
            if (string.IsNullOrEmpty(dw_yw_cg_top_title_init_djlx))
            {
                dw_yw_cg_top_title_init_djlx = "1";

            }
            string curQxlx = "db";
            this.SetParm("dw_yw_cg_top_title_init_djlx_value", dw_yw_cg_top_title_init_djlx);
            if (dw_yw_cg_top_title_init_djlx == "2")
            { curQxlx = "db"; }
            else if (dw_yw_cg_top_title_init_djlx == "1")
            { curQxlx = "dbc"; }
            else
                curQxlx = "db";
            this.SetParm("curQxlx", curQxlx);
            //dw_hz.Retrieve(dw_yw_cg_top_title_init_djlx, curYhbh);

            DataWindowChild dwc = dw_mx.GetChild("spkh");
            //dwc.SetTransaction(this.AdoTransaction);
            //dwc.Retrieve("%");

            DataWindowChild idwc_ys_child = dw_mx.GetChild("ysbh");
            dwc.SetTransaction(this.AdoTransaction);

            dwc = dw_mx.GetChild("bxbh");
            dwc.SetTransaction(this.AdoTransaction);

            dwc = dw_mx_top.GetChild("bmbh1");
            dwc.SetTransaction(this.AdoTransaction);
            if (flag == "DC")
            {
                dwc.Retrieve("%", curYhbh, "DB");
            }
            else {
                dwc.Retrieve("%", curYhbh, "DBC");
            }

            if (flag == "DC")
            {
            dwc = dw_mx_top.GetChild("dbck");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Retrieve("%", curYhbh, "DBJ");
            }

            dwc = dw_mx_top.GetChild("bmbh2");
            dwc.SetTransaction(this.AdoTransaction);
            if (flag == "DC")
            {
                dwc.Retrieve("%", curYhbh, "DBC");
            }
            else
            {
                dwc.Retrieve("%", curYhbh, "DB");
            }
            //dwc.Retrieve("%", curYhbh, "DBC");

            dwc = dw_mx_top.GetChild("cgfs");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Modify("csbh.visible='0'");
            dwc.Retrieve(cg_fs_dwChild);       // 订单方式的下拉框。

            dwc = dw_mx_top.GetChild("cgry");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Retrieve("%", curYhbh, "db");

            dwc = dw_mx_top.GetChild("xxly");
            dwc.SetTransaction(this.AdoTransaction);
            dwc.Retrieve("XXLY");


            dw_tmjqfs.Retrieve();//条码扫描截取方式。。 

            ////数据窗口关联服务
            //LinkageService service = (LinkageService)dw_mx_top.Services.Add(ServiceName.Linkage);          
            //service.SetMaster(dw_hz.DataStore);
            //service.Add("pzh", "pzh");
            //service.LinkageStyle = LinkageStyle.Retrieve;

            //service = (LinkageService)dw_mx.Services.Add(ServiceName.Linkage);
            //service.SetMaster(dw_mx_top.DataStore);
            //service.Add("pzh", "pzh");
            //service.LinkageStyle = LinkageStyle.Retrieve;

            //ReportService service2 = (ReportService)dw_hz.Services.Add(ServiceName.Report);
            //service2.RequestorDrawTitle = false;

            string FormatPzhAndValue = AppService.GetYwFormatBH(AppService.GetUserID(), this.FunctionID);
            if (FormatPzhAndValue != null && FormatPzhAndValue != "")
            {
                this.SetParm("format_pzh", FormatPzhAndValue.Split('&')[0].ToString());
                AppService.GetYwFormat(dw_hz, FormatPzhAndValue.Split('&')[1].ToString());
            }
            //来源于对账单
            string curDzdPzh = "";
            if (Request["curDzdPzh"] != null)
            {
                curDzdPzh = Request["curDzdPzh"].ToString();
            }

            this.SetParm("curDzdPzh", curDzdPzh);
            
            this.SetParm("flag", flag);
            this.SetParm("curUserId", AppService.GetUserID().ToString());
            this.SetParm("curBmbhAndBmmc", new DBHelp().GetCurrBusinessDefaultBmbhAndBmmc(this.FunctionID));
            this.SetParm("RowAndCol", AppService.Get35HangAndLie());// 35列的表头的行和列
            string curRowAndCol = Common.DWServiceHelp.GetCmBtRowAndCol();
            this.SetParm("curRowAndCol", curRowAndCol);
            this.SetParm("curJqm", AppService.GetJqm());
            this.SetParm("curUserGroupId", AppService.GetUser().GroupId);//权限组
            if ("DC".Equals(flag) || "DT".Equals(flag))
            {
                this.SetParm("curSfqytmlsh", AppService.GetXiTongCanShu("yw_db_tmlsh", "xtcs", "qybz"));
                this.SetParm("curDate", DBHelp.GetServerCurrentDateFormatYYYYMMDDHHMMSS());
            }

            double jqd = 0;


            string csmc = AppService.GetXiTongCanShu("DB", "YW_JQD", "csmc");
            string jqw = AppService.GetXiTongCanShu("DB", "YW_JQD", "bzxx");



            if (csmc == "0")
            {
                jqd = Convert.ToDouble(jqw) * 0.1;
            }
            else if (csmc == "1")
            {
                jqd = Convert.ToDouble(jqw) * 0.01;
            }
            else if (csmc == "2")
            {
                jqd = Convert.ToDouble(jqw) * 0.001;
            }
            else if (csmc == "3")
            {
                jqd = Convert.ToDouble(jqw) * 0.0001;
            }

            #region 调拨记账模式更改,1表示调入方统一记账
            //dt = db.QueryRDataTable(" select qybz from da_qj_csb where csfl='db_jzfs' and csbh='jzfs' ");
            //qybz = dt.Rows[0]["qybz"].ToString();

            string qybz = AppService.GetXiTongCanShu("jzfs", "db_jzfs", "qybz");
            if ("1".Equals(qybz) && "DC".Equals(flag))
            {
                tb_1.GetItem("btn_Tally").Visible = false;
            }

            if ("1".Equals(qybz) && "DR".Equals(flag))
            {
                tb_1.GetItem("btn_DCFAudit").Visible = true;
            }
            else
            {
                tb_1.GetItem("btn_DCFAudit").Visible = false;
            }


            //if ("DZ".Equals(flag))
            //{
            //    tb_1.GetItem("btn_Tally").Visible = true;
            //}
            #endregion

            string bxshow = "";
            #region 是否显示版型
            object bzqybz = CacheHelp.GetDataTable("DA_SP_BXBT").Rows[0]["qybz"];

            if (bzqybz != null)
            {
                bxshow = CacheHelp.GetDataTable("DA_SP_BXBT").Rows[0]["qybz"].ToString();
            }


            #endregion

            this.SetParm("curJqd", jqd.ToString());
            this.SetParm("curJqw", csmc.ToString());
            this.SetParm("curQybz", qybz.ToString());
            this.SetParm("bxshow", bxshow.ToString());

            //--------------------打印----------------------------
            this.SetParm("curFunctionId", this.FunctionID);
            this.SetParm("cur35ColString", A8ERP.Common.DWServiceHelp.GetCMByString1());
            dw_print.AllowCreate = true;
            dw_print.AllowRetrieve = true;
            dw_print.AllowSetFullState = true;
            dw_proxy.AllowCreate = true;
            dw_proxy.AllowRetrieve = true;
            dw_proxy.AllowSetFullState = true;
            //--------------------打印结束-------------------------

            #region  处理权限
            dw_mkfhjb.Retrieve(this.FunctionID);  // 审核级别 
            #endregion

            for (int i = 1; i <= dw_mkfhjb.RowCount; i++)
            {
                ddlb_1.Items.Add(dw_mkfhjb.GetItemString(i, "fhmc"));
            }
            string curShzt = "";
            curShzt = dw_mkfhjb.GetItemString(dw_mkfhjb.RowCount - 1, "fhmc");
           
            #region 订单的反审核操作
            if ("DB".Equals(flag))
            {
                tb_1.GetItem("btn_FAudit").Visible = true;
                ddlb_1.Items.Add("未记账");
                ddlb_1.Items.Add("已记账");
                ddlb_1.Text = "未记账";
            }
            #region 通知单没有记账操作
            else if ("DZ".Equals(flag))
            {
                tb_1.GetItem("btn_FAudit").Visible = true;
                ddlb_1.Items.Add("已转单");
                ddlb_1.Items.Add("未转单");
                ddlb_1.Text = "未记账";
            }
            #endregion
            else
            {
                ddlb_1.Items.Add("未记账");
                ddlb_1.Items.Add("已记账");
                ddlb_1.Text = "未记账";
            }
            #endregion
          
            #region 出入库单没有转单操作
            if ("DR".Equals(flag))
            {
                tb_1.GetItem("btn_Trans").Visible = false;
                //dw_hz.Modify("zdzt_t.visible='0' zdzt.visible='0'");
            }
            if ("DC".Equals(flag))
            {
                // 调拨出库
                ddlb_1.Items.Add("已转单");
                ddlb_1.Items.Add("未转单");
                ddlb_1.Items.Add("入方记账");
            }
            if ("DZ".Equals(flag))
            {
                // 调拨出库
                ddlb_1.Items.Add("未记账");
                ddlb_1.Items.Add("已记账");
            }
            if ("DB".Equals(flag))
            {
                // 调拨出库
                ddlb_1.Items.Add("已转单");
                ddlb_1.Items.Add("未转单");
            }
            #endregion
            ddlb_1.Items.Add("全部");
           
            #region 整单折扣处理
            this.st_1.X = 786;
            this.st_1.Y = 110;
            this.em_1.X = 859;
            this.em_1.Y = 107;
            #endregion

            #region  处理标题35列
            if ("1".Equals(AppService.GetDbSfxsRfkc()))
            {
                Common.DWServiceHelp.Add35CmColShowDbRfkc(dw_mx, isHaveKc);
            }
            else {
                Common.DWServiceHelp.Add35CmCol(dw_mx, isHaveKc);
            }
            dw_mx.Modify("datawindow.detail.color='16777215\tif(currentrow()=getrow(), 12639424,if(mod(getrow(),2)=0,536870912, 16777215) )' ");
            #endregion

            //来源于首页
            string curBmbh = Request["curParam"];
            string curGysbh = Request["curParam1"];
            this.SetParm("curBmbh", curBmbh);
            this.SetParm("curGysbh", curGysbh);

            if (this.Page != null)
            {
                this.Page.ClientScript.RegisterClientScriptInclude("W_DiaoBo_Base", "/DiaoBo/Base/W_DiaoBo_Base.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_DiaoBo_ChaXun", "/DiaoBo/Base/W_DiaoBo_ChaXun.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("OpenWindowQueryBase", "/DangAn/Base/OpenWindowQueryBase.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("OpenWindowSimpleChoseBase", "/DangAn/Base/OpenWindowSimpleChoseBase.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_HYHKDR", "/import/W_HYHKDR.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_TXTDR", "/import/W_TXTDR.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_PH_FenJian_List", "/PiFa/PiFaBase/W_PH_FenJian_List.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_YW_ZNSM_DBYW", "/YeWuZongHeFenXi/W_YW_ZNSM_DBYW.win.js?JSV=" + AppService.GetJSVesion());
                this.Page.ClientScript.RegisterClientScriptInclude("W_KC_Dj_PIC", "/KuCun/TongJiFenXi/W_KC_Dj_PIC.win.js?JSV=" + AppService.GetJSVesion());

            }

            //设置业务基础价格取价方式
            if (!"ERROR".Equals(AppService.GetQjfs()))
                this.SetParm("QJFS", AppService.GetQjfs()); 

            this.SetParm("GnqxStr", this.GnqxStr);// 权限传到前台给右键使用。
            if (this.GnqxStr != null)
            {
                //修改权限
                if ((this.GnqxStr).ToString().IndexOf("btn_Edit") < 0)
                {
                    this.btn_Save.Visible = false;
                }
                //单据复制权限
                if ((this.GnqxStr).ToString().IndexOf("rbn_djfz") < 0)
                {
                    this.rbn_djfz.Enabled = false;
                }
                //单据粘贴权限
                if ((this.GnqxStr).ToString().IndexOf("rbn_djzt") < 0)
                {
                    this.rbn_djzt.Enabled = false;
                }
                //单据导出权限
                if ((this.GnqxStr).ToString().IndexOf("rbn_dcel") < 0)
                {
                    this.btn_Export.Enabled = false;
                }
                //excel导入权限
                if ((this.GnqxStr).ToString().IndexOf("rbn_drcel") < 0)
                {
                    this.rbn_drcel.Enabled = false;
                }
            }
            //价格权限部分
            string curjgqx = base.ColumnQxStr;
            this.SetParm("curjgqx", curjgqx);
            int isExist_dbj = curjgqx.IndexOf("dbj");

            //调拨价格
            if (isExist_dbj != -1)
            {
                dw_hz.Modify("zjje.visible='0'");
                dw_mx.Modify("lsj.visible='0' lsjze.visible='0' zkl.visible='0' ");
            }
            
            //吊牌价格价格
            int isExist_dpj = curjgqx.IndexOf("dpj");
            if (isExist_dpj != -1)
            {
                dw_hz.Modify("dpjze.visible='0'");
                dw_mx.Modify("dpj.visible='0' dpjze.visible='0' ");
            }
            //零售价格
            int isExist_lsj = curjgqx.IndexOf("jj");
            if (isExist_lsj != -1)
            {
                dw_hz.Modify("lsjze.visible='0'");
                dw_mx.Modify("jj.visible='0' jsjze.visible='0' ");
            }
            if (flag == "DB")
                sc_1.Panel1.Title = "调拨订单";
            else if (flag == "DZ")
            {
                sc_1.Panel1.Title = "调拨通知单";
                btn_znph.Visible = true;
            }
            else if (flag == "DC")
                sc_1.Panel1.Title = "仓库调拨出库单";
            else if (flag == "DT")
                sc_1.Panel1.Title = "仓库调拨入库单";

            
            dw_mx.ExportProcessSparse = false;
            #region 记录条码流水需要参数
            this.SetParm("companyidLength", AppService.GetXiTongCanShu("userid", "companyid", "bzxx"));
            this.SetParm("companyid", AppService.GetXiTongCanShu("userid", "companyid", "csmc") + AppService.GetXiTongCanShu("userid", "companyid", "qybz"));

            #endregion


            if (flag == "DC" || flag == "DR")
            {
                 
                dw_hz.PageSize = 0;
                sle_Count.Text = curPageSize;
            }
            else
            {
                tb_paging.Visible = false;
                A8ERP.Common.DWServiceHelp.AddReportService(dw_hz);
            }
        }
    }
}
