コード例 #1
0
ファイル: GncSplit.cs プロジェクト: rstarkov/GnuCashSharp
 public GncSplit(GncTransaction transaction)
 {
     _transaction = transaction;
     _guid        = null;
     _reconciled  = GncReconciled.UNKNOWN;
     _value       = 0;
     _quantity    = 0;
     _accountGuid = null;
     _memo        = null;
 }
コード例 #2
0
ファイル: GncSplit.cs プロジェクト: rstarkov/GnuCashSharp
 public GncSplit(GncTransaction transaction, XElement xml)
     : this(transaction)
 {
     _guid = xml.ChkElement(GncName.Split("id")).Value;
     //_reconciled = xml.ChkElement(GncName.Split("reconciled-state")).Value;
     _accountGuid = xml.ChkElement(GncName.Split("account")).Value;
     _memo        = xml.ValueOrDefault(GncName.Split("memo"), (string)null);
     try
     {
         _value    = xml.ChkElement(GncName.Split("value")).Value.ToGncDecimal();
         _quantity = xml.ChkElement(GncName.Split("quantity")).Value.ToGncDecimal();
     }
     catch (Exception e)
     {
         throw new Exception($"Could not read quantity for split {_guid}, account {_accountGuid}: {e.Message}", e);
     }
 }
コード例 #3
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();
        }