using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.HSSE.CostGoods
{
public partial class ExpenseEdit : PageBase
{
#region 定义项
///
/// 主键
///
private string ExpenseId
{
get
{
return (string)ViewState["ExpenseId"];
}
set
{
ViewState["ExpenseId"] = value;
}
}
///
/// 项目主键
///
public string ProjectId
{
get
{
return (string)ViewState["ProjectId"];
}
set
{
ViewState["ProjectId"] = value;
}
}
#endregion
#region 加载
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ProjectId = this.CurrUser.LoginProjectId;
this.btnClose.OnClientClick = ActiveWindow.GetHideRefreshReference();
BLL.UnitService.InitUnitDropDownList(this.drpUnit, this.ProjectId, false);
this.drpUnit.SelectedValue = this.CurrUser.UnitId ?? Const.UnitId_SEDIN;
this.txtYear.Text = DateTime.Now.Year.ToString();
this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
if (!string.IsNullOrEmpty(this.CurrUser.UnitId) && !CommonService.IsSedinOrSub(this.CurrUser.UnitId))
{
this.drpUnit.Readonly = true;
}
this.ExpenseId = Request.Params["ExpenseId"];
Model.CostGoods_Expense expense = new Model.CostGoods_Expense();
if (string.IsNullOrEmpty(this.ExpenseId))
{
expense = Funs.DB.CostGoods_Expense.FirstOrDefault(x => x.ProjectId == this.ProjectId && x.UnitId == this.drpUnit.SelectedValue && x.Year == Funs.GetNewInt(this.txtYear.Text));
}
else
{
expense = BLL.ExpenseService.GetExpenseById(this.ExpenseId);
}
SetPage(expense);
}
}
#endregion
#region 页面呈现
///
///
///
private void SetPage(Model.CostGoods_Expense expense)
{
if (expense != null)
{
this.ExpenseId = expense.ExpenseId;
this.ProjectId = expense.ProjectId;
this.drpUnit.SelectedValue = expense.UnitId;
this.txtYear.Text = expense.Year.ToString();
this.lbSortIndex.Text = expense.SortIndex.ToString();
this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", expense.ReportDate);
}
else
{
int Year = Funs.GetNewIntOrZero(this.txtYear.Text);
var getProject = ProjectService.GetProjectByProjectId(this.ProjectId);
if (getProject != null && (!getProject.EndDate.HasValue || Year <= getProject.EndDate.Value.Year) && (!getProject.StartDate.HasValue || Year >= getProject.StartDate.Value.Year))
{
if (CommonService.IsSedinOrSub(this.drpUnit.SelectedValue))
{
this.lbSortIndex.Text = "1";
}
else
{
var index = Funs.DB.CostGoods_Expense.Where(x => x.ProjectId == this.ProjectId && x.Year.ToString() == this.txtYear.Text).Max(x => x.SortIndex);
if (index.HasValue)
{
this.lbSortIndex.Text = (index.Value + 1).ToString();
}
else
{
this.lbSortIndex.Text = "2";
}
}
Model.CostGoods_Expense newExpense = new Model.CostGoods_Expense
{
ExpenseId = SQLHelper.GetNewID(),
ProjectId = this.ProjectId,
UnitId = this.drpUnit.SelectedValue,
ReportDate = DateTime.Now,
CreateDate = DateTime.Now,
CompileMan = this.CurrUser.PersonId,
CompileDate = DateTime.Now,
Year = Funs.GetNewInt(this.txtYear.Text),
SortIndex = Funs.GetNewIntOrZero(this.lbSortIndex.Text),
};
ExpenseService.AddExpense(newExpense);
this.ExpenseId = newExpense.ExpenseId;
ExpenseDetailService.AddCostDetail(this.ExpenseId);
}
else
{
Alert.ShowInTop("选择年度不在项目开始和竣工时间范围内!", MessageBoxIcon.Warning);
return;
}
}
this.Grid1.DataSource = from x in Funs.DB.CostGoods_ExpenseDetail
where x.ExpenseId == this.ExpenseId
orderby x.SupSortIndex, x.SortIndex
select x;
this.Grid1.DataBind();
}
#endregion
#region 保存
///
/// 保存按钮
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
if (this.drpUnit.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.txtYear.Text))
{
Alert.ShowInTop("请选择单位、年度!", MessageBoxIcon.Warning);
return;
}
this.SaveData(BLL.Const.BtnSave);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
///
/// 保存数据
///
///
private void SaveData(string type)
{
Model.CostGoods_Expense expense = new Model.CostGoods_Expense
{
ProjectId = this.ProjectId,
UnitId = this.drpUnit.SelectedValue,
ReportDate = Funs.GetNewDateTime(this.txtReportDate.Text.Trim()),
CompileDate = DateTime.Now,
CompileMan = this.CurrUser.PersonId,
SortIndex = Funs.GetNewInt(this.lbSortIndex.Text),
Year = Funs.GetNewInt(this.txtYear.Text),
};
if (!string.IsNullOrEmpty(this.ExpenseId))
{
expense.ExpenseId = this.ExpenseId;
BLL.ExpenseService.UpdateExpense(expense);
BLL.LogService.AddSys_Log(this.CurrUser, expense.ExpenseCode, expense.ExpenseId, BLL.Const.ProjectExpenseMenuId, BLL.Const.BtnModify);
// BLL.ExpenseDetailService.DeleteCostDetailByExpenseId(this.ExpenseId);
}
else
{
this.ExpenseId = SQLHelper.GetNewID();
expense.ExpenseId = this.ExpenseId;
expense.CreateDate = DateTime.Now;
BLL.ExpenseService.AddExpense(expense);
BLL.LogService.AddSys_Log(this.CurrUser, expense.ExpenseCode, expense.ExpenseId, BLL.Const.ProjectExpenseMenuId, BLL.Const.BtnAdd);
}
var getItem = jerqueSaveList();
foreach (var item in getItem)
{
ExpenseDetailService.UpdateExpenseDetail(item);
}
}
#endregion
#region 附件上传
///
/// 上传附件
///
///
///
protected void btnAttachUrl_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.ExpenseId))
{
SaveData(BLL.Const.BtnSave);
}
PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/ExpenseAttachUrl&menuId={1}", ExpenseId, BLL.Const.ProjectExpenseMenuId)));
}
#endregion
#region 单位变化事件
///
/// 单位变化事件
///
///
///
protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.drpUnit.SelectedValue != Const._Null && !string.IsNullOrEmpty(this.txtYear.Text))
{
SetPage(Funs.DB.CostGoods_Expense.FirstOrDefault(x => x.ProjectId == this.ProjectId && x.UnitId == this.drpUnit.SelectedValue && x.Year.ToString() == this.txtYear.Text));
}
}
#endregion
#region 保存集合
///
/// 保存集合
///
private List jerqueSaveList()
{
var returnList = Funs.DB.CostGoods_ExpenseDetail.Where(x => x.ExpenseId == this.ExpenseId);
JArray mergedData = Grid1.GetMergedData();
foreach (JObject mergedRow in mergedData)
{
string status = mergedRow.Value("status");
JObject values = mergedRow.Value("values");
var item = returnList.FirstOrDefault(e => e.ExpenseDetailId == values.Value("ExpenseDetailId"));
if (item != null)
{
item.CostMoney = Funs.GetNewDecimalOrZero(values.Value("CostMoney"));
}
}
return returnList.ToList();
}
#endregion
}
}