using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsFeeTemplate; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; using HcUtility.Core; using DSWeb.Areas.Account.Models.MsOpBill; using DSWeb.MvcShipping.DAL.MsChFeeDAL; using DSWeb.MvcShipping.DAL.MsSysParamSet; namespace DSWeb.MvcShipping.DAL.MsFeeTemplate { public partial class MsFeeTemplateDAL { #region Inquery DataList static public List GetDataList(string strCondition, string userid, string companyid, string deptname,string sort = null) { var rangstr = GetRangDAStr("", userid, companyid, deptname); if (!string.IsNullOrEmpty(rangstr)) { if (!string.IsNullOrEmpty(strCondition)) { strCondition = strCondition + " and ((" + rangstr+") or ISPUBLIC='是')"; } else { strCondition = " ((" + rangstr + ") or ISPUBLIC='是')"; } } var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,Name"); strSql.Append(",Description"); strSql.Append(",OpType,FeeType"); strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=96002 and EnumValueID=code_fee_template.OpType) as OpTyperef"); strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=96003 and EnumValueID=code_fee_template.FeeType) as FeeTyperef"); strSql.Append(",CreateUser,CreateTime,ModifiedUser,ModifiedTime,Remark"); strSql.Append(" from code_fee_template "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by ModifiedTime Desc,Name"); } return SetData(strSql); } static public FeeTemplate GetData(string condition, string userid, string companyid, string deptname) { FeeTemplate data = null; var list = GetDataList(condition,userid,companyid,deptname); if (list.Count > 0) data = list[0]; if (data == null) { data = new FeeTemplate(); } return data; } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { FeeTemplate data = new FeeTemplate(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.Name = Convert.ToString(reader["Name"]); data.Description = Convert.ToString(reader["Description"]); data.OpType = Convert.ToInt16(reader["OpType"]); data.FeeType = Convert.ToInt16(reader["FeeType"]); data.OpTyperef = Convert.ToString(reader["OpTyperef"]); data.FeeTyperef = Convert.ToString(reader["FeeTyperef"]); data.CreateUser = Convert.ToString(reader["CreateUser"]); data.CreateTime = Convert.ToDateTime(reader["CreateTime"]); data.ModifiedUser = Convert.ToString(reader["ModifiedUser"]); if (reader["ModifiedTime"] != DBNull.Value) data.ModifiedTime = Convert.ToDateTime(reader["ModifiedTime"]); data.Remark = Convert.ToString(reader["Remark"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region detail DataList static public List GetDetailDataList(string strCondition) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,TEMPLATEID"); strSql.Append(",FEECODE,FEENAME,CUSTOMERNAME,CLIENT,UNIT,CURRENCY,UNITPRICE,TAXUNITPRICE"); strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=96001 and EnumValueID=code_fee_templatedetail.CLIENT) as CustomerType"); strSql.Append(",REMARK,SORT,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME,FEETYPE,EXCHANGERATE,ISCTN,TAXRATE,TAX,ACCTAXRATE"); strSql.Append(",(case ISCTN when 1 then '是' else '否' end) as ISCTNREF,ISINVOICE,ISADVANCEDPAY,FEEFRT,SALECORP,FEEDESCRIPTION,FEEGROUP"); strSql.Append(" from code_fee_templatedetail "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } strSql.Append(" order by SORT "); return SetDetailData(strSql); } static public FeeTemplateDetail GetDetailData(string condition) { FeeTemplateDetail data = null; var list = GetDetailDataList(condition); if (list.Count > 0) data = list[0]; if (data == null) { data = new FeeTemplateDetail(); } return data; } private static List SetDetailData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { FeeTemplateDetail data = new FeeTemplateDetail(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.TemplateID = Convert.ToString(reader["TEMPLATEID"]); data.FeeCode = Convert.ToString(reader["FEECODE"]); data.FeeName = Convert.ToString(reader["FEENAME"]); data.FEEDESCRIPTION = Convert.ToString(reader["FEEDESCRIPTION"]); data.CustomerName = Convert.ToString(reader["CUSTOMERNAME"]); if (reader["CLIENT"] != DBNull.Value) data.Client = Convert.ToInt16(reader["CLIENT"]); data.CustomerType = Convert.ToString(reader["CustomerType"]); data.Unit = Convert.ToString(reader["UNIT"]); if (reader["UNITPRICE"] != DBNull.Value) data.UnitPrice = Convert.ToDecimal(reader["UNITPRICE"]); if (reader["TAXUNITPRICE"] != DBNull.Value) data.TaxUnitPrice = Convert.ToDecimal(reader["TAXUNITPRICE"]); if (reader["SORT"] != DBNull.Value) data.Sort = Convert.ToInt16(reader["SORT"]); if (reader["FeeType"] != DBNull.Value) data.FeeType = Convert.ToInt16(reader["FeeType"]); data.Currency = Convert.ToString(reader["CURRENCY"]); if (reader["EXCHANGERATE"] != DBNull.Value) data.ExchangeRate = Convert.ToDecimal(reader["EXCHANGERATE"]); if (reader["TAXRATE"] != DBNull.Value) data.TaxRate = Convert.ToDecimal(reader["TAXRATE"]); if (reader["Tax"] != DBNull.Value) data.Tax= Convert.ToDecimal(reader["TAX"]); data.CreateUser = Convert.ToString(reader["CreateUser"]); data.CreateTime = Convert.ToDateTime(reader["CreateTime"]); data.ModifiedUser = Convert.ToString(reader["ModifiedUser"]); if (reader["ModifiedTime"] != DBNull.Value) data.ModifiedTime = Convert.ToDateTime(reader["ModifiedTime"]); data.Remark = Convert.ToString(reader["Remark"]); data.FEEFRT = Convert.ToString(reader["FEEFRT"]); if (reader["ISCTN"] != DBNull.Value) data.ISCTN = Convert.ToBoolean(reader["ISCTN"]); data.ISCTNREF = Convert.ToString(reader["ISCTNREF"]); if (reader["ISINVOICE"] != DBNull.Value) data.ISINVOICE = Convert.ToBoolean(reader["ISINVOICE"]); if (reader["ISADVANCEDPAY"] != DBNull.Value) data.ISADVANCEDPAY = Convert.ToBoolean(reader["ISADVANCEDPAY"]); data.SALECORP= Convert.ToString(reader["SALECORP"]); if (reader["ACCTAXRATE"] != DBNull.Value) data.AccTaxRate = Convert.ToDecimal(reader["ACCTAXRATE"]); else data.AccTaxRate = 0; data.FEEGROUP = Convert.ToString(reader["FEEGROUP"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion public static DBResult UpdateTemplate(string templateid) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmUpdate = db.GetSqlStringCommand("update code_fee_template set MODIFIEDTIME=GETDATE() where GID='" + templateid + "'"); db.ExecuteNonQuery(cmUpdate, tran); tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "更新出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "操作成功"; return result; } public static DBResult SavePiliangUpdate(List billList,List bodyList,string userid, string username,string companyid,string LOCALCURR,string isclienttype) { var result = new DBResult(); var ACCTAXRATESAMETAXRATE =MsSysParamSetDAL.GetData("PARAMNAME='ACCTAXRATESAMETAXRATE'"); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmdInsert = db.GetSqlStringCommand( @"INSERT INTO ch_fee(GID, BSNO, FEETYPE, FEENAME, FEEDESCRIPTION, CUSTOMERTYPE, CUSTOMERNAME, UNIT, UNITPRICE, QUANTITY, AMOUNT, CURRENCY, EXCHANGERATE, REASON, REMARK, COMMISSIONRATE,ENTEROPERATOR, ENTERDATE,ISOPEN, ISADVANCEDPAY, SORT, FEESTATUS, FEEFRT, TAXRATE,NOTAXAMOUNT,ACCTAXRATE,LINENUM,TAXUNITPRICE,MODIFIEDUSER,MODIFIEDTIME,WMSOUTBSNO,PREAMOUNT,INPUTMODE,IsInvoice,LOCALCURR,MANAGER,ISACC) VALUES (@gid,@bsno,@fee_type,@fee_name,@fee_description,@customer_type,@customer_name,@unit,@unit_price,@quantity,@amount,@currency, @exchange_rate,@reason,@remark,@comm_rate,@enter_operator,@enter_date,@is_open,@is_advance_pay,@sort,@fee_status,@fee_frt, @taxrate,@notaxamount,@acctaxrate,@LINENUM,@taxunit_price,@MODIFIEDUSER,@MODIFIEDTIME,@WMSOUTBSNO,@PREAMOUNT,@INPUTMODE,@IsInvoice,@LOCALCURR,@MANAGER,@ISACC) "); var FeeCodeList = MsChFeeDAL.MsChFeeDAL.GetFeeTypeRefList(""); var Currlist = MsChFeeDAL.MsChFeeDAL.GetFeeCurrList(); if (billList != null) { foreach (var enumbillValue in billList) { if (bodyList != null) { foreach (var enumValue in bodyList) { var FeeTempDetaillist = GetDetailDataList("TEMPLATEID='"+enumValue.GID+"'"); if (FeeTempDetaillist!=null&&!enumbillValue.FEESTATUS) { var unitlist = MsChFeeDAL.MsChFeeDAL.GetCtnUnitList(enumbillValue.BSNO, enumbillValue.OPLB,""); var CurrDatelist = MsChFeeDAL.MsChFeeDAL.GetFeeDateCurrList(enumbillValue.OPLB, enumbillValue.BSNO, userid, companyid); foreach (var FeeTempDetail in FeeTempDetaillist) { var unit = unitlist.Find(e => e.UNIT == FeeTempDetail.Unit); if ((FeeTempDetail.ISCTN && unit != null) || (!FeeTempDetail.ISCTN)) { decimal quantity = 1; if (unit != null) { quantity = unit.QUANTITY; } var taxrate = FeeTempDetail.TaxRate; decimal acctaxrate = 0; var taxrateb= Math.Round(1+(taxrate/100), 4, MidpointRounding.AwayFromZero); decimal TaxUnitPrice = 0; if (FeeTempDetail.TaxUnitPrice == 0) TaxUnitPrice = Math.Round(FeeTempDetail.UnitPrice * taxrateb, 4, MidpointRounding.AwayFromZero); else TaxUnitPrice = FeeTempDetail.TaxUnitPrice; var amout= Math.Round(TaxUnitPrice * quantity, 2, MidpointRounding.AwayFromZero); decimal notaxamount = 0; if (amout > 0) { notaxamount= Math.Round(amout / taxrateb, 2, MidpointRounding.AwayFromZero); } else { notaxamount = -Math.Round(Math.Abs(amout) / taxrateb, 2, MidpointRounding.AwayFromZero); } var tax = amout - notaxamount; if (ACCTAXRATESAMETAXRATE.PARAMVALUE == "1") acctaxrate = taxrate; else acctaxrate = FeeTempDetail.AccTaxRate; decimal exrate = FeeTempDetail.ExchangeRate; if (LOCALCURR == FeeTempDetail.Currency) exrate = 1; if (exrate == 0) { var CurrDate = CurrDatelist.Find(x=>x.CURR==FeeTempDetail.Currency); if (CurrDate != null) { if (FeeTempDetail.FeeType == 2) exrate = CurrDate.CRDEFRATE; else exrate = CurrDate.DEFRATE; } else { var Curr = Currlist.Find(x => x.CURR == FeeTempDetail.Currency); if (FeeTempDetail.FeeType == 2) exrate = Curr.CRDEFRATE; else exrate = Curr.DEFRATE; } } var FeeDescription = ""; var FeeCode = FeeCodeList.Find(x => x.Name == FeeTempDetail.FeeName); if (FeeCode != null) FeeDescription = FeeCode.Description; var customername = ""; var custtype = ""; if (string.IsNullOrEmpty(FeeTempDetail.CustomerName)) { var feecust = GetFeeCust(FeeTempDetail.Client.ToString(), enumbillValue); if (string.IsNullOrEmpty(feecust.CUSTOMERNAME)) { if (enumValue.FeeType==1) feecust = GetFeeCust(FeeCode.DefaultDebit, enumbillValue); else feecust = GetFeeCust(FeeCode.DefaultCredit, enumbillValue); } customername = feecust.CUSTOMERNAME; custtype = feecust.CUSTTYPE; } else { customername = FeeTempDetail.CustomerName; custtype = FeeTempDetail.CustomerType; } cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@gid", DbType.String, Guid.NewGuid().ToString()); db.AddInParameter(cmdInsert, "@bsno", DbType.String, enumbillValue.BSNO); db.AddInParameter(cmdInsert, "@fee_type", DbType.Int16, enumValue.FeeType); db.AddInParameter(cmdInsert, "@fee_name", DbType.String, FeeTempDetail.FeeName); if (isclienttype == "1") { db.AddInParameter(cmdInsert, "@customer_type", DbType.String, custtype); db.AddInParameter(cmdInsert, "@customer_name", DbType.String, customername); } else { db.AddInParameter(cmdInsert, "@customer_type", DbType.String, FeeTempDetail.CustomerType); db.AddInParameter(cmdInsert, "@customer_name", DbType.String, FeeTempDetail.CustomerName); } db.AddInParameter(cmdInsert, "@unit", DbType.String, FeeTempDetail.Unit); db.AddInParameter(cmdInsert, "@unit_price", DbType.Decimal, FeeTempDetail.UnitPrice); db.AddInParameter(cmdInsert, "@fee_description", DbType.String, FeeDescription); db.AddInParameter(cmdInsert, "@quantity", DbType.Decimal, quantity); db.AddInParameter(cmdInsert, "@amount", DbType.Decimal, amout); db.AddInParameter(cmdInsert, "@currency", DbType.String, FeeTempDetail.Currency); db.AddInParameter(cmdInsert, "@exchange_rate", DbType.Decimal, exrate); db.AddInParameter(cmdInsert, "@reason", DbType.String, ""); db.AddInParameter(cmdInsert, "@remark", DbType.String, enumValue.Remark); db.AddInParameter(cmdInsert, "@comm_rate", DbType.Decimal, 0); db.AddInParameter(cmdInsert, "@enter_operator", DbType.String, userid); db.AddInParameter(cmdInsert, "@enter_date", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); db.AddInParameter(cmdInsert, "@is_open", DbType.String,"0"); db.AddInParameter(cmdInsert, "@is_advance_pay", DbType.String,"0"); db.AddInParameter(cmdInsert, "@sort", DbType.Int16, 10); db.AddInParameter(cmdInsert, "@fee_status", DbType.Int16, 1); db.AddInParameter(cmdInsert, "@fee_frt", DbType.String, FeeTempDetail.FEEFRT); db.AddInParameter(cmdInsert, "@taxrate", DbType.Decimal, taxrate); db.AddInParameter(cmdInsert, "@notaxamount", DbType.Decimal, notaxamount); db.AddInParameter(cmdInsert, "@acctaxrate", DbType.Decimal, acctaxrate); db.AddInParameter(cmdInsert, "@LINENUM", DbType.Int16, 0); db.AddInParameter(cmdInsert, "@taxunit_price", DbType.Decimal, TaxUnitPrice); db.AddInParameter(cmdInsert, "@MODIFIEDUSER", DbType.String, userid); db.AddInParameter(cmdInsert, "@MODIFIEDTIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); db.AddInParameter(cmdInsert, "@WMSOUTBSNO", DbType.String,""); db.AddInParameter(cmdInsert, "@PREAMOUNT", DbType.Decimal, 0); db.AddInParameter(cmdInsert, "@INPUTMODE", DbType.String, ""); db.AddInParameter(cmdInsert, "@IsInvoice", DbType.String, "0"); db.AddInParameter(cmdInsert, "@LOCALCURR", DbType.String, LOCALCURR); db.AddInParameter(cmdInsert, "@MANAGER", DbType.String,""); db.AddInParameter(cmdInsert, "@ISACC", DbType.String,"0"); db.ExecuteNonQuery(cmdInsert, tran); } } } } } } } tran.Commit(); result.Success = true; result.Message = "保存成功" + result.Message; } catch (Exception e) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"+e.Message; return result; } } return result; } public static FeeCust GetFeeCust(string clienttype, MsOpBill bill) { var result = new FeeCust(); if (clienttype == "1") { result.CUSTOMERNAME = bill.CARRIER; result.CUSTTYPE = "船公司"; } else if (clienttype =="2") { result.CUSTOMERNAME = bill.FORWARDER; result.CUSTTYPE = "订舱代理"; } else if (clienttype == "3") { result.CUSTOMERNAME = bill.YARD; result.CUSTTYPE = "场站"; } else if (clienttype == "4") { result.CUSTOMERNAME = bill.TRUCKER; result.CUSTTYPE = "车队"; } else if (clienttype == "5") { result.CUSTOMERNAME = bill.CUSTOMERNAME; result.CUSTTYPE = "委托单位"; } else if (clienttype == "6") { result.CUSTOMERNAME = bill.CUSTOMSER; result.CUSTTYPE = "报关行"; } else if (clienttype == "7") { result.CUSTOMERNAME = bill.AGENTID; result.CUSTTYPE = "代理"; } else if (clienttype == "8") { result.CUSTOMERNAME = bill.CARRIER; result.CUSTTYPE = "航空公司"; } else if (clienttype == "9") { result.CUSTOMERNAME = ""; result.CUSTTYPE = ""; } else if (clienttype == "10") { result.CUSTOMERNAME = ""; result.CUSTTYPE = ""; } else if (clienttype == "11") { result.CUSTOMERNAME = bill.SHIPAGENCY; result.CUSTTYPE = "船代"; } return result; } #region 参照部分 public static string GetRangDAStr(string tb, string userid, string companyid, string deptname) { string str = ""; var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(" VISIBLERANGE,OPERATERANGE,AUTHORITYID,VSSQL "); strSql.Append(" from VW_User_Authority "); strSql.Append(" where [NAME]='modCodeFeeTemplateSet' and USERID='" + userid + "' and ISDELETE=0"); string visiblerange = "4"; string operaterange = "4"; string AUTHORITYID = ""; string VSSQL = ""; 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"]); AUTHORITYID = Convert.ToString(reader["AUTHORITYID"]); VSSQL = Convert.ToString(reader["VSSQL"]); break; } reader.Close(); } if (visiblerange == "4") { str = "1=2"; } else if (visiblerange == "3") { if (tb == "index") { str = " CreateUser='" + userid + "'"; } else { str = " CreateUser='" + userid + "'"; } } else if (visiblerange == "2") { str = " CreateUser in (select userid from user_baseinfo where DEPTNAME='" + deptname + "') and CreateUser in (select USERID from user_company where COMPANYID='" + companyid + "') "; } else if (visiblerange == "1") { str = " CreateUser in (select USERID from user_company where COMPANYID='" + companyid + "') "; } else if (visiblerange == "5") { if (tb == "index") { var userstr = new StringBuilder(); userstr.Append(" select COMPANYID from user_authority_range_company where userid='" + userid + "' and AUTHORITYID='" + AUTHORITYID + "' and VISIBLERANGE=1"); Database userdb = DatabaseFactory.CreateDatabase(); using (IDataReader reader = userdb.ExecuteReader(CommandType.Text, userstr.ToString())) { str = ""; while (reader.Read()) { if (str == "") { str = " (CreateUser in (select USERID from user_company where COMPANYID='" + Convert.ToString(reader["COMPANYID"]) + "') "; } else { str = str + " or CreateUser in (select USERID from user_company where COMPANYID='" + Convert.ToString(reader["COMPANYID"]) + "') "; }; } str = str + ")"; reader.Close(); } } else { str = " CreateUser in (select USERID from user_company where COMPANYID='" + companyid + "') "; } } else if (visiblerange == "6") { if (tb == "index") { str = " EXISTS (select 1 from user_authority_range_op P left join [user] u on (u.GID=P.OPID) where CreateUser=U.GID AND P.userid='" + userid + "' and P.AUTHORITYID='" + AUTHORITYID + "' and P.VISIBLERANGE=1) "; } else { str = " CreateUser in (select USERID from user_company where COMPANYID='" + companyid + "') "; } } else if (visiblerange == "0") { str = " 1=1 "; } return str; } #endregion } }