Debian Codegraph @

Hledger/Cli/Commands/Prices.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 {-# LANGUAGE NamedFieldPuns #-}
4
5 module Hledger.Cli.Commands.Prices (
6 pricesmode
7 ,prices
8 )
9 where
10
11 import Data.List
12 import Data.Text qualified as T
13 import Data.Text.IO qualified as T
14 import Hledger
15 import Hledger.Cli.CliOptions
16 import System.Console.CmdArgs.Explicit
17 import Data.Maybe (mapMaybe)
18 import Data.Function ((&))
19
20 pricesmode = hledgerCommandMode
21 $(embedFileRelative "Hledger/Cli/Commands/Prices.txt")
22 [flagNone ["show-reverse"] (setboolopt "show-reverse")
23 "also show the prices inferred by reversing known prices"
24 ]
25 cligeneralflagsgroups1
26 (hiddenflags ++
27 [flagNone ["costs"] (setboolopt "infer-market-prices") "deprecated, use --infer-market-prices instead"
28 ,flagNone ["inverted-costs"] (setboolopt "show-reverse") "deprecated, use --show-reverse instead"
29 ,flagNone ["infer-reverse-prices"] (setboolopt "show-reverse") "deprecated, use --show-reverse instead"
30 ])
31 ([], Just $ argsFlag "[QUERY]")
32
33 instance HasAmounts PriceDirective where
34 styleAmounts styles pd = pd{pdamount=styleAmounts styles $ pdamount pd}
35
36 -- List market prices.
37 prices opts j = do
38 let
39 styles = journalCommodityStyles j
40 q = _rsQuery $ reportspec_ opts
41
42 -- XXX duplicates logic in Hledger.Data.Valuation.makePriceGraph, keep synced
43
44 declaredprices =
45 -- dbg0 "declaredprices" $
46 jpricedirectives j
47
48 pricesfromcosts =
49 -- dbg0 "pricesfromcosts" $
50 concatMap postingPriceDirectivesFromCost $
51 journalPostings j
52
53 forwardprices =
54 -- dbg0 "forwardprices" $
55 if boolopt "infer-market-prices" (rawopts_ opts)
56 then declaredprices `mergePriceDirectives` pricesfromcosts
57 else declaredprices
58
59 reverseprices =
60 -- dbg0 "reverseprices" $
61 mapMaybe reversePriceDirective forwardprices
62
63 allprices =
64 -- dbg0 "allprices" $
65 if boolopt "show-reverse" (rawopts_ opts)
66 then forwardprices `mergePriceDirectives` reverseprices
67 else forwardprices
68
69 filteredprices =
70 -- dbg0 "filtered unsorted" $
71 filter (matchesPriceDirective q) allprices
72
73 mapM_ (T.putStrLn . showPriceDirective . styleAmounts styles) $
74 sortOn pddate filteredprices
75
76 -- XXX performance
77 -- | Append any new price directives (with different from commodity,
78 -- to commodity, or date) from the second list to the first.
79 -- (Does not remove redundant prices from the first; just avoids adding more.)
80 mergePriceDirectives :: [PriceDirective] -> [PriceDirective] -> [PriceDirective]
81 mergePriceDirectives pds1 pds2 =
82 pds1 ++ [ pd | pd <- pds2 , pdid pd `notElem` pds1ids ]
83 where
84 pds1ids = map pdid pds1
85 pdid PriceDirective{pddate,pdcommodity,pdamount} = (pddate, pdcommodity, acommodity pdamount)
86
87 showPriceDirective :: PriceDirective -> T.Text
88 showPriceDirective mp = T.unwords [
89 "P",
90 T.pack . show $ pddate mp,
91 quoteCommoditySymbolIfNeeded $ pdcommodity mp,
92 wbToText . showAmountB defaultFmt{displayZeroCommodity=True} $ pdamount mp
93 ]
94
95 -- | Convert a market price directive to a corresponding one in the
96 -- opposite direction, if possible. (A price directive with a zero
97 -- price can't be reversed.)
98 --
99 -- The price's display precision will be set to show all significant
100 -- decimal digits (or if they appear infinite, a smaller default precision (8).
101 -- This is visible eg in the prices command's output.
102 --
103 reversePriceDirective :: PriceDirective -> Maybe PriceDirective
104 reversePriceDirective pd@PriceDirective{pdcommodity=c, pdamount=a}
105 | amountIsZero a = Nothing
106 | otherwise = Just pd{pdcommodity=acommodity a, pdamount=a'}
107 where
108 lbl = lbl_ "reversePriceDirective"
109 a' =
110 amountSetFullPrecisionUpTo (Just defaultMaxPrecision) $
111 invertAmount a{acommodity=c}
112 & dbg9With (lbl "calculated reverse price".showAmount)
113 -- & dbg9With (lbl "precision of reverse price".show.amountDisplayPrecision)