using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Text; using System.Data.SqlClient; using DSWeb.Models; using DSWeb.EntityDA; using System.Text.RegularExpressions; using System.Xml; namespace DSWeb.Invoice { public partial class InvoiceModifyGridSource : System.Web.UI.Page { private string strHandle; private string strCacheName;//缓存名称 private int iCurrencyType;//折算币别类型 1-RMB 2-USD private decimal dExchangeRate;//折算汇率 private string strInvoiceAppID;//发票申请GID private string strDirectFeeID;//手动添加发票费用GID private string strDirectFeeName;//手动添加发票费用名称 private decimal dDirectAmount;//手动添加发票费用金额 protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["handle"] != null) { strHandle = Request.QueryString["handle"].ToString(); } if (Request.QueryString["cachename"] != null) { strCacheName = Request.QueryString["cachename"].ToString(); } if (Request.QueryString["settlectype"] != null) { iCurrencyType = int.Parse(Request.QueryString["settlectype"].ToString()); } else { iCurrencyType = 0; } if(Request.QueryString["invoiceappid"] != null) { strInvoiceAppID = Request.QueryString["invoiceappid"].ToString(); } if (Request.QueryString["rate"] != null) { dExchangeRate = decimal.Parse(Request.QueryString["rate"].ToString()); } if (Request.QueryString["directfeeid"] != null) { strDirectFeeID = Request.QueryString["directfeeid"].ToString(); } if (Request.QueryString["directfeename"] != null) { strDirectFeeName = UnicodeToGB(Request.QueryString["directfeename"].ToString()); } if (Request.QueryString["directfeeamount"] != null) { dDirectAmount = decimal.Parse(Request.QueryString["directfeeamount"].ToString()); } else { dDirectAmount = 0; } if (strHandle != null) { if (strHandle == "getinvoiceapp" && strCacheName != null && iCurrencyType != 0) { Response.Write(GetInvoiceApplicationJsonFeeList(strCacheName, iCurrencyType, dExchangeRate)); } if (strHandle == "getinvoiceapptotal" && strCacheName != null && iCurrencyType != 0) { Response.Write(GetInvoiceApplicationTotalList(strCacheName, iCurrencyType, dExchangeRate)); } if (strHandle == "getinvoiceappmake" && strInvoiceAppID != null) { Response.Write(GetInvoiceApplicationToMakeOutFeeList(strInvoiceAppID)); } if (strHandle == "addfee" && strCacheName != null) { Response.Write(AddDirectInvoiceFee(strCacheName, strDirectFeeID, iCurrencyType, strDirectFeeName, dDirectAmount)); } if (strHandle == "deletefee" && strCacheName != null) { Response.Write(DeleteDirectInvoiceFee(strCacheName, strDirectFeeID, iCurrencyType)); } if (strHandle == "getdirectfee" && strCacheName != null && iCurrencyType != 0) { Response.Write(GetDirectMakeOutFeeList(strCacheName)); } if (strHandle == "getdirectfeetotal" && strCacheName != null && iCurrencyType != 0) { } } } #region 手动添加费用明细 /// /// 手动添加费用明细 /// /// 缓存名称 /// 费用GID /// 开出币别 1-RMB 2-USD /// 费用名称 /// 费用金额 /// 返回合计总数 private string AddDirectInvoiceFee(string tempCacheName,string tempDirectFeeID,int tempCurrency,string tempFeeName,decimal tempAmount) { StringBuilder totalBuilder = new StringBuilder(); DataTable feeTable = new DataTable(); if (Session[tempCacheName] != null) { feeTable = (DataTable)Session[tempCacheName]; int iExistCount = 0; for (int i = 0; i < feeTable.Rows.Count; i++) { if (feeTable.Rows[i]["GID"].ToString().Trim() == tempDirectFeeID) { feeTable.Rows[i]["FEENAME"] = tempFeeName; feeTable.Rows[i]["AMOUNT"] = tempAmount; feeTable.Rows[i]["CTL"] = tempAmount; iExistCount++; break; } } if (iExistCount == 0) { DataRow newRow = feeTable.NewRow(); newRow["GID"] = tempDirectFeeID; newRow["FEENAME"] = tempFeeName; newRow["CURRENCY"] = tempCurrency == 1 ? "RMB" : "USD"; newRow["AMOUNT"] = tempAmount; newRow["CTL"] = tempAmount; newRow["UCTL"] = 0; feeTable.Rows.Add(newRow); } } else { feeTable = CreateFeeTable(); DataRow newRow = feeTable.NewRow(); newRow["GID"] = tempDirectFeeID; newRow["FEENAME"] = tempFeeName; newRow["CURRENCY"] = tempCurrency == 1 ? "RMB" : "USD"; newRow["AMOUNT"] = tempAmount; newRow["CTL"] = tempAmount; newRow["UCTL"] = 0; feeTable.Rows.Add(newRow); } Session[tempCacheName] = feeTable; totalBuilder.Append("{"); totalBuilder.Append("total:"); totalBuilder.Append("["); decimal rmbTotalAmount = 0; decimal usdTotalAmount = 0; for (int i = 0; i < feeTable.Rows.Count; i++) { if (feeTable.Rows[i]["CURRENCY"].ToString().Trim().ToUpper().Equals("RMB")) { rmbTotalAmount += decimal.Parse(feeTable.Rows[i]["CTL"].ToString()); } else if (feeTable.Rows[i]["CURRENCY"].ToString().Trim().ToUpper().Equals("USD")) { usdTotalAmount += decimal.Parse(feeTable.Rows[i]["CTL"].ToString()); } } totalBuilder.Append("{\"finaltotal\":" + (tempCurrency == 1 ? rmbTotalAmount.ToString() : usdTotalAmount.ToString()) + ","); totalBuilder.Append("\"currency\":\"" + (tempCurrency == 1 ? "RMB" : "USD") + "\"}"); totalBuilder.Append("]"); totalBuilder.Append("}"); return totalBuilder.ToString(); } #endregion #region 删除手动添加费用 /// /// 删除手动添加费用 /// /// 缓存名称 /// 手动添加费用GID /// 开出币别 1-RMB 2-USD /// 返回合计总数 private string DeleteDirectInvoiceFee(string tempCacheName, string tempDirectFeeID,int tempCurrency) { StringBuilder totalBuilder = new StringBuilder(); DataTable feeTable = new DataTable(); if (Session[tempCacheName] != null) { feeTable = (DataTable)Session[tempCacheName]; totalBuilder.Append("{"); totalBuilder.Append("total:"); totalBuilder.Append("["); decimal rmbTotalAmount = 0; decimal usdTotalAmount = 0; int iCount = feeTable.Rows.Count; for (int i = 0; i < iCount; i++) { if (feeTable.Rows[i]["GID"].ToString().Trim() == tempDirectFeeID) { feeTable.Rows.RemoveAt(i); iCount = feeTable.Rows.Count; if (feeTable.Rows.Count > 0) { i = iCount - 2; } } else { if (feeTable.Rows[i]["CURRENCY"].ToString().Trim().ToUpper().Equals("RMB")) { rmbTotalAmount += decimal.Parse(feeTable.Rows[i]["CTL"].ToString()); } else if (feeTable.Rows[i]["CURRENCY"].ToString().Trim().ToUpper().Equals("USD")) { usdTotalAmount += decimal.Parse(feeTable.Rows[i]["CTL"].ToString()); } } } if (feeTable.Rows.Count > 0) { Session[tempCacheName] = feeTable; } totalBuilder.Append("{\"finaltotal\":" + (tempCurrency == 1 ? rmbTotalAmount.ToString() : usdTotalAmount.ToString()) + ","); totalBuilder.Append("\"currency\":\"" + (tempCurrency == 1 ? "RMB" : "USD") + "\"}"); totalBuilder.Append("]"); totalBuilder.Append("}"); } else { totalBuilder.Append("{"); totalBuilder.Append("total:"); totalBuilder.Append("["); decimal rmbTotalAmount = 0; decimal usdTotalAmount = 0; totalBuilder.Append("{\"finaltotal\":" + (tempCurrency == 1 ? rmbTotalAmount.ToString() : usdTotalAmount.ToString()) + ","); totalBuilder.Append("\"currency\":\"" + (tempCurrency == 1 ? "RMB" : "USD") + "\"}"); totalBuilder.Append("]"); totalBuilder.Append("}"); } return totalBuilder.ToString(); } #endregion #region 获取手动开票费用明细数据集 /// /// 获取手动开票费用明细数据集 /// /// 缓存名称 /// 返回JSON数据集 private string GetDirectMakeOutFeeList(string tempCacheName) { StringBuilder feeBuilder = new StringBuilder(); DataTable feeTable = new DataTable(); if (Session[tempCacheName] != null) { feeTable = (DataTable)Session[tempCacheName]; } feeBuilder.Append("{"); feeBuilder.Append("invoice:"); feeBuilder.Append("["); if (feeTable.Rows.Count > 0) { for (int i = 0; i < feeTable.Rows.Count; i++) { feeBuilder.Append("{"); feeBuilder.Append("id:\"" + feeTable.Rows[i]["GID"].ToString() + "\","); feeBuilder.Append("feename:\"" + feeTable.Rows[i]["FEENAME"].ToString() + "\","); feeBuilder.Append("currency:\"" + feeTable.Rows[i]["CURRENCY"].ToString() + "\","); feeBuilder.Append("doamount:\"" + feeTable.Rows[i]["CTL"].ToString() + "\","); feeBuilder.Append("origamount:\"" + feeTable.Rows[i]["CTL"].ToString() + "\","); feeBuilder.Append("ftype:\"" + 1 + "\","); feeBuilder.Append("origname:\"" + feeTable.Rows[i]["FEENAME"].ToString() + "\""); if (i == feeTable.Rows.Count - 1) { feeBuilder.Append("}"); } else { if (i == 0 && feeTable.Rows.Count == 1) { feeBuilder.Append("}"); } else { feeBuilder.Append("},"); } } } } feeBuilder.Append("]"); feeBuilder.Append("}"); return feeBuilder.ToString(); } #endregion private string GetInvoiceApplicationToMakeOutFeeList(string tempInvoiceAppGID) { StringBuilder feeBuilder = new StringBuilder(); InvoiceApplicationDA invoiceApplicationDA = new InvoiceApplicationDA(); InvoiceApplicationEntity invoiceApplicationEntity = new InvoiceApplicationEntity(); invoiceApplicationEntity = invoiceApplicationDA.GetInvoiceApplicationByGID(tempInvoiceAppGID); int iCount = 0; if (invoiceApplicationEntity != null) { if (invoiceApplicationEntity.GID != null) { IList feeDoEntities = GetFeeDoXml(invoiceApplicationEntity.FeeItem); foreach (FeeDoEntity feeDoEntity in feeDoEntities) { if (feeDoEntity.Orig == 3) { feeBuilder.Append("{"); feeBuilder.Append("invoice:"); feeBuilder.Append("["); feeBuilder.Append("{"); feeBuilder.Append("id:\"" + feeDoEntity.GID + "\","); feeBuilder.Append("feename:\"" + feeDoEntity.FeeName + "\","); feeBuilder.Append("currency:\"" + invoiceApplicationEntity.Currency + "\","); feeBuilder.Append("doamount:\"" + feeDoEntity.DoAmount + "\","); feeBuilder.Append("origamount:\"" + feeDoEntity.DoAmount + "\","); feeBuilder.Append("ftype:\"" + feeDoEntity.FeeType.ToString() + "\","); feeBuilder.Append("origname:\"" + feeDoEntity.FeeName + "\""); feeBuilder.Append("}"); iCount++; break; } } } } if (iCount == 0) { InvoiceDA invoiceDA = new InvoiceDA(); string strSql = String.Format(" SELECT A.CURRENCY AS MAKEOUTCURRENCY,B.GID,B.FEENAME,B.CURRENCY,B.ORIGAMOUNT,B.EXCHANGERATE,B.FEETYPE FROM ch_fee_invoiceapplication AS A " + " INNER JOIN ch_fee_do AS B ON A.BILLNO = B.BILLNO AND A.COMPANYID = B.COMPANYID WHERE A.GID = '{0}' ", tempInvoiceAppGID); DataTable feeTable = new DataTable(); feeTable = invoiceDA.GetExcuteSql(strSql).Tables[0]; feeBuilder.Append("{"); feeBuilder.Append("invoice:"); feeBuilder.Append("["); for (int i = 0; i < feeTable.Rows.Count; i++) { feeBuilder.Append("{"); string strCurrency = feeTable.Rows[i]["CURRENCY"].ToString().Trim().ToUpper(); decimal rate = decimal.Parse(feeTable.Rows[i]["EXCHANGERATE"].ToString()); decimal feeAmount = decimal.Parse(feeTable.Rows[i]["ORIGAMOUNT"].ToString()); decimal finalAmount = 0; if ((strCurrency == "RMB" && feeTable.Rows[i]["MAKEOUTCURRENCY"].ToString().Trim().ToUpper() == "USD") || (strCurrency == "USD" && feeTable.Rows[i]["MAKEOUTCURRENCY"].ToString().Trim().ToUpper() == "RMB")) { finalAmount = decimal.Parse(C1Round(double.Parse((feeAmount * rate).ToString()), 2).ToString()); } else { finalAmount = feeAmount; } feeBuilder.Append("id:\"" + feeTable.Rows[i]["GID"].ToString() + "\","); feeBuilder.Append("feename:\"" + feeTable.Rows[i]["FEENAME"].ToString() + "\","); feeBuilder.Append("currency:\"" + feeTable.Rows[i]["CURRENCY"].ToString() + "\","); feeBuilder.Append("doamount:\"" + finalAmount.ToString() + "\","); feeBuilder.Append("origamount:\"" + feeAmount.ToString() + "\","); feeBuilder.Append("ftype:\"" + feeTable.Rows[i]["FEETYPE"].ToString() + "\","); feeBuilder.Append("origname:\"" + feeTable.Rows[i]["FEENAME"].ToString() + "\""); if (i == feeTable.Rows.Count - 1) { feeBuilder.Append("}"); } else { if (i == 0 && feeTable.Rows.Count == 1) { feeBuilder.Append("}"); } else { feeBuilder.Append("},"); } } } } feeBuilder.Append("]"); feeBuilder.Append("}"); return feeBuilder.ToString(); } #region 获取发票申请费用明细信息 /// /// 获取发票申请费用明细信息 /// /// 缓存名称 /// 结算币别类型 1-RMB 2-USD /// 折算汇率 /// 返回JSON数据集 private string GetInvoiceApplicationJsonFeeList(string tempCacheName,int tempSettleType,decimal tempRate) { StringBuilder feeBuilder = new StringBuilder(); DataTable bsnoTable = null; DataTable feeTable = null; DataTable totalTable = null; DataSet postSet = null; if (Session[tempCacheName] != null) { postSet = (DataSet)Session[tempCacheName]; } bsnoTable = (DataTable)postSet.Tables[0]; feeTable = (DataTable)postSet.Tables[1]; totalTable = (DataTable)postSet.Tables[2]; feeBuilder.Append("{"); feeBuilder.Append("invoice:"); feeBuilder.Append("["); for (int i = 0; i < feeTable.Rows.Count; i++) { feeBuilder.Append("{"); string strCurrency = feeTable.Rows[i]["CURRENCY"].ToString().Trim().ToUpper(); decimal feeAmount = decimal.Parse(feeTable.Rows[i]["CTL"].ToString()); decimal finalAmount = 0; if ((strCurrency == "RMB" && tempSettleType == 2) || (strCurrency == "USD" && tempSettleType == 1)) { finalAmount = decimal.Parse(C1Round(double.Parse((feeAmount * dExchangeRate).ToString()), 2).ToString()); } else { finalAmount = feeAmount; } feeBuilder.Append("id:\"" + feeTable.Rows[i]["GID"].ToString() + "\","); feeBuilder.Append("feename:\"" + feeTable.Rows[i]["FEENAME"].ToString() + "\","); feeBuilder.Append("currency:\"" + feeTable.Rows[i]["CURRENCY"].ToString() + "\","); feeBuilder.Append("doamount:\"" + finalAmount.ToString() + "\","); feeBuilder.Append("origamount:\"" + feeAmount.ToString() + "\","); feeBuilder.Append("ftype:\"" + feeTable.Rows[i]["FEETYPE"].ToString() + "\","); feeBuilder.Append("origname:\"" + feeTable.Rows[i]["FEENAME"].ToString() + "\""); if (i == feeTable.Rows.Count - 1) { feeBuilder.Append("}"); } else { if (i == 0 && feeTable.Rows.Count == 1) { feeBuilder.Append("}"); } else { feeBuilder.Append("},"); } } } feeBuilder.Append("]"); feeBuilder.Append("}"); return feeBuilder.ToString(); } #endregion private string GetInvoiceApplicationTotalList(string tempCacheName, int tempSettleType, decimal tempRate) { StringBuilder totalBuilder = new StringBuilder(); DataTable bsnoTable = null; DataTable feeTable = null; DataTable totalTable = null; DataSet postSet = null; if (Session[tempCacheName] != null) { postSet = (DataSet)Session[tempCacheName]; } bsnoTable = (DataTable)postSet.Tables[0]; feeTable = (DataTable)postSet.Tables[1]; totalTable = (DataTable)postSet.Tables[2]; //if (feeTable.Rows.Count > 0) //{ // DataRow newRow = feeTable.NewRow(); // newRow["GID"] = Guid.NewGuid().ToString(); // newRow["BSNO"] = ""; // newRow["FEETYPE"] = 1; // newRow["CUSTOMERNAME"] = feeTable.Rows[0]["CUSTOMERNAME"].ToString(); // newRow["FEENAME"] = tempRename; // tempTable.Columns.Add("CURRENCY"); // tempTable.Columns.Add("AMOUNT"); // tempTable.Columns.Add("CTL"); // tempTable.Columns.Add("UCTL"); //} totalBuilder.Append("{"); totalBuilder.Append("total:"); totalBuilder.Append("["); for (int i = 0; i < totalTable.Rows.Count; i++) { totalBuilder.Append("{\"recvrmb\":" + totalTable.Rows[0]["RECVRMB"].ToString() + ","); totalBuilder.Append("\"recvusd\":" + totalTable.Rows[0]["RECVUSD"].ToString() + ","); totalBuilder.Append("\"payrmb\":" + totalTable.Rows[0]["PAYRMB"].ToString() + ","); totalBuilder.Append("\"payusd\":" + totalTable.Rows[0]["PAYUSD"].ToString() + ","); decimal rmbTotalAmount = decimal.Parse(totalTable.Rows[0]["TOTALRMB"].ToString()); totalBuilder.Append("\"totalrmb\":" + rmbTotalAmount.ToString() + ","); decimal usdTotalAmount = decimal.Parse(totalTable.Rows[0]["TOTALUSD"].ToString()); totalBuilder.Append("\"totalusd\":" + usdTotalAmount.ToString() + ","); decimal finalTotalAmount = 0; if (tempSettleType == 1) { finalTotalAmount = rmbTotalAmount + decimal.Parse(C1Round(double.Parse((usdTotalAmount * dExchangeRate).ToString()), 2).ToString()); } else { finalTotalAmount = usdTotalAmount + decimal.Parse(C1Round(double.Parse((rmbTotalAmount * dExchangeRate).ToString()), 2).ToString()); } totalBuilder.Append("\"finaltotal\":" + finalTotalAmount.ToString() + "}"); } totalBuilder.Append("]"); totalBuilder.Append("}"); return totalBuilder.ToString(); } #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 private DataTable CreateFeeTable() { DataTable tempTable = new DataTable(); tempTable.Columns.Add("GID"); tempTable.Columns.Add("BSNO"); tempTable.Columns.Add("FEETYPE"); tempTable.Columns.Add("CUSTOMERNAME"); tempTable.Columns.Add("FEENAME"); tempTable.Columns.Add("CURRENCY"); tempTable.Columns.Add("AMOUNT"); tempTable.Columns.Add("CTL"); tempTable.Columns.Add("UCTL"); return tempTable; } #region Unicode-GB Code转换 /// /// Unicode-GB Code转换 /// /// 将Unicode编码字符转换成GB编码字符 /// GB Code字符串 public string UnicodeToGB(string text) { UnicodeEncoding unicode = new UnicodeEncoding(); text = unicode.GetString(unicode.GetBytes(Regex.Unescape(text.Trim()))); return text; } /// /// Unicode-GB Code转换 /// /// 将Unicode编码字符转换成GB编码字符 /// GB Code字符串 public string UnicodeToGB_Old(string text) { MatchCollection mc = Regex.Matches(text, "([\\w]+)|(\\\\u([\\w]{4}))"); if (mc != null && mc.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (Match m2 in mc) { string v = m2.Value; if (v.IndexOf("\\") >= 0) { string word = v.Substring(2); byte[] codes = new byte[2]; int code = Convert.ToInt32(word.Substring(0, 2), 16); int code2 = Convert.ToInt32(word.Substring(2), 16); codes[0] = (byte)code2; codes[1] = (byte)code; sb.Append(Encoding.Unicode.GetString(codes)); } else { sb.Append(v); } } return sb.ToString(); } else { return text; } } #endregion #region 读取FeeItem费用明细数据 /// /// 读取FeeItem费用明细数据 /// /// XML字符串 /// 返回实体类FeeDoEntity private IList GetFeeDoXml(string strXml) { XmlDocument xmlFeeDoDoc = new XmlDocument(); IList feeDoEntities = new List(); try { xmlFeeDoDoc.LoadXml(strXml); for (int i = 0; i < xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes.Count; i++) { FeeDoEntity feeDoEntity = new FeeDoEntity(); feeDoEntity.FeeID = xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[0].InnerText.Trim(); feeDoEntity.FeeName = xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[1].InnerText.Trim(); feeDoEntity.DoAmount = decimal.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[2].InnerText.Trim()); feeDoEntity.Category = int.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[3].InnerText.Trim()); feeDoEntity.FeeType = int.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[4].InnerText.Trim()); feeDoEntity.Orig = int.Parse(xmlFeeDoDoc.ChildNodes[0].ChildNodes[0].ChildNodes[i].ChildNodes[5].InnerText.Trim()); feeDoEntities.Add(feeDoEntity); } } catch (Exception error) { throw (error); } return feeDoEntities; } #endregion } }