penny-lib-0.2.0.0: Extensible double-entry accounting system - library

Safe HaskellSafe-Infered

Penny.Lincoln.PriceDb

Description

A database of price information. A PricePoint has a DateTime, a From commodity, a To commodity, and a QtyPerUnit. The PriceDb holds this information for several prices. You can query the database by supplying a from commodity, a to commodity, and a DateTime, and the database will give you the QtyPerUnit, if there is one.

Synopsis

Documentation

data PriceDb Source

The PriceDb holds information about prices. Create an empty one using emptyDb then fill it with values using foldl or similar.

emptyDb :: PriceDbSource

An empty PriceDb

addPrice :: PriceDb -> PricePoint -> PriceDbSource

Add a single price to the PriceDb.

getPrice :: PriceDb -> From -> To -> DateTime -> Exceptional PriceDbError CountPerUnitSource

Looks up values from the PriceDb. Throws Error if something fails.

First, tries to find the best possible From match. For example, if From is LUV:2001, first tries to see if there is a From match for LUV:2001. If there is not an exact match for LUV:2001 but there is a match for LUV, then LUV is used. If there is not a match for either LUV:2001 or for LUV, then FromNotFound is thrown.

The To commodity must match exactly. So, if the TO commodity is LUV:2001, only LUV:2001 will do. If the To commodity is not found, ToNotFound is thrown.

The DateTime is the time at which to find a price. If a price exists for that exact DateTime, that price is returned. If no price exists for that exact DateTime, but there is a price for an earlier DateTime, the latest possible price is returned. If there are no earlier prices, CpuNotFound is thrown.

There is no backtracking on earlier decisions. For example, if From is LUV:2001, and there is indeed at least one From price in the PriceDb and CpuNotFound occurs, getPrice does not check to see if the computation would have succeeded had it used LUV rather than LUV:2001.

data PriceDbError Source

Getting prices can fail; if it fails, an Error is returned.

convert :: PriceDb -> DateTime -> To -> Amount -> Exceptional PriceDbError AmountSource

Given an Amount and a Commodity to convert the amount to, converts the Amount to the given commodity. If the Amount given is already in the To commodity, simply returns what was passed in. Can fail and throw PriceDbError. Internally uses getPrice, so read its documentation for details on how price lookup works.