using System;
using System.Data;
using System.Data.SqlClient;
using WebSqlHelper;
using DSWeb.Models;
using System.Collections;
using System.Collections.Generic;
namespace DSWeb.EntityDA
{
public class FeePaySettleDA
{
//ch_fee_settlement
private const string PARM_FEE_SETTLEMENT_GID = "@gid";
private const string PARM_FEE_SETTLEMENT_BILL_NO = "@bill_no";
private const string PARM_FEE_SETTLEMENT_BILL_DATE = "@bill_date";
private const string PARM_FEE_SETTLEMENT_SETTLE_MODE = "@settle_mode";
private const string PARM_FEE_SETTLEMENT_CUSTOMER_NAME = "@customer_name";
private const string PARM_FEE_SETTLEMENT_SETTLE_TYPE = "@settle_type";
private const string PARM_FEE_SETTLEMENT_AMOUNT_RMB = "@amount_rmb";
private const string PARM_FEE_SETTLEMENT_ACCOUNT_RMB = "@account_rmb";
private const string PARM_FEE_SETTLEMENT_RATE = "@rate";
private const string PARM_FEE_SETTLEMENT_AMOUNT_USD = "@amount_usd";
private const string PARM_FEE_SETTLEMENT_ACCOUNT_USD = "@account_usd";
private const string PARM_FEE_SETTLEMENT_BILL_TYPE = "@bill_type";
private const string PARM_FEE_SETTLEMENT_VOUCHER_NO = "@voucher_no";
private const string PARM_FEE_SETTLEMENT_SETTLE_USER = "@settle_user";
private const string PARM_FEE_SETTLEMENT_SETTLE_TIME = "@settle_time";
private const string PARM_FEE_SETTLEMENT_AUDIT_USER = "@audit_user";
private const string PARM_FEE_SETTLEMENT_AUDIT_TIME = "@audit_time";
private const string PARM_FEE_SETTLEMENT_IS_EXPORT = "@is_export";
private const string PARM_FEE_SETTLEMENT_FINANCIAL_VOUCHER = "@financial_voucher";
private const string PARM_FEE_SETTLEMENT_BILL_STATUS = "@bill_status";
private const string PARM_FEE_SETTLEMENT_REMARK = "@remark";
private const string PARM_FEE_SETTLEMENT_IS_DELETE = "@is_delete";
private const string PARM_FEE_SETTLEMENT_DELETE_USER = "@delete_user";
private const string PARM_FEE_SETTLEMENT_DELETE_TIME = "@delete_time";
private const string PARM_FEE_SETTLEMENT_LIKE = "@like";
private const string PARM_FEE_SETTLEMENT_COMPANY_ID = "@company_id";
//ch_fee_settle_link
private const string PARM_FEE_SETTLE_LINK = "@gid";
//ch_fee_do
private const string PARM_FEE_DO_GID = "@gid";
private const string PARM_FEE_DO_BILL_NO = "@bill_no";
private const string PARM_FEE_DO_BSNO = "@bsno";
private const string PARM_FEE_DO_MBL_NO = "@mbl_no";
private const string PARM_FEE_DO_HBL_NO = "@nbl_no";
private const string PARM_FEE_DO_CUSTOMER_NAME = "@customer_name";
private const string PARM_FEE_DO_BSTYPE = "@bs_type";
private const string PARM_FEE_DO_FEE_ID = "@fee_id";
private const string PARM_FEE_DO_FEE_NAME = "@fee_name";
private const string PARM_FEE_DO_CURRENCY = "@currency";
private const string PARM_FEE_DO_AMOUNT = "@amount";
private const string PARM_FEE_DO_DO_AMOUNT = "@do_amount";
private const string PARM_FEE_DO_FEE_TYPE = "@fee_type";
private const string PARM_FEE_DO_CATEGORY = "@category";
private const string PARM_FEE_DO_BILL_STATUS = "@bill_status";
private const string PARM_FEE_DO_REMARK = "@remark";
private const string PARM_FEE_DO_EXCHANGE_RATE = "@exchange_rate";
private const string PARM_FEE_DO_COMPANY_ID = "@company_id";
private const string PARM_FEE_DO_DELETE_USER = "@delete_user";
private const string PARM_FEE_DO_ORIG_AMOUNT = "@orig_amount";
//ch_fee
private const string PARM_FEE_GID = "@gid";
private const string PARM_FEE_STATUS = "@fee_status";
private const string PARM_FEE_SETTLEMENT = "@settlement";
private const string PARM_FEE_CHEQUE_NUM = "@cheque_num";
//fee_gain
private const string PARM_FEE_GAIN_STL_CR_RMB = "@stl_crrmb";
private const string PARM_FEE_GAIN_STL_CR_USD = "@stl_crusd";
private const string PARM_FEE_GAIN_STL_CR_TTL = "@stl_crttl";
private const string PARM_FEE_GAIN_BSNO = "@bsno";
//run_fee_settlement
private const string PARM_RUN_FEE_SETTLEMENT_GID = "@gid";
private const string PARM_RUN_FEE_SETTLEMENT_BILL_NO = "@bill_no";
private const string PARM_RUN_FEE_SETTLEMENT_APPLICANT = "@applicant";
private const string PARM_RUN_FEE_SETTLEMENT_SETTLE_USER = "@settle_user";
private const string PARM_RUN_FEE_SETTLEMENT_REMARK = "@remark";
private const string PARM_RUN_FEE_SETTLEMENT_PAYSETTLE_ID = "@paysettle_id";
private const string PARM_RUN_FEE_SETTLEMENT_COMPANY_ID = "@company_id";
//run_fee_do
private const string PARM_RUN_FEE_DO_BILL_NO = "@bill_no";
private const string PARM_RUN_FEE_DO_RUN_SETTLE_ID = "@runsettle_id";
//结算关联表ch_fee_settle_link
private const string PARM_CH_FEE_SETTLE_LINK = "@gid";
private const string PARM_CH_FEE_SETTLE_SETTLE_ID = "@settle_id";
private const string PARM_CH_FEE_SETTLE_TYPE = "@type";
private const string PARM_CH_FEE_SETTLE_RELATION_SUB_ID = "@relation_sub_id";
private const string PARM_CH_FEE_SETTLE_CREATE_USER = "@create_user";
private const string PARM_CH_FEE_SETTLE_CREATE_TIME = "@create_time";
//ch_fee_payapplication
private const string PARM_CH_FEE_PAY_APP_GID = "@gid";
private const string SQL_SELECT_GET_SERVERTIME = " SELECT GETDATE() ";//获取服务端时间
private const string SQL_SELECT_LAST_BILL_NO = " SELECT TOP 1 BILLNO FROM ch_fee_settlement WHERE BILLNO LIKE @like AND COMPANYID = @company_id ORDER BY BILLNO DESC ";
private const string SQL_INSERT_FEE_DO = " INSERT INTO ch_fee_do(GID,BILLNO,BSNO,MBLNO,HBLNO,CUSTOMERNAME,BSTYPE,FEEID,FEENAME,CURRENCY,AMOUNT,DOAMOUNT,FEETYPE,CATEGORY,BILLSTATUS,REMARK,EXCHANGERATE,COMPANYID,ORIGAMOUNT) "
+ " VALUES(@gid,@bill_no,@bsno,@mbl_no,@nbl_no,@customer_name,@bs_type,@fee_id,@fee_name,@currency,@amount,@do_amount,@fee_type,@category,@bill_status,@remark,@exchange_rate,@company_id,@orig_amount) ";
private const string SQL_UPDATE_FEE = " UPDATE ch_fee SET ch_fee.SETTLEMENT = ch_fee.SETTLEMENT+@settlement,ch_fee.FEESTATUS = C.FEESTATUS,ch_fee.CHEQUENUM = @cheque_num "
+ " FROM "
+ " (SELECT A.GID,CASE WHEN B.AMOUNT < A.AMOUNT THEN CASE WHEN B.AMOUNT+A.SETTLEMENT = A.AMOUNT THEN 9 ELSE 8 END ELSE "
+ " CASE WHEN B.AMOUNT = A.AMOUNT THEN 9 END END as FEESTATUS FROM ch_fee as A INNER JOIN ch_fee_do as B ON A.GID = B.FEEID WHERE B.GID = @gid) as C "
+ " WHERE C.GID = ch_fee.GID ";
//合计更新利润信息表
private const string SQL_UPDATE_FEE_GAIN = " UPDATE fee_gain SET STLCRRMB = ISNULL(STLCRRMB,0) + @stl_crrmb,STLCRUSD = ISNULL(STLCRUSD,0) + @stl_crusd,STLCRTTL = ISNULL(STLCRTTL,0) + @stl_crttl WHERE BSNO = @bsno ";
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 FROM ch_fee_settlement WHERE GID = @gid";
private const string SQL_VALID_CH_FEE_SETTLEMENT_BY_RUNFEEDO = " SELECT COUNT(A.GID)-SUM(CASE WHEN C.AMOUNT = C.SETTLEMENT THEN -1 ELSE CASE WHEN (C.AMOUNT - C.SETTLEMENT) < A.ORIGAMOUNT THEN -1 ELSE "
+ " CASE WHEN (C.AMOUNT - C.SETTLEMENT) >= A.ORIGAMOUNT THEN 1 END END END) FROM run_fee_do as A INNER JOIN run_fee_settlement as B "
+ " ON A.RUNSETTLEID = B.GID INNER JOIN ch_fee as C ON A.FEEID = C.GID WHERE B.GID = @gid ";
//删除运行结算信息表
private const string SQL_DELETE_RUN_FEE_SETTLEMENT_BY_GID = " DELETE FROM run_fee_settlement WHERE GID = @gid ";
//删除运行结算明细表信息
private const string SQL_DELETE_RUN_FEE_DO_BY_RUNSETTLE_ID = " DELETE FROM run_fee_do WHERE RUNSETTLEID = @runsettle_id ";
//将运行结算明细表中的费用明细插入到ch_fee_do
private const string SQL_INSERT_CH_FEE_FEE_DO_RUN = " INSERT INTO ch_fee_do(GID,BILLNO,BSNO,MBLNO,HBLNO,CUSTOMERNAME,BSTYPE,FEEID,FEENAME,CURRENCY,AMOUNT,DOAMOUNT,FEETYPE,CATEGORY,BILLSTATUS,"
+ " REMARK,CREATETIME,EXCHANGERATE,ORIGCURRENCY,ORIGAMOUNT,INVOICESETTLENO,COMPANYID) "
+ " SELECT newid(),@bill_no,BSNO,MBLNO,HBLNO,CUSTOMERNAME,BSTYPE,FEEID,FEENAME,CURRENCY,AMOUNT,DOAMOUNT,FEETYPE,CATEGORY,BILLSTATUS,REMARK,GETDATE(), "
+ " EXCHANGERATE,ORIGCURRENCY,ORIGAMOUNT,INVOICESETTLENO,COMPANYID FROM run_fee_do WHERE RUNSETTLEID = @runsettle_id ";
private const string SQL_UPDATE_CH_FEE_BY_RUN_FEE_DO = " UPDATE ch_fee SET ch_fee.SETTLEMENT = ISNULL(ch_fee.SETTLEMENT,0) + ch_fee_do.ORIGAMOUNT,ch_fee.FEESTATUS = 9 FROM ch_fee_do WHERE ch_fee.GID = ch_fee_do.FEEID AND ch_fee_do.BILLNO = @bill_no AND ch_fee_do.COMPANYID = @company_id ";
private const string SQL_SELECT_CH_FEE_PAY_APPLICATION_BILLNO = " SELECT BILLNO FROM ch_fee_settlement WHERE GID = @gid ";
//将已保存的未结算付费插入到费用结算表
private const string SQL_INSERT_CH_FEE_SETTLEMENT_RUN = "INSERT INTO ch_fee_settlement(GID, BILLNO, BILLDATE, SETTLEMODE, CUSTOMERNAME, SETTLETYPE, AMOUNTRMB, ACCOUNTRMB, RATE, AMOUNTUSD, ACCOUNTUSD,"
+ " BILLTYPE, VOUCHERNO, SETTLEUSER, SETTLETIME, ISEXPORT, FINANCIALVOUCHER, BILLSTATUS, REMARK,COMPANYID)"
+ " SELECT @paysettle_id,@bill_no,GETDATE(),SETTLEMODE,CUSTOMERNAME,SETTLETYPE,AMOUNTRMB,ACCOUNTRMB,RATE,AMOUNTUSD,ACCOUNTUSD,"
+ " BILLTYPE,VOUCHERNO,@settle_user,GETDATE(),ISEXPORT,FINANCIALVOUCHER,BILLSTATUS,REMARK,@company_id "
+ " FROM run_fee_settlement WHERE GID = @gid ";
//还原已经进行付费结算的费用(撤销付费结算)
private const 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 = @bill_no AND COMPANYID = @company_id ";
//删除付费结算费用明细表(撤销付费结算)
private const string SQL_DELETE_CH_FEE_DO = " UPDATE ch_fee_do SET ISDELETED = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no AND COMPANYID = @company_id ";
//删除付费结算表(撤销付费结算)
private const string SQL_DELETE_CH_FEE_SETTLEMENT = " UPDATE ch_fee_settlement SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no AND COMPANYID = @company_id ";
//获取付费结算来源
private const string SQL_VALID_PAY_SETTLE_SOURCE = " SELECT COUNT(*) FROM ch_fee_settle_link WHERE SETTLEID = @settle_id ";
//校验费用是否满足付费结算金额
private const string SQL_VALID_CH_FEE_SETTLEMENT_FOR_MERGE = " SELECT COUNT(A.GID)-SUM(CASE WHEN C.AMOUNT = C.SETTLEMENT THEN -1 ELSE CASE WHEN (C.AMOUNT - C.SETTLEMENT) < A.ORIGAMOUNT THEN -1 ELSE "
+ " CASE WHEN (C.AMOUNT - C.SETTLEMENT) >= A.ORIGAMOUNT THEN 1 END END END) FROM ch_fee_do as A INNER JOIN ch_fee_payapplication as B "
+ " ON A.BILLNO = B.BILLNO AND A.COMPANYID = B.COMPANYID INNER JOIN ch_fee as C ON A.FEEID = C.GID INNER JOIN ch_fee_settle_link as D ON B.GID = D.RELATIONSUBID WHERE D.SETTLEID = @settle_id ";
//将所有合并付费申请费用明细插入付费结算
private const string SQL_INSERT_CH_FEE_FEE_DO_FOR_MERGE = " INSERT INTO ch_fee_do(GID,BILLNO,BSNO,MBLNO,HBLNO,CUSTOMERNAME,BSTYPE,FEEID,FEENAME,CURRENCY,AMOUNT,DOAMOUNT,FEETYPE,CATEGORY,BILLSTATUS,"
+ " REMARK,CREATETIME,EXCHANGERATE,ORIGCURRENCY,ORIGAMOUNT,INVOICESETTLENO,COMPANYID) "
+ " SELECT newid(),@bill_no,A.BSNO,A.MBLNO,A.HBLNO,A.CUSTOMERNAME,A.BSTYPE,A.FEEID,A.FEENAME,A.CURRENCY,A.AMOUNT,A.DOAMOUNT,A.FEETYPE,A.CATEGORY,A.BILLSTATUS,A.REMARK,GETDATE(), "
+ " A.EXCHANGERATE,A.ORIGCURRENCY,A.ORIGAMOUNT,A.INVOICESETTLENO,A.COMPANYID FROM ch_fee_do AS A INNER JOIN ch_fee_payapplication as B ON A.BILLNO = B.BILLNO AND A.COMPANYID = B.COMPANYID "
+ " INNER JOIN ch_fee_settle_link as C ON B.GID = C.RELATIONSUBID WHERE C.SETTLEID = @settle_id ";
//根据付费申请费用明细更新费用表
private const string SQL_UPDATE_CH_FEE_BY_CH_FEE_DO_FOR_MERGE = " UPDATE ch_fee SET ch_fee.SETTLEMENT = ISNULL(ch_fee.SETTLEMENT,0) + ch_fee_do.ORIGAMOUNT "
+ " FROM ch_fee_do WHERE ch_fee.GID = ch_fee_do.FEEID AND ch_fee_do.BILLNO = @bill_no AND ch_fee_do.COMPANYID = @company_id ";
//建立付费结算关联信息(生成付费结算和付费申请关联关系)
private const string SQL_INSERT_CH_FEE_SETTLE_LINK = " INSERT INTO ch_fee_settle_link(GID,TYPE,SETTLEID,RELATIONSUBID,CREATEUSER,CREATETIME) "
+ " VALUES(newid(),@type,@settle_id,@relation_sub_id,@create_user,GETDATE()) ";
//更新付费申请状态为已经结算
private const string SQL_UPDATE_CH_FEE_PAY_APP_SETTLED = " UPDATE ch_fee_payapplication SET BILLSTATUS = 4 FROM ch_fee_settle_link WHERE ch_fee_settle_link.RELATIONSUBID = ch_fee_payapplication.GID AND SETTLEID = @settle_id ";
//更新付费申请相关的费用状态
private const string SQL_UPDATE_CH_FEE_DO_APP_SETTLED = " UPDATE ch_fee_do SET ISDELETED = 1 FROM ch_fee_payapplication WHERE ch_fee_payapplication.BILLNO = ch_fee_do.BILLNO AND ch_fee_payapplication.COMPANYID = ch_fee_do.COMPANYID AND ch_fee_payapplication.GID = @gid ";
//更新付费申请状态从已经结算更新为已审核(撤销结算)
//private const string SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL = " UPDATE ch_fee_payapplication SET BILLSTATUS = 0 FROM ch_fee_settle_link WHERE ch_fee_settle_link.RELATIONSUBID = ch_fee_payapplication.GID AND SETTLEID = @settle_id ";
//更新付费申请相关的费用状态为激活状态(撤销结算)
private const string SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL = " UPDATE ch_fee_do SET ISDELETED = 0 FROM ch_fee_payapplication WHERE ch_fee_payapplication.BILLNO = ch_fee_do.BILLNO AND ch_fee_payapplication.COMPANYID = ch_fee_do.COMPANYID AND ch_fee_payapplication.GID = @gid ";
//删除合并结算关联信息表记录
private const string SQL_DELETE_CH_FEE_SETTLE_LINK = " DELETE FROM ch_fee_settle_link WHERE SETTLEID = @settle_id ";
#region 插入付费结算
///
/// 插入付费结算
///
/// 付费结算实体类
/// 值1-插入完成 值-1插入失败
public int InsertFeePaySettle(IList tempFeeDoEntities, FeeSettlementEntity tempFeeSettlementEntity)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string strLastBillNo = "";
string strNewBillNo = "";
//先获取SQL SERVER 数据库端服务器时间
DateTime nowServerDateTime = (DateTime)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_GET_SERVERTIME, null);
if (nowServerDateTime == DateTime.MinValue)
{
throw new Exception("未正常获取到服务端时间");
}
//获取分公司的票号头字符
string tempBANKSHEAD = (String)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, "SELECT TOP 1 BANKSHEAD FROM company WHERE GID='" + tempFeeSettlementEntity.CompanyID.ToString().Trim() + "'", null) as string;
//生成申请编号起始串
string strBillNoProfix = CreateBeginTitleCode(nowServerDateTime, tempBANKSHEAD+"CR", 4);
SqlParameter[] getLastBillParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_SETTLEMENT_LIKE, SqlDbType.VarChar, 200),
new SqlParameter(PARM_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36)
};
getLastBillParms[0].Value = strBillNoProfix + "%";
getLastBillParms[1].Value = tempFeeSettlementEntity.CompanyID;
//获取最大的编号
string tempLastBillNo = (String)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_LAST_BILL_NO, getLastBillParms) as string;
strLastBillNo = tempLastBillNo ?? "";
//生成付费申请编号
strNewBillNo = CreateSettleCode(nowServerDateTime, tempBANKSHEAD+"CR", 4, strLastBillNo);
foreach (FeeDoEntity feeDoEntity in tempFeeDoEntities)
{
SqlParameter[] doParms = new SqlParameter[]{
new SqlParameter(PARM_FEE_DO_GID, SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_DO_BILL_NO, SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_BSNO, SqlDbType.VarChar,100),
new SqlParameter(PARM_FEE_DO_MBL_NO,SqlDbType.VarChar,30),
new SqlParameter(PARM_FEE_DO_HBL_NO,SqlDbType.VarChar,30),
new SqlParameter(PARM_FEE_DO_CUSTOMER_NAME,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_BSTYPE,SqlDbType.Int),
new SqlParameter(PARM_FEE_DO_FEE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_DO_FEE_NAME,SqlDbType.VarChar,100),
new SqlParameter(PARM_FEE_DO_CURRENCY,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_AMOUNT,SqlDbType.Decimal,40),
new SqlParameter(PARM_FEE_DO_DO_AMOUNT,SqlDbType.Decimal,40),
new SqlParameter(PARM_FEE_DO_FEE_TYPE,SqlDbType.Int),
new SqlParameter(PARM_FEE_DO_CATEGORY,SqlDbType.Int),
new SqlParameter(PARM_FEE_DO_BILL_STATUS,SqlDbType.Int),
new SqlParameter(PARM_FEE_DO_REMARK,SqlDbType.VarChar,1024),
new SqlParameter(PARM_FEE_DO_EXCHANGE_RATE,SqlDbType.Decimal,21),
new SqlParameter(PARM_FEE_DO_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_DO_ORIG_AMOUNT,SqlDbType.Decimal,20)
};
doParms[0].Value = feeDoEntity.GID;
doParms[1].Value = strNewBillNo;
doParms[2].Value = feeDoEntity.BSNO;
doParms[3].Value = feeDoEntity.MBLNO;
doParms[4].Value = feeDoEntity.HBLNO;
doParms[5].Value = feeDoEntity.CustomerName;
doParms[6].Value = feeDoEntity.BSType;
doParms[7].Value = feeDoEntity.FeeID;
doParms[8].Value = feeDoEntity.FeeName;
doParms[9].Value = feeDoEntity.Currency;
doParms[10].Value = feeDoEntity.OriginalFeeCSTL;
doParms[11].Value = feeDoEntity.DoAmount;
doParms[12].Value = feeDoEntity.FeeType;
doParms[13].Value = feeDoEntity.Category;
doParms[14].Value = feeDoEntity.BillStatus;
doParms[15].Value = feeDoEntity.Remark;
doParms[16].Value = feeDoEntity.ExchangeRate;
doParms[17].Value = feeDoEntity.CompanyID;
doParms[18].Value = feeDoEntity.OriginalFeeCSTL;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_FEE_DO, doParms);
//
SqlParameter[] feeParms = new SqlParameter[]{
new SqlParameter(PARM_FEE_DO_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_CHEQUE_NUM,SqlDbType.VarChar,200)
};
feeParms[0].Value = feeDoEntity.GID;
feeParms[1].Value = feeDoEntity.OriginalFeeCSTL;
if (feeDoEntity.Currency.ToString().Trim() == "RMB")
{
feeParms[2].Value = tempFeeSettlementEntity.ChequeNo;
}
else
{
feeParms[2].Value = tempFeeSettlementEntity.ChequeNoUsd;
}
//private const string SQL_UPDATE_FEE = " UPDATE ch_fee SET ch_fee.SETTLEMENT = ch_fee.SETTLEMENT+@settlement,ch_fee.FEESTATUS = C.FEESTATUS,ch_fee.CHEQUENUM = @cheque_num "
// + " FROM "
// + " (SELECT A.GID,CASE WHEN B.AMOUNT < A.AMOUNT THEN CASE WHEN B.AMOUNT+A.SETTLEMENT = A.AMOUNT THEN 9 ELSE 8 END ELSE "
// + " CASE WHEN B.AMOUNT = A.AMOUNT THEN 9 END END as FEESTATUS FROM ch_fee as A INNER JOIN ch_fee_do as B ON A.GID = B.FEEID WHERE B.GID = @gid) as C "
// + " WHERE C.GID = ch_fee.GID ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_FEE, feeParms);
//更新合计利润综合信息
SqlParameter[] updateFeeGainParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_GAIN_STL_CR_RMB,SqlDbType.Decimal,21),
new SqlParameter(PARM_FEE_GAIN_STL_CR_USD,SqlDbType.Decimal,21),
new SqlParameter(PARM_FEE_GAIN_STL_CR_TTL,SqlDbType.Decimal,21),
new SqlParameter(PARM_FEE_GAIN_BSNO,SqlDbType.VarChar,100)
};
if (feeDoEntity.Currency == "RMB")
{
updateFeeGainParms[0].Value = feeDoEntity.OriginalFeeCSTL;
updateFeeGainParms[1].Value = 0;
updateFeeGainParms[2].Value = feeDoEntity.OriginalFeeCSTL;
updateFeeGainParms[3].Value = feeDoEntity.BSNO;
}
else if(feeDoEntity.Currency == "USD")
{
updateFeeGainParms[0].Value = 0;
updateFeeGainParms[1].Value = feeDoEntity.OriginalFeeCSTL;
updateFeeGainParms[2].Value = decimal.Parse(C1Round(double.Parse((feeDoEntity.OriginalFeeCSTL * feeDoEntity.ExchangeRate).ToString()),2).ToString());
updateFeeGainParms[3].Value = feeDoEntity.BSNO;
}
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_FEE_GAIN, updateFeeGainParms);
}
//
SqlParameter[] settleParms = new SqlParameter[]{
new SqlParameter(PARM_FEE_SETTLEMENT_GID, SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_NO, SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_SETTLE_MODE, SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_CUSTOMER_NAME,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_SETTLE_TYPE,SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_AMOUNT_RMB,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_SETTLEMENT_ACCOUNT_RMB,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_RATE,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_SETTLEMENT_AMOUNT_USD,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_SETTLEMENT_ACCOUNT_USD,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_TYPE,SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_VOUCHER_NO,SqlDbType.VarChar,50),
new SqlParameter(PARM_FEE_SETTLEMENT_SETTLE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_IS_EXPORT,SqlDbType.Bit),
new SqlParameter(PARM_FEE_SETTLEMENT_FINANCIAL_VOUCHER,SqlDbType.VarChar,50),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_STATUS,SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_REMARK,SqlDbType.VarChar,1024),
new SqlParameter(PARM_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter("@ACCOUNTS_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@ACCOUNTS_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@ACCOUNTS_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@PREPAY_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@PREPAY_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@PREPAY_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@AHSR_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@AHSR_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@AHSR_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@FINANCIAL_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@FINANCIAL_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@FINANCIAL_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@ADVANCE_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@ADVANCE_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@ADVANCE_MONEY", SqlDbType.Decimal,9)
};
settleParms[0].Value = tempFeeSettlementEntity.GID;
settleParms[1].Value = strNewBillNo;
settleParms[2].Value = tempFeeSettlementEntity.SettleMode;
settleParms[3].Value = tempFeeSettlementEntity.CustomerName;
settleParms[4].Value = tempFeeSettlementEntity.SettleType;
settleParms[5].Value = tempFeeSettlementEntity.AmountRMB;
settleParms[6].Value = tempFeeSettlementEntity.AccountRMB;
settleParms[7].Value = tempFeeSettlementEntity.Rate;
settleParms[8].Value = tempFeeSettlementEntity.AmountUSD;
settleParms[9].Value = tempFeeSettlementEntity.AccountUSD;
settleParms[10].Value = tempFeeSettlementEntity.BillType;
settleParms[11].Value = tempFeeSettlementEntity.VoucherNO;
settleParms[12].Value = tempFeeSettlementEntity.SettleUser;
settleParms[13].Value = tempFeeSettlementEntity.IsExport;
settleParms[14].Value = tempFeeSettlementEntity.FinancialVoucher;
settleParms[15].Value = tempFeeSettlementEntity.BillStatus;
settleParms[16].Value = tempFeeSettlementEntity.Remark;
settleParms[17].Value = tempFeeSettlementEntity.CompanyID;
settleParms[18].Value = tempFeeSettlementEntity.ACCOUNTS_CURRENCY;//记账资料_币别
settleParms[19].Value = tempFeeSettlementEntity.ACCOUNTS_RATE;//记账资料_汇率
settleParms[20].Value = tempFeeSettlementEntity.ACCOUNTS_MONEY;//记账资料_金额
settleParms[21].Value = tempFeeSettlementEntity.PREPAY_CURRENCY;//预付支资料_币别
settleParms[22].Value = tempFeeSettlementEntity.PREPAY_RATE;//预付支资料_汇率
settleParms[23].Value = tempFeeSettlementEntity.PREPAY_MONEY;//预付支资料_金额
settleParms[24].Value = tempFeeSettlementEntity.AHSR_CURRENCY;//实付支资料_币别
settleParms[25].Value = tempFeeSettlementEntity.AHSR_RATE;//实付支资料_汇率
settleParms[26].Value = tempFeeSettlementEntity.AHSR_MONEY;//实付支资料_金额
settleParms[27].Value = tempFeeSettlementEntity.FINANCIAL_CURRENCY;//财务费用_币别
settleParms[28].Value = tempFeeSettlementEntity.FINANCIAL_RATE;//财务费用_汇率
settleParms[29].Value = tempFeeSettlementEntity.FINANCIAL_MONEY;//财务费用_金额
settleParms[30].Value = tempFeeSettlementEntity.ADVANCE_CURRENCY;//预付支取用资料_币别
settleParms[31].Value = tempFeeSettlementEntity.ADVANCE_RATE;//预付支取用资料_汇率
settleParms[32].Value = tempFeeSettlementEntity.ADVANCE_MONEY;//预付支取用资料_金额
//
string SETTLETIME = tempFeeSettlementEntity.SettleTime.ToString().Trim().IndexOf("0001") > -1 ? "GETDATE()," : "'" + tempFeeSettlementEntity.SettleTime.ToString().Trim() + "',";
string SETTLETIME_NAME = tempFeeSettlementEntity.SettleTime.ToString().Trim().IndexOf("0001") > -1 ? "SETTLETIME," : "SETTLETIME,";
string SQL_INSERT_FEE_SETTLEMENT = String.Format("INSERT INTO ch_fee_settlement(GID, BILLNO, BILLDATE, SETTLEMODE, CUSTOMERNAME, SETTLETYPE, AMOUNTRMB, ACCOUNTRMB, RATE, AMOUNTUSD, ACCOUNTUSD, BILLTYPE, VOUCHERNO, SETTLEUSER, {0} ISEXPORT, FINANCIALVOUCHER, BILLSTATUS, REMARK,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)"
+ " VALUES(@gid,@bill_no,GETDATE(),@settle_mode,@customer_name,@settle_type,@amount_rmb,@account_rmb,@rate,@amount_usd,@account_usd,@bill_type,@voucher_no,@settle_user, {1}@is_export,@financial_voucher,@bill_status,@remark,@company_id,@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)", SETTLETIME_NAME, SETTLETIME);
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_FEE_SETTLEMENT, settleParms);
//事务提交
sqlTran.Commit();
iResult = 1;//状态为1表示插入成功
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
#region 生成付费结算编号起始串
///
/// 生成付费结算编号起始串
///
/// 当前服务器时间
/// 结算编号起始串头字母
/// 结算编号累计编码长度
/// 返回结算编号起始串
private string CreateBeginTitleCode(DateTime tempNowDateTime, string tempBeginTitle, int tempCodeLength)
{
int CodeLength = tempCodeLength;
string strFeeType = tempBeginTitle;
//解析当前时间
DateTime nowDateTime = tempNowDateTime;
string strYear = nowDateTime.Year.ToString();
string strMonth = nowDateTime.Month.ToString().Length < 2 ? "0" + nowDateTime.Month.ToString() : nowDateTime.Month.ToString();
//获取年+月组合的字符串
string strDateTime = strYear + strMonth;
//将申请编号抬头+日期作为结算编号
string strCodePrefix = strFeeType + strDateTime;
return strCodePrefix;
}
#endregion
#region 生成付费结算编号
///
/// 生成付费结算编号
///
/// 当前服务器时间
/// 结算编号起始串头字母
/// 结算编号累计编码长度
/// 最大结算编号
/// 返回新的付费结算编号
private string CreateSettleCode(DateTime tempNowDateTime, string tempBeginTitle, int tempCodeLength, string tempLastBillNo)
{
int CodeLength = tempCodeLength;
string strSettleCode = "";
string strFeeType = tempBeginTitle;
//解析当前时间
DateTime nowDateTime = tempNowDateTime;
string strYear = nowDateTime.Year.ToString();
string strMonth = nowDateTime.Month.ToString().Length < 2 ? "0" + nowDateTime.Month.ToString() : nowDateTime.Month.ToString();
//获取年+月组合的字符串
string strDateTime = strYear + strMonth;
//将结算编号抬头+日期作为结算编号
string strCodePrefix = strFeeType + strDateTime;
//string strSql = "SELECT TOP 1 BILLNO FROM ch_fee_payapplication WHERE BILLNO LIKE '" + strFeeType + strDateTime + "%' ORDER BY BILLNO DESC";
//DataTable billTable = feeDoDA.GetExcuteSql(strSql).Tables[0];
ArrayList codeArg = new ArrayList();
for (int i = 0; i < CodeLength; i++)
{
codeArg.Add(0);
}
string strCode = "";//编号
if (tempLastBillNo != "")
{
string oldCode = tempLastBillNo;
if (oldCode.IndexOf(strCodePrefix) >= 0)
{
oldCode = oldCode.Substring(strCodePrefix.Length, oldCode.Length - strCodePrefix.Length);
if (oldCode.Length == CodeLength)
{
for (int i = 0; i < oldCode.Length; i++)
{
if (oldCode[i].ToString().IndexOf("0") == 0)
{
oldCode = oldCode.Remove(i, 1);
i = -1;
continue;
}
else
{
break;
}
}
}
}
int newNum = int.Parse(oldCode);
newNum = newNum + 1;
string strNewNum = newNum.ToString();
int codeArgCount = codeArg.Count - 1;
for (int i = strNewNum.Length - 1; i >= 0; i--)
{
codeArg[codeArgCount] = strNewNum[i];
codeArgCount--;
}
//for (int i = 0; i < strNewNum.Length; i++)
//{
// codeArg[i] = strNewNum[i];
//}
}
else
{
int iOld = int.Parse(codeArg[codeArg.Count - 1].ToString());
codeArg[codeArg.Count - 1] = (iOld + 1).ToString();
}
for (int i = 0; i < codeArg.Count; i++)
{
strCode += codeArg[i].ToString();
}
strSettleCode = strFeeType + strDateTime + strCode;
return strSettleCode;
}
#endregion
#region 金额四舍五入
///
/// 金额四舍五入
///
/// 金额值
/// 小数点后位数
/// 返回Double型四舍五入金额
public double C1Round(double value, int digit)
{
double vt = Math.Pow(10, digit);
double vx = value * vt;
vx += 0.5;
return (Math.Floor(vx) / vt);
}
#endregion
#region 通过SQL语句获取数据
///
/// 通过SQL语句获取数据
///
/// 要执行查询的SQL语句
/// 返回DataSet数据
public DataSet GetDataSetBySql(string strSql)
{
DataSet dataSet = new DataSet();
dataSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql);
return dataSet;
}
#endregion
#region 通过惟一主键获取付费结算信息
///
/// 通过惟一主键获取付费结算信息
///
/// 费用结算GID
/// 返回实体类FeeSettlementEntity
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);
}
}
}
catch (Exception execError)
{
throw execError;
}
}
return feeSettlementEntity;
}
#endregion
public string GetBillNoByPaySettleID(string tempPaySettleID)
{
string result = "";
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
SqlParameter parm = new SqlParameter(PARM_FEE_SETTLEMENT_GID, SqlDbType.VarChar, 36);
parm.Value = tempPaySettleID;
result = (String)SqlHelper.ExecuteScalar(conn, CommandType.Text, SQL_SELECT_CH_FEE_PAY_APPLICATION_BILLNO, parm) as String ?? "";
}
catch (Exception error)
{
throw error;
}
}
return result;
}
#region 将已保存的未结算的付费进行结算
///
/// 将已保存的未结算的付费进行结算
///
/// 付费结算GID
/// 运行结算GID
/// 操作人GID
/// 备注
/// 分公司GID
/// 值1表示结算成功 值不等于1表示结算失败
public int PostSavePaySettle(string tempFeeSettleID, string tempRunSettleID, string tempOperator, string tempRemark, string tempCompanyID)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string strLastBillNo = "";
string strNewBillNo = "";
//先校验费用申请金额是否满足申请要求
int validState = 0;
//先校验是否费用已经做了申请
SqlParameter[] validFeeParms = new SqlParameter[]{
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_GID,SqlDbType.VarChar,36)
};
validFeeParms[0].Value = tempRunSettleID;
validState = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_VALID_CH_FEE_SETTLEMENT_BY_RUNFEEDO, validFeeParms);
if (validState == 0)
{
//先获取SQL SERVER 数据库端服务器时间
DateTime nowServerDateTime = (DateTime)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_GET_SERVERTIME, null);
if (nowServerDateTime == DateTime.MinValue)
{
throw new Exception("未正常获取到服务端时间");
}
//获取分公司的票号头字符
string tempBANKSHEAD = (String)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, "SELECT TOP 1 BANKSHEAD FROM company WHERE GID='" + tempCompanyID.Trim() + "'", null) as string;
//生成申请编号起始串
string strBillNoProfix = CreateBeginTitleCode(nowServerDateTime, tempBANKSHEAD+"CR", 4);
SqlParameter[] getLastBillParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_SETTLEMENT_LIKE, SqlDbType.VarChar, 200),
new SqlParameter(PARM_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36)
};
getLastBillParms[0].Value = strBillNoProfix + "%";
getLastBillParms[1].Value = tempCompanyID;
//获取最大的编号
string tempLastBillNo = (String)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_LAST_BILL_NO, getLastBillParms) as string;
strLastBillNo = tempLastBillNo ?? "";
//生成付费申请编号
strNewBillNo = CreateSettleCode(nowServerDateTime, tempBANKSHEAD+"CR", 4, strLastBillNo);
SqlParameter[] insertSettleParms = new SqlParameter[]{
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_PAYSETTLE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_GID,SqlDbType.VarChar,36),
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_SETTLE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_REMARK,SqlDbType.VarChar,1024),
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36)
};
insertSettleParms[0].Value = tempFeeSettleID;
insertSettleParms[1].Value = tempRunSettleID;
insertSettleParms[2].Value = strNewBillNo;
insertSettleParms[3].Value = tempOperator;
insertSettleParms[4].Value = tempRemark;
insertSettleParms[5].Value = tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_CH_FEE_SETTLEMENT_RUN, insertSettleParms);
//
SqlParameter[] insertFeeDoParms = new SqlParameter[] {
new SqlParameter(PARM_RUN_FEE_DO_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_RUN_FEE_DO_RUN_SETTLE_ID,SqlDbType.VarChar,36)
};
insertFeeDoParms[0].Value = strNewBillNo;
insertFeeDoParms[1].Value = tempRunSettleID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_CH_FEE_FEE_DO_RUN, insertFeeDoParms);
//
//将运行结算主表中的RMB支票号插入到ch_fee
string SQL_UPDATE_CH_FEE_BY_RUN_FEE_SETTLEMENT_RMB = " update ch_fee set CHEQUENUM=(select top 1 CHEQUENO from run_fee_settlement where gid=@runsettle_id) where gid in (SELECT feeid FROM run_fee_do WHERE RUNSETTLEID = @runsettle_id and CURRENCY='RMB') and CURRENCY='RMB'";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_BY_RUN_FEE_SETTLEMENT_RMB, insertFeeDoParms);
//
//将运行结算主表中的USD支票号插入到ch_fee
string SQL_UPDATE_CH_FEE_BY_RUN_FEE_SETTLEMENT_USD = " update ch_fee set CHEQUENUM=(select top 1 CHEQUENOUSD from run_fee_settlement where gid=@runsettle_id) where gid in (SELECT feeid FROM run_fee_do WHERE RUNSETTLEID = @runsettle_id and CURRENCY='USD') and CURRENCY='USD'";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_BY_RUN_FEE_SETTLEMENT_USD, insertFeeDoParms);
//
SqlParameter[] updateFeeParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_COMPANY_ID,SqlDbType.VarChar,36)
};
updateFeeParms[0].Value = strNewBillNo;
updateFeeParms[1].Value = tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_BY_RUN_FEE_DO, updateFeeParms);
//
SqlParameter[] deleteRunSettleParms = new SqlParameter[] {
new SqlParameter(PARM_RUN_FEE_SETTLEMENT_GID,SqlDbType.VarChar,36)
};
deleteRunSettleParms[0].Value = tempRunSettleID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_RUN_FEE_SETTLEMENT_BY_GID, deleteRunSettleParms);
//
SqlParameter[] deleteRunDoParms = new SqlParameter[] {
new SqlParameter(PARM_RUN_FEE_DO_RUN_SETTLE_ID,SqlDbType.VarChar,36)
};
deleteRunDoParms[0].Value = tempRunSettleID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_RUN_FEE_DO_BY_RUNSETTLE_ID, deleteRunDoParms);
}
//事务提交
sqlTran.Commit();
if (validState == 0)
{
iResult = 1;//状态为1表示插入成功
}
else
{
iResult = -3;//费用表没有足够的申请金额使用,无法提交付费申请
}
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
#region 撤销付费结算
///
/// 撤销付费结算
///
/// 付费结算GID
/// 付费结算单号
/// 操作人GID
/// 分公司GID
/// 值1表示撤销结算成功 值不等于1表示撤销结算失败
public int CacelPaySettle(string tempPaySettleID,string tempBillNO,string tempOperator,string tempCompanyID)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
int iSourceState = 0;
string sSourceState2 = "";
//再判断当前结算信息是否来自付费申请
SqlParameter[] isPayAppParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_SETTLE_SETTLE_ID,SqlDbType.VarChar,36)
};
isPayAppParms[0].Value = tempPaySettleID;
//获取付费结算来源
//string SQL_VALID_PAY_SETTLE_SOURCE = " SELECT COUNT(*) FROM ch_fee_settle_link WHERE SETTLEID = @settle_id ";
iSourceState = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_VALID_PAY_SETTLE_SOURCE, isPayAppParms);
//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_payapplication 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_payapplication 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_payapplication 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_payapplication的部分结算金额(撤销结算)
if (sCURRENCY == "RMB")
{
sSQL = " UPDATE ch_fee_payapplication 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_payapplication.GID AND SETTLEID = '" + tempPaySettleID + "' and (ch_fee_payapplication.ISDELETE is null or ch_fee_payapplication.ISDELETE=0) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
else
{
sSQL = " UPDATE ch_fee_payapplication 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_payapplication.GID AND SETTLEID = '" + tempPaySettleID + "' and (ch_fee_payapplication.ISDELETE is null or ch_fee_payapplication.ISDELETE=0) ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, sSQL, null);
}
//更新付费申请状态从已经结算更新为已审核(撤销结算)
string SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL = " UPDATE ch_fee_payapplication 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_payapplication 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_payapplication 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);
//更新付费申请费用明细状态未结算
SqlParameter[] updateFeeDoStateParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_PAY_APP_GID,SqlDbType.VarChar,36)
};
updateFeeDoStateParms[0].Value = tempPaySettleID;
//更新付费申请相关的费用状态为激活状态(撤销结算)
//string SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL = " UPDATE ch_fee_do SET ISDELETED = 0 FROM ch_fee_payapplication WHERE ch_fee_payapplication.BILLNO = ch_fee_do.BILLNO AND ch_fee_payapplication.COMPANYID = ch_fee_do.COMPANYID AND ch_fee_payapplication.GID = @gid ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL, updateFeeDoStateParms);
}
else
{
//更新付费申请状态从已经结算更新为已审核(撤销结算)
string SQL_UPDATE_CH_FEE_PAY_APP_FOR_CANCEL = " UPDATE ch_fee_payapplication SET BILLSTATUS=0,SETTLERMB=0,SETTLEUSD=0 FROM ch_fee_settle_link WHERE ch_fee_settle_link.RELATIONSUBID = ch_fee_payapplication.GID AND SETTLEID = '" + tempPaySettleID + "' and (ch_fee_payapplication.ISDELETE is null or ch_fee_payapplication.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_payapplication 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);
//更新付费申请费用明细状态未结算
SqlParameter[] updateFeeDoStateParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_PAY_APP_GID,SqlDbType.VarChar,36)
};
updateFeeDoStateParms[0].Value = tempPaySettleID;
//更新付费申请相关的费用状态为激活状态(撤销结算)
//string SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL = " UPDATE ch_fee_do SET ISDELETED = 0 FROM ch_fee_payapplication WHERE ch_fee_payapplication.BILLNO = ch_fee_do.BILLNO AND ch_fee_payapplication.COMPANYID = ch_fee_do.COMPANYID AND ch_fee_payapplication.GID = @gid ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_DO_APP_FOR_CANCEL, updateFeeDoStateParms);
//先将费用表的结算信息更新到结算前状态
SqlParameter[] updateFeeParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_COMPANY_ID,SqlDbType.VarChar,36)
};
updateFeeParms[0].Value = tempBillNO;
updateFeeParms[1].Value = tempCompanyID;
//还原已经进行付费结算的费用(撤销付费结算)
//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(ch_fee.SETTLEMENT,0) - ch_fee_do.ORIGAMOUNT "
//+ " FROM ch_fee_do WHERE ch_fee.GID = ch_fee_do.FEEID AND ch_fee_do.BILLNO = @bill_no AND COMPANYID = @company_id ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_FOR_CANCELPAYSETTLE, updateFeeParms);
}
//更新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
{
//先将费用表的结算信息更新到结算前状态
SqlParameter[] updateFeeParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_COMPANY_ID,SqlDbType.VarChar,36)
};
updateFeeParms[0].Value = tempBillNO;
updateFeeParms[1].Value = tempCompanyID;
//还原已经进行付费结算的费用(撤销付费结算)
//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(ch_fee.SETTLEMENT,0) - ch_fee_do.ORIGAMOUNT "
//+ " FROM ch_fee_do WHERE ch_fee.GID = ch_fee_do.FEEID AND ch_fee_do.BILLNO = @bill_no AND COMPANYID = @company_id ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_FOR_CANCELPAYSETTLE, updateFeeParms);
}
//删除FeeDo记录
SqlParameter[] deleteFeeDoParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_DO_DELETE_USER,SqlDbType.VarChar,36)
};
deleteFeeDoParms[0].Value = tempBillNO;
deleteFeeDoParms[1].Value = tempCompanyID;
deleteFeeDoParms[2].Value = tempOperator;
//删除付费结算费用明细表(撤销付费结算)
//string SQL_DELETE_CH_FEE_DO = " UPDATE ch_fee_do SET ISDELETED = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no AND COMPANYID = @company_id ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_CH_FEE_DO, deleteFeeDoParms);
//更新主表,更新要删除的预收预付明细表中的预付支取用资料
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记录
SqlParameter[] deletePaySettleParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_DELETE_USER,SqlDbType.VarChar,36)
};
deletePaySettleParms[0].Value = tempBillNO;
deletePaySettleParms[1].Value = tempCompanyID;
deletePaySettleParms[2].Value = tempOperator;
//删除付费结算表(撤销付费结算)
//string SQL_DELETE_CH_FEE_SETTLEMENT = " UPDATE ch_fee_settlement SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE BILLNO = @bill_no AND COMPANYID = @company_id ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_CH_FEE_SETTLEMENT, deletePaySettleParms);
//
SqlParameter[] deleteLinkParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_SETTLE_SETTLE_ID,SqlDbType.VarChar,36)
};
deleteLinkParms[0].Value = tempPaySettleID;
//删除合并结算关联信息表记录
//string SQL_DELETE_CH_FEE_SETTLE_LINK = " DELETE FROM ch_fee_settle_link WHERE SETTLEID = @settle_id ";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_DELETE_CH_FEE_SETTLE_LINK, deleteLinkParms);
//事务提交
sqlTran.Commit();
iResult = 1;//状态为1表示插入成功
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
#endregion
public int MergePayApplicationSettle(IList tempFeePayAppEntities,FeeSettlementEntity tempFeeSettlementEntity,string tempCompanyID,string tempOperator)
{
int iResult = 0;
using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
try
{
string strLastBillNo = "";
string strNewBillNo = "";
//先校验费用申请金额是否满足申请要求
int validState = 0;
//string strPayAppIdGroup = "";
for (int i = 0; i < tempFeePayAppEntities.Count; i++)
{
//if (i == tempFeePayAppEntities.Count - 1)
//{
// strPayAppIdGroup += "'" + tempFeePayAppEntities[i].GID + "' ";
//}
//else
//{
// strPayAppIdGroup += "'" + tempFeePayAppEntities[i].GID + "',";
//}
SqlParameter[] insertSettleLinkParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_SETTLE_SETTLE_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_CH_FEE_SETTLE_TYPE,SqlDbType.Int),
new SqlParameter(PARM_CH_FEE_SETTLE_RELATION_SUB_ID,SqlDbType.VarChar,36),
new SqlParameter(PARM_CH_FEE_SETTLE_CREATE_USER,SqlDbType.VarChar,36),
};
insertSettleLinkParms[0].Value = tempFeeSettlementEntity.GID;
insertSettleLinkParms[1].Value = 1;
insertSettleLinkParms[2].Value = tempFeePayAppEntities[i].GID;
insertSettleLinkParms[3].Value = tempOperator;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_CH_FEE_SETTLE_LINK, insertSettleLinkParms);
}
//if (strPayAppIdGroup.EndsWith(","))
//{
// strPayAppIdGroup = strPayAppIdGroup.Substring(0, strPayAppIdGroup.Length - 1);
//}
//string strValidSql = String.Format(,strPayAppIdGroup);
SqlParameter[] validParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_SETTLE_SETTLE_ID,SqlDbType.VarChar,36)
};
validParms[0].Value = tempFeeSettlementEntity.GID;
validState = (int)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_VALID_CH_FEE_SETTLEMENT_FOR_MERGE, validParms);
if (validState == 0)
{
//先获取SQL SERVER 数据库端服务器时间
DateTime nowServerDateTime = (DateTime)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_GET_SERVERTIME, null);
if (nowServerDateTime == DateTime.MinValue)
{
throw new Exception("未正常获取到服务端时间");
}
//获取分公司的票号头字符
string tempBANKSHEAD = (String)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, "SELECT TOP 1 BANKSHEAD FROM company WHERE GID='" + tempCompanyID.Trim() + "'", null) as string;
//生成申请编号起始串
string strBillNoProfix = CreateBeginTitleCode(nowServerDateTime, tempBANKSHEAD+"CR", 4);
SqlParameter[] getLastBillParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_SETTLEMENT_LIKE, SqlDbType.VarChar, 200),
new SqlParameter(PARM_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36)
};
getLastBillParms[0].Value = strBillNoProfix + "%";
getLastBillParms[1].Value = tempCompanyID;
//获取最大的编号
string tempLastBillNo = (String)SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, SQL_SELECT_LAST_BILL_NO, getLastBillParms) as string;
strLastBillNo = tempLastBillNo ?? "";
//生成付费申请编号
strNewBillNo = CreateSettleCode(nowServerDateTime, tempBANKSHEAD+"CR", 4, strLastBillNo);
SqlParameter[] settleParms = new SqlParameter[]{
new SqlParameter(PARM_FEE_SETTLEMENT_GID, SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_NO, SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_SETTLE_MODE, SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_CUSTOMER_NAME,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_SETTLE_TYPE,SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_AMOUNT_RMB,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_SETTLEMENT_ACCOUNT_RMB,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_RATE,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_SETTLEMENT_AMOUNT_USD,SqlDbType.Decimal,20),
new SqlParameter(PARM_FEE_SETTLEMENT_ACCOUNT_USD,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_TYPE,SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_VOUCHER_NO,SqlDbType.VarChar,50),
new SqlParameter(PARM_FEE_SETTLEMENT_SETTLE_USER,SqlDbType.VarChar,36),
new SqlParameter(PARM_FEE_SETTLEMENT_IS_EXPORT,SqlDbType.Bit),
new SqlParameter(PARM_FEE_SETTLEMENT_FINANCIAL_VOUCHER,SqlDbType.VarChar,50),
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_STATUS,SqlDbType.Int),
new SqlParameter(PARM_FEE_SETTLEMENT_REMARK,SqlDbType.VarChar,1024),
new SqlParameter(PARM_FEE_SETTLEMENT_COMPANY_ID,SqlDbType.VarChar,36),
new SqlParameter("@ACCOUNTS_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@ACCOUNTS_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@ACCOUNTS_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@PREPAY_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@PREPAY_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@PREPAY_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@AHSR_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@AHSR_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@AHSR_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@FINANCIAL_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@FINANCIAL_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@FINANCIAL_MONEY", SqlDbType.Decimal,9) ,
new SqlParameter("@ADVANCE_CURRENCY", SqlDbType.VarChar,20) ,
new SqlParameter("@ADVANCE_RATE", SqlDbType.Decimal,9) ,
new SqlParameter("@ADVANCE_MONEY", SqlDbType.Decimal,9)
};
settleParms[0].Value = tempFeeSettlementEntity.GID;
settleParms[1].Value = strNewBillNo;
settleParms[2].Value = tempFeeSettlementEntity.SettleMode;
settleParms[3].Value = tempFeeSettlementEntity.CustomerName;
settleParms[4].Value = tempFeeSettlementEntity.SettleType;
settleParms[5].Value = tempFeeSettlementEntity.AmountRMB;
settleParms[6].Value = tempFeeSettlementEntity.AccountRMB;
settleParms[7].Value = tempFeeSettlementEntity.Rate;
settleParms[8].Value = tempFeeSettlementEntity.AmountUSD;
settleParms[9].Value = tempFeeSettlementEntity.AccountUSD;
settleParms[10].Value = tempFeeSettlementEntity.BillType;
settleParms[11].Value = tempFeeSettlementEntity.VoucherNO;
settleParms[12].Value = tempFeeSettlementEntity.SettleUser;
settleParms[13].Value = tempFeeSettlementEntity.IsExport;
settleParms[14].Value = tempFeeSettlementEntity.FinancialVoucher;
settleParms[15].Value = tempFeeSettlementEntity.BillStatus;
settleParms[16].Value = tempFeeSettlementEntity.Remark;
settleParms[17].Value = tempFeeSettlementEntity.CompanyID;
settleParms[18].Value = tempFeeSettlementEntity.ACCOUNTS_CURRENCY;//记账资料_币别
settleParms[19].Value = tempFeeSettlementEntity.ACCOUNTS_RATE;//记账资料_汇率
settleParms[20].Value = tempFeeSettlementEntity.ACCOUNTS_MONEY;//记账资料_金额
settleParms[21].Value = tempFeeSettlementEntity.PREPAY_CURRENCY;//预付支资料_币别
settleParms[22].Value = tempFeeSettlementEntity.PREPAY_RATE;//预付支资料_汇率
settleParms[23].Value = tempFeeSettlementEntity.PREPAY_MONEY;//预付支资料_金额
settleParms[24].Value = tempFeeSettlementEntity.AHSR_CURRENCY;//实付支资料_币别
settleParms[25].Value = tempFeeSettlementEntity.AHSR_RATE;//实付支资料_汇率
settleParms[26].Value = tempFeeSettlementEntity.AHSR_MONEY;//实付支资料_金额
settleParms[27].Value = tempFeeSettlementEntity.FINANCIAL_CURRENCY;//财务费用_币别
settleParms[28].Value = tempFeeSettlementEntity.FINANCIAL_RATE;//财务费用_汇率
settleParms[29].Value = tempFeeSettlementEntity.FINANCIAL_MONEY;//财务费用_金额
settleParms[30].Value = tempFeeSettlementEntity.ADVANCE_CURRENCY;//预付支取用资料_币别
settleParms[31].Value = tempFeeSettlementEntity.ADVANCE_RATE;//预付支取用资料_汇率
settleParms[32].Value = tempFeeSettlementEntity.ADVANCE_MONEY;//预付支取用资料_金额
//将所有合并付费申请生成的结算信息插入付费结算
string SQL_INSERT_CH_FEE_SETTLEMENT_FOR_MERGE = "INSERT INTO ch_fee_settlement(GID, BILLNO, BILLDATE, SETTLEMODE, CUSTOMERNAME, SETTLETYPE, AMOUNTRMB, ACCOUNTRMB, RATE, AMOUNTUSD, ACCOUNTUSD, BILLTYPE, VOUCHERNO, SETTLEUSER, SETTLETIME, ISEXPORT, FINANCIALVOUCHER, BILLSTATUS, REMARK,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)"
+ " VALUES(@gid,@bill_no,GETDATE(),@settle_mode,@customer_name,@settle_type,@amount_rmb,@account_rmb,@rate,@amount_usd,@account_usd,@bill_type,@voucher_no,@settle_user,GETDATE(),@is_export,@financial_voucher,@bill_status,@remark,@company_id,@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)";
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_CH_FEE_SETTLEMENT_FOR_MERGE, settleParms);
//
SqlParameter[] insertFeeDoParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_SETTLEMENT_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_CH_FEE_SETTLE_SETTLE_ID,SqlDbType.VarChar,36)
};
insertFeeDoParms[0].Value = strNewBillNo;
insertFeeDoParms[1].Value = tempFeeSettlementEntity.GID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_CH_FEE_FEE_DO_FOR_MERGE, insertFeeDoParms);
//更新付费申请为已结算
SqlParameter[] updatePayAppStateParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_SETTLE_SETTLE_ID,SqlDbType.VarChar,36)
};
updatePayAppStateParms[0].Value = tempFeeSettlementEntity.GID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_PAY_APP_SETTLED, updatePayAppStateParms);
//更新付费申请费用明细状态为已结算
SqlParameter[] updateFeeDoStateParms = new SqlParameter[] {
new SqlParameter(PARM_CH_FEE_PAY_APP_GID,SqlDbType.VarChar,36)
};
updateFeeDoStateParms[0].Value = tempFeeSettlementEntity.GID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_DO_APP_SETTLED, updateFeeDoStateParms);
SqlParameter[] updateFeeParms = new SqlParameter[] {
new SqlParameter(PARM_FEE_DO_BILL_NO,SqlDbType.VarChar,20),
new SqlParameter(PARM_FEE_DO_COMPANY_ID,SqlDbType.VarChar,36)
};
updateFeeParms[0].Value = strNewBillNo;
updateFeeParms[1].Value = tempCompanyID;
SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_UPDATE_CH_FEE_BY_CH_FEE_DO_FOR_MERGE, updateFeeParms);
}
//事务提交
sqlTran.Commit();
if (validState == 0)
{
iResult = 1;//状态为1表示插入成功
}
else
{
iResult = -3;//费用表没有足够的申请金额使用,无法提交付费申请
}
}
catch (Exception execError)
{
iResult = -1;//有异常,插入失败
sqlTran.Rollback();
iResult = -2;//插入异常,事务已回滚成功
throw execError;
}
finally
{
SqlHelper.CloseConnection();
}
}
return iResult;
}
}
}