public SumInfo GetSumInfo(List <RateItem> rate_table) { List <string> totalprices = new List <string>(); SumInfo info = new SumInfo(); info.Seller = this.Seller; foreach (OrderListItem item in _items) { info.BiblioCount++; info.CopyCount += item.Copy; totalprices.Add(item.TotalPrice); } if (totalprices.Count > 1) { string strError = ""; List <string> sum_prices = null; int nRet = PriceUtil.TotalPrice(totalprices, out sum_prices, out strError); if (nRet == -1) { info.TotalPrice = strError; } else { // Debug.Assert(sum_prices.Count == 1, ""); // info.TotalPrice = sum_prices[0]; info.TotalPrice = PriceUtil.JoinPriceString(sum_prices); } } else if (totalprices.Count == 1) { info.TotalPrice = totalprices[0]; } // 计算汇率 if (rate_table != null && string.IsNullOrEmpty(info.TotalPrice) == false) { try { string strRatePrice = RateItem.RatePrices( rate_table, info.TotalPrice); info.TotalPrice1 = strRatePrice; if (info.TotalPrice == info.TotalPrice1) { info.TotalPrice1 = ""; } } catch (Exception ex) { info.TotalPrice1 = ex.Message; } } return(info); }
// 为一列的字符串值增量 // 本方法只能应用在string值类型的列上,否则会抛出异常 // parameters: // createValue 如果列单元不存在,则采用此值初始设置 // incValue 如果列单元已经存在,则采用原来的值后面追加此值,修改回 public void IncCurrency( int nColumn, string createValue, string incValue) { EnsureCells(nColumn); if (cells[nColumn] == null) { cells[nColumn] = createValue; } else { object oldvalue = cells[nColumn]; if (oldvalue is string) { string v = (string)oldvalue; // 连接两个价格字符串 v = PriceUtil.JoinPriceString(v, incValue); string strSumPrices = ""; string strError = ""; // 将形如"-123.4+10.55-20.3"的价格字符串归并汇总 int nRet = PriceUtil.SumPrices(v, out strSumPrices, out strError); if (nRet == 0) { v = strSumPrices; } if (nRet == -1) { throw new Exception("汇总金额字符串 '" + v + "' 时出错:" + strError); } // v += incValue; cells[nColumn] = v; } else { throw (new Exception("列" + Convert.ToString(nColumn) + "类型必须为string")); } } }
static void Inc(Table table, string strEntry, int nColumn, string strPrice) { Line line = table.EnsureLine(strEntry); string strOldValue = (string)line[nColumn]; if (string.IsNullOrEmpty(strOldValue) == true) { line.SetValue(nColumn, strPrice); return; } // 连接两个价格字符串 string strPrices = PriceUtil.JoinPriceString(strOldValue, strPrice); string strError = ""; List <string> prices = null; // 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号 // return: // -1 error // 0 succeed int nRet = PriceUtil.SplitPrices(strPrices, out prices, out strError); if (nRet == -1) { throw new Exception(strError); } string strResult = ""; nRet = PriceUtil.TotalPrice(prices, out strResult, out strError); if (nRet == -1) { throw new Exception(strError); } line.SetValue(nColumn, strResult); }
// 将形如"-CNY123.4+USD10.55-20.3"的价格字符串计算汇率 // parameters: public static string RatePrices( List <RateItem> rate_table, string strPrices) { string strError = ""; strPrices = strPrices.Trim(); if (String.IsNullOrEmpty(strPrices) == true) { return(""); } List <string> prices = null; // 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号 // return: // -1 error // 0 succeed int nRet = PriceUtil.SplitPrices(strPrices, out prices, out strError); if (nRet == -1) { throw new Exception(strError); } List <string> changed_prices = new List <string>(); foreach (string price in prices) { CurrencyItem item = CurrencyItem.Parse(price); RateItem rate = FindBySource(rate_table, item.Prefix, item.Postfix); if (rate == null) { changed_prices.Add(price); continue; } CurrencyItem result = rate.Exchange(item); changed_prices.Add(result.ToString()); } List <string> results = new List <string>(); // 汇总价格 // 货币单位不同的,互相独立 // return: // -1 error // 0 succeed nRet = PriceUtil.TotalPrice(changed_prices, out results, out strError); if (nRet == -1) { throw new Exception(strError); } #if NO StringBuilder text = new StringBuilder(); for (int i = 0; i < results.Count; i++) { string strOnePrice = results[i]; if (String.IsNullOrEmpty(strOnePrice) == true) { continue; } if (strOnePrice[0] == '+') { text.Append("+" + strOnePrice.Substring(1)); } else if (strOnePrice[0] == '-') { text.Append("-" + strOnePrice.Substring(1)); } else { text.Append("+" + strOnePrice); // 缺省为正数 } } return(text.ToString().TrimStart(new char[] { '+' })); #endif return(PriceUtil.JoinPriceString(results)); }
// 将 OrderStorage 对象的信息合并到本对象 public void Merge(OrderStore order) { string strError = ""; if (this.Orders == null) { this.Orders = new List <OrderStore>(); } if (this.Orders.IndexOf(order) != -1) { throw new Exception("order 对象已经在 Orders 中存在了,不允许重复合并"); } this.Orders.Add(order); XmlDocument dom = new XmlDocument(); dom.LoadXml(order.Xml); Hashtable value_table = GetValues(order); string strSeller = (string)value_table["seller"]; if (string.IsNullOrEmpty(this.Seller)) { this.Seller = strSeller; } else { if (this.Seller != strSeller) { throw new Exception("this.Seller '" + this.Seller + "' 和即将合并的 order.Seller '" + strSeller + "' 不一致"); } } string strCatalogNo = (string)value_table["catalogNo"]; if (string.IsNullOrEmpty(this.CatalogNo)) { this.CatalogNo = strCatalogNo; } else { if (this.CatalogNo != strCatalogNo) { throw new Exception("this.CatalogNo '" + this.CatalogNo + "' 和即将合并的 order.CatalogNo '" + strCatalogNo + "' 不一致"); } } string strPrice = (string)value_table["price"]; if (string.IsNullOrEmpty(this.Price)) { this.Price = strPrice; } else { if (this.Price != strPrice) { throw new Exception("this.Price '" + this.Price + "' 和即将合并的 order.Price '" + strPrice + "' 不一致"); } } string strAcceptPrice = (string)value_table["acceptPrice"]; if (string.IsNullOrEmpty(this.AcceptPrice)) { this.AcceptPrice = strAcceptPrice; } else { if (this.AcceptPrice != strAcceptPrice) { throw new Exception("this.AcceptPrice '" + this.AcceptPrice + "' 和即将合并的 order.AcceptPrice '" + strAcceptPrice + "' 不一致"); } } string strIssueCount = (string)value_table["issueCount"]; if (string.IsNullOrEmpty(this.IssueCount)) { this.IssueCount = strIssueCount; } else { if (this.IssueCount != strIssueCount) { throw new Exception("this.IssueCount '" + this.IssueCount + "' 和即将合并的 order.IssueCount '" + strIssueCount + "' 不一致"); } } string strRange = (string)value_table["range"]; if (string.IsNullOrEmpty(this.Range)) { this.Range = strRange; } else { if (this.Range != strRange) { throw new Exception("this.Range '" + this.Range + "' 和即将合并的 order.Range '" + strRange + "' 不一致"); } } int nSubCopy = (int)value_table["subcopy"]; if (this.SubCopy == 0) { this.SubCopy = nSubCopy; } else { if (this.SubCopy != nSubCopy) { throw new Exception("this.SubCopy '" + this.SubCopy + "' 和即将合并的 order.SubCopy '" + nSubCopy + "' 不一致"); } } string strSellerAddress = (string)value_table["sellerAddress"]; if (string.IsNullOrEmpty(this.SellerAddress)) { this.SellerAddress = strSellerAddress; } // 以下是需要累加的字段 if (this.MergeComment == null) { this.MergeComment = new List <string>(); } int nCopy = (int)value_table["copy"]; this.Copy += nCopy; string strSource = DomUtil.GetElementText(dom.DocumentElement, "source"); string strMergeComment = strSource + ", " + nCopy.ToString() + "册 (" + order.RecPath + ")"; this.MergeComment.Add(strMergeComment); int nIssueCount = 1; if (string.IsNullOrEmpty(strIssueCount) == false) { Int32.TryParse(strIssueCount, out nIssueCount); } // 汇总价格 List <string> totalprices = new List <string>(); if (string.IsNullOrEmpty(this.TotalPrice) == false) { totalprices.Add(this.TotalPrice); } string strTotalPrice = ""; if (String.IsNullOrEmpty(strPrice) == false) { int nRet = PriceUtil.MultiPrice(strPrice, nCopy * nIssueCount, out strTotalPrice, out strError); if (nRet == -1) { strError = "原始数据事项 " + order.RecPath + " 内价格字符串 '" + strPrice + "' 格式不正确: " + strError; throw new Exception(strError); } totalprices.Add(strTotalPrice); } if (totalprices.Count > 1) { List <string> sum_prices = null; int nRet = PriceUtil.TotalPrice(totalprices, out sum_prices, out strError); if (nRet == -1) { throw new Exception(strError); } // Debug.Assert(sum_prices.Count == 1, ""); // this.TotalPrice = sum_prices[0]; this.TotalPrice = PriceUtil.JoinPriceString(sum_prices); } else if (totalprices.Count == 1) { this.TotalPrice = totalprices[0]; } // 汇总注释 if (this.Comments == null) { this.Comments = new List <string>(); } string strComment = DomUtil.GetElementText(dom.DocumentElement, "comment"); if (String.IsNullOrEmpty(strComment) == false) { this.Comments.Add(strComment + " @" + order.RecPath); } // 汇总馆藏分配字符串 string strDistribute = DomUtil.GetElementText(dom.DocumentElement, "distribute"); if (String.IsNullOrEmpty(strDistribute) == false) { if (String.IsNullOrEmpty(this.Distribute) == true) { this.Distribute = strDistribute; } else { string strLocationString = ""; int nRet = LocationCollection.MergeTwoLocationString(this.Distribute, strDistribute, false, out strLocationString, out strError); if (nRet == -1) { throw new Exception(strError); } this.Distribute = strLocationString; } } }
object AddValue(ColumnDataType datatype, object o1, object o2) { if (o1 == null && o2 == null) { return(null); } if (o1 == null) { return(o2); } if (o2 == null) { return(o1); } if (datatype == ColumnDataType.Auto) { if (o1 is Int64) { return((Int64)o1 + Convert.ToInt64(o2)); // 2016/11/24 } if (o1 is Int32) { return((Int32)o1 + (Int32)o2); } if (o1 is double) { #if NO if (o2 is long) { return((double)o1 + Convert.ToDouble(o2)); } return((double)o1 + (double)o2); #endif return((double)o1 + Convert.ToDouble(o2)); } if (o1 is decimal) { return((decimal)o1 + (decimal)o2); } if (o1 is string) { return((string)o1 + (string)o2); } throw new Exception("无法支持的 Auto 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString()); } if (datatype == ColumnDataType.Number) { if (o1 is Int64) { return((Int64)o1 + (Int64)o2); } if (o1 is Int32) { return((Int32)o1 + (Int32)o2); } if (o1 is double) { return((double)o1 + (double)o2); } if (o1 is decimal) { return((decimal)o1 + (decimal)o2); } if (o1 is string) // 2015/7/16 { Int64 v1 = 0; Int64 v2 = 0; Int64.TryParse(o1 as string, out v1); Int64.TryParse(o2 as string, out v2); return((v1 + v2).ToString()); } throw new Exception("无法支持的 Number 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString()); } if (datatype == ColumnDataType.String) { if (o1 is string) { return((string)o1 + (string)o2); } throw new Exception("无法支持的 String 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString()); } if (datatype == ColumnDataType.Price) // 100倍金额整数 { return((Int64)o1 + (Int64)o2); } if (datatype == ColumnDataType.PriceDouble) // double,用来表示金额。也就是最多只有两位小数部分 -- 注意,有累计误差问题,以后建议废止 { return((double)o1 + (double)o2); } if (datatype == ColumnDataType.PriceDecimal) // decimal,用来表示金额。 { return((decimal)o1 + (decimal)o2); } if (datatype == ColumnDataType.Currency) { #if NO // 这一句容易发现列 数据类型 的错误 return(PriceUtil.JoinPriceString((string)o1, (string)o2)); #endif return(PriceUtil.Add((string)o1, (string)o2)); #if NO // 这一句更健壮一些 return(PriceUtil.JoinPriceString(Convert.ToString(o1), Convert.ToString(o2))); #endif } throw new Exception("无法支持的 " + datatype.ToString() + " 类型累加 o1 type=" + o1.GetType().ToString() + ", o2 type=" + o2.GetType().ToString()); }