コード例 #1
0
ファイル: GncBook.cs プロジェクト: rstarkov/GnuCashSharp
 internal void rebuildCacheAccountChildren()
 {
     _cacheAccountChildren = new Dictionary <string, List <string> >();
     _accountRoot          = null;
     foreach (var acct in _accounts.Values)
     {
         if (acct.ParentGuid == null)
         {
             if (_accountRoot == null)
             {
                 _accountRoot = acct;
             }
             else
             {
                 throw new GncException("Multiple root accounts found.");
             }
         }
         else
         {
             if (!_cacheAccountChildren.ContainsKey(acct.ParentGuid))
             {
                 _cacheAccountChildren.Add(acct.ParentGuid, new List <string>());
             }
             _cacheAccountChildren[acct.ParentGuid].Add(acct.Guid);
         }
     }
     foreach (var list in _cacheAccountChildren.Values)
     {
         list.Sort((x, y) => _accounts[x].Name.CompareTo(_accounts[y].Name));
     }
 }
コード例 #2
0
ファイル: GncBook.cs プロジェクト: rstarkov/GnuCashSharp
 public IEnumerable <GncSplit> AccountEnumSplits(GncAccount acct)
 {
     if (!_cacheAccountSplits.ContainsKey(acct.Guid))
     {
         yield break;
     }
     foreach (var splitGuid in _cacheAccountSplits[acct.Guid])
     {
         yield return(_splits[splitGuid]);
     }
 }
コード例 #3
0
ファイル: GncBook.cs プロジェクト: rstarkov/GnuCashSharp
 public IEnumerable <GncAccount> AccountEnumChildren(GncAccount acct)
 {
     if (!_cacheAccountChildren.ContainsKey(acct.Guid))
     {
         yield break;
     }
     foreach (var childGuid in _cacheAccountChildren[acct.Guid])
     {
         yield return(_accounts[childGuid]);
     }
 }
コード例 #4
0
ファイル: GncBook.cs プロジェクト: rstarkov/GnuCashSharp
 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;
 }
コード例 #5
0
ファイル: GncBook.cs プロジェクト: rstarkov/GnuCashSharp
        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();
        }