turnovers qry options coa = do
  let calcTotals = TShowTotals `elem` options
      calcSaldo  = TShowSaldo  `elem` options
      bqry = if TBothBalances `elem` options
                then BothBalances
                else if TLedgerBalances `elem` options
                      then Only LedgerBalance
                      else Only AvailableBalance
      rgroup = selectRateGroup (commonFlags options)
  tree <- mapTreeM (sumTurnovers (qEnd qry) rgroup calcTotals calcSaldo) (allTurnovers bqry calcTotals calcSaldo qry) coa
  let tree' = if TNoZeros `elem` options
                then filterLeafs noZeroTurns tree
                else tree
  oformat <- getOutputFormat
  let struct = case oformat of
                 OASCII _ -> showTreeStructure tree'
                 OCSV _  -> map (output . intercalate "/") (allPaths tree')
                 OHTML _ -> showTreeStructure tree'
      nodes = allNodes tree'
      credits = map trCredit nodes
      debits  = map trDebit  nodes
      inc     = map trIncSaldo nodes
      out     = map trOutSaldo nodes
      totals  = map trTotals nodes
      saldo   = map trSaldo  nodes
  oformat <- getOutputFormat
  let format = case oformat of
                 OASCII ASCII -> tableColumns ASCII
                 OCSV csv   -> tableColumns csv
                 OHTML html -> tableColumns html
  outputText $ unlinesText $ format $
      [([output "ACCOUNT"],     ALeft, struct),
       ([output "BALANCE C/F"], ARight, map prettyPrint inc),
       ([output "DEBIT"],       ARight, map prettyPrint debits),
       ([output "CREDIT"],      ARight, map prettyPrint credits),
       ([output "BALANCE B/D"], ARight,  map prettyPrint out)] ++
      (if calcTotals
         then [([output "TOTALS"], ARight, map (prettyPrint . fromJust) totals)]
         else []) ++
      (if calcSaldo
         then [([output "SALDO"],  ARight, map (prettyPrint . fromJust) saldo)]
         else [])
turnovers qry options coa = do
  let calcTotals = TShowTotals `elem` options
      calcSaldo  = TShowSaldo  `elem` options
      bqry = if TBothBalances `elem` options
                then BothBalances
                else if TLedgerBalances `elem` options
                      then Only LedgerBalance
                      else Only AvailableBalance
      rgroup = selectRateGroup (commonFlags options)
  tree <- mapTreeM (sumTurnovers (qEnd qry) rgroup calcTotals calcSaldo) (allTurnovers bqry calcTotals calcSaldo qry) coa
  let tree' = if TNoZeros `elem` options
                then filterLeafs noZeroTurns tree
                else tree
  oformat <- getOutputFormat
  let struct = case oformat of
                 OASCII _ -> showTreeStructure tree'
                 OCSV _  -> map (output . intercalate "/") (allPaths tree')
                 OHTML _ -> showTreeStructure tree'
      nodes = allNodes tree'
      credits = map trCredit nodes
      debits  = map trDebit  nodes
      inc     = map trIncSaldo nodes
      out     = map trOutSaldo nodes
      totals  = map trTotals nodes
      saldo   = map trSaldo  nodes
  oformat <- getOutputFormat
  let format = case oformat of
                 OASCII ASCII -> tableColumns ASCII
                 OCSV csv   -> tableColumns csv
                 OHTML html -> tableColumns html
  outputText $ unlinesText $ format $
      [([output "ACCOUNT"],     ALeft, struct),
       ([output "BALANCE C/F"], ARight, map prettyPrint inc),
       ([output "DEBIT"],       ARight, map prettyPrint debits),
       ([output "CREDIT"],      ARight, map prettyPrint credits),
       ([output "BALANCE B/D"], ARight,  map prettyPrint out)] ++
      (if calcTotals
         then [([output "TOTALS"], ARight, map (prettyPrint . fromJust) totals)]
         else []) ++
      (if calcSaldo
         then [([output "SALDO"],  ARight, map (prettyPrint . fromJust) saldo)]
         else [])
turnovers qry options coa = do
  let calcTotals = TShowTotals `elem` options
      calcSaldo  = TShowSaldo  `elem` options
      bqry = if TBothBalances `elem` options
                then BothBalances
                else if TLedgerBalances `elem` options
                      then Only LedgerBalance
                      else Only AvailableBalance
      rgroup = selectRateGroup (commonFlags options)
  tree <- mapTreeM (sumTurnovers (qEnd qry) rgroup calcTotals calcSaldo) (allTurnovers bqry calcTotals calcSaldo qry) coa
  let tree' = if TNoZeros `elem` options
                then filterLeafs noZeroTurns tree
                else tree
  oformat <- getOutputFormat
  let struct = case oformat of
                 OASCII _ -> showTreeStructure tree'
                 OCSV _  -> map (output . intercalate "/") (allPaths tree')
                 OHTML _ -> showTreeStructure tree'
      nodes = allNodes tree'
      credits = map trCredit nodes
      debits  = map trDebit  nodes
      inc     = map trIncSaldo nodes
      out     = map trOutSaldo nodes
      totals  = map trTotals nodes
      saldo   = map trSaldo  nodes
  oformat <- getOutputFormat
  let format = case oformat of
                 OASCII ASCII -> tableColumns ASCII
                 OCSV csv   -> tableColumns csv
                 OHTML html -> tableColumns html
  outputText $ unlinesText $ format $
      [([output "ACCOUNT"],     ALeft, struct),
       ([output "BALANCE C/F"], ARight, map prettyPrint inc),
       ([output "DEBIT"],       ARight, map prettyPrint debits),
       ([output "CREDIT"],      ARight, map prettyPrint credits),
       ([output "BALANCE B/D"], ARight,  map prettyPrint out)] ++
      (if calcTotals
         then [([output "TOTALS"], ARight, map (prettyPrint . fromJust) totals)]
         else []) ++
      (if calcSaldo
         then [([output "SALDO"],  ARight, map (prettyPrint . fromJust) saldo)]
         else [])