Ejemplo n.º 1
0
    /// <summary>
    /// BUTTON [資料匯出]鈕
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_tab1_Out_Click(object sender, EventArgs e)
    {
        try
        {
            #region 檢查條件

            BCO.CAACommon CAAComm = new BCO.CAACommon();
            ArrayList arl_Check_Condition = Check_Condition("TAB1");

            #region 如果檢查有誤,則Return

            if (arl_Check_Condition[1].ToString() != string.Empty)
            {
                #region 錯誤訊息

                this.ErrorMsgLabel.Text = arl_Check_Condition[1].ToString();

                #endregion

                #region Focus欄位

                if (arl_Check_Condition[0].ToString() != string.Empty)
                {
                    string s_ScriptManager_Script = CAAComm.ToMakeUp_SetFocus_Script(arl_Check_Condition[0].ToString(), true);
                    ScriptManager.RegisterStartupScript(this.up_Msg, typeof(UpdatePanel), "CAA051", s_ScriptManager_Script, true);
                }

                #endregion

                return;
            }

            #endregion

            #endregion

            #region 傳入參數

            #region 處理起迄年月

            string s_Date_B = string.Empty;
            string s_Date_E = string.Empty;

            s_Date_B = DateTime.ParseExact(this.slp_tab1_DATE_B.Text + "01 000000", "yyyyMMdd HHmmss", null).ToString("yyyy/MM/dd HH:mm:ss");

            if (this.slp_tab1_DATE_E.Text == "999912")
            { s_Date_E = "9999/12/31 23:59:59"; }
            else
            { s_Date_E = DateTime.ParseExact(this.slp_tab1_DATE_E.Text + "01 235959", "yyyyMMdd HHmmss", null).AddMonths(1).AddDays(-1).ToString("yyyy/MM/dd HH:mm:ss"); }

            #endregion

            ParameterList.Clear();
            ParameterList.Add(CAAComm.GetValueSetParameter(s_Date_B, "datetime", false));//[起迄年月]起
            ParameterList.Add(CAAComm.GetValueSetParameter(s_Date_E, "datetime", false));//[起迄年月]迄
            ParameterList.Add(CAAComm.GetValueSetParameter(this.slp_tab1_DATE_B.Text, "string", false));//[起迄年月]起
            ParameterList.Add(CAAComm.GetValueSetParameter(this.slp_tab1_DATE_E.Text, "string", false));//[起迄年月]迄
            ParameterList.Add(CAAComm.GetValueSetParameter(Session["UID"].ToString(), "string", false));

            #endregion

            #region 連結資料庫

            DataSet ds_Return = new DataSet();

            BCO.ProcessInvRecord bco = new BCO.ProcessInvRecord(ConntionDB);
            ds_Return = bco.QueryExportFile(ParameterList);

            #endregion

            #region 檢查回傳資料

            if (ds_Return.Tables["INV_TRACK"].Rows.Count == 0)
            {
                this.ErrorMsgLabel.Text = "查無字軌檔資料";
                return;
            }

            if (ds_Return.Tables["EXPORT_DATA"].Rows.Count == 0)
            {
                this.ErrorMsgLabel.Text = "查無匯出資料";
                return;
            }

            #endregion

            #region 整理匯出資料

            #region 宣告變數

            System.Text.StringBuilder sb_Out_String = new System.Text.StringBuilder();
            DataView dv_EXPORT_DATA = new DataView(ds_Return.Tables["EXPORT_DATA"]);
            DataRow[] dr_EXPORT_INV_TRACK_Array = dv_EXPORT_DATA.ToTable(true, "DISTINCT_COLUMN").Select();//Distinct dv_DETL
            int i_Serial_Number = 0;//流水編號
            DateTime d_Now = DateTime.Now;

            #region 建立一個TempTable來記錄流水號資料

            DataTable dt_Serial_Number = new DataTable();

            dt_Serial_Number.Columns.Add("TYPE", typeof(string));
            dt_Serial_Number.Columns.Add("INV_NO", typeof(string));
            dt_Serial_Number.Columns.Add("DISC_NO", typeof(string));
            dt_Serial_Number.Columns.Add("SEQNO", typeof(string));
            dt_Serial_Number.Columns.Add("UPDATEDATE", typeof(DateTime));
            dt_Serial_Number.Columns.Add("UPDATEUID", typeof(string));

            #endregion

            #endregion

            foreach (DataRow dr_EXPORT_INV_TRACK in dr_EXPORT_INV_TRACK_Array)
            {
                string s_Filter_Expression = string.Empty;
                s_Filter_Expression = "DISTINCT_COLUMN = '" + dr_EXPORT_INV_TRACK["DISTINCT_COLUMN"].ToString() + "'";
                DataRow[] dr_EXPORT_DATA_Array = ds_Return.Tables["EXPORT_DATA"].Select(s_Filter_Expression);

                for (int i = 0; i < dr_EXPORT_DATA_Array.Length; i++)
                {
                    #region 檢查是否為連續號碼

                    //如果是第一筆資料或是折讓單資料則不必檢查是否為連續號碼
                    if (i != 0 &&
                        dr_EXPORT_DATA_Array[i]["DISTINCT_COLUMN"].ToString() != "DISC")
                    {
                        #region 取出發票扣除字軌後八碼數字

                        int i_Previous_NO = 0;//上一筆發票後八碼
                        int i_NO = 0;//這一筆發票後八碼

                        i_Previous_NO = int.Parse(dr_EXPORT_DATA_Array[i - 1]["INV_NO"].ToString().Substring(2, 8));
                        i_NO = int.Parse(dr_EXPORT_DATA_Array[i]["INV_NO"].ToString().Substring(2, 8));

                        #endregion

                        #region 如果不是連續號碼

                        if (i_NO - i_Previous_NO != 1)
                        {
                            sb_Out_String.AppendLine();
                            sb_Out_String.Append(dr_EXPORT_DATA_Array[i - 1]["FORM"].ToString());//格式代號
                            sb_Out_String.Append("111415765");//申報營業人稅籍編號
                            sb_Out_String.Append(Convert.ToString(i_Serial_Number + 1).PadLeft(7, '0'));//流水號
                            sb_Out_String.Append(dr_EXPORT_DATA_Array[i - 1]["DATE_RANGE"].ToString());//年月
                            sb_Out_String.Append(Convert.ToString(i_NO - 1).PadLeft(8, '0'));//發票迄號
                            sb_Out_String.Append("27741787");//高見統編
                            sb_Out_String.Append(dr_EXPORT_INV_TRACK["DISTINCT_COLUMN"].ToString().Substring(0, 2) + Convert.ToString(i_Previous_NO + 1).PadLeft(8, '0'));//銷項發票
                            sb_Out_String.Append(string.Empty.PadLeft(12, '0'));//銷售額(12個0)
                            sb_Out_String.Append("D");//課稅別
                            sb_Out_String.Append(string.Empty.PadLeft(10, '0'));//稅額(10個0)
                            sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//扣抵代號(1個空白)
                            sb_Out_String.Append(string.Empty.PadLeft(5, ' '));//空白(5個空白)
                            sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//特種稅額稅率(1個空白)
                            sb_Out_String.Append("A");//彙加註記
                            sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//洋菸酒註記(1個空白)

                            i_Serial_Number += 1;//流水號加1
                        }

                        #endregion
                    }

                    #endregion

                    #region 寫入資料

                    #region 取得稅別

                    //20090921秀玲更改規則,
                    //文字檔傳入的課稅別為,1:應稅,2:零稅率,3:免稅
                    //高見系統的課稅別為  ,0:應稅,2:零稅率,1:免稅
                    string s_TAX_TYPE = string.Empty;

                    if (dr_EXPORT_DATA_Array[i]["TAX_TYPE"].ToString() == "0")
                    { s_TAX_TYPE = "1"; }
                    else if (dr_EXPORT_DATA_Array[i]["TAX_TYPE"].ToString() == "2")
                    { s_TAX_TYPE = "2"; }
                    else if (dr_EXPORT_DATA_Array[i]["TAX_TYPE"].ToString() == "1")
                    { s_TAX_TYPE = "3"; }
                    else
                    { s_TAX_TYPE = dr_EXPORT_DATA_Array[i]["TAX_TYPE"].ToString(); }

                    #endregion

                    if (i_Serial_Number != 0)
                    { sb_Out_String.AppendLine(); }//第一行不必空一行
                    sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["FORM"].ToString());//格式代號
                    sb_Out_String.Append("111415765");//申報營業人稅籍編號
                    sb_Out_String.Append(Convert.ToString(i_Serial_Number + 1).PadLeft(7, '0'));//流水號
                    sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["DATE_RANGE"].ToString());//年月
                    sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["VOUCH_RFNO"].ToString().PadLeft(8, ' ').Substring(0, 8));//客戶統編
                    sb_Out_String.Append("27741787");//高見統編
                    sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["INV_NO"].ToString());//銷項發票
                    sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["UAMT"].ToString().PadLeft(12, '0'));//銷售額
                    sb_Out_String.Append(s_TAX_TYPE);//課稅別

                    sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["TAX"].ToString().PadLeft(10, '0'));//稅額
                    sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//扣抵代號(1個空白)
                    sb_Out_String.Append(string.Empty.PadLeft(5, ' '));//空白(5個空白)
                    sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//特種稅額稅率(1個空白)
                    sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//彙加註記(1個空白)
                    sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//洋菸酒註記(1個空白)

                    #region 記錄流水編號

                    DataRow dr_Serial_Number = dt_Serial_Number.NewRow();

                    dr_Serial_Number["TYPE"] = (dr_EXPORT_DATA_Array[i]["DISTINCT_COLUMN"].ToString() == "DISC") ? "DISC" : "INV";
                    dr_Serial_Number["INV_NO"] = dr_EXPORT_DATA_Array[i]["INV_NO"].ToString();
                    dr_Serial_Number["DISC_NO"] = dr_EXPORT_DATA_Array[i]["DISC_NO"].ToString();
                    dr_Serial_Number["SEQNO"] = Convert.ToString(i_Serial_Number + 1).PadLeft(7, '0');
                    dr_Serial_Number["UPDATEDATE"] = d_Now;
                    dr_Serial_Number["UPDATEUID"] = Session["UID"].ToString();

                    dt_Serial_Number.Rows.Add(dr_Serial_Number);

                    #endregion

                    i_Serial_Number += 1;//流水號加1

                    #endregion
                    
                    #region 如果是最後一筆

                    if (i == dr_EXPORT_DATA_Array.Length - 1 &&
                        dr_EXPORT_DATA_Array[i]["DISTINCT_COLUMN"].ToString() != "DISC")
                    {
                        #region 檢查字軌檔最後一筆資料,與抓出的最後一筆資料是否相符

                        string s_Filter_Expression_INV_TRACK = string.Empty;
                        string s_INVOICE_TRACK = string.Empty;
                        string s_INVOICE_MONTH = string.Empty;
                        string s_INVOICE_TYPE = string.Empty;

                        s_INVOICE_TRACK = dr_EXPORT_INV_TRACK["DISTINCT_COLUMN"].ToString().Substring(0, 2);
                        s_INVOICE_MONTH = dr_EXPORT_INV_TRACK["DISTINCT_COLUMN"].ToString().Substring(2, 6);
                        s_INVOICE_TYPE = dr_EXPORT_INV_TRACK["DISTINCT_COLUMN"].ToString().Substring(8, 1);

                        s_Filter_Expression_INV_TRACK = "INVOICE_TRACK = '" + s_INVOICE_TRACK + "' and INVOICE_MONTH = '" + s_INVOICE_MONTH + "' and INVOICE_TYPE = '"+ s_INVOICE_TYPE+ "'";
                        DataRow[] dr_INV_TRACK_Array = ds_Return.Tables["INV_TRACK"].Select(s_Filter_Expression_INV_TRACK);

                        if (dr_INV_TRACK_Array.Length == 0)
                        {
                            this.ErrorMsgLabel.Text = "發票字軌檔無字軌:" + s_INVOICE_TRACK + "的資料";
                            return;
                        }

                        if (dr_INV_TRACK_Array[0]["INVOICE_TRACK"].ToString() + dr_INV_TRACK_Array[0]["CURRENT_NO"].ToString() != dr_EXPORT_DATA_Array[i]["INV_NO"].ToString())
                        {
                            this.ErrorMsgLabel.Text = "發票字軌檔最後一筆發票號碼:" + dr_INV_TRACK_Array[0]["INVOICE_TRACK"].ToString() + dr_INV_TRACK_Array[0]["CURRENT_NO"].ToString() + "與發票主檔最後一筆發票號碼:" + dr_EXPORT_DATA_Array[i]["INV_NO"].ToString() + "不符";
                            return;
                        }

                        #endregion

                        #region 確認為最後一筆

                        else
                        {
                            //如果[目前使用號碼]&[迄止號碼]不相等,才必須做以下動作
                            if (dr_INV_TRACK_Array[0]["CURRENT_NO"].ToString() != dr_INV_TRACK_Array[0]["END_NO"].ToString())
                            {
                                int i_NO = 0;//這一筆發票後八碼
                                i_NO = int.Parse(dr_EXPORT_DATA_Array[i]["INV_NO"].ToString().Substring(2, 8));

                                sb_Out_String.AppendLine();
                                sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["FORM"].ToString());//格式代號
                                sb_Out_String.Append("111415765");//申報營業人稅籍編號
                                sb_Out_String.Append(Convert.ToString(i_Serial_Number + 1).PadLeft(7, '0'));//流水號
                                sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["DATE_RANGE"].ToString());//年月
                                sb_Out_String.Append(dr_INV_TRACK_Array[0]["END_NO"].ToString().PadLeft(8, '0'));//發票迄號
                                sb_Out_String.Append("27741787");//高見統編
                                sb_Out_String.Append(dr_EXPORT_DATA_Array[i]["INV_NO"].ToString().Substring(0, 2) + Convert.ToString(i_NO + 1).PadLeft(8, '0'));//銷項發票
                                sb_Out_String.Append(string.Empty.PadLeft(12, '0'));//銷售額(12個0)
                                sb_Out_String.Append("D");//課稅別
                                sb_Out_String.Append(string.Empty.PadLeft(10, '0'));//稅額(10個0)
                                sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//扣抵代號(1個空白)
                                sb_Out_String.Append(string.Empty.PadLeft(5, ' '));//空白(5個空白)
                                sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//特種稅額稅率(1個空白)
                                sb_Out_String.Append("A");//彙加註記
                                sb_Out_String.Append(string.Empty.PadLeft(1, ' '));//洋菸酒註記(1個空白)

                                i_Serial_Number += 1;//流水號加1
                            }
                        }

                        #endregion
                    }

                    #endregion
                }
            }

            #endregion

            #region 將流水號資料寫入資料庫

            bco.UPDATE_CAA05_SEQNO(null, dt_Serial_Number);

            #endregion

            #region 將資料匯出成TXT檔

            string s_FileName = "CAA051_[" + this.slp_tab1_DATE_E.Text + "-27741787].txt";

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "BIG5";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(s_FileName));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("BIG5");
            Response.ContentType = "application/ms-txt";
            this.EnableViewState = false;
            Response.Write(sb_Out_String.ToString());
            Response.End();

            #endregion
        }
        catch (Exception ex)
        {
            WaringLogProcess(ex.Message);
            this.ErrorMsgLabel.Text = ex.Message;
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// BUTTON [查詢明細]鈕
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_tab1_Query_Detail_Click(object sender, EventArgs e)
    {
        try
        {
            #region 檢查條件

            BCO.CAACommon CAAComm = new BCO.CAACommon();
            ArrayList arl_Check_Condition = Check_Condition("TAB1");

            #region 如果檢查有誤,則Return

            if (arl_Check_Condition[1].ToString() != string.Empty)
            {
                #region 錯誤訊息

                this.ErrorMsgLabel.Text = arl_Check_Condition[1].ToString();

                #endregion

                #region Focus欄位

                if (arl_Check_Condition[0].ToString() != string.Empty)
                {
                    string s_ScriptManager_Script = CAAComm.ToMakeUp_SetFocus_Script(arl_Check_Condition[0].ToString(), true);
                    ScriptManager.RegisterStartupScript(this.up_Msg, typeof(UpdatePanel), "CAA051", s_ScriptManager_Script, true);
                }

                #endregion

                return;
            }

            #endregion

            #endregion

            #region 傳入參數

            #region 處理起迄年月

            string s_Date_B = string.Empty;
            string s_Date_E = string.Empty;

            s_Date_B = DateTime.ParseExact(this.slp_tab1_DATE_B.Text + "01 000000", "yyyyMMdd HHmmss", null).ToString("yyyy/MM/dd HH:mm:ss");

            if (this.slp_tab1_DATE_E.Text == "999912")
            { s_Date_E = "9999/12/31 23:59:59"; }
            else
            { s_Date_E = DateTime.ParseExact(this.slp_tab1_DATE_E.Text + "01 235959", "yyyyMMdd HHmmss", null).AddMonths(1).AddDays(-1).ToString("yyyy/MM/dd HH:mm:ss"); }

            #endregion

            ParameterList.Clear();
            ParameterList.Add(CAAComm.GetValueSetParameter(s_Date_B, "datetime", false));//[起迄年月]起
            ParameterList.Add(CAAComm.GetValueSetParameter(s_Date_E, "datetime", false));//[起迄年月]迄
            ParameterList.Add(CAAComm.GetValueSetParameter(this.slp_tab1_DATE_B.Text, "string", false));//[起迄年月]起
            ParameterList.Add(CAAComm.GetValueSetParameter(this.slp_tab1_DATE_E.Text, "string", false));//[起迄年月]迄
            ParameterList.Add(CAAComm.GetValueSetParameter((this.txt_RowCountLimit.Text == string.Empty) ? "100" : (int.Parse(this.txt_RowCountLimit.Text) < 0) ? "100" : this.txt_RowCountLimit.Text, "int", false));//[筆數]
            ParameterList.Add(CAAComm.GetValueSetParameter(Session["UID"].ToString(), "string", false));

            #endregion

            #region 連結資料庫

            DataTable dt_Return = new DataTable();

            BCO.ProcessInvRecord bco = new BCO.ProcessInvRecord(ConntionDB);
            dt_Return = bco.QueryPeriodDetail(ParameterList);

            #endregion

            #region 檢查回傳資料

            if (dt_Return.Rows.Count == 0)
            {
                this.ErrorMsgLabel.Text = "查無資料";
                this.gv_tab1_Gather.DataSource = null;
                this.gv_tab1_Detail.DataSource = null;
                this.gv_tab1_Gather.DataBind();
                this.gv_tab1_Detail.DataBind();
                return;
            }

            #endregion

            #region 資料與GridView繫結

            //抓取本頁初次登記的時間
            string SessionIDName = "CAA051_Detail_" + PageTimeStamp.Value;

            Session["SessionID"] = SessionIDName;
            Session[SessionIDName] = dt_Return;

            this.gv_tab1_Detail.DataSource = dt_Return;
            this.gv_tab1_Gather.DataSource = null;
            this.gv_tab1_Detail.PageSize = (this.txt_Pagesize.Text == string.Empty) ? 20 : (int.Parse(this.txt_Pagesize.Text) < 0) ? 20 : int.Parse(this.txt_Pagesize.Text);
            this.gv_tab1_Detail.PageIndex = 0;
            this.gv_tab1_Detail.DataBind();
            this.gv_tab1_Gather.DataBind();

            #endregion
        }
        catch (Exception ex)
        {
            WaringLogProcess(ex.Message);
            this.ErrorMsgLabel.Text = ex.Message;
        }
    }