diff options
author | John Wiegley <johnw@newartisans.com> | 2006-03-24 01:41:22 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 02:41:31 -0400 |
commit | 44561c1c1d233d9432de319a71b44a3e05275d49 (patch) | |
tree | 3b9fa53fa4bd0e50e0890f6aafea69533e89832c /amount.cc | |
parent | 964e74e333cb20d3519be64f79e19224f2bcc84e (diff) | |
download | fork-ledger-44561c1c1d233d9432de319a71b44a3e05275d49.tar.gz fork-ledger-44561c1c1d233d9432de319a71b44a3e05275d49.tar.bz2 fork-ledger-44561c1c1d233d9432de319a71b44a3e05275d49.zip |
Further refinement of commodity lot information.
Diffstat (limited to 'amount.cc')
-rw-r--r-- | amount.cc | 78 |
1 files changed, 71 insertions, 7 deletions
@@ -1633,6 +1633,30 @@ amount_t commodity_base_t::value(const std::time_t moment) return price; } +bool annotated_commodity_t::operator==(const commodity_t& comm) const +{ + // If the base commodities don't match, the game's up. + if (base != comm.base) + return false; + + if (price && + (! comm.annotated || + price != static_cast<const annotated_commodity_t&>(comm).price)) + return false; + + if (date && + (! comm.annotated || + date != static_cast<const annotated_commodity_t&>(comm).date)) + return false; + + if (! tag.empty() && + (! comm.annotated || + tag != static_cast<const annotated_commodity_t&>(comm).tag)) + return false; + + return true; +} + void annotated_commodity_t::write_annotations(std::ostream& out, const amount_t& price, @@ -1751,15 +1775,55 @@ bool compare_amount_commodities::operator()(const amount_t * left, annotated_commodity_t& aleftcomm(static_cast<annotated_commodity_t&>(leftcomm)); annotated_commodity_t& arightcomm(static_cast<annotated_commodity_t&>(rightcomm)); - amount_t val = aleftcomm.price - arightcomm.price; - if (val) - return val < 0; + if (! aleftcomm.price && arightcomm.price) + return true; + if (aleftcomm.price && ! arightcomm.price) + return false; - int diff = aleftcomm.date - arightcomm.date; - if (diff) - return diff < 0; + if (aleftcomm.price && arightcomm.price) { + amount_t leftprice(aleftcomm.price); + leftprice.reduce(); + amount_t rightprice(arightcomm.price); + rightprice.reduce(); - return aleftcomm.tag < arightcomm.tag; + if (leftprice.commodity() == rightprice.commodity()) { + amount_t val = leftprice - rightprice; + if (val) + return val < 0; + } else { + // Since we have two different amounts, there's really no way + // to establish a true sorting order; we'll just do it based + // on the numerical values. + leftprice.clear_commodity(); + rightprice.clear_commodity(); + + amount_t val = leftprice - rightprice; + if (val) + return val < 0; + } + } + + if (! aleftcomm.date && arightcomm.date) + return true; + if (aleftcomm.date && ! arightcomm.date) + return false; + + if (aleftcomm.date && arightcomm.date) { + int diff = aleftcomm.date - arightcomm.date; + if (diff) + return diff < 0; + } + + if (aleftcomm.tag.empty() && ! arightcomm.tag.empty()) + return true; + if (! aleftcomm.tag.empty() && arightcomm.tag.empty()) + return false; + + if (! aleftcomm.tag.empty() && ! arightcomm.tag.empty()) + return aleftcomm.tag < arightcomm.tag; + + assert(0); + return true; } } |