using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCwGenlegAccitems; 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.RptCwGenlegAccitemsDAL { public class RptCwGenlegAccitemsDAL { #region 查询 static public List GetDataList(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCbHSKM, string strCURRENCY, string strCbJZF, string userid, string usercode, string companyid, string sort = null) { var strSql = new StringBuilder(); string sSql = GetDataListSQL(strCondition, ACCDATEbgn, ACCDATEend, strCbHSKM, strCURRENCY, strCbJZF, userid, usercode, companyid); strSql.Append(sSql); // var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by LINKCODE"); } return SetData(strSql); } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = strSql.ToString(); cmd.CommandTimeout = 1200000; //要加这一句 using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { MsCwGenlegAccitems data = new MsCwGenlegAccitems(); #region Set DB data to Object data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码 //data.YEAR = (reader["YEAR"] == null ? "" : Convert.ToString(reader["YEAR"]));//年 //data.MONTH = (reader["MONTH"] == null ? "" : Convert.ToString(reader["MONTH"]));//月 //data.LINKGID = (reader["LINKGID"] == null ? "" : Convert.ToString(reader["LINKGID"]));//科目GID //data.CURRENCY = (reader["CURRENCY"] == null ? "RMB" : Convert.ToString(reader["CURRENCY"]));//是否核算外币 data.DEPTACC = (reader["DEPTACC"] == null ? "" : Convert.ToString(reader["DEPTACC"]));//是否核算部门 data.EMPLACC = (reader["EMPLACC"] == null ? "" : Convert.ToString(reader["EMPLACC"]));//是否核算人员 data.CORPACC = (reader["CORPACC"] == null ? "" : Convert.ToString(reader["CORPACC"]));//是否算客户门 data.ITEMACC = (reader["ITEMACC"] == null ? "" : Convert.ToString(reader["ITEMACC"]));//是否核算项目 data.AMTYEARDR = (reader["YEARDR"] == null ? 0 : Convert.ToDecimal(reader["YEARDR"]));//年初借方 data.AMTYEARCR = (reader["YEARCR"] == null ? 0 : Convert.ToDecimal(reader["YEARCR"]));//年初贷方 data.AMTYEARBLC = (reader["YEARBLC"] == null ? 0 : Convert.ToDecimal(reader["YEARBLC"]));//年初余额 data.AMTDR = (reader["DR"] == null ? 0 : Convert.ToDecimal(reader["DR"]));//本月借方 data.AMTCR = (reader["CR"] == null ? 0 : Convert.ToDecimal(reader["CR"]));//本月贷方 data.AMTBLC = (reader["BLC"] == null ? 0 : Convert.ToDecimal(reader["BLC"]));//本月余额 data.AMTLASTDR = (reader["LASTDR"] == null ? 0 : Convert.ToDecimal(reader["LASTDR"]));//累计借方 data.AMTLASTCR = (reader["LASTCR"] == null ? 0 : Convert.ToDecimal(reader["LASTCR"]));//累计贷方 data.AMTLASTBLC = (reader["LASTBLC"] == null ? 0 : Convert.ToDecimal(reader["LASTBLC"]));//累计余额 data.QTYYEARDR = (reader["QTYYEARDR"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARDR"]));//年初借方 data.QTYYEARCR = (reader["QTYYEARCR"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARCR"]));//年初贷方 data.QTYYEARBLC = (reader["QTYYEARBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARBLC"]));//年初余额 data.QTYDR = (reader["QTYDR"] == null ? 0 : Convert.ToDecimal(reader["QTYDR"]));//本月借方 data.QTYCR = (reader["QTYCR"] == null ? 0 : Convert.ToDecimal(reader["QTYCR"]));//本月贷方 data.QTYBLC = (reader["QTYBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYBLC"]));//本月余额 data.QTYLASTDR = (reader["QTYLASTDR"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTDR"]));//累计借方 data.QTYLASTCR = (reader["QTYLASTCR"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTCR"]));//累计贷方 data.QTYLASTBLC = (reader["QTYLASTBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTBLC"]));//累计余额 data.AMTCPDR = (reader["AMTCPDR"] == null ? 0 : Convert.ToDecimal(reader["AMTCPDR"]));//期末余额 data.AMTCPCR = (reader["AMTCPCR"] == null ? 0 : Convert.ToDecimal(reader["AMTCPCR"]));//期末余额 data.QTYCPDR = (reader["QTYCPDR"] == null ? 0 : Convert.ToDecimal(reader["QTYCPDR"]));//折合本位币期末余额 data.QTYCPCR = (reader["QTYCPCR"] == null ? 0 : Convert.ToDecimal(reader["QTYCPCR"]));//折合本位币期末余额 //data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码 //data.ISDELETE = (reader["ISDELETE"] == null ? false : Convert.ToBoolean(reader["ISDELETE"]));//是否删除 //data.DELETEUSER = (reader["DELETEUSER"] == null ? "" : Convert.ToString(reader["DELETEUSER"]));//删除人 //string kjdE = reader["DELETETIME"].ToString();//删除时间 //if (reader["DELETETIME"] != null && reader["DELETETIME"].ToString().Trim().IndexOf("0001") < 0 && reader["DELETETIME"].ToString().Trim().IndexOf("1900") < 0 && reader["DELETETIME"].ToString().Trim()!="") //{ // data.DELETETIME = Convert.ToDateTime(reader["DELETETIME"]);//删除时间 //} //data.CREATEUSER = (reader["CREATEUSER"] == null ? "" : Convert.ToString(reader["CREATEUSER"]));//创建人gid //if (data.CREATETIME != null) // data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]);//创建时间 //data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid //if (data.MODIFIEDTIME != null) // data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//更改操作时间 data.LINKNAME = (reader["LINKNAME"] == null ? "" : Convert.ToString(reader["LINKNAME"]));//科目名称 //data.MODIFIEDUSERNAME = (reader["MODIFIEDUSERNAME"] == null ? "" : Convert.ToString(reader["MODIFIEDUSERNAME"]));//更改人 //data.DR = (reader["DR"] == null ? 0 : Convert.ToDecimal(reader["DR"])); //data.CR = (reader["CR"] == null ? 0 : Convert.ToDecimal(reader["CR"])); //data.ISENABLE = (reader["ISENABLE"] == null ? false : Convert.ToBoolean(reader["ISENABLE"])); //data.PFADR = (reader["PFADR"] == null ? 0 : Convert.ToDecimal(reader["PFADR"])); //data.PFACR = (reader["PFACR"] == null ? 0 : Convert.ToDecimal(reader["PFACR"])); data.LINKCODE = (reader["LINKCODE"] == null ? "" : Convert.ToString(reader["LINKCODE"])); //data.ACCDATE = (reader["ACCDATE"] == null ? "" : Convert.ToString(reader["ACCDATE"]));//会计期间 #endregion headList.Add(data); } reader.Close(); } return headList; } static public string GetDataListStr(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCbHSKM, string strCURRENCY, string strCbJZF, string userid, string usercode, string companyid) { var strSql = new StringBuilder(); string sSql = GetDataListSQL(strCondition, ACCDATEbgn, ACCDATEend, strCbHSKM, strCURRENCY, strCbJZF, userid, usercode, companyid); // string strls = "select "; strls += "GID,LINKCODE as 科目代码,LINKNAME 科目名称,YEARDR 期初借方,YEARCR 期初贷方,YEARBLC 期初余额,DR 本期发生借方,CR 本期发生贷方,BLC 本期发生余额,LASTDR 本年累计借方,LASTCR 本年累计贷方,LASTBLC 本年累计余额,QTYYEARDR 折合本位币期初借方,QTYYEARCR 折合本位币期初贷方,QTYYEARBLC 折合本位币期初余额,QTYDR 折合本位币本期发生借方,QTYCR 折合本位币本期发生贷方,QTYBLC 折合本位币本期发生余额,QTYLASTDR 折合本位币本年累计借方,QTYLASTCR 折合本位币本年累计贷方,QTYLASTBLC 折合本位币本年累计余额,QTYCPDR 折合本位币期末余额借方,QTYCPCR 折合本位币期末余额贷方,AMTCPDR 期末余额借方,AMTCPCR 期末余额贷方"; strls += " from ("+sSql+") as z order by LINKCODE"; // strSql.Append(strls); return strSql.ToString(); } static public string GetDataListSQL(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCbHSKM, string strCURRENCY, string strCbJZF, string userid, string usercode, string companyid) { string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid); string strACCDATE = ACCDATEbgn.Replace(" and ACCDATE='", "").Replace("'", ""); string sACCDATEbgnend = ACCDATEbgn.Replace("=", ">=") + ACCDATEend.Replace("=", "<="); string sACCDATE01end = " and ACCDATE>='" + DateTime.Parse(strACCDATE+"-01").Year.ToString() + "-01'" + ACCDATEend.Replace("=", "<="); var strSql = new StringBuilder(); if (strCURRENCY.Trim() == "综合本位币") { #region 综合本位币 strSql.Append("("); //strSql.Append("SELECT *"); strSql.Append("SELECT newid() as GID,LINKCODE,DC,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC"); strSql.Append(",isnull(sum(YEARDR),0) as YEARDR,isnull(sum(YEARCR),0) as YEARCR,isnull(sum(YEARBLC),0) as YEARBLC,isnull(sum(DR),0) as DR,isnull(sum(CR),0) as CR,isnull(sum(BLC),0) as BLC,isnull(sum(LASTDR),0) as LASTDR,isnull(sum(LASTCR),0) as LASTCR,isnull(sum(LASTBLC),0) as LASTBLC,isnull(sum(AMTCPDR),0) as AMTCPDR,isnull(sum(AMTCPCR),0) as AMTCPCR,isnull(sum(QTYYEARDR),0) as QTYYEARDR,isnull(sum(QTYYEARCR),0) as QTYYEARCR,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC,isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC,isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC,isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(",QTYCPDR=isnull((case when DC='借' then (isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0))-(isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0)) else 0 end),0)"); strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0))-(isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0)) else 0 end),0)"); strSql.Append(" from ("); strSql.Append("SELECT newid() as GID,LINKCODE,DC"); if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(",(LINKNAME+(case when (CORPACC='' or CORPACC is null) then '' else ('▁'+CORPACC) end)+(case when (DEPTACC='' or DEPTACC is null) then '' else ('▁'+DEPTACC) end)+(case when (EMPLACC='' or EMPLACC is null) then '' else ('▁'+EMPLACC) end)+(case when (ITEMACC='' or ITEMACC is null) then '' else ('▁'+ITEMACC) end)) as LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC"); } else { strSql.Append(",LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC"); } strSql.Append(",0 as YEARDR"); strSql.Append(",0 as YEARCR"); strSql.Append(",0 as YEARBLC"); strSql.Append(",0 as DR"); strSql.Append(",0 as CR"); strSql.Append(",0 as BLC"); strSql.Append(",0 as LASTDR"); strSql.Append(",0 as LASTCR"); strSql.Append(",0 as LASTBLC"); strSql.Append(",0 as AMTCPDR"); strSql.Append(",0 as AMTCPCR"); strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 " + strCbHSKM + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(" from ("); strSql.Append("select *,LINKCODE=(select top 1 ACCID from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),LINKNAME=(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) from cw_genleg_accitems with(nolock) where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 " + strCbHSKM); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC,STARTGID"); } else { //strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC"); strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID"); } strSql.Append(") as b where 1=1"); if (!string.IsNullOrEmpty(strCbJZF)) { strSql.Append(" and " + strCbJZF); } if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(" group by LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC"); } else { //strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC"); strSql.Append(" group by LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC"); } strSql.Append(") union ("); strSql.Append("SELECT NEWID() as GID,'合计:' as LINKCODE,'' as DC,'' as LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC"); strSql.Append(",0 as YEARDR,0 as YEARCR,0 as YEARBLC,0 as DR,0 as CR,0 as BLC,0 as LASTDR,0 as LASTCR,0 as LASTBLC,0 as AMTCPDR,0 as AMTCPCR"); strSql.Append(",isnull(sum(QTYYEARDR),0) as QTYYEARDR"); strSql.Append(",isnull(sum(QTYYEARCR),0) as QTYYEARCR"); strSql.Append(",isnull(sum(QTYYEARBLC),0) as QTYYEARBLC"); strSql.Append(",isnull(sum(QTYDR),0) as QTYDR"); strSql.Append(",isnull(sum(QTYCR),0) as QTYCR"); strSql.Append(",isnull(sum(QTYBLC),0) as QTYBLC"); strSql.Append(",isnull(sum(QTYLASTDR),0) as QTYLASTDR"); strSql.Append(",isnull(sum(QTYLASTCR),0) as QTYLASTCR"); strSql.Append(",isnull(sum(QTYLASTBLC),0) as QTYLASTBLC"); strSql.Append(",isnull(sum(PFADR),0) as PFADR"); strSql.Append(",isnull(sum(PFACR),0) as PFACR"); strSql.Append(",isnull(sum(QTYCPDR),0) as QTYCPDR"); strSql.Append(",isnull(sum(QTYCPCR),0) as QTYCPCR"); strSql.Append(" from ("); strSql.Append("SELECT *"); strSql.Append(",QTYCPDR=isnull((case when DC='借' then (QTYYEARDR+QTYDR)-(QTYYEARCR+QTYCR) else 0 end),0)"); strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (QTYYEARCR+QTYCR)-(QTYYEARDR+QTYDR) else 0 end),0)"); strSql.Append(" from ("); strSql.Append("SELECT STARTGID"); strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1" + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(",DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=a.LINKGID)"); strSql.Append(" from cw_genleg_accitems as a"); strSql.Append(" where a.[STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1"); strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] with(nolock) where [YEAR]=SUBSTRING('" + strACCDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "' and DETAILED=1)");//合计数不计算子科目,否则会造成重复合计 if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID"); strSql.Append(") as b where 1=1"); if (!string.IsNullOrEmpty(strCbJZF)) { strSql.Append(" and " + strCbJZF); } strSql.Append(") as c"); strSql.Append(")"); #endregion } else if (strCURRENCY.Trim() == "RMB") { #region RMB strSql.Append("("); //strSql.Append("SELECT *"); strSql.Append("SELECT newid() as GID,LINKCODE,DC,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC"); //strSql.Append(",YEARDR,YEARCR,YEARBLC,DR,CR,BLC,LASTDR,LASTCR,LASTBLC,QTYYEARDR,QTYYEARCR,QTYYEARBLC,QTYDR,QTYCR,QTYBLC,QTYLASTDR,QTYLASTCR,QTYLASTBLC,PFADR,PFACR"); strSql.Append(",isnull(sum(YEARDR),0) as YEARDR,isnull(sum(YEARCR),0) as YEARCR,isnull(sum(YEARBLC),0) as YEARBLC,isnull(sum(DR),0) as DR,isnull(sum(CR),0) as CR,isnull(sum(BLC),0) as BLC,isnull(sum(LASTDR),0) as LASTDR,isnull(sum(LASTCR),0) as LASTCR,isnull(sum(LASTBLC),0) as LASTBLC,isnull(sum(QTYYEARDR),0) as QTYYEARDR,isnull(sum(QTYYEARCR),0) as QTYYEARCR,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC,isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC,isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC,isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(",AMTCPDR=isnull((case when DC='借' then (isnull(sum(YEARDR),0)+isnull(sum(DR),0))-(isnull(sum(YEARCR),0)+isnull(sum(CR),0)) else 0 end),0)"); strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (isnull(sum(YEARCR),0)+isnull(sum(CR),0))-(isnull(sum(YEARDR),0)+isnull(sum(DR),0)) else 0 end),0)"); strSql.Append(",QTYCPDR=isnull((case when DC='借' then (isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0))-(isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0)) else 0 end),0)"); strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0))-(isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0)) else 0 end),0)"); strSql.Append(" from ("); strSql.Append("SELECT newid() as GID,LINKCODE,DC"); if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(",(LINKNAME+(case when (CORPACC='' or CORPACC is null) then '' else ('▁'+CORPACC) end)+(case when (DEPTACC='' or DEPTACC is null) then '' else ('▁'+DEPTACC) end)+(case when (EMPLACC='' or EMPLACC is null) then '' else ('▁'+EMPLACC) end)+(case when (ITEMACC='' or ITEMACC is null) then '' else ('▁'+ITEMACC) end)) as LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC"); } else { strSql.Append(",LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC"); } strSql.Append(",YEARDR=isnull((select sum(AMTYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARCR=isnull((select sum(AMTYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARBLC=isnull((select sum(AMTYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",DR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",CR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",BLC=isnull((select sum(AMTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTDR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTCR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTBLC=isnull((select sum(AMTLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR "); strSql.Append(" from ("); strSql.Append("select *,LINKCODE=(select top 1 ACCID from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),LINKNAME=(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) from cw_genleg_accitems with(nolock) where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC,STARTGID"); } else { //strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC"); strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID"); } strSql.Append(") as b where 1=1"); if (!string.IsNullOrEmpty(strCbJZF)) { strSql.Append(" and " + strCbJZF); } if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(" group by LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC"); } else { //strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC"); strSql.Append(" group by LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC"); } strSql.Append(") union ("); strSql.Append("SELECT NEWID() as GID,'合计:' as LINKCODE,'' as DC,'' as LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC"); strSql.Append(",isnull(sum(YEARDR),0) as YEARDR"); strSql.Append(",isnull(sum(YEARCR),0) as YEARCR"); strSql.Append(",isnull(sum(YEARBLC),0) as YEARBLC"); strSql.Append(",isnull(sum(DR),0) as DR"); strSql.Append(",isnull(sum(CR),0) as CR"); strSql.Append(",isnull(sum(BLC),0) as BLC"); strSql.Append(",isnull(sum(LASTDR),0) as LASTDR"); strSql.Append(",isnull(sum(LASTCR),0) as LASTCR"); strSql.Append(",isnull(sum(LASTBLC),0) as LASTBLC"); strSql.Append(",isnull(sum(QTYYEARDR),0) as QTYYEARDR"); strSql.Append(",isnull(sum(QTYYEARCR),0) as QTYYEARCR"); strSql.Append(",isnull(sum(QTYYEARBLC),0) as QTYYEARBLC"); strSql.Append(",isnull(sum(QTYDR),0) as QTYDR"); strSql.Append(",isnull(sum(QTYCR),0) as QTYCR"); strSql.Append(",isnull(sum(QTYBLC),0) as QTYBLC"); strSql.Append(",isnull(sum(QTYLASTDR),0) as QTYLASTDR"); strSql.Append(",isnull(sum(QTYLASTCR),0) as QTYLASTCR"); strSql.Append(",isnull(sum(QTYLASTBLC),0) as QTYLASTBLC"); strSql.Append(",isnull(sum(PFADR),0) as PFADR"); strSql.Append(",isnull(sum(PFACR),0) as PFACR"); strSql.Append(",isnull(sum(AMTCPDR),0) as AMTCPDR"); strSql.Append(",isnull(sum(AMTCPCR),0) as AMTCPCR"); strSql.Append(",isnull(sum(QTYCPDR),0) as QTYCPDR"); strSql.Append(",isnull(sum(QTYCPCR),0) as QTYCPCR"); strSql.Append(" from ("); strSql.Append("SELECT *"); strSql.Append(",AMTCPDR=isnull((case when DC='借' then (YEARDR+DR)-(YEARCR+CR) else 0 end),0)"); strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (YEARCR+CR)-(YEARDR+DR) else 0 end),0)"); strSql.Append(",QTYCPDR=isnull((case when DC='借' then (QTYYEARDR+QTYDR)-(QTYYEARCR+QTYCR) else 0 end),0)"); strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (QTYYEARCR+QTYCR)-(QTYYEARDR+QTYDR) else 0 end),0)"); strSql.Append(" from ("); strSql.Append("SELECT STARTGID"); strSql.Append(",YEARDR=isnull((select sum(AMTYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARCR=isnull((select sum(AMTYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARBLC=isnull((select sum(AMTYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",DR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",CR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",BLC=isnull((select sum(AMTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTDR=isnull((select sum(AMTDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTCR=isnull((select sum(AMTCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTBLC=isnull((select sum(AMTLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(",DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=a.LINKGID)"); strSql.Append(" from cw_genleg_accitems as a"); strSql.Append(" where a.[STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'"); strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] with(nolock) where [YEAR]=SUBSTRING('" + strACCDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "' and DETAILED=1)");// + strCbHSKM//合计数不计算子科目,否则会造成重复合计 if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID"); strSql.Append(") as b where 1=1"); if (!string.IsNullOrEmpty(strCbJZF)) { strSql.Append(" and " + strCbJZF); } strSql.Append(") as c"); strSql.Append(")"); #endregion } else//外币 { #region 外币 strSql.Append("("); //strSql.Append("SELECT *"); strSql.Append("SELECT newid() as GID,LINKCODE,DC,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC"); //strSql.Append(",YEARDR,YEARCR,YEARBLC,DR,CR,BLC,LASTDR,LASTCR,LASTBLC,QTYYEARDR,QTYYEARCR,QTYYEARBLC,QTYDR,QTYCR,QTYBLC,QTYLASTDR,QTYLASTCR,QTYLASTBLC,PFADR,PFACR"); strSql.Append(",isnull(sum(YEARDR),0) as YEARDR,isnull(sum(YEARCR),0) as YEARCR,isnull(sum(YEARBLC),0) as YEARBLC,isnull(sum(DR),0) as DR,isnull(sum(CR),0) as CR,isnull(sum(BLC),0) as BLC,isnull(sum(LASTDR),0) as LASTDR,isnull(sum(LASTCR),0) as LASTCR,isnull(sum(LASTBLC),0) as LASTBLC,isnull(sum(QTYYEARDR),0) as QTYYEARDR,isnull(sum(QTYYEARCR),0) as QTYYEARCR,isnull(sum(QTYYEARBLC),0) as QTYYEARBLC,isnull(sum(QTYDR),0) as QTYDR,isnull(sum(QTYCR),0) as QTYCR,isnull(sum(QTYBLC),0) as QTYBLC,isnull(sum(QTYLASTDR),0) as QTYLASTDR,isnull(sum(QTYLASTCR),0) as QTYLASTCR,isnull(sum(QTYLASTBLC),0) as QTYLASTBLC,isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(",AMTCPDR=isnull((case when DC='借' then (isnull(sum(YEARDR),0)+isnull(sum(DR),0))-(isnull(sum(YEARCR),0)+isnull(sum(CR),0)) else 0 end),0)"); strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (isnull(sum(YEARCR),0)+isnull(sum(CR),0))-(isnull(sum(YEARDR),0)+isnull(sum(DR),0)) else 0 end),0)"); strSql.Append(",QTYCPDR=isnull((case when DC='借' then (isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0))-(isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0)) else 0 end),0)"); strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (isnull(sum(QTYYEARCR),0)+isnull(sum(QTYCR),0))-(isnull(sum(QTYYEARDR),0)+isnull(sum(QTYDR),0)) else 0 end),0)"); strSql.Append(" from ("); strSql.Append("SELECT newid() as GID,LINKCODE,DC"); if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(",(LINKNAME+(case when (CORPACC='' or CORPACC is null) then '' else ('▁'+CORPACC) end)+(case when (DEPTACC='' or DEPTACC is null) then '' else ('▁'+DEPTACC) end)+(case when (EMPLACC='' or EMPLACC is null) then '' else ('▁'+EMPLACC) end)+(case when (ITEMACC='' or ITEMACC is null) then '' else ('▁'+ITEMACC) end)) as LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC"); } else { strSql.Append(",LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC"); } strSql.Append(",YEARDR=isnull((select sum(FCYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARCR=isnull((select sum(FCYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARBLC=isnull((select sum(FCYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",DR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",CR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",BLC=isnull((select sum(FCYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTDR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTCR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTBLC=isnull((select sum(FCYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR "); strSql.Append(" from ("); strSql.Append("select *,LINKCODE=(select top 1 ACCID from [cw_accitems_gl] with(nolock) where gid=cw_genleg_accitems.LINKGID),LINKNAME=(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) from cw_genleg_accitems with(nolock) where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + strCbHSKM); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC,STARTGID"); } else { //strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC"); strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID"); } strSql.Append(") as b where 1=1"); if (!string.IsNullOrEmpty(strCbJZF)) { strSql.Append(" and " + strCbJZF); } if (strCbHSKM.IndexOf("CORPACC") > -1) { strSql.Append(" group by LINKCODE,LINKNAME,DEPTACC,EMPLACC,CORPACC,ITEMACC,DC"); } else { //strSql.Append(" group by LINKGID,LINKCODE,LINKNAME,DC"); strSql.Append(" group by LINKCODE,LINKNAME,DC,DEPTACC,EMPLACC,CORPACC,ITEMACC"); } strSql.Append(") union ("); strSql.Append("SELECT NEWID() as GID,'合计:' as LINKCODE,'' as DC,'' as LINKNAME,'' as DEPTACC,'' as EMPLACC,'' as CORPACC,'' as ITEMACC"); strSql.Append(",isnull(sum(YEARDR),0) as YEARDR"); strSql.Append(",isnull(sum(YEARCR),0) as YEARCR"); strSql.Append(",isnull(sum(YEARBLC),0) as YEARBLC"); strSql.Append(",isnull(sum(DR),0) as DR"); strSql.Append(",isnull(sum(CR),0) as CR"); strSql.Append(",isnull(sum(BLC),0) as BLC"); strSql.Append(",isnull(sum(LASTDR),0) as LASTDR"); strSql.Append(",isnull(sum(LASTCR),0) as LASTCR"); strSql.Append(",isnull(sum(LASTBLC),0) as LASTBLC"); strSql.Append(",isnull(sum(QTYYEARDR),0) as QTYYEARDR"); strSql.Append(",isnull(sum(QTYYEARCR),0) as QTYYEARCR"); strSql.Append(",isnull(sum(QTYYEARBLC),0) as QTYYEARBLC"); strSql.Append(",isnull(sum(QTYDR),0) as QTYDR"); strSql.Append(",isnull(sum(QTYCR),0) as QTYCR"); strSql.Append(",isnull(sum(QTYBLC),0) as QTYBLC"); strSql.Append(",isnull(sum(QTYLASTDR),0) as QTYLASTDR"); strSql.Append(",isnull(sum(QTYLASTCR),0) as QTYLASTCR"); strSql.Append(",isnull(sum(QTYLASTBLC),0) as QTYLASTBLC"); strSql.Append(",isnull(sum(PFADR),0) as PFADR"); strSql.Append(",isnull(sum(PFACR),0) as PFACR"); strSql.Append(",isnull(sum(AMTCPDR),0) as AMTCPDR"); strSql.Append(",isnull(sum(AMTCPCR),0) as AMTCPCR"); strSql.Append(",isnull(sum(QTYCPDR),0) as QTYCPDR"); strSql.Append(",isnull(sum(QTYCPCR),0) as QTYCPCR"); strSql.Append(" from ("); strSql.Append("SELECT *"); strSql.Append(",AMTCPDR=isnull((case when DC='借' then (YEARDR+DR)-(YEARCR+CR) else 0 end),0)"); strSql.Append(",AMTCPCR=isnull((case when DC='贷' then (YEARCR+CR)-(YEARDR+DR) else 0 end),0)"); strSql.Append(",QTYCPDR=isnull((case when DC='借' then (QTYYEARDR+QTYDR)-(QTYYEARCR+QTYCR) else 0 end),0)"); strSql.Append(",QTYCPCR=isnull((case when DC='贷' then (QTYYEARCR+QTYCR)-(QTYYEARDR+QTYDR) else 0 end),0)"); strSql.Append(" from ("); strSql.Append("SELECT STARTGID"); strSql.Append(",YEARDR=isnull((select sum(FCYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARCR=isnull((select sum(FCYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",YEARBLC=isnull((select sum(FCYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",DR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",CR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",BLC=isnull((select sum(FCYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTDR=isnull((select sum(FCYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTCR=isnull((select sum(FCYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",LASTBLC=isnull((select sum(FCYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARDR=isnull((select sum(QTYYEARDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARCR=isnull((select sum(QTYYEARCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYYEARBLC=isnull((select sum(QTYYEARBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEbgn + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYBLC=isnull((select sum(QTYBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATEbgnend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTDR=isnull((select sum(QTYDR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTCR=isnull((select sum(QTYCR) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + sACCDATE01end + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",QTYLASTBLC=isnull((select sum(QTYLASTBLC) from cw_genleg_accitems with(nolock) where ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'" + ACCDATEend + " and LINKGID=a.LINKGID and DEPTACC=a.DEPTACC and EMPLACC=a.EMPLACC and CORPACC=a.CORPACC and ITEMACC=a.ITEMACC and [STARTGID]=a.[STARTGID]),0)"); strSql.Append(",isnull(sum(PFADR),0) as PFADR,isnull(sum(PFACR),0) as PFACR"); strSql.Append(",DC=(select top 1 DC from [cw_accitems_gl] with(nolock) where gid=a.LINKGID)"); strSql.Append(" from cw_genleg_accitems as a"); strSql.Append(" where a.[STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "'"); strSql.Append(" and LINKGID in (select GID from [cw_accitems_gl] with(nolock) where [YEAR]=SUBSTRING('" + strACCDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "' and DETAILED=1)");// + strCbHSKM//合计数不计算子科目,否则会造成重复合计 if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" group by LINKGID,DEPTACC,EMPLACC,CORPACC,ITEMACC,STARTGID"); strSql.Append(") as b where 1=1"); if (!string.IsNullOrEmpty(strCbJZF)) { strSql.Append(" and " + strCbJZF); } strSql.Append(") as c"); strSql.Append(")"); #endregion } return strSql.ToString(); } static public String GetData(string strUserID) { string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUserID); string strCwSTARTNAME = BasicDataRefDAL.GetCwSTARTNAME(strUserID); string strCwACCDATE = BasicDataRefDAL.GetCwACCDATE(strUserID); try { return strCwSTARTGID + "&" + strCwSTARTNAME + "&" + strCwACCDATE + "&" + strCwACCDATE.Substring(0, 4) + "&" + strCwACCDATE.Substring(5, 2); } catch { return ""; } } #endregion } }