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("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+"')");
//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_DAT_ALL_DA=newT_ALL_DA();
stringsSQL="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+")";
DataSetds=T_ALL_DA.GetAllSQL(sSQL);
for(inti=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;
stringsSQL="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+"'";
stringsSQL="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";
stringsSQL="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";
stringsSQL="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";
stringsSQL="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";
stringsCwSTARTGID=" and [STARTGID]='"+strCwSTARTGID+"'";
stringstrACCDATE=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");
stringsSQL="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;
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+")";