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/TruckMng/DAL/Comm/MsChFeeDAL.cs

479 lines
21 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using DSWeb.Areas.TruckMng.Models.Comm;
using HcUtility.Comm;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.TruckMng.DAL.MsWlBs;
namespace DSWeb.Areas.TruckMng.DAL.Comm
{
public class MsChFeeDAL
{
#region 查询费用数据
static public List<MsChFee> GetDataList(string strCondition,string userid)
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append("GId,BsNo,FeeStatus,");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99024 and EnumValueID=ch_fee.FeeStatus) as FeeStatus_Ref");
strSql.Append(",FeeType,");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99020 and EnumValueID=ch_fee.FeeType) as FeeType_Ref");
strSql.Append(",FeeName,");
strSql.Append("(select top 1 NAME from vMsTruckFeeType where NAME=ch_fee.FeeName) as FeeName_Ref");
strSql.Append(",FeeDescription,CustomerType,CustomerName,");
strSql.Append("(select CustName from vMsTruckClient where CustName=ch_fee.CustomerName) as CustomerName_Ref");
strSql.Append(",Unit,UnitPrice,Quantity,Amount,Currency,ExChangerate,Reason");
strSql.Append(",Remark,Commissionrate,Settlement,Invoice,OrderAmount,OrderInvoice,SubmitDate");
strSql.Append(",Auditoperator,AuditDate,EnteroPerator,EnterDate,DebitNo,IsDebit,IsOpen");
strSql.Append(",IsAdvancedpay,Sort,IsInvoice,FeeFrt,IsCrmOrderFee,AuditStatus,InvoiceNum");
strSql.Append(",(select showname from [user] WHERE GID = ch_fee.ENTEROPERATOR) EnterOperatorName");
strSql.Append(",ChequeNum,WmsOutBsNo");
strSql.Append(" from ch_fee where 1=1 ");
if (strCondition.Trim() != String.Empty)
{
strSql.Append(" and " + strCondition);
}
string powerstr = "";
int power = MsWlPcDAL.GetFFSFPower(userid);
if (power == 1)
{
powerstr = @"and feetype = 1 ";
}
else if (power==2)
{
powerstr = @"and feetype = 2 ";
}
else if (power == 0) {
powerstr = @"and 1=2 ";
}
strSql.Append(powerstr);
return SetData(strSql);
}
private static List<MsChFee> SetData(StringBuilder strSql)
{
var bodyList = new List<MsChFee>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsChFee data = new MsChFee();
#region Set DB data to Object
data.GId = Convert.ToString(reader["GId"]);
data.BsNo = Convert.ToString(reader["BsNo"]);
data.FeeStatus = Convert.ToDecimal(reader["FeeStatus"]);
data.FeeStatus_Ref = Convert.ToString(reader["FeeStatus_Ref"]);
data.FeeType = Convert.ToDecimal(reader["FeeType"]);
data.FeeType_Ref = Convert.ToString(reader["FeeType_Ref"]);
data.FeeName = Convert.ToString(reader["FeeName"]);
data.FeeName_Ref = Convert.ToString(reader["FeeName_Ref"]);
data.FeeDescription = Convert.ToString(reader["FeeDescription"]);
data.CustomerType = Convert.ToString(reader["CustomerType"]);
data.CustomerName = Convert.ToString(reader["CustomerName"]);
data.CustomerName_Ref = Convert.ToString(reader["CustomerName_Ref"]);
data.Unit = Convert.ToString(reader["Unit"]);
data.UnitPrice = Convert.ToDecimal(reader["UnitPrice"]);
data.Quantity = Convert.ToDecimal(reader["Quantity"]);
data.Amount = Convert.ToDecimal(reader["Amount"]);
data.Currency = Convert.ToString(reader["Currency"]);
data.ExChangerate = Convert.ToDecimal(reader["ExChangerate"]);
data.Reason = Convert.ToString(reader["Reason"]);
data.Remark = Convert.ToString(reader["Remark"]);
data.Commissionrate = Convert.ToDecimal(reader["Commissionrate"]);
data.Settlement = Convert.ToDecimal(reader["Settlement"]);
if (reader["Invoice"] != DBNull.Value)
data.Invoice = Convert.ToDecimal(reader["Invoice"]);
data.OrderAmount = Convert.ToDecimal(reader["OrderAmount"]);
data.OrderInvoice = Convert.ToDecimal(reader["OrderInvoice"]);
if (reader["SubmitDate"] != DBNull.Value)
data.SubmitDate = Convert.ToDateTime(reader["SubmitDate"]);
data.Auditoperator = Convert.ToString(reader["Auditoperator"]);
if (reader["AuditDate"] != DBNull.Value)
data.AuditDate = Convert.ToDateTime(reader["AuditDate"]);
data.EnteroPerator = Convert.ToString(reader["EnteroPerator"]);
if (reader["EnterDate"] != DBNull.Value)
data.EnterDate = Convert.ToDateTime(reader["EnterDate"]);
data.DebitNo = Convert.ToString(reader["DebitNo"]);
data.IsDebit = Convert.ToString(reader["IsDebit"]);
data.IsOpen = Convert.ToString(reader["IsOpen"]);
data.IsAdvancedpay = Convert.ToString(reader["IsAdvancedpay"]);
data.Sort = Convert.ToString(reader["Sort"]);
data.IsInvoice = Convert.ToString(reader["IsInvoice"]);
data.FeeFrt = Convert.ToString(reader["FeeFrt"]);
data.IsCrmOrderFee = Convert.ToString(reader["IsCrmOrderFee"]);
data.AuditStatus = Convert.ToDecimal(reader["AuditStatus"]);
data.InvoiceNum = Convert.ToString(reader["InvoiceNum"]);
data.ChequeNum = Convert.ToString(reader["ChequeNum"]);
data.WmsOutBsNo = Convert.ToString(reader["WmsOutBsNo"]);
data.EnterOperatorName = reader["EnterOperatorName"] == DBNull.Value ? "" : reader["EnterOperatorName"].ToString();
#endregion
bodyList.Add(data);
}
reader.Close();
}
return bodyList;
}
#endregion
#region 提交申请
// 根据handle类型执行更新操作更新费用信息ch_fee.feestatus和工作流运行表WorkFlowDo信息
// 费用状态值 费用状态主要有11个状态,分别为审核通过STATUS=0录入状态STATUS=1提交审核STATUS=2申请修改 (STATUS=3)
// 取消申请(STATUS=5)驳回提交STATUS=6驳回申请STATUS=7部分结算STATUS=8结算完毕STATUS=9
internal static DBResult Audit(int newStatus, List<MsChFee> chfeeList)
{
var result = new DBResult();
foreach (var chFee in chfeeList)
{
var status = chFee.FeeStatus;
var canAudit = true;
if (newStatus == 2)
{
canAudit = status == 1 || status == 6;
}
else if (newStatus == 0)
{
canAudit = status == 2;
}
else if (newStatus == 6)
{
canAudit = status == 0 || status == 2;
}
else if (newStatus == 8 || newStatus == 9)
{
canAudit = status == 0 ;
}
if (!canAudit)
{
result.Success = false;
if (newStatus == 2)
result.Message = "只有在'录入状态'和'驳回提交'状态下才能提交审核操作";
else if (newStatus == 0)
{
result.Message = "只有在‘提交审核’状态下才能进行此操作";
}
else if (newStatus == 6)
{
result.Message = "只有在‘审核通过’或‘提交审核’状态下才能进行此操作";
}
else if (newStatus == 8 || newStatus == 9)
{
result.Message = "只有在‘审核通过’状态下才能进行此操作";
}
return result;
}
}
var feeBsNoList = new List<string>();
foreach (var msChFee in chfeeList)
{
var bsNo = msChFee.BsNo;
if (!feeBsNoList.Contains(bsNo))
{
feeBsNoList.Add(bsNo);
}
}
Database db = DatabaseFactory.CreateDatabase();
//费用锁定后不允许驳回。
foreach (var bsNo in feeBsNoList)
{
if (newStatus == 6)
{
string sql = string.Empty;
if (bsNo.StartsWith("TMSBS"))
{
sql = "select FeeStatus from tMsWlBsHead where Gid=@gid";
}else
{
sql = "select FeeStatus from tMsWlPcHead where Gid=@gid"; ;
}
var cmdFeeStatus = db.GetSqlStringCommand(sql);
db.AddInParameter(cmdFeeStatus, "@gid", DbType.String, bsNo);
var feeStatus = Convert.ToString(db.ExecuteScalar(cmdFeeStatus));
if (feeStatus == "1")
{
result.Success = false;
result.Message = "单据的费用已经锁定,不允许驳回费用";
return result;
}
}
}
var feeGIdList = new List<string>();
foreach (var msChFee in chfeeList)
{
var GId = msChFee.GId;
if (!feeGIdList.Contains(GId))
{
feeGIdList.Add(GId);
}
}
//费用锁定后不允许驳回。
foreach (var GId in feeGIdList)
{
if (newStatus == 6)
{
string sql = string.Empty;
sql = "select case when ISVOU=1 or ACCDATE<>'' or accdate is not null then 1 else 0 end FeeStatus from ch_fee where bsno like '%XXH%' and Gid=@gid";
var cmdFeeStatus = db.GetSqlStringCommand(sql);
db.AddInParameter(cmdFeeStatus, "@gid", DbType.String, GId);
var feeStatus = Convert.ToString(db.ExecuteScalar(cmdFeeStatus));
if (feeStatus == "1")
{
result.Success = false;
result.Message = "费用已经锁定或已生成凭证,不允许驳回或撤回";
return result;
}
}
}
const string selectSql = "select FeeStatus from ch_fee where gid=@gid";
const string updateSql = "update ch_fee set FeeStatus=@FeeStatus, Auditdate=getdate() where gid=@gid";
const string enumSql =
"select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99024 and EnumValueID=@EnumValueID";
const string updateBsBillSql = "update tMsWlBsHead set FeeOpStatus=@FeeOpStatus where gid=@gid";
const string updatePcBillSql = "update tMsWlPcHead set FeeOpStatus=@FeeOpStatus where gid=@gid";
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
try
{
var cmdEnum = db.GetSqlStringCommand(enumSql);
db.AddInParameter(cmdEnum, "@EnumValueID", DbType.Int32, newStatus);
var newStatusRef = Convert.ToString(db.ExecuteScalar(cmdEnum));
DbTransaction transaction = connection.BeginTransaction();
try
{
foreach (var msChFee in chfeeList)
{
var cmdSelect = db.GetSqlStringCommand(selectSql);
db.AddInParameter(cmdSelect, "@gid", DbType.String, msChFee.GId);
var feeStatus = db.ExecuteScalar(cmdSelect, transaction);
if (feeStatus == null)
{
throw new MsChFeeDealException("'" + msChFee.FeeName + "'数据还没有保存,不想允许提交,请保存后重试!");
}
if (Convert.ToInt32(feeStatus) != msChFee.FeeStatus)
{
throw new MsChFeeDealException("'"+msChFee.FeeName+"'费用状态已经改变,不想允许提交,请刷新后重试!");
}
var cmdUpdate = db.GetSqlStringCommand(updateSql);
db.AddInParameter(cmdUpdate, "@FeeStatus", DbType.Int32, newStatus);
db.AddInParameter(cmdUpdate, "@gid", DbType.String, msChFee.GId);
db.ExecuteNonQuery(cmdUpdate, transaction);
}
foreach (var bsNo in feeBsNoList)
{
var rejectCount = GetFeeStatusRecordCount(db, bsNo, 6, transaction);
if (rejectCount > 0)
{
SetBillFeeStatus(bsNo, "3", updateBsBillSql, updatePcBillSql, db, transaction);//驳回
}
else
{
var passCount = GetFeeStatusRecordCount(db, bsNo, 0, transaction);
if (passCount > 0)
{
SetBillFeeStatus(bsNo, "2", updateBsBillSql, updatePcBillSql, db, transaction);//审核通过
}
else
{
var tjCount = GetFeeStatusRecordCount(db, bsNo, 2, transaction);
if (tjCount > 0)
{
SetBillFeeStatus(bsNo, "1", updateBsBillSql, updatePcBillSql, db, transaction);//已提交
}
}
}
}
transaction.Commit();
result.Success = true;
result.Message = "提交成功";
result.Data = newStatusRef;
}
catch (Exception e)
{
transaction.Rollback();
result.Success = false;
if (e is MsChFeeDealException)
result.Message = e.Message;
else
result.Message = "执行提交发生未知错误,请重试";
}
}
finally
{
connection.Close();
}
}
return result;
}
private static void SetBillFeeStatus(
string bsNo,
string feeOpStatus,
string updateBsBillSql,
string updatePcBillSql,
Database db,
DbTransaction transaction)
{
string sql = string.Empty;
if (bsNo.StartsWith("TMSBS"))
{
sql = updateBsBillSql;
}
else
{
sql = updatePcBillSql;
}
var cmdUpdate = db.GetSqlStringCommand(sql);
db.AddInParameter(cmdUpdate, "@FeeOpStatus", DbType.String, feeOpStatus); //驳回
db.AddInParameter(cmdUpdate, "@gid", DbType.String, bsNo);
db.ExecuteNonQuery(cmdUpdate, transaction);
}
private static int GetFeeStatusRecordCount(Database db, string bsNo, int feeStatus, DbTransaction transaction)
{
var cmdRejectCount = db.GetSqlStringCommand("select count(*) from ch_fee where BsNo=@BsNo and FeeStatus=@FeeStatus");
db.AddInParameter(cmdRejectCount, "@BsNo", DbType.String, bsNo);
db.AddInParameter(cmdRejectCount, "@FeeStatus", DbType.Int32, feeStatus);
var rejectCount = Convert.ToInt32(db.ExecuteScalar(cmdRejectCount, transaction));
return rejectCount;
}
#endregion
#region 查询应收/实收(现金帐目)
static public List<MsChFeeDo> GetChFeeDo(string condition)
{
var strSql = new StringBuilder();
strSql.Append(" select fd.gid,f.bsno,f.customername,f.feeid,f.feename,fd.Doamount,f.feetype, ");
strSql.Append("(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99020 and EnumValueID=f.FeeType) as FeeType_Ref");
strSql.Append(" fd.createtime from ch_fee_do fd ");
strSql.Append(" left join ch_fee f on f.gid=fd.feeid ");
if (condition.Trim() != String.Empty)
{
strSql.Append(" where " + condition);
}
else
{ strSql.Clear(); }//防止无参数调用
return SetChFeeDo(strSql);
}
private static List<MsChFeeDo> SetChFeeDo(StringBuilder strSql)
{
var bodyList = new List<MsChFeeDo>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsChFeeDo data = new MsChFeeDo();
#region Set DB data to Object
data.Gid = Convert.ToString(reader["Gid"]);
data.bsno = Convert.ToString(reader["bsno"]);
data.customername = Convert.ToString(reader["customername"]);
data.feeid = Convert.ToString(reader["feeid"]);
data.feename = Convert.ToString(reader["feename"]);
data.Doamount = Convert.ToString(reader["Doamount"]);
data.feetype = Convert.ToString(reader["feetype"]);
data.createtime = Convert.ToString(reader["createtime"]);
#endregion
bodyList.Add(data);
}
reader.Close();
}
return bodyList;
}
#endregion
public static bool GetFeeCount(string BSNO)
{
var isfee = false;
var strSql = new StringBuilder();
strSql.Append("Select count(*) as count ");
strSql.Append(" from ch_fee ");
strSql.Append(" where BSNO='" + BSNO + "'");
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
var evData = Convert.ToInt32(reader["count"]);
if (evData > 0) { isfee = true; };
}
reader.Close();
}
return isfee;
}
/* static public int CWStart(string ContractNo)
{
var strSql = new StringBuilder();
strSql.Append(" exec smsCWStart '" + ContractNo + "'");
return setCWStart(strSql);//ExecuteNonQuery
}
private static int setCWStart(StringBuilder strSql)
{
var _count = 0;
Database db = DatabaseFactory.CreateDatabase();
_count = db.ExecuteNonQuery(CommandType.Text, strSql.ToString());
return _count;
}*/
}
internal class MsChFeeDealException : Exception
{
internal MsChFeeDealException(string message)
: base(message)
{
}
}
}