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.

271 lines
19 KiB
C#

10 months ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.DAL.MsCtInitialBalanceDAL;
using DSWeb.MvcShipping.Models.RptCtGenlegAccitems;
using DSWeb.MvcShipping.Models.MsCwAccitemsGl;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.EntityDA;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using System.Data.SqlClient;
using DSWeb.DataAccess;
using HcUtility.Core;
using DSWeb.Areas.CommMng.DAL;
namespace DSWeb.MvcShipping.DAL.RptCtGenlegAccitemsDAL
{
public class RptCtGenlegAccitemsDAL
{
#region 查询
static public List<RptCtGenlegAccitems> GetDataList(string sVOUDATE, string sACCID, string sCURRENCY, string userid, string usercode, string companyid, string sort = null)
{
var strSql = new StringBuilder();
string sSql = GetDataListSQL(sVOUDATE, sACCID, sCURRENCY, userid, usercode, companyid);
strSql.Append(sSql);
//
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by LINKCODE");
}
return SetData(strSql);
}
private static List<RptCtGenlegAccitems> SetData(StringBuilder strSql)
{
var headList = new List<RptCtGenlegAccitems>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
RptCtGenlegAccitems data = new RptCtGenlegAccitems();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.YEAR = (reader["YEAR"] == null ? "" : Convert.ToString(reader["YEAR"]));//年
data.MONTH = (reader["MONTH"] == null ? "" : Convert.ToString(reader["MONTH"]));//月
data.DAY = (reader["DAY"] == null ? "" : Convert.ToString(reader["DAY"]));//日
data.LINKGID = (reader["LINKGID"] == null ? "" : Convert.ToString(reader["LINKGID"]));//科目GID(cw_accitems)
data.CURRENCY = (reader["CURRENCY"] == null ? "" : Convert.ToString(reader["CURRENCY"]));//币别
data.AMTYEARDR = (reader["AMTYEARDR"] == null ? 0 : Convert.ToDecimal(reader["AMTYEARDR"]));//RMB年初借方
data.AMTYEARCR = (reader["AMTYEARCR"] == null ? 0 : Convert.ToDecimal(reader["AMTYEARCR"]));//RMB年初贷方
data.AMTYEARBLC = (reader["AMTYEARBLC"] == null ? 0 : Convert.ToDecimal(reader["AMTYEARBLC"]));//RMB年初余额
data.AMTDR = (reader["AMTDR"] == null ? 0 : Convert.ToDecimal(reader["AMTDR"]));//RMB本月借方
data.AMTCR = (reader["AMTCR"] == null ? 0 : Convert.ToDecimal(reader["AMTCR"]));//RMB本月贷方
data.AMTBLC = (reader["AMTBLC"] == null ? 0 : Convert.ToDecimal(reader["AMTBLC"]));//RMB本月余额
data.AMTLASTDR = (reader["AMTLASTDR"] == null ? 0 : Convert.ToDecimal(reader["AMTLASTDR"]));//RMB累计借方
data.AMTLASTCR = (reader["AMTLASTCR"] == null ? 0 : Convert.ToDecimal(reader["AMTLASTCR"]));//RMB累计贷方
data.AMTLASTBLC = (reader["AMTLASTBLC"] == null ? 0 : Convert.ToDecimal(reader["AMTLASTBLC"]));//RMB累计余额
data.FCYYEARDR = (reader["FCYYEARDR"] == null ? 0 : Convert.ToDecimal(reader["FCYYEARDR"]));//USD年初借方
data.FCYYEARCR = (reader["FCYYEARCR"] == null ? 0 : Convert.ToDecimal(reader["FCYYEARCR"]));//USD年初贷方
data.FCYYEARBLC = (reader["FCYYEARBLC"] == null ? 0 : Convert.ToDecimal(reader["FCYYEARBLC"]));//USD年初余额
data.FCYDR = (reader["FCYDR"] == null ? 0 : Convert.ToDecimal(reader["FCYDR"]));//USD本月借方
data.FCYCR = (reader["FCYCR"] == null ? 0 : Convert.ToDecimal(reader["FCYCR"]));//USD本月贷方
data.FCYBLC = (reader["FCYBLC"] == null ? 0 : Convert.ToDecimal(reader["FCYBLC"]));//USD本月余额
data.FCYLASTDR = (reader["FCYLASTDR"] == null ? 0 : Convert.ToDecimal(reader["FCYLASTDR"]));//外币累计借方
data.FCYLASTCR = (reader["FCYLASTCR"] == null ? 0 : Convert.ToDecimal(reader["FCYLASTCR"]));//外币累计贷方
data.FCYLASTBLC = (reader["FCYLASTBLC"] == null ? 0 : Convert.ToDecimal(reader["FCYLASTBLC"]));//外币累计余额
data.QTYYEARDR = (reader["QTYYEARDR"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARDR"]));//年初借方
data.QTYYEARCR = (reader["QTYYEARCR"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARCR"]));//年初贷方
data.QTYYEARBLC = (reader["QTYYEARBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYYEARBLC"]));//年初余额
data.QTYDR = (reader["QTYDR"] == null ? 0 : Convert.ToDecimal(reader["QTYDR"]));//本月借方
data.QTYCR = (reader["QTYCR"] == null ? 0 : Convert.ToDecimal(reader["QTYCR"]));//本月贷方
data.QTYBLC = (reader["QTYBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYBLC"]));//本月余额
data.QTYLASTDR = (reader["QTYLASTDR"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTDR"]));//累计借方
data.QTYLASTCR = (reader["QTYLASTCR"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTCR"]));//累计贷方
data.QTYLASTBLC = (reader["QTYLASTBLC"] == null ? 0 : Convert.ToDecimal(reader["QTYLASTBLC"]));//累计余额
data.IsInitialEntry = (reader["IsInitialEntry"] == null ? false : Convert.ToBoolean(reader["IsInitialEntry"]));//是否期初录入
data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码
data.CREATEUSER = (reader["CREATEUSER"] == null ? "" : Convert.ToString(reader["CREATEUSER"]));//创建人gid
data.CREATETIME = (reader["CREATETIME"] == null ? DateTime.Now : Convert.ToDateTime(reader["CREATETIME"]));//创建时间
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//最后一次更新操作人GID
data.MODIFIEDTIME = (reader["MODIFIEDTIME"] == null ? DateTime.Now : Convert.ToDateTime(reader["MODIFIEDTIME"]));//最后一次更改时间
data.ACCDATE = (reader["ACCDATE"] == null ? "" : Convert.ToString(reader["ACCDATE"]));//会计期间
data.STARTGID = (reader["STARTGID"] == null ? "" : Convert.ToString(reader["STARTGID"]));//账套启用GID
data.LINKNAME = (reader["LINKNAME"] == null ? "" : Convert.ToString(reader["LINKNAME"]));//科目名称
data.LINKCODE = (reader["LINKCODE"] == null ? "" : Convert.ToString(reader["LINKCODE"]));
data.DC = (reader["DC"] == null ? "" : Convert.ToString(reader["DC"]));//余额方向
data.DC2 = (reader["DC2"] == null ? "" : Convert.ToString(reader["DC2"]));//余额方向
data.iDR = (reader["iDR"] == null ? "" : Convert.ToString(reader["iDR"]));//余额方向
data.iCR = (reader["iCR"] == null ? "" : Convert.ToString(reader["iCR"]));//余额方向
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
static public string GetDataListStr(string sVOUDATE, string sACCID, string sCURRENCY, string sTYPE, string userid, string usercode, string companyid)
{
var strSql = new StringBuilder();
string sSql = GetDataListSQL(sVOUDATE, sACCID, sCURRENCY, userid, usercode, companyid);
if (sCURRENCY == "RMB")
{
strSql.Append("select LINKCODE as 科目代码,LINKNAME as 科目名称,DC as 方向,QTYYEARBLC as 昨日余额,QTYDR as 今日借方,QTYCR as 今日贷方,DC2 as 方向,QTYLASTBLC as 今日余额,iDR as 借方笔数,iCR as 贷方笔数 from (" + sSql + ") as ls");
}
else
{
strSql.Append("select LINKCODE as [科目代码],LINKNAME as [科目名称],DC as 方向,FCYYEARBLC as [昨日余额(原币)],QTYYEARBLC as [昨日余额(本位币)],FCYDR as [今日借方(原币)],QTYDR as [今日借方(本位币)],FCYCR as [今日贷方(原币)],QTYCR as [今日贷方(本位币)],DC2 as [方向],FCYLASTBLC as [今日余额(原币)],QTYLASTBLC as [今日余额(本位币)],iDR as [借方笔数],iCR as [贷方笔数] from (" + sSql + ") as ls");
}
if (sTYPE != "export")
{
strSql.Append(" order by LINKCODE");
}
return strSql.ToString();
}
static public string GetDataListSQL(string sVOUDATE, string sACCID, string sCURRENCY, string userid, string usercode, string companyid)
{
#region 基本信息
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(userid);
string strCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(userid);
String strCondition = "";
String strCondition2 = "";
String strCondition3 = "";
if (!string.IsNullOrEmpty(sVOUDATE))
{
strCondition += " and ([YEAR]+'-'+[MONTH]+'-'+[DAY])='" + sVOUDATE + "'";
strCondition2 += " and VOUDATE='" + sVOUDATE + "'";
}
if (!string.IsNullOrEmpty(sACCID))
{
strCondition += " and LINKGID in (select GID from cw_accitems_gl where [YEAR]=SUBSTRING('" + sVOUDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "' and ACCID='" + sACCID + "')";
strCondition3 += " and ACCID='" + sACCID + "'";
}
if (!string.IsNullOrEmpty(sCURRENCY))
{
strCondition += " and CURRENCY='" + sCURRENCY + "'";
strCondition2 += " and FCYNO='" + sCURRENCY + "'";
}
#endregion
#region 判断ct_genleg_accitems是否有数据
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string sSQL = "select * from cw_accitems_gl where ISBANK=1 and DETAILED=1 and [YEAR]=SUBSTRING('" + sVOUDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "' and gid in (select LINKGID from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' GROUP BY LINKGID) and gid not in (select LINKGID from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "'" + strCondition + ")" + strCondition3;
DataSet dsCwAccitemsGl = T_ALL_DA.GetAllSQL(sSQL);
//
if (dsCwAccitemsGl != null)
{
if (dsCwAccitemsGl.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsCwAccitemsGl.Tables[0].Rows.Count; i++)
{
insertRow(dsCwAccitemsGl.Tables[0].Rows[i]["GID"].ToString(), dsCwAccitemsGl.Tables[0].Rows[i]["DC"].ToString(), sCURRENCY, sVOUDATE, strCtSTARTGID, companyid, userid);
#region 今日父类总账表
MsCtInitialBalanceDAL.MsCtInitialBalanceDAL.SaveRow(dsCwAccitemsGl.Tables[0].Rows[i]["GID"].ToString(), sCURRENCY, sVOUDATE, strCtSTARTGID, false, companyid, userid);
#endregion
}
}
}
#endregion
#region 查询数据
var strSql = new StringBuilder();
strSql.Append("select [GID],[YEAR],[MONTH],[DAY],[LINKGID],[CURRENCY],[AMTYEARDR],[AMTYEARCR],[AMTYEARBLC],[AMTDR],[AMTCR],[AMTBLC],[AMTLASTDR],[AMTLASTCR]");
strSql.Append(",[AMTLASTBLC]=(case when AMTLASTBLC<0 then (0-AMTLASTBLC) else AMTLASTBLC end)");
strSql.Append(",[FCYYEARDR],[FCYYEARCR]");
strSql.Append(",[FCYYEARBLC]=(case when FCYYEARBLC<0 then (0-FCYYEARBLC) else FCYYEARBLC end)");
strSql.Append(",[FCYDR],[FCYCR],[FCYBLC],[FCYLASTDR],[FCYLASTCR]");
strSql.Append(",[FCYLASTBLC]=(case when FCYLASTBLC<0 then (0-FCYLASTBLC) else FCYLASTBLC end)");
strSql.Append(",[QTYYEARDR],[QTYYEARCR]");
strSql.Append(",[QTYYEARBLC]=(case when QTYYEARBLC<0 then (0-QTYYEARBLC) else QTYYEARBLC end)");
strSql.Append(",[QTYDR],[QTYCR],[QTYBLC],[QTYLASTDR],[QTYLASTCR]");
strSql.Append(",[QTYLASTBLC]=(case when QTYLASTBLC<0 then (0-QTYLASTBLC) else QTYLASTBLC end)");
strSql.Append(",[IsInitialEntry],[CORPID],[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],[ACCDATE],[STARTGID],LINKCODE=(select top 1 ACCID from [cw_accitems_gl] WITH(NOLOCK) WHERE STARTGID='" + strCwSTARTGID + "' AND gid=ct_genleg_accitems.LINKGID),LINKNAME=(select top 1 ACCNAME from [cw_accitems_gl] WITH(NOLOCK) WHERE STARTGID='" + strCwSTARTGID + "' AND gid=ct_genleg_accitems.LINKGID),DC=(CASE WHEN QTYYEARBLC<0 THEN '贷' ELSE '借' END),DC2=(CASE WHEN QTYLASTBLC<0 THEN '贷' ELSE '借' END),iDR=(SELECT COUNT(*) AS inum from ct_bank_journal WHERE STARTGID='" + strCtSTARTGID + "' and DR<>0 AND ACCGID=ct_genleg_accitems.LINKGID" + strCondition2 + "),iCR=(SELECT COUNT(*) AS inum from ct_bank_journal WHERE STARTGID='" + strCtSTARTGID + "' and CR<>0 AND ACCGID=ct_genleg_accitems.LINKGID" + strCondition2 + ") from ct_genleg_accitems where STARTGID='" + strCtSTARTGID + "'" + strCondition);
//strSql.Append(" and CORPID='" + companyid + "'");
#endregion
return strSql.ToString();
}
#region 插入ct_genleg_accitems表
public static void insertRow(string strACCGID, string sACCDC, string sCURRENCY, string sVOUDATE, string strCtSTARTGID, string strCOMPANYID, string strUSERID)
{
#region 基本信息
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string sYear = DateTime.Parse(sVOUDATE).Year.ToString();
string sMonth = DateTime.Parse(sVOUDATE).Month.ToString();
if (sMonth.Trim().Length == 1)
{
sMonth = "0" + sMonth;
}
string sDay = DateTime.Parse(sVOUDATE).Day.ToString();
if (sDay.Trim().Length == 1)
{
sDay = "0" + sDay;
}
#endregion
#region 插入当日总账表
string sSQL = "select top 1 * from ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND LINKGID='" + strACCGID + "' and CURRENCY='" + sCURRENCY + "' and ([Year]+'-'+[Month]+'-'+[Day])<'" + sVOUDATE + "' order by [YEAR] desc,[MONTH] desc,[DAY] desc";
DataSet dsLS = T_ALL_DA.GetAllSQL(sSQL);
if (dsLS != null)
{
if (dsLS.Tables[0].Rows.Count > 0)
{
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
#region 插入当日总账表
sSQL = "INSERT INTO [ct_genleg_accitems]([GID],[YEAR],[MONTH],[DAY],[LINKGID],[CURRENCY],[AMTYEARDR],[AMTYEARCR],[AMTYEARBLC],[AMTDR],[AMTCR],[AMTBLC],[AMTLASTDR],[AMTLASTCR],[AMTLASTBLC],[FCYYEARDR],[FCYYEARCR],[FCYYEARBLC],[FCYDR],[FCYCR],[FCYBLC],[FCYLASTDR],[FCYLASTCR],[FCYLASTBLC],[QTYYEARDR],[QTYYEARCR],[QTYYEARBLC],[QTYDR],[QTYCR],[QTYBLC],[QTYLASTDR],[QTYLASTCR],[QTYLASTBLC],[IsInitialEntry],[CORPID],[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],[ACCDATE],[STARTGID])"
+ " select NEWID() as GID,'" + sYear + "' as [YEAR],'" + sMonth + "' as [MONTH],'" + sDay + "' as [DAY],'" + strACCGID + "' as [LINKGID],'" + sCURRENCY + "' as [CURRENCY]"
+ ",AMTYEARDR=" + (sACCDC == "借" ? dsLS.Tables[0].Rows[0]["AMTLASTBLC"].ToString() : "0")
+ ",AMTYEARCR=" + (sACCDC == "贷" ? dsLS.Tables[0].Rows[0]["AMTLASTBLC"].ToString() : "0")
+ ",AMTYEARBLC=" + dsLS.Tables[0].Rows[0]["AMTLASTBLC"].ToString()
+ ",AMTDR=0,AMTCR=0,AMTBLC=0"
+ ",AMTLASTDR=" + (sACCDC == "借" ? dsLS.Tables[0].Rows[0]["AMTLASTBLC"].ToString() : "0")
+ ",AMTLASTCR=" + (sACCDC == "贷" ? dsLS.Tables[0].Rows[0]["AMTLASTBLC"].ToString() : "0")
+ ",AMTLASTBLC=" + dsLS.Tables[0].Rows[0]["AMTLASTBLC"].ToString()
+ ",FCYYEARDR=" + (sACCDC == "借" ? dsLS.Tables[0].Rows[0]["FCYLASTBLC"].ToString() : "0")
+ ",FCYYEARCR=" + (sACCDC == "贷" ? dsLS.Tables[0].Rows[0]["FCYLASTBLC"].ToString() : "0")
+ ",FCYYEARBLC=" + dsLS.Tables[0].Rows[0]["FCYLASTBLC"].ToString()
+ ",FCYDR=0,FCYCR=0,FCYBLC=0"
+ ",FCYLASTDR=" + (sACCDC == "借" ? dsLS.Tables[0].Rows[0]["FCYLASTBLC"].ToString() : "0")
+ ",FCYLASTCR=" + (sACCDC == "贷" ? dsLS.Tables[0].Rows[0]["FCYLASTBLC"].ToString() : "0")
+ ",FCYLASTBLC=" + dsLS.Tables[0].Rows[0]["FCYLASTBLC"].ToString()
+ ",QTYYEARDR=" + (sACCDC == "借" ? dsLS.Tables[0].Rows[0]["QTYLASTBLC"].ToString() : "0")
+ ",QTYYEARCR=" + (sACCDC == "贷" ? dsLS.Tables[0].Rows[0]["QTYLASTBLC"].ToString() : "0")
+ ",QTYYEARBLC=" + dsLS.Tables[0].Rows[0]["QTYLASTBLC"].ToString()
+ ",QTYDR=0,QTYCR=0,QTYBLC=0"
+ ",QTYLASTDR=" + (sACCDC == "借" ? dsLS.Tables[0].Rows[0]["QTYLASTBLC"].ToString() : "0")
+ ",QTYLASTCR=" + (sACCDC == "贷" ? dsLS.Tables[0].Rows[0]["QTYLASTBLC"].ToString() : "0")
+ ",QTYLASTBLC=" + dsLS.Tables[0].Rows[0]["QTYLASTBLC"].ToString()
+ ",0 as IsInitialEntry,'" + strCOMPANYID + "' as CORPID,'" + strUSERID + "' as CREATEUSER,getdate() as CREATETIME,'" + strUSERID + "' as MODIFIEDUSER,getdate() as MODIFIEDTIME,'" + sYear + "-" + sMonth + "' as ACCDATE,'" + strCtSTARTGID + "' as STARTGID"
+ " FROM ct_genleg_accitems WITH(NOLOCK) WHERE STARTGID='" + strCtSTARTGID + "' AND gid='" + dsLS.Tables[0].Rows[0]["gid"].ToString() + "'";
#endregion
var cmdSql = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmdSql, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
}
}
}
}
#endregion
}
#endregion
static public String GetData(string strUserID)
{
string strCwACCDATE = BasicDataRefDAL.GetCwACCDATE(strUserID);
return strCwACCDATE;
}
#endregion
}
}