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/MsCtCarryOver/MsCtCarryOverDAL.cs

272 lines
13 KiB
C#

3 years ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DSWeb.MvcShipping.Models.MsCtCarryOver;
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.MsCtCarryOverDAL
{
public class MsCtCarryOverDAL
{
#region 查询
static public List<MsCtCarryOver> GetDataList(string strCondition, string userid, string usercode, string companyid,string sort = null)
{
string sCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(userid);
var strSql = new StringBuilder();
strSql.Append("SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=ct_carry_over.MODIFIEDUSER) from ct_carry_over where CORPID='" + companyid + "' and STARTGID='" + sCtSTARTGID + "'");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
//
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by STARTGID,ACCDAY");
}
return SetData(strSql);
}
static public MsCtCarryOver GetData(string condition, string companyid, string strUserID)
{
string sCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(strUserID);
var strSql = new StringBuilder();
strSql.Append("SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=ct_carry_over.MODIFIEDUSER) from ct_carry_over where CORPID='" + companyid + "' and STARTGID='" + sCtSTARTGID + "'");
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" and " + condition);
}
var list=SetData(strSql);
if (list.Count > 0)
return list[0];
return new MsCtCarryOver();
}
private static List<MsCtCarryOver> SetData(StringBuilder strSql)
{
var headList = new List<MsCtCarryOver>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsCtCarryOver data = new MsCtCarryOver();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.STARTGID = (reader["STARTGID"] == null ? "" : Convert.ToString(reader["STARTGID"]));//账套启用GID
data.ACCDAY = Convert.ToDateTime(reader["ACCDAY"]);//会计日期
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.ISCARRY = (reader["ISCARRY"] == null ? false : Convert.ToBoolean(reader["ISCARRY"]));//是否期末轧账
data.ACCDATE = (reader["ACCDATE"] == null ? "" : Convert.ToString(reader["ACCDATE"]));//会计期间
data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid
if (data.MODIFIEDTIME != null)
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//更改操作时间
data.ISRETURN = (reader["ISRETURN"] == null ? false : Convert.ToBoolean(reader["ISRETURN"]));//是否返轧账
data.MODIFIEDUSERNAME = (reader["MODIFIEDUSERNAME"] == null ? "" : Convert.ToString(reader["MODIFIEDUSERNAME"]));//更改人
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 轧账
public static DBResult onCARRYClick(string ACCDAY, string ISCARRY, string strCOMPANYID, string strUSERID)
{
string sCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(strUSERID);
string sCtACCDATE = BasicDataRefDAL.GetCtACCDATE(strUSERID);
var result = new DBResult();
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string sSQL = "select count(*) as inum from ct_carry_over where STARTGID='" + sCtSTARTGID.Trim() + "' and ACCDAY>='" + ACCDAY + "'";
int inum = int.Parse(T_ALL_DA.GetStrSQL("inum", sSQL));
if (inum==0)
{
string sACCDAY = DateTime.Parse(ACCDAY).ToString("yyyy-MM-dd");
string sYear = sACCDAY.Substring(0,4);
string sMonth = sACCDAY.Substring(5,2);
string sDay = sACCDAY.Substring(8,2);
//
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
sSQL = "INSERT INTO [ct_carry_over]([GID],[STARTGID],[ACCDAY],[YEAR],[MONTH],[DAY],[ISCARRY],[ACCDATE],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],[ISRETURN])"
+ " VALUES(newid(),'" + sCtSTARTGID + "','" + sACCDAY + "','" + sYear + "','" + sMonth + "','" + sDay + "'," + ISCARRY + ",'" + sCtACCDATE + "','" + strCOMPANYID + "','" + strUSERID + "',getdate(),0)";
var cmdDelete = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmdDelete, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
result.Success = true;
result.Message = "操作成功";
return result;
}
else
{
result.Success = false;
result.Message = "此日期已经轧账,不允许重复操作!";
return result;
}
}
#endregion
#region 返轧账
public static DBResult onRETURNClick(string ACCDAY, string strCOMPANYID, string strUSERID)
{
#region 基本信息
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string strCwSTARTGID = BasicDataRefDAL.GetCwSTARTGID(strUSERID);
string sCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(strUSERID);
string sCtACCDATE = BasicDataRefDAL.GetCtACCDATE(strUSERID);
var result = new DBResult();
string sSQL = "select STARTMONTH from ct_design_startusing where (ISDELETE=0 or ISDELETE is null) and gid='" + sCtSTARTGID + "'";
string sSTARTMONTH = T_ALL_DA.GetStrSQL("STARTMONTH", sSQL);
#endregion
sSQL = "select count(*) as inum from ct_design_startusing where STARTGID='" + strCwSTARTGID.Trim() + "' and (ISDELETE=0 or ISDELETE is null) and ISENABLE=1 and (STARTMONTH+'-01')>'" + ACCDAY + "'";
int inum = int.Parse(T_ALL_DA.GetStrSQL("inum", sSQL));
if (inum == 0)
{
sSQL = "select count(*) as inum from ct_carry_over where STARTGID='" + sCtSTARTGID.Trim() + "'";
inum = int.Parse(T_ALL_DA.GetStrSQL("inum", sSQL));
if (inum > 0)
{
sSQL = "select isnull(ACCDAY,'') as ACCDAY from (select ACCDAY from (select top 1 ACCDAY from ct_carry_over where STARTGID='" + sCtSTARTGID.Trim() + "' order by STARTGID,ACCDAY DESC) as a where ACCDAY<='" + ACCDAY + "') as b";
string strACCDAY = T_ALL_DA.GetStrSQL("ACCDAY", sSQL);
if (strACCDAY.Trim() == "")
{
string sACCDAY = DateTime.Parse(ACCDAY).ToString("yyyy-MM-dd");
string sYear = sACCDAY.Substring(0, 4);
string sMonth = sACCDAY.Substring(5, 2);
string sDay = sACCDAY.Substring(8, 2);
//
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
sSQL = "delete from [ct_carry_over] where STARTGID='" + sCtSTARTGID + "' and ACCDAY>'" + ACCDAY + "'";
var cmdDelete = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmdDelete, tran);
if (sACCDAY != (sSTARTMONTH + "-01"))
{
sSQL = "INSERT INTO [ct_carry_over]([GID],[STARTGID],[ACCDAY],[YEAR],[MONTH],[DAY],[ISCARRY],[ACCDATE],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME],[ISRETURN])"
+ " VALUES(newid(),'" + sCtSTARTGID + "','" + sACCDAY + "','" + sYear + "','" + sMonth + "','" + sDay + "',0,'" + sCtACCDATE + "','" + strCOMPANYID + "','" + strUSERID + "',getdate(),1)";
cmdDelete = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmdDelete, tran);
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
result.Success = true;
result.Message = "操作成功";
return result;
}
else
{
result.Success = false;
result.Message = "返轧账指定日期不能大于上次轧账日期[" + DateTime.Parse(strACCDAY).ToString("yyyy-MM-dd") + "]";
return result;
}
}
else
{
result.Success = false;
result.Message = "现无轧账期间!";
return result;
}
}
else
{
result.Success = false;
result.Message = "不能返轧账到启用期间之前的期间!";
return result;
}
}
#endregion
#region 判断是否是已轧账数据
public static DBResult onIsCARRY(string gid, string voudate, string tablename, string strCOMPANYID, string strUSERID)
{
string sCtSTARTGID = BasicDataRefDAL.GetCtSTARTGID(strUSERID);
var result = new DBResult();
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
//
string sSQL = "select top 1 ACCDAY from ct_carry_over where STARTGID='" + sCtSTARTGID.Trim() + "' order by STARTGID,ACCDAY desc";
string sACCDAY = T_ALL_DA.GetStrSQL("ACCDAY", sSQL);
if (sACCDAY != "")
{
sACCDAY = DateTime.Parse(sACCDAY).ToString("yyyy-MM-dd");
if (gid.Trim() == "*" || gid.Trim() == "")
{
if (voudate != "")
{
voudate = voudate.Replace(" GMT+0800 (中国标准时间)", "");
voudate = DateTime.Parse(voudate).ToString("yyyy-MM-dd");
if (DateTime.Parse(voudate) <= DateTime.Parse(sACCDAY))
{
result.Success = false;
result.Message = "[" + sACCDAY + "]已经轧账,不允许操作之前的数据!";
return result;
}
}
}
else
{
sSQL = "select count(*) as inum from " + tablename + " where STARTGID='" + sCtSTARTGID.Trim() + "' and gid='" + gid + "' and VOUDATE>'" + sACCDAY + "'";
int inum = int.Parse(T_ALL_DA.GetStrSQL("inum", sSQL));
if (inum == 0)
{
result.Success = false;
result.Message = "[" + sACCDAY + "]已经轧账,不允许操作之前的数据!";
return result;
}
}
}
result.Success = true;
result.Message = "";
return result;
}
#endregion
}
}