using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Core.Mes.ClientFrameWork;
using Core.Mes.IBaseInterface;
using System.Data;

namespace Core.XgMes.Client.JGKC.SteelPlateManager
{
    public partial class FrmOrdernoAcliat : Mes.ClientFrameWork.FrmBase
    {
        public FrmOrdernoAcliat()
        {
            InitializeComponent();
        }
        private void InitCombo()
        {
            string strSQL = "";
            string strout = "";
            DataSet ds;
           

            try
            {
                //合同类型
                strSQL = "select ID_ ,Name_ from KCJ_BASEDATA where Sort_Code='8091' ";
                ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet",
                    new object[] { strSQL }, out strout);
                if (strout != "") { MessageBox.Show(strout); return; }


                Comm.FillComboEditor(ref this.ultrCmb_PactNo, ds);

                strSQL = "SELECT BUYERCODE,BUYERNAME FROM SEL_BUYERBASEINFO UNION SELECT CUST_CD BUYERCODE, CUST_NM BUYERNAME FROM TBZ00_CUSTOMER WHERE  REC_TP='01' AND REC_STS='1'  ";
                ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet",
                    new object[] { strSQL }, out strout);
                if (strout != "") { MessageBox.Show(strout); return; }

                Comm.Init_ComboControl(this.Cmb_Buy, "SEL_BUYERBASEINFO", "BUYERNAME", "BUYERCODE", ref ds);
                Comm.FillComboEditor(ref this.qBuyer, ds);
                Comm.SetGridDropDownList(this.ultraGrid1, ds, "SEL_BUYERBASEINFO", "BUYERCODE", "BUYERNAME", "BUYERCODE");


                strSQL = "SELECT INCEPTCORPCODE,INCEPTCORPNAME FROM SEL_INCEPTCORPINFO  UNION SELECT CUST_CD INCEPTCORPNAME, CUST_NM BUYERNAME FROM TBZ00_CUSTOMER WHERE  REC_TP='02' AND REC_STS='1'  ";
                ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet",
                    new object[] { strSQL }, out strout);
                if (strout != "") { MessageBox.Show(strout); return; }

