Exemple #1
0
 public GncBook(GncSession session, string baseCurrency)
 {
     _session        = session;
     _guid           = null;
     _accountRoot    = null;
     _accounts       = new Dictionary <string, GncAccount>();
     _transactions   = new Dictionary <string, GncTransaction>();
     _splits         = new Dictionary <string, GncSplit>();
     _commodities    = new Dictionary <string, GncCommodity>();
     _baseCurrencyId = baseCurrency;
 }
Exemple #2
0
        public GncBook(GncSession session, XElement xml, string baseCurrency)
            : this(session, baseCurrency)
        {
            _guid = xml.ChkElement(GncName.Book("id")).Value;
            foreach (var cmdtyXml in xml.Elements(GncName.Gnc("commodity")))
            {
                GncCommodity cmdty = new GncCommodity(this, cmdtyXml);
                _commodities.Add(cmdty.Identifier, cmdty);
            }
            foreach (var acctXml in xml.Elements(GncName.Gnc("account")))
            {
                GncAccount acct = new GncAccount(this, acctXml);
                _accounts.Add(acct.Guid, acct);
            }
            foreach (var transXml in xml.Elements(GncName.Gnc("transaction")))
            {
                GncTransaction trans = new GncTransaction(this, transXml);
                _transactions.Add(trans.Guid, trans);
                foreach (var split in trans.EnumSplits())
                {
                    _splits.Add(split.Guid, split);
                }
            }

            // Price DB
            {
                var pel = xml.ChkElement(GncName.Gnc("pricedb"));
                foreach (var priceXml in pel.Elements("price"))
                {
                    var      cmdty     = new GncCommodity(null, priceXml.ChkElement(GncName.Price("commodity")));
                    var      ccy       = new GncCommodity(null, priceXml.ChkElement(GncName.Price("currency")));
                    DateTime timepoint = GncUtil.ParseGncDate(priceXml.ChkElement(GncName.Price("time")).ChkElement(GncName.Ts("date")).Value);
                    string   source    = priceXml.ChkElement(GncName.Price("source")).Value;
                    decimal  value;
                    try
                    {
                        value = priceXml.ChkElement(GncName.Price("value")).Value.ToGncDecimal();
                        if (value == 0)
                        {
                            continue; // GnuCash sometimes adds 0 for prices
                        }
                    }
                    catch (Exception e)
                    {
                        _session.Warn($"Could not read commodity price {cmdty.Identifier}/{ccy.Identifier}, on {timepoint.ToShortDateString()}, source {source}: {e.Message}");
                        continue;
                    }

                    if (!_commodities.ContainsKey(cmdty.Identifier))
                    {
                        _commodities.Add(cmdty.Identifier, new GncCommodity(this, identifier: cmdty.Identifier, name: cmdty.Identifier));
                    }

                    if (cmdty.Identifier == _baseCurrencyId)
                    {
                        _commodities[ccy.Identifier].ExRate[timepoint] = 1m / value;
                    }
                    else if (ccy.Identifier == _baseCurrencyId)
                    {
                        _commodities[cmdty.Identifier].ExRate[timepoint] = value;
                    }
                    else if (source != "user:xfer-dialog")
                    {
                        // Ignore and warn
                        //_session.Warn("Ignoring commodity price {0}/{1}, on {3}, source {4}, as it is not linked to the base currency ({2})".Fmt(cmdty.Identifier, ccy.Identifier, _baseCurrencyId, timepoint.ToShortDateString(), source));
                    }
                    // Otherwise just ignore completely
                }
                // Always add 1.0 to the base currency ExRate curve to make it more like the others
                _commodities[_baseCurrencyId].ExRate[new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc)] = 1;
            }

            rebuildCacheAccountChildren();
            rebuildCacheAccountSplits();
            rebuildCacheAccountAllBalances();
            verifyBalsnaps();
            verifyCurrencies();
        }