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/EntityDA/InvoiceBookDA.cs

499 lines
23 KiB
C#

3 years ago
using System;
using System.Data;
using System.Data.SqlClient;
using DSWeb.Models;
using WebSqlHelper;
namespace DSWeb.EntityDA
{
public class InvoiceBookDA
{
//发票册主表
private string PARM_INVOICE_BOOK_GID = "@gid";
private string PARM_INVOICE_BOOK_NAME = "@name";
private string PARM_INVOICE_BOOK_DESCRIPTION = "@description";
private string PARM_INVOICE_BOOK_CREATE_USER = "@create_user";
//private string PARM_INVOICE_BOOK_CREATE_TIME = "@create_time";
//private string PARM_INVOICE_BOOK_MODIFIED_USER = "@modified_user";
//private string PARM_INVOICE_BOOK_MODIFIED_TIME = "@modified_time";
private string PARM_INVOICE_BOOK_INVOICE_CODE = "@invoice_code";
private string PARM_INVOICE_BOOK_INVOICE_NUM_START = "@invoice_num_start";
private string PARM_INVOICE_BOOK_INVOICE_NUM_END = "@invoice_num_end";
private string PARM_INVOICE_BOOK_INVOICE_TOTAL = "@invoice_total";
private string PARM_INVOICE_BOOK_IS_LOCK = "@is_lock";
private string PARM_INVOICE_BOOK_IS_DELETE = "@is_delete";
private string PARM_INVOICE_BOOK_DELETE_USER = "@delete_user";
//private string PARM_INVOICE_BOOK_DELETE_TIME = "@delete_time";
private string PARM_INVOICE_BOOK_COMPANY_ID = "@company_id";
private string PARM_INVOICE_BOOK_TYPE = "@type";
//private string PARM_INVOICE_BOOK_COLUMN_NAME = "@column_name";
//private string PARM_INVOICE_BOOK_COLUMN_VALUE = "@column_value";
//发票册从表
private string PARM_INVOICE_BOOK_ITEM_GID = "@gid";
private string PARM_INVOICE_BOOK_ITEM_BOOK_ID = "@book_id";
private string PARM_INVOICE_BOOK_ITEM_INVOICE_NUM = "@invoice_num";
private string PARM_INVOICE_BOOK_ITEM_CREATE_USER = "@create_user";
//private string PARM_INVOICE_BOOK_ITEM_CREATE_TIME = "@create_time";
private string PARM_INVOICE_BOOK_ITEM_IS_LOCK = "@is_lock";
private string PARM_INVOICE_BOOK_ITEM_IS_DELETE = "@is_delete";
private string PARM_INVOICE_BOOK_ITEM_DELETE_USER = "@delete_user";
//private string PARM_INVOICE_BOOK_ITEM_DELETE_TIME = "@delete_time";
private string PARM_INVOICE_BOOK_ITEM_IS_MAKE_OUT = "@is_makeout";
private string SQL_SELECT_INVOICE_BOOK_BY_GID = " SELECT GID, NAME, DESCRIPTION, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME, INVOICECODE, INVOICENUMSTART, INVOICENUMEND, "
+ " INVOICETOTAL, ISLOCK, ISDELETE, DELETEUSER, DELETETIME,TYPE,COMPANYID FROM ch_fee_invoicebooks WHERE GID = @gid ";
//private string SQL_SELECT_INVOICE_BOOK_BY_INVOICECODE = " SELECT GID, NAME, DESCRIPTION, CREATEUSER, CREATETIME, MODIFIEDUSER, MODIFIEDTIME, INVOICECODE, INVOICENUMSTART, INVOICENUMEND, INVOICETOTAL, ISLOCK, ISDELETE, DELETEUSER, DELETETIME FROM ch_fee_invoicebooks WHERE INVOICECODE = @invoice_code ";
private string SQL_INSERT_INVOICE_BOOK = " INSERT INTO ch_fee_invoicebooks(GID,NAME,DESCRIPTION,CREATEUSER,CREATETIME,INVOICECODE,INVOICENUMSTART,INVOICENUMEND,INVOICETOTAL,ISLOCK,ISDELETE,COMPANYID,TYPE) "
+ " VALUES(@gid,@name,@description,@create_user,GETDATE(),@invoice_code,@invoice_num_start,@invoice_num_end,@invoice_total,@is_lock,@is_delete,@company_id,@type) ";
private string SQL_INSERT_INVOICE_BOOK_ITEM = " INSERT INTO ch_fee_invoiceitems(GID, BOOKID, INVOICENUM, CREATEUSER, CREATETIME, ISLOCK, ISDELETE, ISMAKEOUT) "
+ " VALUES(@gid,@book_id,@invoice_num,@create_user,GETDATE(),@is_lock,@is_delete,@is_makeout)";
private string SQL_SELECT_SAME_INVOICE_NUM = " SELECT COUNT(*) FROM ch_fee_invoiceitems WHERE INVOICENUM = @invoice_num AND ISDELETE <> 1 ";
//private string SQL_DELETE_INVOICE_BOOK = " DELETE FROM ch_fee_invoicebooks WHERE GID = @gid ";
//private string SQL_DELETE_INVOICE_BOOK_ITEM = " DELETE FROM ch_fee_invoiceitems WHERE BOOKID = @book_id ";
private string SQL_SELECT_CURRENT_INVOICE_BOOK_ITEM = " SELECT TOP 1 A.GID, A.BOOKID, A.INVOICENUM, A.CREATEUSER, A.CREATETIME, A.ISLOCK, A.ISDELETE, A.DELETEUSER, A.DELETETIME, A.ISMAKEOUT "
+ " FROM ch_fee_invoiceitems as A INNER JOIN ch_fee_invoicebooks as B ON A.BOOKID = B.GID "
+ " WHERE A.ISLOCK <> 1 AND A.ISDELETE <> 1 AND A.ISMAKEOUT <> 1 AND B.COMPANYID = @company_id ORDER BY INVOICENUM ASC ";
private string SQL_SELECT_INVOICE_BOOK_IS_EXIST_MAKEOUT = " SELECT COUNT(*) FROM ch_fee_invoiceitems WHERE BOOKID = @book_id AND ISMAKEOUT <> 0 AND ISDELETE <> 1";
//delete ch_fee_invoicebooks
private string SQL_UPDATE_INVOICE_BOOK_VOID = "UPDATE ch_fee_invoicebooks SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE GID = @gid";
//delete ch_fee_invoiceitems
private string SQL_UPDATE_INVOICE_ITEM_VOID = "UPDATE ch_fee_invoiceitems SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BOOKID = @book_id";
private string SQL_SELECT_INVOICE_BOOK_INVOICE_CODE = "SELECT COUNT(*) FROM ch_fee_invoicebooks WHERE INVOICECODE = @invoice_code and ISDELETE=0";
#region 删除发票册主表信息
/// <summary>
/// 删除发票册主表信息
/// </summary>
/// <param name="tempInvoiceBookGID">发票册主表GID</param>
/// <param name="tempUserID">操作人GID</param>
/// <returns>值1表示删除成功 值不等于1表示删除失败</returns>
public int DeleteInvoiceBook(string tempInvoiceBookGID,string tempUserID)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iExistState = 0;
SqlParameter[] existParms = new SqlParameter[]{
new SqlParameter(PARM_INVOICE_BOOK_ITEM_BOOK_ID,SqlDbType.VarChar,36)
};
existParms[0].Value = tempInvoiceBookGID;
iExistState = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_INVOICE_BOOK_IS_EXIST_MAKEOUT, existParms);
if (iExistState == 0)
{
SqlParameter[] updateItemParms = new SqlParameter[] {
new SqlParameter(PARM_INVOICE_BOOK_ITEM_DELETE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_BOOK_ID,SqlDbType.VarChar,36)
};
updateItemParms[0].Value = tempUserID;
updateItemParms[1].Value = tempInvoiceBookGID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_INVOICE_ITEM_VOID, updateItemParms);
SqlParameter[] updateBookParms = new SqlParameter[] {
new SqlParameter(PARM_INVOICE_BOOK_DELETE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_GID,SqlDbType.VarChar,36)
};
updateBookParms[0].Value = tempUserID;
updateBookParms[1].Value = tempInvoiceBookGID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_INVOICE_BOOK_VOID, updateBookParms);
}
//事务提交
sqlTran.Commit();
if (iExistState > 0)
{
iResult = 2;//状态为2表示发票册含有已经开出的发票,不能将整个发票删除
}
else
{
iResult = 1;//状态为1表示插入成功
}
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
#region 通过发票册GID获取发票信息
/// <summary>
/// 通过发票册GID获取发票信息
/// </summary>
/// <param name="tempInvoiceBookGID">发票GID</param>
/// <returns>返回实体类InvoiceBookEntity信息</returns>
public InvoiceBookEntity GetInvoiceBookByGID(string tempInvoiceBookGID)
{
InvoiceBookEntity invoiceBookEntity = null;
SqlParameter parm = new SqlParameter(PARM_INVOICE_BOOK_GID, SqlDbType.VarChar, 36);
parm.Value = tempInvoiceBookGID;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_INVOICE_BOOK_BY_GID, parm))
{
try
{
while (sqlRead.Read())
{
invoiceBookEntity = new InvoiceBookEntity();
if (!sqlRead.IsDBNull(0))
{
invoiceBookEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
invoiceBookEntity.Name = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
invoiceBookEntity.Description = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
invoiceBookEntity.CreateUser = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
invoiceBookEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
invoiceBookEntity.ModifiedUser = sqlRead.GetString(5);
}
if (!sqlRead.IsDBNull(6))
{
invoiceBookEntity.ModifiedTime = sqlRead.GetDateTime(6);
}
if (!sqlRead.IsDBNull(7))
{
invoiceBookEntity.InvoiceCode = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
invoiceBookEntity.InvoiceNumStart = sqlRead.GetString(8);
}
if (!sqlRead.IsDBNull(9))
{
invoiceBookEntity.InvoiceNumEnd = sqlRead.GetString(9);
}
if (!sqlRead.IsDBNull(10))
{
invoiceBookEntity.InvoiceTotal = sqlRead.GetInt32(10);
}
if (!sqlRead.IsDBNull(11))
{
invoiceBookEntity.IsLock = sqlRead.GetBoolean(11);
}
if (!sqlRead.IsDBNull(12))
{
invoiceBookEntity.IsDelete = sqlRead.GetBoolean(12);
}
if (!sqlRead.IsDBNull(13))
{
invoiceBookEntity.DeleteUser = sqlRead.GetString(13);
}
if (!sqlRead.IsDBNull(14))
{
invoiceBookEntity.DeleteTime = sqlRead.GetDateTime(14);
}
if (!sqlRead.IsDBNull(15))
{
invoiceBookEntity.Type = sqlRead.GetInt32(15);
}
if (!sqlRead.IsDBNull(16))
{
invoiceBookEntity.CompanyID = sqlRead.GetString(16);
}
}
}
catch (Exception execError)
{
throw execError;
}
}
return invoiceBookEntity;
}
#endregion
#region 获取当前可开发票信息
/// <summary>
/// 获取当前可开发票信息
/// </summary>
/// <param name="tempCompanyID">分公司GID</param>
/// <returns>返回实体类InvoiceBookItemEntity</returns>
public InvoiceBookItemEntity GetCurrentBookItem(string tempCompanyID)
{
InvoiceBookItemEntity invoiceBookItemEntity = null;
SqlParameter parm = new SqlParameter(PARM_INVOICE_BOOK_COMPANY_ID, SqlDbType.VarChar, 36);
parm.Value = tempCompanyID;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CURRENT_INVOICE_BOOK_ITEM, parm))
{
try
{
while (sqlRead.Read())
{
invoiceBookItemEntity = new InvoiceBookItemEntity();
if (!sqlRead.IsDBNull(0))
{
invoiceBookItemEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
invoiceBookItemEntity.BookID = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
invoiceBookItemEntity.InvoiceNum = sqlRead.GetString(2);
}
if (!sqlRead.IsDBNull(3))
{
invoiceBookItemEntity.CreateUser = sqlRead.GetString(3);
}
if (!sqlRead.IsDBNull(4))
{
invoiceBookItemEntity.CreateTime = sqlRead.GetDateTime(4);
}
if (!sqlRead.IsDBNull(5))
{
invoiceBookItemEntity.IsLock = sqlRead.GetBoolean(5);
}
if (!sqlRead.IsDBNull(6))
{
invoiceBookItemEntity.IsDelete = sqlRead.GetBoolean(6);
}
if (!sqlRead.IsDBNull(7))
{
invoiceBookItemEntity.DeleteUser = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
invoiceBookItemEntity.DeleteTime = sqlRead.GetDateTime(8);
}
if (!sqlRead.IsDBNull(9))
{
invoiceBookItemEntity.IsMakeOut = sqlRead.GetBoolean(9);
}
}
}
catch (Exception execError)
{
throw execError;
}
}
return invoiceBookItemEntity;
}
#endregion
#region 插入发票信息(事务控制)
/// <summary>
/// 插入发票信息(事务控制)
/// </summary>
/// <param name="tempInvoiceBookEntity">发票实体类</param>
/// <returns>值1表示插入成功 值不等于1表示插入失败</returns>
public int InsertInvoiceBook(InvoiceBookEntity tempInvoiceBookEntity)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
SqlParameter[] bookParms = new SqlParameter[] {
new SqlParameter(PARM_INVOICE_BOOK_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_NAME,SqlDbType.VarChar,50),
new SqlParameter(PARM_INVOICE_BOOK_DESCRIPTION,SqlDbType.VarChar,50),
new SqlParameter(PARM_INVOICE_BOOK_CREATE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_INVOICE_CODE,SqlDbType.VarChar,200),
new SqlParameter(PARM_INVOICE_BOOK_INVOICE_NUM_START,SqlDbType.VarChar,200),
new SqlParameter(PARM_INVOICE_BOOK_INVOICE_NUM_END,SqlDbType.VarChar,200),
new SqlParameter(PARM_INVOICE_BOOK_INVOICE_TOTAL,SqlDbType.Int),
new SqlParameter(PARM_INVOICE_BOOK_IS_LOCK,SqlDbType.Bit),
new SqlParameter(PARM_INVOICE_BOOK_IS_DELETE,SqlDbType.Bit),
new SqlParameter(PARM_INVOICE_BOOK_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_TYPE,SqlDbType.Int)
};
bookParms[0].Value = tempInvoiceBookEntity.GID;
bookParms[1].Value = tempInvoiceBookEntity.Name;
bookParms[2].Value = tempInvoiceBookEntity.Description;
bookParms[3].Value = tempInvoiceBookEntity.CreateUser;
bookParms[4].Value = tempInvoiceBookEntity.InvoiceCode;
bookParms[5].Value = tempInvoiceBookEntity.InvoiceNumStart;
bookParms[6].Value = tempInvoiceBookEntity.InvoiceNumEnd;
bookParms[7].Value = tempInvoiceBookEntity.InvoiceTotal;
bookParms[8].Value = tempInvoiceBookEntity.IsLock;
bookParms[9].Value = tempInvoiceBookEntity.IsDelete;
bookParms[10].Value = tempInvoiceBookEntity.CompanyID;
bookParms[11].Value = tempInvoiceBookEntity.Type;
//private string SQL_INSERT_INVOICE_BOOK = " INSERT INTO ch_fee_invoicebooks(GID,NAME,DESCRIPTION,CREATEUSER,CREATETIME,INVOICECODE,INVOICENUMSTART,INVOICENUMEND,INVOICETOTAL,ISLOCK,ISDELETE,COMPANYID,TYPE) "
//+ " VALUES(@gid,@name,@description,@create_user,GETDATE(),@invoice_code,@invoice_num_start,@invoice_num_end,@invoice_total,@is_lock,@is_delete,@company_id,@type) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_INVOICE_BOOK, bookParms);
SqlParameter[] itemParms = new SqlParameter[] {
new SqlParameter(PARM_INVOICE_BOOK_ITEM_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_BOOK_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_INVOICE_NUM,SqlDbType.VarChar,200),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_CREATE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_IS_LOCK,SqlDbType.Bit),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_IS_DELETE,SqlDbType.Bit),
new SqlParameter(PARM_INVOICE_BOOK_ITEM_IS_MAKE_OUT,SqlDbType.Bit)
};
SqlParameter[] sameParms = new SqlParameter[] {
new SqlParameter(PARM_INVOICE_BOOK_ITEM_INVOICE_NUM,SqlDbType.VarChar,200)
};
foreach (InvoiceBookItemEntity itemEntity in tempInvoiceBookEntity.InvoiceBookItemEntities)
{
sameParms[0].Value = itemEntity.InvoiceNum;
//private string SQL_SELECT_SAME_INVOICE_NUM = " SELECT COUNT(*) FROM ch_fee_invoiceitems WHERE INVOICENUM = @invoice_num AND ISDELETE <> 1 ";
int iSameStatus = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_SAME_INVOICE_NUM, sameParms);
if (iSameStatus > 0)
{
iResult = -4;//已有发票明细,不能重复添加
sqlTran.Rollback();
return iResult;
//throw execError;
}
itemParms[0].Value = itemEntity.GID;
itemParms[1].Value = itemEntity.BookID;
itemParms[2].Value = itemEntity.InvoiceNum;
itemParms[3].Value = itemEntity.CreateUser;
itemParms[4].Value = itemEntity.IsLock;
itemParms[5].Value = itemEntity.IsDelete;
itemParms[6].Value = itemEntity.IsMakeOut;
//private string SQL_INSERT_INVOICE_BOOK_ITEM = " INSERT INTO ch_fee_invoiceitems(GID, BOOKID, INVOICENUM, CREATEUSER, CREATETIME, ISLOCK, ISDELETE, ISMAKEOUT) VALUES(@gid,@book_id,@invoice_num,@create_user,GETDATE(),@is_lock,@is_delete,@is_makeout)";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_INVOICE_BOOK_ITEM, itemParms);
}
//事务提交
sqlTran.Commit();
iResult = 1;//状态为1表示插入成功
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
#region 获取SQL语句查询数据集
/// <summary>
/// 获取SQL语句查询数据集
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public DataSet GetExcuteSql(string strSql)
{
DataSet tempSet = new DataSet();
tempSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql);
return tempSet;
}
#endregion
#region
/// <summary>
/// 验证字段值是否已经存在
/// </summary>
/// <returns>值1表示已经存在 值为0表示不存在</returns>
public int ValidateInvoiceCode(string tempInvoiceCode)
{
int iResult = 0;
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter(PARM_INVOICE_BOOK_INVOICE_CODE,SqlDbType.VarChar,200),
};
parms[0].Value = tempInvoiceCode;
iResult = (int)SqlHelper.ExecuteScalar(conn, CommandType.Text, SQL_SELECT_INVOICE_BOOK_INVOICE_CODE, parms);
}
return iResult;
}
#endregion
}
}