using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Pub_Class; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.Http; using WebAPI.Models; using WebAPI.Controllers.SCGL.日计划管理; using System.IO; using System.Linq; using System.Text; namespace WebAPI.Controllers { //成本费用单Controller public class CB_ItemMoneyBillController : ApiController { //成本费用单 //获取系统参数 CB_ItemMoneyBillMain CB_ItemMoneyBill public DBUtility.ClsPub.Enum_BillStatus BillStatus; public DAL.ClsCB_ItemMoneyBillMain BillOld = new DAL.ClsCB_ItemMoneyBillMain(); public DAL.ClsCB_ItemMoneyBillMain BillNew0 = new DAL.ClsCB_ItemMoneyBillMain(); private json objJsonResult = new json(); SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); DataSet ds; #region 成本费用单列表 查询 /// /// 返回项目阶段列表 ///参数:string sql。 ///返回值:object。 /// [Route("CB_ItemMoneyBillTran/CB_ItemMoneyBillMainList")] [HttpGet] public object CB_ItemMoneyBillMainList(string sWhere, string user) { try { List columnNameList = new List(); //查看权限 if (!DBUtility.ClsPub.Security_Log("CB_ItemMoney_Query", 1, false, user)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "无查看权限!"; objJsonResult.data = null; return objJsonResult; } if (sWhere == null || sWhere.Equals("")) { ds = oCN.RunProcReturn("select * from h_v_CB_ItemMoneyBillMainList order by 单据号 desc", "h_v_CB_ItemMoneyBillMainList"); } else { string sql1 = "select * from h_v_CB_ItemMoneyBillMainList where 1 = 1 "; string sql = sql1 + sWhere + " order by 单据号 desc"; ds = oCN.RunProcReturn(sql, "h_v_CB_ItemMoneyBillMainList"); } //添加列名 foreach (DataColumn col in ds.Tables[0].Columns) { Type dataType = col.DataType; string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}"; columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名 } objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "Sucess!"; objJsonResult.data = ds.Tables[0]; objJsonResult.list = columnNameList; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 成本费用单保存成本费用单保存 新增/编辑 [Route("CB_ItemMoneyBillTran/SaveCB_ItemMoneyBillMain")] [HttpPost] public object SaveCB_ItemMoneyBillMain ([FromBody] JObject msg) { var _value = msg["msg"].ToString(); string msg1 = _value.ToString(); string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); string msg2 = sArray[0].ToString(); string msg3 = sArray[1].ToString(); //子表 string refSav = sArray[2].ToString();//操作方式数据类型 1添加 3修改 2 复制 string msg4 = sArray[3].ToString();//用户名 DBUtility.ClsPub.CurUserName = msg4; string UserName = ""; string s = ""; ListModels oListModels = new ListModels(); try { //编辑权限 if (!DBUtility.ClsPub.Security_Log_second("CB_ItemMoney_Edit", 1, false, msg4)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "无保存权限!"; objJsonResult.data = null; return objJsonResult; } DAL.ClsCB_ItemMoneyBillMain oBill = new DAL.ClsCB_ItemMoneyBillMain(); List lsmain = new List(); msg2 = msg2.Replace("\\", ""); msg2 = msg2.Replace("\n", ""); //\n lsmain = oListModels.getObjectByJson_CB_ItemMoneyBillMain(msg2); foreach (Model.ClsCB_ItemMoneyBillMain_1 oItem in lsmain) { if (refSav == "Add") { //单据号是否重复 if (BillNew0.IsExistBillNo(ref ClsPub.sExeReturnInfo, oItem.HBillNo, BillStatus, BillOld.omodel.HInterID)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据号重复!不允许保存!"; objJsonResult.data = 1; return objJsonResult; } } if (refSav == "Update") { if (BillOld.ShowBill(oItem.HInterID, ref s) == false) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "此单据有误!"; objJsonResult.data = 1; return objJsonResult; } //判断是否可编辑 if (BillOld.omodel.HChecker != "" && BillOld.omodel.HChecker != null) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "此单据已经被审核,不允许修改!"; objJsonResult.data = 1; return objJsonResult; } if (BillOld.omodel.HBillStatus > 1) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "此单据处于不可编辑状态,不允许修改!"; objJsonResult.data = 1; return objJsonResult; } if (!DBUtility.Xt_BaseBillFun.Fun_AllowEditBill(BillOld, ref s)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = s + ",不允许修改"; objJsonResult.data = 1; return objJsonResult; } } UserName = oItem.HMaker; //制单人 oItem.HBillType = "1802"; oItem.HYear = DBUtility.ClsPub.isLong(DateTime.Now.Year); oItem.HPeriod = DBUtility.ClsPub.isLong(DateTime.Now.Month); oItem.HMakeDate = DBUtility.ClsPub.isStrNull(DateTime.Now.ToString("yyyy-MM-dd")); if (DBUtility.ClsPub.isStrNull(oItem.HDate) == "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "保存失败!没有单据日期,无法保存!"; objJsonResult.data = 1; return objJsonResult; } oBill.omodel = oItem; } // 表体数据 //按 },{来拆分数组 //去掉【和】 msg3 = msg3.Substring(1, msg3.Length - 2); msg3 = msg3.Replace("\\", ""); msg3 = msg3.Replace("\n", ""); //\n List ls = new List(); ls = oListModels.getObjectByJson_CB_ItemMoneyBillSub(msg3); int i = 0; foreach (Model.ClsCB_ItemMoneyBillSub_1 oItemSub in ls) { i++; oItemSub.HEntryID = i; oItemSub.HEntryCloseDate = DBUtility.ClsPub.isDate(DateTime.Now); oItemSub.HCloseType = false; //关闭类型 oBill.DetailColl.Add(oItemSub); } //进行 会计期间 结账 的判断和控制 int sYear = 0; int sPeriod = 0; DateTime HDate = DateTime.Now; if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false) { objJsonResult.Message = s; return objJsonResult; } //保存 //保存完毕后处理 bool bResult; if (refSav == "Add") { bResult = oBill.AddBill(ref DBUtility.ClsPub.sExeReturnInfo); } else if (refSav == "Update") { bResult = oBill.ModifyBill(oBill.omodel.HInterID, ref DBUtility.ClsPub.sExeReturnInfo); } else { bResult = false; } if (bResult) { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "保存成功!"; objJsonResult.data = 1; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "保存失败!" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = 1; return objJsonResult; } } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "保存失败!" + e.ToString(); objJsonResult.data = 1; return objJsonResult; } } #endregion #region 成本费用单 编辑页面初始化 [Route("CB_ItemMoneyBillTran/CB_ItemMoneyBillMainEditList")] [HttpGet] public object CB_ItemMoneyBillMainEditList(long HInterID) { try { List tableList = new List(); ds = oCN.RunProcReturn("exec h_p_CB_ItemMoneyBillMain_Edit @HInterID = " + HInterID, "h_p_CB_ItemMoneyBillMain_Edit"); tableList.Add(ds.Tables[0]); tableList.Add(ds.Tables[1]); if (ds == null || ds.Tables[0].Rows.Count == 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "false!"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "Sucess!"; objJsonResult.data = tableList; return objJsonResult; } } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 成本费用单 删除 /// ///参数:string HInterID。 ///返回值:object。 /// [Route("CB_ItemMoneyBillTran/DeleteCB_ItemMoneyBillMain")] [HttpGet] public object DeleteCB_ItemMoneyBillMain (string HInterID, string user) { try { string s = ""; //查看权限 if (!DBUtility.ClsPub.Security_Log("CB_ItemMoney_Drop", 1, false, user)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "无删除权限!"; objJsonResult.data = null; return objJsonResult; } if (HInterID == null || HInterID.Equals("")) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "HInterID不能为空!"; objJsonResult.data = null; return objJsonResult; } if (BillOld.ShowBill(long.Parse(HInterID), ref s) == false) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "此单据有误!"; objJsonResult.data = 1; return objJsonResult; } //判断是否可编辑 if (BillOld.omodel.HChecker != "" && BillOld.omodel.HChecker != null) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "此单据已经被审核,不允许删除!"; objJsonResult.data = 1; return objJsonResult; } //删除前控制========================================= string sql1 = "exec h_p_CB_ItemMoneyBill_BeforeDelCtrl " + BillOld.omodel.HInterID + ",'" + BillOld.omodel.HBillNo + "','" + user + "'"; ds = oCN.RunProcReturn(sql1, "h_p_CB_ItemMoneyBill _BeforeDelCtrl"); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "删除失败!原因:删除前判断失败,请与网络管理人员联系"; objJsonResult.data = null; return objJsonResult; } if (ds.Tables[0].Rows[0]["HBack"].ToString() != "0") { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "删除失败!原因:" + ds.Tables[0].Rows[0]["HRemark"].ToString(); ; objJsonResult.data = null; return objJsonResult; } //================================================================================== //进行 会计期间 结账 的判断和控制 int sYear = 0; int sPeriod = 0; DateTime HDate = DateTime.Now; if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false) { objJsonResult.Message = s; return objJsonResult; } oCN.BeginTran(); oCN.RunProc("delete from CB_ItemMoneyBillMain where HInterID = " + HInterID); oCN.RunProc("delete from CB_ItemMoneyBillSub where HInterID= " + HInterID); //删除后控制================================================================================== string sql2 = "exec h_p_CB_ItemMoneyBill_AfterDelCtrl " + BillOld.omodel.HInterID + ",'" + BillOld.omodel.HBillNo + "','" + user + "'"; ds = oCN.RunProcReturn(sql2, "h_p_CB_ItemMoneyBill _AfterDelCtrl"); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { s = "删除后判断失败,请与网络管理人员联系"; objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "删除失败!原因:" + s; objJsonResult.data = null; oCN.RollBack(); return objJsonResult; } if (ds.Tables[0].Rows[0]["HBack"].ToString() != "0") { s = ds.Tables[0].Rows[0]["HRemark"].ToString(); objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "删除失败!原因:" + s; objJsonResult.data = null; oCN.RollBack(); return objJsonResult; } //============================================================================================== oCN.Commit(); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "删除成功"; objJsonResult.data = null; return objJsonResult; } catch (Exception e) { oCN.RollBack(); objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 成本费用单 审核/反审核 /// /// /// 单据ID /// 审核(0),反审核(1) /// 审核人 /// [Route("CB_ItemMoneyBillTran/AuditCB_ItemMoneyBillMain")] [HttpGet] public object AuditCB_ItemMoneyBillMain (int HInterID, int IsAudit, string CurUserName) { string ModRightNameCheck = "CB_ItemMoney_Check"; DBUtility.ClsPub.CurUserName = CurUserName; try { //审核权限 if (!DBUtility.ClsPub.Security_Log_second(ModRightNameCheck, 1, false, CurUserName)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "审核失败!无权限!"; objJsonResult.data = null; return objJsonResult; } //HInterID数据判断 if (HInterID <= 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "HInterID小于0!"; objJsonResult.data = null; return objJsonResult; } Int64 lngBillKey = 0; lngBillKey = DBUtility.ClsPub.isLong(HInterID); //对HInterID进行类型的转换 DAL.ClsCB_ItemMoneyBillMain oBill = new DAL.ClsCB_ItemMoneyBillMain(); //实例化单据操作类,用于进行相关操作 //针对需要进行的操作,检验当前单据的状态是否支持需要进行的操作 if (oBill.ShowBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo)) //根据HInterID获取该单据的数据 { if (oBill.omodel.HCloseMan.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已关闭!不能再次审核!"; objJsonResult.data = null; return objJsonResult; } if (oBill.omodel.HDeleteMan.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已作废!不能再次审核!"; objJsonResult.data = null; return objJsonResult; } if (IsAudit == 0) //审核判断 { if (oBill.omodel.HChecker.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已审核!不能再次审核!"; objJsonResult.data = null; return objJsonResult; } } if (IsAudit == 1) //反审核判断 { if (oBill.omodel.HChecker.Trim() == "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据未审核!不需要反审核!"; objJsonResult.data = null; return objJsonResult; } } } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } //进行需要进行的审核/反审核操作 if (IsAudit == 0) //审核提交 { //进行 会计期间 结账 的判断和控制 string s = ""; int sYear = 0; int sPeriod = 0; DateTime HDate = DateTime.Now; if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false) { objJsonResult.Message = s; return objJsonResult; } oCN.BeginTran(); //记录返回信息 string msg = ""; //审核前控制========================================= string sql1 = "exec h_p_CB_ItemMoneyBill_BeforeCheckCtrl " + oBill.omodel.HInterID + ",'" + oBill.omodel.HBillNo + "','" + CurUserName + "'"; ds = oCN.RunProcReturn(sql1, "h_p_CB_ItemMoneyBill_BeforeCheckCtrl"); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "审核失败!原因:审核前判断失败,请与网络管理人员联系"; objJsonResult.data = null; oCN.RollBack(); return objJsonResult; } if (ds.Tables[0].Rows[0]["HBack"].ToString() != "0") { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "审核失败!原因:" + ds.Tables[0].Rows[0]["HRemark"].ToString(); ; objJsonResult.data = null; oCN.RollBack(); return objJsonResult; } //================================================================================== //审核提交 if (oBill.CheckBill(oBill.omodel.HInterID, oBill.omodel.HBillNo, "h_p_CB_ItemMoneyBill_AfterCheckCtrl", CurUserName, ref DBUtility.ClsPub.sExeReturnInfo) == true) { oCN.Commit(); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "审核成功!" + msg; objJsonResult.data = null; return objJsonResult; } else { oCN.RollBack(); objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "审核失败!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } } if (IsAudit == 1) //反审核提交 { //反审核前控制========================================= string sql1 = "exec h_p_CB_ItemMoneyBill_BeforeUnCheckCtrl " + oBill.omodel.HInterID + ",'" + oBill.omodel.HBillNo + "','" + CurUserName + "'"; ds = oCN.RunProcReturn(sql1, "h_p_CB_ItemMoneyBill_BeforeUnCheckCtrl"); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "反审核失败!原因:反审核前判断失败,请与网络管理人员联系"; objJsonResult.data = null; return objJsonResult; } if (ds.Tables[0].Rows[0]["HBack"].ToString() != "0") { objJsonResult.code = "0"; objJsonResult.count = 1; objJsonResult.Message = "反审核失败!原因:" + ds.Tables[0].Rows[0]["HRemark"].ToString(); ; objJsonResult.data = null; return objJsonResult; } //=========================================================== //进行 会计期间 结账 的判断和控制 string s = ""; int sYear = 0; int sPeriod = 0; DateTime HDate = DateTime.Now; if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false) { objJsonResult.Message = s; return objJsonResult; } //反审核提交AbandonCheck if (oBill.AbandonCheck(oBill.omodel.HInterID, oBill.omodel.HBillNo, "h_p_CB_ItemMoneyBill_AfterUnCheckCtrl", CurUserName, ref DBUtility.ClsPub.sExeReturnInfo) == true) { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "反审核成功"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "反审核失败!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } } return objJsonResult; } catch (Exception e) { oCN.RollBack(); objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "审核失败或者反审核失败!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 成本费用单 关闭/反关闭 /// /// /// 单据ID /// 关闭(0),反关闭(1) /// 关闭人 /// [Route("CB_ItemMoneyBillTran/CloseCB_ItemMoneyBill")] [HttpGet] public object CloseCB_ItemMoneyBill (int HInterID, int IsAudit, string CurUserName) { string ModRightNameCheck = "CB_ItemMoney_Close"; DBUtility.ClsPub.CurUserName = CurUserName; try { //检查权限 if (!DBUtility.ClsPub.Security_Log_second(ModRightNameCheck, 1, false, CurUserName)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "关闭失败!无权限!"; objJsonResult.data = null; return objJsonResult; } //HInterID数据判断 if (HInterID <= 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "HInterID小于0!"; objJsonResult.data = null; return objJsonResult; } Int64 lngBillKey = 0; lngBillKey = DBUtility.ClsPub.isLong(HInterID); //对HInterID进行类型的转换 DAL.ClsCB_ItemMoneyBillMain oBill = new DAL.ClsCB_ItemMoneyBillMain(); //实例化单据操作类,用于进行相关操作 //针对需要进行的操作,检验当前单据的状态是否支持需要进行的操作 if (oBill.ShowBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo)) //根据HInterID获取该单据的数据 { if (oBill.omodel.HDeleteMan.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已作废!不能进行关闭!"; objJsonResult.data = null; return objJsonResult; } if (oBill.omodel.HChecker.Trim() == "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据未审核!不能进行关闭!"; objJsonResult.data = null; return objJsonResult; } if (IsAudit == 0) //关闭判断 { if (oBill.omodel.HCloseMan.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已关闭!不能再次关闭!"; objJsonResult.data = null; return objJsonResult; } } if (IsAudit == 1) //反关闭判断 { if (oBill.omodel.HCloseMan.Trim() == "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据未关闭!不需要反关闭!"; objJsonResult.data = null; return objJsonResult; } } } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } //进行需要进行的关闭/反关闭操作 if (IsAudit == 0) //关闭提交 { //关闭提交 if (oBill.CloseBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo) == true) { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "关闭成功"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "关闭失败!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } } if (IsAudit == 1) //反关闭提交 { //反关闭提交 if (oBill.CancelClose(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo) == true) { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "反关闭成功"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "反关闭失败!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } } return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "关闭失败或者反关闭失败!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 成本费用单 作废/反作废 /// /// /// 单据ID /// 作废(0),反作废(1) /// 作废人 /// [Route("CB_ItemMoneyBillTran/DeleteCB_ItemMoneyBill")] [HttpGet] public object DeleteCB_ItemMoneyBill (int HInterID, int IsAudit, string CurUserName) { string ModRightNameCheck = "CB_ItemMoney_Delete"; DBUtility.ClsPub.CurUserName = CurUserName; try { //检查权限 if (!DBUtility.ClsPub.Security_Log_second(ModRightNameCheck, 1, false, CurUserName)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "作废失败!无权限!"; objJsonResult.data = null; return objJsonResult; } //HInterID数据判断 if (HInterID <= 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "HInterID小于0!"; objJsonResult.data = null; return objJsonResult; } Int64 lngBillKey = 0; lngBillKey = DBUtility.ClsPub.isLong(HInterID); //对HInterID进行类型的转换 DAL.ClsCB_ItemMoneyBillMain oBill = new DAL.ClsCB_ItemMoneyBillMain(); //实例化单据操作类,用于进行相关操作 //针对需要进行的操作,检验当前单据的状态是否支持需要进行的操作 if (oBill.ShowBill(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo)) //根据HInterID获取该单据的数据 { if (oBill.omodel.HChecker.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已审核!不能进行作废!"; objJsonResult.data = null; return objJsonResult; } if (IsAudit == 0) //作废判断 { if (oBill.omodel.HDeleteMan.Trim() != "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据已作废!不能再作废!"; objJsonResult.data = null; return objJsonResult; } } if (IsAudit == 1) //反作废判断 { if (oBill.omodel.HDeleteMan.Trim() == "") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据未作废!不需要反作废!"; objJsonResult.data = null; return objJsonResult; } } } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "单据不存在!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } //进行需要进行的作废/反作废操作 if (IsAudit == 0) //作废提交 { //作废提交 if (oBill.Cancelltion(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo) == true) { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "作废成功"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "作废失败!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } } if (IsAudit == 1) //反关闭提交 { //反关闭提交 if (oBill.AbandonCancelltion(lngBillKey, ref DBUtility.ClsPub.sExeReturnInfo) == true) { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "反作废成功"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "反作废失败!原因:" + DBUtility.ClsPub.sExeReturnInfo; objJsonResult.data = null; return objJsonResult; } } return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "作废失败或者反作废失败!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 文件上传 [Route("CB_ItemMoneyBillController/CB_ItemMoneyBill_Excel")] [HttpPost] public json CB_ItemMoneyBill_Excel() { json res = new json(); try { //获取文件名称 var file = HttpContext.Current.Request.Files[0]; //获取文件物理路径 string ExcelPath = HttpContext.Current.Server.MapPath("~/" + file.FileName); //保存文件 file.SaveAs(ExcelPath); SCGL.日计划管理.NpoiHelper np = new NpoiHelper(); DataSet ExcelDs = np.ReadExcel(ExcelPath, 1, 1, "0"); //删除文件 File.Delete(ExcelPath); //创建临时表 DataTable provisional = new DataTable("dt2"); //添加列名 for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++) { provisional.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString()); } provisional.Columns.Add("单据号", typeof(string)); //添加数据 for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++) { DataRow row = provisional.NewRow(); for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++) { row[j] = ExcelDs.Tables[0].Rows[i][j].ToString(); } provisional.Rows.Add(row); } //判断列 string error = JudgmentColumns(provisional); if (error.Length > 0) { res.code = "0"; res.count = 0; res.Message = $"Excel模板存在错误,{error}\r\n"; res.data = null; return res; } for (int i = 0; i <= provisional.Rows.Count - 1; i++) { //string HBillNo= DBUtility.ClsPub.CreateBillCode("1802", ref DBUtility.ClsPub.sExeReturnInfo, true); //provisional.Rows[i]["单据号"] = HBillNo; string HDepNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["部门代码"].ToString()); string HDepName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["部门"].ToString()); string HEmpNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["职员代码"].ToString()); string HEmpName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["职员"].ToString()); string HICMOBillNo = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["生产订单号"].ToString()); string HProc = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["工序"].ToString()); string HCostItemNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["成本项目代码"].ToString()); string HCostItemName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["成本项目"].ToString()); //string HEntryID = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["子表内码"].ToString()); //string HDepNumber_sub = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["子部门代码"].ToString()); //string HDepName_sub = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["子部门"].ToString()); string HMaterNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["物料编码"].ToString()); string HMaterName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["物料名称"].ToString()); string HQty = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["数量"].ToString()); string HPrice = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["单价"].ToString()); string HMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["金额"].ToString()); string HWaster = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["损耗量"].ToString()); //string HRelationQty = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["关联数量"].ToString()); //string HRelationMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["关联金额"].ToString()); string remark = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["备注"].ToString()); //获取真实行数 int line = i + 1; } res.code = "1"; res.count = 1; res.Message = error; res.data = provisional; return res; } catch (Exception e) { res.code = "0"; res.count = 0; res.Message = "Exception!" + e.ToString(); res.data = null; return res; } } /// /// 判断列 /// /// /// private static string JudgmentColumns(DataTable provisional) { var error = ""; //查询没有的列 //if (!provisional.Columns.Contains("子表内码")) // error += "没有找到【子表内码】的标题,"; if (!provisional.Columns.Contains("物料编码")) error += "没有找到【物料编码】的标题,"; return error; } #endregion #region 上传保存 [Route("CB_ItemMoneyBillController/Excel_btnSave")] [HttpPost] public object Excel_btnSave([FromBody] JObject sMainSub) { var _value = sMainSub["sMainSub"].ToString(); string msg1 = _value.ToString(); string[] sArray = msg1.Split(new string[] { "&和" }, StringSplitOptions.RemoveEmptyEntries); string msg2 = sArray[0].ToString(); string user = sArray[1].ToString(); string organ = sArray[2].ToString(); json res = new json(); try { oCN.BeginTran(); List Excel = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg2); List> list = new List>(); foreach (JObject item in Excel) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } // 1. 收集所有需要查询的数据 var HBillnos = list.Select(x => x["单据号"].ToString()).Distinct().ToList(); var HProc = list.Select(x => x["工序"].ToString()).Distinct().ToList(); //var HDeptNames = list.Select(x => x["部门"].ToString()).Distinct().ToList(); //var HDeptNumbers = list.Select(x => x["部门代码"].ToString()).Distinct().ToList(); var HDeptNames_sub = list.Select(x => x["部门"].ToString()).Distinct().ToList(); var HDeptNumbers_sub = list.Select(x => x["部门代码"].ToString()).Distinct().ToList(); var materialNumbers = list.Select(x => x["物料编码"].ToString()).Distinct().ToList(); var materialNames = list.Select(x => x["物料名称"].ToString()).Distinct().ToList(); var EmpNumbers = list.Select(x => x["职员代码"].ToString()).Distinct().ToList(); var EmpNames = list.Select(x => x["职员"].ToString()).Distinct().ToList(); var CostNumbers = list.Select(x => x["成本项目代码"].ToString()).Distinct().ToList(); var CostNames = list.Select(x => x["成本项目"].ToString()).Distinct().ToList(); var ICMONumbers = list.Select(x => x["生产订单号"].ToString()).Distinct().ToList(); // 2. 批量查询生产订单内码 string ICMOQuery = $"SELECT HInterID,HBillNo FROM Sc_ICMOBillMain WHERE HBillNo IN ({string.Join(",", ICMONumbers.Select(s => $"'{s.Replace("'", "''")}'"))})"; DataSet icmoData = oCN.RunProcReturn(ICMOQuery, "Sc_ICMOBillMain"); Dictionary ICMODict = new Dictionary(); foreach (DataRow row in icmoData.Tables[0].Rows) { ICMODict[row["HBillNo"].ToString()] = (int)row["HInterID"]; } //// 2. 批量查询主子表部门信息 //string supplierQuery = $"SELECT HItemID, HName FROM Gy_Department WHERE HName IN ({string.Join(",", HDeptNames.Select(s => $"'{s.Replace("'", "''")}'"))})"; //DataSet supData = oCN.RunProcReturn(supplierQuery, "Gy_Department"); //Dictionary supplierDict = new Dictionary(); //foreach (DataRow row in supData.Tables[0].Rows) //{ // supplierDict[row["HName"].ToString()] = (int)row["HItemID"]; //} // 2.1 批量查询主子表部门信息 string supplierQuery_sub = $"SELECT HItemID, HName FROM Gy_Department WHERE HName IN ({string.Join(",", HDeptNames_sub.Select(s => $"'{s.Replace("'", "''")}'"))})"; DataSet supData_sub = oCN.RunProcReturn(supplierQuery_sub, "Gy_Department"); Dictionary supplierDict_sub = new Dictionary(); foreach (DataRow row in supData_sub.Tables[0].Rows) { supplierDict_sub[row["HName"].ToString()] = (int)row["HItemID"]; } // 3. 批量查询物料信息 string materialQuery = $"SELECT HItemID, HNumber, HName FROM Gy_Material WHERE HNumber IN ({string.Join(",", materialNumbers.Select(m => $"'{m.Replace("'", "''")}'"))}) AND HName IN ({string.Join(",", materialNames.Select(m => $"'{m.Replace("'", "''")}'"))})"; DataSet materData = oCN.RunProcReturn(materialQuery, "Gy_Material"); Dictionary materialDict = new Dictionary(); foreach (DataRow row in materData.Tables[0].Rows) { string key = $"{row["HNumber"]}_{row["HName"]}"; materialDict[key] = (int)row["HItemID"]; } // 3. 批量查询职员信息 string EmployeeQuery = $"SELECT HItemID, HNumber, HName FROM Gy_Employee WHERE HNumber IN ({string.Join(",", EmpNumbers.Select(m => $"'{m.Replace("'", "''")}'"))}) AND HName IN ({string.Join(",", EmpNames.Select(m => $"'{m.Replace("'", "''")}'"))})"; DataSet EmpData = oCN.RunProcReturn(EmployeeQuery, "Gy_Employee"); Dictionary EmpDict = new Dictionary(); foreach (DataRow row in EmpData.Tables[0].Rows) { string key = $"{row["HName"].ToString()}"; EmpDict[key] = (int)row["HItemID"]; } // 3. 批量查询chengbenxiangmu信息 string CostQuery = $"SELECT HItemID, HNumber, HName FROM Gy_ItemMoney_1 WHERE HNumber IN ({string.Join(",", CostNumbers.Select(m => $"'{m.Replace("'", "''")}'"))}) AND HName IN ({string.Join(",", CostNames.Select(m => $"'{m.Replace("'", "''")}'"))})"; DataSet CostData = oCN.RunProcReturn(CostQuery, "Gy_ItemMoney_1"); Dictionary CostDict = new Dictionary(); foreach (DataRow row in CostData.Tables[0].Rows) { string key = $"{row["HName"].ToString()}"; CostDict[key] = (int)row["HItemID"]; } // 3.1 批量查询工序信息 string ProcessQuery = $"SELECT HItemID, HNumber, HName FROM Gy_Process WHERE HName IN ({string.Join(",", HProc.Select(m => $"'{m.Replace("'", "''")}'"))})"; DataSet ProcData = oCN.RunProcReturn(ProcessQuery, "Gy_Process"); Dictionary ProcDict = new Dictionary(); foreach (DataRow row in ProcData.Tables[0].Rows) { string key = $"{row["HName"].ToString()}"; ProcDict[key] = (int)row["HItemID"]; } // 4. 查询用户信息 DataSet emp = oCN.RunProcReturn($"SELECT Czybm FROM Gy_Czygl WHERE Czymc = '{user.Replace("'", "''")}'", "Gy_Czygl"); if (emp.Tables[0].Rows.Count == 0) { res.code = "0"; res.count = 0; res.Message = "用户不存在!"; res.data = null; return res; } string HUserID = emp.Tables[0].Rows[0]["Czybm"].ToString(); // 5. 检查缺失的部门和物料,并记录行号 List missingSupplierMessages = new List(); List missingMaterialMessages = new List(); // 遍历每一行数据,记录行号(从1开始) for (int i = 0; i < list.Count; i++) { var item = list[i]; int lineNumber = i + 1; // 行号从1开始 string ICMOcheck = item["生产订单号"]; string supplier = item["部门"]; string materialNumber = item["物料编码"]; string materialName = item["物料名称"]; string materialKey = $"{materialNumber}_{materialName}"; // 检查供应商 if (!supplierDict_sub.ContainsKey(supplier)) { missingSupplierMessages.Add($"第{lineNumber}行的部门【{supplier}】"); } // 检查物料 if (!materialDict.ContainsKey(materialKey)) { missingMaterialMessages.Add($"第{lineNumber}行的物料【{materialName}(编码:{materialNumber})】"); } // 检查生产订单 if (!string.IsNullOrEmpty(ICMOcheck) && !ICMODict.ContainsKey(ICMOcheck)) { missingMaterialMessages.Add($"第{lineNumber}行的生产订单【{ICMOcheck}】"); } } StringBuilder errorMessage = new StringBuilder(); if (missingSupplierMessages.Any()) { errorMessage.Append("以下部门未维护:"); errorMessage.Append(string.Join("、", missingSupplierMessages)); errorMessage.Append(";"); } if (missingMaterialMessages.Any()) { errorMessage.Append("以下物料未维护:"); errorMessage.Append(string.Join("、", missingMaterialMessages)); errorMessage.Append(";"); } if (errorMessage.Length > 0) { res.code = "0"; res.count = 0; res.Message = errorMessage.ToString(); res.data = null; return res; } // 6. 批量查询已存在的记录 var materialIds = list.Select(x => materialDict[$"{x["物料编码"]}_{x["物料名称"]}"]).Distinct().ToList(); var supplierIds = list.Select(x => supplierDict_sub[x["部门"]]).Distinct().ToList(); string existQuery = $"select * from h_v_CB_ItemMoneyBillMain_Edit_excel WHERE HMaterID IN ({string.Join(",", materialIds)}) AND HDeptID_sub IN ({string.Join(",", supplierIds)})"; DataSet existData = oCN.RunProcReturn(existQuery, "h_v_CB_ItemMoneyBillMain_Edit_excel"); HashSet existRecords = new HashSet(); foreach (DataRow row in existData.Tables[0].Rows) { existRecords.Add($"{row["HMaterID"]}_{row["HDeptID_sub"]}"); } // 7. 按照部门代码、部门、职员代码、职员、生产订单号进行分组 var groupedData = list.GroupBy(item => new { 部门代码 = item["部门代码"], 部门 = item["部门"], 职员代码 = item["职员代码"], 职员 = item["职员"], 生产订单号 = item["生产订单号"] }).ToList(); StringBuilder insertSql = new StringBuilder(); StringBuilder insertSql_sub = new StringBuilder(); string currentDate = DateTime.Today.ToString("yyyy-MM-dd"); foreach (var group in groupedData) { var firstItem = group.First(); // 获取组信息 int HSupplierID = supplierDict_sub[firstItem["部门"]]; int EmpID = EmpDict[firstItem["职员"]]; string ICMO = firstItem["生产订单号"]; int ICMOInterID = !string.IsNullOrEmpty(ICMO) && ICMODict.ContainsKey(ICMO) ? ICMODict[ICMO] : 0; // 生成主表ID和单据号(每组生成一个) string HBillNo = DBUtility.ClsPub.CreateBillCode("1802", ref DBUtility.ClsPub.sExeReturnInfo, true); long HInterID = DBUtility.ClsPub.CreateBillID("1802", ref DBUtility.ClsPub.sExeReturnInfo); // 检查该组是否已存在(根据部门ID和物料ID组合) bool groupExists = false; foreach (var item in group) { int HMaterID = materialDict[$"{item["物料编码"]}_{item["物料名称"]}"]; string recordKey = $"{HMaterID}_{HSupplierID}"; if (existRecords.Contains(recordKey)) { groupExists = true; break; } } if (!groupExists) { // 插入主表(每组插入一次) insertSql.AppendLine($"INSERT INTO CB_ItemMoneyBillMain ([HInterID], [HBillNo], [HDate], [HYear], [HPeriod], [HBillType], [HBillSubType], [HBillStatus], [HCheckItemNowID], [HCheckItemNextID], [HCheckFlowID], [HRemark], [HBacker], [HBackDate], [HBackRemark], [HChecker], [HCheckDate], [HMaker], [HMakeDate], [HUpDater], [HUpDateDate], [HCloseMan], [HCloseDate], [HCloseType], [HDeleteMan], [HDeleteDate], [HMainSourceBillType], [HMainSourceInterID], [HMainSourceEntryID], [HMainSourceBillNo], [HPrintQty], [HEmpID], [HDeptID], [HICMOInterID], [HMaterID], [HICMOBillNo], [HExplanation], [HInnerBillNo], [HBeginDate], [HEndDate], [HOrgID]) VALUES ("); insertSql.AppendLine($"'{HInterID}', '{HBillNo}', '{DateTime.Now}', {list[0]["年份"]}, {list[0]["月份"]}, {"1802"}, '{"1802"}', '{"1"}', '{"0"}', '{"0"}', '{"0"}', '{"excel导入"}', '{""}', '{DateTime.Now}', '{""}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{"0"}', '{""}', '{DateTime.Now}', '{""}', '{"0"}', '{"0"}', '{""}', '{"0"}', '{EmpID}', '{HSupplierID}', '{ICMOInterID}','{0}','{ICMO}','{""}','{"0"}','{DateTime.Now}','{DateTime.Now}','{"100038"}');"); // 插入子表(组内每个物料插入一次) int HEntryID = 1; foreach (var item in group) { int HMaterID = materialDict[$"{item["物料编码"]}_{item["物料名称"]}"]; int HCostID = CostDict[item["成本项目"]]; int HProcID = ProcDict[item["工序"]]; insertSql_sub.AppendLine($"INSERT INTO [CB_ItemMoneyBillSub] ([HInterID], [HEntryID], [HCloseMan], [HEntryCloseDate], [HCloseType], [HRemark],[HSourceInterID], [HSourceEntryID], [HSourceBillNo], [HSourceBillType],[HRelationQty], [HRelationMoney], [HBillNo_bak], [HCostItemID],[HDeptID], [HQty], [HPrice], [HMoney],[HMaterID], [HWaster], [HDate], [HBeginBalance], [HEndBalance], [HTimes],[HProcID]) VALUES ("); insertSql_sub.AppendLine($"'{HInterID}', '{HEntryID}', '{""}', '{DateTime.Now}', '{"0"}', '{"excel导入"}','{"0"}', '{"0"}', '{"0"}', '{"0"}','{"0"}', '{"0"}', '{""}', '{HCostID}', '{HSupplierID}', '{item["数量"]}', '{item["单价"]}', '{item["金额"]}','{HMaterID}', '{item["损耗量"] ?? "0"}', '{DateTime.Now}', '{"0"}', '{"0"}', '{"0"}', '{HProcID}');"); HEntryID++; } } } // 执行批量操作 if (insertSql.Length > 0) { oCN.RunProc(insertSql.ToString()); } if (insertSql_sub.Length > 0) { oCN.RunProc(insertSql_sub.ToString()); } oCN.Commit(); res.code = "1"; res.count = groupedData.Count; res.Message = $"导入成功! 共处理{groupedData.Count}组数据"; res.data = null; return res; } catch (Exception e) { //oCN.Rollback(); LogService.Write(e); res.code = "0"; res.count = 0; res.Message = "Exception!" + e.ToString(); res.data = null; return res; } } #endregion } }