You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/Account/DAL/Chfee_bankdata/Chfee_bankdataDAL.cs

620 lines
28 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using DSWeb.EntityDA;
using HcUtility.Comm;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.Areas.Account.Models.Chfee_Bankdata;
using HcUtility.Core;
using System.IO;
using DSWeb.Areas.Account.Models.Chfee_Invoice;
using DSWeb.Areas.Account.DAL.Chfee_Invoice;
using DSWeb.Areas.Account.Models.Chfee_Settlement;
using DSWeb.Areas.Account.DAL.Chfee_Settlement;
namespace DSWeb.Areas.Account.DAL.Chfee_bankdata
{
public class ChBankdataDAL
{
#region 列表
static public List<ChBankdata> GetDataList(string strCondition, string userid = "", string usercode = "", string orgcode = "", string sort = null)
{
var strSql = new StringBuilder();
strSql.Append(" SELECT [GID],[PCNO],[SFNO],[TRANSTYPE],[DRAWEE_BANK] ,[DRAWEE_ACCOUNT],[DRAWEE_NAME],[PAYEE_BANK],[PAYEE_ACCOUNT]");
strSql.Append(",[PAYEE_NAME],[TRANSDATE],[CURRENCY],[AMOUNT],[TRANSNO],[IMPORTDATE],[IMPORTER],[ISFIT],[FITCOUNT],[INVNO],[FITDATE]");
strSql.Append(",[FITTYPE],[PRINTDATE],[ISPRINT],[PRINTER],[SALE],[ATTITIONAL],[REMARK],[SUMMARY],FITUSER,CREATEUSER,CREATETIME");
strSql.Append(",(select ShowName from[user] where GID = ch_fee_bankdata.CREATEUSER) as CREATEUSERREF");
strSql.Append(" FROM ch_fee_bankdata where CORPID='"+orgcode+"' ");
if (!string.IsNullOrEmpty(strCondition))
{
strSql.Append(" and " + strCondition);
}
var sortstring = DatasetSort.Getsortstring(sort);
if (!string.IsNullOrEmpty(sortstring))
{
strSql.Append(" order by " + sortstring);
}
else
{
strSql.Append(" order by PCNO DESC,SFNO");
}
return SetData(strSql);
}
static public ChBankdata GetData(string condition, string userid = "", string usercode = "", string orgcode = "")
{
var list = GetDataList(condition, userid, usercode, orgcode, "");
if (list.Count > 0)
return list[0];
return new ChBankdata();
}
private static List<ChBankdata> SetData(StringBuilder strSql)
{
var headList = new List<ChBankdata>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
ChBankdata data = new ChBankdata();
#region Set DB data to Object
data.GID = Convert.ToString(reader["GID"]);
if (reader["PCNO"] != DBNull.Value)
data.PCNO = Convert.ToInt32(reader["PCNO"]);
if (reader["SFNO"] != DBNull.Value)
data.SFNO = Convert.ToInt32(reader["SFNO"]);
data.TRANSTYPE = Convert.ToString(reader["TRANSTYPE"]);
data.DRAWEE_BANK = Convert.ToString(reader["DRAWEE_BANK"]);
data.DRAWEE_ACCOUNT = Convert.ToString(reader["DRAWEE_ACCOUNT"]);
data.DRAWEE_NAME = Convert.ToString(reader["DRAWEE_NAME"]);
data.PAYEE_BANK = Convert.ToString(reader["PAYEE_BANK"]);
data.PAYEE_ACCOUNT = Convert.ToString(reader["PAYEE_ACCOUNT"]);
data.PAYEE_NAME = Convert.ToString(reader["PAYEE_NAME"]);
if (reader["TRANSDATE"] != DBNull.Value)
data.TRANSDATE = Convert.ToDateTime(reader["TRANSDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
data.CURRENCY = Convert.ToString(reader["CURRENCY"]);
if (reader["AMOUNT"] != DBNull.Value)
data.AMOUNT = Convert.ToDecimal(reader["AMOUNT"]);
// data.CHEQUENO = Convert.ToString(reader["CHEQUENO"]);
data.TRANSNO = Convert.ToString(reader["TRANSNO"]);
if (reader["IMPORTDATE"] != DBNull.Value)
data.IMPORTDATE = Convert.ToDateTime(reader["IMPORTDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
data.IMPORTER = Convert.ToString(reader["IMPORTER"]);
if (reader["ISFIT"] != DBNull.Value)
data.ISFIT = Convert.ToBoolean(reader["ISFIT"]);
if (reader["FITCOUNT"] != DBNull.Value)
data.FITCOUNT = Convert.ToInt32(reader["FITCOUNT"]);
data.INVNO = Convert.ToString(reader["INVNO"]);
if (reader["FITDATE"] != DBNull.Value)
data.FITDATE = Convert.ToDateTime(reader["FITDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
data.FITTYPE = Convert.ToString(reader["FITTYPE"]);
if (reader["PRINTDATE"] != DBNull.Value)
data.PRINTDATE = Convert.ToDateTime(reader["PRINTDATE"]).ToString("yyyy-MM-dd HH:mm:ss");
if (reader["ISPRINT"] != DBNull.Value)
data.ISPRINT = Convert.ToBoolean(reader["ISPRINT"]);
data.PRINTER = Convert.ToString(reader["PRINTER"]);
data.SALE = Convert.ToString(reader["SALE"]);
data.ATTITIONAL = Convert.ToString(reader["ATTITIONAL"]);
data.REMARK = Convert.ToString(reader["REMARK"]);
data.SUMMARY = Convert.ToString(reader["SUMMARY"]);
data.FITUSER = Convert.ToString(reader["FITUSER"]);
data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]);
data.CREATEUSERREF = Convert.ToString(reader["CREATEUSERREF"]);
if (reader["CREATETIME"] != DBNull.Value)
data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]).ToString("yyyy-MM-dd HH:mm:ss");
#endregion
headList.Add(data);
}
reader.Close();
}
return headList;
}
#endregion
#region 结算匹配
public static DBResult AddAutoBankDataStlDetail(string condition, string fitcondition, string username, string userid, string companyid)
{
var result = new DBResult();
var BankdataList = ChBankdataDAL.GetDataList(condition, userid, username, companyid);
if (BankdataList != null && BankdataList.Count != 0)
{
foreach (var Bankdata in BankdataList)
{
var invsql = " AND EXISTS (select 1 from info_client WHERE CM.CUSTOMERNAME=info_client.SHORTNAME and info_client.DESCRIPTION='"+ Bankdata.DRAWEE_NAME+ Bankdata.PAYEE_NAME + "') and CURR='" + Bankdata.CURRENCY + "' and (AMOUNTRMB=" + Bankdata.AMOUNT + " or AMOUNTUSD=" + Bankdata.AMOUNT + ") ";
if (Bankdata.TRANSTYPE=="收入")
invsql = invsql + " and BILLTYPE=1 ";
else invsql = invsql + " and BILLTYPE=2 ";
var InvList = ChsettlementDAL.GetDataListAll(fitcondition+ invsql);
if (InvList.Count == 1)
{
result = AddBankDataStlDetail(InvList, Bankdata.GID, username, userid);
}
}
}
result.Success = true;
result.Message = "匹配结算成功";
return result;
}
public static DBResult AddBankDataStlDetail(List<ChSettlement> boday, string billno, string username, string userid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
if (boday != null)
{
var cmdInsert =
db.GetSqlStringCommand(
@"insert into ch_fee_bankdata_fitdetail (GID,LINKGID,INVOICENO,BILLNO,CREATEUSER,CREATETIME)
values (@GID,@LINKGID,@INVOICENO,@BILLNO,@CREATEUSER,@CREATETIME) ");
var cmdupdate = db.GetSqlStringCommand("update ch_fee_bankdata set ISFIT=1,FITDATE=GETDATE(),FITTYPE='手动',FITUSER=@FITUSER,FITCOUNT=@FITCOUNT,INVNO=@INVNO where GID=@GID");
int invcount = 0;
string invnostr = "";
foreach (var enumValue in boday)
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString());
db.AddInParameter(cmdInsert, "@LINKGID", DbType.String, billno);
db.AddInParameter(cmdInsert, "@INVOICENO", DbType.String, enumValue.BILLNO);
db.AddInParameter(cmdInsert, "@BILLNO", DbType.String, enumValue.BILLNO);
db.AddInParameter(cmdInsert, "@CREATEUSER", DbType.String, username);
db.AddInParameter(cmdInsert, "@CREATETIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.ExecuteNonQuery(cmdInsert, tran);
invcount = invcount + 1;
if (!string.IsNullOrEmpty(invnostr)) invnostr = invnostr + ";" + enumValue.BILLNO;
else invnostr = enumValue.BILLNO;
}
cmdupdate.Parameters.Clear();
db.AddInParameter(cmdupdate, "@FITUSER", DbType.String, username);
db.AddInParameter(cmdupdate, "@FITCOUNT", DbType.Int16, invcount);
db.AddInParameter(cmdupdate, "@INVNO", DbType.String, invnostr);
db.AddInParameter(cmdupdate, "@GID", DbType.String, billno);
db.ExecuteNonQuery(cmdupdate, tran);
}
result = new DBResult();
result.Success = true;
result.Message = "匹配发票成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "匹配发票出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "匹配发票成功";
return result;
}
#endregion
public static DBResult AddAutoBankDataDetail(string condition, string fitcondition, string username, string userid,string companyid)
{
var result = new DBResult();
var BankdataList = ChBankdataDAL.GetDataList(condition, userid, username, companyid);
if (BankdataList != null&& BankdataList.Count!=0)
{
foreach (var Bankdata in BankdataList) {
var invsql = " INVOICECUSTNAME='"+Bankdata.DRAWEE_NAME + "' and RECVCURR='"+ Bankdata.CURRENCY + "' and (AMOUNT="+Bankdata.AMOUNT+ " or OTCURRAMOUNT=" + Bankdata.AMOUNT + ") ";
invsql = invsql + " and EXISTS (select 1 from ch_fee f left join ch_fee_do d on (d.FEEID=f.GID) and cm.BILLNO=d.BILLNO AND F.AMOUNT<>F.SETTLEMENT) ";
var InvList = ChinvoiceDAL.GetDataList(0, 5, fitcondition, userid,username,companyid);
if (InvList.Count == 1) {
result = AddBankDataDetail(InvList,Bankdata.GID,username,userid);
}
}
}
result.Success = true;
result.Message = "匹配发票成功";
return result;
}
public static DBResult AddBankDataDetail(List<ChInvoice> boday, string billno,string username,string userid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
if (boday != null)
{
var cmdInsert =
db.GetSqlStringCommand(
@"insert into ch_fee_bankdata_fitdetail (GID,LINKGID,INVOICENO,BILLNO,CREATEUSER,CREATETIME)
values (@GID,@LINKGID,@INVOICENO,@BILLNO,@CREATEUSER,@CREATETIME) ");
var cmdupdate = db.GetSqlStringCommand("update ch_fee_bankdata set ISFIT=1,FITDATE=GETDATE(),FITTYPE='手动',FITUSER=@FITUSER,FITCOUNT=@FITCOUNT,INVNO=@INVNO where GID=@GID");
int invcount = 0;
string invnostr = "";
foreach (var enumValue in boday)
{
cmdInsert.Parameters.Clear();
db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString());
db.AddInParameter(cmdInsert, "@LINKGID", DbType.String, billno);
db.AddInParameter(cmdInsert, "@INVOICENO", DbType.String, enumValue.INVOICENO);
db.AddInParameter(cmdInsert, "@BILLNO", DbType.String, enumValue.BILLNO);
db.AddInParameter(cmdInsert, "@CREATEUSER", DbType.String, username);
db.AddInParameter(cmdInsert, "@CREATETIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
db.ExecuteNonQuery(cmdInsert, tran);
invcount = invcount + 1;
if (!string.IsNullOrEmpty(invnostr)) invnostr = invnostr + ";" + enumValue.INVOICENO;
else invnostr = enumValue.INVOICENO;
}
cmdupdate.Parameters.Clear();
db.AddInParameter(cmdupdate, "@FITUSER", DbType.String,username);
db.AddInParameter(cmdupdate, "@FITCOUNT", DbType.Int16, invcount);
db.AddInParameter(cmdupdate, "@INVNO", DbType.String, invnostr);
db.AddInParameter(cmdupdate, "@GID", DbType.String, billno);
db.ExecuteNonQuery(cmdupdate, tran);
}
result = new DBResult();
result.Success = true;
result.Message = "匹配发票成功";
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
result.Success = false;
result.Message = "匹配发票出现错误,请重试或联系系统管理员";
return result;
}
}
result.Success = true;
result.Message = "匹配发票成功";
return result;
}
public static DBResult DelBankDataDetail(List<ChBankdata> body)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
using (var conn = db.CreateConnection())
{
conn.Open();
var tran = conn.BeginTransaction();
try
{
var cmdupdate = db.GetSqlStringCommand("update ch_fee_bankdata set ISFIT=0,FITDATE=null,FITTYPE='',FITUSER=null,FITCOUNT=0,INVNO='' where GID=@GID");
var cmdDelete = db.GetSqlStringCommand("delete from ch_fee_bankdata_fitdetail where LINKGID=@GID");
foreach (var bill in body)
{
cmdDelete.Parameters.Clear();
db.AddInParameter(cmdDelete, "@GID", DbType.String, bill.GID);
db.ExecuteNonQuery(cmdDelete, tran);
cmdupdate.Parameters.Clear();
db.AddInParameter(cmdupdate, "@GID", DbType.String, bill.GID);
db.ExecuteNonQuery(cmdupdate, tran);
}
result = new DBResult();
result.Success = true;
result.Message = "撤销匹配成功";
tran.Commit();
}
catch (Exception e)
{
tran.Rollback();
result.Success = false;
result.Message = "撤销匹配出现错误,请重试或联系系统管理员"+e.Message;
return result;
}
}
result.Success = true;
result.Message = "撤销匹配成功";
return result;
}
public static DBResult ImpBankData(DataTable table,string pcno, string userid, string username, string companyid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
var _date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
try
{
int num = 0;
foreach (DataRow row in table.Rows)
{
if (row[1].ToString() != "")
{
var TRANSTYPE = Convert.ToString(row["交易类型[ Transaction Type ]"]).Replace("\"","").Replace("\t", "");
var DRAWEE_NAME=Convert.ToString(row["付款人名称[ Payer's Name ]"]).Replace("\"", "").Replace("\t", "");
if (!string.IsNullOrEmpty(TRANSTYPE) && TRANSTYPE== "来账"&& DRAWEE_NAME.IndexOf("经汉")<0)
{
num = num + 1;
var headData = new ChBankdata();
headData.GID = System.Guid.NewGuid().ToString();
headData.DbOperationType = DbOperationType.DbotIns;
headData.PCNO = Convert.ToInt32(pcno)+1;
headData.SFNO = num;
headData.TRANSTYPE = TRANSTYPE;
headData.DRAWEE_BANK = Convert.ToString(row["付款人开户行名[ Payer account bank ]"]).Replace("\"", "").Replace("\t", "");
headData.DRAWEE_ACCOUNT = Convert.ToString(row["付款人账号[ Debit Account No. ]"]).Replace("\"", "").Replace("\t", "");
headData.DRAWEE_NAME = DRAWEE_NAME;
headData.PAYEE_BANK = Convert.ToString(row["收款人开户行名[ Beneficiary account bank ]"]).Replace("\"", "").Replace("\t", "");
headData.PAYEE_ACCOUNT = Convert.ToString(row["收款人账号[ Payee's Account Number ]"]).Replace("\"", "").Replace("\t", "");
headData.PAYEE_NAME = Convert.ToString(row["收款人名称[ Payee's Name ]"]).Replace("\"", "").Replace("\t", "");
var transdate= Convert.ToString(row["交易日期[ Transaction Date ]"]).Replace("\"", "");
transdate = transdate.Substring(0, 4) + "-" + transdate.Substring(4, 2) + "-" + transdate.Substring(6, 2);
transdate= transdate+" "+ Convert.ToString(row["交易时间[ Transaction time ]"]).Replace("\"", "");
headData.TRANSDATE = transdate.Replace("\t","");
var curr= Convert.ToString(row["交易货币[ Trade Currency ]"]).Replace("\"", "").Replace("\t", "");
if (curr == "CNY") headData.CURRENCY = "RMB";
headData.AMOUNT =Convert.ToDecimal(Convert.ToString(row["交易金额[ Trade Amount ]"]).Replace("\"", "").Replace("\t", ""));
headData.TRANSNO= Convert.ToString(row["交易流水号[ Transaction reference number ]"]).Replace("\"", "").Replace("\t", "");
headData.ATTITIONAL = Convert.ToString(row["交易附言[ Remark ]"]).Replace("\"", "").Replace("\t", "");
headData.SUMMARY = Convert.ToString(row["摘要[ Reference ]"]).Replace("\"", "").Replace("\t", "");
headData.REMARK = Convert.ToString(row["备注[ Remarks ]"]).Replace("\"", "").Replace("\t", "");
headData.IMPORTER = username;
headData.CREATEUSER = userid;
headData.IMPORTDATE= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
headData.CREATETIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
headData.CORPID = companyid;
var modb = new ModelObjectDB();
result = modb.Save(headData);
}
}
}
result.Success = true;
result.Message = "导入成功";
}
catch (Exception ex)
{
result.Success = false;
result.Message = "导入中出错:" + ex.Message;
}
return result;
}
public static DBResult ImpBankData2(DataTable table, string pcno, string userid, string username, string companyid)
{
var result = new DBResult();
Database db = DatabaseFactory.CreateDatabase();
var _date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
try
{
int num = 0;
foreach (DataRow row in table.Rows)
{
if (row[0].ToString() != ""&& row[0].ToString() != "流水号" && row[1].ToString() != "交易日期")
{
var TRANSTYPE = Convert.ToString(row["交易类型"]);
var DRAWEE_NAME = Convert.ToString(row["对方户名"]);
var DR = Convert.ToString(row["收入金额"]);
var CR = Convert.ToString(row["支出金额"]);
var ACCOUNT = Convert.ToString(row["对方账号"]);
var BANK = Convert.ToString(row["对方开户行"]);
var CURR = Convert.ToString(row["币种"]);
num = num + 1;
var headData = new ChBankdata();
headData.GID = System.Guid.NewGuid().ToString();
headData.DbOperationType = DbOperationType.DbotIns;
headData.PCNO = Convert.ToInt32(pcno) + 1;
headData.SFNO = num;
if (!string.IsNullOrEmpty(DR))
{
headData.TRANSTYPE ="收入";
headData.DRAWEE_BANK = BANK;
headData.DRAWEE_ACCOUNT = ACCOUNT;
headData.DRAWEE_NAME = DRAWEE_NAME;
headData.PAYEE_BANK = "";
headData.PAYEE_ACCOUNT ="";
headData.PAYEE_NAME ="";
} else {
headData.TRANSTYPE = "支出";
headData.DRAWEE_BANK ="";
headData.DRAWEE_ACCOUNT ="";
headData.DRAWEE_NAME ="";
headData.PAYEE_BANK = BANK;
headData.PAYEE_ACCOUNT = ACCOUNT;
headData.PAYEE_NAME = DRAWEE_NAME;
}
var transdate = Convert.ToString(row["交易日期"]);
//transdate = transdate.Substring(0, 4) + "-" + transdate.Substring(4, 2) + "-" + transdate.Substring(6, 2);
//transdate = transdate + " " + Convert.ToString(row["交易时间[ Transaction time ]"]).Replace("\"", "");
headData.TRANSDATE = transdate;
var curr = Convert.ToString(row["币种"]);
if (curr == "人民币") headData.CURRENCY = "RMB";
if (curr == "美元") headData.CURRENCY = "USD";
headData.AMOUNT = Convert.ToDecimal(Convert.ToString(DR+CR));
headData.TRANSNO = Convert.ToString(row["流水号"]);
headData.ATTITIONAL = TRANSTYPE;
headData.SUMMARY = Convert.ToString(row["核心流水号"]);
headData.REMARK = Convert.ToString(row["备注"]).Replace("\"", "").Replace("\t", "");
headData.IMPORTER = username;
headData.CREATEUSER = userid;
headData.IMPORTDATE = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
headData.CREATETIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
headData.CORPID = companyid;
var modb = new ModelObjectDB();
result = modb.Save(headData);
}
}
result.Success = true;
result.Message = "导入成功";
}
catch (Exception ex)
{
result.Success = false;
result.Message = "导入中出错:" + ex.Message;
}
return result;
}
public static System.Data.DataTable CSV2DataTable(string fileName, out string strMsg)
{
var result = new DBResult();
System.Data.DataTable dt=new System.Data.DataTable();
FileStream fs= new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
StreamReader sr=new StreamReader(fs, System.Text.Encoding.Default);
//记录每次读取的一行记录
string strLine="";
//记录每行记录中的各字段内容
string[] aryLine;
//标示列数
int columnCount=0;
int headcount = 0;
int startline = 8;
int line = 0;
//标示是否是读取的第一行
bool IsFirst=true;
try
{
//逐行读取CSV中的数据
while ((strLine = sr.ReadLine()) != null)
{
line = line + 1;
if (line>= startline&&!string.IsNullOrEmpty(strLine))
{
if (IsFirst == true)
{
aryLine = strLine.Split(',');
IsFirst = false;
headcount = aryLine.Length;
//创建列
for (int i = 0; i < headcount; i++)
{
DataColumn dc = new DataColumn(aryLine[i]);
dt.Columns.Add(dc);
}
}
else
{
string[] stringSeparators = new string[] { "\",\"" };
aryLine = strLine.Split(stringSeparators, StringSplitOptions.None);
columnCount = aryLine.Length;
if (columnCount < headcount) {
aryLine = strLine.Split(',');
columnCount = aryLine.Length;
}
DataRow dr = dt.NewRow();
for (int j = 0; j < columnCount; j++)
{
dr[j] = aryLine[j];
}
dt.Rows.Add(dr);
}
}
}
sr.Close();
fs.Close();
strMsg = "";
return dt;
}
catch (Exception ex)
{
strMsg ="导入中出错:" + ex.Message;
return dt;
}
}
}
}