using BLL;
using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.CNCCG.HSSE.CostGoods
{
public partial class CostSmallDetailEdit : PageBase
{
#region 定义项
///
/// 主键
///
private string CostSmallDetailId
{
get
{
return (string)ViewState["CostSmallDetailId"];
}
set
{
ViewState["CostSmallDetailId"] = value;
}
}
///
/// 项目主键
///
public string ProjectId
{
get
{
return (string)ViewState["ProjectId"];
}
set
{
ViewState["ProjectId"] = value;
}
}
#endregion
public static List hSSECostSmallDetailOutputs =new List();
#region 加载
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ProjectId = this.CurrUser.LoginProjectId;
this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
this.hdProjectA1.Text = "0";
this.hdProjectA2.Text = "0";
this.hdProjectA3.Text = "0";
this.hdProjectA4.Text = "0";
this.hdProjectA5.Text = "0";
this.hdProjectA6.Text = "0";
this.hdProjectB1.Text = "0";
this.hdProjectB2.Text = "0";
this.hdProjectB3.Text = "0";
this.InitDropDownList();
this.CostSmallDetailId = Request.Params["CostSmallDetailId"];
if (hSSECostSmallDetailOutputs!=null)
{
hSSECostSmallDetailOutputs = new List();
}
if (!string.IsNullOrEmpty(this.CostSmallDetailId))
{
Model.CostGoods_CostSmallDetail costSmallDetail = BLL.CostSmallDetailService.GetCostSmallDetailById(this.CostSmallDetailId);
if (costSmallDetail != null)
{
this.ProjectId = costSmallDetail.ProjectId;
if (this.ProjectId != this.CurrUser.LoginProjectId)
{
this.InitDropDownList();
}
if (costSmallDetail.Months != null)
{
//this.txtMonths.Text = string.Format("{0:yyyy-MM}", costSmallDetail.Months);
this.drpYear.SelectedValue = Convert.ToString(costSmallDetail.Months.Value.Year);
this.drpMonths.SelectedValue = Convert.ToString(costSmallDetail.Months.Value.Month);
}
this.txtCostSmallDetailCode.Text = CodeRecordsService.ReturnCodeByDataId(this.CostSmallDetailId);
if (!string.IsNullOrEmpty(costSmallDetail.UnitId))
{
this.drpUnitId.SelectedValue = costSmallDetail.UnitId;
}
if (costSmallDetail.ReportDate != null)
{
this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", costSmallDetail.ReportDate);
}
var costGoods_CostSmallDetailItems=from x in Funs.DB.CostGoods_CostSmallDetailItem where x.CostSmallDetailId==this.CostSmallDetailId orderby x.CostType select x ;
var constTypeList = CostTypeService.GetCostTypeList();
foreach (var item in costGoods_CostSmallDetailItems)
{
HSSECostSmallDetailOutput hSSECost = new HSSECostSmallDetailOutput();
hSSECost.CostSmallDetailItemId = item.CostSmallDetailItemId;
hSSECost.CostTypeCode = item.CostType;
hSSECost.CostTypeName = constTypeList.FirstOrDefault(x => x.CostTypeCode == item.CostType)?.CostTypeName;
hSSECost.CostMoney = item.CostMoney;
hSSECost.Remark = constTypeList.FirstOrDefault(x => x.CostTypeCode == item.CostType)?.Remark;
hSSECostSmallDetailOutputs.Add(hSSECost);
}
GetData();
}
}
else
{
this.drpYear.SelectedValue = Convert.ToString(DateTime.Now.Year);
this.drpMonths.SelectedValue = Convert.ToString(DateTime.Now.Month);
//this.txtMonths.Text = DateTime.Now.ToString("yyyy-MM");
//this.txtCompileMan.Text = this.CurrUser.UserName;
this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
//this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
this.txtCostSmallDetailCode.Text = CodeRecordsService.ReturnCodeByMenuIdProjectId(BLL.Const.ProjectCostSmallDetailMenuId, this.ProjectId, this.CurrUser.UnitId);
var constTypeList = CostTypeService.GetCostTypeList();
foreach (var item in constTypeList)
{
HSSECostSmallDetailOutput hSSECost = new HSSECostSmallDetailOutput();
hSSECost.CostSmallDetailItemId = SQLHelper.GetNewID();
hSSECost.CostTypeCode = item.CostTypeCode;
hSSECost.CostTypeName = item.CostTypeName;
hSSECost.CostMoney = 0;
hSSECost.SumCostMoney = 0;
hSSECost.Remark = item.Remark;
hSSECostSmallDetailOutputs.Add(hSSECost);
}
Grid1.DataSource = hSSECostSmallDetailOutputs;
Grid1.DataBind();
}
}
}
///
/// 初始化下拉框
///
private void InitDropDownList()
{
this.drpUnitId.DataValueField = "UnitId";
this.drpUnitId.DataTextField = "UnitName";
this.drpUnitId.DataSource = BLL.UnitService.GetMainAndSubUnitByProjectIdList(this.ProjectId);
this.drpUnitId.DataBind();
Funs.FineUIPleaseSelect(this.drpUnitId);
BLL.ConstValue.InitConstValueDropDownList(this.drpYear, BLL.ConstValue.Group_0008, true);
BLL.ConstValue.InitConstValueDropDownList(this.drpMonths, BLL.ConstValue.Group_0009, true);
}
#endregion
#region 保存
///
/// 保存按钮
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
if (this.drpYear.SelectedValue == BLL.Const._Null || this.drpMonths.SelectedValue == BLL.Const._Null)
{
Alert.ShowInTop("请选择月份", MessageBoxIcon.Warning);
return;
}
this.SaveData(BLL.Const.BtnSave);
PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
}
///
/// 保存数据
///
///
private void SaveData(string type)
{
Model.CostGoods_CostSmallDetail costSmallDetail = new Model.CostGoods_CostSmallDetail
{
ProjectId = this.ProjectId
};
if (this.drpYear.SelectedValue != BLL.Const._Null && this.drpMonths.SelectedValue != BLL.Const._Null)
{
costSmallDetail.Months = Funs.GetNewDateTime(this.drpYear.SelectedValue + "-" + this.drpMonths.SelectedValue);
}
costSmallDetail.CostSmallDetailCode = this.txtCostSmallDetailCode.Text.Trim();
if (this.drpUnitId.SelectedValue != BLL.Const._Null)
{
costSmallDetail.UnitId = this.drpUnitId.SelectedValue;
}
costSmallDetail.ReportDate = Funs.GetNewDateTime(this.txtReportDate.Text.Trim());
//costSmallDetail.CheckMan = this.txtCheckMan.Text;
//costSmallDetail.CheckDate = Funs.GetNewDateTime(this.txtCheckDate.Text.Trim());
//costSmallDetail.ApproveMan = this.txtApproveMan.Text;
//costSmallDetail.ApproveDate = Funs.GetNewDateTime(this.txtApproveDate.Text.Trim());
costSmallDetail.States = BLL.Const.State_2;
if (!string.IsNullOrEmpty(this.CostSmallDetailId))
{
costSmallDetail.CostSmallDetailId = this.CostSmallDetailId;
costSmallDetail.CompileMan = this.CurrUser.UserName;
costSmallDetail.CompileDate = DateTime.Now;
BLL.CostSmallDetailService.UpdateCostSmallDetail(costSmallDetail);
BLL.LogService.AddSys_Log(this.CurrUser, costSmallDetail.CostSmallDetailCode, costSmallDetail.CostSmallDetailId,BLL.Const.ProjectCostSmallDetailMenuId,BLL.Const.BtnModify);
BLL.CostSmallDetailItemService.DeleteCostSmallDetailItemByCostSmallDetailId(this.CostSmallDetailId);
}
else
{
this.CostSmallDetailId = SQLHelper.GetNewID(typeof(Model.CostGoods_CostSmallDetail));
costSmallDetail.CostSmallDetailId = this.CostSmallDetailId;
costSmallDetail.CompileMan = this.CurrUser.UserName;
costSmallDetail.CompileDate = DateTime.Now;
BLL.CostSmallDetailService.AddCostSmallDetail(costSmallDetail);
BLL.LogService.AddSys_Log(this.CurrUser, costSmallDetail.CostSmallDetailCode, costSmallDetail.CostSmallDetailId, BLL.Const.ProjectCostSmallDetailMenuId, BLL.Const.BtnAdd);
}
SaveDetail(costSmallDetail.CostSmallDetailId);
//保存费用明细
/* BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "A1", Funs.GetNewDecimalOrZero(this.nbA1.Text), this.txtDefA1.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "A2", Funs.GetNewDecimalOrZero(this.nbA2.Text), this.txtDefA2.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "A3", Funs.GetNewDecimalOrZero(this.nbA3.Text), this.txtDefA3.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "A4", Funs.GetNewDecimalOrZero(this.nbA4.Text), this.txtDefA4.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "A5", Funs.GetNewDecimalOrZero(this.nbA5.Text), this.txtDefA5.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "A6", Funs.GetNewDecimalOrZero(this.nbA6.Text), this.txtDefA6.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "B1", Funs.GetNewDecimalOrZero(this.nbB1.Text), this.txtDefB1.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "B2", Funs.GetNewDecimalOrZero(this.nbB2.Text), this.txtDefB2.Text.Trim());
BLL.CostSmallDetailItemService.AddCostDetail(costSmallDetail.CostSmallDetailId, "B3", Funs.GetNewDecimalOrZero(this.nbB3.Text), this.txtDefB3.Text.Trim());
*///Project_HSSEData_HSSEService.StatisticalData(this.CurrUser.LoginProjectId, Project_HSSEData_HSSEService.HSSEDateType.SecurityCost);
}
private void SaveDetail(string objId)
{
//1.先删除明细项
var detailList = Funs.DB.CostGoods_CostSmallDetailItem.Where(x => x.CostSmallDetailId == objId).ToList();
if (detailList.Count > 0)
{
Funs.DB.CostGoods_CostSmallDetailItem.DeleteAllOnSubmit(detailList);
Funs.DB.SubmitChanges();
}
//2.根据列表中的明细项添加
List detailLists = new List();
JArray teamGroupData = Grid1.GetMergedData();
foreach (JObject teamGroupRow in teamGroupData)
{
JObject values = teamGroupRow.Value("values");
int rowIndex = teamGroupRow.Value("index");
Model.CostGoods_CostSmallDetailItem newDetail = new Model.CostGoods_CostSmallDetailItem
{
CostSmallDetailId = objId,
CostType = values.Value("CostTypeCode"),
CostMoney = values.Value("CostMoney"),
CostDef = values.Value("Remark"),
};
if (Grid1.Rows[rowIndex].DataKeys.Length > 0)
{
newDetail.CostSmallDetailItemId = Grid1.Rows[rowIndex].DataKeys[0].ToString();
}
detailLists.Add(newDetail);
}
if (detailLists.Count > 0)
{
Funs.DB.CostGoods_CostSmallDetailItem.InsertAllOnSubmit(detailLists);
Funs.DB.SubmitChanges();
}
}
#endregion
void GetData()
{
if (this.drpUnitId.SelectedValue != BLL.Const._Null)
{
Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(this.ProjectId);
DateTime? date = Funs.GetNewDateTime(this.drpYear.SelectedValue + "-" + this.drpMonths.SelectedValue + "-01");
if (project != null && date.HasValue)
{
List projectDetails = BLL.CostSmallDetailItemService.GetCostDetailsByUnitId(this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, project != null ? project.StartDate : null, date);
foreach (var item in hSSECostSmallDetailOutputs)
{
var a = Grid1.FindRow(item.CostSmallDetailItemId);
item.SumCostMoney = (from x in projectDetails where x.CostType == item.CostTypeCode select x.CostMoney ?? 0).Sum() + item.CostMoney;
}
Grid1.DataSource = hSSECostSmallDetailOutputs;
Grid1.DataBind();
}
}
}
#region 附件上传
///
/// 上传附件
///
///
///
protected void btnAttachUrl_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.CostSmallDetailId))
{
this.SaveData(BLL.Const.BtnSave);
}
PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/CostSmallDetailAttachUrl&menuId={1}", this.CostSmallDetailId, BLL.Const.ProjectCostSmallDetailMenuId)));
}
#endregion
#region 单位变化事件
///
/// 单位变化事件
///
///
///
protected void drpUnitId_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.drpUnitId.SelectedValue != BLL.Const._Null)
{
Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(this.ProjectId);
DateTime? date = Funs.GetNewDateTime(this.drpYear.SelectedValue + "-" + this.drpMonths.SelectedValue + "-01");
if (project != null && date.HasValue)
{
List projectDetails = BLL.CostSmallDetailItemService.GetCostDetailsByUnitId(this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, project != null ? project.StartDate : null, date);
foreach (var item in hSSECostSmallDetailOutputs)
{
item.SumCostMoney = (from x in projectDetails where x.CostType == item.CostTypeCode select x.CostMoney ?? 0).Sum() + item.CostMoney;
}
Grid1.DataSource = hSSECostSmallDetailOutputs;
Grid1.DataBind();
}
}
}
#endregion
}
}