public GncCommodity(GncBook book, XElement xml) : this(book) { _space = xml.ChkElement(GncName.Cmdty("space")).Value; _name = xml.ChkElement(GncName.Cmdty("id")).Value; _identifier = MakeIdentifier(_space, _name); }
public static string MakeIdentifier(XElement xml) { if (xml == null) { return(null); } else { return(MakeIdentifier(xml.ChkElement(GncName.Cmdty("space")).Value, xml.ChkElement(GncName.Cmdty("id")).Value)); } }
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); } }
public GncAccount(GncBook book, XElement xml) : this(book) { _name = xml.ChkElement(GncName.Act("name")).Value; _guid = xml.ChkElement(GncName.Act("id")).Value; _parentGuid = xml.ValueOrDefault(GncName.Act("parent"), (string)null); _commodity = book.GetCommodity(GncCommodity.MakeIdentifier(xml.Element(GncName.Act("commodity")))); if (_commodity == null && xml.Element(GncName.Act("type"))?.Value == "ROOT") { _commodity = _book.BaseCurrency; } string scu = xml.ValueOrDefault(GncName.Act("commodity-scu"), "1"); _commodityDecimals = scu.Count(c => c == '0'); if (scu != "1" + new string('0', _commodityDecimals)) { throw new Exception("Could not parse commodity-scu: expected a power of 10."); } _commodityScu = int.Parse(scu); _description = xml.ValueOrDefault(GncName.Act("description"), (string)null); }
public void LoadFromFile(string file, string baseCurrency, string balsnapPrefix) { Clear(); _balsnapPrefix = balsnapPrefix; XDocument doc; _modifiedTimestamp = File.GetLastWriteTimeUtc(file); try { using (var gz = new GZipStream(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read), CompressionMode.Decompress)) using (var sr = new StreamReader(gz)) doc = XDocument.Load(sr); } catch { try { doc = XDocument.Load(file); } catch (Exception E) { throw new GncException("Cannot parse XML file: " + E.Message); } } if (doc.Root.Name != "gnc-v2") { throw new GncException("Cannot load file: root node name is not \"gnc-v2\""); } foreach (var el in doc.Root.Elements(GncName.Gnc("book"))) { GncBook book = new GncBook(this, el, baseCurrency); _books.Add(book.Guid, book); if (Book == null) { _book = book; } else { throw new GncException("Multiple books in the file; this is currently entirely untested."); } } }
public GncTransaction(GncBook book, XElement xml) : this(book) { _guid = xml.ChkElement(GncName.Trn("id")).Value; _datePosted = DateTimeOffset.Parse(xml.ChkElement(GncName.Trn("date-posted")).ChkElement(GncName.Ts("date")).Value).Date.AssumeUtc(); _dateEntered = DateTimeOffset.Parse(xml.ChkElement(GncName.Trn("date-entered")).ChkElement(GncName.Ts("date")).Value).UtcDateTime; _num = xml.ValueOrDefault(GncName.Trn("num"), ""); _description = xml.ChkElement(GncName.Trn("description")).Value; _commodity = _book.GetCommodity(GncCommodity.MakeIdentifier(xml.ChkElement(GncName.Trn("currency")))); foreach (var el in xml.ChkElement(GncName.Trn("splits")).Elements(GncName.Trn("split"))) { try { GncSplit split = new GncSplit(this, el); _splits.Add(split.Guid, split); } catch (Exception e) { throw new Exception($"Could not read split for transaction: {_datePosted}, descr {_description}, {e.Message} ({_guid})", e); } } }
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(); }