DS7/DSWeb/EntityDA/FeeSettlementDA.cs

725 lines
42 KiB
C#

using System;
using System.Data;
using System.Data.SqlClient;
using DSWeb.Models;
using WebSqlHelper;
namespace DSWeb.EntityDA
{
public class FeeSettlementDA
{
private string PARM_FEE_SETTLEMENT_GID = "@gid";
private string PARM_FEE_SETTLEMENT_BILL_NO = "@bill_no";
//private string PARM_FEE_SETTLEMENT_SETTLE_MODE = "@settle_mode";
//private string PARM_FEE_SETTLEMENT_CUSTOMER_NAME = "@customer_name";
//private string PARM_FEE_SETTLEMENT_SETTLE_TYPE = "@settle_type";
//private string PARM_FEE_SETTLEMENT_AMOUNT_RMB = "@amount_rmb";
//private string PARM_FEE_SETTLEMENT_ACCOUNT_RMB = "@account_rmb";
//private string PARM_FEE_SETTLEMENT_RATE = "@rate";
//private string PARM_FEE_SETTLEMENT_AMOUNT_USD = "@amount_usd";
//private string PARM_FEE_SETTLEMENT_ACCOUNT_USD = "@account_usd";
//private string PARM_FEE_SETTLEMENT_BILL_TYPE = "@bill_type";
//private string PARM_FEE_SETTLEMENT_VOUCHER_NO = "@voucher_no";
//private string PARM_FEE_SETTLEMENT_SETTLE_USER = "@settle_user";
//private string PARM_FEE_SETTLEMENT_SETTLE_TIME = "@settle_time";
//private string PARM_FEE_SETTLEMENT_AUDIT_USER = "@audit_user";
//private string PARM_FEE_SETTLEMENT_AUDIT_TIME = "@audit_time";
//private string PARM_FEE_SETTLEMENT_IS_EXPORT = "@is_export";
//private string PARM_FEE_SETTLEMENT_FINANCIAL_VOUCHER = "@financial_voucher";
//private string PARM_FEE_SETTLEMENT_BILL_STATUS = "@bill_status";
//private string PARM_FEE_SETTLEMENT_REMARK = "@remark";
//private string PARM_FEE_SETTLEMENT_IS_DELETE = "@is_delete";
private string PARM_FEE_SETTLEMENT_DELETE_USER = "@delete_user";
//private string PARM_FEE_SETTLEMENT_DELETE_TIME = "@delete_time";
//ch_fee_do
//private string PARM_FEE_DO_GID = "@gid";
private string PARM_FEE_DO_BILL_NO = "@bill_no";
//private string PARM_FEE_DO_IS_DELETE = "@is_delete";
private string PARM_FEE_DO_DELETE_USER = "@delete_user";
//private string PARM_FEE_DO_DELETE_TIME = "@delete_time";
private string SQL_SELECT_FEE_SETTLEMENT_BY_GID = " SELECT GID, BILLNO, BILLDATE, SETTLEMODE, CUSTOMERNAME, SETTLETYPE, AMOUNTRMB, ACCOUNTRMB, RATE, AMOUNTUSD, ACCOUNTUSD, BILLTYPE, VOUCHERNO, SETTLEUSER, SETTLETIME, AUDITUSER, AUDITTIME, ISEXPORT, FINANCIALVOUCHER, BILLSTATUS, REMARK, ISDELETE,DELETEUSER,DELETETIME,COMPANYID, ACCOUNTS_CURRENCY, ACCOUNTS_RATE, ACCOUNTS_MONEY, PREPAY_CURRENCY, PREPAY_RATE, PREPAY_MONEY, AHSR_CURRENCY, AHSR_RATE, AHSR_MONEY, FINANCIAL_CURRENCY, FINANCIAL_RATE, FINANCIAL_MONEY, ADVANCE_CURRENCY, ADVANCE_RATE, ADVANCE_MONEY FROM ch_fee_settlement WHERE GID = @gid";
private string SQL_SELECT_FEE_SETTLEMENT_BY_BILLNO = " SELECT GID, BILLNO, BILLDATE, SETTLEMODE, CUSTOMERNAME, SETTLETYPE, AMOUNTRMB, ACCOUNTRMB, RATE, AMOUNTUSD, ACCOUNTUSD, BILLTYPE, VOUCHERNO, SETTLEUSER, SETTLETIME, AUDITUSER, AUDITTIME, ISEXPORT, FINANCIALVOUCHER, BILLSTATUS, REMARK,ISDELETE,DELETEUSER,DELETETIME "
+ " FROM ch_fee_settlement WHERE BILLNO = @bill_no";
private string SQL_SELECT_FEE_SETTLEMENT_ALL = " SELECT GID, BILLNO, BILLDATE, SETTLEMODE, CUSTOMERNAME, SETTLETYPE, AMOUNTRMB, ACCOUNTRMB, RATE, AMOUNTUSD, ACCOUNTUSD, BILLTYPE, VOUCHERNO, SETTLEUSER, SETTLETIME, AUDITUSER, AUDITTIME, ISEXPORT, FINANCIALVOUCHER, BILLSTATUS, REMARK FROM ch_fee_settlement";
private string SQL_UPDATE_FEE_SETTLEMENT_BY_BILL_NO = " UPDATE ch_fee_settlement SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no ";
private string SQL_UPDATE_FEE_DO_BY_BILL_NO = " UPDATE ch_fee_do SET ISDELETED = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no ";
private string SQL_UPDATE_CH_FEE = " UPDATE ch_fee SET SETTLEMENT = isnull((SETTLEMENT - A.AMOUNT),0),FEESTATUS = A.FEESTATUS "
+ " FROM (SELECT CASE WHEN B.AMOUNT = C.SETTLEMENT THEN 0 ELSE 8 END as FEESTATUS,B.FEEID,B.AMOUNT FROM ch_fee_do as B INNER JOIN ch_fee as C ON "
+ " B.FEEID = C.GID WHERE B.BILLNO = @bill_no) as A WHERE A.FEEID = ch_fee.GID ";
#region 通过惟一主键获取费用结算信息
/// <summary>
/// 通过惟一主键获取费用结算信息
/// </summary>
/// <param name="tempFeeSettlementID">费用结算GID</param>
/// <returns>返回实体类FeeSettlementEntity</returns>
public FeeSettlementEntity GetFeeSettlementByGID(string tempFeeSettlementID)
{
SqlParameter parm = new SqlParameter(PARM_FEE_SETTLEMENT_GID, SqlDbType.VarChar, 36);
parm.Value = tempFeeSettlementID;
FeeSettlementEntity feeSettlementEntity = null;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_FEE_SETTLEMENT_BY_GID, parm))
{
try
{
while (sqlRead.Read())
{
feeSettlementEntity = new FeeSettlementEntity();
if (!sqlRead.IsDBNull(0))
{
feeSettlementEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
feeSettlementEntity.BillNO = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
feeSettlementEntity.BillDate = sqlRead.GetDateTime(2);
}
if (!sqlRead.IsDBNull(3))
{
feeSettlementEntity.SettleMode = sqlRead.GetInt32(3);
}
if (!sqlRead.IsDBNull(4))
{
feeSettlementEntity.CustomerName = sqlRead.GetString(4);
}
if (!sqlRead.IsDBNull(5))
{
feeSettlementEntity.SettleType = sqlRead.GetInt32(5);
}
if (!sqlRead.IsDBNull(6))
{
feeSettlementEntity.AmountRMB = sqlRead.GetDecimal(6);
}
if (!sqlRead.IsDBNull(7))
{
feeSettlementEntity.AccountRMB = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
feeSettlementEntity.Rate = sqlRead.GetDecimal(8);
}
if (!sqlRead.IsDBNull(9))
{
feeSettlementEntity.AmountUSD = sqlRead.GetDecimal(9);
}
if (!sqlRead.IsDBNull(10))
{
feeSettlementEntity.AccountUSD = sqlRead.GetString(10);
}
if (!sqlRead.IsDBNull(11))
{
feeSettlementEntity.BillType = sqlRead.GetInt32(11);
}
if (!sqlRead.IsDBNull(12))
{
feeSettlementEntity.VoucherNO = sqlRead.GetString(12);
}
if (!sqlRead.IsDBNull(13))
{
feeSettlementEntity.SettleUser = sqlRead.GetString(13);
}
if (!sqlRead.IsDBNull(14))
{
feeSettlementEntity.SettleTime = sqlRead.GetDateTime(14);
}
if (!sqlRead.IsDBNull(15))
{
feeSettlementEntity.AuditUser = sqlRead.GetString(15);
}
if (!sqlRead.IsDBNull(16))
{
feeSettlementEntity.AuditTime = sqlRead.GetDateTime(16);
}
if (!sqlRead.IsDBNull(17))
{
feeSettlementEntity.IsExport = sqlRead.GetBoolean(17);
}
if (!sqlRead.IsDBNull(18))
{
feeSettlementEntity.FinancialVoucher = sqlRead.GetString(18);
}
if (!sqlRead.IsDBNull(19))
{
feeSettlementEntity.BillStatus = sqlRead.GetInt32(19);
}
if (!sqlRead.IsDBNull(20))
{
feeSettlementEntity.Remark = sqlRead.GetString(20);
}
if (!sqlRead.IsDBNull(21))
{
feeSettlementEntity.IsDelete = sqlRead.GetBoolean(21);
}
if (!sqlRead.IsDBNull(22))
{
feeSettlementEntity.DeleteUser = sqlRead.GetString(22);
}
if (!sqlRead.IsDBNull(23))
{
feeSettlementEntity.DeleteTime = sqlRead.GetDateTime(23);
}
if (!sqlRead.IsDBNull(24))
{
feeSettlementEntity.CompanyID = sqlRead.GetString(24);
}
if (!sqlRead.IsDBNull(25))
{
feeSettlementEntity.ACCOUNTS_CURRENCY = sqlRead.GetString(25);
}
if (!sqlRead.IsDBNull(26))
{
feeSettlementEntity.ACCOUNTS_RATE = sqlRead.GetDecimal(26);
}
if (!sqlRead.IsDBNull(27))
{
feeSettlementEntity.ACCOUNTS_MONEY = sqlRead.GetDecimal(27);
}
if (!sqlRead.IsDBNull(28))
{
feeSettlementEntity.PREPAY_CURRENCY = sqlRead.GetString(28);
}
if (!sqlRead.IsDBNull(29))
{
feeSettlementEntity.PREPAY_RATE = sqlRead.GetDecimal(29);
}
if (!sqlRead.IsDBNull(30))
{
feeSettlementEntity.PREPAY_MONEY = sqlRead.GetDecimal(30);
}
if (!sqlRead.IsDBNull(31))
{
feeSettlementEntity.AHSR_CURRENCY = sqlRead.GetString(31);
}
if (!sqlRead.IsDBNull(32))
{
feeSettlementEntity.AHSR_RATE = sqlRead.GetDecimal(32);
}
if (!sqlRead.IsDBNull(33))
{
feeSettlementEntity.AHSR_MONEY = sqlRead.GetDecimal(33);
}
if (!sqlRead.IsDBNull(34))
{
feeSettlementEntity.FINANCIAL_CURRENCY = sqlRead.GetString(34);
}
if (!sqlRead.IsDBNull(35))
{
feeSettlementEntity.FINANCIAL_RATE = sqlRead.GetDecimal(35);
}
if (!sqlRead.IsDBNull(36))
{
feeSettlementEntity.FINANCIAL_MONEY = sqlRead.GetDecimal(36);
}
if (!sqlRead.IsDBNull(37))
{
feeSettlementEntity.ADVANCE_CURRENCY = sqlRead.GetString(37);
}
if (!sqlRead.IsDBNull(38))
{
feeSettlementEntity.ADVANCE_RATE = sqlRead.GetDecimal(38);
}
if (!sqlRead.IsDBNull(39))
{
feeSettlementEntity.ADVANCE_MONEY = sqlRead.GetDecimal(39);
}
}
}
catch (Exception execError)
{
throw execError;
}
}
return feeSettlementEntity;
}
#endregion
#region 通过业务编号获取费用结算信息
/// <summary>
/// 通过业务编号获取费用结算信息
/// </summary>
/// <param name="tempFeeSettlementID">费用结算业务编号</param>
/// <returns>返回实体类FeeSettlementEntity</returns>
public FeeSettlementEntity GetFeeSettlementByBillNO(string tempBillNO)
{
SqlParameter parm = new SqlParameter(PARM_FEE_SETTLEMENT_BILL_NO, SqlDbType.VarChar, 20);
parm.Value = tempBillNO;
FeeSettlementEntity feeSettlementEntity = null;
using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_FEE_SETTLEMENT_BY_BILLNO, parm))
{
try
{
while (sqlRead.Read())
{
feeSettlementEntity = new FeeSettlementEntity();
if (!sqlRead.IsDBNull(0))
{
feeSettlementEntity.GID = sqlRead.GetString(0);
}
if (!sqlRead.IsDBNull(1))
{
feeSettlementEntity.BillNO = sqlRead.GetString(1);
}
if (!sqlRead.IsDBNull(2))
{
feeSettlementEntity.BillDate = sqlRead.GetDateTime(2);
}
if (!sqlRead.IsDBNull(3))
{
feeSettlementEntity.SettleMode = sqlRead.GetInt32(3);
}
if (!sqlRead.IsDBNull(4))
{
feeSettlementEntity.CustomerName = sqlRead.GetString(4);
}
if (!sqlRead.IsDBNull(5))
{
feeSettlementEntity.SettleType = sqlRead.GetInt32(5);
}
if (!sqlRead.IsDBNull(6))
{
feeSettlementEntity.AmountRMB = sqlRead.GetDecimal(6);
}
if (!sqlRead.IsDBNull(7))
{
feeSettlementEntity.AccountRMB = sqlRead.GetString(7);
}
if (!sqlRead.IsDBNull(8))
{
feeSettlementEntity.Rate = sqlRead.GetDecimal(8);
}
if (!sqlRead.IsDBNull(9))
{
feeSettlementEntity.AmountUSD = sqlRead.GetDecimal(9);
}
if (!sqlRead.IsDBNull(10))
{
feeSettlementEntity.AccountUSD = sqlRead.GetString(10);
}
if (!sqlRead.IsDBNull(11))
{
feeSettlementEntity.BillType = sqlRead.GetInt32(11);
}
if (!sqlRead.IsDBNull(12))
{
feeSettlementEntity.VoucherNO = sqlRead.GetString(12);
}
if (!sqlRead.IsDBNull(13))
{
feeSettlementEntity.SettleUser = sqlRead.GetString(13);
}
if (!sqlRead.IsDBNull(14))
{
feeSettlementEntity.SettleTime = sqlRead.GetDateTime(14);
}
if (!sqlRead.IsDBNull(15))
{
feeSettlementEntity.AuditUser = sqlRead.GetString(15);
}
if (!sqlRead.IsDBNull(16))
{
feeSettlementEntity.AuditTime = sqlRead.GetDateTime(16);
}
if (!sqlRead.IsDBNull(17))
{
feeSettlementEntity.IsExport = sqlRead.GetBoolean(17);
}
if (!sqlRead.IsDBNull(18))
{
feeSettlementEntity.FinancialVoucher = sqlRead.GetString(18);
}
if (!sqlRead.IsDBNull(19))
{
feeSettlementEntity.BillStatus = sqlRead.GetInt32(19);
}
if (!sqlRead.IsDBNull(20))
{
feeSettlementEntity.Remark = sqlRead.GetString(20);
}
if (!sqlRead.IsDBNull(21))
{
feeSettlementEntity.IsDelete = sqlRead.GetBoolean(21);
}
if (!sqlRead.IsDBNull(22))
{
feeSettlementEntity.DeleteUser = sqlRead.GetString(22);
}
if (!sqlRead.IsDBNull(23))
{
feeSettlementEntity.DeleteTime = sqlRead.GetDateTime(23);
}
}
}
catch (Exception execError)
{
throw execError;
}
}
return feeSettlementEntity;
}
#endregion
#region 获取所有费用结算信息(DataSet)
/// <summary>
/// 获取所有费用结算信息(DataSet)
/// </summary>
/// <returns>返回DataSet数据</returns>
public DataSet GetAllFeeSettlement()
{
return GetExcuteSql(SQL_SELECT_FEE_SETTLEMENT_ALL);
}
#endregion
#region 通过SQL语句获取数据
/// <summary>
/// 通过SQL语句获取数据
/// </summary>
/// <param name="strSql">要执行查询的SQL语句</param>
/// <returns>返回DataSet数据</returns>
public DataSet GetExcuteSql(string strSql)
{
DataSet dataSet = new DataSet();
dataSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql);
return dataSet;
}
#endregion
#region 撤销收费结算(不包含收费申请结算)
/// <summary>
/// 撤销收费结算(不包含收费申请结算)
/// </summary>
/// <param name="tempUserID">用户GID</param>
/// <param name="tempBillNO">收费结算业务编号</param>
/// <returns>值1表示撤销成功 值不等于1表示撤销失败</returns>
public int RevocateSettlement(string tempUserID,string tempBillNO)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
//撤销收费结算
//1-删除结算表ch_fee_settlement记录
SqlParameter[] updateSettleParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_SETTLEMENT_DELETE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_NO,SqlDbType.VarChar,20)
};
updateSettleParms[0].Value = tempUserID;
updateSettleParms[1].Value = tempBillNO;
//string SQL_UPDATE_FEE_SETTLEMENT_BY_BILL_NO = " UPDATE ch_fee_settlement SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_FEE_SETTLEMENT_BY_BILL_NO, updateSettleParms);
//2-删除ch_fee_do表
SqlParameter[] updateFeeDoParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_DELETE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20)
};
updateFeeDoParms[0].Value = tempUserID;
updateFeeDoParms[1].Value = tempBillNO;
//string SQL_UPDATE_FEE_DO_BY_BILL_NO = " UPDATE ch_fee_do SET ISDELETED = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_FEE_DO_BY_BILL_NO, updateFeeDoParms);
//3-还原费用表SETTLEMENT字段值
SqlParameter[] updateFeeParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20)
};
updateFeeParms[0].Value = tempBillNO;
//string SQL_UPDATE_CH_FEE = " UPDATE ch_fee SET SETTLEMENT = SETTLEMENT - A.AMOUNT,FEESTATUS = A.FEESTATUS "
//+ " FROM (SELECT CASE WHEN B.AMOUNT = C.SETTLEMENT THEN 0 ELSE 8 END as FEESTATUS,B.FEEID,B.AMOUNT FROM ch_fee_do as B INNER JOIN ch_fee as C ON B.FEEID = C.GID WHERE B.BILLNO = @bill_no) as A WHERE A.FEEID = ch_fee.GID ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE, updateFeeParms);
//事务提交
sqlTran.Commit();
iResult = 1;//状态为1表示插入成功
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
#region 撤销收费结算(包含收费申请结算)
/// <summary>
/// 撤销收费结算(包含收费申请结算)
/// </summary>
/// <param name="tempPaySettleID">收费结算GID</param>
/// <param name="tempPaySettleID">收费结算单号</param>
/// <param name="tempOperator">操作人GID</param>
/// <param name="tempCompanyID">分公司GID</param>
/// <returns>值1表示撤销结算成功 值不等于1表示撤销结算失败</returns>
public int CacelRecvSettle(string tempPaySettleID, string tempBillNO, string tempOperator, string tempCompanyID)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iSourceState = 0;
string sSourceState2 = "";
if (tempCompanyID == null)
{
tempCompanyID = "";
}
else
{
tempCompanyID = tempCompanyID.ToString().Trim() == "" ? "" : " AND COMPANYID = '" + tempCompanyID + "'";
}
//获取收费结算来源//再判断当前结算信息是否来自收费申请
string SQL_VALID_PAY_SETTLE_SOURCE = " SELECT COUNT(*) FROM ch_fee_settle_link WHERE SETTLEID = '" + tempPaySettleID + "'";
iSourceState = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_VALID_PAY_SETTLE_SOURCE, null);
//iSourceState为0表示收费结算是直接结算 iSourceState大于0表示收费结算是通过收费申请结算
if (iSourceState > 0)
{
string sSQL = "SELECT COUNT(*) FROM ch_fee_settle_link WHERE RELATIONSUBID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "')";
iSourceState = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, sSQL, null);
//
string sCURRENCY = "RMB";
sSQL = "SELECT (case when isnull(AMOUNTRMB,0)<>0 then 'RMB' else (case when isnull(AMOUNTUSD,0)<>0 then 'USD' else 'RMB' end) end) as CURRENCY from ch_fee_RecvApplication where GID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "') and (ISDELETE is null or ISDELETE=0) ";
object statusObj = SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, sSQL, null);
if (statusObj != null)
{
sCURRENCY = statusObj.ToString().Trim();
}
//
if (sCURRENCY == "RMB")
{
sSQL = "SELECT (case when isnull(AMOUNTRMB,0)=(case when isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0)=0 then isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20,2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='USD' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) else isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) end) then '全部结算' else '部分结算' end) as zt from ch_fee_RecvApplication where GID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "') and (ISDELETE is null or ISDELETE=0) ";
statusObj = SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, sSQL, null);
if (statusObj == null)
{
sSourceState2 = "全部结算";
}
else
{
sSourceState2 = statusObj.ToString().Trim();
}
}
else
{
sSQL = "SELECT (case when isnull(AMOUNTUSD,0)=(case when isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20,2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0)=0 then isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='USD' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) else isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20,2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) end) then '全部结算' else '部分结算' end) as zt from ch_fee_RecvApplication where GID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "') and (ISDELETE is null or ISDELETE=0) ";
statusObj = SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, sSQL, null);
if (statusObj == null)
{
sSourceState2 = "全部结算";
}
else
{
sSourceState2 = statusObj.ToString().Trim();
}
}
//说明是部分结算
if (iSourceState > 0 && sSourceState2 == "部分结算")
{
//更新ch_fee_RecvApplication的部分结算金额(撤销结算)
if (sCURRENCY == "RMB")
{
sSQL = " UPDATE ch_fee_RecvApplication SET "
+ " SETTLERMB=isnull((SETTLERMB-(case when isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0)<>0 then isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) else (case when isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20, 2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='USD' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0)<>0 then isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20, 2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='USD' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) else SETTLERMB end) end)),0) "
+ " FROM ch_fee_settle_link WHERE ch_fee_settle_link.RELATIONSUBID = ch_fee_RecvApplication.GID AND SETTLEID = '" + tempPaySettleID + "' and (ch_fee_RecvApplication.ISDELETE is null or ch_fee_RecvApplication.ISDELETE=0) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
else
{
sSQL = " UPDATE ch_fee_RecvApplication SET "
+ " SETTLEUSD=isnull((SETTLEUSD-(case when isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20, 2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0)<>0 then isnull((select CAST(sum(DOAMOUNT*EXCHANGERATE) as decimal(20, 2)) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='RMB' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) else (case when isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='USD' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0)<>0 then isnull((select sum(DOAMOUNT) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and CURRENCY='USD' and feename<>'汇率损益' and (ISDELETED is null or ISDELETED=0)),0) else SETTLEUSD end) end)),0) "
+ " FROM ch_fee_settle_link WHERE ch_fee_settle_link.RELATIONSUBID = ch_fee_RecvApplication.GID AND SETTLEID = '" + tempPaySettleID + "' and (ch_fee_RecvApplication.ISDELETE is null or ch_fee_RecvApplication.ISDELETE=0) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
//更新收费申请状态从已经结算更新为已审核(撤销结算)
string SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL = " UPDATE ch_fee_recvapplication SET "
+ "BILLSTATUS=(case when isnull(AMOUNTRMB,0)<>0 then (case when isnull(AMOUNTRMB,0)=isnull(SETTLERMB,0) then 4 else (case when isnull(SETTLERMB,0)=0 then 0 else 5 end) end) else (case when isnull(AMOUNTUSD,0)<>0 then (case when isnull(AMOUNTUSD,0)=isnull(SETTLEUSD,0) then 4 else (case when isnull(SETTLEUSD,0)=0 then 0 else 5 end) end)else 0 end) end)"
+ " from ch_fee_recvapplication where GID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "') and (ISDELETE is null or ISDELETE=0) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL, null);
//更新ch_fee_do的结算金额-部分结算金额(撤销结算)
sSQL = " update ch_fee_do set "
+ " DOAMOUNT=(case when DOAMOUNT-(select DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and feeid=D.feeid and (ISDELETED is null or ISDELETED=0))>0 then DOAMOUNT-(select DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and feeid=D.feeid and (ISDELETED is null or ISDELETED=0)) else isnull(AMOUNT,0) end) "
+ " from ch_fee_do as D where D.billno=(select top 1 billno from ch_fee_recvapplication where GID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "') and (ISDELETE is null or ISDELETE=0)) and (D.ISDELETED is null or D.ISDELETED=0)";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//更新ch_fee的结算金额-部分结算金额(撤销结算)
sSQL = " update ch_fee set "
+ " SETTLEMENT=isnull((SETTLEMENT-(select (case when AMOUNT-DOAMOUNT=0 then AMOUNT else DOAMOUNT end) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and feeid=D.GID and (ISDELETED is null or ISDELETED=0))),0) "
+ ",FEESTATUS=(case when SETTLEMENT-(select (case when AMOUNT-DOAMOUNT=0 then AMOUNT else DOAMOUNT end) as DOAMOUNT from ch_fee_do where billno = '" + tempBillNO + "' and feeid=D.GID and (ISDELETED is null or ISDELETED=0))=0 then 0 else 8 end) "
+ "from ch_fee as D where D.GID in (select feeid from ch_fee_do where billno = '" + tempBillNO + "' and (ISDELETED is null or ISDELETED=0))";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//更新收费申请相关的费用状态为激活状态(撤销结算)//更新收费申请费用明细状态未结算
string SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL = " UPDATE ch_fee_do SET ISDELETED = 0 FROM ch_fee_recvapplication WHERE ch_fee_recvapplication.BILLNO = ch_fee_do.BILLNO AND ch_fee_recvapplication.COMPANYID = ch_fee_do.COMPANYID AND ch_fee_recvapplication.GID = '" + tempPaySettleID + "' ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL, null);
}
else
{
//更新收费申请状态从已经结算更新为已审核(撤销结算)
string SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL = " UPDATE ch_fee_recvapplication SET BILLSTATUS=0,SETTLERMB=0,SETTLEUSD=0 FROM ch_fee_settle_link WHERE ch_fee_settle_link.RELATIONSUBID = ch_fee_recvapplication.GID AND SETTLEID = '" + tempPaySettleID + "' and (ch_fee_recvapplication.ISDELETE is null or ch_fee_recvapplication.ISDELETE=0) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL, null);
//更新ch_fee_do的结算金额-部分结算金额(撤销结算)
sSQL = " update ch_fee_do set "
+ " DOAMOUNT=AMOUNT "
+ "from ch_fee_do as D where D.billno=(select top 1 billno from ch_fee_recvapplication where (ISDELETE is null or ISDELETE=0) and GID=(select top 1 RELATIONSUBID from ch_fee_settle_link where SETTLEID = '" + tempPaySettleID + "')) and (D.ISDELETED is null or D.ISDELETED=0)";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
//更新收费申请相关的费用状态为激活状态(撤销结算),更新收费申请费用明细状态未结算
string SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL = " UPDATE ch_fee_do SET ISDELETED = 0 FROM ch_fee_recvapplication WHERE ch_fee_recvapplication.BILLNO = ch_fee_do.BILLNO AND ch_fee_recvapplication.COMPANYID = ch_fee_do.COMPANYID AND ch_fee_recvapplication.GID = '" + tempPaySettleID + "' ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL, null);
//还原已经进行收费结算的费用(撤销收费结算),先将费用表的结算信息更新到结算前状态
string SQL_UPDATE_CH_FEE_FOR_CANCELPAYSETTLE = " UPDATE ch_fee SET ch_fee.FEESTATUS = (CASE WHEN ch_fee_do.ORIGAMOUNT = ch_fee.SETTLEMENT THEN 0 ELSE 8 END), "
+ " ch_fee.SETTLEMENT = isnull((ISNULL(ch_fee.SETTLEMENT,0) - ch_fee_do.ORIGAMOUNT),0) "
+ " FROM ch_fee_do WHERE ch_fee.GID = ch_fee_do.FEEID AND ch_fee_do.BILLNO = '" + tempBillNO + "'" + tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_FOR_CANCELPAYSETTLE, null);
}
//更新ch_fee的结算金额-部分结算金额(撤销结算)
sSQL = " delete from ch_fee where GID in (select feeid from ch_fee_do where billno = '" + tempBillNO + "' and (ISDELETED is null or ISDELETED=0)) and feename='汇率损益'";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
else
{
//还原已经进行收费结算的费用(撤销收费结算),先将费用表的结算信息更新到结算前状态
string SQL_UPDATE_CH_FEE_FOR_CANCELPAYSETTLE = " UPDATE ch_fee SET ch_fee.FEESTATUS = (CASE WHEN ch_fee_do.ORIGAMOUNT = ch_fee.SETTLEMENT THEN 0 ELSE 8 END), "
+ " ch_fee.SETTLEMENT = isnull((ISNULL(ch_fee.SETTLEMENT,0) - ch_fee_do.ORIGAMOUNT),0) "
+ " FROM ch_fee_do WHERE ch_fee.GID = ch_fee_do.FEEID AND ch_fee_do.BILLNO = '" + tempBillNO + "'" + tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_FOR_CANCELPAYSETTLE, null);
}
//删除收费结算费用明细表(撤销收费结算)//删除FeeDo记录
string SQL_DELETE_CH_FEE_DO = " UPDATE ch_fee_do SET ISDELETED = 1,DELETEUSER = '" + tempOperator + "',DELETETIME = GETDATE() WHERE BILLNO = '" + tempBillNO + "' " + tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_CH_FEE_DO, null);
//更新主表,更新要删除的预收预收明细表中的预收支取用资料
string ssql = "update [ch_fee_advance_payment] set DOAMOUNT=isnull((DOAMOUNT-(select sum(DOAMOUNT) from ch_fee_advance_payment_detail where SETTLEBILLNO='" + tempBillNO + "' and LINKGID=ch_fee_advance_payment.GID and (ISDELETE=0 or ISDELETE is null))),0),ISFINISH=0 where GID in (select LINKGID from ch_fee_advance_payment_detail where SETTLEBILLNO='" + tempBillNO + "' and (ISDELETE=0 or ISDELETE is null))";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, ssql, null);
//删除预收预收明细表中的预收支取用资料
ssql = "delete from ch_fee_advance_payment_detail where SETTLEBILLNO='" + tempBillNO + "'";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, ssql, null);
//删除预收预收主表中的预收支资料
ssql = "delete from ch_fee_advance_payment where BILLNO='" + tempBillNO + "'";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, ssql, null);
//删除收费结算表(撤销收费结算)//删除FeeSettlement记录
string SQL_DELETE_CH_FEE_SETTLEMENT = " UPDATE ch_fee_settlement SET ISDELETE = 1,DELETEUSER = '" + tempOperator + "',DELETETIME = GETDATE() WHERE BILLNO = '" + tempBillNO + "' " + tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_CH_FEE_SETTLEMENT, null);
//删除合并结算关联信息表记录
string SQL_DELETE_CH_FEE_SETTLE_LINK = " DELETE FROM ch_fee_settle_link WHERE SETTLEID = '" + tempPaySettleID + "' ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_CH_FEE_SETTLE_LINK, null);
//事务提交
sqlTran.Commit();
iResult = 1;//状态为1表示插入成功
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
}
}