strSql.Append("ORDNO,VOUALLNO,VOUNO,VKNO,VOUDATE,ACCYEAR,ACCMONTH,ATTACHS,ACCDATE,PREPARED,ISCHECKED,CHECKED,ENTERED,ISEXPORT,SUM(case when FCYNO='RMB' THEN AMTDR ELSE 0 END) as AMTDR,SUM(case when FCYNO='RMB' THEN AMTCR ELSE 0 END) as AMTCR,SUM(FCYDR) as FCYDR,SUM(FCYCR) as FCYCR,SUM(AMTDR) as QTYDR,SUM(AMTCR) as QTYCR,CREATEUSER,CREATETIME,CREATEUSERNAME,MODIFIEDUSER,MODIFIEDTIME,MODIFIEDUSERNAME,ISCOPY,ISAUTO,VOUPROP from VW_cw_vouchers_vouitems_gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null)");
strSql.Append(" GROUP BY ORDNO,VOUALLNO,VOUNO,VKNO,VOUDATE,ACCYEAR,ACCMONTH,ATTACHS,ACCDATE,PREPARED,ISCHECKED,CHECKED,ENTERED,ISEXPORT,CREATEUSER,CREATETIME,CREATEUSERNAME,MODIFIEDUSER,MODIFIEDTIME,MODIFIEDUSERNAME,ISCOPY,ISAUTO,VOUPROP");
strSql.Append("SELECT COUNT(*) FROM (SELECT ORDNO,VOUALLNO,VOUNO,VKNO,VOUDATE,ACCYEAR,ACCMONTH,ATTACHS,ACCDATE,PREPARED,ISCHECKED,CHECKED,ENTERED,ISEXPORT,SUM(case when FCYNO='RMB' THEN AMTDR ELSE 0 END) as AMTDR,SUM(case when FCYNO='RMB' THEN AMTCR ELSE 0 END) as AMTCR,SUM(FCYDR) as FCYDR,SUM(FCYCR) as FCYCR,SUM(AMTDR) as QTYDR,SUM(AMTCR) as QTYCR,CREATEUSER,CREATETIME,CREATEUSERNAME,MODIFIEDUSER,MODIFIEDTIME,MODIFIEDUSERNAME,ISCOPY,ISAUTO,VOUPROP from VW_cw_vouchers_vouitems_gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null) ");// and CORPID='" + companyid + "'
strSql.Append(" GROUP BY ORDNO,VOUALLNO,VOUNO,VKNO,VOUDATE,ACCYEAR,ACCMONTH,ATTACHS,ACCDATE,PREPARED,ISCHECKED,CHECKED,ENTERED,ISEXPORT,CREATEUSER,CREATETIME,CREATEUSERNAME,MODIFIEDUSER,MODIFIEDTIME,MODIFIEDUSERNAME,ISCOPY,ISAUTO,VOUPROP) AS V ");
strSql.Append("SELECT *,ISNULL((SELECT TOP (1) SHOWNAME FROM [user] WITH(NOLOCK) WHERE (GID = Cw_Vouchers_Gl.CHECKED)), '未审核') AS CHECKEDNAME,(select top 1 SHOWNAME from [user] where GID=Cw_Vouchers_Gl.CREATEUSER) as CREATEUSERNAME,(select top 1 SHOWNAME from [user] where GID=Cw_Vouchers_Gl.MODIFIEDUSER) as MODIFIEDUSERNAME from Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null)");
strSql.Append("SELECT GID,GID as 唯一编码,VOUALLNO as 凭证字号,VOUDATE as 凭证日期,VKNO as 凭证字,VOUNO as 凭证号,ITEMNO as 行号,EXPLAN as 摘要,ACCID as 科目代码,ACCALL as 会计科目,FCYNO as 原币币别,FCYEXRATE as 汇率,FCYAMT as 原币金额,AMTDR as 借方金额,AMTCR as 贷方金额,PREPARED as 制单人,MODIFIEDUSERNAME_d as 改单人,CHECKED as 审核人 from VW_cw_vouchers_vouitems_gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null)");
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
varsortstring=DatasetSort.Getsortstring(sort);
if(!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by "+sortstring+",ITEMNO");
}
else
{
//strSql.Append(" order by convert(int,VOUNO) desc,VOUDATE desc,ITEMNO");
strSql.Append(" order by VOUALLNO desc,VOUDATE desc,ITEMNO");
strSql.Append("SELECT ORDNO,ORDNO as 唯一编码,VOUALLNO as 凭证字号,VOUNO as 凭证号,VKNO as 凭证字,VOUDATE as 凭证日期,ACCYEAR as 凭证年,ACCMONTH as 凭证月,ATTACHS as 附件数,ACCDATE as 会计期间,PREPARED as 制单人,ISCHECKED as 是否审核,CHECKED as 审核人,ENTERED as 记账人,MODIFIEDUSERNAME as 改单人,ISEXPORT as 是否导出凭证,SUM(case when FCYNO='RMB' THEN AMTDR ELSE 0 END) as 本位币借方,SUM(case when FCYNO='RMB' THEN AMTCR ELSE 0 END) as 本位币贷方,SUM(FCYDR) as 外币借方,SUM(FCYCR) as 外币贷方,SUM(AMTDR) as 综合本位币借方,SUM(AMTCR) as 综合本位币贷方 from VW_cw_vouchers_vouitems_gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null)");
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition);
}
strSql.Append(" GROUP BY ORDNO,VOUALLNO,VOUNO,VKNO,VOUDATE,ACCYEAR,ACCMONTH,ATTACHS,ACCDATE,PREPARED,ISCHECKED,CHECKED,ENTERED,ISEXPORT,MODIFIEDUSERNAME");
//strSql.Append(" order by convert(int,VOUNO) desc,VOUDATE desc");
varsortstring=DatasetSort.Getsortstring(sort);
if(!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by "+sortstring);
}
else
{
strSql.Append(" order by VOUALLNO desc,VOUDATE desc");
stringsSQL2="select top 1 isnull(CURRENCY,'RMB') as CURRENCY from cw_accitems_currency where LINKGID='"+LINKGID+"' ORDER BY CURRENCY";
FCY=T_ALL_DA.GetStrSQL("CURRENCY",sSQL2);
}
if(FCY!="RMB")
{
stringsSQL="SELECT top 1 DEFAULTRATE=isnull((select top 1 BEGRATE from cw_currency_rate where LINKGID=code_currency.GID and ISDELETE=0 and ACCDATE= '"+strCwACCDATE+"' and STARTGID='"+strCwSTARTGID+"'),DEFAULTRATE) from code_currency WITH(NOLOCK) where CODENAME='"+FCY+"'";// and CORPID='" + strCompanyID + "'
stringsSQL2="select top 1 isnull(CURRENCY,'RMB') as CURRENCY from cw_accitems_currency where LINKGID='"+LINKGID+"' ORDER BY CURRENCY";
FCY=T_ALL_DA.GetStrSQL("CURRENCY",sSQL2);
}
if(FCY!="RMB")
{
stringsSQL="SELECT top 1 DEFAULTRATE=isnull((select top 1 BEGRATE from cw_currency_rate where LINKGID=code_currency.GID and ISDELETE=0 and ACCDATE= '"+VouDate+"' and STARTGID='"+strCwSTARTGID+"'),DEFAULTRATE) from code_currency WITH(NOLOCK) where CODENAME='"+FCY+"'";// and CORPID='" + strCompanyID + "'
varsGids=" and ORDNO in ('"+gids.Replace(",","','")+"')";
varcmdDelete=db.GetSqlStringCommand("UPDATE ch_fee SET VOUALLNO='' where VOUALLNO in (select VOUALLNO from Cw_Vouchers_Gl where 1=1"+sGids+")");
db.ExecuteNonQuery(cmdDelete,tran);
cmdDelete=db.GetSqlStringCommand("UPDATE ch_fee_settlement SET VOUCHERNO='' where VOUCHERNO in (select VOUALLNO from Cw_Vouchers_Gl where 1=1"+sGids+")");
db.ExecuteNonQuery(cmdDelete,tran);
cmdDelete=db.GetSqlStringCommand("UPDATE ch_fee_invoice SET VOUNO='' where VOUNO in (select VOUALLNO from Cw_Vouchers_Gl where 1=1"+sGids+")");
db.ExecuteNonQuery(cmdDelete,tran);
cmdDelete=db.GetSqlStringCommand("UPDATE ch_fee_invoicehexiao SET VOUNO='' where VOUNO in (select VOUALLNO from Cw_Vouchers_Gl where 1=1"+sGids+")");
db.ExecuteNonQuery(cmdDelete,tran);
*/
varsGids=" and ORDNO in ('"+gids.Replace(",","','")+"')";
varcmdDelete=db.GetSqlStringCommand("delete from [cw_vouno_bs_gl] where STARTGID='"+strCwSTARTGID+"'"+sGids);
db.ExecuteNonQuery(cmdDelete,tran);
//把删除的凭证号插入序号的漏号补号表
cmdDelete=db.GetSqlStringCommand("INSERT INTO [code_fill_serialno]([GID],[TABLENAME],[SERIALNO],[CREATEUSER],[CREATETIME],[ISDELETE],[DELETEUSER],[DELETETIME],STARTGID) select newid(),'cw_vouchers_gl',VOUALLNO,'"+strUserID+"',getdate(),0,'',getdate(),'"+strCwSTARTGID+"' from cw_vouchers_gl where 1=1"+sGids);
db.ExecuteNonQuery(cmdDelete,tran);
cmdDelete=db.GetSqlStringCommand("delete from Cw_Vouitems_Gl where 1=1"+sGids);
db.ExecuteNonQuery(cmdDelete,tran);
cmdDelete=db.GetSqlStringCommand("delete from Cw_Vouchers_Gl where 1=1"+sGids);
DataSetds=T_ALL_DA.GetAllSQL("SELECT * from [VW_cw_vouchers_vouitems_gl] WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null) and ORDNO in ('"+strGIDs+"')");
varAccList=BasicDataRefDAL.GetCwAccitemsGlAll(" DETAILED=1 and ISENABLE=1 ",USERID);
varcmdDelete=db.GetSqlStringCommand("update Cw_Vouchers_Gl set ISCHECKED=1,CHECKED='"+USERID+"',CHECKEDTIME=getdate() where ORDNO in ('"+strGIDs+"')");
if(iTYPE.Trim()=="0")
{
cmdDelete=db.GetSqlStringCommand("update Cw_Vouchers_Gl set ISCHECKED=0,CHECKED='',CHECKEDTIME=getdate(),ISCARRY=0 where ORDNO in ('"+strGIDs+"')");
}
db.ExecuteNonQuery(cmdDelete,tran);
tran.Commit();
}
catch(Exception)
{
tran.Rollback();
result.Success=false;
result.Message="操作出现错误,请重试!";
returnresult;
}
}
#endregion
#region 结转已审核的凭证到总账本期发生额
stringstrVOUDATE="SELECT top 1 SUBSTRING(convert(varchar,VOUDATE,23),1,7) as VOUDATE from [Cw_Vouchers_Gl] WITH(NOLOCK) where ORDNO in ('"+strGIDs+"')";
//+ ",AMTLASTBLC=(case WHEN DC2='借' THEN ISNULL(((AMTYEARDR-AMTYEARCR)+(AMTDR-AMTCR)),0) ELSE ISNULL(((AMTYEARCR-AMTYEARDR)+(AMTCR-AMTDR)),0) END)" //RMB累计余额
// //FCYYEARDR 外币期初借方
// //FCYYEARCR 外币期初贷方
//+ ",FCYYEARBLC=(case WHEN DC2='借' THEN ISNULL((FCYYEARDR-FCYYEARCR),0) ELSE ISNULL((FCYYEARCR-FCYYEARDR),0) END)" //外币期初余额
//+ ",FCYDR2=FCYDR" //外币本月借方
//+ ",FCYCR2=FCYCR" //外币本月贷方
//+ ",FCYBLC=(case WHEN DC2='借' THEN ISNULL((FCYDR-FCYCR),0) ELSE ISNULL((FCYCR-FCYDR),0) END)" //外币本月余额
//+ ",FCYLASTBLC=(case WHEN DC2='借' THEN ISNULL(((FCYYEARDR-FCYYEARCR)+(FCYDR-FCYCR)),0) ELSE ISNULL(((FCYYEARCR-FCYYEARDR)+(FCYCR-FCYDR)),0) END)" //外币累计余额
// //QTYYEARDR 期初借方
// //QTYYEARCR 期初贷方
//+ ",QTYYEARBLC=(case WHEN DC2='借' THEN ISNULL((QTYYEARDR-QTYYEARCR),0) ELSE ISNULL((QTYYEARCR-QTYYEARDR),0) END)" //期初余额
//+ ",QTYDR2=QTYDR" //本月借方
//+ ",QTYCR2=QTYCR" //本月贷方
//+ ",QTYBLC=(case WHEN DC2='借' THEN ISNULL((QTYDR-QTYCR),0) ELSE ISNULL((QTYCR-QTYDR),0) END)" //本月余额
//+ ",QTYLASTBLC=(case WHEN DC2='借' THEN ISNULL(((QTYYEARDR-QTYYEARCR)+(QTYDR-QTYCR)),0) ELSE ISNULL(((QTYYEARCR-QTYYEARDR)+(QTYCR-QTYDR)),0) END)" //累计余额
//+ ",AMTYEARDR=ISNULL((select top 1 AMTYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//RMB期初借方
//+ ",AMTYEARCR=ISNULL((select top 1 AMTYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//RMB期初贷方
//+ ",sum(AMTDR_RMB) as AMTDR"//RMB本月借方
//+ ",sum(AMTCR_RMB) as AMTCR"//RMB本月贷方
//+ ",FCYYEARDR=ISNULL((select top 1 FCYYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//外币期初借方
//+ ",FCYYEARCR=ISNULL((select top 1 FCYYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//外币期初贷方
//+ ",sum(FCYDR) as FCYDR"//外币本月借方
//+ ",sum(FCYCR) as FCYCR"//外币本月贷方
//+ ",QTYYEARDR=ISNULL((select top 1 QTYYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//期初借方
//+ ",QTYYEARCR=ISNULL((select top 1 QTYYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//期初贷方
//+ ",sum(AMTDR) as QTYDR"//本月借方
//+ ",sum(AMTCR) as QTYCR"//本月贷方
//+ ",DC2"
//+ " from ("
//+ "select *"
//+ ",(case FCYNO WHEN 'RMB' THEN AMTDR ELSE 0 END) as AMTDR_RMB"//RMB本月借方
//+ ",(case FCYNO WHEN 'RMB' THEN AMTCR ELSE 0 END) as AMTCR_RMB"//RMB本月贷方
//+ ",ACCGID=ISNULL((select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",PACCGID=ISNULL((select top 1 PACCGID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",DC2=ISNULL((select top 1 DC from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item WITH(NOLOCK) where GID=Cw_Vouitems_Gl.ITEMGID),'')"
//+ ",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept WITH(NOLOCK) where GID=Cw_Vouitems_Gl.DEPTGID),'')"
//+ " from Cw_Vouitems_Gl WITH(NOLOCK) where 1=1"
//+ " and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and ISCHECKED=1 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7))"
//+ ") as a"
//+ " group BY ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2,ACCGID,PACCGID"
//+ ") as b"
//+ " order by ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME";
+",AMTYEARBLC=(case WHEN DC2='借' THEN ISNULL((AMTYEARDR-AMTYEARCR),0) ELSE ISNULL((AMTYEARCR-AMTYEARDR),0) END)"//RMB期初余额
+",AMTDR2=AMTDR"//RMB本月借方
+",AMTCR2=AMTCR"//RMB本月贷方
+",AMTBLC=(case WHEN DC2='借' THEN ISNULL((AMTDR-AMTCR),0) ELSE ISNULL((AMTCR-AMTDR),0) END)"//RMB本月余额
+",AMTLASTBLC=(case WHEN DC2='借' THEN ISNULL(((AMTYEARDR-AMTYEARCR)+(AMTDR-AMTCR)),0) ELSE ISNULL(((AMTYEARCR-AMTYEARDR)+(AMTCR-AMTDR)),0) END)"//RMB累计余额
//FCYYEARDR 外币期初借方
//FCYYEARCR 外币期初贷方
+",FCYYEARBLC=(case WHEN DC2='借' THEN ISNULL((FCYYEARDR-FCYYEARCR),0) ELSE ISNULL((FCYYEARCR-FCYYEARDR),0) END)"//外币期初余额
+",FCYDR2=FCYDR"//外币本月借方
+",FCYCR2=FCYCR"//外币本月贷方
+",FCYBLC=(case WHEN DC2='借' THEN ISNULL((FCYDR-FCYCR),0) ELSE ISNULL((FCYCR-FCYDR),0) END)"//外币本月余额
+",FCYLASTDR=ISNULL((FCYYEARDR+FCYDR),0)"//外币累计借方
+",FCYLASTCR=ISNULL((FCYYEARCR+FCYCR),0)"//外币累计贷方
+",FCYLASTBLC=(case WHEN DC2='借' THEN ISNULL(((FCYYEARDR-FCYYEARCR)+(FCYDR-FCYCR)),0) ELSE ISNULL(((FCYYEARCR-FCYYEARDR)+(FCYCR-FCYDR)),0) END)"//外币累计余额
//QTYYEARDR 期初借方
//QTYYEARCR 期初贷方
+",QTYYEARBLC=(case WHEN DC2='借' THEN ISNULL((QTYYEARDR-QTYYEARCR),0) ELSE ISNULL((QTYYEARCR-QTYYEARDR),0) END)"//期初余额
+",QTYDR2=QTYDR"//本月借方
+",QTYCR2=QTYCR"//本月贷方
+",QTYBLC=(case WHEN DC2='借' THEN ISNULL((QTYDR-QTYCR),0) ELSE ISNULL((QTYCR-QTYDR),0) END)"//本月余额
+",QTYLASTDR=ISNULL((QTYYEARDR+QTYDR),0)"//累计借方
+",QTYLASTCR=ISNULL((QTYYEARCR+QTYCR),0)"//累计贷方
+",QTYLASTBLC=(case WHEN DC2='借' THEN ISNULL(((QTYYEARDR-QTYYEARCR)+(QTYDR-QTYCR)),0) ELSE ISNULL(((QTYYEARCR-QTYYEARDR)+(QTYCR-QTYDR)),0) END)"//累计余额
+",(case FCYNO WHEN 'RMB' THEN AMTDR ELSE 0 END) as AMTDR_RMB"//RMB本月借方
+",(case FCYNO WHEN 'RMB' THEN AMTCR ELSE 0 END) as AMTCR_RMB"//RMB本月贷方
+",ACCGID=ISNULL((select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",PACCGID=ISNULL((select top 1 PACCGID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",DC2=ISNULL((select top 1 DC from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item WITH(NOLOCK) where GID=Cw_Vouitems_Gl.ITEMGID),'')"
+",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept WITH(NOLOCK) where GID=Cw_Vouitems_Gl.DEPTGID),'')"
+" from Cw_Vouitems_Gl WITH(NOLOCK) where 1=1"
+" and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and ISCHECKED=1 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7))"
+") as a"
+" left join cw_genleg_accitems gl WITH(NOLOCK) on (gl.[STARTGID]='"+strCwSTARTGID+"' and gl.CURRENCY=a.FCYNO and gl.DEPTACC=a.DEPTNAME and gl.EMPLACC=a.SALE and gl.CORPACC=a.CUSTOMERNAME and gl.ITEMACC=a.ITEMNAME and gl.ACCDATE=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and gl.LINKGID=a.ACCGID) "
+" group BY ACCID,ACCNAME,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2,ACCGID,PACCGID,gl.AMTYEARDR,gl.AMTYEARCR,gl.FCYYEARDR,gl.FCYYEARCR,gl.QTYYEARDR,gl.QTYYEARCR"
+") as b"
+" order by ACCID,ACCNAME,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME";
//+ ",AMTYEARDR=ISNULL((select top 1 AMTYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//RMB期初借方
//+ ",AMTYEARCR=ISNULL((select top 1 AMTYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//RMB期初贷方
//+ ",sum(AMTDR_RMB) as AMTDR"//RMB本月借方
//+ ",sum(AMTCR_RMB) as AMTCR"//RMB本月贷方
//+ ",FCYYEARDR=ISNULL((select top 1 FCYYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//外币期初借方
//+ ",FCYYEARCR=ISNULL((select top 1 FCYYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//外币期初贷方
//+ ",sum(FCYDR) as FCYDR"//外币本月借方
//+ ",sum(FCYCR) as FCYCR"//外币本月贷方
//+ ",QTYYEARDR=ISNULL((select top 1 QTYYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//期初借方
//+ ",QTYYEARCR=ISNULL((select top 1 QTYYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//期初贷方
//+ ",sum(AMTDR) as QTYDR"//本月借方
//+ ",sum(AMTCR) as QTYCR"//本月贷方
//+ ",DC2"
//+ " from ("
//+ "select *"
//+ ",(case FCYNO WHEN 'RMB' THEN AMTDR ELSE 0 END) as AMTDR_RMB"//RMB本月借方
//+ ",(case FCYNO WHEN 'RMB' THEN AMTCR ELSE 0 END) as AMTCR_RMB"//RMB本月贷方
//+ ",ACCGID=ISNULL((select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",PACCGID=ISNULL((select top 1 PACCGID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",DC2=ISNULL((select top 1 DC from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item WITH(NOLOCK) where GID=Cw_Vouitems_Gl.ITEMGID),'')"
//+ ",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept WITH(NOLOCK) where GID=Cw_Vouitems_Gl.DEPTGID),'')"
//+ " from Cw_Vouitems_Gl WITH(NOLOCK) where 1=1"
//+ " and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and ISCHECKED=0 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7))"
//+ ") as a"
//+ " group BY ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2,ACCGID,PACCGID"
//+ ") as b"
//+ " order by ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME";
+",(case FCYNO WHEN 'RMB' THEN AMTDR ELSE 0 END) as AMTDR_RMB"//RMB本月借方
+",(case FCYNO WHEN 'RMB' THEN AMTCR ELSE 0 END) as AMTCR_RMB"//RMB本月贷方
+",ACCGID=ISNULL((select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",PACCGID=ISNULL((select top 1 PACCGID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",DC2=ISNULL((select top 1 DC from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item WITH(NOLOCK) where GID=Cw_Vouitems_Gl.ITEMGID),'')"
+",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept WITH(NOLOCK) where GID=Cw_Vouitems_Gl.DEPTGID),'')"
+" from Cw_Vouitems_Gl WITH(NOLOCK) where 1=1"
+" and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and ISCHECKED=0 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7))"
+") as a"
+" left join cw_genleg_accitems gl WITH(NOLOCK) on (gl.[STARTGID]='"+strCwSTARTGID+"' and gl.CURRENCY=a.FCYNO and gl.DEPTACC=a.DEPTNAME and gl.EMPLACC=a.SALE and gl.CORPACC=a.CUSTOMERNAME and gl.ITEMACC=a.ITEMNAME and gl.ACCDATE=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and gl.LINKGID=a.ACCGID) "
+" group BY ACCID,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2,ACCGID,PACCGID,gl.AMTYEARDR,gl.AMTYEARCR,gl.FCYYEARDR,gl.FCYYEARCR,gl.QTYYEARDR,gl.QTYYEARCR"
+") as b"
+" order by ACCID,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME";
//+ ",AMTYEARDR=ISNULL((select top 1 AMTYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//RMB期初借方
//+ ",AMTYEARCR=ISNULL((select top 1 AMTYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//RMB期初贷方
//+ ",sum(AMTDR_RMB) as AMTDR"//RMB本月借方
//+ ",sum(AMTCR_RMB) as AMTCR"//RMB本月贷方
//+ ",FCYYEARDR=ISNULL((select top 1 FCYYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//外币期初借方
//+ ",FCYYEARCR=ISNULL((select top 1 FCYYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//外币期初贷方
//+ ",sum(FCYDR) as FCYDR"//外币本月借方
//+ ",sum(FCYCR) as FCYCR"//外币本月贷方
//+ ",QTYYEARDR=ISNULL((select top 1 QTYYEARDR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//期初借方
//+ ",QTYYEARCR=ISNULL((select top 1 QTYYEARCR from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and CURRENCY=a.FCYNO and DEPTACC=a.DEPTNAME and EMPLACC=a.SALE and CORPACC=a.CUSTOMERNAME and ITEMACC=a.ITEMNAME and ACCDATE=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7) and LINKGID=(SELECT top 1 GID from cw_accitems_gl WITH(NOLOCK) where GID=a.ACCGID)),0)"//期初贷方
//+ ",sum(AMTDR) as QTYDR"//本月借方
//+ ",sum(AMTCR) as QTYCR"//本月贷方
//+ ",DC2"
//+ " from ("
//+ "select *"
//+ ",(case FCYNO WHEN 'RMB' THEN AMTDR ELSE 0 END) as AMTDR_RMB"//RMB本月借方
//+ ",(case FCYNO WHEN 'RMB' THEN AMTCR ELSE 0 END) as AMTCR_RMB"//RMB本月贷方
//+ ",ACCGID=ISNULL((select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",PACCGID=ISNULL((select top 1 PACCGID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",DC2=ISNULL((select top 1 DC from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,4)),'')"
//+ ",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item WITH(NOLOCK) where GID=Cw_Vouitems_Gl.ITEMGID),'')"
//+ ",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept WITH(NOLOCK) where GID=Cw_Vouitems_Gl.DEPTGID),'')"
//+ " from Cw_Vouitems_Gl WITH(NOLOCK) where 1=1"
//+ " and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='" + strCwSTARTGID + "' and ISCHECKED=0 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'" + strVOUDATE + "',23),1,7))"
//+ ") as a"
//+ " group BY ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2,ACCGID,PACCGID"
//+ ") as b"
//+ " order by ACCID,ACCNAME,ACCALL,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME";
+",(case FCYNO WHEN 'RMB' THEN AMTDR ELSE 0 END) as AMTDR_RMB"//RMB本月借方
+",(case FCYNO WHEN 'RMB' THEN AMTCR ELSE 0 END) as AMTCR_RMB"//RMB本月贷方
+",ACCGID=ISNULL((select top 1 GID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",PACCGID=ISNULL((select top 1 PACCGID from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",DC2=ISNULL((select top 1 DC from cw_accitems_gl WITH(NOLOCK) where ACCID=Cw_Vouitems_Gl.ACCID and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4)),'')"
+",ITEMNAME=ISNULL((select top 1 ITEMNAME from cw_item WITH(NOLOCK) where GID=Cw_Vouitems_Gl.ITEMGID),'')"
+",DEPTNAME=ISNULL((select top 1 DEPTNAME from sys_dept WITH(NOLOCK) where GID=Cw_Vouitems_Gl.DEPTGID),'')"
+" from Cw_Vouitems_Gl WITH(NOLOCK) where 1=1"
+" and ORDNO in(select ORDNO FROM Cw_Vouchers_Gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and ISCHECKED=0 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7))"
+") as a"
+" left join cw_genleg_accitems gl WITH(NOLOCK) on (gl.[STARTGID]='"+strCwSTARTGID+"' and gl.CURRENCY=a.FCYNO and gl.DEPTACC=a.DEPTNAME and gl.EMPLACC=a.SALE and gl.CORPACC=a.CUSTOMERNAME and gl.ITEMACC=a.ITEMNAME and gl.ACCDATE=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and gl.LINKGID=a.ACCGID) "
+" group BY ACCID,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME,FCYNO,DC2,ACCGID,PACCGID,gl.AMTYEARDR,gl.AMTYEARCR,gl.FCYYEARDR,gl.FCYYEARCR,gl.QTYYEARDR,gl.QTYYEARCR"
+") as b"
+" order by ACCID,CUSTOMERNAME,SALE,ITEMNAME,DEPTNAME";
sSQL="INSERT into [cw_genleg_accitems] (GID,YEAR,MONTH,LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,CURRENCY,AMTYEARDR,AMTYEARCR,AMTYEARBLC,AMTDR,AMTCR,AMTBLC,AMTLASTDR,AMTLASTCR,AMTLASTBLC,FCYYEARDR,FCYYEARCR,FCYYEARBLC,FCYDR,FCYCR,FCYBLC,FCYLASTDR,FCYLASTCR,FCYLASTBLC,QTYYEARDR,QTYYEARCR,QTYYEARBLC,QTYDR,QTYCR,QTYBLC,QTYLASTDR,QTYLASTCR,QTYLASTBLC,CORPID,ISDELETE,DELETEUSER,DELETETIME,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME,ISENABLE,PFADR,PFACR,STARTGID,ACCDATE) VALUES("
+" AMTLASTDR=(SELECT isnull(sum(AMTDR),0) from cw_genleg_accitems as a where STARTGID='"+strCwSTARTGID+"' and ACCDATE<=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and ISDELETE=0 and ISENABLE=1 and LINKGID=cw_genleg_accitems.LINKGID and DEPTACC=cw_genleg_accitems.DEPTACC and EMPLACC=cw_genleg_accitems.EMPLACC and CORPACC=cw_genleg_accitems.CORPACC and ITEMACC=cw_genleg_accitems.ITEMACC)"//RMB累计借方
+",AMTLASTCR=(SELECT isnull(sum(AMTCR),0) from cw_genleg_accitems as a where STARTGID='"+strCwSTARTGID+"' and ACCDATE<=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and ISDELETE=0 and ISENABLE=1 and LINKGID=cw_genleg_accitems.LINKGID and DEPTACC=cw_genleg_accitems.DEPTACC and EMPLACC=cw_genleg_accitems.EMPLACC and CORPACC=cw_genleg_accitems.CORPACC and ITEMACC=cw_genleg_accitems.ITEMACC)"//RMB累计贷方
+",FCYLASTDR=(SELECT isnull(sum(FCYDR),0) from cw_genleg_accitems as a where STARTGID='"+strCwSTARTGID+"' and ACCDATE<=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and ISDELETE=0 and ISENABLE=1 and LINKGID=cw_genleg_accitems.LINKGID and DEPTACC=cw_genleg_accitems.DEPTACC and EMPLACC=cw_genleg_accitems.EMPLACC and CORPACC=cw_genleg_accitems.CORPACC and ITEMACC=cw_genleg_accitems.ITEMACC)"//外币累计借方
+",FCYLASTCR=(SELECT isnull(sum(FCYCR),0) from cw_genleg_accitems as a where STARTGID='"+strCwSTARTGID+"' and ACCDATE<=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and ISDELETE=0 and ISENABLE=1 and LINKGID=cw_genleg_accitems.LINKGID and DEPTACC=cw_genleg_accitems.DEPTACC and EMPLACC=cw_genleg_accitems.EMPLACC and CORPACC=cw_genleg_accitems.CORPACC and ITEMACC=cw_genleg_accitems.ITEMACC)"//外币累计贷方
+",QTYLASTDR=(SELECT isnull(sum(QTYDR),0) from cw_genleg_accitems as a where STARTGID='"+strCwSTARTGID+"' and ACCDATE<=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and ISDELETE=0 and ISENABLE=1 and LINKGID=cw_genleg_accitems.LINKGID and DEPTACC=cw_genleg_accitems.DEPTACC and EMPLACC=cw_genleg_accitems.EMPLACC and CORPACC=cw_genleg_accitems.CORPACC and ITEMACC=cw_genleg_accitems.ITEMACC)"//累计借方
+",QTYLASTCR=(SELECT isnull(sum(QTYCR),0) from cw_genleg_accitems as a where STARTGID='"+strCwSTARTGID+"' and ACCDATE<=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and ISDELETE=0 and ISENABLE=1 and LINKGID=cw_genleg_accitems.LINKGID and DEPTACC=cw_genleg_accitems.DEPTACC and EMPLACC=cw_genleg_accitems.EMPLACC and CORPACC=cw_genleg_accitems.CORPACC and ITEMACC=cw_genleg_accitems.ITEMACC)"//累计贷方
+" where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and LINKGID in (select GID from [cw_accitems_gl] where [YEAR]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"' and ACCATTRIBUTE='损益')";
+" AMTLASTBLC=(case when (select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID)='借' then isnull((AMTDR-AMTCR),0) else isnull((AMTCR-AMTDR),0) end)"//RMB累计余额
+",FCYLASTBLC=(case when (select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID)='借' then isnull((FCYDR-FCYCR),0) else isnull((FCYCR-FCYDR),0) end)"//外币累计余额
+",QTYLASTBLC=(case when (select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID)='借' then isnull((QTYDR-QTYCR),0) else isnull((QTYCR-QTYDR),0) end)"//累计余额
+" where STARTGID='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7) and LINKGID in (select GID from [cw_accitems_gl] where [YEAR]='"+strYEAR+"' and [STARTGID]='"+strCwSTARTGID+"' and ACCATTRIBUTE='损益')";
stringlsSQL="select PACCGID from cw_accitems_gl WITH(NOLOCK) where DETAILED=1 and ACCID in (select ACCID from VW_cw_vouchers_vouitems_gl where STARTGID='"+strCwSTARTGID+"' and ISCHECKED=1 and SUBSTRING(convert(varchar,VOUDATE,23),1,7)=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,7)) and [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING(convert(varchar,'"+strVOUDATE+"',23),1,4) and PACCGID<>'0' and PACCGID<>'ZC' and PACCGID<>'FZ' and PACCGID<>'GT' and PACCGID<>'QY' and PACCGID<>'CB' and PACCGID<>'SY' group by PACCGID";
DataSetds=T_ALL_DA.GetAllSQL(lsSQL);
stringsSQLAcc="SELECT GID,ACCID,ACCNAME,DETAILED,DC,ISFCY,ISDEPTACC,ISEMPLACC,ISCORPACC,ISITEMACC,REMARKS,[YEAR],[MONTH],PACCGID=(case when (PACCGID='ZC' or PACCGID='FZ' or PACCGID='GT' or PACCGID='QY' or PACCGID='CB' or PACCGID='SY') then '0' else PACCGID end),ACCATTRIBUTE,ISENABLE,ACCTYPE,PACCID=(select top 1 ACCID from [cw_accitems_gl] as a where [STARTGID]='"+strCwSTARTGID+"' and a.gid=cw_accitems_gl.PACCGID),PACCNAME=(select top 1 ACCNAME from [cw_accitems_gl] as b where [STARTGID]='"+strCwSTARTGID+"' and b.gid=cw_accitems_gl.PACCGID),gid as [id],ACCID+' '+ACCNAME as [NAME],0 as DR,0 as CR from [cw_accitems_gl] WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and [YEAR]=SUBSTRING('"+strVOUDATE+"',1,4) order by [YEAR],ACCID";
varsSQL="SELECT CURRENCY,sum(AMTYEARDR) as AMTYEARDR,sum(AMTYEARCR) as AMTYEARCR,sum(case when '"+pDC+"'=DC then AMTYEARBLC else (-AMTYEARBLC) end) as AMTYEARBLC,sum(AMTDR) as AMTDR,sum(AMTCR) as AMTCR,sum(case when '"+pDC+"'=DC then AMTBLC else (-AMTBLC) end) as AMTBLC,sum(AMTLASTDR) as AMTLASTDR,sum(AMTLASTCR) as AMTLASTCR,sum(case when '"+pDC+"'=DC then AMTLASTBLC else (-AMTLASTBLC) end) as AMTLASTBLC,sum(FCYYEARDR) as FCYYEARDR,sum(FCYYEARCR) as FCYYEARCR,sum(case when '"+pDC+"'=DC then FCYYEARBLC else (-FCYYEARBLC) end) as FCYYEARBLC,sum(FCYDR) as FCYDR,sum(FCYCR) as FCYCR,sum(case when '"+pDC+"'=DC then FCYBLC else (-FCYBLC) end) as FCYBLC,sum(FCYLASTDR) as FCYLASTDR,sum(FCYLASTCR) as FCYLASTCR,sum(case when '"+pDC+"'=DC then FCYLASTBLC else (-FCYLASTBLC) end) as FCYLASTBLC,sum(QTYYEARDR) as QTYYEARDR,sum(QTYYEARCR) as QTYYEARCR,sum(case when '"+pDC+"'=DC then QTYYEARBLC else (-QTYYEARBLC) end) as QTYYEARBLC,sum(QTYDR) as QTYDR,sum(QTYCR) as QTYCR,sum(case when '"+pDC+"'=DC then QTYBLC else (-QTYBLC) end) as QTYBLC,sum(QTYLASTDR) as QTYLASTDR,sum(QTYLASTCR) as QTYLASTCR,sum(case when '"+pDC+"'=DC then QTYLASTBLC else (-QTYLASTBLC) end) as QTYLASTBLC,sum(PFADR) as PFADR,sum(PFACR) as PFACR from [vw_cw_genleg_accitems_gl] WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,'"+strACCDATE+"',23),1,7) and LINKGID in(select gid from cw_accitems_gl WITH(NOLOCK) where PACCGID='"+PACCGID.Trim()+"' and [YEAR]=SUBSTRING('"+strACCDATE+"',1,4) and [STARTGID]='"+strCwSTARTGID+"') group by CURRENCY";
DataSetdsLS=T_ALL_DA.GetAllSQL(sSQL);
if(dsLS!=null)
{
if(dsLS.Tables[0].Rows.Count>0)
{
foreach(DataTablemyTableindsLS.Tables)
{
foreach(DataRowdsLSRowinmyTable.Rows)//遍历表
{
stringPACCGID_gid=T_ALL_DA.GetStrSQL("gid","select top 1 isnull(gid,'') as gid from cw_genleg_accitems WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and ACCDATE=SUBSTRING(convert(varchar,'"+strACCDATE+"',23),1,7) and [YEAR]=SUBSTRING('"+strACCDATE+"',1,4) and LINKGID='"+PACCGID.Trim()+"' and DEPTACC='' and EMPLACC='' and CORPACC='' and ITEMACC='' and CURRENCY='"+dsLSRow["CURRENCY"].ToString().Trim()+"'");
stringsSql="update cw_genleg_accitems set [STARTGID]='"+strCwSTARTGID+"',CORPID='"+strCOMPANYID+"',MODIFIEDUSER='"+strUSERID+"',MODIFIEDTIME=GETDATE() where STARTGID='' or STARTGID is null";
strSql.Append("SELECT ORDNO,VOUALLNO,convert(int,VOUNO) as VOUNO,VOUDATE from VW_cw_vouchers_vouitems_gl WITH(NOLOCK) where STARTGID='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null)");
if(!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and "+strCondition.Replace("ACCDATE","SUBSTRING(convert(char(10),VOUDATE,120),1,7)"));
}
strSql.Append(" group by ORDNO,VOUALLNO,VOUNO,VOUDATE");//
//strSql.Append(" order by convert(int,VOUNO) desc,VOUDATE desc");
strSql.Append(" order by VOUALLNO desc,VOUDATE desc");
sSQL="select top 1 (case when (FARCODE='' or FAPCODE='') then '' else FARCODE end) as FINANCESOFTCODE from info_client WITH(NOLOCK) where SHORTNAME='"+VALUE.ToString()+"' and corpid='"+strCompanyID+"'";
sSQL="select top 1 FINANCESOFTCODE from user_baseinfo,[user] WITH(NOLOCK) where [user].GID=user_baseinfo.USERID and [user].SHOWNAME='"+VALUE.ToString()+"'";
}
elseif(TYPE=="ITEMNAME")
{
sSQL="select top 1 FINANCESOFTCODE from cw_item WITH(NOLOCK) where [STARTGID]='"+strCwSTARTGID+"' and (ISDELETE=0 or ISDELETE is null) and ITEMNAME='"+VALUE.ToString()+"'";//
sSQL="select VOUALLNO,cha from (select (SUM(AMTDR)-SUM(AMTCR)) as cha,VOUALLNO from VW_cw_vouchers_vouitems_gl where 1=1"+sCondition+" group by VOUALLNO,ORDNO) as a where cha<>'0.00' group by VOUALLNO,cha order by cha desc";
//sSQL = "select VOUALLNO from (select (case SUM(AMTDR) when '0.00' then SUM(AMTCR) else SUM(AMTDR) end) as amt,VOUALLNO=(select VOUALLNO from cw_vouchers_gl where ORDNO=VW_cw_vouchers_vouitems_gl.ORDNO) from VW_cw_vouchers_vouitems_gl where 1=1" + sCondition + " group by VOUALLNO,ORDNO) as a where amt='0.00' group by VOUALLNO order by VOUALLNO";
//ds = T_ALL_DA.GetAllSQL(sSQL);
//if (ds != null)
//{
// for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
sSQL="select VOUALLNO from (select VOUALLNO=(select VOUALLNO from cw_vouchers_gl where ORDNO=VW_cw_vouchers_vouitems_gl.ORDNO) from VW_cw_vouchers_vouitems_gl where (accid is null or accid='')"+sCondition+") as a group by VOUALLNO order by VOUALLNO";
sSQL="select CUSTOMERNAME,VOUALLNO from (select CUSTOMERNAME,VOUALLNO=(select VOUALLNO from cw_vouchers_gl where ORDNO=VW_cw_vouchers_vouitems_gl.ORDNO) from VW_cw_vouchers_vouitems_gl where ISCORPACC=1 and (CUSTOMERNAME is not null and CUSTOMERNAME<>'' and (CORPID is null or CORPID='') and (FIELD3 is null or FIELD3=''))"+sCondition+") as a group by CUSTOMERNAME,VOUALLNO order by VOUALLNO";
sSQL="select VOUALLNO from (select VOUALLNO=(select VOUALLNO from cw_vouchers_gl where ORDNO=VW_cw_vouchers_vouitems_gl.ORDNO) from VW_cw_vouchers_vouitems_gl where ISEMPLACC=1 and (EMPLID is null or EMPLID='')"+sCondition+") as a group by VOUALLNO order by VOUALLNO";
sSQL="select VOUALLNO from (select VOUALLNO=(select VOUALLNO from cw_vouchers_gl where ORDNO=VW_cw_vouchers_vouitems_gl.ORDNO) from VW_cw_vouchers_vouitems_gl where ISDEPTACC=1 and (DEPTID is null or DEPTID='')"+sCondition+") as a group by VOUALLNO order by VOUALLNO";
sSQL="select VOUALLNO from (select VOUALLNO=(select VOUALLNO from cw_vouchers_gl where ORDNO=VW_cw_vouchers_vouitems_gl.ORDNO) from VW_cw_vouchers_vouitems_gl where ISITEMACC=1 and (ITEMID is null or ITEMID='')"+sCondition+") as a group by VOUALLNO order by VOUALLNO";
+" VouDate,AccMonth,VKNO,VOUNO,ROW_NUMBER() OVER ( PARTITION BY ORDNO ORDER BY ORDNO,ITEMNO )-1 as ITEMNO ,left(ExPlan,118) ExPlan,ACCID,CORPACC,CorpID,CUSTOMERNAME"
+",CASE WHEN ISNULL(EMPLACC,'')<>'' then EMPLACC ELSE CORPACC END,case when isnull(EMPLID,'')<>'' then EMPLID else CorpID end,case when isnull(EMPLID,'')<>'' then EMPLID else CorpID end,null,null,null,null,null,null,left(BLNO,118) BLNO"//财务说不需要提单号字段的内容导入金蝶中
+",FCYNO,FCYEXRATE,DC,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO"
+" VouDate,AccMonth,VKNO,VOUNO,ROW_NUMBER() OVER ( PARTITION BY ORDNO ORDER BY ORDNO,ITEMNO )-1 as ITEMNO,left(ExPlan,118) ExPlan,ACCID,CORPACC,CorpID,CUSTOMERNAME"
+",CASE WHEN ISNULL(EMPLACC,'')<>'' then EMPLACC ELSE CORPACC END,case when isnull(EMPLID,'')<>'' then EMPLID else CorpID end,case when isnull(EMPLID,'')<>'' then EMPLID else CorpID end,null,null,null,null,null,null,BLNO"
+",FCYNO,FCYEXRATE,DC,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO,FSETTLDATE"
+" VouDate,AccMonth,VKNO,VOUNO,ROW_NUMBER() OVER ( PARTITION BY ORDNO ORDER BY ORDNO,ITEMNO )-1 as ITEMNO,left(ExPlan,118) ExPlan,ACCID,CORPACC,CorpID,CUSTOMERNAME"
+",FCYNO,FCYEXRATE,CASE WHEN DC='D' THEN '1' ELSE '0' END,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO"
+" VouDate,VouDate,AccMonth,VKNO,VOUNO,ROW_NUMBER() OVER ( PARTITION BY ORDNO ORDER BY ORDNO,ITEMNO )-1 as ITEMNO,left(ExPlan,118) ExPlan,ACCID,CORPACC,CorpID,CUSTOMERNAME"
+",CASE WHEN ISNULL(EMPLACC,'')<>'' then EMPLACC ELSE CORPACC END,case when isnull(EMPLID,'')<>'' then EMPLID else CorpID end,case when isnull(EMPLID,'')<>'' then EMPLID else CorpID end,null,null,null,null,null,null,BLNO"
+",FCYNO,FCYEXRATE,DC,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO"
sbl=T_ALL_DA.GetStrSQL("gid","select gid from cw_design_gl where AR='"+myRow["ACCID"].ToString().Trim()+"' or ARFC='"+myRow["ACCID"].ToString().Trim()+"' or AP='"+myRow["ACCID"].ToString().Trim()+"' or APFC='"+myRow["ACCID"].ToString().Trim()+"'");
+" VouDate,VouDate,AccMonth,VKNO,VOUNO,ROW_NUMBER() OVER ( PARTITION BY ORDNO ORDER BY ORDNO,ITEMNO )-1 as ITEMNO,left(ExPlan,118) ExPlan,ACCID"
+",CASE WHEN ISNULL(CORPACC,'')='' THEN DEPTACC ELSE CORPACC END,CASE WHEN ISNULL(CORPACC,'')='' THEN DEPTID ELSE CorpID END,CASE WHEN ISNULL(CORPACC,'')='' THEN DEPTNAME ELSE CUSTOMERNAME END"
+",CASE WHEN ISNULL(EMPLACC,'')<>'' then EMPLACC ELSE '' END,case when isnull(EMPLID,'')<>'' then EMPLID else '' end,case when isnull(EMPLID,'')<>'' then EMPLID else '' end,null,null,null,null,null,null"
// + ",BLNO,FCYNO,FCYEXRATE,DC,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO,FSETTLDATE"
+",BLNO,FCYNO,FCYEXRATE,CASE WHEN DC='D' THEN '1' ELSE CASE WHEN DC='C' THEN '0' ELSE DC END END,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO"
+" VouDate,AccMonth,VKNO,VOUNO,ROW_NUMBER() OVER ( PARTITION BY ORDNO ORDER BY ORDNO,ITEMNO )-1 as ITEMNO,left(ExPlan,118) ExPlan,ACCID"
+",CASE WHEN ISNULL(CORPACC,'')='' THEN DEPTACC ELSE CORPACC END,CASE WHEN ISNULL(CORPACC,'')='' THEN DEPTID ELSE CorpID END,CASE WHEN ISNULL(CORPACC,'')='' THEN DEPTNAME ELSE CUSTOMERNAME END"
+",CASE WHEN ISNULL(EMPLACC,'')<>'' then EMPLACC ELSE '' END,case when isnull(EMPLID,'')<>'' then EMPLID else '' end,case when isnull(EMPLID,'')<>'' then EMPLID else '' end,null,null,null,null,null,null"
// + ",BLNO,FCYNO,FCYEXRATE,DC,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO,FSETTLDATE"
+",BLNO,FCYNO,FCYEXRATE,CASE WHEN DC='D' THEN '1' ELSE CASE WHEN DC='C' THEN '0' ELSE DC END END,(case FCYNO when 'RMB' then AMTDR+AMTCR else FCYDR+FCYCR end) as FFCYAMT,null,0,AMTDR,AMTCR,FSETTLCODE,FSETTLENO"
varcmdDelete=db.GetSqlStringCommand("update cw_vouchers_gl set ATTACHS=isnull((select count(*) as ATTACHS from [Receipt_Doc] where bsno='"+sORDNO+"'),1) where ORDNO='"+sORDNO+"'");
stringsSQL="select VKNO from VW_cw_vouchers_vouitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE>='"+accdatefr+"' and ACCDATE<='"+accdateto+"' group by VKNO";
DataSetds=T_ALL_DA.GetAllSQL(sSQL);
if(ds!=null)
{
if(ds.Tables[0].Rows.Count>0)
{
for(inti=0;i<ds.Tables[0].Rows.Count;i++)
{
sSQL="select VOUNO from VW_cw_vouchers_vouitems_gl where STARTGID='"+strCwSTARTGID+"' and ACCDATE>='"+accdatefr+"' and ACCDATE<='"+accdateto+"' and VKNO='"+ds.Tables[0].Rows[i]["VKNO"].ToString()+"' group by VOUNO order by convert(int,VOUNO) DESC";