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.

617 lines
48 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.RptCwSofpEntity;
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.RptCwSofpDAL
{
public class RptCwSofpDAL
{
#region 查询
static public List<RptCwSofpEntity> GetDataList(string TEMPLATE, string ACCDATE, string USERID, string SHOWNAME, string companyid, string sort = null)
{
//isSaveRow(TEMPLATE, ACCDATE, USERID, companyid);
//SaveRow(TEMPLATE, ACCDATE, companyid);
var strSql = new StringBuilder();
string sSql = GetDataListSQL(TEMPLATE, ACCDATE, 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 TEMPLATE, string ACCDATE, string companyid, string strUSERID)
{
var strSql = new StringBuilder();
string sSql = GetDataListSQL(TEMPLATE, ACCDATE, companyid, strUSERID);
strSql.Append("select GID as 唯一编码,TEMPLATE as 模板名称,ACCDATE as 会计期间,LINENUM as 行号,LINENUM1 as 行次,ACCTYPE as 资产,QTYLASTBLC as 期末余额,QTYYEARBLC as 年初余额,LINENUM2 as 行次2,ACCTYPE2 as [负债和所有者权益(或股东权益)],QTYLASTBLC2 as 期末余额2,QTYYEARBLC2 as 年初余额2,(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 TEMPLATE, string ACCDATE, string companyid, string strUserID)
{
#region 综合本位币
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUserID);
var strSql = new StringBuilder();
strSql.Append("select * from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "'");
//if (!string.IsNullOrEmpty(ACCDATE))
//{
// strSql.Append(" and " + ACCDATE);
//}
//strSql.Append(" and CORPID='" + companyid + "'");
#endregion
return strSql.ToString();
}
private static List<RptCwSofpEntity> SetData(StringBuilder strSql)
{
var headList = new List<RptCwSofpEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwSofpEntity data = new RptCwSofpEntity();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.TEMPLATE = (reader["TEMPLATE"] == null ? "" : Convert.ToString(reader["TEMPLATE"]));//模板名称
data.ACCDATE = (reader["ACCDATE"] == null ? "" : Convert.ToString(reader["ACCDATE"]));//会计期间
data.LINENUM = (reader["LINENUM"] == null ? 0 : Convert.ToInt32(reader["LINENUM"]));//行次
data.LINENUM1 = (reader["LINENUM1"] == null ? "" : Convert.ToString(reader["LINENUM1"]));//行次1
data.ACCTYPE = (reader["ACCTYPE"] == null ? "" : Convert.ToString(reader["ACCTYPE"]).Replace(" ", "&nbsp"));//资产
data.QTYLASTBLC = (reader["QTYLASTBLC"] == null ? "" : Convert.ToString(reader["QTYLASTBLC"]));//期末余额
data.QTYYEARBLC = (reader["QTYYEARBLC"] == null ? "" : Convert.ToString(reader["QTYYEARBLC"]));//年初余额
if (data.LINENUM == 1 || data.LINENUM == 14)
{
data.QTYLASTBLC = "";//期末余额
data.QTYYEARBLC = "";//年初余额
}
data.LINENUM2 = (reader["LINENUM2"] == null ? "" : Convert.ToString(reader["LINENUM2"]));//行次2
data.ACCTYPE2 = (reader["ACCTYPE2"] == null ? "" : Convert.ToString(reader["ACCTYPE2"]).Replace(" ", "&nbsp"));//负债和所有者权益(或股东权益)
data.QTYLASTBLC2 = (reader["QTYLASTBLC2"] == null ? "" : Convert.ToString(reader["QTYLASTBLC2"]));//期末余额2
data.QTYYEARBLC2 = (reader["QTYYEARBLC2"] == null ? "" : Convert.ToString(reader["QTYYEARBLC2"]));//年初余额2
if (data.LINENUM == 1 || data.LINENUM == 15 || data.LINENUM == 25 || data.LINENUM == 32)
{
data.QTYLASTBLC2 = "";//期末余额2
data.QTYYEARBLC2 = "";//年初余额2
}
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<RptCwSofpDetailEntity> GetDetailList(string ACCDATE, string strCondition, string companyid, string strUSERID)
{
//RptCwSofpDAL.SaveRowDetail(ACCDATE, strCondition);
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_sofp_template_detail.ACCGID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + ACCDATE + "-01',23),1,4)),''),ACCDATE=(select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where GID=cw_sofp_template_detail.LINKGID),ACCIDNAME=ISNULL((select top 1 (ACCID+'-'+ACCNAME) as ACCIDNAME from cw_accitems_gl WITH(NOLOCK) where GID=cw_sofp_template_detail.ACCGID and [YEAR]=SUBSTRING(convert(varchar,'" + ACCDATE + "-01',23),1,4)),'') from cw_sofp_template_detail WITH(NOLOCK) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "')");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
strSql.Append(" order by LINENUM,MODIFIEDTIME");
return SetDetail(strSql);
}
private static List<RptCwSofpDetailEntity> SetDetail(StringBuilder strSql)
{
var headList = new List<RptCwSofpDetailEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwSofpDetailEntity data = new RptCwSofpDetailEntity();
#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.LINKNUM = (reader["LINKNUM"] == null ? "" : Convert.ToString(reader["LINKNUM"]));//模板行次
data.ACCGID = (reader["ACCGID"] == null ? "" : Convert.ToString(reader["ACCGID"]));//科目GID
data.LINENUM = (reader["LINENUM"] == null ? 0 : Convert.ToInt32(reader["LINENUM"]));//运算顺序
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.QTYYEARBLC = (reader["QTYYEARBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARBLC"]));//年初余额
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 删除明细表
public static DBResult DeleteDetail(RptCwSofpDetailEntity 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_sofp_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 TEMPLATE, string ACCDATE, string USERID, string companyid)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(USERID);
isSaveRow(TEMPLATE, ACCDATE, strCwSTARTGID, USERID, companyid);
SaveRow(TEMPLATE, ACCDATE, companyid, USERID);
}
#endregion
#region 按规则合计明细表金额
public static void SaveRowDetail(string ACCDATE, string strCondition, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
string strVOUDATE = ACCDATE + "-01";
string sYEAR = DateTime.Parse(strVOUDATE).Year.ToString();
#region 更新_明细表金额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iResult = 0;
#region 年初余额
#region 余额
string sSQL = "update cw_sofp_template_detail set QTYYEARBLC=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]='" + ACCDATE.Trim().Substring(0, 4) + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_sofp_template_detail.ACCGID"
+ "),0) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "') and ACCESS='余额' and " + strCondition;
#endregion
iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 借方余额
sSQL = "update cw_sofp_template_detail set QTYYEARBLC=isnull(("
+ "select QTYYEARDR from (select LINKGID,isnull(sum(QTYYEARDR),0) as QTYYEARDR"//期初借方
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where IsInitialEntry=1 and ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and [STARTGID]='" + strCwSTARTGID + "'"
+ " and [YEAR]='" + ACCDATE.Trim().Substring(0, 4) + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_sofp_template_detail.ACCGID"
+ "),0) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "') and ACCESS='借方余额' and " + strCondition;
#endregion
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 贷方余额
sSQL = "update cw_sofp_template_detail set QTYYEARBLC=isnull(("
+ "select QTYYEARCR from (select LINKGID,isnull(sum(QTYYEARCR),0) as QTYYEARCR"//期初贷方
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where IsInitialEntry=1 and ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and [STARTGID]='" + strCwSTARTGID + "'"
+ " and [YEAR]='" + ACCDATE.Trim().Substring(0, 4) + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_sofp_template_detail.ACCGID"
+ "),0) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "') and ACCESS='贷方余额' and " + strCondition;
#endregion
iResult = -4;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#endregion
#region 期末余额
#region 余额
sSQL = "update cw_sofp_template_detail set QTYLASTBLC=isnull(("
+ "select QTYLASTBLC from (select LINKGID,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC"//累计余额
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and [STARTGID]='" + strCwSTARTGID + "'"
+ " and ACCDATE='" + ACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_sofp_template_detail.ACCGID"
+ "),0) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "') and ACCESS='余额' and " + strCondition;
#endregion
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 借方余额
sSQL = "update cw_sofp_template_detail set QTYLASTBLC=isnull(("
+ "select (case when DC='贷' then (-QTYLASTBLC) ELSE QTYLASTBLC END) as QTYLASTBLC from (select LINKGID,DC=(SELECT top 1 DC from cw_accitems_gl where gid=cw_genleg_accitems.LINKGID),isnull(sum(QTYLASTBLC),0) as QTYLASTBLC"//累计借方
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and [STARTGID]='" + strCwSTARTGID + "'"
+ " and ACCDATE='" + ACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_sofp_template_detail.ACCGID"
+ "),0) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "') and ACCESS='借方余额' and " + strCondition;
#endregion
iResult = -6;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 贷方余额
sSQL = "update cw_sofp_template_detail set QTYLASTBLC=isnull(("
+ "select (case when DC='借' then (-QTYLASTBLC) ELSE QTYLASTBLC END) as QTYLASTBLC from (select LINKGID,DC=(SELECT top 1 DC from cw_accitems_gl where gid=cw_genleg_accitems.LINKGID),isnull(sum(QTYLASTBLC),0) as QTYLASTBLC"//累计贷方
+ " from cw_genleg_accitems WITH(NOLOCK)"
+ " where ISENABLE=1 and (ISDELETE=0 or ISDELETE is NULL)"
+ " and [STARTGID]='" + strCwSTARTGID + "'"
+ " and ACCDATE='" + ACCDATE + "'"
+ " GROUP by LINKGID) as a"
+ " where a.LINKGID=cw_sofp_template_detail.ACCGID"
+ "),0) where LINKGID in (select GID from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "') and ACCESS='贷方余额' and " + strCondition;
#endregion
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#endregion
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
#endregion
#region 按规则合计主表金额
public static void SaveRow(string TEMPLATE, string ACCDATE, string companyid, string strUSERID)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
string strVOUDATE = ACCDATE + "-01";
string sYEAR = DateTime.Parse(strVOUDATE).Year.ToString();
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
#region 更新_明细表金额
string strSql = GetDataListSQL(TEMPLATE, ACCDATE, 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() + "' and LINKNUM='" + ds.Tables[0].Rows[i]["LINENUM1"].ToString() + "'";
SaveRowDetail(ACCDATE, strCondition, strUSERID);
strCondition = "LINKGID='" + ds.Tables[0].Rows[i]["GID"].ToString() + "' and LINKNUM='" + ds.Tables[0].Rows[i]["LINENUM2"].ToString() + "'";
SaveRowDetail(ACCDATE, strCondition, strUSERID);
}
}
}
#endregion
#region 更新_主表明细金额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iResult = 0;
#region 更新_主表金额
#region 资产
string sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum((case when OPERATORS='-' then (-QTYLASTBLC) ELSE QTYLASTBLC end)) from cw_sofp_template_detail WITH(NOLOCK) where LINKGID=cw_sofp_template.GID and LINKNUM=cw_sofp_template.LINENUM1),0),QTYYEARBLC=isnull((select sum((case when OPERATORS='-' then (-QTYYEARBLC) ELSE QTYYEARBLC end)) from cw_sofp_template_detail WITH(NOLOCK) where LINKGID=cw_sofp_template.GID and LINKNUM=cw_sofp_template.LINENUM1),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "'";
#endregion
iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 权益和负债
sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum((case when OPERATORS='-' then (-QTYLASTBLC) ELSE QTYLASTBLC end)) from cw_sofp_template_detail WITH(NOLOCK) where LINKGID=cw_sofp_template.GID and LINKNUM=cw_sofp_template.LINENUM2),0),QTYYEARBLC2=isnull((select sum((case when OPERATORS='-' then (-QTYYEARBLC) ELSE QTYYEARBLC end)) from cw_sofp_template_detail where LINKGID=cw_sofp_template.GID and LINKNUM=cw_sofp_template.LINENUM2),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "'";
#endregion
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#endregion
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
#region 更新_主表合计金额1
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iResult = 0;
//#region 流动资产合计_行号12
//string sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='01' or LINENUM1='02' or LINENUM1='03' or LINENUM1='04' or LINENUM1='05' or LINENUM1='06' or LINENUM1='07' or LINENUM1='08' or LINENUM1='09' or LINENUM1='10')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='01' or LINENUM1='02' or LINENUM1='03' or LINENUM1='04' or LINENUM1='05' or LINENUM1='06' or LINENUM1='07' or LINENUM1='08' or LINENUM1='09' or LINENUM1='10')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=12";
//#endregion
//iResult = -4;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 长期投资合计_行号16
//sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='11' or LINENUM1='12')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='11' or LINENUM1='12')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=16";
//#endregion
//iResult = -5;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 固定资产净值_行号20
//sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(case when LINENUM1='14' then (-QTYLASTBLC) else QTYLASTBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='13' or LINENUM1='14')),0),QTYYEARBLC=isnull((select sum(case when LINENUM1='14' then (-QTYYEARBLC) else QTYYEARBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='13' or LINENUM1='14')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=20";
//#endregion
//iResult = -6;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 固定资产合计_行号24
//sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(case when LINENUM1='14' then (-QTYLASTBLC) else QTYLASTBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='13' or LINENUM1='14' or LINENUM1='15' or LINENUM1='16' or LINENUM1='17')),0),QTYYEARBLC=isnull((select sum(case when LINENUM1='14' then (-QTYYEARBLC) else QTYYEARBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='13' or LINENUM1='14' or LINENUM1='15' or LINENUM1='16' or LINENUM1='17')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=24";
//#endregion
//iResult = -7;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 无形资产及其他资产合计_行号29
//sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='18' or LINENUM1='19' or LINENUM1='20')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM1='18' or LINENUM1='19' or LINENUM1='20')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=29";
//#endregion
//iResult = -8;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 资产合计_行号30
//sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=12 or LINENUM=16 or LINENUM=24 or LINENUM=29)),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=12 or LINENUM=16 or LINENUM=24 or LINENUM=29)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=30";
//#endregion
//iResult = -9;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 流动负债合计_行号14
//sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM2='21' or LINENUM2='22' or LINENUM2='23' or LINENUM2='24' or LINENUM2='25' or LINENUM2='26' or LINENUM2='27' or LINENUM2='28' or LINENUM2='29' or LINENUM2='30' or LINENUM2='31' or LINENUM2='32')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM2='21' or LINENUM2='22' or LINENUM2='23' or LINENUM2='24' or LINENUM2='25' or LINENUM2='26' or LINENUM2='27' or LINENUM2='28' or LINENUM2='29' or LINENUM2='30' or LINENUM2='31' or LINENUM2='32')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=14";
//#endregion
//iResult = -10;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 长期负债合计_行号20
//sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM2='33' or LINENUM2='34' or LINENUM2='35')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM2='33' or LINENUM2='34' or LINENUM2='35')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=20";
//#endregion
//iResult = -11;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 负债合计_行号22
//sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=14 or LINENUM=20)),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=14 or LINENUM=20)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=22";
//#endregion
//iResult = -12;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 所有者权益或股东权益合计_行号29
//sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM2='36' or LINENUM2='37' or LINENUM2='38' or LINENUM2='40')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM2='36' or LINENUM2='37' or LINENUM2='38' or LINENUM2='40')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=29";
//#endregion
//iResult = -13;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//#region 负债和所有者权益或股东权益合计_行号30
//sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=14 or LINENUM=20 or LINENUM=29)),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=14 or LINENUM=20 or LINENUM=29)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=30";
//#endregion
//iResult = -14;
//SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 流动资产合计_行号12
string sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='流动资产合计')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='流动资产合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM1ACCTYPE='流动资产合计'";
#endregion
iResult = -4;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 长期投资合计_行号16
sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='长期投资合计')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='长期投资合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM1ACCTYPE='长期投资合计'";
#endregion
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 固定资产净值_行号20
sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(case when LINENUM1ACCTYPE='累计折旧' then (-QTYLASTBLC) else QTYLASTBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='固定资产净值')),0),QTYYEARBLC=isnull((select sum(case when LINENUM1ACCTYPE='累计折旧' then (-QTYYEARBLC) else QTYYEARBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='固定资产净值')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM1ACCTYPE='固定资产净值'";
#endregion
iResult = -6;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 固定资产合计_行号24
sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(case when LINENUM1='14' then (-QTYLASTBLC) else QTYLASTBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='固定资产合计')),0),QTYYEARBLC=isnull((select sum(case when LINENUM1='14' then (-QTYYEARBLC) else QTYYEARBLC end) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='固定资产合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM1ACCTYPE='固定资产合计'";
#endregion
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 无形资产及其他资产合计_行号29
sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='无形资产及其他')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='无形资产及其他')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM1ACCTYPE='无形资产及其他'";
#endregion
iResult = -8;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 资产合计_行号30
sSQL = "update cw_sofp_template set QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='资产合计')),0),QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM1ACCTYPE='资产合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM1ACCTYPE='资产合计'";
#endregion
iResult = -9;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 流动负债合计_行号14
sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='流动负债合计')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='流动负债合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM2ACCTYPE='流动负债合计'";
#endregion
iResult = -10;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 长期负债合计_行号20
sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='长期负债合计')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='长期负债合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM2ACCTYPE='长期负债合计'";
#endregion
iResult = -11;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 负债合计_行号22
sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='负债合计')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='负债合计')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM2ACCTYPE='负债合计'";
#endregion
iResult = -12;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 所有者权益或股东权益合计_行号29
sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='所有者权益')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='所有者权益')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM2ACCTYPE='所有者权益'";
#endregion
iResult = -13;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 负债和所有者权益或股东权益合计_行号30
sSQL = "update cw_sofp_template set QTYLASTBLC2=isnull((select sum(QTYLASTBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='负债和所有者权益')),0),QTYYEARBLC2=isnull((select sum(QTYYEARBLC2) from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (PLINENUM2ACCTYPE='负债和所有者权益')),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM2ACCTYPE='负债和所有者权益'";
#endregion
iResult = -14;
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 isSaveRow(string TEMPLATE, string ACCDATE, string strCwSTARTGID, string USERID, string companyid)
{
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
Boolean blSTARTGID = false;
string sCwSTARTGID = " and [STARTGID]='" + strCwSTARTGID + "'";
string strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "'" + sCwSTARTGID + " and ACCDATE='" + ACCDATE + "' group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "'" + sCwSTARTGID + " and SUBSTRING(convert(varchar,(ACCDATE + '-01'),23),1,4)=SUBSTRING(convert(varchar,'" + ACCDATE + "-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_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "'" + sCwSTARTGID + " group by ACCDATE order by ACCDATE desc");
if (strACCDATE == "")
{
strACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", "select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "' group by ACCDATE,STARTGID order by ACCDATE desc,STARTGID");
sCwSTARTGID = T_ALL_DA.GetStrSQL("STARTGID", "select top 1 STARTGID from cw_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "' group by ACCDATE,STARTGID order by ACCDATE desc,STARTGID");
sCwSTARTGID = " and [STARTGID]='" + sCwSTARTGID + "'";
blSTARTGID = true;
}
else blSTARTGID = true;
}
else {
blSTARTGID = true;
}
}
if (strACCDATE != ACCDATE || blSTARTGID)
{
#region 插入当前会计期间的数据
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iResult = 0;
string sSQL = "INSERT INTO [cw_sofp_template]([GID],[TEMPLATE],[ACCDATE],[LINENUM],[LINENUM1],[ACCTYPE],[QTYLASTBLC],[QTYYEARBLC],[LINENUM2],[ACCTYPE2],[QTYLASTBLC2],[QTYYEARBLC2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],PLINENUM1ACCTYPE,LINENUM1ACCTYPE,PLINENUM2ACCTYPE,LINENUM2ACCTYPE) "
+ "SELECT NEWID() as [GID],[TEMPLATE],'" + ACCDATE + "' as [ACCDATE],[LINENUM],[LINENUM1],[ACCTYPE],0 as [QTYLASTBLC],0 as [QTYYEARBLC],[LINENUM2],[ACCTYPE2],0 as [QTYLASTBLC2],0 as [QTYYEARBLC2],'" + strCwSTARTGID + "' as [STARTGID],'" + companyid + "' as [CORPID],'" + USERID + "' as [MODIFIEDUSER],GETDATE(),PLINENUM1ACCTYPE,LINENUM1ACCTYPE,PLINENUM2ACCTYPE,LINENUM2ACCTYPE FROM [cw_sofp_template] where ACCDATE='" + strACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sCwSTARTGID;
iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//明细表
sSQL = "INSERT INTO [cw_sofp_template_detail]([GID],[LINKGID],[LINKNUM],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYYEARBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=(SELECT top 1 [GID] FROM [cw_sofp_template] where STARTGID='" + strCwSTARTGID + "' and LINENUM=(select top 1 LINENUM from cw_sofp_template WITH(NOLOCK) where gid=cw_sofp_template_detail.LINKGID) and ACCDATE='" + ACCDATE + "'),[LINKNUM]"
+ ",[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_sofp_template_detail.ACCGID) and [year]='" + ACCDATE.Trim().Substring(0, 4) + "')"
+ ",[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + USERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_sofp_template_detail] WITH(NOLOCK) where LINKGID in (SELECT [GID] FROM [cw_sofp_template] WITH(NOLOCK) where ACCDATE='" + strACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sCwSTARTGID + ")";
iResult = -3;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
#endregion
}
}
#endregion
}
}