{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Hledger.Data.Amount (
showCommoditySymbol,
isNonsimpleCommodityChar,
quoteCommoditySymbolIfNeeded,
nullamt,
missingamt,
num,
usd,
eur,
gbp,
per,
hrs,
at,
(@@),
amountWithCommodity,
amountCost,
amountIsZero,
amountLooksZero,
divideAmount,
multiplyAmount,
AmountDisplayOpts(..),
noColour,
noPrice,
oneLine,
csvDisplay,
amountstyle,
styleAmount,
styleAmountExceptPrecision,
amountUnstyled,
showAmountB,
showAmount,
showAmountPrice,
cshowAmount,
showAmountWithZeroCommodity,
showAmountDebug,
showAmountWithoutPrice,
amountSetPrecision,
withPrecision,
amountSetFullPrecision,
setAmountInternalPrecision,
withInternalPrecision,
setAmountDecimalPoint,
withDecimalPoint,
amountStripPrices,
canonicaliseAmount,
nullmixedamt,
missingmixedamt,
isMissingMixedAmount,
mixed,
mixedAmount,
maAddAmount,
maAddAmounts,
amounts,
amountsRaw,
maCommodities,
filterMixedAmount,
filterMixedAmountByCommodity,
mapMixedAmount,
unifyMixedAmount,
mixedAmountStripPrices,
mixedAmountCost,
maNegate,
maPlus,
maMinus,
maSum,
divideMixedAmount,
multiplyMixedAmount,
averageMixedAmounts,
isNegativeAmount,
isNegativeMixedAmount,
mixedAmountIsZero,
maIsZero,
maIsNonZero,
mixedAmountLooksZero,
styleMixedAmount,
mixedAmountUnstyled,
showMixedAmount,
showMixedAmountOneLine,
showMixedAmountDebug,
showMixedAmountWithoutPrice,
showMixedAmountOneLineWithoutPrice,
showMixedAmountElided,
showMixedAmountWithZeroCommodity,
showMixedAmountB,
showMixedAmountLinesB,
wbToText,
wbUnpack,
mixedAmountSetPrecision,
mixedAmountSetFullPrecision,
canonicaliseMixedAmount,
ltraceamount,
tests_Amount
) where
import Control.Applicative (liftA2)
import Control.Monad (foldM)
import Data.Char (isDigit)
import Data.Decimal (DecimalRaw(..), decimalPlaces, normalizeDecimal, roundTo)
import Data.Default (Default(..))
import Data.Foldable (toList)
import Data.List (find, foldl', intercalate, intersperse, mapAccumL, partition)
import Data.List.NonEmpty (NonEmpty(..), nonEmpty)
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.Maybe (fromMaybe, isNothing, isJust)
import Data.Semigroup (Semigroup(..))
import qualified Data.Text as T
import qualified Data.Text.Lazy.Builder as TB
import Data.Word (Word8)
import Safe (headDef, lastDef, lastMay)
import System.Console.ANSI (Color(..),ColorIntensity(..))
import Debug.Trace (trace)
import Test.Tasty (testGroup)
import Test.Tasty.HUnit ((@?=), assertBool, testCase)
import Hledger.Data.Types
import Hledger.Utils (colorB, numDigitsInt)
import Hledger.Utils.Text (textQuoteIfNeeded)
import Text.WideString (WideBuilder(..), wbFromText, wbToText, wbUnpack)
showCommoditySymbol :: T.Text -> T.Text
showCommoditySymbol :: CommoditySymbol -> CommoditySymbol
showCommoditySymbol = CommoditySymbol -> CommoditySymbol
textQuoteIfNeeded
isNonsimpleCommodityChar :: Char -> Bool
isNonsimpleCommodityChar :: Char -> Bool
isNonsimpleCommodityChar = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(||) Char -> Bool
isDigit Char -> Bool
isOther
where
otherChars :: CommoditySymbol
otherChars = CommoditySymbol
"-+.@*;\t\n \"{}=" :: T.Text
isOther :: Char -> Bool
isOther Char
c = (Char -> Bool) -> CommoditySymbol -> Bool
T.any (forall a. Eq a => a -> a -> Bool
==Char
c) CommoditySymbol
otherChars
quoteCommoditySymbolIfNeeded :: T.Text -> T.Text
quoteCommoditySymbolIfNeeded :: CommoditySymbol -> CommoditySymbol
quoteCommoditySymbolIfNeeded CommoditySymbol
s
| (Char -> Bool) -> CommoditySymbol -> Bool
T.any Char -> Bool
isNonsimpleCommodityChar CommoditySymbol
s = CommoditySymbol
"\"" forall a. Semigroup a => a -> a -> a
<> CommoditySymbol
s forall a. Semigroup a => a -> a -> a
<> CommoditySymbol
"\""
| Bool
otherwise = CommoditySymbol
s
data AmountDisplayOpts = AmountDisplayOpts
{ AmountDisplayOpts -> Bool
displayPrice :: Bool
, AmountDisplayOpts -> Bool
displayZeroCommodity :: Bool
, AmountDisplayOpts -> Bool
displayThousandsSep :: Bool
, AmountDisplayOpts -> Bool
displayColour :: Bool
, AmountDisplayOpts -> Bool
displayOneLine :: Bool
, AmountDisplayOpts -> Maybe Int
displayMinWidth :: Maybe Int
, AmountDisplayOpts -> Maybe Int
displayMaxWidth :: Maybe Int
, AmountDisplayOpts -> Maybe [CommoditySymbol]
displayOrder :: Maybe [CommoditySymbol]
} deriving (Int -> AmountDisplayOpts -> ShowS
[AmountDisplayOpts] -> ShowS
AmountDisplayOpts -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [AmountDisplayOpts] -> ShowS
$cshowList :: [AmountDisplayOpts] -> ShowS
show :: AmountDisplayOpts -> [Char]
$cshow :: AmountDisplayOpts -> [Char]
showsPrec :: Int -> AmountDisplayOpts -> ShowS
$cshowsPrec :: Int -> AmountDisplayOpts -> ShowS
Show)
instance Default AmountDisplayOpts where def :: AmountDisplayOpts
def = AmountDisplayOpts
noColour
noColour :: AmountDisplayOpts
noColour :: AmountDisplayOpts
noColour = AmountDisplayOpts { displayPrice :: Bool
displayPrice = Bool
True
, displayColour :: Bool
displayColour = Bool
False
, displayZeroCommodity :: Bool
displayZeroCommodity = Bool
False
, displayThousandsSep :: Bool
displayThousandsSep = Bool
True
, displayOneLine :: Bool
displayOneLine = Bool
False
, displayMinWidth :: Maybe Int
displayMinWidth = forall a. a -> Maybe a
Just Int
0
, displayMaxWidth :: Maybe Int
displayMaxWidth = forall a. Maybe a
Nothing
, displayOrder :: Maybe [CommoditySymbol]
displayOrder = forall a. Maybe a
Nothing
}
noPrice :: AmountDisplayOpts
noPrice :: AmountDisplayOpts
noPrice = forall a. Default a => a
def{displayPrice :: Bool
displayPrice=Bool
False}
oneLine :: AmountDisplayOpts
oneLine :: AmountDisplayOpts
oneLine = forall a. Default a => a
def{displayOneLine :: Bool
displayOneLine=Bool
True, displayPrice :: Bool
displayPrice=Bool
False}
csvDisplay :: AmountDisplayOpts
csvDisplay :: AmountDisplayOpts
csvDisplay = AmountDisplayOpts
oneLine{displayThousandsSep :: Bool
displayThousandsSep=Bool
False}
amountstyle :: AmountStyle
amountstyle = Side
-> Bool
-> AmountPrecision
-> Maybe Char
-> Maybe DigitGroupStyle
-> AmountStyle
AmountStyle Side
L Bool
False (Word8 -> AmountPrecision
Precision Word8
0) (forall a. a -> Maybe a
Just Char
'.') 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=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=forall a. Num a => a -> a
signum Quantity
q}
fromInteger :: Integer -> Amount
fromInteger Integer
i = Amount
nullamt{aquantity :: Quantity
aquantity=forall a. Num a => Integer -> a
fromInteger Integer
i}
negate :: Amount -> Amount
negate = (Quantity -> Quantity) -> Amount -> Amount
transformAmount forall a. Num a => a -> a
negate
+ :: Amount -> Amount -> Amount
(+) = (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp forall a. Num a => a -> a -> a
(+)
(-) = (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp (-)
* :: Amount -> Amount -> Amount
(*) = (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Amount
similarAmountsOp forall a. Num a => a -> a -> a
(*)
nullamt :: Amount
nullamt :: Amount
nullamt = Amount{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"", aquantity :: Quantity
aquantity=Quantity
0, aprice :: Maybe AmountPrice
aprice=forall a. Maybe a
Nothing, astyle :: AmountStyle
astyle=AmountStyle
amountstyle}
missingamt :: Amount
missingamt :: Amount
missingamt = Amount
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"AUTO"}
num :: Quantity -> Amount
num Quantity
n = Amount
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"", aquantity :: Quantity
aquantity=Quantity
n}
hrs :: Quantity -> Amount
hrs Quantity
n = Amount
nullamt{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
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"$", aquantity :: Quantity
aquantity=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
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"€", aquantity :: Quantity
aquantity=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
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
"£", aquantity :: Quantity
aquantity=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
nullamt{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=forall a. a -> Maybe a
Just 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=forall a. a -> Maybe a
Just 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
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
c2, aquantity :: Quantity
aquantity=Quantity
q1 Quantity -> Quantity -> Quantity
`op` Quantity
q2, astyle :: AmountStyle
astyle=AmountStyle
s2{asprecision :: AmountPrecision
asprecision=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=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 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}
transformAmount :: (Quantity -> Quantity) -> Amount -> Amount
transformAmount :: (Quantity -> Quantity) -> Amount -> Amount
transformAmount Quantity -> Quantity
f a :: Amount
a@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q,aprice :: Amount -> Maybe AmountPrice
aprice=Maybe AmountPrice
p} = Amount
a{aquantity :: Quantity
aquantity=Quantity -> Quantity
f Quantity
q, aprice :: Maybe AmountPrice
aprice=AmountPrice -> AmountPrice
f' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe AmountPrice
p}
where
f' :: AmountPrice -> AmountPrice
f' (TotalPrice a1 :: Amount
a1@Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
pq}) = Amount -> AmountPrice
TotalPrice Amount
a1{aquantity :: Quantity
aquantity = Quantity -> Quantity
f Quantity
pq}
f' AmountPrice
p' = AmountPrice
p'
divideAmount :: Quantity -> Amount -> Amount
divideAmount :: Quantity -> Amount -> Amount
divideAmount Quantity
n = (Quantity -> Quantity) -> Amount -> Amount
transformAmount (forall a. Fractional a => a -> a -> a
/Quantity
n)
multiplyAmount :: Quantity -> Amount -> Amount
multiplyAmount :: Quantity -> Amount -> Amount
multiplyAmount Quantity
n = (Quantity -> Quantity) -> Amount -> Amount
transformAmount (forall a. Num a => a -> a -> a
*Quantity
n)
isNegativeAmount :: Amount -> Bool
isNegativeAmount :: Amount -> Bool
isNegativeAmount Amount{aquantity :: Amount -> Quantity
aquantity=Quantity
q} = Quantity
q 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' -> forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
p' Quantity
q
testAmountAndTotalPrice :: (Amount -> Bool) -> Amount -> Bool
testAmountAndTotalPrice :: (Amount -> Bool) -> Amount -> Bool
testAmountAndTotalPrice Amount -> Bool
f Amount
amt = case Amount -> Maybe AmountPrice
aprice Amount
amt of
Just (TotalPrice Amount
price) -> Amount -> Bool
f Amount
amt Bool -> Bool -> Bool
&& Amount -> Bool
f Amount
price
Maybe AmountPrice
_ -> Amount -> Bool
f Amount
amt
amountLooksZero :: Amount -> Bool
amountLooksZero :: Amount -> Bool
amountLooksZero = (Amount -> Bool) -> Amount -> Bool
testAmountAndTotalPrice Amount -> Bool
looksZero
where
looksZero :: Amount -> Bool
looksZero Amount{aquantity :: Amount -> Quantity
aquantity=Decimal Word8
e Integer
q, astyle :: Amount -> AmountStyle
astyle=AmountStyle{asprecision :: AmountStyle -> AmountPrecision
asprecision=AmountPrecision
p}} = case AmountPrecision
p of
Precision Word8
d -> if Word8
e forall a. Ord a => a -> a -> Bool
> Word8
d then forall a. Num a => a -> a
abs Integer
q forall a. Ord a => a -> a -> Bool
<= Integer
5forall a. Num a => a -> a -> a
*Integer
10forall a b. (Num a, Integral b) => a -> b -> a
^(Word8
eforall a. Num a => a -> a -> a
-Word8
dforall a. Num a => a -> a -> a
-Word8
1) else Integer
q forall a. Eq a => a -> a -> Bool
== Integer
0
AmountPrecision
NaturalPrecision -> Integer
q forall a. Eq a => a -> a -> Bool
== Integer
0
amountIsZero :: Amount -> Bool
amountIsZero :: Amount -> Bool
amountIsZero = (Amount -> Bool) -> Amount -> Bool
testAmountAndTotalPrice (\Amount{aquantity :: Amount -> Quantity
aquantity=Decimal Word8
_ Integer
q} -> Integer
q forall a. Eq a => a -> a -> Bool
== Integer
0)
withPrecision :: Amount -> AmountPrecision -> Amount
withPrecision :: Amount -> AmountPrecision -> Amount
withPrecision = forall a b c. (a -> b -> c) -> b -> a -> c
flip AmountPrecision -> Amount -> Amount
amountSetPrecision
amountSetPrecision :: AmountPrecision -> Amount -> Amount
amountSetPrecision :: AmountPrecision -> Amount -> Amount
amountSetPrecision AmountPrecision
p a :: Amount
a@Amount{astyle :: Amount -> AmountStyle
astyle=AmountStyle
s} = Amount
a{astyle :: AmountStyle
astyle=AmountStyle
s{asprecision :: AmountPrecision
asprecision=AmountPrecision
p}}
amountSetFullPrecision :: Amount -> Amount
amountSetFullPrecision :: Amount -> Amount
amountSetFullPrecision Amount
a = AmountPrecision -> Amount -> Amount
amountSetPrecision AmountPrecision
p Amount
a
where
p :: AmountPrecision
p = forall a. Ord a => a -> a -> a
max AmountPrecision
displayprecision AmountPrecision
naturalprecision
displayprecision :: AmountPrecision
displayprecision = AmountStyle -> AmountPrecision
asprecision forall a b. (a -> b) -> a -> b
$ Amount -> AmountStyle
astyle Amount
a
naturalprecision :: AmountPrecision
naturalprecision = Word8 -> AmountPrecision
Precision forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i. DecimalRaw i -> Word8
decimalPlaces forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i. Integral i => DecimalRaw i -> DecimalRaw i
normalizeDecimal 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=forall i. Integral i => Word8 -> DecimalRaw i -> DecimalRaw i
roundTo Word8
p Quantity
q
}
withInternalPrecision :: Amount -> Word8 -> Amount
withInternalPrecision :: Amount -> Word8 -> Amount
withInternalPrecision = 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 = forall a b c. (a -> b -> c) -> b -> a -> c
flip Maybe Char -> Amount -> Amount
setAmountDecimalPoint
amountStripPrices :: Amount -> Amount
amountStripPrices :: Amount -> Amount
amountStripPrices Amount
a = Amount
a{aprice :: Maybe AmountPrice
aprice=forall a. Maybe a
Nothing}
showAmountPrice :: Amount -> WideBuilder
showAmountPrice :: Amount -> WideBuilder
showAmountPrice Amount
amt = case Amount -> Maybe AmountPrice
aprice Amount
amt of
Maybe AmountPrice
Nothing -> forall a. Monoid a => a
mempty
Just (UnitPrice Amount
pa) -> Builder -> Int -> WideBuilder
WideBuilder ([Char] -> Builder
TB.fromString [Char]
" @ ") Int
3 forall a. Semigroup a => a -> a -> a
<> AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
noColour Amount
pa
Just (TotalPrice Amount
pa) -> Builder -> Int -> WideBuilder
WideBuilder ([Char] -> Builder
TB.fromString [Char]
" @@ ") Int
4 forall a. Semigroup a => a -> a -> a
<> AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
noColour (Amount -> Amount
sign Amount
pa)
where sign :: Amount -> Amount
sign = if Amount -> Quantity
aquantity Amount
amt forall a. Ord a => a -> a -> Bool
< Quantity
0 then forall a. Num a => a -> a
negate else forall a. a -> a
id
showAmountPriceDebug :: Maybe AmountPrice -> String
showAmountPriceDebug :: Maybe AmountPrice -> [Char]
showAmountPriceDebug Maybe AmountPrice
Nothing = [Char]
""
showAmountPriceDebug (Just (UnitPrice Amount
pa)) = [Char]
" @ " forall a. [a] -> [a] -> [a]
++ Amount -> [Char]
showAmountDebug Amount
pa
showAmountPriceDebug (Just (TotalPrice Amount
pa)) = [Char]
" @@ " forall a. [a] -> [a] -> [a]
++ Amount -> [Char]
showAmountDebug Amount
pa
styleAmount :: M.Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmount Map CommoditySymbol AmountStyle
styles Amount
a = Amount
styledAmount{aprice :: Maybe AmountPrice
aprice = Map CommoditySymbol AmountStyle
-> Maybe AmountPrice -> Maybe AmountPrice
stylePrice Map CommoditySymbol AmountStyle
styles (Amount -> Maybe AmountPrice
aprice Amount
styledAmount)}
where
styledAmount :: Amount
styledAmount = case 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
stylePrice :: M.Map CommoditySymbol AmountStyle -> Maybe AmountPrice -> Maybe AmountPrice
stylePrice :: Map CommoditySymbol AmountStyle
-> Maybe AmountPrice -> Maybe AmountPrice
stylePrice Map CommoditySymbol AmountStyle
styles (Just (UnitPrice Amount
a)) = forall a. a -> Maybe a
Just (Amount -> AmountPrice
UnitPrice forall a b. (a -> b) -> a -> b
$ Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map CommoditySymbol AmountStyle
styles Amount
a)
stylePrice Map CommoditySymbol AmountStyle
styles (Just (TotalPrice Amount
a)) = forall a. a -> Maybe a
Just (Amount -> AmountPrice
TotalPrice forall a b. (a -> b) -> a -> b
$ Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map CommoditySymbol AmountStyle
styles Amount
a)
stylePrice Map CommoditySymbol AmountStyle
_ Maybe AmountPrice
_ = forall a. Maybe a
Nothing
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 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 -> [Char]
showAmount = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
noColour
showAmountB :: AmountDisplayOpts -> Amount -> WideBuilder
showAmountB :: AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
_ Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
"AUTO"} = forall a. Monoid a => a
mempty
showAmountB AmountDisplayOpts
opts a :: Amount
a@Amount{astyle :: Amount -> AmountStyle
astyle=AmountStyle
style} =
WideBuilder -> WideBuilder
color forall a b. (a -> b) -> a -> b
$ case AmountStyle -> Side
ascommodityside AmountStyle
style of
Side
L -> forall {a}. Monoid a => a -> a -> a
showC (CommoditySymbol -> WideBuilder
wbFromText CommoditySymbol
c) WideBuilder
space forall a. Semigroup a => a -> a -> a
<> WideBuilder
quantity' forall a. Semigroup a => a -> a -> a
<> WideBuilder
price
Side
R -> WideBuilder
quantity' forall a. Semigroup a => a -> a -> a
<> forall {a}. Monoid a => a -> a -> a
showC WideBuilder
space (CommoditySymbol -> WideBuilder
wbFromText CommoditySymbol
c) forall a. Semigroup a => a -> a -> a
<> WideBuilder
price
where
quantity :: WideBuilder
quantity = Amount -> WideBuilder
showamountquantity forall a b. (a -> b) -> a -> b
$ if AmountDisplayOpts -> Bool
displayThousandsSep AmountDisplayOpts
opts then Amount
a else Amount
a{astyle :: AmountStyle
astyle=(Amount -> AmountStyle
astyle Amount
a){asdigitgroups :: Maybe DigitGroupStyle
asdigitgroups=forall a. Maybe a
Nothing}}
(WideBuilder
quantity',CommoditySymbol
c) | Amount -> Bool
amountLooksZero Amount
a Bool -> Bool -> Bool
&& Bool -> Bool
not (AmountDisplayOpts -> Bool
displayZeroCommodity AmountDisplayOpts
opts) = (Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
'0') Int
1,CommoditySymbol
"")
| Bool
otherwise = (WideBuilder
quantity, CommoditySymbol -> CommoditySymbol
quoteCommoditySymbolIfNeeded forall a b. (a -> b) -> a -> b
$ Amount -> CommoditySymbol
acommodity Amount
a)
space :: WideBuilder
space = if Bool -> Bool
not (CommoditySymbol -> Bool
T.null CommoditySymbol
c) Bool -> Bool -> Bool
&& AmountStyle -> Bool
ascommodityspaced AmountStyle
style then Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
' ') Int
1 else forall a. Monoid a => a
mempty
showC :: a -> a -> a
showC a
l a
r = if forall a. Maybe a -> Bool
isJust (AmountDisplayOpts -> Maybe [CommoditySymbol]
displayOrder AmountDisplayOpts
opts) then forall a. Monoid a => a
mempty else a
l forall a. Semigroup a => a -> a -> a
<> a
r
price :: WideBuilder
price = if AmountDisplayOpts -> Bool
displayPrice AmountDisplayOpts
opts then Amount -> WideBuilder
showAmountPrice Amount
a else forall a. Monoid a => a
mempty
color :: WideBuilder -> WideBuilder
color = if AmountDisplayOpts -> Bool
displayColour AmountDisplayOpts
opts Bool -> Bool -> Bool
&& Amount -> Bool
isNegativeAmount Amount
a then ColorIntensity -> Color -> WideBuilder -> WideBuilder
colorB ColorIntensity
Dull Color
Red else forall a. a -> a
id
cshowAmount :: Amount -> String
cshowAmount :: Amount -> [Char]
cshowAmount = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> Amount -> WideBuilder
showAmountB forall a. Default a => a
def{displayColour :: Bool
displayColour=Bool
True}
showAmountWithoutPrice :: Amount -> String
showAmountWithoutPrice :: Amount -> [Char]
showAmountWithoutPrice = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
noPrice
showAmountWithZeroCommodity :: Amount -> String
showAmountWithZeroCommodity :: Amount -> [Char]
showAmountWithZeroCommodity = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
noColour{displayZeroCommodity :: Bool
displayZeroCommodity=Bool
True}
showAmountDebug :: Amount -> String
showAmountDebug :: Amount -> [Char]
showAmountDebug Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
"AUTO"} = [Char]
"(missing)"
showAmountDebug Amount{Maybe AmountPrice
Quantity
CommoditySymbol
AmountStyle
aprice :: Maybe AmountPrice
astyle :: AmountStyle
aquantity :: Quantity
acommodity :: CommoditySymbol
astyle :: Amount -> AmountStyle
aprice :: Amount -> Maybe AmountPrice
acommodity :: Amount -> CommoditySymbol
aquantity :: Amount -> Quantity
..} =
[Char]
"Amount {acommodity=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show CommoditySymbol
acommodity forall a. [a] -> [a] -> [a]
++ [Char]
", aquantity=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Quantity
aquantity
forall a. [a] -> [a] -> [a]
++ [Char]
", aprice=" forall a. [a] -> [a] -> [a]
++ Maybe AmountPrice -> [Char]
showAmountPriceDebug Maybe AmountPrice
aprice forall a. [a] -> [a] -> [a]
++ [Char]
", astyle=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show AmountStyle
astyle forall a. [a] -> [a] -> [a]
++ [Char]
"}"
showamountquantity :: Amount -> WideBuilder
showamountquantity :: Amount -> WideBuilder
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}} =
WideBuilder
signB forall a. Semigroup a => a -> a -> a
<> WideBuilder
intB forall a. Semigroup a => a -> a -> a
<> WideBuilder
fracB
where
Decimal Word8
e Integer
n = Amount -> Quantity
amountRoundedQuantity Amount
amt
strN :: CommoditySymbol
strN = [Char] -> CommoditySymbol
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> [Char]
show forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a
abs Integer
n
len :: Int
len = CommoditySymbol -> Int
T.length CommoditySymbol
strN
intLen :: Int
intLen = forall a. Ord a => a -> a -> a
max Int
1 forall a b. (a -> b) -> a -> b
$ Int
len forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
e
dec :: Char
dec = forall a. a -> Maybe a -> a
fromMaybe Char
'.' Maybe Char
mdec
padded :: CommoditySymbol
padded = Int -> CommoditySymbol -> CommoditySymbol
T.replicate (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
e forall a. Num a => a -> a -> a
+ Int
1 forall a. Num a => a -> a -> a
- Int
len) CommoditySymbol
"0" forall a. Semigroup a => a -> a -> a
<> CommoditySymbol
strN
(CommoditySymbol
intPart, CommoditySymbol
fracPart) = Int -> CommoditySymbol -> (CommoditySymbol, CommoditySymbol)
T.splitAt Int
intLen CommoditySymbol
padded
intB :: WideBuilder
intB = Maybe DigitGroupStyle -> Int -> CommoditySymbol -> WideBuilder
applyDigitGroupStyle Maybe DigitGroupStyle
mgrps Int
intLen forall a b. (a -> b) -> a -> b
$ if Word8
e forall a. Eq a => a -> a -> Bool
== Word8
0 then CommoditySymbol
strN else CommoditySymbol
intPart
signB :: WideBuilder
signB = if Integer
n forall a. Ord a => a -> a -> Bool
< Integer
0 then Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
'-') Int
1 else forall a. Monoid a => a
mempty
fracB :: WideBuilder
fracB = if Word8
e forall a. Ord a => a -> a -> Bool
> Word8
0 then Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
dec forall a. Semigroup a => a -> a -> a
<> CommoditySymbol -> Builder
TB.fromText CommoditySymbol
fracPart) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
e forall a. Num a => a -> a -> a
+ Int
1) else forall a. Monoid a => a
mempty
applyDigitGroupStyle :: Maybe DigitGroupStyle -> Int -> T.Text -> WideBuilder
applyDigitGroupStyle :: Maybe DigitGroupStyle -> Int -> CommoditySymbol -> WideBuilder
applyDigitGroupStyle Maybe DigitGroupStyle
Nothing Int
l CommoditySymbol
s = Builder -> Int -> WideBuilder
WideBuilder (CommoditySymbol -> Builder
TB.fromText CommoditySymbol
s) Int
l
applyDigitGroupStyle (Just (DigitGroups Char
_ [])) Int
l CommoditySymbol
s = Builder -> Int -> WideBuilder
WideBuilder (CommoditySymbol -> Builder
TB.fromText CommoditySymbol
s) Int
l
applyDigitGroupStyle (Just (DigitGroups Char
c (Word8
g0:[Word8]
gs0))) Int
l0 CommoditySymbol
s0 = forall {a}.
Integral a =>
NonEmpty a -> Integer -> CommoditySymbol -> WideBuilder
addseps (Word8
g0forall a. a -> [a] -> NonEmpty a
:|[Word8]
gs0) (forall a. Integral a => a -> Integer
toInteger Int
l0) CommoditySymbol
s0
where
addseps :: NonEmpty a -> Integer -> CommoditySymbol -> WideBuilder
addseps (a
g1:|[a]
gs1) Integer
l1 CommoditySymbol
s1
| Integer
l2 forall a. Ord a => a -> a -> Bool
> Integer
0 = NonEmpty a -> Integer -> CommoditySymbol -> WideBuilder
addseps NonEmpty a
gs2 Integer
l2 CommoditySymbol
rest forall a. Semigroup a => a -> a -> a
<> Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
c forall a. Semigroup a => a -> a -> a
<> CommoditySymbol -> Builder
TB.fromText CommoditySymbol
part) (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
g1 forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Builder -> Int -> WideBuilder
WideBuilder (CommoditySymbol -> Builder
TB.fromText CommoditySymbol
s1) (forall a. Num a => Integer -> a
fromInteger Integer
l1)
where
(CommoditySymbol
rest, CommoditySymbol
part) = Int -> CommoditySymbol -> (CommoditySymbol, CommoditySymbol)
T.splitAt (forall a. Num a => Integer -> a
fromInteger Integer
l2) CommoditySymbol
s1
gs2 :: NonEmpty a
gs2 = forall a. a -> Maybe a -> a
fromMaybe (a
g1forall a. a -> [a] -> NonEmpty a
:|[]) forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [a]
gs1
l2 :: Integer
l2 = Integer
l1 forall a. Num a => a -> a -> a
- forall a. Integral a => a -> Integer
toInteger a
g1
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' = forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault AmountStyle
s CommoditySymbol
c Map CommoditySymbol AmountStyle
styles
instance Semigroup MixedAmount where
<> :: MixedAmount -> MixedAmount -> MixedAmount
(<>) = MixedAmount -> MixedAmount -> MixedAmount
maPlus
sconcat :: NonEmpty MixedAmount -> MixedAmount
sconcat = forall (t :: * -> *). Foldable t => t MixedAmount -> MixedAmount
maSum
stimes :: forall b. Integral b => b -> MixedAmount -> MixedAmount
stimes b
n = Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmount (forall a b. (Integral a, Num b) => a -> b
fromIntegral b
n)
instance Monoid MixedAmount where
mempty :: MixedAmount
mempty = MixedAmount
nullmixedamt
mconcat :: [MixedAmount] -> MixedAmount
mconcat = forall (t :: * -> *). Foldable t => t MixedAmount -> MixedAmount
maSum
instance Num MixedAmount where
fromInteger :: Integer -> MixedAmount
fromInteger = Amount -> MixedAmount
mixedAmount forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
negate :: MixedAmount -> MixedAmount
negate = MixedAmount -> MixedAmount
maNegate
+ :: MixedAmount -> MixedAmount -> MixedAmount
(+) = MixedAmount -> MixedAmount -> MixedAmount
maPlus
* :: MixedAmount -> MixedAmount -> MixedAmount
(*) = forall a. HasCallStack => [Char] -> a
error [Char]
"error, mixed amounts do not support multiplication"
abs :: MixedAmount -> MixedAmount
abs = forall a. HasCallStack => [Char] -> a
error [Char]
"error, mixed amounts do not support abs"
signum :: MixedAmount -> MixedAmount
signum = forall a. HasCallStack => [Char] -> a
error [Char]
"error, mixed amounts do not support signum"
amountKey :: Amount -> MixedAmountKey
amountKey :: Amount -> MixedAmountKey
amountKey amt :: Amount
amt@Amount{acommodity :: Amount -> CommoditySymbol
acommodity=CommoditySymbol
c} = case Amount -> Maybe AmountPrice
aprice Amount
amt of
Maybe AmountPrice
Nothing -> CommoditySymbol -> MixedAmountKey
MixedAmountKeyNoPrice CommoditySymbol
c
Just (TotalPrice Amount
p) -> CommoditySymbol -> CommoditySymbol -> MixedAmountKey
MixedAmountKeyTotalPrice CommoditySymbol
c (Amount -> CommoditySymbol
acommodity Amount
p)
Just (UnitPrice Amount
p) -> CommoditySymbol -> CommoditySymbol -> Quantity -> MixedAmountKey
MixedAmountKeyUnitPrice CommoditySymbol
c (Amount -> CommoditySymbol
acommodity Amount
p) (Amount -> Quantity
aquantity Amount
p)
nullmixedamt :: MixedAmount
nullmixedamt :: MixedAmount
nullmixedamt = Map MixedAmountKey Amount -> MixedAmount
Mixed forall a. Monoid a => a
mempty
missingmixedamt :: MixedAmount
missingmixedamt :: MixedAmount
missingmixedamt = Amount -> MixedAmount
mixedAmount Amount
missingamt
isMissingMixedAmount :: MixedAmount -> Bool
isMissingMixedAmount :: MixedAmount -> Bool
isMissingMixedAmount (Mixed Map MixedAmountKey Amount
ma) = Amount -> MixedAmountKey
amountKey Amount
missingamt forall k a. Ord k => k -> Map k a -> Bool
`M.member` Map MixedAmountKey Amount
ma
mixed :: Foldable t => t Amount -> MixedAmount
mixed :: forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed = forall (t :: * -> *).
Foldable t =>
MixedAmount -> t Amount -> MixedAmount
maAddAmounts MixedAmount
nullmixedamt
mixedAmount :: Amount -> MixedAmount
mixedAmount :: Amount -> MixedAmount
mixedAmount Amount
a = Map MixedAmountKey Amount -> MixedAmount
Mixed forall a b. (a -> b) -> a -> b
$ forall k a. k -> a -> Map k a
M.singleton (Amount -> MixedAmountKey
amountKey Amount
a) Amount
a
maAddAmount :: MixedAmount -> Amount -> MixedAmount
maAddAmount :: MixedAmount -> Amount -> MixedAmount
maAddAmount (Mixed Map MixedAmountKey Amount
ma) Amount
a = Map MixedAmountKey Amount -> MixedAmount
Mixed forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith Amount -> Amount -> Amount
sumSimilarAmountsUsingFirstPrice (Amount -> MixedAmountKey
amountKey Amount
a) Amount
a Map MixedAmountKey Amount
ma
maAddAmounts :: Foldable t => MixedAmount -> t Amount -> MixedAmount
maAddAmounts :: forall (t :: * -> *).
Foldable t =>
MixedAmount -> t Amount -> MixedAmount
maAddAmounts = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' MixedAmount -> Amount -> MixedAmount
maAddAmount
maNegate :: MixedAmount -> MixedAmount
maNegate :: MixedAmount -> MixedAmount
maNegate = (Quantity -> Quantity) -> MixedAmount -> MixedAmount
transformMixedAmount forall a. Num a => a -> a
negate
maPlus :: MixedAmount -> MixedAmount -> MixedAmount
maPlus :: MixedAmount -> MixedAmount -> MixedAmount
maPlus (Mixed Map MixedAmountKey Amount
as) (Mixed Map MixedAmountKey Amount
bs) = Map MixedAmountKey Amount -> MixedAmount
Mixed forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
M.unionWith Amount -> Amount -> Amount
sumSimilarAmountsUsingFirstPrice Map MixedAmountKey Amount
as Map MixedAmountKey Amount
bs
maMinus :: MixedAmount -> MixedAmount -> MixedAmount
maMinus :: MixedAmount -> MixedAmount -> MixedAmount
maMinus MixedAmount
a = MixedAmount -> MixedAmount -> MixedAmount
maPlus MixedAmount
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
maNegate
maSum :: Foldable t => t MixedAmount -> MixedAmount
maSum :: forall (t :: * -> *). Foldable t => t MixedAmount -> MixedAmount
maSum = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' MixedAmount -> MixedAmount -> MixedAmount
maPlus MixedAmount
nullmixedamt
divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmount Quantity
n = (Quantity -> Quantity) -> MixedAmount -> MixedAmount
transformMixedAmount (forall a. Fractional a => a -> a -> a
/Quantity
n)
multiplyMixedAmount :: Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmount :: Quantity -> MixedAmount -> MixedAmount
multiplyMixedAmount Quantity
n = (Quantity -> Quantity) -> MixedAmount -> MixedAmount
transformMixedAmount (forall a. Num a => a -> a -> a
*Quantity
n)
transformMixedAmount :: (Quantity -> Quantity) -> MixedAmount -> MixedAmount
transformMixedAmount :: (Quantity -> Quantity) -> MixedAmount -> MixedAmount
transformMixedAmount Quantity -> Quantity
f = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe ((Quantity -> Quantity) -> Amount -> Amount
transformAmount Quantity -> Quantity
f)
averageMixedAmounts :: [MixedAmount] -> MixedAmount
averageMixedAmounts :: [MixedAmount] -> MixedAmount
averageMixedAmounts [MixedAmount]
as = forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (t :: * -> *) a. Foldable t => t a -> Int
length [MixedAmount]
as) Quantity -> MixedAmount -> MixedAmount
`divideMixedAmount` forall (t :: * -> *). Foldable t => t MixedAmount -> MixedAmount
maSum [MixedAmount]
as
isNegativeMixedAmount :: MixedAmount -> Maybe Bool
isNegativeMixedAmount :: MixedAmount -> Maybe Bool
isNegativeMixedAmount MixedAmount
m =
case MixedAmount -> [Amount]
amounts forall a b. (a -> b) -> a -> b
$ MixedAmount -> MixedAmount
mixedAmountStripPrices MixedAmount
m of
[] -> forall a. a -> Maybe a
Just Bool
False
[Amount
a] -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Amount -> Bool
isNegativeAmount Amount
a
[Amount]
as | forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Amount -> Bool
isNegativeAmount [Amount]
as -> forall a. a -> Maybe a
Just Bool
True
[Amount]
as | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Amount -> Bool
isNegativeAmount [Amount]
as) -> forall a. a -> Maybe a
Just Bool
False
[Amount]
_ -> forall a. Maybe a
Nothing
mixedAmountLooksZero :: MixedAmount -> Bool
mixedAmountLooksZero :: MixedAmount -> Bool
mixedAmountLooksZero (Mixed Map MixedAmountKey Amount
ma) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Amount -> Bool
amountLooksZero Map MixedAmountKey Amount
ma
mixedAmountIsZero :: MixedAmount -> Bool
mixedAmountIsZero :: MixedAmount -> Bool
mixedAmountIsZero (Mixed Map MixedAmountKey Amount
ma) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Amount -> Bool
amountIsZero Map MixedAmountKey Amount
ma
maIsZero :: MixedAmount -> Bool
maIsZero :: MixedAmount -> Bool
maIsZero = MixedAmount -> Bool
mixedAmountIsZero
maIsNonZero :: MixedAmount -> Bool
maIsNonZero :: MixedAmount -> Bool
maIsNonZero = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> Bool
mixedAmountIsZero
amounts :: MixedAmount -> [Amount]
amounts :: MixedAmount -> [Amount]
amounts (Mixed Map MixedAmountKey Amount
ma)
| MixedAmount -> Bool
isMissingMixedAmount (Map MixedAmountKey Amount -> MixedAmount
Mixed Map MixedAmountKey Amount
ma) = [Amount
missingamt]
| forall k a. Map k a -> Bool
M.null Map MixedAmountKey Amount
nonzeros = [Amount
newzero]
| Bool
otherwise = forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Map MixedAmountKey Amount
nonzeros
where
newzero :: Amount
newzero = forall a. a -> Maybe a -> a
fromMaybe Amount
nullamt forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommoditySymbol -> Bool
T.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> CommoditySymbol
acommodity) Map MixedAmountKey Amount
zeros
(Map MixedAmountKey Amount
zeros, Map MixedAmountKey Amount
nonzeros) = forall a k. (a -> Bool) -> Map k a -> (Map k a, Map k a)
M.partition Amount -> Bool
amountIsZero Map MixedAmountKey Amount
ma
amountsRaw :: MixedAmount -> [Amount]
amountsRaw :: MixedAmount -> [Amount]
amountsRaw (Mixed Map MixedAmountKey Amount
ma) = forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Map MixedAmountKey Amount
ma
maCommodities :: MixedAmount -> S.Set CommoditySymbol
maCommodities :: MixedAmount -> Set CommoditySymbol
maCommodities = forall a. Ord a => [a] -> Set a
S.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Amount -> CommoditySymbol
acommodity forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts'
where amounts' :: MixedAmount -> [Amount]
amounts' ma :: MixedAmount
ma@(Mixed Map MixedAmountKey Amount
m) = if forall k a. Map k a -> Bool
M.null Map MixedAmountKey Amount
m then [] else MixedAmount -> [Amount]
amounts MixedAmount
ma
unifyMixedAmount :: MixedAmount -> Maybe Amount
unifyMixedAmount :: MixedAmount -> Maybe Amount
unifyMixedAmount = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts
where
combine :: Amount -> Amount -> Maybe Amount
combine Amount
amt Amount
result
| Amount -> Bool
amountIsZero Amount
amt = forall a. a -> Maybe a
Just Amount
result
| Amount -> Bool
amountIsZero Amount
result = forall a. a -> Maybe a
Just Amount
amt
| Amount -> CommoditySymbol
acommodity Amount
amt forall a. Eq a => a -> a -> Bool
== Amount -> CommoditySymbol
acommodity Amount
result = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Amount
amt forall a. Num a => a -> a -> a
+ Amount
result
| Bool
otherwise = forall a. Maybe a
Nothing
sumSimilarAmountsUsingFirstPrice :: Amount -> Amount -> Amount
sumSimilarAmountsUsingFirstPrice :: Amount -> Amount -> Amount
sumSimilarAmountsUsingFirstPrice Amount
a Amount
b = (Amount
a forall a. Num a => a -> a -> a
+ Amount
b){aprice :: Maybe AmountPrice
aprice=Maybe AmountPrice
p}
where
p :: Maybe AmountPrice
p = case (Amount -> Maybe AmountPrice
aprice Amount
a, Amount -> Maybe AmountPrice
aprice Amount
b) of
(Just (TotalPrice Amount
ap), Just (TotalPrice Amount
bp))
-> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> AmountPrice
TotalPrice forall a b. (a -> b) -> a -> b
$ Amount
ap{aquantity :: Quantity
aquantity = Amount -> Quantity
aquantity Amount
ap forall a. Num a => a -> a -> a
+ Amount -> Quantity
aquantity Amount
bp }
(Maybe AmountPrice, Maybe AmountPrice)
_ -> Amount -> Maybe AmountPrice
aprice Amount
a
filterMixedAmount :: (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount :: (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount Amount -> Bool
p (Mixed Map MixedAmountKey Amount
ma) = Map MixedAmountKey Amount -> MixedAmount
Mixed forall a b. (a -> b) -> a -> b
$ forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter Amount -> Bool
p Map MixedAmountKey Amount
ma
filterMixedAmountByCommodity :: CommoditySymbol -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity :: CommoditySymbol -> MixedAmount -> MixedAmount
filterMixedAmountByCommodity CommoditySymbol
c (Mixed Map MixedAmountKey Amount
ma)
| forall k a. Map k a -> Bool
M.null Map MixedAmountKey Amount
ma' = Amount -> MixedAmount
mixedAmount Amount
nullamt{acommodity :: CommoditySymbol
acommodity=CommoditySymbol
c}
| Bool
otherwise = Map MixedAmountKey Amount -> MixedAmount
Mixed Map MixedAmountKey Amount
ma'
where ma' :: Map MixedAmountKey Amount
ma' = forall a k. (a -> Bool) -> Map k a -> Map k a
M.filter ((CommoditySymbol
cforall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> CommoditySymbol
acommodity) Map MixedAmountKey Amount
ma
mapMixedAmount :: (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount :: (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount Amount -> Amount
f (Mixed Map MixedAmountKey Amount
ma) = forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Amount -> Amount
f forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Map MixedAmountKey Amount
ma
mapMixedAmountUnsafe :: (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe :: (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe Amount -> Amount
f (Mixed Map MixedAmountKey Amount
ma) = Map MixedAmountKey Amount -> MixedAmount
Mixed forall a b. (a -> b) -> a -> b
$ forall a b k. (a -> b) -> Map k a -> Map k b
M.map Amount -> Amount
f Map MixedAmountKey Amount
ma
mixedAmountCost :: MixedAmount -> MixedAmount
mixedAmountCost :: MixedAmount -> MixedAmount
mixedAmountCost (Mixed Map MixedAmountKey Amount
ma) =
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\MixedAmount
m Amount
a -> MixedAmount -> Amount -> MixedAmount
maAddAmount MixedAmount
m (Amount -> Amount
amountCost Amount
a)) (Map MixedAmountKey Amount -> MixedAmount
Mixed Map MixedAmountKey Amount
noPrices) Map MixedAmountKey Amount
withPrices
where (Map MixedAmountKey Amount
noPrices, Map MixedAmountKey Amount
withPrices) = forall a k. (a -> Bool) -> Map k a -> (Map k a, Map k a)
M.partition (forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Maybe AmountPrice
aprice) Map MixedAmountKey Amount
ma
styleMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount Map CommoditySymbol AmountStyle
styles = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe (Map CommoditySymbol AmountStyle -> Amount -> Amount
styleAmount Map CommoditySymbol AmountStyle
styles)
mixedAmountUnstyled :: MixedAmount -> MixedAmount
mixedAmountUnstyled :: MixedAmount -> MixedAmount
mixedAmountUnstyled = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe Amount -> Amount
amountUnstyled
showMixedAmount :: MixedAmount -> String
showMixedAmount :: MixedAmount -> [Char]
showMixedAmount = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
noColour
showMixedAmountOneLine :: MixedAmount -> String
showMixedAmountOneLine :: MixedAmount -> [Char]
showMixedAmountOneLine = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
oneLine
showMixedAmountWithZeroCommodity :: MixedAmount -> String
showMixedAmountWithZeroCommodity :: MixedAmount -> [Char]
showMixedAmountWithZeroCommodity = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
noColour{displayZeroCommodity :: Bool
displayZeroCommodity=Bool
True}
showMixedAmountWithoutPrice :: Bool -> MixedAmount -> String
showMixedAmountWithoutPrice :: Bool -> MixedAmount -> [Char]
showMixedAmountWithoutPrice Bool
c = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
noPrice{displayColour :: Bool
displayColour=Bool
c}
showMixedAmountOneLineWithoutPrice :: Bool -> MixedAmount -> String
showMixedAmountOneLineWithoutPrice :: Bool -> MixedAmount -> [Char]
showMixedAmountOneLineWithoutPrice Bool
c = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
oneLine{displayColour :: Bool
displayColour=Bool
c}
showMixedAmountElided :: Int -> Bool -> MixedAmount -> String
showMixedAmountElided :: Int -> Bool -> MixedAmount -> [Char]
showMixedAmountElided Int
w Bool
c = WideBuilder -> [Char]
wbUnpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
oneLine{displayColour :: Bool
displayColour=Bool
c, displayMaxWidth :: Maybe Int
displayMaxWidth=forall a. a -> Maybe a
Just Int
w}
showMixedAmountDebug :: MixedAmount -> String
showMixedAmountDebug :: MixedAmount -> [Char]
showMixedAmountDebug MixedAmount
m | MixedAmount
m forall a. Eq a => a -> a -> Bool
== MixedAmount
missingmixedamt = [Char]
"(missing)"
| Bool
otherwise = [Char]
"Mixed [" forall a. [a] -> [a] -> [a]
++ [Char]
as forall a. [a] -> [a] -> [a]
++ [Char]
"]"
where as :: [Char]
as = forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"\n " forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Amount -> [Char]
showAmountDebug forall a b. (a -> b) -> a -> b
$ MixedAmount -> [Amount]
amounts MixedAmount
m
showMixedAmountB :: AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB :: AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountB AmountDisplayOpts
opts MixedAmount
ma
| AmountDisplayOpts -> Bool
displayOneLine AmountDisplayOpts
opts = AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountOneLineB AmountDisplayOpts
opts MixedAmount
ma
| Bool
otherwise = Builder -> Int -> WideBuilder
WideBuilder (WideBuilder -> Builder
wbBuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a]
intersperse WideBuilder
sep [WideBuilder]
ls) Int
width
where
ls :: [WideBuilder]
ls = AmountDisplayOpts -> MixedAmount -> [WideBuilder]
showMixedAmountLinesB AmountDisplayOpts
opts MixedAmount
ma
width :: Int
width = forall a. a -> [a] -> a
headDef Int
0 forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map WideBuilder -> Int
wbWidth [WideBuilder]
ls
sep :: WideBuilder
sep = Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
'\n') Int
0
showMixedAmountLinesB :: AmountDisplayOpts -> MixedAmount -> [WideBuilder]
showMixedAmountLinesB :: AmountDisplayOpts -> MixedAmount -> [WideBuilder]
showMixedAmountLinesB opts :: AmountDisplayOpts
opts@AmountDisplayOpts{displayMaxWidth :: AmountDisplayOpts -> Maybe Int
displayMaxWidth=Maybe Int
mmax,displayMinWidth :: AmountDisplayOpts -> Maybe Int
displayMinWidth=Maybe Int
mmin} MixedAmount
ma =
forall a b. (a -> b) -> [a] -> [b]
map (AmountDisplay -> WideBuilder
adBuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplay -> AmountDisplay
pad) [AmountDisplay]
elided
where
astrs :: [AmountDisplay]
astrs = Int -> (Amount -> WideBuilder) -> [Amount] -> [AmountDisplay]
amtDisplayList (WideBuilder -> Int
wbWidth WideBuilder
sep) (AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> [Amount]
orderedAmounts AmountDisplayOpts
opts forall a b. (a -> b) -> a -> b
$
if AmountDisplayOpts -> Bool
displayPrice AmountDisplayOpts
opts then MixedAmount
ma else MixedAmount -> MixedAmount
mixedAmountStripPrices MixedAmount
ma
sep :: WideBuilder
sep = Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
'\n') Int
0
width :: Int
width = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (WideBuilder -> Int
wbWidth forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplay -> WideBuilder
adBuilder) [AmountDisplay]
elided
pad :: AmountDisplay -> AmountDisplay
pad AmountDisplay
amt
| Just Int
mw <- Maybe Int
mmin =
let w :: Int
w = (forall a. Ord a => a -> a -> a
max Int
width Int
mw) forall a. Num a => a -> a -> a
- WideBuilder -> Int
wbWidth (AmountDisplay -> WideBuilder
adBuilder AmountDisplay
amt)
in AmountDisplay
amt{ adBuilder :: WideBuilder
adBuilder = Builder -> Int -> WideBuilder
WideBuilder (CommoditySymbol -> Builder
TB.fromText forall a b. (a -> b) -> a -> b
$ Int -> CommoditySymbol -> CommoditySymbol
T.replicate Int
w CommoditySymbol
" ") Int
w forall a. Semigroup a => a -> a -> a
<> AmountDisplay -> WideBuilder
adBuilder AmountDisplay
amt }
| Bool
otherwise = AmountDisplay
amt
elided :: [AmountDisplay]
elided = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Maybe Int
mmax [AmountDisplay]
astrs
elideTo :: Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Int
m [AmountDisplay]
xs = 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 (forall a. a -> Maybe a
Just Int
m) (WideBuilder -> Int
wbWidth WideBuilder
sep) (forall (t :: * -> *) a. Foldable t => t a -> Int
length [AmountDisplay]
long) forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> a
lastDef AmountDisplay
nullAmountDisplay [AmountDisplay]
short
([AmountDisplay]
short, [AmountDisplay]
long) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((Int
mforall a. Ord a => a -> a -> Bool
>=) forall b c a. (b -> c) -> (a -> b) -> a -> c
. WideBuilder -> Int
wbWidth forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplay -> WideBuilder
adBuilder) [AmountDisplay]
xs
showMixedAmountOneLineB :: AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountOneLineB :: AmountDisplayOpts -> MixedAmount -> WideBuilder
showMixedAmountOneLineB opts :: AmountDisplayOpts
opts@AmountDisplayOpts{displayMaxWidth :: AmountDisplayOpts -> Maybe Int
displayMaxWidth=Maybe Int
mmax,displayMinWidth :: AmountDisplayOpts -> Maybe Int
displayMinWidth=Maybe Int
mmin} MixedAmount
ma =
Builder -> Int -> WideBuilder
WideBuilder (WideBuilder -> Builder
wbBuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. WideBuilder -> WideBuilder
pad forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
intersperse WideBuilder
sep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map AmountDisplay -> WideBuilder
adBuilder [AmountDisplay]
elided)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => a -> a -> a
max Int
width forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mmin
where
width :: Int
width = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 AmountDisplay -> Int
adTotal forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe a
lastMay [AmountDisplay]
elided
astrs :: [AmountDisplay]
astrs = Int -> (Amount -> WideBuilder) -> [Amount] -> [AmountDisplay]
amtDisplayList (WideBuilder -> Int
wbWidth WideBuilder
sep) (AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
opts) forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> MixedAmount -> [Amount]
orderedAmounts AmountDisplayOpts
opts forall a b. (a -> b) -> a -> b
$
if AmountDisplayOpts -> Bool
displayPrice AmountDisplayOpts
opts then MixedAmount
ma else MixedAmount -> MixedAmount
mixedAmountStripPrices MixedAmount
ma
sep :: WideBuilder
sep = Builder -> Int -> WideBuilder
WideBuilder ([Char] -> Builder
TB.fromString [Char]
", ") Int
2
n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length [AmountDisplay]
astrs
pad :: WideBuilder -> WideBuilder
pad = (Builder -> Int -> WideBuilder
WideBuilder (CommoditySymbol -> Builder
TB.fromText forall a b. (a -> b) -> a -> b
$ Int -> CommoditySymbol -> CommoditySymbol
T.replicate Int
w CommoditySymbol
" ") Int
w forall a. Semigroup a => a -> a -> a
<>)
where w :: Int
w = forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mmin forall a. Num a => a -> a -> a
- Int
width
elided :: [AmountDisplay]
elided = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Maybe Int
mmax [AmountDisplay]
astrs
elideTo :: Int -> [AmountDisplay] -> [AmountDisplay]
elideTo Int
m = forall {a}. [(a, Maybe a)] -> [a]
addElide forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> [(AmountDisplay, Maybe AmountDisplay)]
-> [(AmountDisplay, Maybe AmountDisplay)]
takeFitting Int
m 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 = forall a. Maybe a -> [a] -> [a]
maybeAppend (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
last [(a, Maybe a)]
xs) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(a, Maybe a)]
xs
takeFitting :: Int
-> [(AmountDisplay, Maybe AmountDisplay)]
-> [(AmountDisplay, Maybe AmountDisplay)]
takeFitting Int
_ [] = []
takeFitting Int
m ((AmountDisplay, Maybe AmountDisplay)
x:[(AmountDisplay, Maybe AmountDisplay)]
xs) = (AmountDisplay, Maybe AmountDisplay)
x forall a. a -> [a] -> [a]
: forall {t :: * -> *} {a}. Foldable t => (a -> Bool) -> t a -> [a]
dropWhileRev (\(AmountDisplay
a,Maybe AmountDisplay
e) -> Int
m forall a. Ord a => a -> a -> Bool
< AmountDisplay -> Int
adTotal (forall a. a -> Maybe a -> a
fromMaybe AmountDisplay
a Maybe AmountDisplay
e)) [(AmountDisplay, Maybe AmountDisplay)]
xs
dropWhileRev :: (a -> Bool) -> t a -> [a]
dropWhileRev a -> Bool
p = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x [a]
xs -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs Bool -> Bool -> Bool
&& a -> Bool
p a
x then [] else a
xforall a. a -> [a] -> [a]
:[a]
xs) []
withElided :: [AmountDisplay] -> [(AmountDisplay, Maybe AmountDisplay)]
withElided = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Int
n2 AmountDisplay
amt -> (AmountDisplay
amt, Maybe Int -> Int -> Int -> AmountDisplay -> Maybe AmountDisplay
elisionDisplay forall a. Maybe a
Nothing (WideBuilder -> Int
wbWidth WideBuilder
sep) Int
n2 AmountDisplay
amt)) [Int
nforall a. Num a => a -> a -> a
-Int
1,Int
nforall a. Num a => a -> a -> a
-Int
2..Int
0]
orderedAmounts :: AmountDisplayOpts -> MixedAmount -> [Amount]
orderedAmounts :: AmountDisplayOpts -> MixedAmount -> [Amount]
orderedAmounts AmountDisplayOpts
dopts = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {t :: * -> *}.
Foldable t =>
CommoditySymbol -> t Amount -> Amount
pad) (AmountDisplayOpts -> Maybe [CommoditySymbol]
displayOrder AmountDisplayOpts
dopts) forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts
where
pad :: CommoditySymbol -> t Amount -> Amount
pad CommoditySymbol
c = forall a. a -> Maybe a -> a
fromMaybe (CommoditySymbol -> Amount -> Amount
amountWithCommodity CommoditySymbol
c Amount
nullamt) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((CommoditySymbol
cforall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> CommoditySymbol
acommodity)
data AmountDisplay = AmountDisplay
{ AmountDisplay -> WideBuilder
adBuilder :: !WideBuilder
, AmountDisplay -> Int
adTotal :: !Int
} deriving (Int -> AmountDisplay -> ShowS
[AmountDisplay] -> ShowS
AmountDisplay -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [AmountDisplay] -> ShowS
$cshowList :: [AmountDisplay] -> ShowS
show :: AmountDisplay -> [Char]
$cshow :: AmountDisplay -> [Char]
showsPrec :: Int -> AmountDisplay -> ShowS
$cshowsPrec :: Int -> AmountDisplay -> ShowS
Show)
nullAmountDisplay :: AmountDisplay
nullAmountDisplay :: AmountDisplay
nullAmountDisplay = WideBuilder -> Int -> AmountDisplay
AmountDisplay forall a. Monoid a => a
mempty Int
0
amtDisplayList :: Int -> (Amount -> WideBuilder) -> [Amount] -> [AmountDisplay]
amtDisplayList :: Int -> (Amount -> WideBuilder) -> [Amount] -> [AmountDisplay]
amtDisplayList Int
sep Amount -> WideBuilder
showamt = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL Int -> Amount -> (Int, AmountDisplay)
display (-Int
sep)
where
display :: Int -> Amount -> (Int, AmountDisplay)
display Int
tot Amount
amt = (Int
tot', WideBuilder -> Int -> AmountDisplay
AmountDisplay WideBuilder
str Int
tot')
where
str :: WideBuilder
str = Amount -> WideBuilder
showamt Amount
amt
tot' :: Int
tot' = Int
tot forall a. Num a => a -> a -> a
+ (WideBuilder -> Int
wbWidth WideBuilder
str) 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 forall a. Ord a => a -> a -> Bool
> Int
0 = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ WideBuilder -> Int -> AmountDisplay
AmountDisplay (Builder -> Int -> WideBuilder
WideBuilder (CommoditySymbol -> Builder
TB.fromText CommoditySymbol
str) Int
len) (AmountDisplay -> Int
adTotal AmountDisplay
lastAmt forall a. Num a => a -> a -> a
+ Int
len)
| Bool
otherwise = forall a. Maybe a
Nothing
where
fullString :: CommoditySymbol
fullString = [Char] -> CommoditySymbol
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show Int
n forall a. [a] -> [a] -> [a]
++ [Char]
" more.."
fullLength :: Int
fullLength = Int
sep forall a. Num a => a -> a -> a
+ Int
7 forall a. Num a => a -> a -> a
+ forall a. Integral a => Int -> a
numDigitsInt Int
n
str :: CommoditySymbol
str | Just Int
m <- Maybe Int
mmax, Int
fullLength forall a. Ord a => a -> a -> Bool
> Int
m = Int -> CommoditySymbol -> CommoditySymbol
T.take (Int
m forall a. Num a => a -> a -> a
- Int
2) CommoditySymbol
fullString forall a. Semigroup a => a -> a -> a
<> CommoditySymbol
".."
| Bool
otherwise = CommoditySymbol
fullString
len :: Int
len = case Maybe Int
mmax of Maybe Int
Nothing -> Int
fullLength
Just Int
m -> forall a. Ord a => a -> a -> a
max Int
2 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
min Int
m Int
fullLength
maybeAppend :: Maybe a -> [a] -> [a]
maybeAppend :: forall a. Maybe a -> [a] -> [a]
maybeAppend Maybe a
Nothing = forall a. a -> a
id
maybeAppend (Just a
a) = (forall a. [a] -> [a] -> [a]
++[a
a])
ltraceamount :: String -> MixedAmount -> MixedAmount
ltraceamount :: [Char] -> MixedAmount -> MixedAmount
ltraceamount [Char]
s MixedAmount
a = forall a. [Char] -> a -> a
trace ([Char]
s forall a. [a] -> [a] -> [a]
++ [Char]
": " forall a. [a] -> [a] -> [a]
++ MixedAmount -> [Char]
showMixedAmount MixedAmount
a) MixedAmount
a
mixedAmountSetPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
mixedAmountSetPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
mixedAmountSetPrecision AmountPrecision
p = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe (AmountPrecision -> Amount -> Amount
amountSetPrecision AmountPrecision
p)
mixedAmountSetFullPrecision :: MixedAmount -> MixedAmount
mixedAmountSetFullPrecision :: MixedAmount -> MixedAmount
mixedAmountSetFullPrecision = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe Amount -> Amount
amountSetFullPrecision
mixedAmountStripPrices :: MixedAmount -> MixedAmount
mixedAmountStripPrices :: MixedAmount -> MixedAmount
mixedAmountStripPrices (Mixed Map MixedAmountKey Amount
ma) =
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\MixedAmount
m Amount
a -> MixedAmount -> Amount -> MixedAmount
maAddAmount MixedAmount
m Amount
a{aprice :: Maybe AmountPrice
aprice=forall a. Maybe a
Nothing}) (Map MixedAmountKey Amount -> MixedAmount
Mixed Map MixedAmountKey Amount
noPrices) Map MixedAmountKey Amount
withPrices
where (Map MixedAmountKey Amount
noPrices, Map MixedAmountKey Amount
withPrices) = forall a k. (a -> Bool) -> Map k a -> (Map k a, Map k a)
M.partition (forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Maybe AmountPrice
aprice) Map MixedAmountKey Amount
ma
canonicaliseMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
canonicaliseMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
canonicaliseMixedAmount Map CommoditySymbol AmountStyle
styles = (Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmountUnsafe (Map CommoditySymbol AmountStyle -> Amount -> Amount
canonicaliseAmount Map CommoditySymbol AmountStyle
styles)
tests_Amount :: TestTree
tests_Amount = [Char] -> [TestTree] -> TestTree
testGroup [Char]
"Amount" [
[Char] -> [TestTree] -> TestTree
testGroup [Char]
"Amount" [
[Char] -> Assertion -> TestTree
testCase [Char]
"amountCost" forall a b. (a -> b) -> a -> b
$ do
Amount -> Amount
amountCost (Quantity -> Amount
eur Quantity
1) 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=forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
UnitPrice forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
2} 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=forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
TotalPrice forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
2} 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=forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
TotalPrice forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd (-Quantity
2)} forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd (-Quantity
2)
,[Char] -> Assertion -> TestTree
testCase [Char]
"amountLooksZero" forall a b. (a -> b) -> a -> b
$ do
HasCallStack => [Char] -> Bool -> Assertion
assertBool [Char]
"" forall a b. (a -> b) -> a -> b
$ Amount -> Bool
amountLooksZero Amount
nullamt
HasCallStack => [Char] -> Bool -> Assertion
assertBool [Char]
"" forall a b. (a -> b) -> a -> b
$ Amount -> Bool
amountLooksZero forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
usd Quantity
0
,[Char] -> Assertion -> TestTree
testCase [Char]
"negating amounts" forall a b. (a -> b) -> a -> b
$ do
forall a. Num a => a -> a
negate (Quantity -> Amount
usd Quantity
1) 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=forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Amount -> AmountPrice
UnitPrice forall a b. (a -> b) -> a -> b
$ Quantity -> Amount
eur Quantity
2} in forall a. Num a => a -> a
negate Amount
b forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Amount
b{aquantity :: Quantity
aquantity= -Quantity
1}
,[Char] -> Assertion -> TestTree
testCase [Char]
"adding amounts without prices" forall a b. (a -> b) -> a -> b
$ do
(Quantity -> Amount
usd Quantity
1.23 forall a. Num a => a -> a -> a
+ Quantity -> Amount
usd (-Quantity
1.23)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
0
(Quantity -> Amount
usd Quantity
1.23 forall a. Num a => a -> a -> a
+ Quantity -> Amount
usd (-Quantity
1.23)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
0
(Quantity -> Amount
usd (-Quantity
1.23) forall a. Num a => a -> a -> a
+ Quantity -> Amount
usd (-Quantity
1.23)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd (-Quantity
2.46)
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))] forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Quantity -> Amount
usd Quantity
0
AmountStyle -> AmountPrecision
asprecision (Amount -> AmountStyle
astyle forall a b. (a -> b) -> a -> b
$ 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]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Word8 -> AmountPrecision
Precision Word8
3
AmountStyle -> AmountPrecision
asprecision (Amount -> AmountStyle
astyle forall a b. (a -> b) -> a -> b
$ 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]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Word8 -> AmountPrecision
Precision Word8
3
HasCallStack => [Char] -> Bool -> Assertion
assertBool [Char]
"" forall a b. (a -> b) -> a -> b
$ Amount -> Bool
amountLooksZero (Quantity -> Amount
usd Quantity
1.23 forall a. Num a => a -> a -> a
- Quantity -> Amount
eur Quantity
1.23)
,[Char] -> Assertion -> TestTree
testCase [Char]
"showAmount" forall a b. (a -> b) -> a -> b
$ do
Amount -> [Char]
showAmount (Quantity -> Amount
usd Quantity
0 forall a. Num a => a -> a -> a
+ Quantity -> Amount
gbp Quantity
0) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"0"
]
,[Char] -> [TestTree] -> TestTree
testGroup [Char]
"MixedAmount" [
[Char] -> Assertion -> TestTree
testCase [Char]
"comparing mixed amounts compares based on quantities" forall a b. (a -> b) -> a -> b
$ do
let usdpos :: MixedAmount
usdpos = forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed [Quantity -> Amount
usd Quantity
1]
usdneg :: MixedAmount
usdneg = forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed [Quantity -> Amount
usd (-Quantity
1)]
eurneg :: MixedAmount
eurneg = forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed [Quantity -> Amount
eur (-Quantity
12)]
forall a. Ord a => a -> a -> Ordering
compare MixedAmount
usdneg MixedAmount
usdpos forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Ordering
LT
forall a. Ord a => a -> a -> Ordering
compare MixedAmount
eurneg MixedAmount
usdpos forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Ordering
LT
,[Char] -> Assertion -> TestTree
testCase [Char]
"adding mixed amounts to zero, the commodity and amount style are preserved" forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *). Foldable t => t MixedAmount -> MixedAmount
maSum (forall a b. (a -> b) -> [a] -> [b]
map Amount -> MixedAmount
mixedAmount
[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)
])
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Amount -> MixedAmount
mixedAmount (Quantity -> Amount
usd Quantity
0 Amount -> AmountPrecision -> Amount
`withPrecision` Word8 -> AmountPrecision
Precision Word8
3)
,[Char] -> Assertion -> TestTree
testCase [Char]
"adding mixed amounts with total prices" forall a b. (a -> b) -> a -> b
$ do
forall (t :: * -> *). Foldable t => t MixedAmount -> MixedAmount
maSum (forall a b. (a -> b) -> [a] -> [b]
map Amount -> MixedAmount
mixedAmount
[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
])
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Amount -> MixedAmount
mixedAmount (Quantity -> Amount
usd (-Quantity
1) Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
2)
,[Char] -> Assertion -> TestTree
testCase [Char]
"showMixedAmount" forall a b. (a -> b) -> a -> b
$ do
MixedAmount -> [Char]
showMixedAmount (Amount -> MixedAmount
mixedAmount (Quantity -> Amount
usd Quantity
1)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"$1.00"
MixedAmount -> [Char]
showMixedAmount (Amount -> MixedAmount
mixedAmount (Quantity -> Amount
usd Quantity
1 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
2)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"$1.00 @ €2.00"
MixedAmount -> [Char]
showMixedAmount (Amount -> MixedAmount
mixedAmount (Quantity -> Amount
usd Quantity
0)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"0"
MixedAmount -> [Char]
showMixedAmount MixedAmount
nullmixedamt forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"0"
MixedAmount -> [Char]
showMixedAmount MixedAmount
missingmixedamt forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
""
,[Char] -> Assertion -> TestTree
testCase [Char]
"showMixedAmountWithoutPrice" 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 -> [Char]
showMixedAmountWithoutPrice Bool
False (Amount -> MixedAmount
mixedAmount (Amount
a)) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"$1.00"
Bool -> MixedAmount -> [Char]
showMixedAmountWithoutPrice Bool
False (forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed [Amount
a, -Amount
a]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
"0"
,[Char] -> [TestTree] -> TestTree
testGroup [Char]
"amounts" [
[Char] -> Assertion -> TestTree
testCase [Char]
"a missing amount overrides any other amounts" forall a b. (a -> b) -> a -> b
$
MixedAmount -> [Amount]
amounts (forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed [Quantity -> Amount
usd Quantity
1, Amount
missingamt]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount
missingamt]
,[Char] -> Assertion -> TestTree
testCase [Char]
"unpriced same-commodity amounts are combined" forall a b. (a -> b) -> a -> b
$
MixedAmount -> [Amount]
amounts (forall (t :: * -> *). Foldable t => t Amount -> MixedAmount
mixed [Quantity -> Amount
usd Quantity
0, Quantity -> Amount
usd Quantity
2]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Quantity -> Amount
usd Quantity
2]
,[Char] -> Assertion -> TestTree
testCase [Char]
"amounts with same unit price are combined" forall a b. (a -> b) -> a -> b
$
MixedAmount -> [Amount]
amounts (forall (t :: * -> *). Foldable t => t 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]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Quantity -> Amount
usd Quantity
2 Amount -> Amount -> Amount
`at` Quantity -> Amount
eur Quantity
1]
,[Char] -> Assertion -> TestTree
testCase [Char]
"amounts with different unit prices are not combined" forall a b. (a -> b) -> a -> b
$
MixedAmount -> [Amount]
amounts (forall (t :: * -> *). Foldable t => t 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]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [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]
,[Char] -> Assertion -> TestTree
testCase [Char]
"amounts with total prices are combined" forall a b. (a -> b) -> a -> b
$
MixedAmount -> [Amount]
amounts (forall (t :: * -> *). Foldable t => t 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]) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Quantity -> Amount
usd Quantity
2 Amount -> Amount -> Amount
@@ Quantity -> Amount
eur Quantity
2]
]
,[Char] -> Assertion -> TestTree
testCase [Char]
"mixedAmountStripPrices" forall a b. (a -> b) -> a -> b
$ do
MixedAmount -> [Amount]
amounts (MixedAmount -> MixedAmount
mixedAmountStripPrices MixedAmount
nullmixedamt) forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Amount
nullamt]
HasCallStack => [Char] -> Bool -> Assertion
assertBool [Char]
"" forall a b. (a -> b) -> a -> b
$ MixedAmount -> Bool
mixedAmountLooksZero forall a b. (a -> b) -> a -> b
$ MixedAmount -> MixedAmount
mixedAmountStripPrices
(forall (t :: * -> *). Foldable t => t 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)
])
]
]