You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/DAL/RptCwGLDetail/RptCwGLDetailDAL.cs

711 lines
51 KiB
C#

3 years ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.RptCwGLDetail;
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.RptCwGLDetailDAL
{
public class RptCwGLDetailDAL
{
#region 查询
static public List<RptCwGLDetail> GetDataList(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string userid, string usercode, string companyid, string sort = null)
{
var strSql = new StringBuilder();
string sSql = GetDataListSQL(strCondition, ACCDATEbgn, ACCDATEend, strCURRENCY, strACCID, strCustNAME, strDeptName, strSALE, strITEMNAME, 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 VOUDATE,EXPLAN2,VKNO,convert(INT,VOUNO),ITEMNO");
}
return SetData(strSql);
}
static public List<RptCwGLDetail> GetDataList2(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, string userid, string usercode, string companyid, string sort = null)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid);
var strSql = new StringBuilder();
strSql.Append("SELECT * from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "'");
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by VOUDATE,EXPLAN2,VKNO,convert(INT,VOUNO),ITEMNO");
}
return SetData(strSql);
}
static public string GetDataListStr(string strCondition, string ACCDATEbgn, string ACCDATEend, string strCURRENCY, string strACCID, string strCustNAME, string strDeptName, string strSALE, string strITEMNAME, bool isPrint, string userid, string usercode, string companyid)
{
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid);
var strSql = new StringBuilder();
string sSql = "SELECT * from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "'";
if (isPrint)
{
sSql = "select VOUDATE,VOUDATE as 日期,VKNO,VKNO as 凭证字,VOUNO,VOUNO as 凭证号,EXPLAN,EXPLAN as 摘要,DR,DR as 借方,CR,CR as 贷方,DC,DC as 方向,BLC,BLC as 余额 from (" + sSql + ") as y ";
}
else
{
sSql = "select VOUDATE as 日期,VKNO as 凭证字,VOUNO as 凭证号,EXPLAN as 摘要,DR as 借方,CR as 贷方,DC as 方向,BLC as 余额 from (" + sSql + ") as y ";
}
strSql.Append(sSql);
strSql.Append(" order by VOUDATE,EXPLAN2,VKNO,convert(INT,VOUNO),ITEMNO");
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 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 + "'";
}
#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,[CORPID],isnull(sum(QTYLASTBLC),0) as [QTYLASTBLC],DC=(select top 1 DC from [cw_accitems_gl] where gid=f.LINKGID)");//,[CREATEUSER]
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 + "'", "");
// sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE='" + sYear + "-01'");
// strSql.Append(sls);
//}
//strSql.Append(")");
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 + "'", "");
// sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE='" + sYear + "-01'");
// strSql.Append(sls);
//}
//strSql.Append(")");
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,[CORPID]");//,[CREATEUSER]
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,[CORPID],isnull(sum(AMTLASTBLC),0) as [AMTLASTBLC],DC=(select top 1 DC from [cw_accitems_gl] where gid=f.LINKGID)");//,[CREATEUSER]
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 + "'", "");
// sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE='" + sYear + "-01'");
// strSql.Append(sls);
//}
//strSql.Append(")");
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 + "'", "");
// sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE='" + sYear + "-01'");
// strSql.Append(sls);
//}
//strSql.Append(")");
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,[CORPID]");//,[CREATEUSER]
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,[CORPID],isnull(sum(FCYLASTBLC),0) as [FCYLASTBLC],DC=(select top 1 DC from [cw_accitems_gl] where gid=f.LINKGID)");//,[CREATEUSER]
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 + "'", "");
// sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE='" + sYear + "-01'");
// strSql.Append(sls);
//}
//strSql.Append(")");
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 + "'", "");
// sls = sls.Replace("ACCDATE<='" + strACCDATE_end + "'", "ACCDATE='" + sYear + "-01'");
// strSql.Append(sls);
//}
//strSql.Append(")");
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,[CORPID]");//,[CREATEUSER]
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;//插入异常,事务已回滚成功
//违反了 PRIMARY KEY 约束 'PK_cw_glDetail_rpt_temp'。不能在对象 'dbo.cw_glDetail_rpt_temp' 中插入重复键。
if (execError.Message.IndexOf("不能在对象 'dbo.cw_glDetail_rpt_temp' 中插入重复键") > -1)
{
String sSql = "delete from [cw_glDetail_rpt_temp]";
bool bl = T_ALL_DA.GetExecuteSqlCommand(sSql);
}
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,convert(INT,VOUNO),ITEMNO";
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
#region 查询临时表
var strSql2 = new StringBuilder();
strSql2.Append("SELECT * from [cw_glDetail_rpt_temp] where STARTGID='" + strCwSTARTGID + "' and CREATEUSER='" + userid + "' and CORPID='" + companyid + "'");
#endregion
return strSql2.ToString();
}
private static List<RptCwGLDetail> SetData(StringBuilder strSql)
{
var headList = new List<RptCwGLDetail>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCwGLDetail data = new RptCwGLDetail();
#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"]));//分公司代码
data.STARTGID = (reader["STARTGID"] == null ? "" : Convert.ToString(reader["STARTGID"]));
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 币别_下拉框
static public List<CodeCurrencyModel> GetCodeCurrencyList()
{
String strSql = "select * from [code_currency]";
return SetCodeCurrencyData(strSql);
}
private static List<CodeCurrencyModel> SetCodeCurrencyData(String strSql)
{
var headList = new List<CodeCurrencyModel>();
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
}
}