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/RptCtBankReconciliation/RptCtBankReconciliationDAL.cs

313 lines
35 KiB
C#

2 years ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.RptCtBankReconciliation;
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.RptCtBankReconciliationDAL
{
public class RptCtBankReconciliationDAL
{
#region 查询主表
static public List<RptCtBankReconciliation> GetDataList(string sACCGID, string sFCYNO, string sVOUDATE, string userid, string usercode, string companyid, string sort = null)
{
string strCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(userid);
int iResult = 0;
#region 第一步 插入
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
#region 清空以前的数据
string sSQL = "delete from [ct_bank_reconciliation] where STARTGID='" + strCtSTARTGID + "' and CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "'";
iResult = -2;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 截止日期的余额-1
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT TOP 1 NEWID() as GID,'银行日记账余额:' as ITEM1,AMOUNT1=isnull((SELECT top 1 (CASE WHEN CURRENCY='RMB' THEN AMTLASTBLC ELSE FCYLASTBLC END) from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND ([YEAR]+'-'+[MONTH]+'-'+[DAY])<='" + sVOUDATE + "' AND CURRENCY='" + sFCYNO + "' AND LINKGID='" + sACCGID + "' ORDER BY [YEAR] DESC,[MONTH] DESC,[DAY] DESC),0),1 as LINENUM1,0 as ROWNUM1,'银行对账单余额:' as ITEM2"
+ ",AMOUNT2=isnull((SELECT TOP 1 ISNULL((case when DC='借' then BLC ELSE (BLC) END),0) as BLC from ct_bank_statement WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' ORDER BY VOUDATE DESC,ITEMNO DESC),ISNULL((select top 1 QTYYEARBLC_B FROM ct_initial_balance WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND CURRENCY='" + sFCYNO + "' AND ACCGID='" + sACCGID + "'),0))"//需求编号SR2017090700004原程序(case when DC='借' then (-BLC) ELSE BLC END)
+ ",1 as LINENUM2,0 as ROWNUM1,'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME";
iResult = -3;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 加-2
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT TOP 1 NEWID() as GID,'加:银行已收企业未收' as ITEM1"
+ ",AMOUNT1=ISNULL((SELECT ISNULL(SUM(CR),0) as CR from ct_bank_statement where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')),0)"
+ ",2 as LINENUM1,0 as ROWNUM1,'加:企业已收银行未收' as ITEM2"
+ ",AMOUNT2=ISNULL((SELECT ISNULL(SUM(DR),0) as DR from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')),0)"
+ ",2 as LINENUM2,0 as ROWNUM2,'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME";
iResult = -4;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 加对账单明细-3
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT NEWID() as GID"
+ ",ITEM1=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_statement where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23))"
+ ",ISNULL(CR,0) as AMOUNT1,3 as LINENUM1,ROWNUM1=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_statement where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'' as ITEM2,0 as AMOUNT2,3 as LINENUM2,10000 as ROWNUM2,'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_statement as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
iResult = -5;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 减-4
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT TOP 1 NEWID() as GID,'减:银行已付企业未付' as ITEM1"
+ ",AMOUNT1=ISNULL((SELECT ISNULL(SUM(DR),0) as DR from ct_bank_statement where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')),0)"
+ ",4 as LINENUM1,0 as ROWNUM1,'减:企业已付银行未付' as ITEM2"
+ ",AMOUNT2=ISNULL((SELECT ISNULL(SUM(CR),0) as CR from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')),0)"
+ ",4 as LINENUM2,0 as ROWNUM2,'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME";
iResult = -6;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
#region 减对账单明细-5
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT NEWID() as GID"
+ ",ITEM1=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_statement where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23))"
+ ",ISNULL(DR,0) as AMOUNT1,5 as LINENUM1,ROWNUM1=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_statement where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'' as ITEM2,0 as AMOUNT2,5 as LINENUM2,10000 as ROWNUM2,'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_statement as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + " 23:59:59.000' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行对账单' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
iResult = -7;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
iResult = -1;
}
}
#endregion
#region 第二步 插入
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
#region 加日记账明细-3
string sSQL = "select * from ct_bank_reconciliation where STARTGID='" + strCtSTARTGID + "' and CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "' and LINENUM1=3 order by ROWNUM1";
DataSet dsCtBankReconciliation = SqlHelper.ExecuteQueryDataset(sqlTran, CommandType.Text, sSQL, null);
if (dsCtBankReconciliation != null)
{
if (dsCtBankReconciliation.Tables[0].Rows.Count > 0)
{
sSQL = "SELECT NEWID() as GID,'' as ITEM1,0 as AMOUNT1,3 as LINENUM1,10000 as ROWNUM1,ITEM2=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23)+' '+(case when vkno<>'' then VKNO+'-'+VOUNO ELSE '(无凭证)' END)),AMOUNT2=ISNULL(DR,0),3 as LINENUM2,ROWNUM2=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_journal as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
DataSet dsCtBankJournal = SqlHelper.ExecuteQueryDataset(sqlTran, CommandType.Text, sSQL, null);
if (dsCtBankJournal != null)
{
if (dsCtBankReconciliation.Tables[0].Rows.Count >= dsCtBankJournal.Tables[0].Rows.Count)
{
for (int i = 0; i < dsCtBankJournal.Tables[0].Rows.Count; i++)
{
sSQL = "update [ct_bank_reconciliation] set [ITEM2]='" + dsCtBankJournal.Tables[0].Rows[i]["ITEM2"].ToString() + "',[AMOUNT2]=" + dsCtBankJournal.Tables[0].Rows[i]["AMOUNT2"].ToString() + " where STARTGID='" + strCtSTARTGID + "' AND gid='" + dsCtBankReconciliation.Tables[0].Rows[i]["GID"].ToString() + "'";
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
}
else
{
for (int i = 0; i < dsCtBankReconciliation.Tables[0].Rows.Count; i++)
{
sSQL = "update [ct_bank_reconciliation] set [ITEM2]='" + dsCtBankJournal.Tables[0].Rows[i]["ITEM2"].ToString() + "',[AMOUNT2]=" + dsCtBankJournal.Tables[0].Rows[i]["AMOUNT2"].ToString() + " where STARTGID='" + strCtSTARTGID + "' AND gid='" + dsCtBankReconciliation.Tables[0].Rows[i]["GID"].ToString() + "'";
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
//
for (int i = dsCtBankReconciliation.Tables[0].Rows.Count; i < dsCtBankJournal.Tables[0].Rows.Count; i++)
{
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT GID='" + dsCtBankJournal.Tables[0].Rows[i]["GID"].ToString() + "','' as ITEM1,0 as AMOUNT1,3 as LINENUM1,10000 as ROWNUM1,ITEM2='" + dsCtBankJournal.Tables[0].Rows[i]["ITEM2"].ToString() + "',AMOUNT2=isnull(" + dsCtBankJournal.Tables[0].Rows[i]["AMOUNT2"].ToString() + ",0),3 as LINENUM2,ROWNUM2='" + dsCtBankJournal.Tables[0].Rows[i]["ROWNUM2"].ToString() + "','" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME";
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
}
}
}
else
{
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT NEWID() as GID,'' as ITEM1,0 as AMOUNT1,3 as LINENUM1,10000 as ROWNUM1,ITEM2=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23)+' '+(case when vkno<>'' then VKNO+'-'+VOUNO ELSE '(无凭证)' END)),AMOUNT2=ISNULL(DR,0),3 as LINENUM2,ROWNUM2=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_journal as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
}
else
{
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT NEWID() as GID,'' as ITEM1,0 as AMOUNT1,3 as LINENUM1,10000 as ROWNUM1,ITEM2=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23)+' '+(case when vkno<>'' then VKNO+'-'+VOUNO ELSE '(无凭证)' END)),AMOUNT2=ISNULL(DR,0),3 as LINENUM2,ROWNUM2=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_journal as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and DR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
iResult = -5;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
#endregion
#region 减日记账明细-5
sSQL = "select * from ct_bank_reconciliation where STARTGID='" + strCtSTARTGID + "' and CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "' and LINENUM1=5 order by ROWNUM1";
dsCtBankReconciliation = SqlHelper.ExecuteQueryDataset(sqlTran, CommandType.Text, sSQL, null);
if (dsCtBankReconciliation != null)
{
if (dsCtBankReconciliation.Tables[0].Rows.Count > 0)
{
sSQL = "SELECT NEWID() as GID,'' as ITEM1,0 as AMOUNT1,5 as LINENUM1,10000 as ROWNUM1,ITEM2=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23)+' '+(case when vkno<>'' then VKNO+'-'+VOUNO ELSE '(无凭证)' END)),AMOUNT2=ISNULL(CR,0),5 as LINENUM2,ROWNUM2=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_journal as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
DataSet dsCtBankJournal = SqlHelper.ExecuteQueryDataset(sqlTran, CommandType.Text, sSQL, null);
if (dsCtBankJournal != null)
{
if (dsCtBankReconciliation.Tables[0].Rows.Count >= dsCtBankJournal.Tables[0].Rows.Count)
{
for (int i = 0; i < dsCtBankJournal.Tables[0].Rows.Count; i++)
{
sSQL = "update [ct_bank_reconciliation] set [ITEM2]='" + dsCtBankJournal.Tables[0].Rows[i]["ITEM2"].ToString() + "',[AMOUNT2]=" + dsCtBankJournal.Tables[0].Rows[i]["AMOUNT2"].ToString() + " WHERE STARTGID='" + strCtSTARTGID + "' AND gid='" + dsCtBankReconciliation.Tables[0].Rows[i]["GID"].ToString() + "'";
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
}
else
{
for (int i = 0; i < dsCtBankReconciliation.Tables[0].Rows.Count; i++)
{
sSQL = "update [ct_bank_reconciliation] set [ITEM2]='" + dsCtBankJournal.Tables[0].Rows[i]["ITEM2"].ToString() + "',[AMOUNT2]=" + dsCtBankJournal.Tables[0].Rows[i]["AMOUNT2"].ToString() + " WHERE STARTGID='" + strCtSTARTGID + "' AND gid='" + dsCtBankReconciliation.Tables[0].Rows[i]["GID"].ToString() + "'";
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
//
for (int i = dsCtBankReconciliation.Tables[0].Rows.Count; i < dsCtBankJournal.Tables[0].Rows.Count; i++)
{
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT GID='" + dsCtBankJournal.Tables[0].Rows[i]["GID"].ToString() + "','' as ITEM1,0 as AMOUNT1,5 as LINENUM1,10000 as ROWNUM1,ITEM2='" + dsCtBankJournal.Tables[0].Rows[i]["ITEM2"].ToString() + "',AMOUNT2=ISNULL(" + dsCtBankJournal.Tables[0].Rows[i]["AMOUNT2"].ToString() + ",0),5 as LINENUM2,ROWNUM2='" + dsCtBankJournal.Tables[0].Rows[i]["ROWNUM2"].ToString() + "','" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME";
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
}
}
}
else
{
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT NEWID() as GID,'' as ITEM1,0 as AMOUNT1,5 as LINENUM1,10000 as ROWNUM1,ITEM2=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23)+' '+(case when vkno<>'' then VKNO+'-'+VOUNO ELSE '(无凭证)' END)),AMOUNT2=ISNULL(CR,0),5 as LINENUM2,ROWNUM2=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_journal as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
}
else
{
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME])"
+ " SELECT NEWID() as GID,'' as ITEM1,0 as AMOUNT1,5 as LINENUM1,10000 as ROWNUM1,ITEM2=(CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23)+'. '+CONVERT(varchar(100), VOUDATE, 23)+' '+(case when vkno<>'' then VKNO+'-'+VOUNO ELSE '(无凭证)' END)),AMOUNT2=ISNULL(CR,0),5 as LINENUM2,ROWNUM2=CONVERT(varchar(100),(1+isnull((select count(*) from (select * from ct_bank_journal where STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')) as b where VOUDATE<a.VOUDATE or (VOUDATE=a.VOUDATE and ITEMNO<a.ITEMNO)),0)), 23),'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME FROM ct_bank_journal as a"
+ " where a.STARTGID='" + strCtSTARTGID + "' and VOUDATE<='" + sVOUDATE + "' AND FCYNO='" + sFCYNO + "' AND ACCGID='" + sACCGID + "' and CR<>0 and gid not in (select LINKGID from ct_bank_checking where LINKTYPE='银行日记账' and VOUDATE<='" + sVOUDATE + "' and STARTGID='" + strCtSTARTGID + "')"
+ " ORDER BY VOUDATE,ITEMNO";
iResult = -7;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
#endregion
#region 调节后余额-6
sSQL = "INSERT INTO [ct_bank_reconciliation]([GID],[ITEM1],[AMOUNT1],[LINENUM1],[ROWNUM1],[ITEM2],[AMOUNT2],[LINENUM2],[ROWNUM2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT TOP 1 NEWID() as GID,'调节后余额(单位)' as ITEM1"
+ ",AMOUNT1=ISNULL((SELECT ISNULL(SUM(case when LINENUM1=4 then (-AMOUNT1) ELSE AMOUNT1 END),0) as AMOUNT1 from ct_bank_reconciliation where (LINENUM1=1 or LINENUM1=2 or LINENUM1=4) and STARTGID='" + strCtSTARTGID + "' and CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "'),0)"
+ ",6 as LINENUM1,0 as ROWNUM1,'调节后余额(银行)' as ITEM2"
+ ",AMOUNT2=ISNULL((SELECT ISNULL(SUM(case when LINENUM2=4 then (-AMOUNT2) ELSE AMOUNT2 END),0) as AMOUNT2 from ct_bank_reconciliation where (LINENUM2=1 or LINENUM2=2 or LINENUM2=4) and STARTGID='" + strCtSTARTGID + "' and CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "'),0)"
+ ",6 as LINENUM2,0 as ROWNUM2,'" + strCtSTARTGID + "' as STARTGID,'" + companyid + "' as CORPID,'" + userid + "' as MODIFIEDUSER,GETDATE() as MODIFIEDTIME"
+ " from ct_bank_reconciliation WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "'";
iResult = -8;
#endregion
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//事务提交
iResult = 1;
sqlTran.Commit();
}
catch (Exception)
{
sqlTran.Rollback();
iResult = -1;
}
}
#endregion
#region 查询统计数据
var strSql = new StringBuilder();
strSql.Append("select * from ct_bank_reconciliation WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND CORPID='" + companyid + "' and MODIFIEDUSER='" + userid + "'");
strSql.Append(" order by LINENUM1,ROWNUM1,ROWNUM2");
#endregion
return SetData(strSql);
}
private static List<RptCtBankReconciliation> SetData(StringBuilder strSql)
{
var headList = new List<RptCtBankReconciliation>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCtBankReconciliation data = new RptCtBankReconciliation();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//惟一值
data.ITEM1 = (reader["ITEM1"] == null ? "" : Convert.ToString(reader["ITEM1"]));//日记账项目
data.AMOUNT1 = (reader["AMOUNT1"] == null ? 0 : Convert.ToDecimal(reader["AMOUNT1"]));//日记账金额
data.LINENUM1 = (reader["LINENUM1"] == null ? 0 : Convert.ToInt32(reader["LINENUM1"]));//行号1
data.ROWNUM1 = (reader["ROWNUM1"] == null ? 0 : Convert.ToInt32(reader["ROWNUM1"]));//凭证行号1
data.ITEM2 = (reader["ITEM2"] == null ? "" : Convert.ToString(reader["ITEM2"]));//对账单项目
data.AMOUNT2 = (reader["AMOUNT2"] == null ? 0 : Convert.ToDecimal(reader["AMOUNT2"]));//对账単金额
data.LINENUM2 = (reader["LINENUM2"] == null ? 0 : Convert.ToInt32(reader["LINENUM2"]));//行号2
data.ROWNUM2 = (reader["ROWNUM2"] == null ? 0 : Convert.ToInt32(reader["ROWNUM2"]));//凭证行号2
data.STARTGID = (reader["STARTGID"] == null ? "" : Convert.ToString(reader["STARTGID"]));//账套启用GID
data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid
if (reader["MODIFIEDTIME"] != null && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("0001") < 0 && reader["MODIFIEDTIME"].ToString().Trim().IndexOf("1900") < 0 && reader["MODIFIEDTIME"].ToString().Trim() != "")
{
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//最后一次更改操作时间
}
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 打印
static public string GetDataListStr(string sACCGID, string sFCYNO, string sVOUDATE, string sTYPE, string userid, string usercode, string companyid)
{
string strCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(userid);
var strSql = new StringBuilder();
strSql.Append("select [ITEM1] as 项目1,[AMOUNT1] as 金额1,[ITEM2] as 项目2,[AMOUNT2] as 金额2 from ct_bank_reconciliation WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND MODIFIEDUSER='" + userid + "'");// and CORPID='" + companyid + "'
if (sTYPE != "export")
{
strSql.Append(" order by LINENUM1,ROWNUM1,ROWNUM2");
}
return strSql.ToString();
}
#endregion
}
}