<commit_before>{-# LANGUAGE ScopedTypeVariables, FlexibleContexts, OverlappingInstances, GADTs #-}
{-# OPTIONS_GHC -F -pgmF MonadLoc #-}

module YaLedger.Reports.Postings where

import Control.Monad
import Control.Monad.State
import Control.Monad.Exception
import Control.Monad.Exception.Base
import Control.Monad.Loc
import qualified Data.Map as M
import Data.List
import Data.Decimal
import Data.Dates

import YaLedger.Types
import YaLedger.Types.Reports
import YaLedger.Kernel
import YaLedger.Monad
import YaLedger.Strings
import YaLedger.Pretty
import YaLedger.Exceptions
import YaLedger.Reports.Common

postings :: Query -> AnyAccount -> Ledger NoExceptions ()
postings _ acc = do
    postings' acc
  `catchWithSrcLoc`
    (\l (e :: InternalError) -> handler l e)

postings' acc = do
  credit <- readIOList =<< creditPostings acc
  debit  <- readIOList =<< debitPostings  acc
  let postings = sort (map left credit ++ map right debit)
      res = unlines $ showPostings postings
  wrapIO $ putStrLn res

left :: Ext (Posting Decimal Credit) -> Ext (Either (Posting Decimal Credit) (Posting Decimal Debit))
left (Ext date pos attrs posting) = Ext date pos attrs (Left posting)

right :: Ext (Posting Decimal Debit) -> Ext (Either (Posting Decimal Credit) (Posting Decimal Debit))
right (Ext date pos attrs posting) = Ext date pos attrs (Right posting)

showPostings :: [Ext (Either (Posting Decimal Credit) (Posting Decimal Debit))] -> [String]
showPostings list =
    let dates = map (prettyPrint . getDate) list
        amounts = map getAmountS list

        getAmountS (Ext {getContent = (Left p)}) = show (getAmount p)
        getAmountS (Ext {getContent = (Right p)}) = '-': show (getAmount p)
    in  twoColumns "DATE" "AMOUNT" (alignMax ACenter dates) (alignMax ARight amounts)<commit_msg>Update Postings report.<commit_after>{-# LANGUAGE ScopedTypeVariables, FlexibleContexts, OverlappingInstances, GADTs #-}
{-# OPTIONS_GHC -F -pgmF MonadLoc #-}

module YaLedger.Reports.Postings where

import Control.Monad
import Control.Monad.State
import Control.Monad.Exception
import Control.Monad.Exception.Base
import Control.Monad.Loc
import qualified Data.Map as M
import Data.List
import Data.Decimal
import Data.Dates

import YaLedger.Types
import YaLedger.Types.Reports
import YaLedger.Kernel
import YaLedger.Monad
import YaLedger.Strings
import YaLedger.Pretty
import YaLedger.Exceptions
import YaLedger.Reports.Common

postings :: Query -> Maybe Path -> Ledger NoExceptions ()
postings _ mbPath = do
    postings' mbPath
  `catchWithSrcLoc`
    (\l (e :: InternalError) -> handler l e)
  `catchWithSrcLoc`
    (\l (e :: InvalidPath) -> handler l e)

postings' mbPath = do
  plan <- case mbPath of
            Nothing   -> gets lsAccountPlan
            Just path -> getAccountPlanItem path
  forL plan $ \path acc -> do
      credit <- readIOList =<< creditPostings acc
      debit  <- readIOList =<< debitPostings  acc
      let postings = sort (map left credit ++ map right debit)
          res = unlines $ showPostings postings
      wrapIO $ do
        putStrLn $ path ++ ":"
        putStrLn res

left :: Ext (Posting Decimal Credit) -> Ext (Either (Posting Decimal Credit) (Posting Decimal Debit))
left (Ext date pos attrs posting) = Ext date pos attrs (Left posting)

right :: Ext (Posting Decimal Debit) -> Ext (Either (Posting Decimal Credit) (Posting Decimal Debit))
right (Ext date pos attrs posting) = Ext date pos attrs (Right posting)

showPostings :: [Ext (Either (Posting Decimal Credit) (Posting Decimal Debit))] -> [String]
showPostings [] = ["No postings."]
showPostings list =
    let dates = map (prettyPrint . getDate) list
        amounts = map getAmountS list

        getAmountS (Ext {getContent = (Left p)}) = show (getAmount p)
        getAmountS (Ext {getContent = (Right p)}) = '-': show (getAmount p)
    in  twoColumns "DATE" "AMOUNT" (alignMax ACenter dates) (alignMax ARight amounts)
{'fraction_non_alphanumeric': 0.09727527374586198, 'fraction_numerical': 0.0, 'mean_word_length': 3.8734491315136474, 'pattern_counts': {'":': 1, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7529512', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1234, 'n_words': 415}