diff --git a/hledger-lib/Hledger/Write/Ods.hs b/hledger-lib/Hledger/Write/Ods.hs index 31d95ad6c..492bc0583 100644 --- a/hledger-lib/Hledger/Write/Ods.hs +++ b/hledger-lib/Hledger/Write/Ods.hs @@ -9,19 +9,28 @@ This is derived from Map Text ((Maybe Int, Maybe Int), [[Cell]]) -> TL.Text printFods encoding tables = - let fileOpen = + let fileOpen customStyles = map (map (\c -> case c of '\'' -> '"'; _ -> c)) $ printf "" (show encoding) : "" : " " : + customStyles ++ "" : [] @@ -130,7 +140,9 @@ printFods encoding tables = [] in TL.unlines $ map (TL.fromStrict . T.pack) $ - fileOpen ++ + fileOpen + (numberConfig + =<< Set.toList (numberStyles (foldMap (concat.snd) tables))) ++ tableConfig (fmap fst tables) ++ (Map.toAscList tables >>= \(name,(_,table)) -> tableOpen name ++ @@ -142,18 +154,65 @@ printFods encoding tables = tableClose) ++ fileClose + +numberStyles :: [Cell] -> Set (CommoditySymbol, AmountPrecision) +numberStyles = + Set.fromList . + mapMaybe (\cell -> + case cellType cell of + TypeAmount amt -> Just (acommodity amt, asprecision $ astyle amt) + _ -> Nothing) + +numberStyleName :: (CommoditySymbol, AmountPrecision) -> String +numberStyleName (comm, prec) = + printf "%s-%s" comm $ + case prec of + NaturalPrecision -> "natural" + Precision k -> show k + +numberConfig :: (CommoditySymbol, AmountPrecision) -> [String] +numberConfig (comm, prec) = + let precStr = + case prec of + NaturalPrecision -> "" + Precision k -> printf " number:decimal-places='%d'" k + name = numberStyleName (comm, prec) + in + printf " " name : + printf " " precStr : + printf " %s%s" + (if T.null comm then "" else " ") comm : + " " : + " " name name : + [] + + formatCell :: Cell -> [String] formatCell cell = - let style :: String + let style, valueType :: String style = case (cellStyle cell, cellType cell) of (Ordinary, TypeString) -> "" - (Ordinary, TypeAmount) -> " table:style-name='amount'" + (Ordinary, TypeMixedAmount) -> " table:style-name='amount'" + (Ordinary, TypeAmount amt) -> numberStyle amt (Foot, TypeString) -> " table:style-name='foot'" - (Foot, TypeAmount) -> " table:style-name='total-amount'" + (Foot, _) -> " table:style-name='total-amount'" (Head, _) -> " table:style-name='head'" + + numberStyle amt = + printf " table:style-name='%s'" + (numberStyleName (acommodity amt, asprecision $ astyle amt)) + valueType = + case cellType cell of + TypeAmount amt -> + printf + "office:value-type='float' office:value='%s'" + (show $ aquantity amt) + _ -> "office:value-type='string'" + in - printf "" style : + printf "" style valueType : printf "%s" (cellContent cell) : "" : [] diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index a78bdbb9c..8b3aeb6ba 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -585,14 +585,21 @@ balanceReportAsFods opts (items, total) = _ -> [[showName name, renderAmount ma]] showName = cell . accountNameDrop (drop_ opts) - renderAmount amt = - (cell $ wbToText $ showMixedAmountB bopts amt) { - Ods.cellType = Ods.TypeAmount + renderAmount mixedAmt = + (cell $ wbToText $ showMixedAmountB bopts mixedAmt) { + Ods.cellType = + case unifyMixedAmount mixedAmt of + Just amt -> + Ods.TypeAmount $ + if showcomm + then amt + else amt {acommodity = T.empty} + Nothing -> Ods.TypeMixedAmount } where bopts = machineFmt{displayCommodity=showcomm, displayCommodityOrder = commorder} (showcomm, commorder) - | layout_ opts == LayoutBare = (False, Just $ S.toList $ maCommodities amt) + | layout_ opts == LayoutBare = (False, Just $ S.toList $ maCommodities mixedAmt) | otherwise = (True, Nothing) -- Multi-column balance reports