diff --git a/web/admin/order/print.aspx.cs b/web/admin/order/print.aspx.cs index 473ad92..25d4256 100644 --- a/web/admin/order/print.aspx.cs +++ b/web/admin/order/print.aspx.cs @@ -14,212 +14,228 @@ public partial class admin_follower_print_ : System.Web.UI.Page List _detail = new List(); List _bedDt = new List(); Dictionary tdesc = new Dictionary(); + + // Request 參數變數 + private string _orderNo; + private string _subject; + private string _uName; + private string _introducer; + private string _actItem; + private string _keyin1; + private string _address; + private string _upTime1; + private string _upTime2; + private string _country; + private string _country2; + private string _hasPrice; + private string _year; + private string _month; + private string _season; + private string _chkHasAct; + private string _chkNoAct; + private string _selectAct; + private string _selectActItem; + protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { + // 初始化所有 Request 參數 + InitializeRequestParameters(); + MyWeb.admin admin = new MyWeb.admin(); - if ( admin.isLoign()) + if (admin.isLoign()) { - //紀錄匯出條件 string _query = ""; var qry = _db.pro_order.AsQueryable(); - // ❌ 錯誤寫法: qry = qry.Where(o => o.order_no.Contains(Request["order_no"].Trim())); - // LINQ to Entities 無法轉換 Request[] 方法,必須先轉換為變數再使用 - string orderNoParam = Request["order_no"]?.Trim(); - if (!string.IsNullOrEmpty(orderNoParam)) + if (!string.IsNullOrEmpty(_orderNo)) { - qry = qry.Where(o => o.order_no.Contains(orderNoParam)); - _query += "單號:" + orderNoParam + "\n"; + qry = qry.Where(o => o.order_no.Contains(_orderNo)); + _query += "單號:" + _orderNo + "\n"; } - string subjectParam = Request["subject"]?.Trim(); - if (!string.IsNullOrEmpty(subjectParam)) + if (!string.IsNullOrEmpty(_subject)) { - qry = qry.Where(o => o.activity_num.HasValue && o.activity.subject.Contains(subjectParam)); - _query += "報名活動:" + subjectParam + "\n"; + qry = qry.Where(o => o.activity_num.HasValue && o.activity.subject.Contains(_subject)); + _query += "報名活動:" + _subject + "\n"; } - string uNameParam = Request["u_name"]?.Trim(); - if (!string.IsNullOrEmpty(uNameParam)) + if (!string.IsNullOrEmpty(_uName)) { - qry = qry.Where(o => o.f_num.HasValue && o.follower.u_name.Contains(uNameParam)); - _query += "姓名/名稱:" + uNameParam + "\n"; + qry = qry.Where(o => o.f_num.HasValue && o.follower.u_name.Contains(_uName)); + _query += "姓名/名稱:" + _uName + "\n"; } - string introducerParam = Request["introducerTxt"]?.Trim(); - if (!string.IsNullOrEmpty(introducerParam)) + if (!string.IsNullOrEmpty(_introducer)) { - qry = qry.Where(o => o.introducer.HasValue && o.follower1.u_name.Contains(introducerParam)); - _query += "介紹人:" + introducerParam + "\n"; + qry = qry.Where(o => o.introducer.HasValue && o.follower1.u_name.Contains(_introducer)); + _query += "介紹人:" + _introducer + "\n"; } - string actItemParam = Request["actItemTxt"]?.Trim(); - if (!string.IsNullOrEmpty(actItemParam)) + if (!string.IsNullOrEmpty(_actItem)) { - qry = qry.Where(o => o.pro_order_detail.Where(f2 => f2.actItem_num.HasValue && f2.actItem.subject.Contains(actItemParam)).Count() > 0); - _query += "品項:" + actItemParam + "\n"; - } - if (!string.IsNullOrEmpty(Request["keyin1"])) - { - // ❌ 錯誤寫法: qry = qry.Where(o => o.keyin1==Request["keyin1"].ToString()); - // LINQ to Entities 無法轉換 Request[].ToString(),必須先轉換為變數再使用 - string keyin1Value = Request["keyin1"]?.ToString(); - qry = qry.Where(o => o.keyin1 == keyin1Value); - _query += "單據狀態:" + Model.pro_order.keyin1_value_to_text( Request["keyin1"].Trim()) + "\n"; - } - string addressParam = Request["address"]?.Trim(); - if (!string.IsNullOrEmpty(addressParam)) - { - qry = qry.Where(o => o.address.Contains(addressParam)); - _query += "地址:" + addressParam + "\n"; + qry = qry.Where(o => o.pro_order_detail.Where(f2 => f2.actItem_num.HasValue && f2.actItem.subject.Contains(_actItem)).Count() > 0); + _query += "品項:" + _actItem + "\n"; } - if (!string.IsNullOrEmpty(Request["up_time1"])) + if (!string.IsNullOrEmpty(_keyin1)) { - DateTime upTime1Param = Convert.ToDateTime(Request["up_time1"].Trim()); - qry = qry.Where(o => o.up_time >= upTime1Param); - _query += "報名日期(起):" + upTime1Param.ToString("yyyy/MM/dd") + "\n"; + qry = qry.Where(o => o.keyin1 == _keyin1); + _query += "單據狀態:" + Model.pro_order.keyin1_value_to_text(_keyin1) + "\n"; } - if (!string.IsNullOrEmpty(Request["up_time2"])) + if (!string.IsNullOrEmpty(_address)) { - DateTime upTime2Param = Convert.ToDateTime(Request["up_time2"].Trim()); - qry = qry.Where(o => o.up_time < upTime2Param.AddDays(1)); - _query += "報名日期(訖):" + upTime2Param.ToString("yyyy/MM/dd") + "\n"; + qry = qry.Where(o => o.address.Contains(_address)); + _query += "地址:" + _address + "\n"; } - string countryParam = Request["country"]?.ToString(); - if (!string.IsNullOrEmpty(countryParam)) + if (!string.IsNullOrEmpty(_upTime1)) { - qry = qry.Where(o => o.f_num != null && o.follower.country == countryParam); - _query += "國家:" + (_db.countries.Where(x => x.ID == countryParam).Select(x => x.name_zh).FirstOrDefault() ?? "") + "\n"; + DateTime upTime1 = Convert.ToDateTime(_upTime1); + qry = qry.Where(o => o.up_time >= upTime1); + _query += "報名日期(起):" + upTime1.ToString("yyyy/MM/dd") + "\n"; } - string country2Param = Request["country2"]?.ToString(); - if (!string.IsNullOrEmpty(country2Param)) + if (!string.IsNullOrEmpty(_upTime2)) { - if (country2Param == "1") + DateTime upTime2 = Convert.ToDateTime(_upTime2); + qry = qry.Where(o => o.up_time < upTime2.AddDays(1)); + _query += "報名日期(訖):" + upTime2.ToString("yyyy/MM/dd") + "\n"; + } + + if (!string.IsNullOrEmpty(_country)) + { + qry = qry.Where(o => o.f_num != null && o.follower.country == _country); + _query += "國家:" + (_db.countries.Where(x => x.ID == _country).Select(x => x.name_zh).FirstOrDefault() ?? "") + "\n"; + } + + if (!string.IsNullOrEmpty(_country2)) + { + if (_country2 == "1") { qry = qry.Where(o => o.f_num != null && o.follower.country == "158"); } - else if (country2Param == "2") + else if (_country2 == "2") { qry = qry.Where(o => o.f_num != null && o.follower.country != "158"); } - _query += "國家:" + (_db.countries.Where(x => x.ID == country2Param).Select(x => x.name_zh).FirstOrDefault() ?? "") + "\n"; + _query += "國家:" + (_country2 == "1" ? "台灣" : _country2 == "2" ? "非台灣" : "") + "\n"; } - if (!string.IsNullOrEmpty(Request["hasPrice"])) + + if (!string.IsNullOrEmpty(_hasPrice)) { - if (Request["hasPrice"].ToString() == "Y") + if (_hasPrice == "Y") { _query += "有金額\n"; } - else if (Request["hasPrice"].ToString() == "N") + else if (_hasPrice == "N") { _query += "無金額\n"; } } //管理報表 - if (!string.IsNullOrEmpty(Request["year"])) + if (!string.IsNullOrEmpty(_year)) { title.Text = "報名管理報表"; - int yearParam = Convert.ToInt32(Request["year"]); - qry = qry.Where(o => o.up_time.HasValue && o.up_time.Value.Year == yearParam); - _query += "年份:" + Request["year"] + "\n"; + int year = Convert.ToInt32(_year); + qry = qry.Where(o => o.up_time.HasValue && o.up_time.Value.Year == year); + _query += "年份:" + _year + "\n"; } - if (!string.IsNullOrEmpty(Request["month"])) + if (!string.IsNullOrEmpty(_month)) { - int monthParam = Convert.ToInt32(Request["month"]); - qry = qry.Where(o => o.up_time.HasValue && o.up_time.Value.Month == monthParam); - _query += "月份:" + Request["month"] + "\n"; + int month = Convert.ToInt32(_month); + qry = qry.Where(o => o.up_time.HasValue && o.up_time.Value.Month == month); + _query += "月份:" + _month + "\n"; } - if (!string.IsNullOrEmpty(Request["season"])) + + if (!string.IsNullOrEmpty(_season)) { - - if (Request["season"] == "1") + if (_season == "1") { qry = qry.Where(o => o.up_time.HasValue) .Where(o => o.up_time.Value.Month == 1 || o.up_time.Value.Month == 2 || o.up_time.Value.Month == 3); } - else if (Request["season"] == "2") + else if (_season == "2") { qry = qry.Where(o => o.up_time.HasValue) .Where(o => o.up_time.Value.Month == 4 || o.up_time.Value.Month == 5 || o.up_time.Value.Month == 6); } - else if (Request["season"] == "3") + else if (_season == "3") { qry = qry.Where(o => o.up_time.HasValue) .Where(o => o.up_time.Value.Month == 7 || o.up_time.Value.Month == 8 || o.up_time.Value.Month == 9); } - else if (Request["season"] == "4") + else if (_season == "4") { qry = qry.Where(o => o.up_time.HasValue) .Where(o => o.up_time.Value.Month == 10 || o.up_time.Value.Month == 11 || o.up_time.Value.Month == 12); } - _query += "季度:" + Request["season"] + "\n"; + _query += "季度:" + _season + "\n"; } - if (!string.IsNullOrEmpty(Request["chk_hasact"]) && Convert.ToBoolean(Request["chk_hasact"])) + + if (!string.IsNullOrEmpty(_chkHasAct) && Convert.ToBoolean(_chkHasAct)) { _query += "活動報名\n"; - if (!string.IsNullOrEmpty(Request["chk_noact"]) && Convert.ToBoolean(Request["chk_noact"])) + + if (!string.IsNullOrEmpty(_chkNoAct) && Convert.ToBoolean(_chkNoAct)) { _query += "非活動報名\n"; - if (!string.IsNullOrEmpty(Request["select_act"])) + if (!string.IsNullOrEmpty(_selectAct)) { - int selectActParam = Convert.ToInt32(Request["select_act"]); - if (selectActParam > 0) + int selectAct = Convert.ToInt32(_selectAct); + if (selectAct > 0) { - qry = qry.Where(o => o.activity_num.HasValue && o.activity_num.Value == selectActParam); + qry = qry.Where(o => o.activity_num.HasValue && o.activity_num.Value == selectAct); } } } else { qry = qry.Where(o => o.activity_num.HasValue); - if (!string.IsNullOrEmpty(Request["select_act"])) + + if (!string.IsNullOrEmpty(_selectAct)) { - int selectActParam = Convert.ToInt32(Request["select_act"]); - if (selectActParam > 0) + int selectAct = Convert.ToInt32(_selectAct); + if (selectAct > 0) { - qry = qry.Where(o => o.activity_num.Value == selectActParam); + qry = qry.Where(o => o.activity_num.Value == selectAct); } } } - } else { - if (!string.IsNullOrEmpty(Request["chk_noact"]) && Convert.ToBoolean(Request["chk_noact"])) + if (!string.IsNullOrEmpty(_chkNoAct) && Convert.ToBoolean(_chkNoAct)) { qry = qry.Where(o => o.activity_num == null); _query += "非活動報名\n"; } } - if (!string.IsNullOrEmpty(Request["select_actitem"])) + if (!string.IsNullOrEmpty(_selectActItem)) { - int selectActItemParam = Convert.ToInt32(Request["select_actitem"]); - if (selectActItemParam > 0) + int selectActItem = Convert.ToInt32(_selectActItem); + if (selectActItem > 0) { - qry = qry.Where(o => o.pro_order_detail.Where(f2 => f2.actItem_num.HasValue && f2.actItem_num.Value == selectActItemParam).Count() > 0); + qry = qry.Where(o => o.pro_order_detail.Where(f2 => f2.actItem_num.HasValue && f2.actItem_num.Value == selectActItem).Count() > 0); } } - if (!string.IsNullOrEmpty(Request["year"])) - qry = qry.OrderByDescending(o => o.activity != null ? o.activity.startDate_solar : null).ThenByDescending(o=>o.up_time).ThenByDescending(o=>o.order_no); + if (!string.IsNullOrEmpty(_year)) + qry = qry.OrderByDescending(o => o.activity != null ? o.activity.startDate_solar : null).ThenByDescending(o => o.up_time).ThenByDescending(o => o.order_no); else qry = qry.OrderByDescending(o => o.order_no); //紀錄匯出條件 - var prod= qry.ToList(); + var prod = qry.ToList(); if (prod.Count > 0) { tdesc = publicFun.enum_desc(); @@ -234,14 +250,10 @@ public partial class admin_follower_print_ : System.Web.UI.Page Model.admin_log admin_log = new Model.admin_log(); admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Order, (int)Model.admin_log.Status.Print, admin_log.LogViewBtn(prod.Select(x => x.order_no).ToList())); - } else { PlaceHolder1.Visible = true; - //Response.Clear(); - //Response.StatusCode = 404; - //Response.End(); } string _data = "列印時間 : " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); @@ -249,91 +261,194 @@ public partial class admin_follower_print_ : System.Web.UI.Page _data += "
列印條件 : " + (!string.IsNullOrEmpty(_query) ? _query : "-"); footer.Text = _data; - if (Request["hasPrice"].ToString() == "Y") + if (!string.IsNullOrEmpty(_hasPrice) && _hasPrice == "Y" && Repeater1.Items.Count > 0) { - ((Panel)Repeater1.Items[Repeater1.Items.Count - 1].FindControl("hr")).Visible = false; + Panel hrPanel = (Panel)Repeater1.Items[Repeater1.Items.Count - 1].FindControl("hr"); + if (hrPanel != null) + { + hrPanel.Visible = false; + } int _total = 0; int _item_qty = 0; foreach (RepeaterItem gItem in Repeater1.Items) { Repeater rpt = (Repeater)gItem.FindControl("Repeater2"); - foreach (RepeaterItem rItem in rpt.Items) + if (rpt != null) { - Literal item_price = (Literal)rItem.FindControl("item_price"); - Literal item_qty = (Literal)rItem.FindControl("item_qty"); + foreach (RepeaterItem rItem in rpt.Items) + { + Literal item_price = (Literal)rItem.FindControl("item_price"); + Literal item_qty = (Literal)rItem.FindControl("item_qty"); - _total += Convert.ToInt32(item_price.Text) * Convert.ToInt32(item_qty.Text); - _item_qty += Convert.ToInt32(item_qty.Text); + int price = 0; + int qty = 0; + + if (item_price != null && !string.IsNullOrEmpty(item_price.Text)) + { + int.TryParse(item_price.Text, out price); + } + + if (item_qty != null && !string.IsNullOrEmpty(item_qty.Text)) + { + int.TryParse(item_qty.Text, out qty); + } + + _total += price * qty; + _item_qty += qty; + } } - } _data = "報名單筆數 : " + Repeater1.Items.Count; _data += "項目數量合計 : " + _item_qty; - _data += "金額合計 : $" + _total; + _data += "金額合計 : $" + _total; count_data.Text = _data; count_data2.Text = _data; } - } else { Response.Clear(); Response.StatusCode = 404; Response.End(); - } - } } + /// + /// 初始化所有 Request 參數 + /// + private void InitializeRequestParameters() + { + _orderNo = Request["order_no"]?.Trim(); + _subject = Request["subject"]?.Trim(); + _uName = Request["u_name"]?.Trim(); + _introducer = Request["introducerTxt"]?.Trim(); + _actItem = Request["actItemTxt"]?.Trim(); + _keyin1 = Request["keyin1"]?.Trim(); + _address = Request["address"]?.Trim(); + _upTime1 = Request["up_time1"]?.Trim(); + _upTime2 = Request["up_time2"]?.Trim(); + _country = Request["country"]?.Trim(); + _country2 = Request["country2"]?.Trim(); + _hasPrice = Request["hasPrice"]?.Trim(); + _year = Request["year"]?.Trim(); + _month = Request["month"]?.Trim(); + _season = Request["season"]?.Trim(); + _chkHasAct = Request["chk_hasact"]?.Trim(); + _chkNoAct = Request["chk_noact"]?.Trim(); + _selectAct = Request["select_act"]?.Trim(); + _selectActItem = Request["select_actitem"]?.Trim(); + } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { - Model.pro_order row = (Model.pro_order)e.Item.DataItem; + if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) + { + Model.pro_order row = (Model.pro_order)e.Item.DataItem; - ((Literal)e.Item.FindControl("keyin1")).Text = Model.pro_order.keyin1_value_to_text(row.keyin1); - ((Literal)e.Item.FindControl("f_numTxt")).Text = row.f_num.HasValue ? row.follower.u_name : ""; - ((Literal)e.Item.FindControl("activityTxt")).Text = row.activity_num.HasValue ? row.activity.subject : ""; + Literal keyin1Lit = (Literal)e.Item.FindControl("keyin1"); + if (keyin1Lit != null) + { + keyin1Lit.Text = Model.pro_order.keyin1_value_to_text(row.keyin1); + } - Repeater Repeater2 = (Repeater)e.Item.FindControl("Repeater2"); - var _ds = _detail.Where(x => x.order_no == row.order_no).ToList(); - if (_ds .Count>0) { - ((PlaceHolder)e.Item.FindControl("detailTable")).Visible = true; - Repeater2.DataSource = _ds; - Repeater2.DataBind(); + Literal fNumTxtLit = (Literal)e.Item.FindControl("f_numTxt"); + if (fNumTxtLit != null) + { + fNumTxtLit.Text = row.f_num.HasValue && row.follower != null ? row.follower.u_name : ""; + } - ((PlaceHolder)e.Item.FindControl("PlaceHolder2")).Visible = Request["hasPrice"].ToString() == "Y"; + Literal activityTxtLit = (Literal)e.Item.FindControl("activityTxt"); + if (activityTxtLit != null) + { + activityTxtLit.Text = row.activity_num.HasValue && row.activity != null ? row.activity.subject : ""; + } + Repeater Repeater2 = (Repeater)e.Item.FindControl("Repeater2"); + if (Repeater2 != null) + { + var _ds = _detail.Where(x => x.order_no == row.order_no).ToList(); + if (_ds.Count > 0) + { + PlaceHolder detailTable = (PlaceHolder)e.Item.FindControl("detailTable"); + if (detailTable != null) + { + detailTable.Visible = true; + } + + Repeater2.DataSource = _ds; + Repeater2.DataBind(); + + PlaceHolder ph2 = (PlaceHolder)e.Item.FindControl("PlaceHolder2"); + if (ph2 != null) + { + ph2.Visible = !string.IsNullOrEmpty(_hasPrice) && _hasPrice == "Y"; + } + } + } } - } protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e) { - Model.pro_order_detail row = (Model.pro_order_detail)e.Item.DataItem; + if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) + { + Model.pro_order_detail row = (Model.pro_order_detail)e.Item.DataItem; - Literal actitem_numTxt = (Literal)e.Item.FindControl("actitem_numTxt"); - actitem_numTxt.Text = row.actItem_num.HasValue ? row.actItem.subject : ""; + Literal actitem_numTxt = (Literal)e.Item.FindControl("actitem_numTxt"); + if (actitem_numTxt != null) + { + actitem_numTxt.Text = row.actItem_num.HasValue && row.actItem != null ? row.actItem.subject : ""; + } - ((Literal)e.Item.FindControl("f_numTxt")).Text = row.f_num.HasValue ? row.follower.u_name : ""; - ((Literal)e.Item.FindControl("from_idTxt")).Text = row.from_id.HasValue ? row.follower1.u_name : ""; + Literal fNumTxtLit = (Literal)e.Item.FindControl("f_numTxt"); + if (fNumTxtLit != null) + { + fNumTxtLit.Text = row.f_num.HasValue && row.follower != null ? row.follower.u_name : ""; + } - ((Literal)e.Item.FindControl("due_date")).Text = row.due_date.HasValue? row.due_date.Value.ToString("yyyy-MM-dd"):""; - // ((Literal)e.Item.FindControl("start_date")).Text = row.start_date.HasValue? row.start_date.Value.ToString("yyyy-MM-dd"):""; - ((Literal)e.Item.FindControl("extend_date")).Text = row.extend_date.HasValue? row.extend_date.Value.ToString("yyyy-MM-dd"):""; + Literal fromIdTxtLit = (Literal)e.Item.FindControl("from_idTxt"); + if (fromIdTxtLit != null) + { + fromIdTxtLit.Text = row.from_id.HasValue && row.follower1 != null ? row.follower1.u_name : ""; + } - //劃位狀態 - int writeBedQty = _bedDt.Where(b => b.bed_order.o_detail_id == row.num && b.checkIn_date.HasValue && b.bed_kind_detail_id.HasValue).Count(); //已劃數量 - int notBedQty = _bedDt.Where(b => b.bed_order.o_detail_id == row.num && (!b.checkIn_date.HasValue || !b.bed_kind_detail_id.HasValue)).Count(); //未劃數量 - ((Literal)e.Item.FindControl("BedQty")).Text = (Convert.ToInt32(row.actItem?.category) == (int)Model.activity.category.Order) ? (notBedQty + "/" + writeBedQty) : ""; + Literal dueDateLit = (Literal)e.Item.FindControl("due_date"); + if (dueDateLit != null) + { + dueDateLit.Text = row.due_date.HasValue ? row.due_date.Value.ToString("yyyy-MM-dd") : ""; + } - ((Literal)e.Item.FindControl("keyin1")).Text = row.keyin1.HasValue && row.keyin1.Value > 0 ? tdesc[row.keyin1 ?? 1] : ""; + Literal extendDateLit = (Literal)e.Item.FindControl("extend_date"); + if (extendDateLit != null) + { + extendDateLit.Text = row.extend_date.HasValue ? row.extend_date.Value.ToString("yyyy-MM-dd") : ""; + } - ((PlaceHolder)e.Item.FindControl("PlaceHolder2")).Visible = Request["hasPrice"].ToString() == "Y"; + //劃位狀態 + int writeBedQty = _bedDt.Where(b => b.bed_order != null && b.bed_order.o_detail_id == row.num && b.checkIn_date.HasValue && b.bed_kind_detail_id.HasValue).Count(); + int notBedQty = _bedDt.Where(b => b.bed_order != null && b.bed_order.o_detail_id == row.num && (!b.checkIn_date.HasValue || !b.bed_kind_detail_id.HasValue)).Count(); + + Literal bedQtyLit = (Literal)e.Item.FindControl("BedQty"); + if (bedQtyLit != null) + { + bedQtyLit.Text = (row.actItem != null && row.actItem.category.HasValue && Convert.ToInt32(row.actItem.category.Value) == (int)Model.activity.category.Order) ? (notBedQty + "/" + writeBedQty) : ""; + } + Literal keyin1Lit = (Literal)e.Item.FindControl("keyin1"); + if (keyin1Lit != null) + { + keyin1Lit.Text = row.keyin1.HasValue && row.keyin1.Value > 0 && tdesc.ContainsKey(row.keyin1.Value) ? tdesc[row.keyin1.Value] : ""; + } + PlaceHolder ph2 = (PlaceHolder)e.Item.FindControl("PlaceHolder2"); + if (ph2 != null) + { + ph2.Visible = !string.IsNullOrEmpty(_hasPrice) && _hasPrice == "Y"; + } + } } } \ No newline at end of file