SGGL_SHJ/SGGL/FineUIPro.Web/HSSE/CostGoods/ExpenseUnit.aspx.cs

418 lines
15 KiB
C#
Raw Normal View History

using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace FineUIPro.Web.HSSE.CostGoods
{
public partial class ExpenseUnit : PageBase
{
/// <summary>
/// 项目id
/// </summary>
public string ProjectId
{
get
{
return (string)ViewState["ProjectId"];
}
set
{
ViewState["ProjectId"] = value;
}
}
#region Page_Init
// 注意动态创建的代码需要放置于Page_Init不是Page_Load这样每次构造页面时都会执行
protected void Page_Init(object sender, EventArgs e)
{
InitUnitGrid();
}
protected static List<Model.Base_Unit> getUnit = new List<Model.Base_Unit>();
protected static List<Model.Base_Unit> getSUnit = new List<Model.Base_Unit>();
#region
private void InitUnitGrid()
{
FineUIPro.BoundField bf;
bf = new FineUIPro.BoundField();
bf.DataField = "Id";
bf.DataFormatString = "{0}";
bf.HeaderText = "编号";
bf.Width = 60;
bf.Hidden = true;
Grid1.Columns.Add(bf);
bf = new FineUIPro.BoundField();
bf.DataField = "SupCostTypeName";
bf.DataFormatString = "{0}";
bf.HeaderText = "类别名称";
bf.ColumnID = "GroupB";
bf.Width = 90;
bf.HeaderTextAlign = TextAlign.Center;
Grid1.Columns.Add(bf);
bf = new FineUIPro.BoundField();
bf.DataField = "CostTypeName";
bf.DataFormatString = "{0}";
bf.HeaderText = "分项名称";
2023-05-31 10:18:16 +08:00
bf.Width = 150;
bf.HeaderTextAlign = TextAlign.Center;
Grid1.Columns.Add(bf);
this.ProjectId = this.ProjectId ?? this.CurrUser.LoginProjectId;
getUnit = (from x in Funs.DB.Project_ProjectUnit
join y in Funs.DB.Base_Unit on x.UnitId equals y.UnitId
where x.ProjectId == this.ProjectId && x.UnitType == Const.ProjectUnitType_1
select y).ToList();
foreach (var item in getUnit)
{
bf = new FineUIPro.BoundField();
bf.DataField = item.UnitId;
bf.DataFormatString = "{0}";
bf.HeaderText = item.UnitName + "(元)";
bf.Width = 200;
Grid1.Columns.Add(bf);
}
getSUnit = (from x in Funs.DB.Project_ProjectUnit
join y in Funs.DB.Base_Unit on x.UnitId equals y.UnitId
where x.ProjectId == this.ProjectId && x.UnitType == Const.ProjectUnitType_2
select y).ToList();
foreach (var item in getSUnit)
{
bf = new FineUIPro.BoundField();
bf.DataField = item.UnitId;
bf.DataFormatString = "{0}";
bf.HeaderText = item.UnitName + "(元)";
2023-05-31 10:18:16 +08:00
bf.Width = 220;
Grid1.Columns.Add(bf);
}
bf = new FineUIPro.BoundField();
bf.DataField = "SumC";
bf.DataFormatString = "{0}";
bf.HeaderText = "合计(元)";
bf.Width = 90;
bf.HeaderTextAlign = TextAlign.Center;
Grid1.Columns.Add(bf);
Grid1.DataKeyNames = new string[] { "Id", "CostTypeName" };
2023-05-31 10:18:16 +08:00
if (Grid1.Columns.Count() > 9)
{
Grid1.ForceFit = false;
}
}
#endregion
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ProjectId = this.CurrUser.LoginProjectId;
if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.ProjectId)
{
this.ProjectId = Request.Params["projectId"];
}
Funs.DropDownPageSize(this.ddlPageSize);
////权限按钮方法
this.GetButtonPower();
this.btnNew.OnClientClick = Window1.GetShowReference("ExpenseEdit.aspx") + "return false;";
LoadData();
}
}
/// <summary>
/// 获取数据
/// </summary>
private void LoadData()
{
DataTable tb = GetDataTable(); //this.LINQToDataTable(getData.ToList());
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
/// <summary>
/// 获取模拟树表格
/// </summary>
/// <returns></returns>
public DataTable GetDataTable()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Id", typeof(string)));
table.Columns.Add(new DataColumn("SupCostTypeName", typeof(string)));
table.Columns.Add(new DataColumn("CostTypeName", typeof(string)));
foreach (var item in getUnit)
{
table.Columns.Add(new DataColumn(item.UnitId, typeof(string)));
}
foreach (var item in getSUnit)
{
table.Columns.Add(new DataColumn(item.UnitId, typeof(string)));
}
table.Columns.Add(new DataColumn("SumC", typeof(String)));
if (!string.IsNullOrEmpty(this.txtYear.Text))
{
SetTable(table, Funs.GetNewIntOrZero(this.txtYear.Text));
}
else
{
SetTable(table, null);
}
return table;
}
/// <summary>
/// 设置table
/// </summary>
/// <param name="table"></param>
/// <param name="year"></param>
private void SetTable(DataTable table, int? year)
{
var getDetail = from x in Funs.DB.CostGoods_ExpenseDetail
join y in Funs.DB.CostGoods_Expense on x.ExpenseId equals y.ExpenseId
where y.ProjectId == this.ProjectId && (!year.HasValue || y.Year == year)
orderby y.Year, x.SupSortIndex, x.SortIndex
select new { x.ExpenseDetailId, x.ExpenseId, y.UnitId, y.Year, x.SupCostTypeId, x.SupCostTypeName, x.SupSortIndex, x.CostType, x.CostTypeName, x.SortIndex, x.CostMoney };
var getType = (from x in getDetail select new { x.SupSortIndex, x.SortIndex, x.CostType }).Distinct();
foreach (var item in getType)
{
var getD = getDetail.FirstOrDefault(x => x.CostType == item.CostType);
if (getD != null)
{
DataRow row = table.NewRow();
row[0] = getD.ExpenseDetailId + "#" + getD.ExpenseId;
row[1] = getD.SupCostTypeName;
row[2] = getD.CostTypeName;
int r = 3;
foreach (var uitem in getUnit)
{
var getD1 = getDetail.Where(x => x.UnitId == uitem.UnitId && x.CostType == item.CostType);
if (getD1.Count() > 0)
{
row[r] = getD1.Sum(X => X.CostMoney ?? 0).ToString();
}
else
{
row[r] = 0.00;
}
r++;
2023-05-31 10:18:16 +08:00
}
foreach (var suitem in getSUnit)
{
var getD2 = getDetail.Where(x => x.UnitId == suitem.UnitId && x.CostType == item.CostType);
if (getD2.Count() > 0)
{
row[r] = getD2.Sum(X => X.CostMoney ?? 0).ToString();
}
else
{
row[r] = 0.00;
}
r++;
2023-05-31 10:18:16 +08:00
}
row[r] = getDetail.Where(x => x.Year == getD.Year && x.CostType == item.CostType).Sum(x => x.CostMoney ?? 0);
table.Rows.Add(row);
}
}
}
#region
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
Response.ClearContent();
string filename = Funs.GetNewFileName();
Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("费用计划" + filename, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/excel";
Response.ContentEncoding = System.Text.Encoding.UTF8;
this.Grid1.PageSize = this.Grid1.RecordCount;
2023-05-31 10:18:16 +08:00
Response.Write(GetGridTableHtml(this.Grid1));
Response.End();
}
#endregion
#region
/// <summary>
/// 改变索引事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
LoadData();
}
/// <summary>
/// 分页下拉选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);
LoadData();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort(object sender, GridSortEventArgs e)
{
LoadData();
}
#endregion
/// <summary>
/// 查看
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnView_Click(object sender, EventArgs e)
{
this.EditData("view");
}
/// <summary>
/// 双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
{
this.EditData();
}
/// <summary>
/// 右键编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuEdit_Click(object sender, EventArgs e)
{
this.EditData();
}
/// <summary>
/// 编辑数据
/// </summary>
private void EditData(string type = null)
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
return;
}
string id = Grid1.SelectedRow.Values[0].ToString();
var getID = Funs.GetStrListByStr(id, '#');
if (getID != null && getID.Count > 1)
{
var getEx = ExpenseService.GetExpenseById(getID[1]);
if (getEx != null && !string.IsNullOrEmpty(getEx.UnitId))
{
if (this.btnMenuEdit.Hidden || type == "view") ////双击事件 编辑权限有:编辑页面,无:查看页面 或者状态是完成时查看页面
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ExpenseView.aspx?ExpenseId={0}", id, "查看 - ")));
}
else
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ExpenseEdit.aspx?ExpenseId={0}", id, "编辑 - ")));
}
}
}
}
#region
/// <summary>
/// 右键删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuDelete_Click(object sender, EventArgs e)
{
if (Grid1.SelectedRowIndexArray.Length > 0)
{
foreach (int rowIndex in Grid1.SelectedRowIndexArray)
{
string rowID = Grid1.DataKeys[rowIndex][0].ToString();
var getEx = ExpenseService.GetExpenseById(rowID);
if (getEx != null && !string.IsNullOrEmpty(getEx.UnitId))
{
BLL.LogService.AddSys_Log(this.CurrUser, getEx.ExpenseCode, getEx.ExpenseId, BLL.Const.ProjectExpenseMenuId, BLL.Const.BtnDelete);
BLL.ExpenseService.DeleteExpenseById(rowID);
}
}
this.LoadData();
ShowNotify("删除数据成功!", MessageBoxIcon.Success);
}
}
#endregion
/// <summary>
/// 单位查询事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
{
this.LoadData();
}
#region
/// <summary>
/// 获取按钮权限
/// </summary>
/// <param name="button"></param>
/// <returns></returns>
private void GetButtonPower()
{
if (Request.Params["value"] == "0")
{
return;
}
var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, BLL.Const.ProjectExpenseMenuId);
if (buttonList.Count() > 0)
{
if (buttonList.Contains(BLL.Const.BtnAdd))
{
this.btnNew.Hidden = false;
}
if (buttonList.Contains(BLL.Const.BtnModify))
{
this.btnMenuEdit.Hidden = false;
}
if (buttonList.Contains(BLL.Const.BtnDelete))
{
this.btnMenuDelete.Hidden = false;
}
}
}
#endregion
protected void txtYear_ClearIconClick(object sender, EventArgs e)
{
this.txtYear.Text = string.Empty;
this.LoadData();
}
}
}