Exemplo n.º 1
0
        // return:
        //      true    合法
        //      false   不合法。已经报错了
        bool VerifyData()
        {
            if (this._dom == null)
            {
                return(true);
            }

            XmlNodeList nodes = _dom.DocumentElement.SelectNodes("item");

            foreach (XmlElement item in nodes)
            {
                string strSeller = item.GetAttribute("seller");
                string strTable  = item.GetAttribute("table");

                // 验证数据是否合法
                try
                {
                    RateItem.ParseList(strTable);
                }
                catch (Exception ex)
                {
                    this.comboBox_seller.Text = strSeller;
                    MessageBox.Show(this, "渠道(书商) '" + strSeller + "' 的汇率表格式不合法: " + ex.Message);
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 private void button_OK_Click(object sender, EventArgs e)
 {
     // 验证数据是否合法
     try
     {
         RateItem.ParseList(this.RateList);
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message);
         return;
     }
     this.DialogResult = System.Windows.Forms.DialogResult.OK;
     this.Close();
 }
Exemplo n.º 4
0
        // CNY1.00=USD0.22222
        public static RateItem Parse(string strText)
        {
            if (strText.IndexOf('=') == -1)
            {
                throw new Exception("对照事项格式不正确,缺乏等号。'" + strText + "'");
            }
            List <string> parts     = StringUtil.ParseTwoPart(strText, "=");
            string        strSource = parts[0].Trim();
            string        strTarget = parts[1].Trim();

            RateItem result = new RateItem();

            result.Source = DoubleCurrencyItem.Parse(strSource);
            result.Target = DoubleCurrencyItem.Parse(strTarget);

            return(result);
        }
Exemplo n.º 5
0
        // 分号分隔。
        public static List <RateItem> ParseList(string strText)
        {
            List <RateItem> results = new List <RateItem>();

            string[] segments = strText.Split(new char[] { ';' });
            foreach (string segment in segments)
            {
                if (string.IsNullOrEmpty(segment))
                {
                    continue;
                }
                string strSegment = segment.Trim();
                if (string.IsNullOrEmpty(strSegment))
                {
                    continue;
                }
                results.Add(RateItem.Parse(strSegment));
            }

            return(results);
        }
Exemplo n.º 6
0
        // 将形如"-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));
        }