using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.RptCwGLDetailColumn; 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.RptCwGLDetailColumnDAL { public class RptCwGLDetailColumnDAL { #region 查询 static public String GetDataList(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string strSubjectAccount, int acctype, string userid, string usercode, string companyid, string sort = null) { T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA(); var strSql = new StringBuilder(); string sSql = GetDataListSQL(strCondition, ACCDATEbgn, ACCDATEend, strCURRENCY, strACCID, strCustNAME, strDeptName, strSALE, strITEMNAME, strSubjectAccount, acctype, userid, usercode, companyid); if (sSql.Trim() != "") { strSql.Append(sSql); // var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by VOUDATE,EXPLAN2,VKNO,VOUNO,ITEMNO");//convert(INT,VOUNO) } } //return SetData(strSql); return strSql.ToString(); } static public string GetDataListStr(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string strSubjectAccount, int acctype, string userid, string usercode, string companyid) { var strSql = new StringBuilder(); string sSql = GetDataListSQL(strCondition, ACCDATEbgn, ACCDATEend, strCURRENCY, strACCID, strCustNAME, strDeptName, strSALE, strITEMNAME, strSubjectAccount, acctype, userid, usercode, companyid); if (sSql.Trim() != "") { strSql.Append(sSql); strSql.Append(" order by VOUDATE,EXPLAN2,VKNO,VOUNO,ITEMNO");//convert(INT,VOUNO) } return strSql.ToString(); } static public string GetDataListSQL(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string strSubjectAccount, int acctype, string userid, string usercode, string companyid) { #region 基本变量 T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA(); int iState = 0; int result = 0; string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid); string strACCDATE = ACCDATEbgn.Replace(" and ACCDATE='", "").Replace("'", ""); string sYear = DateTime.Parse(strACCDATE + "-01").Year.ToString(); string strCwSTARTMONTH = BasicDataRefDAL.GetCwSTARTMONTH(userid); if (strCwSTARTMONTH.IndexOf(sYear) < 0) { string sCwSTARTMONTH = sYear + "-01"; if (DateTime.Parse(strCwSTARTMONTH + "-01") < DateTime.Parse(sCwSTARTMONTH + "-01")) { strCwSTARTMONTH = sCwSTARTMONTH; } } if (DateTime.Parse(strCwSTARTMONTH + "-01") > DateTime.Parse(strACCDATE + "-01")) { ACCDATEbgn = ACCDATEbgn.Replace(strACCDATE, strCwSTARTMONTH); strCondition = strCondition.Replace(strACCDATE, strCwSTARTMONTH); strACCDATE = strCwSTARTMONTH; } string strACCDATE_end = ACCDATEend.Replace(" and ACCDATE='", "").Replace("'", ""); if (DateTime.Parse(strCwSTARTMONTH + "-01") > DateTime.Parse(strACCDATE_end + "-01")) { ACCDATEend = ACCDATEend.Replace(strACCDATE_end, strCwSTARTMONTH); strCondition = strCondition.Replace(strACCDATE_end, strCwSTARTMONTH); strACCDATE_end = strCwSTARTMONTH; } // string strCustNAME1 = strCustNAME; if (!string.IsNullOrEmpty(strCustNAME)) { strCustNAME1 = " and CUSTOMERNAME='" + strCustNAME + "'"; } string strCustNAME2 = strCustNAME; if (!string.IsNullOrEmpty(strCustNAME)) { strCustNAME2 = " and CORPACC='" + strCustNAME + "'"; } // string strDeptName1 = strDeptName; if (!string.IsNullOrEmpty(strDeptName)) { strDeptName1 = " and DEPTNAME='" + strDeptName + "'"; } string strDeptName2 = strDeptName; if (!string.IsNullOrEmpty(strDeptName)) { strDeptName2 = " and DEPTACC='" + strDeptName + "'"; } // string strSALE1 = strSALE; if (!string.IsNullOrEmpty(strSALE)) { strSALE1 = " and SALE='" + strSALE + "'"; } string strSALE2 = strSALE; if (!string.IsNullOrEmpty(strSALE)) { strSALE2 = " and EMPLACC='" + strSALE + "'"; } // string strITEMNAME1 = strITEMNAME; if (!string.IsNullOrEmpty(strITEMNAME)) { strITEMNAME1 = " and ITEMNAME='" + strITEMNAME + "'"; } string strITEMNAME2 = strITEMNAME; if (!string.IsNullOrEmpty(strITEMNAME)) { strITEMNAME2 = " and ITEMACC='" + strITEMNAME + "'"; } // string SubjectAccount = ""; string SubjectAccountName = ""; if (!string.IsNullOrEmpty(strSubjectAccount)) { if (strSubjectAccount.Trim() == "客户") { SubjectAccount = " and ISCORPACC=1"; SubjectAccountName = "CUSTOMERNAME"; } else if (strSubjectAccount.Trim() == "部门") { SubjectAccount = " and ISDEPTACC=1"; SubjectAccountName = "DEPTNAME"; } else if (strSubjectAccount.Trim() == "人员") { SubjectAccount = " and ISEMPLACC=1"; SubjectAccountName = "SALE"; } else if (strSubjectAccount.Trim() == "项目") { SubjectAccount = " and ISITEMACC=1"; SubjectAccountName = "ITEMNAME"; } else { SubjectAccount = ""; SubjectAccountName = ""; } } #endregion #region 基本查询语句 var strSql = new StringBuilder(); if (strCURRENCY.Trim() == "综合本位币") { #region 综合本位币 strSql.Append("(select GID,ORDNO,VKNO,VOUNO,ITEMNO,VOUDATE,EXPLAN,EXPLAN2,DC=(SELECT top 1 DC FROM cw_accitems_gl WHERE GID=a.LINKGID),DR,CR,isnull((CASE WHEN DC='借' THEN (DR-CR) ELSE (CR-DR) END),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID] from (select GID,ORDNO,LINKGID=(SELECT top 1 GID FROM cw_accitems_gl WHERE ACCID=VW_cw_vouchers_vouitems_gl.ACCID and STARTGID='" + strCwSTARTGID + "' and [YEAR]='" + sYear + "'),VKNO,VOUNO,ITEMNO,VOUDATE,(ACCYEAR+'-'+ACCMONTH) as ACCDATE,EXPLAN,2 as EXPLAN2,DC=(CASE WHEN DC='D' THEN '借' ELSE '贷' END),(case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END) as DR,(case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END) as CR from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 + ") as a where (DR<>0 OR CR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "%' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')"); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(")"); strSql.Append(" union "); strSql.Append("(SELECT newid() as GID,'' as VKNO,'' as ORDNO,0 as VOUNO,0 as ITEMNO,ACCDATE+'-01' as VOUDATE,'期初余额' as EXPLAN,1 as EXPLAN2,DC"); strSql.Append(",isnull(sum(QTYYEARDR),0) as DR,isnull(sum(QTYYEARCR),0) as CR,isnull(sum(QTYYEARBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + ACCDATEbgn + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append(" union "); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本期合计' as EXPLAN,3 as EXPLAN2,DC"); strSql.Append(",isnull(sum(QTYDR),0) as DR,isnull(sum(QTYCR),0) as CR,isnull(sum(QTYBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append(" union "); //strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本年累计' as EXPLAN,4 as EXPLAN2,DC"); //strSql.Append(",isnull(sum(QTYLASTDR),0) as DR,isnull(sum(QTYLASTCR),0) as CR,isnull(sum(QTYLASTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); //strSql.Append(" from ("); //strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); //if (!string.IsNullOrEmpty(strCondition)) //{ // strSql.Append(" and " + strCondition); //} ////strSql.Append(" and CORPID='" + companyid + "'"); //strSql.Append(") as a"); //strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本年累计' as EXPLAN,4 as EXPLAN2,DC"); strSql.Append(",isnull(sum(QTYDR),0) as DR,isnull(sum(QTYCR),0) as CR,isnull(sum(QTYLASTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select LINKGID,ACCDATE,[CREATEUSER],[CORPID],isnull(sum(QTYLASTBLC),0) as [QTYLASTBLC],DC=(select top 1 DC from [cw_accitems_gl] where gid=f.LINKGID)"); strSql.Append(",QTYDR=((select isnull(sum(QTYDR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append(")"); strSql.Append("+(select isnull(sum(QTYYEARDR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE='" + strCwSTARTMONTH + "'"); sls = sls.Replace(" and ACCDATE<='" + strACCDATE_end + "'", ""); strSql.Append(" and " + sls); } strSql.Append("))"); strSql.Append(",QTYCR=((select isnull(sum(QTYCR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append(")"); strSql.Append("+(select isnull(sum(QTYYEARCR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append("))"); strSql.Append(" from cw_genleg_accitems as f where f.[STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" GROUP BY LINKGID,ACCDATE,[CREATEUSER],[CORPID]"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); #endregion } else if (strCURRENCY.Trim() == "RMB") { #region RMB strSql.Append("(select GID,ORDNO,VKNO,VOUNO,ITEMNO,VOUDATE,EXPLAN,EXPLAN2,DC=(SELECT top 1 DC FROM cw_accitems_gl WHERE GID=a.LINKGID),DR,CR,BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID] from (select GID,ORDNO,LINKGID=(SELECT top 1 GID FROM cw_accitems_gl WHERE ACCID=VW_cw_vouchers_vouitems_gl.ACCID and STARTGID='" + strCwSTARTGID + "' and [YEAR]='" + sYear + "'),VKNO,VOUNO,ITEMNO,VOUDATE,(ACCYEAR+'-'+ACCMONTH) as ACCDATE,EXPLAN,2 as EXPLAN2,DC=(CASE WHEN DC='D' THEN '借' ELSE '贷' END),AMTDR as DR,AMTCR as CR,isnull((CASE WHEN DC='D' THEN (AMTDR-AMTCR) ELSE (AMTCR-AMTDR) END),0) as BLC from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1 and (AMTDR<>0 OR AMTCR<>0)" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 //+ " and (case when FCY='USD' then FCY else 'RMB' end)='" + strCURRENCY + "'" + " and FCYNO='" + strCURRENCY + "'" + ") as a where LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "%' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')");// and CURRENCY='" + strCURRENCY + "' if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(")"); strSql.Append(" union "); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,ACCDATE+'-01' as VOUDATE,'期初余额' as EXPLAN,1 as EXPLAN2,DC"); strSql.Append(",isnull(sum(AMTYEARDR),0) as DR,isnull(sum(AMTYEARCR),0) as CR,isnull(sum(AMTYEARBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + ACCDATEbgn + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append(" union "); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本期合计' as EXPLAN,3 as EXPLAN2,DC"); strSql.Append(",isnull(sum(AMTDR),0) as DR,isnull(sum(AMTCR),0) as CR,isnull(sum(AMTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append(" union "); //strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本年累计' as EXPLAN,4 as EXPLAN2,DC"); //strSql.Append(",isnull(sum(AMTLASTDR),0) as DR,isnull(sum(AMTLASTCR),0) as CR,isnull(sum(AMTLASTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); //strSql.Append(" from ("); //strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); //if (!string.IsNullOrEmpty(strCondition)) //{ // strSql.Append(" and " + strCondition); //} ////strSql.Append(" and CORPID='" + companyid + "'"); //strSql.Append(") as a"); //strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本年累计' as EXPLAN,4 as EXPLAN2,DC"); strSql.Append(",isnull(sum(AMTDR),0) as DR,isnull(sum(AMTCR),0) as CR,isnull(sum(AMTLASTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select LINKGID,ACCDATE,[CREATEUSER],[CORPID],isnull(sum(AMTLASTBLC),0) as [AMTLASTBLC],DC=(select top 1 DC from [cw_accitems_gl] where gid=f.LINKGID)"); strSql.Append(",AMTDR=((select isnull(sum(AMTDR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append(")"); strSql.Append("+(select isnull(sum(AMTYEARDR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE='" + strCwSTARTMONTH + "'"); sls = sls.Replace(" and ACCDATE<='" + strACCDATE_end + "'", ""); strSql.Append(" and " + sls); } strSql.Append("))"); strSql.Append(",AMTCR=((select isnull(sum(AMTCR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append(")"); strSql.Append("+(select isnull(sum(AMTYEARCR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append("))"); strSql.Append(" from cw_genleg_accitems as f where f.[STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (AMTLASTBLC<>0 OR AMTDR<>0 OR AMTCR<>0 or AMTLASTDR<>0 or AMTLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" GROUP BY LINKGID,ACCDATE,[CREATEUSER],[CORPID]"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); #endregion } else//外币 { #region 外币 strSql.Append("(select GID,ORDNO,VKNO,VOUNO,ITEMNO,VOUDATE,EXPLAN,EXPLAN2,DC=(SELECT top 1 DC FROM cw_accitems_gl WHERE GID=a.LINKGID),DR,CR,BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID] from (select GID,ORDNO,LINKGID=(SELECT top 1 GID FROM cw_accitems_gl WHERE ACCID=VW_cw_vouchers_vouitems_gl.ACCID and STARTGID='" + strCwSTARTGID + "' and [YEAR]='" + sYear + "'),VKNO,VOUNO,ITEMNO,VOUDATE,(ACCYEAR+'-'+ACCMONTH) as ACCDATE,EXPLAN,2 as EXPLAN2,DC=(CASE WHEN DC='D' THEN '借' ELSE '贷' END),FCYDR as DR,FCYCR as CR,isnull((CASE WHEN DC='D' THEN (FCYDR-FCYCR) ELSE (FCYCR-FCYDR) END),0) as BLC from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1 and (FCYDR<>0 OR FCYCR<>0)" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 //+ " and (case when FCY='USD' then FCY else 'RMB' end)='" + strCURRENCY + "'" + " and FCYNO='" + strCURRENCY + "'" + ") as a where LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "%' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')");// and CURRENCY='" + strCURRENCY + "' if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(")"); strSql.Append(" union "); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,ACCDATE+'-01' as VOUDATE,'期初余额' as EXPLAN,1 as EXPLAN2,DC"); strSql.Append(",isnull(sum(FCYYEARDR),0) as DR,isnull(sum(FCYYEARCR),0) as CR,isnull(sum(FCYYEARBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + ACCDATEbgn + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append(" union "); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本期合计' as EXPLAN,3 as EXPLAN2,DC"); strSql.Append(",isnull(sum(FCYDR),0) as DR,isnull(sum(FCYCR),0) as CR,isnull(sum(FCYBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append(" union "); //strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本年累计' as EXPLAN,4 as EXPLAN2,DC"); //strSql.Append(",isnull(sum(FCYLASTDR),0) as DR,isnull(sum(FCYLASTCR),0) as CR,isnull(sum(FCYLASTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); //strSql.Append(" from ("); //strSql.Append("select *,DC=(select top 1 DC from [cw_accitems_gl] where gid=cw_genleg_accitems.LINKGID) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); //if (!string.IsNullOrEmpty(strCondition)) //{ // strSql.Append(" and " + strCondition); //} ////strSql.Append(" and CORPID='" + companyid + "'"); //strSql.Append(") as a"); //strSql.Append(" group by LINKGID,ACCDATE,DC)"); strSql.Append("(SELECT newid() as GID,'' as ORDNO,'' as VKNO,0 as VOUNO,0 as ITEMNO,CONVERT(varchar(100),(DATEADD(mm,1,(ACCDATE+'-01'))-1),23) as VOUDATE,'本年累计' as EXPLAN,4 as EXPLAN2,DC"); strSql.Append(",isnull(sum(FCYDR),0) as DR,isnull(sum(FCYCR),0) as CR,isnull(sum(FCYLASTBLC),0) as BLC,'" + userid + "' as [CREATEUSER],'" + companyid + "' as [CORPID],'" + strCwSTARTGID + "' as [STARTGID]"); strSql.Append(" from ("); strSql.Append("select LINKGID,ACCDATE,[CREATEUSER],[CORPID],isnull(sum(FCYLASTBLC),0) as [FCYLASTBLC],DC=(select top 1 DC from [cw_accitems_gl] where gid=f.LINKGID)"); strSql.Append(",FCYDR=((select isnull(sum(FCYDR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append(")"); strSql.Append("+(select isnull(sum(FCYYEARDR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE='" + strCwSTARTMONTH + "'"); sls = sls.Replace(" and ACCDATE<='" + strACCDATE_end + "'", ""); strSql.Append(" and " + sls); } strSql.Append("))"); strSql.Append(",FCYCR=((select isnull(sum(FCYCR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append(")"); strSql.Append("+(select isnull(sum(FCYYEARCR),0) from cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { string sls = strCondition.Replace("ACCDATE>='" + strACCDATE + "'", "ACCDATE>='" + sYear + "-01'"); sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE<=f.ACCDATE"); strSql.Append(" and " + sls); } strSql.Append("))"); strSql.Append(" from cw_genleg_accitems as f where f.[STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and CURRENCY='" + strCURRENCY + "' and (FCYLASTBLC<>0 OR FCYDR<>0 OR FCYCR<>0 or FCYLASTDR<>0 or FCYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID like '" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" GROUP BY LINKGID,ACCDATE,[CREATEUSER],[CORPID]"); strSql.Append(") as a"); strSql.Append(" group by LINKGID,ACCDATE,DC)"); #endregion } #endregion #region 清理临时表 using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { #region 清理临时表 String sSql = "delete from [cw_glDetail_rpt_temp] where CREATEUSER='" + userid + "' and CORPID='" + companyid + "'";//STARTGID='" + strCwSTARTGID + "' and iState = SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSql, null); if (iState < 0) { result = -3; sqlTran.Rollback(); } #endregion result = 1;//状态为1表示删除成功 sqlTran.Commit(); } catch (Exception execError) { sqlTran.Rollback(); result = -6;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } #endregion #region 插入临时表 using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { #region 插入临时表【[cw_glDetail_rpt_temp]】 //GID,VKNO,VOUNO,ITEMNO,VOUDATE,EXPLAN,EXPLAN2,DC,DR,CR,BLC string sSql = "insert into [cw_glDetail_rpt_temp]([GID],[ORDNO],[VKNO],[VOUNO],[ITEMNO],[VOUDATE],[EXPLAN],[EXPLAN2],[DC],[DR],[CR],[BLC],[CREATEUSER],[CORPID],[STARTGID]) " + strSql; iState = SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSql, null); if (iState < 0) { result = -4; sqlTran.Rollback(); } #endregion result = 1;//状态为1表示删除成功 sqlTran.Commit(); } catch (Exception execError) { sqlTran.Rollback(); result = -6;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } #endregion #region 更新临时表 using (SqlTransaction sqlTran1 = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { #region 插入合计到【[cw_glDetail_rpt_temp]】表 string sSql = "select * from cw_glDetail_rpt_temp where STARTGID='" + strCwSTARTGID + "' and [CREATEUSER]='" + userid + "' and [CORPID]='" + companyid + "' order by VOUDATE,EXPLAN2,VKNO,VOUNO,ITEMNO";//convert(INT,VOUNO) DataSet dsCGRT = T_ALL_DA.GetAllSQL(sSql); if (dsCGRT != null) { if (dsCGRT.Tables[0].Rows.Count > 0) { Decimal iBLD = 0; Decimal iBLC = 0; Decimal iBL = 0; for (int i = 0; i < dsCGRT.Tables[0].Rows.Count; i++) { #region 更新合计【[cw_glDetail_rpt_temp]】 string strDC = dsCGRT.Tables[0].Rows[i]["DC"].ToString(); if (dsCGRT.Tables[0].Rows[i]["EXPLAN"].ToString() != "本年累计") { if (dsCGRT.Tables[0].Rows[i]["EXPLAN"].ToString() != "本期合计") { //if (strDC.Trim() == "借") //{ // iBLD = iBLD + Decimal.Parse(dsCGRT.Tables[0].Rows[i]["BLC"].ToString()); //} //else //{ // iBLC = iBLC + Decimal.Parse(dsCGRT.Tables[0].Rows[i]["BLC"].ToString()); //} Decimal dlDR_F = Decimal.Parse(dsCGRT.Tables[0].Rows[i]["DR"].ToString()); if (dlDR_F < 0) { dlDR_F = dlDR_F * (-1); } Decimal dlCR_F = Decimal.Parse(dsCGRT.Tables[0].Rows[i]["CR"].ToString()); if (dlCR_F < 0) { dlCR_F = dlCR_F * (-1); } if (dlDR_F > dlCR_F) { iBLD = iBLD + Decimal.Parse(dsCGRT.Tables[0].Rows[i]["DR"].ToString()); } else { iBLC = iBLC + Decimal.Parse(dsCGRT.Tables[0].Rows[i]["CR"].ToString()); } //if (iBLD == 0) //{ // iBL = iBLC; //} //else //{ iBL = iBLD - iBLC; if (strDC.Trim() == "借") { iBL = iBLD - iBLC; } else { iBL = iBLC - iBLD; } //} // sSql = "update [cw_glDetail_rpt_temp] set BLC=" + iBL + " where GID='" + dsCGRT.Tables[0].Rows[i]["GID"].ToString() + "' and STARTGID='" + strCwSTARTGID + "' and [CREATEUSER]='" + userid + "' and [CORPID]='" + companyid + "'"; iState = SqlHelper.ExecuteNonQuery(sqlTran1, CommandType.Text, sSql, null); if (iState < 0) { result = -5; sqlTran1.Rollback(); } } else { sSql = "update [cw_glDetail_rpt_temp] set BLC=" + iBL + " where GID='" + dsCGRT.Tables[0].Rows[i]["GID"].ToString() + "' and STARTGID='" + strCwSTARTGID + "' and [CREATEUSER]='" + userid + "' and [CORPID]='" + companyid + "'"; iState = SqlHelper.ExecuteNonQuery(sqlTran1, CommandType.Text, sSql, null); if (iState < 0) { result = -5; sqlTran1.Rollback(); } } } #endregion } } } #endregion result = 1;//状态为1表示删除成功 sqlTran1.Commit(); } catch (Exception execError) { sqlTran1.Rollback(); result = -6;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } #endregion string sSQL = ""; //分币别变量 string sBB = "QTY"; if (strCURRENCY.Trim() == "综合本位币") { sBB = "QTY"; } else if (strCURRENCY.Trim() == "RMB") { sBB = "AMT"; } else//外币 { sBB = "FCY"; } if (acctype == 1) { #region 子科目循环 var strSql2 = new StringBuilder(); strSql2.Append("select *,ACCID+'-'+ACCNAME AS ACCIDNAME from cw_accitems_gl where YEAR='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "' and ACCID in (select ACCID from (select ACCID,ACCNAME,(ACCYEAR+'-'+ACCMONTH) as ACCDATE,DC=(CASE WHEN DC='D' THEN '借' ELSE '贷' END)" + ",(case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END) as DR" + ",(case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END) as CR" //+ ",isnull(" + sBB + "DR,0) as DR" //+ ",isnull(" + sBB + "CR,0) as CR" + " from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 + " and ACCID LIKE '" + strACCID + "%') as a where (DR<>0 OR CR<>0)"); if (!string.IsNullOrEmpty(strCondition)) { strSql2.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql2.Append(") order by ACCID"); DataSet ds = T_ALL_DA.GetAllSQL(strSql2.ToString()); #endregion #region 凭证数据 sSQL = "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (ds.Tables[0].Rows[i]["DC"].ToString() == "借") { //sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(AMTDR,0)=0 THEN isnull(-AMTCR,0) ELSE isnull(AMTDR,0) END) as AMT FROM VW_cw_vouchers_vouitems_gl WHERE ISDELETE=0 and ISCHECKED=1 and ORDNO=cw_glDetail_rpt_temp.ORDNO AND AMTDR=cw_glDetail_rpt_temp.DR AND AMTCR=cw_glDetail_rpt_temp.CR AND ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "'),0)"; if (strCURRENCY.Trim() == "综合本位币") { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull((case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0)=0 THEN isnull(-(case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END),0) ELSE isnull((case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid AND ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "'),0)"; } else { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(" + sBB + "DR,0)=0 THEN isnull(-" + sBB + "CR,0) ELSE isnull(" + sBB + "DR,0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid AND ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "'),0)"; } } else { //sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(AMTDR,0)=0 THEN isnull(AMTCR,0) ELSE isnull(-AMTDR,0) END) as AMT FROM VW_cw_vouchers_vouitems_gl WHERE ISDELETE=0 and ISCHECKED=1 and ORDNO=cw_glDetail_rpt_temp.ORDNO AND AMTDR=cw_glDetail_rpt_temp.DR AND AMTCR=cw_glDetail_rpt_temp.CR AND ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "'),0)"; if (strCURRENCY.Trim() == "综合本位币") { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull((case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0)=0 THEN isnull((case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END),0) ELSE isnull(-(case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid AND ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "'),0)"; } else { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(" + sBB + "DR,0)=0 THEN isnull(" + sBB + "CR,0) ELSE isnull(-" + sBB + "DR,0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid AND ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "'),0)"; } } } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and ORDNO<>'')"; #endregion #region 期初余额 sSQL += " union "; sSQL += "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select isnull(SUM(" + sBB + "YEARBLC),0) as " + sBB + " FROM cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + ACCDATEbgn + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2; if (!string.IsNullOrEmpty(strCondition)) { sSQL += " and " + strCondition; } //strSql.Append(" and CORPID='" + companyid + "'"); sSQL += "),0)"; } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and EXPLAN='期初余额')"; #endregion #region 本期合计 sSQL += " union "; sSQL += "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select isnull(SUM(" + sBB + "BLC),0) as " + sBB + " FROM cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "') and ACCDATE=convert(varchar(7),[cw_glDetail_rpt_temp].VOUDATE,120)" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2; if (!string.IsNullOrEmpty(strCondition)) { sSQL += " and " + strCondition; } //strSql.Append(" and CORPID='" + companyid + "'"); sSQL += "),0)"; } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and EXPLAN='本期合计')"; #endregion #region 本年累计 sSQL += " union "; sSQL += "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sSQL += ",[" + ds.Tables[0].Rows[i]["ACCNAME"].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select isnull(SUM(" + sBB + "LASTBLC),0) as " + sBB + " FROM cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID='" + ds.Tables[0].Rows[i]["ACCID"].ToString() + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "') and ACCDATE=convert(varchar(7),[cw_glDetail_rpt_temp].VOUDATE,120)" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2; if (!string.IsNullOrEmpty(strCondition)) { sSQL += " and " + strCondition; } //strSql.Append(" and CORPID='" + companyid + "'"); sSQL += "),0)"; } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and EXPLAN='本年累计')"; #endregion } else { #region 核算项目循环 string sSQL2 = "SELECT " + SubjectAccountName + ",DC from (select " + SubjectAccountName + ",DC=(SELECT top 1 DC FROM cw_accitems_gl WHERE ACCID='" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "') from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1 and ORDNO in (SELECT ORDNO from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' group by ORDNO) AND ACCID='" + strACCID + "'" + SubjectAccount + ") as a group by DC," + SubjectAccountName; DataSet ds = T_ALL_DA.GetAllSQL(sSQL2); if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { #region 凭证数据 sSQL = "(SELECT *"; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (ds.Tables[0].Rows[i]["DC"].ToString() == "借") { //sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(AMTDR,0)=0 THEN isnull(-AMTCR,0) ELSE isnull(AMTDR,0) END) as AMT FROM VW_cw_vouchers_vouitems_gl WHERE ISDELETE=0 and ISCHECKED=1 and ORDNO=cw_glDetail_rpt_temp.ORDNO AND AMTDR=cw_glDetail_rpt_temp.DR AND AMTCR=cw_glDetail_rpt_temp.CR AND ACCID='" + strACCID + "'"; if (strCURRENCY.Trim() == "综合本位币") { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull((case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0)=0 THEN isnull(-(case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END),0) ELSE isnull((case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid"; } else { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(" + sBB + "DR,0)=0 THEN isnull(-" + sBB + "CR,0) ELSE isnull(" + sBB + "DR,0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid"; } } else { //sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(AMTDR,0)=0 THEN isnull(AMTCR,0) ELSE isnull(-AMTDR,0) END) as AMT FROM VW_cw_vouchers_vouitems_gl WHERE ISDELETE=0 and ISCHECKED=1 and ORDNO=cw_glDetail_rpt_temp.ORDNO AND AMTDR=cw_glDetail_rpt_temp.DR AND AMTCR=cw_glDetail_rpt_temp.CR AND ACCID='" + strACCID + "'"; if (strCURRENCY.Trim() == "综合本位币") { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull((case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0)=0 THEN isnull((case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END),0) ELSE isnull(-(case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END),0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid"; } else { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select (CASE WHEN isnull(" + sBB + "DR,0)=0 THEN isnull(" + sBB + "CR,0) ELSE isnull(-" + sBB + "DR,0) END) as " + sBB + " FROM VW_cw_vouchers_vouitems_gl WHERE gid=cw_glDetail_rpt_temp.gid"; } } string SubjectAccount2 = ""; if (!string.IsNullOrEmpty(strSubjectAccount)) { if (strSubjectAccount.Trim() == "客户") { SubjectAccount2 = " and CUSTOMERNAME='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "部门") { SubjectAccount2 = " and DEPTNAME='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "人员") { SubjectAccount2 = " and SALE='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "项目") { SubjectAccount2 = " and ITEMNAME='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } } sSQL += SubjectAccount + SubjectAccount2 + "),0)"; } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and ORDNO<>'')";// #endregion #region 期初余额 sSQL += " union "; sSQL += "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select isnull(SUM(" + sBB + "YEARBLC),0) as " + sBB + " FROM cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (" + sBB + "LASTBLC<>0 OR " + sBB + "DR<>0 OR " + sBB + "CR<>0 or " + sBB + "LASTDR<>0 or " + sBB + "LASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID='" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + ACCDATEbgn + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2; if (!string.IsNullOrEmpty(strCondition)) { sSQL += " and " + strCondition; } if (!string.IsNullOrEmpty(strSubjectAccount)) { if (strSubjectAccount.Trim() == "客户") { sSQL += " and [CORPACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "部门") { sSQL += " and [DEPTACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "人员") { sSQL += " and [EMPLACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "项目") { sSQL += " and [ITEMACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } } //strSql.Append(" and CORPID='" + companyid + "'"); sSQL += "),0)"; } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and EXPLAN='期初余额')"; #endregion #region 本期合计 sSQL += " union "; sSQL += "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select isnull(SUM(" + sBB + "BLC),0) as " + sBB + " FROM cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID='" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "') and ACCDATE=convert(varchar(7),[cw_glDetail_rpt_temp].VOUDATE,120)" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2; if (!string.IsNullOrEmpty(strCondition)) { sSQL += " and " + strCondition; } if (!string.IsNullOrEmpty(strSubjectAccount)) { if (strSubjectAccount.Trim() == "客户") { sSQL += " and [CORPACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "部门") { sSQL += " and [DEPTACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "人员") { sSQL += " and [EMPLACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "项目") { sSQL += " and [ITEMACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } } //strSql.Append(" and CORPID='" + companyid + "'"); sSQL += "),0)"; } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and EXPLAN='本期合计')"; #endregion #region 本年累计 sSQL += " union "; sSQL += "(SELECT *"; if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sSQL += ",[" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + ds.Tables[0].Rows[i]["DC"].ToString() + "]=ISNULL((select isnull(SUM(" + sBB + "LASTBLC),0) as " + sBB + " FROM cw_genleg_accitems where [STARTGID]='" + strCwSTARTGID + "' and ISDELETE=0 and ISENABLE=1 and (QTYLASTBLC<>0 OR QTYDR<>0 OR QTYCR<>0 or QTYLASTDR<>0 or QTYLASTCR<>0) and LINKGID in (select GID from cw_accitems_gl where ACCID='" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "') and ACCDATE=convert(varchar(7),[cw_glDetail_rpt_temp].VOUDATE,120)" + strCustNAME2 + strDeptName2 + strSALE2 + strITEMNAME2; if (!string.IsNullOrEmpty(strCondition)) { sSQL += " and " + strCondition; } if (!string.IsNullOrEmpty(strSubjectAccount)) { if (strSubjectAccount.Trim() == "客户") { sSQL += " and [CORPACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "部门") { sSQL += " and [DEPTACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "人员") { sSQL += " and [EMPLACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } else if (strSubjectAccount.Trim() == "项目") { sSQL += " and [ITEMACC]='" + ds.Tables[0].Rows[i][SubjectAccountName].ToString() + "'"; } } //strSql.Append(" and CORPID='" + companyid + "'"); sSQL += "),0)"; } } } sSQL += " from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "' and EXPLAN='本年累计')"; #endregion } } #endregion } // //sSQL = "SELECT * from (" + sSQL + ") as f"; return sSQL; } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { RptCwGLDetailColumn data = new RptCwGLDetailColumn(); #region Set DB data to Object data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码 data.VKNO = (reader["VKNO"] == null ? "" : Convert.ToString(reader["VKNO"])); data.VOUNO = (reader["VOUNO"] == null ? "" : Convert.ToString(reader["VOUNO"])); data.VOUDATE = (reader["VOUDATE"] == null ? "" : Convert.ToString(reader["VOUDATE"])); data.DR = (reader["DR"] == null ? 0 : Convert.ToDecimal(reader["DR"]));//借方 data.CR = (reader["CR"] == null ? 0 : Convert.ToDecimal(reader["CR"]));//贷方 data.BLC = (reader["BLC"] == null ? 0 : Convert.ToDecimal(reader["BLC"]));//余额 data.EXPLAN = (reader["EXPLAN"] == null ? "" : Convert.ToString(reader["EXPLAN"])); data.DC = (reader["DC"] == null ? "" : Convert.ToString(reader["DC"])); data.ORDNO = (reader["ORDNO"] == null ? "" : Convert.ToString(reader["ORDNO"])); //data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码 #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region 币别_下拉框 static public List GetCodeCurrencyList() { String strSql = "select * from [code_currency]"; return SetCodeCurrencyData(strSql); } private static List SetCodeCurrencyData(String strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql)) { while (reader.Read()) { CodeCurrencyModel data = new CodeCurrencyModel(); #region Set DB data to Object data.CODENAME = Convert.ToString(reader["CODENAME"]); #endregion headList.Add(data); } reader.Close(); } // CodeCurrencyModel data2 = new CodeCurrencyModel(); data2.CODENAME = "综合本位币"; headList.Add(data2); return headList; } #endregion #region 核算项目_下拉框 static public List GetSubjectAccount(string strCondition, string ACCDATEbgn, string strUserID) { string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUserID); string sCondition = ""; if (!string.IsNullOrEmpty(strCondition)) { sCondition += " and " + strCondition; } string sYear = DateTime.Parse(ACCDATEbgn + "-01").Year.ToString(); sCondition += " and [YEAR]='" + sYear + "'"; // String strSql = "SELECT SubjectAccount from (" + "select SubjectAccount=(case when ISCORPACC=1 THEN '客户' ELSE '' END) from cw_accitems_gl where 1=1 and [STARTGID]='" + strCwSTARTGID + "'" + sCondition + " UNION " + "select SubjectAccount=(case when ISDEPTACC=1 THEN '部门' ELSE '' END) from cw_accitems_gl where 1=1 and [STARTGID]='" + strCwSTARTGID + "'" + sCondition + " UNION " + "select SubjectAccount=(case when ISEMPLACC=1 THEN '人员' ELSE '' END) from cw_accitems_gl where 1=1 and [STARTGID]='" + strCwSTARTGID + "'" + sCondition + " UNION " + "select SubjectAccount=(case when ISITEMACC=1 THEN '项目' ELSE '' END) from cw_accitems_gl where 1=1 and [STARTGID]='" + strCwSTARTGID + "'" + sCondition + ") as a" + " where SubjectAccount<>'' AND SubjectAccount is NOT NULL"; return SetSubjectAccountData(strSql); } private static List SetSubjectAccountData(String strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql)) { while (reader.Read()) { SubjectAccountModel data = new SubjectAccountModel(); #region Set DB data to Object data.NAME = Convert.ToString(reader["SubjectAccount"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region 判断是否显示核算项目 public static DBResult GetIsSubject(string strCondition, string ACCDATEbgn, string strUserID) { var result = new DBResult(); T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA(); string sCondition = ""; if (!string.IsNullOrEmpty(strCondition)) { sCondition += " and " + strCondition; } string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUserID); string sYear = DateTime.Parse(ACCDATEbgn + "-01").Year.ToString(); sCondition += " and [YEAR]='" + sYear + "'"; // string sSQL = "select COUNT(*) as inum from cw_accitems_gl where PACCGID in (select GID from cw_accitems_gl where 1=1 and [STARTGID]='" + strCwSTARTGID + "'" + sCondition + ")"; int inum = int.Parse(T_ALL_DA.GetStrSQL("inum", sSQL)); if (inum > 0) { result.Success = true; result.Message = "";//有子科目! return result; } else { String strSql = "select top 1 * from cw_accitems_gl where 1=1 and [STARTGID]='" + strCwSTARTGID + "'" + sCondition; DataSet ds = T_ALL_DA.GetAllSQL(strSql); if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { string sHSKM = ""; if(ds.Tables[0].Rows[0]["ISCORPACC"].ToString()=="True") { sHSKM += ",客户"; } if(ds.Tables[0].Rows[0]["ISDEPTACC"].ToString()=="True") { sHSKM += ",部门"; } if(ds.Tables[0].Rows[0]["ISEMPLACC"].ToString()=="True") { sHSKM += ",人员"; } if(ds.Tables[0].Rows[0]["ISITEMACC"].ToString()=="True") { sHSKM += ",项目"; } // if (sHSKM.Trim() == "") { result.Success = false; result.Message = "您所选的科目无法生成多栏账,请选择非明细科目或挂核算项目的科目!"; return result; } else { result.Success = true; result.Message = sHSKM.Substring(1); return result; } } else { result.Success = false; result.Message = "您所选的科目无法生成多栏账,请选择非明细科目或挂核算项目的科目!"; return result; } } else { result.Success = false; result.Message = "您所选的科目无法生成多栏账,请选择非明细科目或挂核算项目的科目!"; return result; } } } #endregion #region 子科目_循环列 static public List GetCwAccitemsGl(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string userid, string usercode, string companyid) { #region 基本变量 string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid); string strACCDATE = ACCDATEbgn.Replace(" and ACCDATE='", "").Replace("'", ""); string sYear = DateTime.Parse(strACCDATE + "-01").Year.ToString(); // string strCustNAME1 = strCustNAME; if (!string.IsNullOrEmpty(strCustNAME)) { strCustNAME1 = " and CUSTOMERNAME='" + strCustNAME + "'"; } string strDeptName1 = strDeptName; if (!string.IsNullOrEmpty(strDeptName)) { strDeptName1 = " and DEPTNAME='" + strDeptName + "'"; } string strSALE1 = strSALE; if (!string.IsNullOrEmpty(strSALE)) { strSALE1 = " and SALE='" + strSALE + "'"; } string strITEMNAME1 = strITEMNAME; if (!string.IsNullOrEmpty(strITEMNAME)) { strITEMNAME1 = " and ITEMNAME='" + strITEMNAME + "'"; } //分币别变量 string sBB = "QTY"; if (strCURRENCY.Trim() == "综合本位币") { sBB = "QTY"; } else if (strCURRENCY.Trim() == "RMB") { sBB = "AMT"; } else//外币 { sBB = "FCY"; } #endregion #region 查询语句 var strSql = new StringBuilder(); strSql.Append("select *,ACCID+'-'+ACCNAME AS ACCIDNAME from cw_accitems_gl where YEAR='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "' and ACCID in (select ACCID from (select ACCID,ACCNAME,(ACCYEAR+'-'+ACCMONTH) as ACCDATE,DC=(CASE WHEN DC='D' THEN '借' ELSE '贷' END)" + ",(case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END) as DR" + ",(case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END) as CR" //+ ",isnull(" + sBB + "DR,0) as DR" //+ ",isnull(" + sBB + "CR,0) as CR" //+ " from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 + " and ACCID LIKE '" + strACCID + "%') as a where (DR<>0 OR CR<>0)"); +" from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 + " and ACCID LIKE '" + strACCID + "%') as a where 1=1 "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(") order by ACCID"); #endregion return SetCwAccitemsGl(strSql); } private static List SetCwAccitemsGl(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CwAccitemsGlModel data = new CwAccitemsGlModel(); #region Set DB data to Object data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]).Trim());//唯一编码 data.ACCID = (reader["ACCID"] == null ? "" : Convert.ToString(reader["ACCID"]).Trim());//科目代码 data.ACCNAME = (reader["ACCNAME"] == null ? "" : Convert.ToString(reader["ACCNAME"]).Trim());//科目名称 data.ACCIDNAME = Convert.ToString(reader["ACCIDNAME"]); data.DETAILED = (reader["DETAILED"] == null ? false : Convert.ToBoolean(reader["DETAILED"]));//是否末级科目(标志) data.DC = (reader["DC"] == null ? "" : Convert.ToString(reader["DC"]).Trim());//借贷方向 data.ISFCY = (reader["ISFCY"] == null ? false : Convert.ToBoolean(reader["ISFCY"]));//是否核算外币 data.ISDEPTACC = (reader["ISDEPTACC"] == null ? false : Convert.ToBoolean(reader["ISDEPTACC"]));//是否核算部门 data.ISEMPLACC = (reader["ISEMPLACC"] == null ? false : Convert.ToBoolean(reader["ISEMPLACC"]));//是否核算人员 data.ISCORPACC = (reader["ISCORPACC"] == null ? false : Convert.ToBoolean(reader["ISCORPACC"]));//是否算客户门 data.ISITEMACC = (reader["ISITEMACC"] == null ? false : Convert.ToBoolean(reader["ISITEMACC"]));//是否核算项目 data.REMARKS = (reader["REMARKS"] == null ? "" : Convert.ToString(reader["REMARKS"]).Trim());//备注 data.YEAR = (reader["YEAR"] == null ? "" : Convert.ToString(reader["YEAR"]).Trim());//年 data.MONTH = (reader["MONTH"] == null ? "" : Convert.ToString(reader["MONTH"]).Trim());//月 data.PACCGID = (reader["PACCGID"] == null ? "" : Convert.ToString(reader["PACCGID"]).Trim());//父级科目代码 data.ACCATTRIBUTE = (reader["ACCATTRIBUTE"] == null ? "" : Convert.ToString(reader["ACCATTRIBUTE"]).Trim());//科目属性(资产、负债、共同、权益、成本、损益) data.ISENABLE = (reader["ISENABLE"] == null ? false : Convert.ToBoolean(reader["ISENABLE"]));//是否启用 data.ACCTYPE = (reader["ACCTYPE"] == null ? "" : Convert.ToString(reader["ACCTYPE"]).Trim()); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region 核算项目_循环列 static public List GetSubject(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string strSubjectAccount, string userid, string usercode, string companyid) { #region 基本变量 var headList = new List(); T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA(); string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid); string strACCDATE = ACCDATEbgn.Replace(" and ACCDATE='", "").Replace("'", ""); string sYear = DateTime.Parse(strACCDATE + "-01").Year.ToString(); // string strCustNAME1 = strCustNAME; if (!string.IsNullOrEmpty(strCustNAME)) { strCustNAME1 = " and CUSTOMERNAME='" + strCustNAME + "'"; } string strDeptName1 = strDeptName; if (!string.IsNullOrEmpty(strDeptName)) { strDeptName1 = " and DEPTNAME='" + strDeptName + "'"; } string strSALE1 = strSALE; if (!string.IsNullOrEmpty(strSALE)) { strSALE1 = " and SALE='" + strSALE + "'"; } string strITEMNAME1 = strITEMNAME; if (!string.IsNullOrEmpty(strITEMNAME)) { strITEMNAME1 = " and ITEMNAME='" + strITEMNAME + "'"; } // string SubjectAccountName = ""; if (!string.IsNullOrEmpty(strSubjectAccount)) { if (strSubjectAccount.Trim() == "客户") { strSubjectAccount = " and ISCORPACC=1"; SubjectAccountName = "CUSTOMERNAME"; } else if (strSubjectAccount.Trim() == "部门") { strSubjectAccount = " and ISDEPTACC=1"; SubjectAccountName = "DEPTNAME"; } else if (strSubjectAccount.Trim() == "人员") { strSubjectAccount = " and ISEMPLACC=1"; SubjectAccountName = "SALE"; } else if (strSubjectAccount.Trim() == "项目") { strSubjectAccount = " and ISITEMACC=1"; SubjectAccountName = "ITEMNAME"; } else { strSubjectAccount = ""; SubjectAccountName = ""; } } //分币别变量 string sBB = "QTY"; if (strCURRENCY.Trim() == "综合本位币") { sBB = "QTY"; } else if (strCURRENCY.Trim() == "RMB") { sBB = "AMT"; } else//外币 { sBB = "FCY"; } #endregion #region 查询语句 var strSql = new StringBuilder(); strSql.Append("select [SubjectAccount],DC from (select [" + SubjectAccountName + "] as [SubjectAccount],(ACCYEAR+'-'+ACCMONTH) as ACCDATE,DC=(SELECT top 1 DC FROM cw_accitems_gl WHERE ACCID='" + strACCID + "' and [YEAR]='" + sYear + "' and [STARTGID]='" + strCwSTARTGID + "')" + ",(case when isnull(AMTDR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYDR*FCYEXRATE,2)),0) else isnull(AMTDR,0) END) as DR" + ",(case when isnull(AMTCR,0)=0 THEN isnull(convert(numeric(8,2),round(FCYCR*FCYEXRATE,2)),0) else isnull(AMTCR,0) END) as CR" //+ ",isnull(" + sBB + "DR,0) as DR" //+ ",isnull(" + sBB + "CR,0) as CR" //+ " from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 + strSubjectAccount + " and ACCID='" + strACCID + "') as a where (DR<>0 OR CR<>0)"); +" from VW_cw_vouchers_vouitems_gl where STARTGID='" + strCwSTARTGID + "' and (ISDELETE=0 or ISDELETE is null) and ISCHECKED=1" + strCustNAME1 + strDeptName1 + strSALE1 + strITEMNAME1 + strSubjectAccount + " and ACCID='" + strACCID + "') as a where 1=1 "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } //strSql.Append(" and CORPID='" + companyid + "'"); strSql.Append(" group by [SubjectAccount],DC"); #endregion DataSet ds = T_ALL_DA.GetAllSQL(strSql.ToString()); if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { SubjectAccountModel data = new SubjectAccountModel(); #region Set DB data to Object data.NAME = Convert.ToString(ds.Tables[0].Rows[i]["SubjectAccount"]); data.DC = Convert.ToString(ds.Tables[0].Rows[i]["DC"]); #endregion headList.Add(data); } } } return headList; } #endregion } }