You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1344 lines
86 KiB
C#

11 months ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.RptCwCfsEntity;
using DSWeb.MvcShipping.Models.MsCwAccitemsGl;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using System.Data.SqlClient;
using DSWeb.DataAccess;
using HcUtility.Core;
using DSWeb.Areas.CommMng.DAL;
namespace DSWeb.MvcShipping.DAL.RptCwCfsDAL
{
public class RptCwCfsDAL
{
#region 查询
static public List<RptVWCwCfsTemplateModelItemEntity> GetDataList(string sMODELGID, string sACCDATE, string USERID, string SHOWNAME, string companyid, string sort = null)
{
isSaveRow(sMODELGID, sACCDATE, USERID, companyid);
var strSql = new StringBuilder();
string sSql = GetDataListSQL(sMODELGID, sACCDATE, companyid, USERID);
strSql.Append(sSql);
//
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by LINENUM");
}
return SetData(strSql);
}
static public List<RptVWCwCfsTemplateModelItemEntity> GetDataListTZ(string sMODELGID, string sACCDATE, string USERID, string SHOWNAME, string companyid, string sort = null)
{
var strSql = new StringBuilder();
string sSql = GetDataListSQL(sMODELGID, sACCDATE, companyid, USERID);
strSql.Append(sSql);
//
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by LINENUM");
}
return SetData(strSql);
}
static public string GetDataListStr(string sMODELGID, string sACCDATE, string companyid, string strUSERID)
{
var strSql = new StringBuilder();
string sSql = GetDataListSQL(sMODELGID, sACCDATE, companyid, strUSERID);
strSql.Append("select GID as 唯一编码, LINENUM as 行号, MODELNAME as 项目方案, ITEMNAME as 项目名称, ITEMTYPE as 项目类型, ACCDATE as 会计期间, QTYLASTBLC as 本年累计金额, QTYBLC as 本月金额, QTYLASTBLC_S as [本年累计金额(系统)], QTYBLC_S as [本月金额(系统)], QTYLASTBLC_H as [本年累计金额(调动)], QTYBLC_H as [本月金额(调动)],(select top 1 STARTNAME from [cw_design_startusing] where gid=a.STARTGID and (ISDELETE=0 or ISDELETE is null)) as 账套启用,(select top 1 [NAME] from [company] where gid=a.CORPID) as 分公司,(select top 1 SHOWNAME from [user] where gid=a.MODIFIEDUSER) as 更改操作人,MODIFIEDTIME as 更改操作时间 from (");
strSql.Append(sSql);
strSql.Append(") as a order by LINENUM");
return strSql.ToString();
}
static public string GetDataListSQL(string sMODELGID, string sACCDATE, string companyid, string strUserID)
{
#region 综合本位币
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUserID);
var strSql = new StringBuilder();
strSql.Append("select * from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "'");
if (!string.IsNullOrEmpty(sMODELGID))
{
strSql.Append(" and MODELGID='" + sMODELGID + "'");
}
if (!string.IsNullOrEmpty(sACCDATE))
{
strSql.Append(" and ACCDATE='" + sACCDATE + "'");
}
//strSql.Append(" and CORPID='" + companyid + "'");//需求编号SR2017082800002
#endregion
return strSql.ToString();
}
private static List<RptVWCwCfsTemplateModelItemEntity> SetData(StringBuilder strSql)
{
var headList = new List<RptVWCwCfsTemplateModelItemEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptVWCwCfsTemplateModelItemEntity data = new RptVWCwCfsTemplateModelItemEntity();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//GID
data.MODELGID = (reader["MODELGID"] == null ? "" : Convert.ToString(reader["MODELGID"]));//MODELGID
data.ITEMGID = (reader["ITEMGID"] == null ? "" : Convert.ToString(reader["ITEMGID"]));//ITEMGID
data.ACCDATE = (reader["ACCDATE"] == null ? "" : Convert.ToString(reader["ACCDATE"]));//ACCDATE
data.QTYLASTBLC = (reader["QTYLASTBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTBLC"]));//QTYLASTBLC
data.QTYBLC = (reader["QTYBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYBLC"]));//QTYBLC
data.QTYLASTBLC_S = (reader["QTYLASTBLC_S"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTBLC_S"]));//QTYLASTBLC_S
data.QTYBLC_S = (reader["QTYBLC_S"] == null ? 0 : Convert.ToDecimal(reader["QTYBLC_S"]));//QTYBLC_S
data.QTYLASTBLC_H = (reader["QTYLASTBLC_H"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTBLC_H"]));//QTYLASTBLC_H
data.QTYBLC_H = (reader["QTYBLC_H"] == null ? 0 : Convert.ToDecimal(reader["QTYBLC_H"]));//QTYBLC_H
data.STARTGID = (reader["STARTGID"] == null ? "" : Convert.ToString(reader["STARTGID"]));//STARTGID
data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//CORPID
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//MODIFIEDUSER
if (reader["MODIFIEDTIME"] != null && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("0001") < 0 && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("1900") < 0 && reader["MODIFIEDTIME"].ToString().Trim()!="")
{
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//更改操作时间
}
data.MODELNAME = (reader["MODELNAME"] == null ? "" : Convert.ToString(reader["MODELNAME"]));//MODELNAME
data.ISDEFAULT = (reader["ISDEFAULT"] == null ? false : Convert.ToBoolean(reader["ISDEFAULT"]));//ISDEFAULT
data.ITEMNAME = (reader["ITEMNAME"] == null ? "" : Convert.ToString(reader["ITEMNAME"]));//ITEMNAME
data.LINENUM = (reader["LINENUM"] == null ? 0 : Convert.ToInt32(reader["LINENUM"]));//LINENUM
data.DETAILED = (reader["DETAILED"] == null ? false : Convert.ToBoolean(reader["DETAILED"]));//DETAILED
data.ISALTER = (reader["ISALTER"] == null ? false : Convert.ToBoolean(reader["ISALTER"]));//ISALTER
data.PACCGID = (reader["PACCGID"] == null ? "" : Convert.ToString(reader["PACCGID"]));//PACCGID
data.ITEMTYPE = (reader["ITEMTYPE"] == null ? "" : Convert.ToString(reader["ITEMTYPE"]));//ITEMTYPE
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 查询明细表
static public List<RptCwCfsDetailEntity> GetDetailList(string sACCDATE, string strCondition, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
var strSql = new StringBuilder();
strSql.Append("SELECT *,ACCNAME=ISNULL((select top 1 ACCNAME from cw_accitems_gl WITH(NOLOCK) where GID=cw_cfs_template_detail.ACCGID and [YEAR]=SUBSTRING(convert(varchar,'" + sACCDATE + "-01',23),1,4)),''),ACCDATE=(select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where GID=cw_cfs_template_detail.LINKGID),ACCIDNAME=ISNULL((select top 1 (ACCID+'-'+ACCNAME) as ACCIDNAME from cw_accitems_gl WITH(NOLOCK) where GID=cw_cfs_template_detail.ACCGID and [YEAR]=SUBSTRING(convert(varchar,'" + sACCDATE + "-01',23),1,4)),'') from cw_cfs_template_detail WITH(NOLOCK) where LINKGID in (select GID from cw_cfs_template where [STARTGID]='" + strCwSTARTGID + "')");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
strSql.Append(" order by ACCIDNAME,MODIFIEDTIME");
return SetDetail(strSql);
}
private static List<RptCwCfsDetailEntity> SetDetail(StringBuilder strSql)
{
var headList = new List<RptCwCfsDetailEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwCfsDetailEntity data = new RptCwCfsDetailEntity();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.LINKGID = (reader["LINKGID"] == null ? "" : Convert.ToString(reader["LINKGID"]));//模板GID
data.ACCGID = (reader["ACCGID"] == null ? "" : Convert.ToString(reader["ACCGID"]));//科目GID
data.OPERATORS = (reader["OPERATORS"] == null ? "" : Convert.ToString(reader["OPERATORS"]));//运算符号
data.ACCESS = (reader["ACCESS"] == null ? "" : Convert.ToString(reader["ACCESS"]));//取数规则
data.QTYLASTBLC = (reader["QTYLASTBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTBLC"]));//本年累计余额
data.QTYBLC = (reader["QTYBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYBLC"]));//本月金额
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid
if (reader["MODIFIEDTIME"] != null && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("0001") < 0 && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("1900") < 0 && reader["MODIFIEDTIME"].ToString().Trim() != "")
{
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//最后一次更新操作时间
}
data.ACCNAME = (reader["ACCNAME"] == null ? "" : Convert.ToString(reader["ACCNAME"]));//科目名称
data.ACCDATE = (reader["ACCDATE"] == null ? "" : Convert.ToString(reader["ACCDATE"]));//会计期间
data.ACCIDNAME = (reader["ACCIDNAME"] == null ? "" : Convert.ToString(reader["ACCIDNAME"]));//科目名称
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 查询项目表
static public List<RptCwCfsModelEntity> GetCwCfsModelList(string strCondition, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
var strSql = new StringBuilder();
strSql.Append("SELECT * from cw_cfs_model WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "'");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
strSql.Append(" order by ISDEFAULT,MODELNAME");
return SetCwCfsModel(strSql);
}
private static List<RptCwCfsModelEntity> SetCwCfsModel(StringBuilder strSql)
{
var headList = new List<RptCwCfsModelEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwCfsModelEntity data = new RptCwCfsModelEntity();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.MODELNAME = (reader["MODELNAME"] == null ? "" : Convert.ToString(reader["MODELNAME"]));//方案名称
data.ISDEFAULT = (reader["ISDEFAULT"] == null ? false : Convert.ToBoolean(reader["ISDEFAULT"]));//是否默认方案并不允许删除
data.STARTGID = (reader["STARTGID"] == null ? "" : Convert.ToString(reader["STARTGID"]));//账套启用GID
data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid
if (reader["MODIFIEDTIME"] != null && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("0001") < 0 && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("1900") < 0 && reader["MODIFIEDTIME"].ToString().Trim() != "")
{
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//最后一次更新操作时间
}
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 项目维护列表
static public List<RptCwCfsModelItemEntity> GetCwCfsModelItemList(string sMODELGID, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
var strSql = new StringBuilder();
strSql.Append("SELECT * from cw_cfs_model_item WITH(NOLOCK) where PACCGID='0' and MODELGID in (SELECT GID from cw_cfs_model WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "')");
if (!string.IsNullOrEmpty(sMODELGID))
{
strSql.Append(" and MODELGID='" + sMODELGID + "'");
}
strSql.Append(" order by LINENUM");
return SetCwCfsModelItem(strSql);
}
static public List<RptCwCfsModelItemEntity> GetCwCfsModelItemList2(string sITEMGID, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
var strSql = new StringBuilder();
strSql.Append("SELECT * from cw_cfs_model_item WITH(NOLOCK) where PACCGID<>'0' and ITEMTYPE<>'' and MODELGID in (SELECT GID from cw_cfs_model WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "')");
if (!string.IsNullOrEmpty(sITEMGID))
{
strSql.Append(" and PACCGID='" + sITEMGID + "'");
}
strSql.Append(" order by LINENUM");
return SetCwCfsModelItem(strSql);
}
private static List<RptCwCfsModelItemEntity> SetCwCfsModelItem(StringBuilder strSql)
{
var headList = new List<RptCwCfsModelItemEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwCfsModelItemEntity data = new RptCwCfsModelItemEntity();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.MODELGID = (reader["MODELGID"] == null ? "" : Convert.ToString(reader["MODELGID"]));//方案GID
data.ITEMNAME = (reader["ITEMNAME"] == null ? "" : Convert.ToString(reader["ITEMNAME"]));//项目名称
data.LINENUM = (reader["LINENUM"] == null ? 0 : Convert.ToInt32(reader["LINENUM"]));//项目行次
data.DETAILED = (reader["DETAILED"] == null ? false : Convert.ToBoolean(reader["DETAILED"]));//末级科目(标志)
data.ISALTER = (reader["ISALTER"] == null ? false : Convert.ToBoolean(reader["ISALTER"]));//是否允许修改项目
data.PACCGID = (reader["PACCGID"] == null ? "" : Convert.ToString(reader["PACCGID"]));//父级科目代码
data.ITEMTYPE = (reader["ITEMTYPE"] == null ? "" : Convert.ToString(reader["ITEMTYPE"]));//项目属性
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid
if (reader["MODIFIEDTIME"] != null && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("0001") < 0 && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("1900") < 0 && reader["MODIFIEDTIME"].ToString().Trim() != "")
{
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//最后一次更新操作时间
}
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#region 项目重新排序
static public void GetSort(string sMODELGID, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
T_ALL_DA T_ALL_DA = new T_ALL_DA();
string sSQL = "SELECT * from cw_cfs_model_item WITH(NOLOCK) where MODELGID in (SELECT GID from cw_cfs_model WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "')";
if (!string.IsNullOrEmpty(sMODELGID))
{
sSQL += " and MODELGID='" + sMODELGID + "'";
}
sSQL += " order by LINENUM,PACCGID,ITEMTYPE desc";
DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sSQL = "update cw_cfs_model_item set LINENUM=" + (i + 1) + " where gid='" + ds.Tables[0].Rows[i]["GID"].ToString() + "'";
bool bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
}
}
}
}
#endregion
#region 新建方案//需求编号SR2017082800002
static public DBResult onAddModel(string dataDiv, string USERID, string companyid)
{
var result = new DBResult();
if (string.IsNullOrEmpty(dataDiv))
{
result.Success = false;
result.Message = "方案名称不允许为空!";
return result;
}
//
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(USERID);
string strCwACCDATE = BasicDataRefDAL.GetCwACCDATE(USERID);
T_ALL_DA T_ALL_DA = new T_ALL_DA();
string strGID = Guid.NewGuid().ToString();
string sSQL = "select top 1 GID from [cw_cfs_model] where ISDEFAULT=1 and STARTGID='" + strCwSTARTGID + "'";// and [MODELNAME]='默认方案'
string sGIDMR = T_ALL_DA.GetStrSQL("GID",sSQL);
if (sGIDMR == "")
{
sSQL = "select top 1 GID from [cw_cfs_model] where ISDEFAULT=1";// and [MODELNAME]='默认方案' and STARTGID='" + strCwSTARTGID + "'
sGIDMR = T_ALL_DA.GetStrSQL("GID", sSQL);
if (sGIDMR == "")
{
result.Success = false;
result.Message = "没有默认方案,请联系管理员!";
return result;
}
}
//
sSQL = "SELECT * from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sGIDMR + "' and [PACCGID]='0'";
DataSet dsMR = T_ALL_DA.GetAllSQL(sSQL);
if (dsMR == null)
{
result.Success = false;
result.Message = "没有默认方案项目,请联系管理员!";
return result;
}
else
{
if (dsMR.Tables[0].Rows.Count == 0)
{
result.Success = false;
result.Message = "没有默认方案项目,请联系管理员!";
return result;
}
}
//
string strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where MODELGID='" + sGIDMR + "' and ITEMGID in (SELECT gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sGIDMR + "') group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
result.Success = false;
result.Message = "没有默认方案,请联系管理员!";
return result;
}
//
sSQL = "SELECT * from cw_cfs_template WITH(NOLOCK) where MODELGID='" + sGIDMR + "' and ITEMGID in (SELECT gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sGIDMR + "') and ACCDATE='" + strACCDATE + "'";//STARTGID='" + strCwSTARTGID + "' and
DataSet dsFAXM = T_ALL_DA.GetAllSQL(sSQL);
if (dsFAXM == null)
{
result.Success = false;
result.Message = "没有默认项目,请联系管理员!";
return result;
}
else
{
if (dsFAXM.Tables[0].Rows.Count == 0)
{
result.Success = false;
result.Message = "没有默认项目,请联系管理员!";
return result;
}
}
//
sSQL = "SELECT * from cw_cfs_template_detail WITH(NOLOCK) where LINKGID in (select gid from cw_cfs_template where MODELGID='" + sGIDMR + "' and ITEMGID in (SELECT gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sGIDMR + "') and ACCDATE='" + strACCDATE + "')";//STARTGID='" + strCwSTARTGID + "' and
DataSet dsXMGS = T_ALL_DA.GetAllSQL(sSQL);
if (dsXMGS == null)
{
result.Success = false;
result.Message = "没有默认项目公式,请联系管理员!";
return result;
}
else
{
if (dsXMGS.Tables[0].Rows.Count == 0)
{
result.Success = false;
result.Message = "没有默认项目公式,请联系管理员!";
return result;
}
}
//插入新方案
sSQL = "INSERT INTO [cw_cfs_model] ([GID],[MODELNAME],[ISDEFAULT],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) VALUES ('" + strGID + "','" + dataDiv + "',0,'" + strCwSTARTGID + "','" + companyid + "','" + USERID + "',getdate())";
bool bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
if (bl)
{
//根据默认方案,插入新方案项目
sSQL = "INSERT INTO [cw_cfs_model_item] ([GID],[MODELGID],[ITEMNAME],[LINENUM],[DETAILED],[ISALTER],[PACCGID],[ITEMTYPE],[MODIFIEDUSER],[MODIFIEDTIME])"
+ "select (case when len(LINENUM)>1 then ('" + strGID.Substring(0, strGID.Length - 2) + "'+ CONVERT(nvarchar(2), [LINENUM])) else ('" + strGID.Substring(0, strGID.Length - 2) + "'+'0'+CONVERT(nvarchar(2), [LINENUM])) end) as [GID],'" + strGID + "' as [MODELGID],[ITEMNAME],[LINENUM],[DETAILED],[ISALTER],[PACCGID],[ITEMTYPE],[MODIFIEDUSER],[MODIFIEDTIME] from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sGIDMR + "'";
bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
if (bl)
{
//查出新方案中的项目大类
sSQL = "SELECT * from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + strGID + "' and [PACCGID]='0'";
DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//更新新方案项目的父类GID
sSQL = "update [cw_cfs_model_item] set [PACCGID]='" + ds.Tables[0].Rows[i]["GID"].ToString() + "' where [MODELGID]='" + strGID + "' and [PACCGID]<>'0' and [PACCGID]='" + dsMR.Tables[0].Rows[i]["GID"].ToString() + "'";
bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
}
}
}
}
//根据默认方案,插入新默认项目
sSQL = "INSERT INTO [cw_cfs_template]([GID],[MODELGID],[ITEMGID],[ACCDATE],[QTYLASTBLC],[QTYBLC],[QTYLASTBLC_S],[QTYBLC_S],[QTYLASTBLC_H],[QTYBLC_H],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ "select newid() as [GID],'" + strGID + "' as [MODELGID],[ITEMGID]"
//+ ",[ITEMGID]=(select top 1 gid from cw_cfs_model_item where LINENUM=(select top 1 LINENUM from cw_cfs_model_item where gid=cw_cfs_template.ITEMGID))"
+ ",'" + strCwACCDATE + "' as [ACCDATE],0 as [QTYLASTBLC],0 as [QTYBLC],0 as [QTYLASTBLC_S],0 as [QTYBLC_S],0 as [QTYLASTBLC_H],0 as [QTYBLC_H],'" + strCwSTARTGID + "' as [STARTGID],'" + companyid + "' as [CORPID],'" + USERID + "' as [MODIFIEDUSER],getdate() as [MODIFIEDTIME] from cw_cfs_template WITH(NOLOCK) where MODELGID='" + sGIDMR + "' and ACCDATE='" + strACCDATE + "'";
bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
if (!bl)
{
result.Success = false;
result.Message = "插入新项目失败,请联系管理员!";
return result;
}
//根据默认方案,插入新默认项目公式
sSQL = "INSERT INTO [cw_cfs_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME])"
+ "select newid() as [GID]"
+ ",[LINKGID]=(select top 1 gid from cw_cfs_template where MODELGID='" + strGID + "' and ITEMGID=(select top 1 ITEMGID from cw_cfs_template where MODELGID='" + sGIDMR + "' and GID=cw_cfs_template_detail.LINKGID))"
+ ",[ACCGID]=(select top 1 gid from cw_accitems_gl where STARTGID='" + strCwSTARTGID + "' and accid=(select top 1 accid from cw_accitems_gl where gid=cw_cfs_template_detail.ACCGID))"
+ ",[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + USERID + "' as [MODIFIEDUSER],getdate() as [MODIFIEDTIME] from cw_cfs_template_detail WITH(NOLOCK) where [LINKGID] in (select gid from cw_cfs_template where MODELGID='" + sGIDMR + "' and ACCDATE='" + strACCDATE + "')";
bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
if (!bl)
{
result.Success = false;
result.Message = "插入新项目公式失败,请联系管理员!";
return result;
}
//根据默认方案,插入新默认项目
sSQL = "update [cw_cfs_template] set [ITEMGID]=(select top 1 gid from cw_cfs_model_item where MODELGID='" + strGID + "' and LINENUM=(select top 1 LINENUM from cw_cfs_model_item where gid=cw_cfs_template.ITEMGID))"
+ " where MODELGID='" + strGID + "' and ACCDATE='" + strCwACCDATE + "'";
bl = T_ALL_DA.GetExecuteSqlCommand(sSQL);
if (!bl)
{
result.Success = false;
result.Message = "更新新项目失败,请联系管理员!";
return result;
}
}
else
{
result.Success = false;
result.Message = "操作出错!";
return result;
}
result.Success = true;
result.Message = "操作成功";
return result;
}
#endregion
#endregion
#region 删除明细表
public static DBResult DeleteDetail(RptCwCfsDetailEntity head)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdDelete = db.GetSqlStringCommand("delete from cw_cfs_template_detail where GID='" + head.GID.ToString().Trim() + "'");
db.ExecuteNonQuery(cmdDelete, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "删除出现错误,请重试!";
return result;
}
}
result.Success = true;
result.Message = "操作成功";
return result;
}
#endregion
#region 重新计算
static public void GetRecount(string sMODELGID, string sACCDATE, string USERID, string companyid)
{
isSaveRow(sMODELGID, sACCDATE, USERID, companyid);
SaveRow(sMODELGID, sACCDATE, companyid, USERID);
}
#endregion
#region 按规则合计明细表金额
public static void SaveRowDetail(string sACCDATE, string strCondition, string strUSERID)
{
string strVOUDATE = sACCDATE + "-01";
string sYEAR = DateTime.Parse(strVOUDATE).Year.ToString();
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
#region 更新_明细表金额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iResult = 0;
#region 损益发生额_old
//string sSQL = "update cw_cfs_template_detail set"
//+ " QTYLASTBLC=isnull(("
//+ "isnull((select QTYLASTBLC from (select LINKGID,isnull(sum(case when ACCATTRIBUTE<>'损益' then QTYLASTBLC ELSE (case when DC='借' then QTYDR ELSE QTYCR END) END),0) as QTYLASTBLC"//累计余额
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID),0)"
//+ "-"
//+ "isnull((select QTYYEARBLC from (select LINKGID,isnull(sum(case when ACCATTRIBUTE<>'损益' then QTYYEARBLC ELSE 0 END),0) as QTYYEARBLC"//累计余额
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and SUBSTRING(ACCDATE,1,4)=SUBSTRING('" + sACCDATE + "',1,4) and IsInitialEntry=1"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID),0)"
//+ "),0)"
//+ ",QTYBLC=isnull(("
//+ "select QTYBLC from (select LINKGID,isnull(sum(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE (case when DC='借' then QTYDR ELSE QTYCR END) END),0) as QTYBLC"//累计余额
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
//+ "),0)"
//+ " where ACCESS='损益发生额' and " + strCondition;
#endregion
#region 损益发生额
string sSQL = "update cw_cfs_template_detail set"
+ " QTYLASTBLC=isnull(("
+ "select QTYBLC from (select LINKGID,isnull(sum(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE (case when DC='借' then QTYDR ELSE QTYCR END) END),0) as QTYBLC"//累计余额
+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE<='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ ",QTYBLC=isnull(("
+ "select QTYBLC from (select LINKGID,isnull(sum(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE (case when DC='借' then QTYDR ELSE QTYCR END) END),0) as QTYBLC"//累计余额
+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ " where ACCESS='损益发生额' and LINKGID in (select GID from cw_cfs_template where [STARTGID]='" + strCwSTARTGID + "') and " + strCondition;
#endregion
iResult = -1;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 借方发生额_old
//sSQL = "update cw_cfs_template_detail set"
//+ " QTYLASTBLC=isnull(("
//+ "isnull((select QTYLASTBLC from (select LINKGID,isnull(sum(case when DC='贷' then (-(case when ACCATTRIBUTE<>'损益' then QTYLASTBLC ELSE QTYDR END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYLASTBLC ELSE QTYDR END) END),0) as QTYLASTBLC"//累计借方
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID),0)"
//+ "-"
//+ "isnull((select QTYYEARBLC from (select LINKGID,isnull(sum(case when DC='贷' then (-(case when ACCATTRIBUTE<>'损益' then QTYYEARBLC ELSE 0 END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYYEARBLC ELSE 0 END) END),0) as QTYYEARBLC"//累计借方
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and SUBSTRING(ACCDATE,1,4)=SUBSTRING('" + sACCDATE + "',1,4) and IsInitialEntry=1"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID),0)"
//+ "),0)"
//+ ",QTYBLC=isnull(("
//+ "select QTYBLC from (select LINKGID,isnull(sum(case when DC='贷' then (-(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYDR END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYDR END) END),0) as QTYBLC"//累计借方
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
//+ "),0)"
//+ " where ACCESS='借方发生额' and " + strCondition;
#endregion
#region 借方发生额
sSQL = "update cw_cfs_template_detail set"
+ " QTYLASTBLC=isnull(("
+ "select QTYBLC from (select LINKGID,isnull(sum(case when DC='贷' then (-(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYDR END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYDR END) END),0) as QTYBLC"//累计借方
+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE<='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ ",QTYBLC=isnull(("
+ "select QTYBLC from (select LINKGID,isnull(sum(case when DC='贷' then (-(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYDR END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYDR END) END),0) as QTYBLC"//累计借方
+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ " where ACCESS='借方发生额' and LINKGID in (select GID from cw_cfs_template where [STARTGID]='" + strCwSTARTGID + "') and " + strCondition;
#endregion
iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 贷方发生额_old
//sSQL = "update cw_cfs_template_detail set"
//+ " QTYLASTBLC=isnull(("
//+ "isnull((select QTYLASTBLC from (select LINKGID,isnull(sum(case when DC='借' then (-(case when ACCATTRIBUTE<>'损益' then QTYLASTBLC ELSE QTYCR END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYLASTBLC ELSE QTYCR END) END),0) as QTYLASTBLC"//累计贷方
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID),0)"
//+ "-"
//+ "isnull((select QTYYEARBLC from (select LINKGID,isnull(sum(case when DC='借' then (-(case when ACCATTRIBUTE<>'损益' then QTYYEARBLC ELSE 0 END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYYEARBLC ELSE 0 END) END),0) as QTYYEARBLC"//累计贷方
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and SUBSTRING(ACCDATE,1,4)=SUBSTRING('" + sACCDATE + "',1,4) and IsInitialEntry=1"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID),0)"
//+ "),0)"
//+ ",QTYBLC=isnull(("
//+ "select QTYBLC from (select LINKGID,isnull(sum(case when DC='借' then (-(case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYCR END)) ELSE (case when ACCATTRIBUTE<>'损益' then QTYBLC ELSE QTYCR END) END),0) as QTYBLC"//累计贷方
//+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
//+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
//+ " and STARTGID='" + strSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
//+ "),0)"
//+ " where ACCESS='贷方发生额' and " + strCondition;
#endregion
#region 贷方发生额
sSQL = "update cw_cfs_template_detail set"
+ " QTYLASTBLC=isnull(("
+ "select QTYCR from (select LINKGID,isnull(sum(case when DC='借' then (-QTYCR) ELSE QTYCR END),0) as QTYCR"//累计贷方
+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE<='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ ",QTYBLC=isnull(("
+ "select QTYCR from (select LINKGID,isnull(sum(case when DC='借' then (-QTYCR) ELSE QTYCR END),0) as QTYCR"//累计贷方
+ " from VW_cw_genleg_accitems_gl WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ " where ACCESS='贷方发生额' and LINKGID in (select GID from cw_cfs_template where [STARTGID]='" + strCwSTARTGID + "') and " + strCondition;
#endregion
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 期初数_本年\本月期初数
sSQL = "update cw_cfs_template_detail set"
+ " QTYLASTBLC=isnull(("
+ "select QTYYEARBLC from (select LINKGID,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC"//累计余额
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where IsInitialEntry=1 and ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "'"
+ " and [YEAR]='" + sACCDATE.Trim().Substring(0, 4) + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ ",QTYBLC=isnull(("
+ "select QTYYEARBLC from (select LINKGID,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC"//累计余额
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_cfs_template_detail.ACCGID"
+ "),0)"
+ " where ACCESS='期初数' and LINKGID in (select GID from cw_cfs_template where [STARTGID]='" + strCwSTARTGID + "') and " + strCondition;
#endregion
iResult = -4;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
#endregion
#region 按规则合计主表金额
public static void SaveRow(string sMODELGID, string sACCDATE, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
#region 更新_明细表金额
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string strSql = GetDataListSQL(sMODELGID, sACCDATE, companyid, strUSERID) + " order by LINENUM";
DataSet ds = T_ALL_DA.GetAllSQL(strSql);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string strCondition = "LINKGID='" + ds.Tables[0].Rows[i]["GID"].ToString() + "'";
SaveRowDetail(sACCDATE, strCondition, strUSERID);
}
}
}
#endregion
#region 更新_主表明细金额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC_S=isnull((select sum((case when OPERATORS='-' then (-QTYLASTBLC) ELSE QTYLASTBLC end)) from cw_cfs_template_detail WITH(NOLOCK) where LINKGID=cw_cfs_template.GID),0)"
+ ",QTYBLC_S=isnull((select sum((case when OPERATORS='-' then (-QTYBLC) ELSE QTYBLC end)) from cw_cfs_template_detail where LINKGID=cw_cfs_template.GID),0)"
+ " where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "'";
int iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_“一、二、三”的现金流量净额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC_S=isnull((select sum((case when ITEMTYPE='现金流入' then QTYLASTBLC_S else (-QTYLASTBLC_S) end)) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE<>'' and PACCGID in (select PACCGID from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where ITEMGID=cw_cfs_template.ITEMGID)),0)"
+ ",QTYBLC_S=isnull((select sum((case when ITEMTYPE='现金流入' then QTYBLC_S else (-QTYBLC_S) end)) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE<>'' and PACCGID in (select PACCGID from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where ITEMGID=cw_cfs_template.ITEMGID)),0)"
+ ",QTYLASTBLC_H=isnull((select sum((case when ITEMTYPE='现金流入' then QTYLASTBLC_H else (-QTYLASTBLC_H) end)) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE<>'' and PACCGID in (select PACCGID from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where ITEMGID=cw_cfs_template.ITEMGID)),0)"
+ ",QTYBLC_H=isnull((select sum((case when ITEMTYPE='现金流入' then QTYBLC_H else (-QTYBLC_H) end)) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE<>'' and PACCGID in (select PACCGID from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where ITEMGID=cw_cfs_template.ITEMGID)),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID in (SELECT gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and DETAILED=1 and ITEMTYPE='') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -16;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_四、现金净增加额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC_S=isnull((select sum(QTYLASTBLC_S) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE=''),0)"
+ ",QTYBLC_S=isnull((select sum(QTYBLC_S) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE=''),0)"
+ ",QTYLASTBLC_H=isnull((select sum(QTYLASTBLC_H) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE=''),0)"
+ ",QTYBLC_H=isnull((select sum(QTYBLC_H) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and DETAILED=1 and ITEMTYPE=''),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID in (SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%四、现金净增加额%' and DETAILED=0 and ITEMTYPE='') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -17;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 五、期末现金余额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC_S=isnull((select sum(QTYLASTBLC_S) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and (ITEMNAME like '%四、现金净增加额%' or PACCGID in (SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%四、现金净增加额%' and DETAILED=0 and ITEMTYPE=''))),0)"
+ ",QTYBLC_S=isnull((select sum(QTYBLC_S) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and (ITEMNAME like '%四、现金净增加额%' or PACCGID in (SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%四、现金净增加额%' and DETAILED=0 and ITEMTYPE=''))),0)"
+ ",QTYLASTBLC_H=isnull((select sum(QTYLASTBLC_H) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and (ITEMNAME like '%四、现金净增加额%' or PACCGID in (SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%四、现金净增加额%' and DETAILED=0 and ITEMTYPE=''))),0)"
+ ",QTYBLC_H=isnull((select sum(QTYBLC_H) from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and (ITEMNAME like '%四、现金净增加额%' or PACCGID in (SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%四、现金净增加额%' and DETAILED=0 and ITEMTYPE=''))),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID in (SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%五、期末现金余额%' and DETAILED=0 and ITEMTYPE='') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -18;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_各个项目的合计字段
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC=isnull((QTYLASTBLC_S+QTYLASTBLC_H),0)"
+ ",QTYBLC=isnull((QTYBLC_S+QTYBLC_H),0)"
+ " where STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "'";
int iResult = -19;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_调整本年“一、二、三”的负数
DataSet dsVW = T_ALL_DA.GetAllSQL("select * from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and (ITEMNAME LIKE '%收到其他与经营活动有关的现金%' or ITEMNAME LIKE '%支付其他与经营活动有关的现金%' or ITEMNAME LIKE '%收回短期投资、长期债券投资和长期股权投资收到的现金%' or ITEMNAME LIKE '%短期投资、长期债券投资和长期股权投资支付的现金%' or ITEMNAME LIKE '%取得借款收到的现金%' or ITEMNAME LIKE '%偿还借款本金支付的现金%') and QTYLASTBLC<0 order by LINENUM");
if (dsVW != null)
{
if (dsVW.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsVW.Tables[0].Rows.Count; i++)
{
#region 提取_调整的项目
string sITEM = "";
string sITEM2 = "";
if (dsVW.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("收到其他与经营活动有关的现金") > -1)
{
sITEM = "收到其他与经营活动有关的现金";
sITEM2 = "支付其他与经营活动有关的现金";
}
else if (dsVW.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("支付其他与经营活动有关的现金") > -1)
{
sITEM = "支付其他与经营活动有关的现金";
sITEM2 = "收到其他与经营活动有关的现金";
}
else if (dsVW.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("收回短期投资、长期债券投资和长期股权投资收到的现金") > -1)
{
sITEM = "收回短期投资、长期债券投资和长期股权投资收到的现金";
sITEM2 = "短期投资、长期债券投资和长期股权投资支付的现金";
}
else if (dsVW.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("短期投资、长期债券投资和长期股权投资支付的现金") > -1)
{
sITEM = "短期投资、长期债券投资和长期股权投资支付的现金";
sITEM2 = "收回短期投资、长期债券投资和长期股权投资收到的现金";
}
else if (dsVW.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("取得借款收到的现金") > -1)
{
sITEM = "取得借款收到的现金";
sITEM2 = "偿还借款本金支付的现金";
}
else if (dsVW.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("偿还借款本金支付的现金") > -1)
{
sITEM = "偿还借款本金支付的现金";
sITEM2 = "取得借款收到的现金";
}
#endregion
#region 更新_调整项目 sITEM2 的负数
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC=isnull((QTYLASTBLC+(" + dsVW.Tables[0].Rows[i]["QTYLASTBLC"].ToString() + "*-1)),0)"
+ ",QTYLASTBLC_S=isnull((QTYLASTBLC_S+(" + dsVW.Tables[0].Rows[i]["QTYLASTBLC_S"].ToString() + "*-1)),0)"
+ ",QTYLASTBLC_H=isnull((QTYLASTBLC_H+(" + dsVW.Tables[0].Rows[i]["QTYLASTBLC_H"].ToString() + "*-1)),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID=(SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%" + sITEM2 + "%' and DETAILED=1 and ITEMTYPE<>'') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -4;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_调整项目 sITEM 的负数
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC=isnull((QTYLASTBLC-(" + dsVW.Tables[0].Rows[i]["QTYLASTBLC"].ToString() + ")),0)"
+ ",QTYLASTBLC_S=isnull((QTYLASTBLC_S-(" + dsVW.Tables[0].Rows[i]["QTYLASTBLC_S"].ToString() + ")),0)"
+ ",QTYLASTBLC_H=isnull((QTYLASTBLC_H-(" + dsVW.Tables[0].Rows[i]["QTYLASTBLC_H"].ToString() + ")),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID=(SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%" + sITEM + "%' and DETAILED=1 and ITEMTYPE<>'') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
}
}
#endregion
#region 更新_调整本月“一、二、三”的负数
DataSet dsVWM = T_ALL_DA.GetAllSQL("select * from VW_CwCfsTemplate_ModelItem WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and (ITEMNAME LIKE '%收到其他与经营活动有关的现金%' or ITEMNAME LIKE '%支付其他与经营活动有关的现金%' or ITEMNAME LIKE '%收回短期投资、长期债券投资和长期股权投资收到的现金%' or ITEMNAME LIKE '%短期投资、长期债券投资和长期股权投资支付的现金%' or ITEMNAME LIKE '%取得借款收到的现金%' or ITEMNAME LIKE '%偿还借款本金支付的现金%') and QTYBLC<0 order by LINENUM");
if (dsVWM != null)
{
if (dsVWM.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsVWM.Tables[0].Rows.Count; i++)
{
#region 提取_调整的项目
string sITEM = "";
string sITEM2 = "";
if (dsVWM.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("收到其他与经营活动有关的现金") > -1)
{
sITEM = "收到其他与经营活动有关的现金";
sITEM2 = "支付其他与经营活动有关的现金";
}
else if (dsVWM.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("支付其他与经营活动有关的现金") > -1)
{
sITEM = "支付其他与经营活动有关的现金";
sITEM2 = "收到其他与经营活动有关的现金";
}
else if (dsVWM.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("收回短期投资、长期债券投资和长期股权投资收到的现金") > -1)
{
sITEM = "收回短期投资、长期债券投资和长期股权投资收到的现金";
sITEM2 = "短期投资、长期债券投资和长期股权投资支付的现金";
}
else if (dsVWM.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("短期投资、长期债券投资和长期股权投资支付的现金") > -1)
{
sITEM = "短期投资、长期债券投资和长期股权投资支付的现金";
sITEM2 = "收回短期投资、长期债券投资和长期股权投资收到的现金";
}
else if (dsVWM.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("取得借款收到的现金") > -1)
{
sITEM = "取得借款收到的现金";
sITEM2 = "偿还借款本金支付的现金";
}
else if (dsVWM.Tables[0].Rows[i]["ITEMNAME"].ToString().IndexOf("偿还借款本金支付的现金") > -1)
{
sITEM = "偿还借款本金支付的现金";
sITEM2 = "取得借款收到的现金";
}
#endregion
#region 更新_调整项目 sITEM2 的负数
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYBLC=isnull((QTYBLC+(" + dsVWM.Tables[0].Rows[i]["QTYBLC"].ToString() + "*-1)),0)"
+ ",QTYBLC_S=isnull((QTYBLC_S+(" + dsVWM.Tables[0].Rows[i]["QTYBLC_S"].ToString() + "*-1)),0)"
+ ",QTYBLC_H=isnull((QTYBLC_H+(" + dsVWM.Tables[0].Rows[i]["QTYBLC_H"].ToString() + "*-1)),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID=(SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%" + sITEM2 + "%' and DETAILED=1 and ITEMTYPE<>'') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -4;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_调整项目 sITEM 的负数
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYBLC=isnull((QTYBLC-(" + dsVWM.Tables[0].Rows[i]["QTYBLC"].ToString() + ")),0)"
+ ",QTYBLC_S=isnull((QTYBLC_S-(" + dsVWM.Tables[0].Rows[i]["QTYBLC_S"].ToString() + ")),0)"
+ ",QTYBLC_H=isnull((QTYBLC_H-(" + dsVWM.Tables[0].Rows[i]["QTYBLC_H"].ToString() + ")),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and ITEMGID=(SELECT top 1 gid from cw_cfs_model_item WITH(NOLOCK) where MODELGID='" + sMODELGID + "' and ITEMNAME like '%" + sITEM + "%' and DETAILED=1 and ITEMTYPE<>'') and STARTGID='" + strCwSTARTGID + "'";
int iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
}
}
#endregion
}
#endregion
#region 判断会计期间内是否有模板
public static void isSaveRow(string sMODELGID, string sACCDATE, string USERID, string companyid)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(USERID);
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string sMODELGID_MR = "";
string strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
//当前方案本期无数据,取本年其他会计期间的数据
strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and SUBSTRING(convert(varchar,(ACCDATE + '-01'),23),1,4)=SUBSTRING(convert(varchar,'" + sACCDATE + "-01',23),1,4) group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
//当前方案无本年数据,取默认方案的本年数据
sMODELGID_MR = T_ALL_DA.GetStrSQL("GID", "select top 1 GID from [cw_cfs_model] where ISDEFAULT=1 and STARTGID='" + strCwSTARTGID + "'");
strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID_MR + "' and SUBSTRING(convert(varchar,(ACCDATE + '-01'),23),1,4)=SUBSTRING(convert(varchar,'" + sACCDATE + "-01',23),1,4) group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
//当默认方案无本年数据,取默认方案的其他年限的数据
strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID_MR + "' group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
//当默认方案无本年数据,取默认方案的其他帐套的数据
strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where MODELGID='" + sMODELGID_MR + "' group by ACCDATE order by ACCDATE desc");
}
}
}
}
if (strACCDATE != sACCDATE)
{
if (sMODELGID_MR == "")
{
#region 插入当前会计期间的数据
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
//主表
string sSQL = "INSERT INTO [cw_cfs_template]([GID],[MODELGID],[ITEMGID],[ACCDATE],[QTYLASTBLC],[QTYBLC],[QTYLASTBLC_S],[QTYBLC_S],[QTYLASTBLC_H],[QTYBLC_H],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],[MODELGID],[ITEMGID],'" + sACCDATE + "' as [ACCDATE],0 as [QTYLASTBLC],0 as [QTYBLC],0 as [QTYLASTBLC_S],0 as [QTYBLC_S],0 as [QTYLASTBLC_H],0 as [QTYBLC_H],[STARTGID],'" + companyid + "' as [CORPID],'" + USERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_cfs_template] where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID + "'";
int iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//明细表
sSQL = "INSERT INTO [cw_cfs_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=isnull((SELECT top 1 [GID] FROM [cw_cfs_template] where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' and MODELGID='" + sMODELGID + "' and ITEMGID=(select top 1 gid from cw_cfs_model_item where MODELGID='" + sMODELGID + "' and ITEMNAME=(select top 1 ITEMNAME from cw_cfs_model_item where MODELGID='" + sMODELGID + "' and gid=(select top 1 ITEMGID from cw_cfs_template where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID + "' and STARTGID='" + strCwSTARTGID + "' and gid=[cw_cfs_template_detail].LINKGID)))),'')"
+ ",[ACCGID]=(SELECT top 1 [GID] FROM [cw_accitems_gl] where STARTGID='" + strCwSTARTGID + "' and ACCID=(select top 1 ACCID from cw_accitems_gl WITH(NOLOCK) where GID=cw_cfs_template_detail.ACCGID) and [year]='" + sACCDATE.Trim().Substring(0, 4) + "')"
+ ",[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + USERID + "' as [MODIFIEDUSER],GETDATE()"
+ " FROM [cw_cfs_template_detail] WITH(NOLOCK)"
+ " where LINKGID in (SELECT [GID] FROM [cw_cfs_template] WITH(NOLOCK) where ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID + "' and STARTGID='" + strCwSTARTGID + "')";
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
else
{
#region 插入当前会计期间的数据_根据默认方案
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
//主表
string sSQL = "INSERT INTO [cw_cfs_template]([GID],[MODELGID],[ITEMGID],[ACCDATE],[QTYLASTBLC],[QTYBLC],[QTYLASTBLC_S],[QTYBLC_S],[QTYLASTBLC_H],[QTYBLC_H],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],'" + sMODELGID + "' as [sMODELGID]"
+ ",[ITEMGID]=isnull((select top 1 GID from cw_cfs_model_item where MODELGID='" + sMODELGID + "' and ITEMNAME=(select top 1 ITEMNAME from cw_cfs_model_item where MODELGID='" + sMODELGID_MR + "' and gid=[cw_cfs_template].ITEMGID)),'')"
+ ",'" + sACCDATE + "' as [ACCDATE],0 as [QTYLASTBLC],0 as [QTYBLC],0 as [QTYLASTBLC_S],0 as [QTYBLC_S],0 as [QTYLASTBLC_H],0 as [QTYBLC_H],[STARTGID],'" + companyid + "' as [CORPID],'" + USERID + "' as [MODIFIEDUSER],GETDATE()"
+ " FROM [cw_cfs_template] where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID_MR + "'";
int iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//明细表
sSQL = "INSERT INTO [cw_cfs_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=isnull((SELECT top 1 [GID] FROM [cw_cfs_template] where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' and STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID + "' and ITEMGID=(select top 1 gid from cw_cfs_model_item where MODELGID='" + sMODELGID + "' and ITEMNAME=(select top 1 ITEMNAME from cw_cfs_model_item where MODELGID='" + sMODELGID_MR + "' and gid=(select top 1 ITEMGID from cw_cfs_template where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + strACCDATE + "' and STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID_MR + "' and gid=[cw_cfs_template_detail].LINKGID)))),'')"
+ ",[ACCGID],[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + USERID + "' as [MODIFIEDUSER],GETDATE()"
+ " FROM [cw_cfs_template_detail] WITH(NOLOCK)"
+ " where LINKGID in (SELECT [GID] FROM [cw_cfs_template] WITH(NOLOCK) where ACCDATE='" + strACCDATE + "' and STARTGID='" + strCwSTARTGID + "' and MODELGID='" + sMODELGID_MR + "')";
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
}
else
{
if (sMODELGID_MR != "")
{
#region 插入当前会计期间的数据_根据默认方案
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
//主表
string sSQL = "INSERT INTO [cw_cfs_template]([GID],[MODELGID],[ITEMGID],[ACCDATE],[QTYLASTBLC],[QTYBLC],[QTYLASTBLC_S],[QTYBLC_S],[QTYLASTBLC_H],[QTYBLC_H],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],'" + sMODELGID + "' as [sMODELGID]"
+ ",[ITEMGID]=isnull((select top 1 GID from cw_cfs_model_item where MODELGID='" + sMODELGID + "' and ITEMNAME=(select top 1 ITEMNAME from cw_cfs_model_item where MODELGID='" + sMODELGID_MR + "' and gid=[cw_cfs_template].ITEMGID)),'')"
+ ",'" + sACCDATE + "' as [ACCDATE],0 as [QTYLASTBLC],0 as [QTYBLC],0 as [QTYLASTBLC_S],0 as [QTYBLC_S],0 as [QTYLASTBLC_H],0 as [QTYBLC_H],[STARTGID],'" + companyid + "' as [CORPID],'" + USERID + "' as [MODIFIEDUSER],GETDATE()"
+ " FROM [cw_cfs_template] where ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID_MR + "' and STARTGID='" + strCwSTARTGID + "'";
int iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//明细表
sSQL = "INSERT INTO [cw_cfs_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=isnull((SELECT top 1 [GID] FROM [cw_cfs_template] where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' and MODELGID='" + sMODELGID + "' and ITEMGID=(select top 1 gid from cw_cfs_model_item where MODELGID='" + sMODELGID + "' and ITEMNAME=(select top 1 ITEMNAME from cw_cfs_model_item where MODELGID='" + sMODELGID_MR + "' and gid=(select top 1 ITEMGID from cw_cfs_template where STARTGID='" + strCwSTARTGID + "' and ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID_MR + "' and gid=[cw_cfs_template_detail].LINKGID)))),'')"
+ ",[ACCGID],[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + USERID + "' as [MODIFIEDUSER],GETDATE()"
+ " FROM [cw_cfs_template_detail] WITH(NOLOCK)"
+ " where LINKGID in (SELECT [GID] FROM [cw_cfs_template] WITH(NOLOCK) where ACCDATE='" + strACCDATE + "' and MODELGID='" + sMODELGID_MR + "' and STARTGID='" + strCwSTARTGID + "')";
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
}
}
#endregion
#region 判断资产负债表是否平衡
public static DBResult onIsCwSofp(string sACCDATE, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
var result = new DBResult();
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
try
{
Decimal dl = Decimal.Parse(T_ALL_DA.GetStrSQL("inum", "SELECT top 1 (QTYLASTBLC-QTYLASTBLC2) as inum from cw_sofp_template where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' ORDER BY LINENUM DESC"));
if (dl != 0)
{
result.Success = false;
result.Message = "资产负债表不平衡,请先调整!";
return result;
}
}
catch (Exception)
{
result.Success = false;
result.Message = "资产负债表不平衡,请先调整!";
return result;
}
//货币资金_期末数
Decimal dlQTYLASTBLC = Decimal.Parse(T_ALL_DA.GetStrSQL("QTYLASTBLC", "SELECT isnull(QTYLASTBLC,0) as QTYLASTBLC from cw_sofp_template where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' and LINENUM=2"));
result.Success = true;
result.Message = dlQTYLASTBLC.ToString();
return result;
}
#endregion
#region 自动调平
static public void onAutoLevel(string sMODELGID, string sACCDATE, string USERID, string companyid)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(USERID);
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
Decimal dlSofp = 0;
int iResult = 0;
DBResult result = onIsCwSofp(sACCDATE, companyid, USERID);
if (result.Message.IndexOf("不平衡") < 0)
{
dlSofp = Decimal.Parse(result.Message.ToString());
}
Decimal dlSofp2 = Decimal.Parse(T_ALL_DA.GetStrSQL("QTYYEARBLC", "SELECT isnull(QTYYEARBLC,0) as QTYYEARBLC from cw_sofp_template where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' and LINENUM=2"));
DataSet ds = T_ALL_DA.GetAllSQL("SELECT top 1 * from VW_CwCfsTemplate_ModelItem where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + sACCDATE + "' ORDER BY LINENUM DESC");
#region 差额 = 最后一行 - 资表【货币资金期末数】,更新到“支付其他与经营活动有关的现金”
Decimal dlCE = Decimal.Parse(ds.Tables[0].Rows[0]["QTYLASTBLC"].ToString()) - dlSofp;
Decimal dlQTYBLC = Decimal.Parse(ds.Tables[0].Rows[0]["QTYBLC"].ToString()) - dlSofp2;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_cfs_template set"
+ " QTYLASTBLC_H=isnull((QTYLASTBLC_H+" + dlCE + "),0)"
+ ",QTYBLC_H=isnull((QTYBLC_H+" + dlQTYBLC + "),0)"
+ " where MODELGID='" + sMODELGID + "' and ACCDATE='" + sACCDATE + "' and STARTGID='" + strCwSTARTGID + "' and ITEMGID=(SELECT top 1 GID from cw_cfs_model_item where MODELGID in (SELECT GID from cw_cfs_model WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "') and ITEMNAME like '%支付其他与经营活动有关的现金%')";
iResult = -1;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
GetRecount(sMODELGID, sACCDATE, USERID, companyid);
}
#endregion
}
}