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/MsCwDesignStartusing/MsCwDesignStartusingDAL.cs

710 lines
47 KiB
C#

2 years ago
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
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;
using DSWeb.MvcShipping.Models.MsCwDesignStartusing;
namespace DSWeb.MvcShipping.DAL.MsCwDesignStartusingDAL
{
public class MsCwDesignStartusingDAL
{
#region 查询
static public List<MsCwDesignStartusing> GetDataList(string strCondition, string userid, string usercode, string companyid, string sort = null)
{
var strSql = new StringBuilder();
strSql.Append("SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=cw_design_startusing.MODIFIEDUSER),CORPNAME=(select top 1 [NAME] from [company] where gid=cw_design_startusing.CORPID),ISLOGIN=isnull((select '是' from [cw_design_startusing_user] where [STARTGID]=cw_design_startusing.GID and USERGID='" + userid + "'),'') from cw_design_startusing where (ISDELETE=0 or ISDELETE is null)");// and CORPID='" + companyid + "'
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 CREATETIME");
}
return SetData(strSql);
}
static public MsCwDesignStartusing GetData(string condition, string companyid, string userid)
{
var strSql = new StringBuilder();
strSql.Append("SELECT *,MODIFIEDUSERNAME=(select top 1 SHOWNAME from [user] where gid=cw_design_startusing.MODIFIEDUSER),CORPNAME=(select top 1 [NAME] from [company] where gid=cw_design_startusing.CORPID),ISLOGIN=isnull((select '是' from [cw_design_startusing_user] where [STARTGID]=cw_design_startusing.GID and USERGID='" + userid + "'),'') from cw_design_startusing where (ISDELETE=0 or ISDELETE is null)");// and CORPID='" + companyid + "'
if (!string.IsNullOrEmpty(condition))
{
strSql.Append(" and " + condition);
}
var list = SetData(strSql);
if (list.Count > 0)
return list[0];
return new MsCwDesignStartusing();
}
private static List<MsCwDesignStartusing> SetData(StringBuilder strSql)
{
var headList = new List<MsCwDesignStartusing>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsCwDesignStartusing data = new MsCwDesignStartusing();
#region Set DB data to Object
data.GID = (reader["GID"] == null ? "" : Convert.ToString(reader["GID"]));//唯一编码
data.STARTNAME = (reader["STARTNAME"] == null ? "" : Convert.ToString(reader["STARTNAME"]));//财务账套
data.STARTMONTH = (reader["STARTMONTH"] == null ? "" : Convert.ToString(reader["STARTMONTH"]));//启用年月
data.ISENABLE = (reader["ISENABLE"] == null ? false : Convert.ToBoolean(reader["ISENABLE"]));//是否启用
data.ISLOGIN = (reader["ISLOGIN"] == null ? "" : Convert.ToString(reader["ISLOGIN"]));//是否登录
data.CORPID = (reader["CORPID"] == null ? "" : Convert.ToString(reader["CORPID"]));//分公司代码
data.ISDELETE = (reader["ISDELETE"] == null ? false : Convert.ToBoolean(reader["ISDELETE"]));//是否删除
data.DELETEUSER = (reader["DELETEUSER"] == null ? "" : Convert.ToString(reader["DELETEUSER"]));//删除人
data.DELETETIME = (reader["DELETETIME"] == null ? DateTime.Now : Convert.ToDateTime(reader["DELETETIME"]));//删除时间
data.CREATEUSER = (reader["CREATEUSER"] == null ? "" : Convert.ToString(reader["CREATEUSER"]));//创建人gid
if (data.CREATETIME != null)
data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]);//创建时间
data.MODIFIEDUSER = (reader["MODIFIEDUSER"] == null ? "" : Convert.ToString(reader["MODIFIEDUSER"]));//更改操作人gid
if (data.MODIFIEDTIME != null)
data.MODIFIEDTIME = Convert.ToDateTime(reader["MODIFIEDTIME"]);//更改操作时间
data.MODIFIEDUSERNAME = (reader["MODIFIEDUSERNAME"] == null ? "" : Convert.ToString(reader["MODIFIEDUSERNAME"]));//更改人
data.CORPNAME = (reader["CORPNAME"] == null ? "" : Convert.ToString(reader["CORPNAME"]));
data.FINANCESOFTCODE= Convert.ToString(reader["FINANCESOFTCODE"]);
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 判断总账表中是否已经使用当前账套
public static DBResult isISENABLE(string STARTGID)
{
var result = new DBResult();
//判断总账表中是否已经使用当前账套
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string ls = "select count(*) as iSTART from cw_genleg_accitems WITH(NOLOCK) where STARTGID='" + STARTGID.Trim() + "' and (ISDELETE=0 or ISDELETE is null)";
int iSTART = int.Parse(T_ALL_DA.GetStrSQL("iSTART", ls));
if (iSTART > 0)
{
result.Success = false;
result.Message = "账套已经被使用,不允许更改!";
return result;
}
//判断是否是已经启用的
ls = "select count(*) as iSTART from cw_design_startusing WITH(NOLOCK) where GID='" + STARTGID.Trim() + "' and ISENABLE=1 and (ISDELETE=0 or ISDELETE is null)";
iSTART = int.Parse(T_ALL_DA.GetStrSQL("iSTART", ls));
if (iSTART > 0)
{
result.Success = false;
result.Message = "账套已经被使用,不允许更改!";
return result;
}
result.Success = true;
result.Message = "";
return result;
}
#endregion
#region 设置启用、取消账套
public static DBResult onISENABLE(MsCwDesignStartusing head, string isSTART, string strUSERID, string strCOMPANYID)
{
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
head.STARTMONTH = head.STARTMONTH.ToString().Substring(0, 7);
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
if (isSTART.Trim() == "1")//启用账套
{
#region 是否已经启用
string ls = "select count(*) as iSTART from cw_design_startusing WITH(NOLOCK) where GID='" + head.GID.ToString().Trim() + "' and ISENABLE=1 and (ISDELETE=0 or ISDELETE is null)";// and CORPID='"+strCOMPANYID+"'
int iSTART = int.Parse(T_ALL_DA.GetStrSQL("iSTART", ls));
if (iSTART > 0)
{
tran.Rollback();
result.Success = false;
result.Message = "账套已经启用!";
return result;
}
#endregion
#region 插入/更新 新账套
if (head.GID.ToString().Trim() == "*")
{
head.GID = Guid.NewGuid().ToString();
var cmdDelete2 = db.GetSqlStringCommand("INSERT INTO [cw_design_startusing]([GID],[STARTNAME],[STARTMONTH],[CORPID],[ISDELETE],[DELETEUSER],[DELETETIME],[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],[ISENABLE]) VALUES('" + head.GID.ToString().Trim() + "','" + head.STARTNAME.ToString().Trim() + "','" + head.STARTMONTH.ToString().Trim() + "','" + strCOMPANYID + "',0,'" + strUSERID + "',GETDATE(),'" + strUSERID + "',GETDATE(),'" + strUSERID + "',GETDATE(),1)");
db.ExecuteNonQuery(cmdDelete2, tran);
}
else
{
var cmdDelete2 = db.GetSqlStringCommand("update [cw_design_startusing] set ISENABLE=1,[MODIFIEDUSER]='" + strUSERID + "',[MODIFIEDTIME]=getdate() where [GID]='" + head.GID.ToString().Trim() + "'");
db.ExecuteNonQuery(cmdDelete2, tran);
}
#endregion
#region 清理冗余数据
#region 主表
var cmdDelete = db.GetSqlStringCommand("delete from [cw_carry_over] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_accitems_gl] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_genleg_accitems] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_currency_rate] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_carry_accitems] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_design_gl] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_item] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_glDetail_rpt_temp] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_vouitems_gl_rpt_temp] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_pl_template] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_sofp_template] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_cfs_model] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_cfs_template] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK))");// where GID<>'" + head.GID.ToString().Trim() + "'
db.ExecuteNonQuery(cmdDelete, tran);
//cmdDelete = db.GetSqlStringCommand("delete from [cw_vouchers_gl] where STARTGID not in (select GID from [cw_design_startusing] WITH(NOLOCK) where GID<>'" + head.GID.ToString().Trim() + "')");
//db.ExecuteNonQuery(cmdDelete, tran);
#endregion
#region 明细表
cmdDelete = db.GetSqlStringCommand("delete from [cw_pl_template_detail] where LINKGID not in (select GID from [cw_pl_template] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_pl_template_detail] where [ACCGID] not in (select GID from [cw_accitems_gl] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_sofp_template_detail] where LINKGID not in (select GID from [cw_sofp_template] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_sofp_template_detail] where [ACCGID] not in (select GID from [cw_accitems_gl] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_cfs_model_item] where MODELGID not in (select GID from [cw_cfs_model] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_cfs_template_detail] where LINKGID not in (select GID from [cw_cfs_template] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_cfs_template_detail] where [ACCGID] not in (select GID from [cw_accitems_gl] WITH(NOLOCK))");
//db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_design_gl_item] where LINKGID not in (select GID from [cw_design_gl] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("delete from [cw_accitems_currency] where LINKGID not in (select GID from [cw_accitems_gl] WITH(NOLOCK))");
db.ExecuteNonQuery(cmdDelete, tran);
//cmdDelete = db.GetSqlStringCommand("delete from [cw_vouitems_gl] where ORDNO not in (select ORDNO from [cw_vouchers_gl] WITH(NOLOCK))");
//db.ExecuteNonQuery(cmdDelete, tran);
#endregion
#endregion
}
else//取消账套
{
#region 取消账套
//判断总账表中是否已经使用当前账套
string ls = "select count(*) as iSTART from cw_genleg_accitems WITH(NOLOCK) where STARTGID='" + head.GID.ToString().Trim() + "'";// and CORPID='"+strCOMPANYID+"'
int iSTART = int.Parse(T_ALL_DA.GetStrSQL("iSTART", ls));
if (iSTART > 0)
{
tran.Rollback();
result.Success = false;
result.Message = "账套已经被使用,不允许更改!";
return result;
}
else
{
//var cmdDelete = db.GetSqlStringCommand("delete from [cw_design_startusing] where GID='" + head.GID.ToString().Trim() + "' and (ISDELETE=0 or ISDELETE is null)");// and CORPID='" + strCOMPANYID + "'
var cmdDelete = db.GetSqlStringCommand("update [cw_design_startusing] set ISENABLE=0 where GID='" + head.GID.ToString().Trim() + "' and (ISDELETE=0 or ISDELETE is null)");// and CORPID='" + strCOMPANYID + "'
db.ExecuteNonQuery(cmdDelete, tran);
}
#endregion
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
if (isSTART.Trim() == "1")//启用账套
{
#region 如果是启用账套,则自动配置各个表的默认信息
string strCwACCDATE = head.STARTMONTH.ToString();
string strCwSTARTGID = head.GID.ToString();
string sqlCwSTARTGID = "";//无 当前账套往年的数据。
#region cw_accitems_gl 科目信息表
string sSQL = "select top 1 [YEAR]+'-'+[month] as ACCDATE from cw_accitems_gl where [STARTGID]='" + strCwSTARTGID + "' and [YEAR]=(SUBSTRING('" + strCwACCDATE + "',1,4)) group by [YEAR],[month] order by [YEAR] desc,[month] desc";
string strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
#region cw_accitems_gl 判断是否有当前账套往年的数据
sSQL = "select top 1 [YEAR]+'-'+[month] as ACCDATE from cw_accitems_gl where [STARTGID]='" + strCwSTARTGID + "' group by [YEAR],[month] order by [YEAR] desc,[month] desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
sSQL = "select top 1 [YEAR]+'-'+[month] as ACCDATE from cw_accitems_gl group by [YEAR],[month] order by [YEAR] desc,[month] desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
sSQL = "select top 1 STARTGID from cw_accitems_gl group by [YEAR],[month],STARTGID order by [YEAR] desc,[month] desc";
string strDescSTARTGID = T_ALL_DA.GetStrSQL("STARTGID", sSQL);
sqlCwSTARTGID = " and [STARTGID]='" + strDescSTARTGID + "'";//无 当前账套往年的数据。
}
else
{
sqlCwSTARTGID = " and [STARTGID]='" + strCwSTARTGID + "'";//有 当前账套往年的数据。
}
#endregion
#region cw_accitems_gl 复制插入操作新科目
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
#region cw_accitems_gl 根据现有数据,复制插入新科目
sSQL = "insert into cw_accitems_gl (GID,ACCID,ACCNAME,DETAILED,DC,ISFCY,ISDEPTACC,ISEMPLACC,ISCORPACC,ISITEMACC,REMARKS,YEAR,MONTH,PACCGID,ACCATTRIBUTE,ISENABLE,ACCTYPE,STARTGID)"
+ "(select newid() as GID,ACCID,ACCNAME,DETAILED,DC,ISFCY,ISDEPTACC,ISEMPLACC,ISCORPACC,ISITEMACC,REMARKS,SUBSTRING('" + strCwACCDATE + "',1,4) as [YEAR],SUBSTRING('" + strCwACCDATE + "',6,2) as [MONTH]"
+ ",PACCGID"
+ ",ACCATTRIBUTE,0 as ISENABLE,ACCTYPE,'" + strCwSTARTGID + "'"
+ " from cw_accitems_gl where [YEAR]=(SUBSTRING('" + strDescACCDATE + "',1,4))" + sqlCwSTARTGID + ")";// and gid not in (select gid from cw_accitems_gl where [YEAR]=SUBSTRING('" + strCwACCDATE + "',1,4)" + sqlCwSTARTGID + ")
#endregion
var cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
#region cw_accitems_gl 根据现有数据更新新科目的父类GID及插入cw_accitems_currency科目币别信息
string sSqlYuan = "SELECT GID,ACCID,ACCNAME,DETAILED,DC,ISFCY,ISDEPTACC,ISEMPLACC,ISCORPACC,ISITEMACC,REMARKS,[YEAR],[MONTH],PACCGID=(case when (PACCGID='ZC' or PACCGID='FZ' or PACCGID='GT' or PACCGID='QY' or PACCGID='CB' or PACCGID='SY') then '0' else PACCGID end),ACCATTRIBUTE,ISENABLE,ACCTYPE from [cw_accitems_gl] WITH(NOLOCK) where [YEAR]=(SUBSTRING('" + strDescACCDATE + "',1,4))" + sqlCwSTARTGID + " order by [YEAR],ACCID";// and PACCGID not in('ZC','FZ','GT','QY','CB','SY')
DataSet dsYuan = T_ALL_DA.GetAllSQL(sSqlYuan);
if (dsYuan != null)
{
if (dsYuan.Tables[0].Rows.Count > 0)
{
string sSqlXian = "SELECT GID,ACCID,ACCNAME,DETAILED,DC,ISFCY,ISDEPTACC,ISEMPLACC,ISCORPACC,ISITEMACC,REMARKS,[YEAR],[MONTH],PACCGID=(case when (PACCGID='ZC' or PACCGID='FZ' or PACCGID='GT' or PACCGID='QY' or PACCGID='CB' or PACCGID='SY') then '0' else PACCGID end),ACCATTRIBUTE,ISENABLE,ACCTYPE from [cw_accitems_gl] WITH(NOLOCK) where [YEAR]=SUBSTRING('" + strCwACCDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "' order by [YEAR],ACCID";// and PACCGID not in('ZC','FZ','GT','QY','CB','SY')
DataSet dsXian = T_ALL_DA.GetAllSQL(sSqlXian);
if (dsXian != null)
{
if (dsXian.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsYuan.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < dsXian.Tables[0].Rows.Count; j++)
{
if (dsYuan.Tables[0].Rows[i]["GID"].ToString().Trim() == dsXian.Tables[0].Rows[j]["PACCGID"].ToString().Trim() && dsXian.Tables[0].Rows[j]["PACCGID"].ToString().Trim() != "0")
{
for (int n = 0; n < dsXian.Tables[0].Rows.Count; n++)
{
if (dsYuan.Tables[0].Rows[i]["ACCID"].ToString().Trim() == dsXian.Tables[0].Rows[n]["ACCID"].ToString().Trim())
{
string sSQL4 = "update cw_accitems_gl set PACCGID='" + dsXian.Tables[0].Rows[n]["GID"].ToString().Trim() + "' where PACCGID='" + dsYuan.Tables[0].Rows[i]["GID"].ToString().Trim() + "' and [YEAR]=SUBSTRING('" + strCwACCDATE + "',1,4) and [STARTGID]='" + strCwSTARTGID + "'";
var cmd4 = db.GetSqlStringCommand(sSQL4);
db.ExecuteNonQuery(cmd4, tran);
}
}
}
}
}
}
}
}
}
#endregion
//cw_accitems_currency 科目币别信息
sSQL = "INSERT INTO [cw_accitems_currency]([GID],[LINKGID],[CURRENCY],[CORPID],[CREATEUSER],[CREATETIME])"
+ " SELECT NEWID() as [GID]"
+ ",[LINKGID]=(SELECT top 1 GID from cw_accitems_gl where STARTGID='" + strCwSTARTGID + "' and accid=(select top 1 accid from cw_accitems_gl where gid=cw_accitems_currency.LINKGID))"
+ ",[CURRENCY],'" + strCOMPANYID + "' as [CORPID],'" + strUSERID + "' as [CREATEUSER],GETDATE() as [CREATETIME]"
+ " FROM [cw_accitems_currency] WITH(NOLOCK) where LINKGID in (select GID from cw_accitems_gl where [YEAR]=(SUBSTRING('" + strDescACCDATE + "',1,4))" + sqlCwSTARTGID + " and STARTGID<>'" + strCwSTARTGID + "')";
cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
#endregion
}
#endregion
#region cw_currency_rate 根据现有数据,复制插入“总账汇率表”的数据
DataSet ds = T_ALL_DA.GetAllSQL("select * from code_currency where CODENAME<>'RMB'");
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
MsCwCurrencyRateDAL.MsCwCurrencyRateDAL.isSaveRow(ds.Tables[0].Rows[i]["CODENAME"].ToString(), strUSERID, strCOMPANYID);
}
}
}
#endregion
#region cw_sofp_template 资产负债表
string TEMPLATE = "formRptCwSofpIndex";
sSQL = "select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + strCwACCDATE + "' and TEMPLATE='" + TEMPLATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
#region cw_sofp_template 判断是否有当前账套往年的数据
sSQL = "select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
sSQL = "select top 1 ACCDATE from cw_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
sSQL = "select top 1 STARTGID from cw_sofp_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "' group by ACCDATE,STARTGID order by ACCDATE desc";
string strDescSTARTGID = T_ALL_DA.GetStrSQL("STARTGID", sSQL);
sqlCwSTARTGID = " and [STARTGID]='" + strDescSTARTGID + "'";//无 当前账套往年的数据。
}
else
{
sqlCwSTARTGID = " and [STARTGID]='" + strCwSTARTGID + "'";//有 当前账套往年的数据。
}
#endregion
#region cw_sofp_template 根据现有数据,复制插入“资产负债表”的数据
if (strDescACCDATE != strCwACCDATE)
{
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
//主表
sSQL = "INSERT INTO [cw_sofp_template]([GID],[TEMPLATE],[ACCDATE],[LINENUM],[LINENUM1],[ACCTYPE],[QTYLASTBLC],[QTYYEARBLC],[LINENUM2],[ACCTYPE2],[QTYLASTBLC2],[QTYYEARBLC2],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],[TEMPLATE],'" + strCwACCDATE + "' as [ACCDATE],[LINENUM],[LINENUM1],[ACCTYPE],0 as [QTYLASTBLC],0 as [QTYYEARBLC],[LINENUM2],[ACCTYPE2],0 as [QTYLASTBLC2],0 as [QTYYEARBLC2],'" + strCwSTARTGID + "' as [STARTGID],'" + strCOMPANYID + "' as [CORPID],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_sofp_template] where ACCDATE='" + strDescACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sqlCwSTARTGID;
var cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
//明细表
sSQL = "INSERT INTO [cw_sofp_template_detail]([GID],[LINKGID],[LINKNUM],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYYEARBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=ISNULL((SELECT top 1 [GID] FROM [cw_sofp_template] where LINENUM=(select top 1 LINENUM from cw_sofp_template WITH(NOLOCK) where gid=cw_sofp_template_detail.LINKGID) and ACCDATE='" + strCwACCDATE + "' and [STARTGID]='" + strCwSTARTGID + "'),'')"
+ ",[LINKNUM]"
+ ",[ACCGID]=ISNULL((SELECT top 1 [GID] FROM [cw_accitems_gl] where [STARTGID]='" + strCwSTARTGID + "' and accid=(select top 1 accid from cw_accitems_gl where gid=cw_sofp_template_detail.ACCGID)),'')"
+ ",[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_sofp_template_detail] WITH(NOLOCK) where LINKGID in (SELECT [GID] FROM [cw_sofp_template] WITH(NOLOCK) where ACCDATE='" + strDescACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sqlCwSTARTGID + ")";
cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
}
#endregion
}
#endregion
#region cw_pl_template 利润表
TEMPLATE = "formRptCwPLIndex";
sSQL = "select top 1 ACCDATE from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + strCwACCDATE + "' and TEMPLATE='" + TEMPLATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
#region cw_pl_template 判断是否有当前账套往年的数据
sSQL = "select top 1 ACCDATE from cw_pl_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and TEMPLATE='" + TEMPLATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
sSQL = "select top 1 ACCDATE from cw_pl_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
sSQL = "select top 1 STARTGID from cw_pl_template WITH(NOLOCK) where TEMPLATE='" + TEMPLATE + "' group by ACCDATE,STARTGID order by ACCDATE desc";
string strDescSTARTGID = T_ALL_DA.GetStrSQL("STARTGID", sSQL);
sqlCwSTARTGID = " and [STARTGID]='" + strDescSTARTGID + "'";//无 当前账套往年的数据。
}
else
{
sqlCwSTARTGID = " and [STARTGID]='" + strCwSTARTGID + "'";//有 当前账套往年的数据。
}
#endregion
#region cw_pl_template 根据现有数据,复制插入“利润表”的数据
if (strDescACCDATE != strCwACCDATE)
{
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
//主表
sSQL = "INSERT INTO [cw_pl_template]([GID],[TEMPLATE],[ACCDATE],[LINENUM],[ACCTYPE],[QTYLASTBLC],[QTYBLC],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],[TEMPLATE],'" + strCwACCDATE + "' as [ACCDATE],[LINENUM],[ACCTYPE],0 as [QTYLASTBLC],0 as [QTYBLC],'" + strCwSTARTGID + "' as [STARTGID],'" + strCOMPANYID + "' as [CORPID],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_pl_template] WITH(NOLOCK) where ACCDATE='" + strDescACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sqlCwSTARTGID;
var cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
//明细表
sSQL = "INSERT INTO [cw_pl_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=(SELECT top 1 [GID] FROM [cw_pl_template] where LINENUM=(select top 1 LINENUM from cw_pl_template WITH(NOLOCK) where gid=cw_pl_template_detail.LINKGID) and ACCDATE='" + strCwACCDATE + "' and [STARTGID]='" + strCwSTARTGID + "')"
+ ",[ACCGID]=(SELECT top 1 [GID] FROM [cw_accitems_gl] where [STARTGID]='" + strCwSTARTGID + "' and accid=(select top 1 accid from cw_accitems_gl where gid=cw_pl_template_detail.ACCGID))"
+ ",[OPERATORS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() FROM [cw_pl_template_detail] WITH(NOLOCK) where LINKGID in (SELECT [GID] FROM [cw_pl_template] WITH(NOLOCK) where ACCDATE='" + strDescACCDATE + "' and TEMPLATE='" + TEMPLATE + "'" + sqlCwSTARTGID + ")";
cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
}
#endregion
}
#endregion
#region cw_cfs_model 现金流量表模板
strDescACCDATE = "";
sSQL = "select top 1 GID from [cw_cfs_model] WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and ISDEFAULT=1 order by MODIFIEDTIME";
string newMODELGID = T_ALL_DA.GetStrSQL("GID", sSQL);
if (newMODELGID == "")
{
newMODELGID = Guid.NewGuid().ToString();
#region cw_cfs_model 插入当前账套的模板数据及明细表
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
//主表
sSQL = "INSERT INTO [cw_cfs_model]([GID],[MODELNAME],[ISDEFAULT],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ " VALUES('" + newMODELGID + "','默认方案',1,'" + strCwSTARTGID + "' ,'" + strCOMPANYID + "','" + strUSERID + "',GETDATE())";
var cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
//明细表
sSQL = "INSERT INTO [cw_cfs_model_item]([GID],[MODELGID],[ITEMNAME],[LINENUM],[DETAILED],[ISALTER],[PACCGID],[ITEMTYPE],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID],'" + newMODELGID + "' as [MODELGID],[ITEMNAME],[LINENUM],[DETAILED],[ISALTER]"
+ ",[PACCGID]=(select top 1 GID from [cw_cfs_model_item] as b where b.LINENUM=(select top 1 LINENUM from [cw_cfs_model_item] as a where a.gid=[cw_cfs_model_item].[PACCGID]) and [MODELGID]='" + newMODELGID + "')"
+ ",[ITEMTYPE],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() as [MODIFIEDTIME] FROM [cw_cfs_model_item] WITH(NOLOCK) where MODELGID=(SELECT top 1 [GID] FROM [cw_cfs_model] WITH(NOLOCK) where [STARTGID]<>'" + strCwSTARTGID + "' and ISDEFAULT=1)";
cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
#endregion
}
#endregion
#region cw_cfs_template 现金流量表
sqlCwSTARTGID = "";//无 当前账套往年的数据。
sSQL = "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and ACCDATE='" + strCwACCDATE + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
#region cw_cfs_template 判断是否有当前账套往年的数据
sSQL = "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
sSQL = "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' and MODELGID=(select top 1 GID from cw_cfs_model where [STARTGID]='" + strCwSTARTGID + "' order by MODIFIEDTIME) group by ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
sSQL = "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) where [STARTGID]='" + strCwSTARTGID + "' group by MODELGID,ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
if (strDescACCDATE == "")
{
sSQL = "select top 1 ACCDATE from cw_cfs_template WITH(NOLOCK) group by MODELGID,ACCDATE order by ACCDATE desc";
strDescACCDATE = T_ALL_DA.GetStrSQL("ACCDATE", sSQL);
sSQL = "select top 1 STARTGID from cw_cfs_template WITH(NOLOCK) group by MODELGID,ACCDATE,STARTGID order by ACCDATE desc";
string strDescSTARTGID = T_ALL_DA.GetStrSQL("STARTGID", sSQL);
sqlCwSTARTGID = " and [STARTGID]='" + strDescSTARTGID + "'";//无 当前账套往年的数据。
}
else
{
sqlCwSTARTGID = " and [STARTGID]='" + strCwSTARTGID + "'";//有 当前账套往年的数据。
}
}
else
{
sqlCwSTARTGID = " and [STARTGID]='" + strCwSTARTGID + "'";//有 当前账套往年的数据。
}
}
#endregion
#region cw_cfs_template 根据现有数据,复制插入“现金流量表”的数据
if (strDescACCDATE != strCwACCDATE)
{
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
//主表
sSQL = "INSERT INTO [cw_cfs_template]([GID],[MODELGID],[ITEMGID],[ACCDATE],[QTYLASTBLC],[QTYBLC],[QTYLASTBLC_S],[QTYBLC_S],[QTYLASTBLC_H],[QTYBLC_H],[STARTGID],[CORPID],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[MODELGID]=(select top 1 GID from cw_cfs_model where [STARTGID]='" + strCwSTARTGID + "' and ISDEFAULT=1)"
+ ",[ITEMGID]=(select top 1 GID from [cw_cfs_model_item] as b where b.LINENUM=(select top 1 LINENUM from [cw_cfs_model_item] as a where a.gid=[cw_cfs_template].[ITEMGID]) and MODELGID=(select top 1 GID from cw_cfs_model where [STARTGID]='" + strCwSTARTGID + "' and ISDEFAULT=1 order by MODIFIEDTIME))"
+ ",'" + strCwACCDATE + "' as [ACCDATE],0 as [QTYLASTBLC],0 as [QTYBLC],0 as [QTYLASTBLC_S],0 as [QTYBLC_S],0 as [QTYLASTBLC_H],0 as [QTYBLC_H],'" + strCwSTARTGID + "' as [STARTGID],'" + strCOMPANYID + "' as [CORPID],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() as [MODIFIEDTIME]"
+ " FROM [cw_cfs_template] WITH(NOLOCK) where ACCDATE='" + strDescACCDATE + "'" + sqlCwSTARTGID;
var cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
//明细表
sSQL = "INSERT INTO [cw_cfs_template_detail]([GID],[LINKGID],[ACCGID],[OPERATORS],[ACCESS],[QTYLASTBLC],[QTYBLC],[MODIFIEDUSER],[MODIFIEDTIME]) "
+ "SELECT NEWID() as [GID]"
+ ",[LINKGID]=(SELECT top 1 [GID] FROM [cw_cfs_template] where ITEMGID=(select top 1 GID from [cw_cfs_model_item] where MODELGID=(select top 1 GID from cw_cfs_model where [STARTGID]='" + strCwSTARTGID + "' and ISDEFAULT=1 order by MODIFIEDTIME) and LINENUM=(select top 1 LINENUM from [cw_cfs_model_item] where gid=(select top 1 ITEMGID from cw_cfs_template WITH(NOLOCK) where gid=cw_cfs_template_detail.LINKGID))) and MODELGID=(select top 1 GID from cw_cfs_model where [STARTGID]='" + strCwSTARTGID + "' and ISDEFAULT=1 order by MODIFIEDTIME) and ACCDATE='" + strCwACCDATE + "' and [STARTGID]='" + strCwSTARTGID + "')"
+ ",[ACCGID]=(SELECT top 1 [GID] FROM [cw_accitems_gl] where [STARTGID]='" + strCwSTARTGID + "' and accid=(select top 1 accid from cw_accitems_gl where gid=cw_cfs_template_detail.ACCGID))"
+ ",[OPERATORS],[ACCESS],0 as [QTYLASTBLC],0 as [QTYBLC],'" + strUSERID + "' as [MODIFIEDUSER],GETDATE() as [MODIFIEDTIME]"
+ " FROM [cw_cfs_template_detail] WITH(NOLOCK) where LINKGID in (SELECT [GID] FROM [cw_cfs_template] WITH(NOLOCK) where ACCDATE='" + strDescACCDATE + "'" + sqlCwSTARTGID + ")";
cmd = db.GetSqlStringCommand(sSQL);
db.ExecuteNonQuery(cmd, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
}
#endregion
}
#endregion
#endregion
}
result.Success = true;
result.Message = "操作成功";
return result;
}
#endregion
#region 登录
public static DBResult onISLOGIN(MsCwDesignStartusing head, string strUSERID, string strCOMPANYID)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdDelete = db.GetSqlStringCommand("delete from [cw_design_startusing_user] where USERGID='" + strUSERID + "'");
db.ExecuteNonQuery(cmdDelete, tran);
cmdDelete = db.GetSqlStringCommand("INSERT INTO [cw_design_startusing_user]([GID],[USERGID],[STARTGID],[CREATETIME]) VALUES(newid(),'" + strUSERID + "','" + head.GID.ToString().Trim() + "',GETDATE())");
db.ExecuteNonQuery(cmdDelete, tran);
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "操作错误,请重试!";
return result;
}
}
result.Success = true;
result.Message = "操作成功";
return result;
}
#endregion
#region 获取已启用已登录的账套
public static string GetSTARTMONTH(string STARTGID, string strCompanyID, string strUSERID)
{
T_ALL_DA T_ALL_DA = new EntityDA.T_ALL_DA();
string sSQL = "select top 1 STARTMONTH from [cw_design_startusing] WITH(NOLOCK) where ISENABLE=1 and GID=(select top 1 STARTGID from [cw_design_startusing_user] WITH(NOLOCK) where USERGID='" + strUSERID + "') and (ISDELETE=0 or ISDELETE is null) order by STARTMONTH desc";
string sSTARTMONTH = T_ALL_DA.GetStrSQL("STARTMONTH", sSQL);
if (sSTARTMONTH.Trim() == "")
{
//return "请先设置帐套信息!";
return "-1";
}
return sSTARTMONTH.Trim();
}
#endregion
}
}