using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsRptCwFunds; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.EntityDA; using HcUtility.Comm; using System.Web; using System.Text.RegularExpressions; using DSWeb.MvcShipping.Models.MonthDataSet; using DSWeb.Areas.CommMng.Models; namespace DSWeb.Areas.MvcShipping.DAL.MsRptCwFundsDAL { public class MsRptOpProfitZouDAL { static public List ListData(string condition, string sort, string startdate, string account, string userid, string username, string companyid) { var strDa = GetRangDAStr("index",userid, username,companyid); if (!string.IsNullOrEmpty(strDa)) { if (!string.IsNullOrEmpty(condition)) { condition = condition + " and " + strDa; } else { condition = strDa; } } var startdata=new CwFunds(); if (startdate!="") startdata = GetStartData(condition,startdate, account); var strSql = new StringBuilder(); strSql.Append("SELECT * "); strSql.Append(",(select BANKNAME from [sys_bank] where GID=B.ACCOUNT) as ACCOUNTREF"); strSql.Append(",(CASE WHEN FEETYPE=1 THEN '收' ELSE '付' END) as FEETYPEREF"); strSql.Append(" FROM v_funds B "); strSql.Append(" WHERE FEENAME='期初余额' "); if (!string.IsNullOrEmpty(condition)) { strSql.Append(" AND " + condition); } strSql.Append(" ORDER BY BILLDATE DESC"); var startbank = SetBankStartData(strSql); var strSql2 = new StringBuilder(); strSql2.Append("SELECT * "); strSql2.Append(",(select BANKNAME from [sys_bank] where GID=B.ACCOUNT) as ACCOUNTREF"); strSql2.Append(",(CASE WHEN FEETYPE=1 THEN '收' ELSE '付' END) as FEETYPEREF"); strSql2.Append(" FROM v_funds B "); strSql2.Append(" WHERE FEENAME<>'期初余额' "); if (!string.IsNullOrEmpty(condition)) { strSql2.Append(" AND " + condition); } strSql2.Append(" ORDER BY BILLDATE DESC"); return SetData(strSql2, startdata, startbank); } private static List SetData(StringBuilder strSql, CwFunds stardata,List bankdata) { var headList = new List(); decimal balace =0; decimal balaceusd = 0; decimal balacehkd = 0; if (stardata != null&&(stardata.BALANCE!=0||stardata.BALANCEUSD!=0||stardata.BALANCEHKD!=0)) { headList.Add(stardata); balace = stardata.BALANCE; balaceusd = stardata.BALANCEUSD; balacehkd = stardata.BALANCEHKD; } if (bankdata != null) { foreach (var bill in bankdata) { headList.Add(bill); } } Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CwFunds data = new CwFunds(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.BILLDATE = Convert.ToDateTime(reader["BILLDATE"]).ToString("yyyy-MM-dd"); data.BILLNO = Convert.ToString(reader["BILLNO"]); data.CUSTOMERNAME = Convert.ToString(reader["CUSTOMERNAME"]); data.FEENAME = Convert.ToString(reader["FEENAME"]); data.CURRENCY = Convert.ToString(reader["CURRENCY"]); data.ACCOUNT = Convert.ToString(reader["ACCOUNT"]); data.ACCOUNTREF = Convert.ToString(reader["ACCOUNTREF"]); data.FEETYPE = Convert.ToString(reader["FEETYPEREF"]); data.BILLTYPE = Convert.ToString(reader["BILLTYPE"]); data.REMARK = Convert.ToString(reader["REMARK"]); data.SETTLEUSER = Convert.ToString(reader["SETTLEUSER"]); if (reader["SETTLETIME"] != DBNull.Value) data.SETTLETIME = Convert.ToDateTime(reader["SETTLETIME"]).ToString("yyyy-MM-dd"); if (reader["EXCHANGERATE"] != DBNull.Value) data.EXCHANGERATE = Convert.ToDecimal(reader["EXCHANGERATE"]); if (reader["AMOUNT"] != DBNull.Value) { if (Convert.ToString(reader["FEETYPE"]) == "1") { if (Convert.ToString(reader["CURRENCY"]) == "RMB") { data.AMOUNTDR = Convert.ToDecimal(reader["AMOUNT"]); balace = balace + data.AMOUNTDR; data.BALANCEHKD = balacehkd; data.BALANCE = balace; data.BALANCEUSD = balaceusd; } else if (Convert.ToString(reader["CURRENCY"]) == "USD") { data.AMOUNTUSDDR = Convert.ToDecimal(reader["AMOUNT"]); balaceusd = balaceusd + data.AMOUNTUSDDR; data.BALANCEHKD = balacehkd; data.BALANCE = balace; data.BALANCEUSD = balaceusd; } else if (Convert.ToString(reader["CURRENCY"]) == "HKD") { balacehkd = balacehkd + Convert.ToDecimal(reader["AMOUNT"]); data.BALANCEHKD = balacehkd; data.BALANCE = balace; data.BALANCEUSD = balaceusd; } data.DR = Convert.ToDecimal(reader["AMOUNT"]); } else { if (Convert.ToString(reader["CURRENCY"]) == "RMB") { data.AMOUNTCR = Convert.ToDecimal(reader["AMOUNT"]); balace = balace- data.AMOUNTCR; data.BALANCE = balace; data.BALANCEUSD = balaceusd; data.BALANCEHKD = balacehkd; } else if (Convert.ToString(reader["CURRENCY"]) == "USD") { data.AMOUNTUSDCR = Convert.ToDecimal(reader["AMOUNT"]); balaceusd = balaceusd - data.AMOUNTUSDCR; data.BALANCEUSD = balaceusd; data.BALANCE = balace; data.BALANCEHKD = balacehkd; } else if (Convert.ToString(reader["CURRENCY"]) == "HKD") { balacehkd = balacehkd -Convert.ToDecimal(reader["AMOUNT"]); data.BALANCEHKD = balacehkd; data.BALANCE = balace; data.BALANCEUSD = balaceusd; } data.CR = Convert.ToDecimal(reader["AMOUNT"]); } } #endregion headList.Add(data); } reader.Close(); } return headList; } private static List SetBankStartData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CwFunds data = new CwFunds(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.BILLDATE = Convert.ToDateTime(reader["BILLDATE"]).ToString("yyyy-MM-dd"); data.BILLNO = Convert.ToString(reader["BILLNO"]); data.CUSTOMERNAME = Convert.ToString(reader["CUSTOMERNAME"]); data.FEENAME = Convert.ToString(reader["FEENAME"]); data.CURRENCY = Convert.ToString(reader["CURRENCY"]); data.ACCOUNT = Convert.ToString(reader["ACCOUNT"]); data.ACCOUNTREF = Convert.ToString(reader["ACCOUNTREF"]); data.FEETYPE = Convert.ToString(reader["FEETYPEREF"]); data.BILLTYPE = Convert.ToString(reader["BILLTYPE"]); data.REMARK = Convert.ToString(reader["REMARK"]); data.SETTLEUSER = Convert.ToString(reader["SETTLEUSER"]); data.SETTLETIME = Convert.ToDateTime(reader["SETTLETIME"]).ToString("yyyy-MM-dd"); if (reader["EXCHANGERATE"] != DBNull.Value) data.EXCHANGERATE = Convert.ToDecimal(reader["EXCHANGERATE"]); if (reader["AMOUNT"] != DBNull.Value) { if (Convert.ToString(reader["FEETYPE"]) == "1") { data.DR = Convert.ToDecimal(reader["AMOUNT"]); data.CR = 0; } else { data.CR = Convert.ToDecimal(reader["AMOUNT"]); } } #endregion headList.Add(data); } reader.Close(); } return headList; } static public CwFunds GetStartData(string condition, string startdate, string account) { var strSql = new StringBuilder(); strSql.Append("SELECT SUM(CASE WHEN CURRENCY='RMB' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE 0 END) AS AMOUNTRMB "); strSql.Append(",SUM(CASE WHEN CURRENCY='USD' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE 0 END) AS AMOUNTUSD"); strSql.Append(",SUM(CASE WHEN CURRENCY='HKD' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE 0 END) AS AMOUNTHKD from v_funds"); strSql.Append(" where BILLDATE<'" + startdate + "'"); if (account != "") { strSql.Append(" AND ACCOUNT='" + account + "'"); } Database db = DatabaseFactory.CreateDatabase(); var evList = new List(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CwFunds Data = new CwFunds(); Data.REMARK ="以前结余"; if (reader["AMOUNTRMB"] != DBNull.Value) Data.BALANCE = Convert.ToDecimal(reader["AMOUNTRMB"]); if (reader["AMOUNTUSD"] != DBNull.Value) Data.BALANCEUSD = Convert.ToDecimal(reader["AMOUNTUSD"]); if (reader["AMOUNTHKD"] != DBNull.Value) Data.BALANCEHKD = Convert.ToDecimal(reader["AMOUNTHKD"]); evList.Add(Data); } reader.Close(); } if (evList.Count > 0) return evList[0]; else return new CwFunds(); } static public List GetSumData(string condition, string nocondition) { var nodc = GetNODRCRData(nocondition); var strSql = new StringBuilder(); strSql.Append("SELECT SUM(CASE WHEN CURRENCY='RMB' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE 0 END) AS AMOUNTRMB "); strSql.Append(",SUM(CASE WHEN CURRENCY='USD' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE 0 END) AS AMOUNTUSD"); strSql.Append(",SUM(CASE WHEN CURRENCY='HKD' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE 0 END) AS AMOUNTHKD "); strSql.Append(",SUM(CASE WHEN CURRENCY='RMB' THEN (CASE WHEN FEETYPE=1 THEN AMOUNT ELSE -AMOUNT END) ELSE (CASE WHEN FEETYPE=1 THEN AMOUNT*ISNULL(EXCHANGERATE,1) ELSE -AMOUNT*ISNULL(EXCHANGERATE,1) END) END) AS AMOUNTTTL from v_funds"); if (!string.IsNullOrEmpty(condition)) { strSql.Append(" WHERE " + condition); } Database db = DatabaseFactory.CreateDatabase(); var evList = new List(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CwFundsSum Data = new CwFundsSum(); if (reader["AMOUNTRMB"] != DBNull.Value) Data.BALANCERMB = Convert.ToDecimal(reader["AMOUNTRMB"]); if (reader["AMOUNTUSD"] != DBNull.Value) Data.BALANCEUSD = Convert.ToDecimal(reader["AMOUNTUSD"]); if (reader["AMOUNTHKD"] != DBNull.Value) Data.BALANCEHKD = Convert.ToDecimal(reader["AMOUNTHKD"]); if (reader["AMOUNTTTL"] != DBNull.Value) Data.BALANCETTL = Convert.ToDecimal(reader["AMOUNTTTL"]); Data.NOCRTTL=nodc.NOCRTTL; Data.NODRTTL = nodc.NODRTTL; Data.TTL = Data.BALANCETTL + Data.NODRTTL - Data.NOCRTTL; evList.Add(Data); } reader.Close(); } return evList; } static public CwFundsSum GetNODRCRData(string condition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("isnull(SUM(CASE FEETYPE WHEN 1 THEN ISNULL(AMOUNT-SETTLEMENT,0) * ISNULL(EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLDR,"); strSql.Append("isnull(SUM(CASE FEETYPE WHEN 2 THEN ISNULL(AMOUNT-SETTLEMENT,0) *ISNULL( EXCHANGERATE,0) ELSE 0 END),0) AS BALTTLCR"); strSql.Append(" FROM CH_FEE F "); strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) "); if (!string.IsNullOrEmpty(condition)) { strSql.Append(" WHERE " + condition); } Database db = DatabaseFactory.CreateDatabase(); var evList = new List(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CwFundsSum Data = new CwFundsSum(); if (reader["BALTTLDR"] != DBNull.Value) Data.NODRTTL = Convert.ToDecimal(reader["BALTTLDR"]); if (reader["BALTTLCR"] != DBNull.Value) Data.NOCRTTL = Convert.ToDecimal(reader["BALTTLCR"]); evList.Add(Data); } reader.Close(); } if (evList.Count > 0) return evList[0]; else return new CwFundsSum(); } public static string GetRangDAStr(string tb, string userid, string usercode, string companyid) { string str = ""; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" VISIBLERANGE,OPERATERANGE "); strSql.Append(" from VW_User_Authority "); strSql.Append(" where [NAME]='modcwfundsRange' and USERID='" + userid + "' and ISDELETE=0"); string visiblerange = "4"; string operaterange = "4"; Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { visiblerange = Convert.ToString(reader["VISIBLERANGE"]); operaterange = Convert.ToString(reader["OPERATERANGE"]); break; } reader.Close(); } if (visiblerange == "4") { str = "1=2"; } else if (visiblerange == "3") { str = " (SETTLEUSER='" + usercode + "')"; } else if (visiblerange == "2") { if (tb == "index") { var rangeDa = new RangeDA(); var deptname = rangeDa.GetDEPTNAME(userid); var userstr = new StringBuilder(); userstr.Append(" select SHOWNAME from [user] where GID in (select USERID from user_company where COMPANYID='" + companyid + "') and GID in (select userid from user_baseinfo where DEPTNAME='" + deptname + "')"); Database userdb = DatabaseFactory.CreateDatabase(); using (IDataReader reader = userdb.ExecuteReader(CommandType.Text, userstr.ToString())) { str = ""; while (reader.Read()) { if (str == "") { str = " (SETTLEUSER='" + Convert.ToString(reader["SHOWNAME"]) + "'"; } else { str = str + " or SETTLEUSER='" + Convert.ToString(reader["SHOWNAME"]) + "'"; }; } str = str + ")"; reader.Close(); } } else { } } else if (visiblerange == "1") { } return str; } #region 参照部分 #endregion } }