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.

487 lines
30 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.RptCwPLEntity;
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.RptCwPLDAL
{
public class RptCwPLDAL
{
#region 查询
static public List<RptCwPLEntity> 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 行号,ACCTYPE as 资产,QTYLASTBLC as 本年累计金额,QTYBLC as 本月金额,(select top 1 STARTNAME from [cw_design_startusing] WITH(NOLOCK) where gid=a.STARTGID and (ISDELETE=0 or ISDELETE is null)) as 账套启用,(select top 1 [NAME] from [company] WITH(NOLOCK) where gid=a.CORPID) as 分公司,(select top 1 SHOWNAME from [user] WITH(NOLOCK) 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_pl_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<RptCwPLEntity> SetData(StringBuilder strSql)
{
var headList = new List<RptCwPLEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwPLEntity data = new RptCwPLEntity();
#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.ACCTYPE = (reader["ACCTYPE"] == null ? "" : Convert.ToString(reader["ACCTYPE"]).Replace(" ", "&nbsp"));//资产
data.QTYLASTBLC = (reader["QTYLASTBLC"] == null ? "" : Convert.ToString(reader["QTYLASTBLC"]));//本年累计金额
data.QTYBLC = (reader["QTYBLC"] == null ? "" : Convert.ToString(reader["QTYBLC"]));//本月金额
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<RptCwPLDetailEntity> GetDetailList(string ACCDATE, string strCondition, string companyid, string strUSERID)
{
//RptCwPLDAL.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_pl_template_detail.ACCGID and [YEAR]=SUBSTRING(convert(varchar,'" + ACCDATE + "-01',23),1,4)),''),ACCDATE=(select top 1 ACCDATE from cw_pl_template WITH(NOLOCK) where GID=cw_pl_template_detail.LINKGID),ACCIDNAME=ISNULL((select top 1 (ACCID+'-'+ACCNAME) as ACCIDNAME from cw_accitems_gl WITH(NOLOCK) where GID=cw_pl_template_detail.ACCGID and [YEAR]=SUBSTRING(convert(varchar,'" + ACCDATE + "-01',23),1,4)),'') from cw_pl_template_detail WITH(NOLOCK) where LINKGID in (select GID from cw_pl_template where [STARTGID]='" + strCwSTARTGID + "')");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
strSql.Append(" order by LINENUM,MODIFIEDTIME");
return SetDetail(strSql);
}
private static List<RptCwPLDetailEntity> SetDetail(StringBuilder strSql)
{
var headList = new List<RptCwPLDetailEntity>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwPLDetailEntity data = new RptCwPLDetailEntity();
#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.LINENUM = (reader["LINENUM"] == null ? 0 : Convert.ToInt32(reader["LINENUM"]));//运算顺序
data.OPERATORS = (reader["OPERATORS"] == null ? "" : Convert.ToString(reader["OPERATORS"]));//运算符号
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 删除明细表
public static DBResult DeleteDetail(RptCwPLDetailEntity 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_pl_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 SHOWNAME, 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);
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
#region 从总账表中取数
//string sSQL = "update cw_pl_template_detail set "
//+ " QTYBLC=isnull(("
//+ "select QTYBLC=(case when DC='借' then isnull(QTYDR,0) else isnull(QTYCR,0) end) from (select LINKGID,ACCID=(select top 1 ACCID from cw_accitems_gl WITH(NOLOCK) where GID=cw_genleg_accitems.LINKGID),ACCNAME=(select top 1 ACCNAME from cw_accitems_gl WITH(NOLOCK) where GID=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from cw_accitems_gl WITH(NOLOCK) where GID=cw_genleg_accitems.LINKGID),isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC"//本月金额
//+ " 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_pl_template_detail.ACCGID"
//+ "),0)"
//+ ",QTYLASTBLC=isnull(("
//+ "select QTYLASTBLC=(case when QTYLASTBLC=0 then (case when DC='借' then isnull(QTYLASTDR,0) else isnull(QTYLASTCR,0) end) else (case when DC='借' then (case when (isnull(QTYLASTDR,0)-isnull(QTYLASTCR,0))>0 then isnull(QTYLASTDR,0) else isnull(QTYLASTCR,0) END) else (case when (isnull(QTYLASTCR,0)-isnull(QTYLASTDR,0))>0 then isnull(QTYLASTCR,0) else isnull(QTYLASTDR,0) END) end) end) from (select LINKGID,ACCID=(select top 1 ACCID from cw_accitems_gl WITH(NOLOCK) where GID=cw_genleg_accitems.LINKGID),ACCNAME=(select top 1 ACCNAME from cw_accitems_gl WITH(NOLOCK) where GID=cw_genleg_accitems.LINKGID),DC=(select top 1 DC from cw_accitems_gl WITH(NOLOCK) where GID=cw_genleg_accitems.LINKGID),isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,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 + "'"
//+ " and ACCDATE='" + ACCDATE + "'"//因为已经修改总账表关于损益类科目的累计数因此,此处直接提取年末数
//+ " and [YEAR]='" + DateTime.Parse(ACCDATE + "-01").Year.ToString() + "'"
//+ " GROUP by LINKGID) as a"
//+ " where a.LINKGID=cw_pl_template_detail.ACCGID"
//+" where 1=1 and " + strCondition;
//+ "),0)"
#endregion
#region 从凭证视图中取数
T_ALL_DA T_ALL_DA = new T_ALL_DA();
string sSQL = "select * from cw_accitems_gl WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and GID in (select ACCGID from cw_pl_template_detail WITH(NOLOCK) where LINKGID in (select GID from cw_pl_template where [STARTGID]='" + strCwSTARTGID + "') and " + strCondition + ")";
DataSet ds = T_ALL_DA.GetAllSQL(sSQL);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sSQL = "update cw_pl_template_detail set "
+ " QTYBLC=isnull(("
+ " select QTYBLC=(case when DC='D' then isnull((sum(QTYDR)-sum(QTYCR)),0) else isnull((sum(QTYCR)-sum(QTYDR)),0) end)"
+ " from ("
+ " select LINKGID=(select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=VW_cw_vouchers_vouitems_gl.ACCID and [YEAR]=VW_cw_vouchers_vouitems_gl.ACCYEAR and [STARTGID]='" + strCwSTARTGID + "'),ACCID,ACCNAME,DC"
+ " ,isnull(sum(AMTDR),0) as QTYDR,isnull(sum(AMTCR),0) as QTYCR"
+ " from VW_cw_vouchers_vouitems_gl WITH(NOLOCK)"
+ " where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is NULL) and CarryOverType<>'结转损益' and ISCHECKED=1"//(CarryOverType='' or CarryOverType is null)
+ " and ACCDATE='" + ACCDATE + "'"
+ " GROUP by ACCYEAR,ACCID,ACCNAME,DC"
+ " ) as a"
+ " where a.LINKGID in (select GID from cw_accitems_gl WITH(NOLOCK) where ACCID like '" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "%' and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]='" + DateTime.Parse(ACCDATE + "-01").Year.ToString() + "')"
+ " group by DC"
+ " ),0)"
+ " ,QTYLASTBLC=isnull(("
+ " select QTYLASTBLC=(case when DC='D' then (isnull(sum(QTYLASTDR),0)-isnull(sum(QTYLASTCR),0)) else (isnull(sum (QTYLASTCR),0)-isnull(sum(QTYLASTDR),0)) end)"
+ " from ("
+ " select LINKGID=(select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=VW_cw_vouchers_vouitems_gl.ACCID and [YEAR]=VW_cw_vouchers_vouitems_gl.ACCYEAR and [STARTGID]='" + strCwSTARTGID + "'),ACCID,ACCNAME,DC"
+ " ,isnull(sum(AMTDR),0) as QTYLASTDR,isnull(sum(AMTCR),0) as QTYLASTCR"
+ " from VW_cw_vouchers_vouitems_gl WITH(NOLOCK)"
+ " where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is NULL) and CarryOverType<>'结转损益' and ISCHECKED=1"//(CarryOverType='' or CarryOverType is null)
+ " and ACCDATE<='" + ACCDATE + "' and ACCYEAR='" + DateTime.Parse(ACCDATE + "-01").Year.ToString() + "'"
+ " GROUP by ACCYEAR,ACCID,ACCNAME,DC"
+ " ) as a"
+ " where a.LINKGID in (select gid from cw_accitems_gl WITH(NOLOCK) where ACCID like '" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "%' and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]='" + DateTime.Parse(ACCDATE + "-01").Year.ToString() + "')"
+ " group by DC"
+ " ),0)"
+ " where LINKGID in (select GID from cw_pl_template where [STARTGID]='" + strCwSTARTGID + "') and ACCGID='" + ds.Tables[0].Rows[i]["GID"].ToString() + "' and " + strCondition;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
#endregion
//事务提交
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
}
finally
{
SqlHelper.CloseConnection();
sqlTran.Dispose();
}
}
}
#endregion
#region 按规则合计主表金额
public static void SaveRow(string TEMPLATE, string ACCDATE, string companyid, string strUSERID)
{
#region 更新_明细表金额
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
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() + "'";
SaveRowDetail(ACCDATE, strCondition, strUSERID);
}
}
}
#endregion
var accyear = ACCDATE.Substring(0,4);
#region 更新_主表明细金额
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_pl_template set QTYLASTBLC=isnull((select sum((case when OPERATORS='-' then (-d.QTYLASTBLC) ELSE d.QTYLASTBLC end))+isnull(sum(PFADR+PFACR),0) from cw_pl_template_detail d WITH(NOLOCK) left join cw_genleg_accitems c on (c.LINKGID=d.ACCGID and c.IsInitialEntry=1 and c.[YEAR]='"+accyear+"') where d.LINKGID=cw_pl_template.GID),0),QTYBLC=isnull((select sum((case when OPERATORS='-' then (-QTYBLC) ELSE QTYBLC end)) from cw_pl_template_detail WITH(NOLOCK) where LINKGID=cw_pl_template.GID),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "'";
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_pl_template set QTYLASTBLC=isnull((select sum((case when LINENUM=1 then QTYLASTBLC else (-QTYLASTBLC) end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=1 or LINENUM=2 or LINENUM=3)),0),QTYBLC=isnull((select sum((case when LINENUM=1 then QTYBLC else (-QTYBLC) end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=1 or LINENUM=2 or LINENUM=3)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=4";
int iResult = -3;
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_pl_template set QTYLASTBLC=isnull((select sum((case when (LINENUM=4 or LINENUM=5) then QTYLASTBLC else (-QTYLASTBLC) end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=4 or LINENUM=5 or LINENUM=6 or LINENUM=7 or LINENUM=8)),0),QTYBLC=isnull((select sum((case when (LINENUM=4 or LINENUM=5) then QTYBLC else (-QTYBLC) end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=4 or LINENUM=5 or LINENUM=6 or LINENUM=7 or LINENUM=8)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=9";
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 更新_四、利润总额
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string sSQL = "update cw_pl_template set QTYLASTBLC=isnull((select sum((case when LINENUM=12 then (-QTYLASTBLC) else QTYLASTBLC end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=9 or LINENUM=10 or LINENUM=11 or LINENUM=12)),0),QTYBLC=isnull((select sum((case when LINENUM=12 then (-QTYBLC) else QTYBLC end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=9 or LINENUM=10 or LINENUM=11 or LINENUM=12)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=13";
int iResult = -5;
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_pl_template set QTYLASTBLC=isnull((select sum((case when LINENUM=14 then (-QTYLASTBLC) else QTYLASTBLC end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=13 or LINENUM=14)),0),QTYBLC=isnull((select sum((case when LINENUM=14 then (-QTYBLC) else QTYBLC end)) from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and (LINENUM=13 or LINENUM=14)),0) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' and ACCDATE='" + ACCDATE + "' and LINENUM=15";
int iResult = -6;
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_pl_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_pl_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_pl_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_pl_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_pl_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
{
//主表
string sSQL = "INSERT INTO [cw_pl_template]([GID],[TEMPLATE],[ACCDATE],[LINENUM],[ACCTYPE],[QTYLASTBLC],[QTYBLC],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],[TEMPLATE],'" + ACCDATE + "' as [ACCDATE],[LINENUM],[ACCTYPE],0 as [QTYLASTBLC],0 as [QTYBLC],'" + strCwSTARTGID + "' as [STARTGID],'" + companyid + "' as [CORPID],'" + USERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_pl_template] WITH(NOLOCK) where ACCDATE='" + strACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sCwSTARTGID;
int iResult = -2;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//明细表
sSQL = "INSERT INTO [cw_pl_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=(SELECT top 1 [GID] FROM [cw_pl_template] WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and LINENUM=(select top 1 LINENUM from cw_pl_template WITH(NOLOCK) where gid=cw_pl_template_detail.LINKGID) and ACCDATE='" + ACCDATE + "')"
+ ",[ACCGID]=isnull((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_pl_template_detail.ACCGID) and [year]='" + ACCDATE.Trim().Substring(0, 4) + "'),'')"
+ ",[OPERATORS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + USERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_pl_template_detail] WITH(NOLOCK) where LINKGID in (SELECT [GID] FROM [cw_pl_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
}
}