{-# LANGUAGE StandaloneDeriving, RecordWildCards, OverloadedStrings #-}
module Hledger.Data.Amount (
amount,
nullamt,
missingamt,
num,
usd,
eur,
gbp,
per,
hrs,
at,
(@@),
amountWithCommodity,
amountCost,
amountIsZero,
amountLooksZero,
divideAmount,
multiplyAmount,
divideAmountAndPrice,
multiplyAmountAndPrice,
amountTotalPriceToUnitPrice,
amountstyle,
styleAmount,
styleAmountExceptPrecision,
amountUnstyled,
showAmount,
cshowAmount,
showAmountWithZeroCommodity,
showAmountDebug,
showAmountWithoutPrice,
setAmountPrecision,
withPrecision,
setFullPrecision,
setAmountInternalPrecision,
withInternalPrecision,
setAmountDecimalPoint,
withDecimalPoint,
canonicaliseAmount,
nullmixedamt,
missingmixedamt,
mixed,
amounts,
filterMixedAmount,
filterMixedAmountByCommodity,
mapMixedAmount,
normaliseMixedAmountSquashPricesForDisplay,
normaliseMixedAmount,
unifyMixedAmount,
mixedAmountStripPrices,
mixedAmountCost,
divideMixedAmount,
multiplyMixedAmount,
divideMixedAmountAndPrice,
multiplyMixedAmountAndPrice,
averageMixedAmounts,
isNegativeAmount,
isNegativeMixedAmount,
mixedAmountIsZero,
mixedAmountLooksZero,
mixedAmountTotalPriceToUnitPrice,
styleMixedAmount,
mixedAmountUnstyled,
showMixedAmount,
showMixedAmountOneLine,
showMixedAmountDebug,
showMixedAmountWithoutPrice,
showMixedAmountOneLineWithoutPrice,
showMixedAmountElided,
showMixedAmountWithZeroCommodity,
showMixedAmountWithPrecision,
showMixed,
showMixedUnnormalised,
showMixedOneLine,
showMixedOneLineUnnormalised,
setMixedAmountPrecision,
canonicaliseMixedAmount,
ltraceamount,
tests_Amount
) where
import Control.Monad (foldM)
import Data.Char (isDigit)
import Data.Decimal (decimalPlaces, normalizeDecimal, roundTo)
import Data.Function (on)
import Data.List (genericSplitAt, groupBy, intercalate, mapAccumL,
partition, sortBy)
import qualified Data.Map as M
import Data.Map (findWithDefault)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Data.Word (Word8)
import Safe (lastDef, lastMay)
import Text.Printf (printf)
import Hledger.Data.Types
import Hledger.Data.Commodity
import Hledger.Utils
deriving instance Show MarketPrice
amountstyle :: AmountStyle
amountstyle = Side
-> Bool
-> AmountPrecision
-> Maybe Char
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
0) (Char -> Maybe Char
forall a. a -> Maybe a
Just Char
'.') Maybe DigitGroupStyle
forall a. Maybe a
Nothing
instance Num Amount where
abs :: Amount -> Amount
abs a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Amount
a{aquantity :: Quantity
aquantity=Quantity -> Quantity
forall a. Num a => a -> a
abs Quantity
q}
signum :: Amount -> Amount
signum a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Amount
a{aquantity :: Quantity
aquantity=Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q}
fromInteger :: Integer -> Amount
fromInteger Integer
i = Amount
nullamt{aquantity :: Quantity
aquantity=Integer -> Quantity
forall a. Num a => Integer -> a
fromInteger Integer
i}
negate :: Amount -> Amount
negate a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Amount
a{aquantity :: Quantity
aquantity= -Quantity
q}
+ :: Amount -> Amount -> Amount
(+) = (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
(+)
(-) = (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp (-)
* :: Amount -> Amount -> Amount
(*) = (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
(*)
amount, nullamt :: Amount
amount :: Amount
amount = Amount :: CommoditySymbol
-> Quantity -> Bool -> AmountStyle -> Maybe AmountPrice -> Amount
Amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"", aquantity :: Quantity
aquantity=Quantity
0, aprice :: Maybe AmountPrice
aprice=Maybe AmountPrice
forall a. Maybe a
Nothing, astyle :: AmountStyle
astyle=AmountStyle
amountstyle, aismultiplier :: Bool
aismultiplier=Bool
False}
nullamt :: Amount
nullamt = Amount
amount
missingamt :: Amount
missingamt :: Amount
missingamt = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"AUTO"}
num :: Quantity -> Amount
num Quantity
n = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"", aquantity :: Quantity
aquantity=Quantity
n}
hrs :: Quantity -> Amount
hrs Quantity
n = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"h", aquantity :: Quantity
aquantity=Quantity
n, astyle :: AmountStyle
astyle=AmountStyle
amountstyle{asprecision :: AmountPrecision
asprecision=Word8 -> AmountPrecision
Precision Word8
2, ascommodityside :: Side
ascommodityside=Side
R}}
usd :: Quantity -> Amount
usd Quantity
n = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"$", aquantity :: Quantity
aquantity=Word8 -> Quantity -> Quantity
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
2 Quantity
n, astyle :: AmountStyle
astyle=AmountStyle
amountstyle{asprecision :: AmountPrecision
asprecision=Word8 -> AmountPrecision
Precision Word8
2}}
eur :: Quantity -> Amount
eur Quantity
n = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"€", aquantity :: Quantity
aquantity=Word8 -> Quantity -> Quantity
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
2 Quantity
n, astyle :: AmountStyle
astyle=AmountStyle
amountstyle{asprecision :: AmountPrecision
asprecision=Word8 -> AmountPrecision
Precision Word8
2}}
gbp :: Quantity -> Amount
gbp Quantity
n = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"£", aquantity :: Quantity
aquantity=Word8 -> Quantity -> Quantity
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
2 Quantity
n, astyle :: AmountStyle
astyle=AmountStyle
amountstyle{asprecision :: AmountPrecision
asprecision=Word8 -> AmountPrecision
Precision Word8
2}}
per :: Quantity -> Amount
per Quantity
n = Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"%", aquantity :: Quantity
aquantity=Quantity
n, astyle :: AmountStyle
astyle=AmountStyle
amountstyle{asprecision :: AmountPrecision
asprecision=Word8 -> AmountPrecision
Precision Word8
1, ascommodityside :: Side
ascommodityside=Side
R, ascommodityspaced :: Bool
ascommodityspaced=Bool
True}}
Amount
amt at :: Amount -> Amount -> Amount
`at` Amount
priceamt = Amount
amt{aprice :: Maybe AmountPrice
aprice=AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
UnitPrice Amount
priceamt}
Amount
amt @@ :: Amount -> Amount -> Amount
@@ Amount
priceamt = Amount
amt{aprice :: Maybe AmountPrice
aprice=AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
TotalPrice Amount
priceamt}
similarAmountsOp :: (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp :: (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp Quantity -> Quantity -> Quantity
op Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
_, aquantity :: Amount -> Quantity
aquantity=Quantity
q1, astyle :: Amount -> AmountStyle
astyle=AmountStyle{asprecision :: AmountStyle -> AmountPrecision
asprecision=AmountPrecision
p1}}
Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
c2, aquantity :: Amount -> Quantity
aquantity=Quantity
q2, astyle :: Amount -> AmountStyle
astyle=s2 :: AmountStyle
s2@AmountStyle{asprecision :: AmountStyle -> AmountPrecision
asprecision=AmountPrecision
p2}} =
Amount
amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
c2, aquantity :: Quantity
aquantity=Quantity
q1 Quantity -> Quantity -> Quantity
`op` Quantity
q2, astyle :: AmountStyle
astyle=AmountStyle
s2{asprecision :: AmountPrecision
asprecision=AmountPrecision -> AmountPrecision -> AmountPrecision
forall a. Ord a => a -> a -> a
max AmountPrecision
p1 AmountPrecision
p2}}
amountWithCommodity :: CommoditySymbol -> Amount -> Amount
amountWithCommodity :: CommoditySymbol -> Amount -> Amount
amountWithCommodity CommoditySymbol
c Amount
a = Amount
a{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
c, aprice :: Maybe AmountPrice
aprice=Maybe AmountPrice
forall a. Maybe a
Nothing}
amountCost :: Amount -> Amount
amountCost :: Amount -> Amount
amountCost a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q, aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
mp} =
case Maybe AmountPrice
mp of
Maybe AmountPrice
Nothing -> Amount
a
Just (UnitPrice p :: Amount
p@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
pq}) -> Amount
p{aquantity :: Quantity
aquantity=Quantity
pq Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
* Quantity
q}
Just (TotalPrice p :: Amount
p@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
pq}) -> Amount
p{aquantity :: Quantity
aquantity=Quantity
pq Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
* Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q}
amountTotalPriceToUnitPrice :: Amount -> Amount
amountTotalPriceToUnitPrice :: Amount -> Amount
amountTotalPriceToUnitPrice
a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q, aprice :: Amount -> Maybe AmountPrice
aprice=Just (TotalPrice pa :: Amount
pa@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
pq, astyle :: Amount -> AmountStyle
astyle=AmountStyle
ps})}
= Amount
a{aprice :: Maybe AmountPrice
aprice = AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
UnitPrice Amount
pa{aquantity :: Quantity
aquantity=Quantity -> Quantity
forall a. Num a => a -> a
abs (Quantity
pqQuantity -> Quantity -> Quantity
forall a. Fractional a => a -> a -> a
/Quantity
q), astyle :: AmountStyle
astyle=AmountStyle
ps{asprecision :: AmountPrecision
asprecision=AmountPrecision
pp}}}
where
pp :: AmountPrecision
pp = case AmountStyle -> AmountPrecision
asprecision AmountStyle
ps of
AmountPrecision
NaturalPrecision -> AmountPrecision
NaturalPrecision
Precision Word8
p -> Word8 -> AmountPrecision
Precision (Word8 -> AmountPrecision) -> Word8 -> AmountPrecision
forall a b. (a -> b) -> a -> b
$ if Word8
p Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
forall a. Bounded a => a
maxBound then Word8
forall a. Bounded a => a
maxBound else Word8
p Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
1
amountTotalPriceToUnitPrice Amount
a = Amount
a
divideAmount :: Quantity -> Amount -> Amount
divideAmount :: Quantity -> Amount -> Amount
divideAmount Quantity
n a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Amount
a{aquantity :: Quantity
aquantity=Quantity
qQuantity -> Quantity -> Quantity
forall a. Fractional a => a -> a -> a
/Quantity
n}
multiplyAmount :: Quantity -> Amount -> Amount
multiplyAmount :: Quantity -> Amount -> Amount
multiplyAmount Quantity
n a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Amount
a{aquantity :: Quantity
aquantity=Quantity
qQuantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
*Quantity
n}
divideAmountAndPrice :: Quantity -> Amount -> Amount
divideAmountAndPrice :: Quantity -> Amount -> Amount
divideAmountAndPrice Quantity
n a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q,aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
p} = Amount
a{aquantity :: Quantity
aquantity=Quantity
qQuantity -> Quantity -> Quantity
forall a. Fractional a => a -> a -> a
/Quantity
n, aprice :: Maybe AmountPrice
aprice=AmountPrice -> AmountPrice
f (AmountPrice -> AmountPrice)
-> Maybe AmountPrice -> Maybe AmountPrice
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe AmountPrice
p}
where
f :: AmountPrice -> AmountPrice
f (TotalPrice Amount
a) = Amount -> AmountPrice
TotalPrice (Amount -> AmountPrice) -> Amount -> AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> Amount
forall a. Num a => a -> a
abs (Amount -> Amount) -> Amount -> Amount
forall a b. (a -> b) -> a -> b
$ Quantity
n Quantity -> Amount -> Amount
`divideAmount` Amount
a
f AmountPrice
p = AmountPrice
p
multiplyAmountAndPrice :: Quantity -> Amount -> Amount
multiplyAmountAndPrice :: Quantity -> Amount -> Amount
multiplyAmountAndPrice Quantity
n a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q,aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
p} = Amount
a{aquantity :: Quantity
aquantity=Quantity
qQuantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
*Quantity
n, aprice :: Maybe AmountPrice
aprice=AmountPrice -> AmountPrice
f (AmountPrice -> AmountPrice)
-> Maybe AmountPrice -> Maybe AmountPrice
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe AmountPrice
p}
where
f :: AmountPrice -> AmountPrice
f (TotalPrice Amount
a) = Amount -> AmountPrice
TotalPrice (Amount -> AmountPrice) -> Amount -> AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> Amount
forall a. Num a => a -> a
abs (Amount -> Amount) -> Amount -> Amount
forall a b. (a -> b) -> a -> b
$ Quantity
n Quantity -> Amount -> Amount
`multiplyAmount` Amount
a
f AmountPrice
p = AmountPrice
p
isNegativeAmount :: Amount -> Bool
isNegativeAmount :: Amount -> Bool
isNegativeAmount Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0
amountRoundedQuantity :: Amount -> Quantity
amountRoundedQuantity :: Amount -> Quantity
amountRoundedQuantity Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q, astyle :: Amount -> AmountStyle
astyle=AmountStyle{asprecision :: AmountStyle -> AmountPrecision
asprecision=AmountPrecision
p}} = case AmountPrecision
p of
AmountPrecision
NaturalPrecision -> Quantity
q
Precision Word8
p' -> Word8 -> Quantity -> Quantity
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
p' Quantity
q
amountLooksZero :: Amount -> Bool
amountLooksZero :: Amount -> Bool
amountLooksZero = (Quantity
0Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
==) (Quantity -> Bool) -> (Amount -> Quantity) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Quantity
amountRoundedQuantity
amountIsZero :: Amount -> Bool
amountIsZero :: Amount -> Bool
amountIsZero Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
== Quantity
0
withPrecision :: Amount -> AmountPrecision -> Amount
withPrecision :: Amount -> AmountPrecision -> Amount
withPrecision = (AmountPrecision -> Amount -> Amount)
-> Amount -> AmountPrecision -> Amount
forall a b c. (a -> b -> c) -> b -> a -> c
flip AmountPrecision -> Amount -> Amount
setAmountPrecision
setAmountPrecision :: AmountPrecision -> Amount -> Amount
setAmountPrecision :: AmountPrecision -> Amount -> Amount
setAmountPrecision AmountPrecision
p a :: Amount
a@Amount{astyle :: Amount -> AmountStyle
astyle=AmountStyle
s} = Amount
a{astyle :: AmountStyle
astyle=AmountStyle
s{asprecision :: AmountPrecision
asprecision=AmountPrecision
p}}
setFullPrecision :: Amount -> Amount
setFullPrecision :: Amount -> Amount
setFullPrecision Amount
a = AmountPrecision -> Amount -> Amount
setAmountPrecision AmountPrecision
p Amount
a
where
p :: AmountPrecision
p = AmountPrecision -> AmountPrecision -> AmountPrecision
forall a. Ord a => a -> a -> a
max AmountPrecision
displayprecision AmountPrecision
naturalprecision
displayprecision :: AmountPrecision
displayprecision = AmountStyle -> AmountPrecision
asprecision (AmountStyle -> AmountPrecision) -> AmountStyle -> AmountPrecision
forall a b. (a -> b) -> a -> b
$ Amount -> AmountStyle
astyle Amount
a
naturalprecision :: AmountPrecision
naturalprecision = Word8 -> AmountPrecision
Precision (Word8 -> AmountPrecision)
-> (Quantity -> Word8) -> Quantity -> AmountPrecision
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Word8
forall i. DecimalRaw i -> Word8
decimalPlaces (Quantity -> Word8) -> (Quantity -> Quantity) -> Quantity -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Quantity
forall i. Integral i => DecimalRaw i -> DecimalRaw i
normalizeDecimal (Quantity -> AmountPrecision) -> Quantity -> AmountPrecision
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity
aquantity Amount
a
setAmountInternalPrecision :: Word8 -> Amount -> Amount
setAmountInternalPrecision :: Word8 -> Amount -> Amount
setAmountInternalPrecision Word8
p a :: Amount
a@Amount{ aquantity :: Amount -> Quantity
aquantity=Quantity
q, astyle :: Amount -> AmountStyle
astyle=AmountStyle
s } = Amount
a{
astyle :: AmountStyle
astyle=AmountStyle
s{asprecision :: AmountPrecision
asprecision=Word8 -> AmountPrecision
Precision Word8
p}
,aquantity :: Quantity
aquantity=Word8 -> Quantity -> Quantity
forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
p Quantity
q
}
withInternalPrecision :: Amount -> Word8 -> Amount
withInternalPrecision :: Amount -> Word8 -> Amount
withInternalPrecision = (Word8 -> Amount -> Amount) -> Amount -> Word8 -> Amount
forall a b c. (a -> b -> c) -> b -> a -> c
flip Word8 -> Amount -> Amount
setAmountInternalPrecision
setAmountDecimalPoint :: Maybe Char -> Amount -> Amount
setAmountDecimalPoint :: Maybe Char -> Amount -> Amount
setAmountDecimalPoint Maybe Char
mc a :: Amount
a@Amount{ astyle :: Amount -> AmountStyle
astyle=AmountStyle
s } = Amount
a{ astyle :: AmountStyle
astyle=AmountStyle
s{asdecimalpoint :: Maybe Char
asdecimalpoint=Maybe Char
mc} }
withDecimalPoint :: Amount -> Maybe Char -> Amount
withDecimalPoint :: Amount -> Maybe Char -> Amount
withDecimalPoint = (Maybe Char -> Amount -> Amount) -> Amount -> Maybe Char -> Amount
forall a b c. (a -> b -> c) -> b -> a -> c
flip Maybe Char -> Amount -> Amount
setAmountDecimalPoint
showAmountPrice :: Maybe AmountPrice -> String
showAmountPrice :: Maybe AmountPrice -> String
showAmountPrice Maybe AmountPrice
Nothing = String
""
showAmountPrice (Just (UnitPrice Amount
pa)) = String
" @ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Amount -> String
showAmount Amount
pa
showAmountPrice (Just (TotalPrice Amount
pa)) = String
" @@ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Amount -> String
showAmount Amount
pa
showAmountPriceDebug :: Maybe AmountPrice -> String
showAmountPriceDebug :: Maybe AmountPrice -> String
showAmountPriceDebug Maybe AmountPrice
Nothing = String
""
showAmountPriceDebug (Just (UnitPrice Amount
pa)) = String
" @ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Amount -> String
showAmountDebug Amount
pa
showAmountPriceDebug (Just (TotalPrice Amount
pa)) = String
" @@ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Amount -> String
showAmountDebug Amount
pa
styleAmount :: M.Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmount Map CommoditySymbol AmountStyle
styles Amount
a =
case CommoditySymbol
-> Map CommoditySymbol AmountStyle -> Maybe AmountStyle
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Amount -> CommoditySymbol
acommodity Amount
a) Map CommoditySymbol AmountStyle
styles of
Just AmountStyle
s -> Amount
a{astyle :: AmountStyle
astyle=AmountStyle
s}
Maybe AmountStyle
Nothing -> Amount
a
styleAmountExceptPrecision :: M.Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmountExceptPrecision :: Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map CommoditySymbol AmountStyle
styles a :: Amount
a@Amount{astyle :: Amount -> AmountStyle
astyle=AmountStyle{asprecision :: AmountStyle -> AmountPrecision
asprecision=AmountPrecision
origp}} =
case CommoditySymbol
-> Map CommoditySymbol AmountStyle -> Maybe AmountStyle
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Amount -> CommoditySymbol
acommodity Amount
a) Map CommoditySymbol AmountStyle
styles of
Just AmountStyle
s -> Amount
a{astyle :: AmountStyle
astyle=AmountStyle
s{asprecision :: AmountPrecision
asprecision=AmountPrecision
origp}}
Maybe AmountStyle
Nothing -> Amount
a
amountUnstyled :: Amount -> Amount
amountUnstyled :: Amount -> Amount
amountUnstyled Amount
a = Amount
a{astyle :: AmountStyle
astyle=AmountStyle
amountstyle}
showAmount :: Amount -> String
showAmount :: Amount -> String
showAmount = Bool -> Amount -> String
showAmountHelper Bool
False
cshowAmount :: Amount -> String
cshowAmount :: Amount -> String
cshowAmount Amount
a = (if Amount -> Bool
isNegativeAmount Amount
a then ColorIntensity -> Color -> ShowS
color ColorIntensity
Dull Color
Red else ShowS
forall a. a -> a
id) ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
Bool -> Amount -> String
showAmountHelper Bool
False Amount
a
showAmountWithoutPrice :: Amount -> String
showAmountWithoutPrice :: Amount -> String
showAmountWithoutPrice Amount
a = Amount -> String
showAmount Amount
a{aprice :: Maybe AmountPrice
aprice=Maybe AmountPrice
forall a. Maybe a
Nothing}
showAmountWithPrecision :: AmountPrecision -> Amount -> String
showAmountWithPrecision :: AmountPrecision -> Amount -> String
showAmountWithPrecision AmountPrecision
p = Amount -> String
showAmount (Amount -> String) -> (Amount -> Amount) -> Amount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountPrecision -> Amount -> Amount
setAmountPrecision AmountPrecision
p
showAmountHelper :: Bool -> Amount -> String
showAmountHelper :: Bool -> Amount -> String
showAmountHelper Bool
_ Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
"AUTO"} = String
""
showAmountHelper Bool
showzerocommodity a :: Amount
a@Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
c, aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
mp, astyle :: Amount -> AmountStyle
astyle=AmountStyle{Bool
Maybe Char
Maybe DigitGroupStyle
AmountPrecision
Side
asdigitgroups :: AmountStyle -> Maybe DigitGroupStyle
asdigitgroups :: Maybe DigitGroupStyle
asdecimalpoint :: Maybe Char
asprecision :: AmountPrecision
ascommodityspaced :: Bool
ascommodityside :: Side
asdecimalpoint :: AmountStyle -> Maybe Char
ascommodityspaced :: AmountStyle -> Bool
ascommodityside :: AmountStyle -> Side
asprecision :: AmountStyle -> AmountPrecision
..}} =
case Side
ascommodityside of
Side
L -> String -> String -> String -> String -> ShowS
forall r. PrintfType r => String -> r
printf String
"%s%s%s%s" (CommoditySymbol -> String
T.unpack CommoditySymbol
c') String
space String
quantity' String
price
Side
R -> String -> String -> String -> String -> ShowS
forall r. PrintfType r => String -> r
printf String
"%s%s%s%s" String
quantity' String
space (CommoditySymbol -> String
T.unpack CommoditySymbol
c') String
price
where
quantity :: String
quantity = Amount -> String
showamountquantity Amount
a
(String
quantity',CommoditySymbol
c') | Amount -> Bool
amountLooksZero Amount
a Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
showzerocommodity = (String
"0",CommoditySymbol
"")
| Bool
otherwise = (String
quantity, CommoditySymbol -> CommoditySymbol
quoteCommoditySymbolIfNeeded CommoditySymbol
c)
space :: String
space = if Bool -> Bool
not (CommoditySymbol -> Bool
T.null CommoditySymbol
c') Bool -> Bool -> Bool
&& Bool
ascommodityspaced then String
" " else String
"" :: String
price :: String
price = Maybe AmountPrice -> String
showAmountPrice Maybe AmountPrice
mp
showAmountWithZeroCommodity :: Amount -> String
showAmountWithZeroCommodity :: Amount -> String
showAmountWithZeroCommodity = Bool -> Amount -> String
showAmountHelper Bool
True
showAmountDebug :: Amount -> String
showAmountDebug :: Amount -> String
showAmountDebug Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
"AUTO"} = String
"(missing)"
showAmountDebug Amount{Bool
Maybe AmountPrice
Quantity
CommoditySymbol
AmountStyle
aprice :: Maybe AmountPrice
astyle :: AmountStyle
aismultiplier :: Bool
aquantity :: Quantity
acommodity :: CommoditySymbol
aismultiplier :: Amount -> Bool
astyle :: Amount -> AmountStyle
aprice :: Amount -> Maybe AmountPrice
acommodity :: Amount -> CommoditySymbol
aquantity :: Amount -> Quantity
..} = String -> String -> String -> String -> ShowS
forall r. PrintfType r => String -> r
printf String
"Amount {acommodity=%s, aquantity=%s, aprice=%s, astyle=%s}" (CommoditySymbol -> String
forall a. Show a => a -> String
show CommoditySymbol
acommodity) (Quantity -> String
forall a. Show a => a -> String
show Quantity
aquantity) (Maybe AmountPrice -> String
showAmountPriceDebug Maybe AmountPrice
aprice) (AmountStyle -> String
forall a. Show a => a -> String
show AmountStyle
astyle)
showamountquantity :: Amount -> String
showamountquantity :: Amount -> String
showamountquantity amt :: Amount
amt@Amount{astyle :: Amount -> AmountStyle
astyle=AmountStyle{asdecimalpoint :: AmountStyle -> Maybe Char
asdecimalpoint=Maybe Char
mdec, asdigitgroups :: AmountStyle -> Maybe DigitGroupStyle
asdigitgroups=Maybe DigitGroupStyle
mgrps}} =
Char -> Maybe DigitGroupStyle -> ShowS
punctuatenumber (Char -> Maybe Char -> Char
forall a. a -> Maybe a -> a
fromMaybe Char
'.' Maybe Char
mdec) Maybe DigitGroupStyle
mgrps ShowS -> (Quantity -> String) -> Quantity -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> String
forall a. Show a => a -> String
show (Quantity -> String) -> Quantity -> String
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity
amountRoundedQuantity Amount
amt
punctuatenumber :: Char -> Maybe DigitGroupStyle -> String -> String
punctuatenumber :: Char -> Maybe DigitGroupStyle -> ShowS
punctuatenumber Char
dec Maybe DigitGroupStyle
mgrps String
s = String
sign String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. [a] -> [a]
reverse (Maybe DigitGroupStyle -> ShowS
applyDigitGroupStyle Maybe DigitGroupStyle
mgrps (ShowS
forall a. [a] -> [a]
reverse String
int)) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
frac''
where
(String
sign,String
num) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Char -> Bool
isDigit String
s
(String
int,String
frac) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'.') String
num
frac' :: String
frac' = (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'.') String
frac
frac'' :: String
frac'' | String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
frac' = String
""
| Bool
otherwise = Char
decChar -> ShowS
forall a. a -> [a] -> [a]
:String
frac'
applyDigitGroupStyle :: Maybe DigitGroupStyle -> String -> String
applyDigitGroupStyle :: Maybe DigitGroupStyle -> ShowS
applyDigitGroupStyle Maybe DigitGroupStyle
Nothing String
s = String
s
applyDigitGroupStyle (Just (DigitGroups Char
c [Word8]
gs)) String
s = [Word8] -> ShowS
forall a. Integral a => [a] -> ShowS
addseps ([Word8] -> [Word8]
forall a. [a] -> [a]
repeatLast [Word8]
gs) String
s
where
addseps :: [a] -> ShowS
addseps [] String
s = String
s
addseps (a
g:[a]
gs) String
s
| Int -> Integer
forall a. Integral a => a -> Integer
toInteger (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= a -> Integer
forall a. Integral a => a -> Integer
toInteger a
g = String
s
| Bool
otherwise = let (String
part,String
rest) = a -> String -> (String, String)
forall i a. Integral i => i -> [a] -> ([a], [a])
genericSplitAt a
g String
s
in String
part String -> ShowS
forall a. [a] -> [a] -> [a]
++ Char
c Char -> ShowS
forall a. a -> [a] -> [a]
: [a] -> ShowS
addseps [a]
gs String
rest
repeatLast :: [a] -> [a]
repeatLast [] = []
repeatLast [a]
gs = [a] -> [a]
forall a. [a] -> [a]
init [a]
gs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a -> [a]
forall a. a -> [a]
repeat ([a] -> a
forall a. [a] -> a
last [a]
gs)
canonicaliseAmount :: M.Map CommoditySymbol AmountStyle -> Amount -> Amount
canonicaliseAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount
canonicaliseAmount Map CommoditySymbol AmountStyle
styles a :: Amount
a@Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
c, astyle :: Amount -> AmountStyle
astyle=AmountStyle
s} = Amount
a{astyle :: AmountStyle
astyle=AmountStyle
s'}
where
s' :: AmountStyle
s' = AmountStyle
-> CommoditySymbol
-> Map CommoditySymbol AmountStyle
-> AmountStyle
forall k a. Ord k => a -> k -> Map k a -> a
findWithDefault AmountStyle
s CommoditySymbol
c Map CommoditySymbol AmountStyle
styles
instance Num MixedAmount where
fromInteger :: Integer -> MixedAmount
fromInteger Integer
i = [Amount] -> MixedAmount
Mixed [Integer -> Amount
forall a. Num a => Integer -> a
fromInteger Integer
i]
negate :: MixedAmount -> MixedAmount
negate (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> Amount
forall a. Num a => a -> a
negate [Amount]
as
+ :: MixedAmount -> MixedAmount -> MixedAmount
(+) (Mixed [Amount]
as) (Mixed [Amount]
bs) = MixedAmount -> MixedAmount
normaliseMixedAmount (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ [Amount]
as [Amount] -> [Amount] -> [Amount]
forall a. [a] -> [a] -> [a]
++ [Amount]
bs
* :: MixedAmount -> MixedAmount -> MixedAmount
(*) = String -> MixedAmount -> MixedAmount -> MixedAmount
forall a. String -> a
error' String
"error, mixed amounts do not support multiplication"
abs :: MixedAmount -> MixedAmount
abs = String -> MixedAmount -> MixedAmount
forall a. String -> a
error' String
"error, mixed amounts do not support abs"
signum :: MixedAmount -> MixedAmount
signum = String -> MixedAmount -> MixedAmount
forall a. String -> a
error' String
"error, mixed amounts do not support signum"
nullmixedamt :: MixedAmount
nullmixedamt :: MixedAmount
nullmixedamt = [Amount] -> MixedAmount
Mixed []
missingmixedamt :: MixedAmount
missingmixedamt :: MixedAmount
missingmixedamt = [Amount] -> MixedAmount
Mixed [Amount
missingamt]
mixed :: [Amount] -> MixedAmount
mixed :: [Amount] -> MixedAmount
mixed = MixedAmount -> MixedAmount
normaliseMixedAmount (MixedAmount -> MixedAmount)
-> ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Amount] -> MixedAmount
Mixed
normaliseMixedAmount :: MixedAmount -> MixedAmount
normaliseMixedAmount :: MixedAmount -> MixedAmount
normaliseMixedAmount = Bool -> MixedAmount -> MixedAmount
normaliseHelper Bool
False
normaliseHelper :: Bool -> MixedAmount -> MixedAmount
normaliseHelper :: Bool -> MixedAmount -> MixedAmount
normaliseHelper Bool
squashprices (Mixed [Amount]
as)
| Amount
missingamt Amount -> [Amount] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Amount]
as = MixedAmount
missingmixedamt
| [Amount] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Amount]
nonzeros = [Amount] -> MixedAmount
Mixed [Amount
newzero]
| Bool
otherwise = [Amount] -> MixedAmount
Mixed [Amount]
nonzeros
where
newzero :: Amount
newzero = Amount -> [Amount] -> Amount
forall a. a -> [a] -> a
lastDef Amount
nullamt ([Amount] -> Amount) -> [Amount] -> Amount
forall a b. (a -> b) -> a -> b
$ (Amount -> Bool) -> [Amount] -> [Amount]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Amount -> Bool) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommoditySymbol -> Bool
T.null (CommoditySymbol -> Bool)
-> (Amount -> CommoditySymbol) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> CommoditySymbol
acommodity) [Amount]
zeros
([Amount]
zeros, [Amount]
nonzeros) = (Amount -> Bool) -> [Amount] -> ([Amount], [Amount])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Amount -> Bool
amountIsZero ([Amount] -> ([Amount], [Amount]))
-> [Amount] -> ([Amount], [Amount])
forall a b. (a -> b) -> a -> b
$
([Amount] -> Amount) -> [[Amount]] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map [Amount] -> Amount
sumSimilarAmountsUsingFirstPrice ([[Amount]] -> [Amount]) -> [[Amount]] -> [Amount]
forall a b. (a -> b) -> a -> b
$
(Amount -> Amount -> Bool) -> [Amount] -> [[Amount]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy Amount -> Amount -> Bool
groupfn ([Amount] -> [[Amount]]) -> [Amount] -> [[Amount]]
forall a b. (a -> b) -> a -> b
$
(Amount -> Amount -> Ordering) -> [Amount] -> [Amount]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Amount -> Amount -> Ordering
sortfn
[Amount]
as
sortfn :: Amount -> Amount -> Ordering
sortfn | Bool
squashprices = CommoditySymbol -> CommoditySymbol -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (CommoditySymbol -> CommoditySymbol -> Ordering)
-> (Amount -> CommoditySymbol) -> Amount -> Amount -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Amount -> CommoditySymbol
acommodity
| Bool
otherwise = (CommoditySymbol, Maybe AmountPrice)
-> (CommoditySymbol, Maybe AmountPrice) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((CommoditySymbol, Maybe AmountPrice)
-> (CommoditySymbol, Maybe AmountPrice) -> Ordering)
-> (Amount -> (CommoditySymbol, Maybe AmountPrice))
-> Amount
-> Amount
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` \Amount
a -> (Amount -> CommoditySymbol
acommodity Amount
a, Amount -> Maybe AmountPrice
aprice Amount
a)
groupfn :: Amount -> Amount -> Bool
groupfn | Bool
squashprices = CommoditySymbol -> CommoditySymbol -> Bool
forall a. Eq a => a -> a -> Bool
(==) (CommoditySymbol -> CommoditySymbol -> Bool)
-> (Amount -> CommoditySymbol) -> Amount -> Amount -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Amount -> CommoditySymbol
acommodity
| Bool
otherwise = \Amount
a1 Amount
a2 -> Amount -> CommoditySymbol
acommodity Amount
a1 CommoditySymbol -> CommoditySymbol -> Bool
forall a. Eq a => a -> a -> Bool
== Amount -> CommoditySymbol
acommodity Amount
a2 Bool -> Bool -> Bool
&& Amount -> Amount -> Bool
combinableprices Amount
a1 Amount
a2
combinableprices :: Amount -> Amount -> Bool
combinableprices Amount{aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
Nothing} Amount{aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
Nothing} = Bool
True
combinableprices Amount{aprice :: Amount -> Maybe AmountPrice
aprice=Just (UnitPrice Amount
p1)} Amount{aprice :: Amount -> Maybe AmountPrice
aprice=Just (UnitPrice Amount
p2)} = Amount
p1 Amount -> Amount -> Bool
forall a. Eq a => a -> a -> Bool
== Amount
p2
combinableprices Amount
_ Amount
_ = Bool
False
normaliseMixedAmountSquashPricesForDisplay :: MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay :: MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay = Bool -> MixedAmount -> MixedAmount
normaliseHelper Bool
True
unifyMixedAmount :: MixedAmount -> Maybe Amount
unifyMixedAmount :: MixedAmount -> Maybe Amount
unifyMixedAmount = (Amount -> Amount -> Maybe Amount)
-> Amount -> [Amount] -> Maybe Amount
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Amount -> Amount -> Maybe Amount
combine Amount
0 ([Amount] -> Maybe Amount)
-> (MixedAmount -> [Amount]) -> MixedAmount -> Maybe Amount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts
where
combine :: Amount -> Amount -> Maybe Amount
combine Amount
amount Amount
result
| Amount -> Bool
amountIsZero Amount
amount = Amount -> Maybe Amount
forall a. a -> Maybe a
Just Amount
result
| Amount -> Bool
amountIsZero Amount
result = Amount -> Maybe Amount
forall a. a -> Maybe a
Just Amount
amount
| Amount -> CommoditySymbol
acommodity Amount
amount CommoditySymbol -> CommoditySymbol -> Bool
forall a. Eq a => a -> a -> Bool
== Amount -> CommoditySymbol
acommodity Amount
result = Amount -> Maybe Amount
forall a. a -> Maybe a
Just (Amount -> Maybe Amount) -> Amount -> Maybe Amount
forall a b. (a -> b) -> a -> b
$ Amount
amount Amount -> Amount -> Amount
forall a. Num a => a -> a -> a
+ Amount
result
| Bool
otherwise = Maybe Amount
forall a. Maybe a
Nothing
sumSimilarAmountsUsingFirstPrice :: [Amount] -> Amount
sumSimilarAmountsUsingFirstPrice :: [Amount] -> Amount
sumSimilarAmountsUsingFirstPrice [] = Amount
nullamt
sumSimilarAmountsUsingFirstPrice [Amount]
as = ([Amount] -> Amount
forall a. Num a => [a] -> a
sumStrict [Amount]
as){aprice :: Maybe AmountPrice
aprice=Amount -> Maybe AmountPrice
aprice (Amount -> Maybe AmountPrice) -> Amount -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ [Amount] -> Amount
forall a. [a] -> a
head [Amount]
as}
amounts :: MixedAmount -> [Amount]
amounts :: MixedAmount -> [Amount]
amounts (Mixed [Amount]
as) = [Amount]
as
filterMixedAmount :: (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount :: (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount Amount -> Bool
p (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Bool) -> [Amount] -> [Amount]
forall a. (a -> Bool) -> [a] -> [a]
filter Amount -> Bool
p [Amount]
as
filterMixedAmountByCommodity :: CommoditySymbol -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity :: CommoditySymbol -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity CommoditySymbol
c (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed [Amount]
as'
where
as' :: [Amount]
as' = case (Amount -> Bool) -> [Amount] -> [Amount]
forall a. (a -> Bool) -> [a] -> [a]
filter ((CommoditySymbol -> CommoditySymbol -> Bool
forall a. Eq a => a -> a -> Bool
==CommoditySymbol
c) (CommoditySymbol -> Bool)
-> (Amount -> CommoditySymbol) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> CommoditySymbol
acommodity) [Amount]
as of
[] -> [Amount
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
c}]
[Amount]
as'' -> [[Amount] -> Amount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Amount]
as'']
mapMixedAmount :: (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount :: (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount Amount -> Amount
f (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> Amount
f [Amount]
as
mixedAmountCost :: MixedAmount -> MixedAmount
mixedAmountCost :: MixedAmount -> MixedAmount
mixedAmountCost (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> Amount
amountCost [Amount]
as
divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmount Quantity
n = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount (Quantity -> Amount -> Amount
divideAmount Quantity
n)
multiplyMixedAmount :: Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmount :: Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmount Quantity
n = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount (Quantity -> Amount -> Amount
multiplyAmount Quantity
n)
divideMixedAmountAndPrice :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmountAndPrice :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmountAndPrice Quantity
n = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount (Quantity -> Amount -> Amount
divideAmountAndPrice Quantity
n)
multiplyMixedAmountAndPrice :: Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmountAndPrice :: Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmountAndPrice Quantity
n = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount (Quantity -> Amount -> Amount
multiplyAmountAndPrice Quantity
n)
averageMixedAmounts :: [MixedAmount] -> MixedAmount
averageMixedAmounts :: [MixedAmount] -> MixedAmount
averageMixedAmounts [] = MixedAmount
0
averageMixedAmounts [MixedAmount]
as = Int -> Quantity
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([MixedAmount] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [MixedAmount]
as) Quantity -> MixedAmount -> MixedAmount
`divideMixedAmount` [MixedAmount] -> MixedAmount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [MixedAmount]
as
isNegativeMixedAmount :: MixedAmount -> Maybe Bool
isNegativeMixedAmount :: MixedAmount -> Maybe Bool
isNegativeMixedAmount MixedAmount
m =
case MixedAmount -> [Amount]
amounts (MixedAmount -> [Amount]) -> MixedAmount -> [Amount]
forall a b. (a -> b) -> a -> b
$ MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay MixedAmount
m of
[] -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
[Amount
a] -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just (Bool -> Maybe Bool) -> Bool -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ Amount -> Bool
isNegativeAmount Amount
a
[Amount]
as | (Amount -> Bool) -> [Amount] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Amount -> Bool
isNegativeAmount [Amount]
as -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
[Amount]
as | Bool -> Bool
not ((Amount -> Bool) -> [Amount] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Amount -> Bool
isNegativeAmount [Amount]
as) -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
[Amount]
_ -> Maybe Bool
forall a. Maybe a
Nothing
mixedAmountLooksZero :: MixedAmount -> Bool
mixedAmountLooksZero :: MixedAmount -> Bool
mixedAmountLooksZero = (Amount -> Bool) -> [Amount] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Amount -> Bool
amountLooksZero ([Amount] -> Bool)
-> (MixedAmount -> [Amount]) -> MixedAmount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts (MixedAmount -> [Amount])
-> (MixedAmount -> MixedAmount) -> MixedAmount -> [Amount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay
mixedAmountIsZero :: MixedAmount -> Bool
mixedAmountIsZero :: MixedAmount -> Bool
mixedAmountIsZero = (Amount -> Bool) -> [Amount] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Amount -> Bool
amountIsZero ([Amount] -> Bool)
-> (MixedAmount -> [Amount]) -> MixedAmount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts (MixedAmount -> [Amount])
-> (MixedAmount -> MixedAmount) -> MixedAmount -> [Amount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay
styleMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount Map CommoditySymbol AmountStyle
styles (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map (Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmount Map CommoditySymbol AmountStyle
styles) [Amount]
as
mixedAmountUnstyled :: MixedAmount -> MixedAmount
mixedAmountUnstyled :: MixedAmount -> MixedAmount
mixedAmountUnstyled = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount Amount -> Amount
amountUnstyled
showMixedAmount :: MixedAmount -> String
showMixedAmount :: MixedAmount -> String
showMixedAmount = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixed Amount -> String
showAmount Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
False
showMixedAmountOneLine :: MixedAmount -> String
showMixedAmountOneLine :: MixedAmount -> String
showMixedAmountOneLine = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLine Amount -> String
showAmountWithoutPrice Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
False
showMixedAmountWithZeroCommodity :: MixedAmount -> String
showMixedAmountWithZeroCommodity :: MixedAmount -> String
showMixedAmountWithZeroCommodity = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixed Amount -> String
showAmountWithZeroCommodity Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
False
showMixedAmountWithPrecision :: AmountPrecision -> MixedAmount -> String
showMixedAmountWithPrecision :: AmountPrecision -> MixedAmount -> String
showMixedAmountWithPrecision AmountPrecision
p = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixed (AmountPrecision -> Amount -> String
showAmountWithPrecision AmountPrecision
p) Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
False
showMixedAmountWithoutPrice :: Bool -> MixedAmount -> String
showMixedAmountWithoutPrice :: Bool -> MixedAmount -> String
showMixedAmountWithoutPrice Bool
c = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixed Amount -> String
showAmountWithoutPrice Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
c
showMixedAmountOneLineWithoutPrice :: Bool -> MixedAmount -> String
showMixedAmountOneLineWithoutPrice :: Bool -> MixedAmount -> String
showMixedAmountOneLineWithoutPrice Bool
c = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLine Amount -> String
showAmountWithoutPrice Maybe Int
forall a. Maybe a
Nothing Maybe Int
forall a. Maybe a
Nothing Bool
c
showMixedAmountElided :: Int -> Bool -> MixedAmount -> String
showMixedAmountElided :: Int -> Bool -> MixedAmount -> String
showMixedAmountElided Int
w Bool
c = (String, Int) -> String
forall a b. (a, b) -> a
fst ((String, Int) -> String)
-> (MixedAmount -> (String, Int)) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLine Amount -> String
showAmountWithoutPrice Maybe Int
forall a. Maybe a
Nothing (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
w) Bool
c
showMixedAmountDebug :: MixedAmount -> String
showMixedAmountDebug :: MixedAmount -> String
showMixedAmountDebug MixedAmount
m | MixedAmount
m MixedAmount -> MixedAmount -> Bool
forall a. Eq a => a -> a -> Bool
== MixedAmount
missingmixedamt = String
"(missing)"
| Bool
otherwise = String -> ShowS
forall r. PrintfType r => String -> r
printf String
"Mixed [%s]" String
as
where as :: String
as = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n " ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (Amount -> String) -> [Amount] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> String
showAmountDebug ([Amount] -> [String]) -> [Amount] -> [String]
forall a b. (a -> b) -> a -> b
$ MixedAmount -> [Amount]
amounts MixedAmount
m
showMixed :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixed :: (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixed Amount -> String
showamt Maybe Int
mmin Maybe Int
mmax Bool
c =
(Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedUnnormalised Amount -> String
showamt Maybe Int
mmin Maybe Int
mmax Bool
c (MixedAmount -> (String, Int))
-> (MixedAmount -> MixedAmount) -> MixedAmount -> (String, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay
showMixedUnnormalised :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedUnnormalised :: (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedUnnormalised Amount -> String
showamt Maybe Int
mmin Maybe Int
mmax Bool
c (Mixed [Amount]
as) =
(String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (AmountDisplay -> String) -> [AmountDisplay] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map AmountDisplay -> String
finalise [AmountDisplay]
elided, Int
width)
where
width :: Int
width = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mmin Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: (AmountDisplay -> Int) -> [AmountDisplay] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map AmountDisplay -> Int
adLength [AmountDisplay]
elided
astrs :: [AmountDisplay]
astrs = Int -> (Amount -> String) -> [Amount] -> [AmountDisplay]
amtDisplayList Int
sepwidth Amount -> String
showamt [Amount]
as
sepwidth :: Int
sepwidth = Int
0
finalise :: AmountDisplay -> String
finalise = AmountDisplay -> String
adString (AmountDisplay -> String)
-> (AmountDisplay -> AmountDisplay) -> AmountDisplay -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplay -> AmountDisplay
pad (AmountDisplay -> AmountDisplay)
-> (AmountDisplay -> AmountDisplay)
-> AmountDisplay
-> AmountDisplay
forall b c a. (b -> c) -> (a -> b) -> a -> c
. if Bool
c then AmountDisplay -> AmountDisplay
colourise else AmountDisplay -> AmountDisplay
forall a. a -> a
id
pad :: AmountDisplay -> AmountDisplay
pad AmountDisplay
amt = AmountDisplay
amt{ adString :: String
adString = Int -> ShowS -> ShowS
forall a. Int -> (a -> a) -> a -> a
applyN (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- AmountDisplay -> Int
adLength AmountDisplay
amt) (Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ AmountDisplay -> String
adString AmountDisplay
amt
, adLength :: Int
adLength = Int
width
}
elided :: [AmountDisplay]
elided = ([AmountDisplay] -> [AmountDisplay])
-> (Int -> [AmountDisplay] -> [AmountDisplay])
-> Maybe Int
-> [AmountDisplay]
-> [AmountDisplay]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [AmountDisplay] -> [AmountDisplay]
forall a. a -> a
id Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Maybe Int
mmax [AmountDisplay]
astrs
elideTo :: Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Int
m [AmountDisplay]
xs = Maybe AmountDisplay -> [AmountDisplay] -> [AmountDisplay]
forall a. Maybe a -> [a] -> [a]
maybeAppend Maybe AmountDisplay
elisionStr [AmountDisplay]
short
where
elisionStr :: Maybe AmountDisplay
elisionStr = Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
elisionDisplay (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
m) Int
sepwidth ([AmountDisplay] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AmountDisplay]
long) (AmountDisplay -> Maybe AmountDisplay)
-> AmountDisplay -> Maybe AmountDisplay
forall a b. (a -> b) -> a -> b
$ AmountDisplay -> [AmountDisplay] -> AmountDisplay
forall a. a -> [a] -> a
lastDef AmountDisplay
nullAmountDisplay [AmountDisplay]
short
([AmountDisplay]
short, [AmountDisplay]
long) = (AmountDisplay -> Bool)
-> [AmountDisplay] -> ([AmountDisplay], [AmountDisplay])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((Int
mInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=) (Int -> Bool) -> (AmountDisplay -> Int) -> AmountDisplay -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplay -> Int
adLength) [AmountDisplay]
xs
showMixedOneLine :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLine :: (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLine Amount -> String
showamt Maybe Int
mmin Maybe Int
mmax Bool
c =
(Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLineUnnormalised Amount -> String
showamt Maybe Int
mmin Maybe Int
mmax Bool
c (MixedAmount -> (String, Int))
-> (MixedAmount -> MixedAmount) -> MixedAmount -> (String, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay
showMixedOneLineUnnormalised :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLineUnnormalised :: (Amount -> String)
-> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int)
showMixedOneLineUnnormalised Amount -> String
showamt Maybe Int
mmin Maybe Int
mmax Bool
c (Mixed [Amount]
as) =
(ShowS
pad ShowS -> ([String] -> String) -> [String] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (AmountDisplay -> String) -> [AmountDisplay] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map AmountDisplay -> String
finalise [AmountDisplay]
elided, Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
width (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mmin)
where
width :: Int
width = Int -> (AmountDisplay -> Int) -> Maybe AmountDisplay -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 AmountDisplay -> Int
adTotal (Maybe AmountDisplay -> Int) -> Maybe AmountDisplay -> Int
forall a b. (a -> b) -> a -> b
$ [AmountDisplay] -> Maybe AmountDisplay
forall a. [a] -> Maybe a
lastMay [AmountDisplay]
elided
astrs :: [AmountDisplay]
astrs = Int -> (Amount -> String) -> [Amount] -> [AmountDisplay]
amtDisplayList Int
sepwidth Amount -> String
showamt [Amount]
as
sepwidth :: Int
sepwidth = Int
2
n :: Int
n = [Amount] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Amount]
as
finalise :: AmountDisplay -> String
finalise = AmountDisplay -> String
adString (AmountDisplay -> String)
-> (AmountDisplay -> AmountDisplay) -> AmountDisplay -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. if Bool
c then AmountDisplay -> AmountDisplay
colourise else AmountDisplay -> AmountDisplay
forall a. a -> a
id
pad :: ShowS
pad = Int -> ShowS -> ShowS
forall a. Int -> (a -> a) -> a -> a
applyN (Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mmin Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
width) (Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:)
elided :: [AmountDisplay]
elided = ([AmountDisplay] -> [AmountDisplay])
-> (Int -> [AmountDisplay] -> [AmountDisplay])
-> Maybe Int
-> [AmountDisplay]
-> [AmountDisplay]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [AmountDisplay] -> [AmountDisplay]
forall a. a -> a
id Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Maybe Int
mmax [AmountDisplay]
astrs
elideTo :: Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Int
m = [(AmountDisplay, Maybe AmountDisplay)] -> [AmountDisplay]
forall a. [(a, Maybe a)] -> [a]
addElide ([(AmountDisplay, Maybe AmountDisplay)] -> [AmountDisplay])
-> ([AmountDisplay] -> [(AmountDisplay, Maybe AmountDisplay)])
-> [AmountDisplay]
-> [AmountDisplay]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> [(AmountDisplay, Maybe AmountDisplay)]
-> [(AmountDisplay, Maybe AmountDisplay)]
forall (t :: * -> *).
Foldable t =>
Int
-> t (AmountDisplay, Maybe AmountDisplay)
-> [(AmountDisplay, Maybe AmountDisplay)]
takeFitting Int
m ([(AmountDisplay, Maybe AmountDisplay)]
-> [(AmountDisplay, Maybe AmountDisplay)])
-> ([AmountDisplay] -> [(AmountDisplay, Maybe AmountDisplay)])
-> [AmountDisplay]
-> [(AmountDisplay, Maybe AmountDisplay)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AmountDisplay] -> [(AmountDisplay, Maybe AmountDisplay)]
withElided
addElide :: [(a, Maybe a)] -> [a]
addElide [] = []
addElide [(a, Maybe a)]
xs = Maybe a -> [a] -> [a]
forall a. Maybe a -> [a] -> [a]
maybeAppend ((a, Maybe a) -> Maybe a
forall a b. (a, b) -> b
snd ((a, Maybe a) -> Maybe a) -> (a, Maybe a) -> Maybe a
forall a b. (a -> b) -> a -> b
$ [(a, Maybe a)] -> (a, Maybe a)
forall a. [a] -> a
last [(a, Maybe a)]
xs) ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ ((a, Maybe a) -> a) -> [(a, Maybe a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, Maybe a) -> a
forall a b. (a, b) -> a
fst [(a, Maybe a)]
xs
takeFitting :: Int
-> t (AmountDisplay, Maybe AmountDisplay)
-> [(AmountDisplay, Maybe AmountDisplay)]
takeFitting Int
m = ((AmountDisplay, Maybe AmountDisplay) -> Bool)
-> t (AmountDisplay, Maybe AmountDisplay)
-> [(AmountDisplay, Maybe AmountDisplay)]
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> [a]
dropWhileRev (\(AmountDisplay
a,Maybe AmountDisplay
e) -> Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< AmountDisplay -> Int
adTotal (AmountDisplay -> Maybe AmountDisplay -> AmountDisplay
forall a. a -> Maybe a -> a
fromMaybe AmountDisplay
a Maybe AmountDisplay
e))
dropWhileRev :: (a -> Bool) -> t a -> [a]
dropWhileRev a -> Bool
p = (a -> [a] -> [a]) -> [a] -> t a -> [a]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x [a]
xs -> if [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs Bool -> Bool -> Bool
&& a -> Bool
p a
x then [] else a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) []
withElided :: [AmountDisplay] -> [(AmountDisplay, Maybe AmountDisplay)]
withElided = (Int -> AmountDisplay -> (AmountDisplay, Maybe AmountDisplay))
-> [Int]
-> [AmountDisplay]
-> [(AmountDisplay, Maybe AmountDisplay)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Int
num AmountDisplay
amt -> (AmountDisplay
amt, Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
elisionDisplay Maybe Int
forall a. Maybe a
Nothing Int
sepwidth Int
num AmountDisplay
amt)) [Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1,Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
2..Int
0]
data AmountDisplay = AmountDisplay
{ AmountDisplay -> Amount
adAmount :: !Amount
, AmountDisplay -> String
adString :: !String
, AmountDisplay -> Int
adLength :: !Int
, AmountDisplay -> Int
adTotal :: !Int
} deriving (Int -> AmountDisplay -> ShowS
[AmountDisplay] -> ShowS
AmountDisplay -> String
(Int -> AmountDisplay -> ShowS)
-> (AmountDisplay -> String)
-> ([AmountDisplay] -> ShowS)
-> Show AmountDisplay
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AmountDisplay] -> ShowS
$cshowList :: [AmountDisplay] -> ShowS
show :: AmountDisplay -> String
$cshow :: AmountDisplay -> String
showsPrec :: Int -> AmountDisplay -> ShowS
$cshowsPrec :: Int -> AmountDisplay -> ShowS
Show)
nullAmountDisplay :: AmountDisplay
nullAmountDisplay :: AmountDisplay
nullAmountDisplay = Amount -> String -> Int -> Int -> AmountDisplay
AmountDisplay Amount
nullamt String
"" Int
0 Int
0
amtDisplayList :: Int -> (Amount -> String) -> [Amount] -> [AmountDisplay]
amtDisplayList :: Int -> (Amount -> String) -> [Amount] -> [AmountDisplay]
amtDisplayList Int
sep Amount -> String
showamt = (Int, [AmountDisplay]) -> [AmountDisplay]
forall a b. (a, b) -> b
snd ((Int, [AmountDisplay]) -> [AmountDisplay])
-> ([Amount] -> (Int, [AmountDisplay]))
-> [Amount]
-> [AmountDisplay]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Amount -> (Int, AmountDisplay))
-> Int -> [Amount] -> (Int, [AmountDisplay])
forall (t :: * -> *) a b c.
Traversable t =>
(a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL Int -> Amount -> (Int, AmountDisplay)
display (-Int
sep)
where
display :: Int -> Amount -> (Int, AmountDisplay)
display Int
tot Amount
amt = (Int
tot', Amount -> String -> Int -> Int -> AmountDisplay
AmountDisplay Amount
amt String
str Int
width Int
tot')
where
str :: String
str = Amount -> String
showamt Amount
amt
width :: Int
width = String -> Int
strWidth String
str
tot' :: Int
tot' = Int
tot Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sep
elisionDisplay :: Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
elisionDisplay :: Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
elisionDisplay Maybe Int
mmax Int
sep Int
n AmountDisplay
lastAmt
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = AmountDisplay -> Maybe AmountDisplay
forall a. a -> Maybe a
Just (AmountDisplay -> Maybe AmountDisplay)
-> AmountDisplay -> Maybe AmountDisplay
forall a b. (a -> b) -> a -> b
$ Amount -> String -> Int -> Int -> AmountDisplay
AmountDisplay Amount
0 String
str Int
len (AmountDisplay -> Int
adTotal AmountDisplay
lastAmt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len)
| Bool
otherwise = Maybe AmountDisplay
forall a. Maybe a
Nothing
where
fullString :: String
fullString = Int -> String
forall a. Show a => a -> String
show Int
n String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" more.."
fullLength :: Int
fullLength = Int
sep Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase Double
10 (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
str :: String
str | Just Int
m <- Maybe Int
mmax, Int
fullLength Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
m = Int -> ShowS
forall a. Int -> [a] -> [a]
take (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) String
fullString String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
".."
| Bool
otherwise = String
fullString
len :: Int
len = case Maybe Int
mmax of Maybe Int
Nothing -> Int
fullLength
Just Int
m -> Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
2 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
m Int
fullLength
maybeAppend :: Maybe a -> [a] -> [a]
maybeAppend :: Maybe a -> [a] -> [a]
maybeAppend Maybe a
Nothing = [a] -> [a]
forall a. a -> a
id
maybeAppend (Just a
a) = ([a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++[a
a])
colourise :: AmountDisplay -> AmountDisplay
colourise :: AmountDisplay -> AmountDisplay
colourise AmountDisplay
amt = AmountDisplay
amt{adString :: String
adString=ShowS
markColour ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ AmountDisplay -> String
adString AmountDisplay
amt}
where markColour :: ShowS
markColour = if Amount -> Bool
isNegativeAmount (AmountDisplay -> Amount
adAmount AmountDisplay
amt) then ColorIntensity -> Color -> ShowS
color ColorIntensity
Dull Color
Red else ShowS
forall a. a -> a
id
ltraceamount :: String -> MixedAmount -> MixedAmount
ltraceamount :: String -> MixedAmount -> MixedAmount
ltraceamount String
s = (MixedAmount -> String) -> MixedAmount -> MixedAmount
forall a. Show a => (a -> String) -> a -> a
traceWith (((String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": ") String -> ShowS
forall a. [a] -> [a] -> [a]
++)ShowS -> (MixedAmount -> String) -> MixedAmount -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.MixedAmount -> String
showMixedAmount)
setMixedAmountPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
setMixedAmountPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
setMixedAmountPrecision AmountPrecision
p (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map (AmountPrecision -> Amount -> Amount
setAmountPrecision AmountPrecision
p) [Amount]
as
mixedAmountStripPrices :: MixedAmount -> MixedAmount
mixedAmountStripPrices :: MixedAmount -> MixedAmount
mixedAmountStripPrices (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map (\Amount
a -> Amount
a{aprice :: Maybe AmountPrice
aprice=Maybe AmountPrice
forall a. Maybe a
Nothing}) [Amount]
as
canonicaliseMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
canonicaliseMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
canonicaliseMixedAmount Map CommoditySymbol AmountStyle
styles (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map (Map CommoditySymbol AmountStyle -> Amount -> Amount
canonicaliseAmount Map CommoditySymbol AmountStyle
styles) [Amount]
as
mixedAmountTotalPriceToUnitPrice :: MixedAmount -> MixedAmount
mixedAmountTotalPriceToUnitPrice :: MixedAmount -> MixedAmount
mixedAmountTotalPriceToUnitPrice (Mixed [Amount]
as) = [Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount) -> [Amount] -> MixedAmount
forall a b. (a -> b) -> a -> b
$ (Amount -> Amount) -> [Amount] -> [Amount]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> Amount
amountTotalPriceToUnitPrice [Amount]
as
tests_Amount :: TestTree
tests_Amount = String -> [TestTree] -> TestTree
tests String
"Amount" [
String -> [TestTree] -> TestTree
tests String
"Amount" [
String -> Assertion -> TestTree
test String
"amountCost" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Amount -> Amount
amountCost (Quantity -> Amount
eur Quantity
1) Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
eur Quantity
1
Amount -> Amount
amountCost (Quantity -> Amount
eur Quantity
2){aprice :: Maybe AmountPrice
aprice=AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
UnitPrice (Amount -> AmountPrice) -> Amount -> AmountPrice
forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
2} Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
4
Amount -> Amount
amountCost (Quantity -> Amount
eur Quantity
1){aprice :: Maybe AmountPrice
aprice=AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
TotalPrice (Amount -> AmountPrice) -> Amount -> AmountPrice
forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
2} Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
2
Amount -> Amount
amountCost (Quantity -> Amount
eur (-Quantity
1)){aprice :: Maybe AmountPrice
aprice=AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
TotalPrice (Amount -> AmountPrice) -> Amount -> AmountPrice
forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
2} Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd (-Quantity
2)
,String -> Assertion -> TestTree
test String
"amountLooksZero" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
HasCallStack => String -> Bool -> Assertion
String -> Bool -> Assertion
assertBool String
"" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Amount -> Bool
amountLooksZero Amount
amount
HasCallStack => String -> Bool -> Assertion
String -> Bool -> Assertion
assertBool String
"" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Amount -> Bool
amountLooksZero (Amount -> Bool) -> Amount -> Bool
forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
0
,String -> Assertion -> TestTree
test String
"negating amounts" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Amount -> Amount
forall a. Num a => a -> a
negate (Quantity -> Amount
usd Quantity
1) Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Quantity -> Amount
usd Quantity
1){aquantity :: Quantity
aquantity= -Quantity
1}
let b :: Amount
b = (Quantity -> Amount
usd Quantity
1){aprice :: Maybe AmountPrice
aprice=AmountPrice -> Maybe AmountPrice
forall a. a -> Maybe a
Just (AmountPrice -> Maybe AmountPrice)
-> AmountPrice -> Maybe AmountPrice
forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
UnitPrice (Amount -> AmountPrice) -> Amount -> AmountPrice
forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
eur Quantity
2} in Amount -> Amount
forall a. Num a => a -> a
negate Amount
b Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Amount
b{aquantity :: Quantity
aquantity= -Quantity
1}
,String -> Assertion -> TestTree
test String
"adding amounts without prices" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
(Quantity -> Amount
usd Quantity
1.23 Amount -> Amount -> Amount
forall a. Num a => a -> a -> a
+ Quantity -> Amount
usd (-Quantity
1.23)) Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
0
(Quantity -> Amount
usd Quantity
1.23 Amount -> Amount -> Amount
forall a. Num a => a -> a -> a
+ Quantity -> Amount
usd (-Quantity
1.23)) Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
0
(Quantity -> Amount
usd (-Quantity
1.23) Amount -> Amount -> Amount
forall a. Num a => a -> a -> a
+ Quantity -> Amount
usd (-Quantity
1.23)) Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd (-Quantity
2.46)
[Amount] -> Amount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Amount
usd Quantity
1.23,Quantity -> Amount
usd (-Quantity
1.23),Quantity -> Amount
usd (-Quantity
1.23),-(Quantity -> Amount
usd (-Quantity
1.23))] Amount -> Amount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
0
AmountStyle -> AmountPrecision
asprecision (Amount -> AmountStyle
astyle (Amount -> AmountStyle) -> Amount -> AmountStyle
forall a b. (a -> b) -> a -> b
$ [Amount] -> Amount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Amount
usd Quantity
1 Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
1, Quantity -> Amount
usd Quantity
1 Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
3]) AmountPrecision -> AmountPrecision -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Word8 -> AmountPrecision
Precision Word8
3
AmountStyle -> AmountPrecision
asprecision (Amount -> AmountStyle
astyle (Amount -> AmountStyle) -> Amount -> AmountStyle
forall a b. (a -> b) -> a -> b
$ [Amount] -> Amount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Amount
usd Quantity
1 Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
3, Quantity -> Amount
usd Quantity
1 Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
1]) AmountPrecision -> AmountPrecision -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Word8 -> AmountPrecision
Precision Word8
3
HasCallStack => String -> Bool -> Assertion
String -> Bool -> Assertion
assertBool String
"" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Amount -> Bool
amountLooksZero (Quantity -> Amount
usd Quantity
1.23 Amount -> Amount -> Amount
forall a. Num a => a -> a -> a
- Quantity -> Amount
eur Quantity
1.23)
,String -> Assertion -> TestTree
test String
"showAmount" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Amount -> String
showAmount (Quantity -> Amount
usd Quantity
0 Amount -> Amount -> Amount
forall a. Num a => a -> a -> a
+ Quantity -> Amount
gbp Quantity
0) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"0"
]
,String -> [TestTree] -> TestTree
tests String
"MixedAmount" [
String -> Assertion -> TestTree
test String
"adding mixed amounts to zero, the commodity and amount style are preserved" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
[MixedAmount] -> MixedAmount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Amount -> MixedAmount) -> [Amount] -> [MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map ([Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount)
-> (Amount -> [Amount]) -> Amount -> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> [Amount] -> [Amount]
forall a. a -> [a] -> [a]
:[]))
[Quantity -> Amount
usd Quantity
1.25
,Quantity -> Amount
usd (-Quantity
1) Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
3
,Quantity -> Amount
usd (-Quantity
0.25)
])
MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
0 Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
3]
,String -> Assertion -> TestTree
test String
"adding mixed amounts with total prices" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
[MixedAmount] -> MixedAmount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Amount -> MixedAmount) -> [Amount] -> [MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map ([Amount] -> MixedAmount
Mixed ([Amount] -> MixedAmount)
-> (Amount -> [Amount]) -> Amount -> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Amount -> [Amount] -> [Amount]
forall a. a -> [a] -> [a]
:[]))
[Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1
,Quantity -> Amount
usd (-Quantity
2) Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1
])
MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1
,Quantity -> Amount
usd (-Quantity
2) Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1
]
,String -> Assertion -> TestTree
test String
"showMixedAmount" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
MixedAmount -> String
showMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1]) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"$1.00"
MixedAmount -> String
showMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
2]) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"$1.00 @ €2.00"
MixedAmount -> String
showMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
0]) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"0"
MixedAmount -> String
showMixedAmount ([Amount] -> MixedAmount
Mixed []) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"0"
MixedAmount -> String
showMixedAmount MixedAmount
missingmixedamt String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
""
,String -> Assertion -> TestTree
test String
"showMixedAmountWithoutPrice" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let a :: Amount
a = Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
2
Bool -> MixedAmount -> String
showMixedAmountWithoutPrice Bool
False ([Amount] -> MixedAmount
Mixed [Amount
a]) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"$1.00"
Bool -> MixedAmount -> String
showMixedAmountWithoutPrice Bool
False ([Amount] -> MixedAmount
Mixed [Amount
a, -Amount
a]) String -> String -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= String
"0"
,String -> [TestTree] -> TestTree
tests String
"normaliseMixedAmount" [
String -> Assertion -> TestTree
test String
"a missing amount overrides any other amounts" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
MixedAmount -> MixedAmount
normaliseMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1, Amount
missingamt]) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= MixedAmount
missingmixedamt
,String -> Assertion -> TestTree
test String
"unpriced same-commodity amounts are combined" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
MixedAmount -> MixedAmount
normaliseMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
0, Quantity -> Amount
usd Quantity
2]) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
2]
,String -> Assertion -> TestTree
test String
"amounts with same unit price are combined" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
MixedAmount -> MixedAmount
normaliseMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
1, Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
1]) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
2 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
1]
,String -> Assertion -> TestTree
test String
"amounts with different unit prices are not combined" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
MixedAmount -> MixedAmount
normaliseMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
1, Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
2]) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
1, Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
2]
,String -> Assertion -> TestTree
test String
"amounts with total prices are not combined" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
MixedAmount -> MixedAmount
normaliseMixedAmount ([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1, Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1]) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1, Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
1]
]
,String -> Assertion -> TestTree
test String
"normaliseMixedAmountSquashPricesForDisplay" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay ([Amount] -> MixedAmount
Mixed []) MixedAmount -> MixedAmount -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount] -> MixedAmount
Mixed [Amount
nullamt]
HasCallStack => String -> Bool -> Assertion
String -> Bool -> Assertion
assertBool String
"" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ MixedAmount -> Bool
mixedAmountLooksZero (MixedAmount -> Bool) -> MixedAmount -> Bool
forall a b. (a -> b) -> a -> b
$ MixedAmount -> MixedAmount
normaliseMixedAmountSquashPricesForDisplay
([Amount] -> MixedAmount
Mixed [Quantity -> Amount
usd Quantity
10
,Quantity -> Amount
usd Quantity
10 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
7
,Quantity -> Amount
usd (-Quantity
10)
,Quantity -> Amount
usd (-Quantity
10) Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
7
])
]
]