                Comm.Init_ComboControl(this.Cmb_Inco, "SEL_INCEPTCORPINFO", "INCEPTCORPNAME", "INCEPTCORPCODE", ref ds);
                Comm.SetGridDropDownList(this.ultraGrid1, ds, "SEL_BUYERBASEINFO", "INCEPTCORPCODE", "INCEPTCORPNAME", "BUYERCODE");
                Comm.FillComboEditor(ref this.qIncep, ds);
                
            }
            catch { }

           
        }

        private void FrmOrdernoAcliat_Load(object sender, EventArgs e)
        {
            dte_TimeB.Value = DateTime.Now;
            dte_TimeE.Value = DateTime.Now;
            uCob_LX.Value = "0";
            InitCombo();
        }
        private void Query()
        {
            string sql = "";
            string strOut = "";
            string strWhere = "";
            if (chk_Intime.Checked == false && checkPactNo.Checked == false)
            {
                MessageBox.Show("请选择任意条件进行查询!!!!!");
            }
            if (chk_Intime.Checked  )
            {
                if (dte_TimeB.Enabled && dte_TimeE.Enabled)
                {
                    strWhere += " WHERE INTIME BETWEEN TO_DATE('" + Convert.ToDateTime(this.dte_TimeB.Value).ToString("yyyy-MM-dd") + "','yyyy-MM-dd') "
                            + " AND TO_DATE('" + Convert.ToDateTime(this.dte_TimeE.Value).ToString("yyyy-MM-dd") + " 23:59:59','yyyy-MM-dd hh24:mi:ss') ";
                }
            }
           
            if (checkPactNo.Checked)
            {
                if (strWhere == "")
                {
                    strWhere += "  WHERE (PACTNO LIKE '" + PactNo.Text.Trim() + "%')";
                }
                else
                {
                    strWhere += " AND (PACTNO LIKE '" + PactNo.Text.Trim() + "%' OR ORDERNO LIKE '" + PactNo.Text.Trim() + "%' )";
                }
            }
            DataSet ds = new DataSet();
            sql = "SELECT MAX(PACTNO)PACTNO,ORDERNO,MAX(BUYERCODE)BUYERCODE,MAX(INCEPTCORPCODE)INCEPTCORPCODE,MAX(BUYERCODE1)BUYERCODE1,MAX(INCEPECODE1)INCEPECODE1,RECODER,PACTNOLX,RECODERTIME,MAX(INTIME)INTIME FROM ("
                 + "SELECT SUBSTR(T.ORDERNO, 1, LENGTH(T.ORDERNO) - 3)PACTNO,T.ORDERNO,"
                 + " CASE WHEN (SELECT COUNT(1) FROM SEL_PACTDETAIL WHERE ORDERNO=T.ORDERNO)>0 THEN "
                 + "(SELECT BUYERCODE FROM SEL_PACTDETAIL WHERE ORDERNO=T.ORDERNO) "
                 + "WHEN (SELECT COUNT(1) FROM SEL_PACTINFO_CLEAR AA WHERE AA.PACTNO = SUBSTR(T.ORDERNO, 1, LENGTH(T.ORDERNO) - 3))>0 THEN  "
                 + "(SELECT AA.BUYERCODE FROM SEL_PACTINFO_CLEAR AA WHERE AA.PACTNO = SUBSTR(T.ORDERNO, 1, LENGTH(T.ORDERNO) - 3)) "
                 + " WHEN  T.ORDERNO LIKE '17%' THEN '106' END BUYERCODE,"
                 + "CASE WHEN (SELECT COUNT(1) FROM SEL_PACTDETAIL WHERE ORDERNO=T.ORDERNO)>0 THEN "
                 + "(SELECT INCEPTCORPCODE FROM SEL_PACTDETAIL WHERE ORDERNO=T.ORDERNO) "
                 + "WHEN (SELECT COUNT(1) FROM SEL_PACTINFO_CLEAR AA WHERE AA.PACTNO = SUBSTR(T.ORDERNO, 1, LENGTH(T.ORDERNO) - 3))>0 THEN  "
                 + "(SELECT AA.INCEPTCORPCODE FROM SEL_PACTINFO_CLEAR AA WHERE AA.PACTNO = SUBSTR(T.ORDERNO, 1, LENGTH(T.ORDERNO) - 3)) "
                 + "WHEN  T.ORDERNO LIKE '17%' THEN '201112020001' END INCEPTCORPCODE,"
                 +"T.INTIME,"
                 +"(SELECT BUYERCODE FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORDERNO AND ISVALID='1')BUYERCODE1, "
                 + "(SELECT INCEPECODE FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORDERNO AND ISVALID='1')INCEPECODE1,"
                 + "(SELECT RECODER FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORDERNO AND ISVALID='1')RECODER,"
                 + "(SELECT PACTNOLX FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORDERNO AND ISVALID='1')PACTNOLX,"
                 + "(SELECT TO_CHAR(RECODERTIME,'YYYY-MM-DD HH24:MI') FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORDERNO AND ISVALID='1')RECODERTIME "
                 +" FROM KCJ_STORAGELIST_ZW T  "
          
                 + " UNION  "
                 + " SELECT B.ORD_NO PACTNO,B.ORD_NO||B.ORD_SEQ ORDERNO,A.CUST_CD BUYERCODE,A.ORDCUST_CD INCEPTCORPCODE,T.INTIME, "
                 + "(SELECT BUYERCODE FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORD_NO||T.ORD_SEQ AND ISVALID='1')BUYERCODE1, "
                 + "(SELECT INCEPECODE FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORD_NO||T.ORD_SEQ AND ISVALID='1')INCEPECODE1,"
                 + "(SELECT RECODER FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORD_NO||T.ORD_SEQ AND ISVALID='1')RECODER,"
                 + "(SELECT PACTNOLX FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORD_NO||T.ORD_SEQ AND ISVALID='1')PACTNOLX,"
                 + "(SELECT TO_CHAR(RECODERTIME,'YYYY-MM-DD HH24:MI') FROM KCJ_PACTNOLX_ZW WHERE ORDERNO=T.ORD_NO||T.ORD_SEQ AND ISVALID='1')RECODERTIME  "
                 + "FROM KCJ3_STUFFLIST_ZW T ,TBA01_ORD_COMM A,TBA01_ORD_LINE B WHERE T.ORD_NO=B.ORD_NO AND T.ORD_SEQ=B.ORD_SEQ AND A.ORD_NO=B.ORD_NO)"
                 + strWhere
                 + " GROUP BY ORDERNO,RECODER,PACTNOLX,RECODERTIME ";
            ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet",
                    new object[] { sql }, out strOut);
            if (ds != null)
            {
                ultraGrid1.DataSource = ds;
            }
        }
        public void Add()
        {
            try
            {
                string strErr = "";
                if (this.ultrCmb_PactNo.Text.Trim() == "")
                {
                    MessageBox.Show("请选择合同类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                ArrayList alorderno = new ArrayList();
                foreach (Infragistics.Win.UltraWinGrid.UltraGridRow row in this.ultraGrid1.Selected.Rows)
                {
                    alorderno.Add(row.Cells["ORDERNO"].Value.ToString());
                }
                ArrayList al = new ArrayList();
                al.Add(ultrCmb_PactNo.Value.ToString());
                al.Add(qBuyer.Value.ToString());
                al.Add(qIncep.Value.ToString());
                al.Add(uCob_LX.Value.ToString());
                al.Add(ClientCommon._UserInfo.UserName.ToString());

                DataSet ds = (DataSet)Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.SendManager", "addDJ", new object[] { alorderno, al }, out strErr);
                if (strErr != "")
                {
                    MessageBox.Show(strErr);
                    return;
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
        private void DoExc(Infragistics.Win.UltraWinGrid.UltraGrid _gd, string _title)
        {
            try
            {
                string filename = _title + @".xls";
                this.excelExporter.Export(_gd, filename);
                System.Diagnostics.ProcessStartInfo st = new System.Diagnostics.ProcessStartInfo(filename);
                st.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                System.Diagnostics.Process.Start(st);
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
        private void ultraToolbars_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            { 
                case "Confirm":
                    Query();
                    break;
                case "Add":
                    Add();
                    break;
                case "Close":
                    this.Close();
                    break;
                case "Excel":
                    DoExc(this.ultraGrid1,this.Text);
                   
                    break;
            }
        }

        private void chk_Intime_CheckedChanged(object sender, EventArgs e)
        {
            dte_TimeB.Enabled = dte_TimeE.Enabled = chk_Intime.Checked;
        }

        private void checkPactNo_CheckedChanged(object sender, EventArgs e)
        {
            PactNo.Enabled = checkPactNo.Checked;
        }

        private void ultraGrid1_AfterRowActivate(object sender, EventArgs e)
        {
            if (this.ultraGrid1.Rows.Count > 0)
            {
                ultrCmb_PactNo.Value = Comm.ObjToStr(this.ultraGrid1.ActiveRow.Cells["PACTNOLX"].Value);
                qBuyer.Value = Comm.ObjToStr(this.ultraGrid1.ActiveRow.Cells["BUYERCODE"].Value);
                qIncep.Value = Comm.ObjToStr(this.ultraGrid1.ActiveRow.Cells["INCEPTCORPCODE"].Value);
            }
        }
    }
}