and more code cleanups.
This commit is contained in:
		
							parent
							
								
									861e2beb1c
								
							
						
					
					
						commit
						a304ad3ca6
					
				| @ -15,33 +15,9 @@ import Ledger.Amount | |||||||
| 
 | 
 | ||||||
| instance Show Entry where show = showEntry | instance Show Entry where show = showEntry | ||||||
| 
 | 
 | ||||||
| showEntryDescription e =  |  | ||||||
|     (showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " " |  | ||||||
| showDate d = printf "%-10s" d |  | ||||||
| showDescription s = printf "%-20s" (elideRight 20 s) |  | ||||||
| 
 |  | ||||||
| isEntryBalanced :: Entry -> Bool |  | ||||||
| isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum |  | ||||||
|     where |  | ||||||
|       sum = sumRawTransactions realts |  | ||||||
|       realts = filter isReal ts |  | ||||||
| 
 |  | ||||||
| -- | Fill in a missing balance in this entry, if there is one,  |  | ||||||
| -- or raise an error if there is more than one. |  | ||||||
| autofillEntry :: Entry -> Entry |  | ||||||
| autofillEntry e@(Entry {etransactions=ts}) = e{etransactions=ts'} |  | ||||||
|     where ts' = fromMaybe  |  | ||||||
|                 (error $ "too many missing amounts in this entry, could not auto-balance:\n" ++ show e) |  | ||||||
|                 (autofillTransactions ts) |  | ||||||
| 
 |  | ||||||
| assertBalancedEntry :: Entry -> Entry |  | ||||||
| assertBalancedEntry e |  | ||||||
|     | isEntryBalanced e = e |  | ||||||
|     | otherwise = error $ "transactions don't balance in:\n" ++ show e |  | ||||||
| 
 |  | ||||||
| {-| | {-| | ||||||
| Helper for the print command which shows cleaned up ledger file | Show a ledger entry, formatted for the print command. ledger 2.x's | ||||||
| entries, something like: | standard format looks like this: | ||||||
| 
 | 
 | ||||||
| @ | @ | ||||||
| yyyy/mm/dd[ *][ CODE] description.........          [  ; comment...............] | yyyy/mm/dd[ *][ CODE] description.........          [  ; comment...............] | ||||||
| @ -75,6 +51,28 @@ showEntry e = | |||||||
|       showaccountname s = printf "%-34s" s |       showaccountname s = printf "%-34s" s | ||||||
|       showcomment s = if (length s) > 0 then "  ; "++s else "" |       showcomment s = if (length s) > 0 then "  ; "++s else "" | ||||||
| 
 | 
 | ||||||
|  | showDate = printf "%-10s" | ||||||
|  | 
 | ||||||
|  | -- | Raise an error if this entry is not balanced. | ||||||
|  | assertBalancedEntry :: Entry -> Entry | ||||||
|  | assertBalancedEntry e | ||||||
|  |     | isEntryBalanced e = e | ||||||
|  |     | otherwise = error $ "transactions don't balance in:\n" ++ show e | ||||||
|  | 
 | ||||||
|  | isEntryBalanced :: Entry -> Bool | ||||||
|  | isEntryBalanced (Entry {etransactions=ts}) = isZeroMixedAmount sum | ||||||
|  |     where | ||||||
|  |       sum = sumRawTransactions realts | ||||||
|  |       realts = filter isReal ts | ||||||
|  | 
 | ||||||
|  | -- | Fill in a missing balance in this entry, if there is one,  | ||||||
|  | -- or raise an error if there is more than one. | ||||||
|  | autofillEntry :: Entry -> Entry | ||||||
|  | autofillEntry e@(Entry {etransactions=ts}) = e{etransactions=ts'} | ||||||
|  |     where ts' = fromMaybe  | ||||||
|  |                 (error $ "too many missing amounts in this entry, could not auto-balance:\n" ++ show e) | ||||||
|  |                 (autofillTransactions ts) | ||||||
|  | 
 | ||||||
| -- modifier & periodic entries | -- modifier & periodic entries | ||||||
| 
 | 
 | ||||||
| instance Show ModifierEntry where  | instance Show ModifierEntry where  | ||||||
|  | |||||||
| @ -32,35 +32,22 @@ showRegisterReport opts args l = showtxns ts nulltxn nullamt | |||||||
|       matchtxn Transaction{account=a} = matchLedgerPatterns False apats a |       matchtxn Transaction{account=a} = matchLedgerPatterns False apats a | ||||||
|       apats = fst $ parseAccountDescriptionArgs args |       apats = fst $ parseAccountDescriptionArgs args | ||||||
| 
 | 
 | ||||||
|       -- show transactions, one per line, keeping a running balance |       -- show transactions, one per line, with a running balance | ||||||
|       showtxns [] _ _ = "" |       showtxns [] _ _ = "" | ||||||
|       showtxns (t@Transaction{amount=a}:ts) tprev bal = |       showtxns (t@Transaction{amount=a}:ts) tprev bal = | ||||||
|           (if isZeroAmount a then "" else this) ++ showtxns ts t bal' |           (if isZeroAmount a then "" else this) ++ showtxns ts t bal' | ||||||
|           where |           where | ||||||
|             this = if t `issame` tprev |             this = showtxn (t `issame` tprev) t bal' | ||||||
|                    then showTransactionWithoutDescription t bal' |  | ||||||
|                    else showTransactionWithDescription t bal' |  | ||||||
|             issame t1 t2 = entryno t1 == entryno t2 |             issame t1 t2 = entryno t1 == entryno t2 | ||||||
|             bal' = bal + amount t |             bal' = bal + amount t | ||||||
| 
 | 
 | ||||||
| showTransactionWithDescription :: Transaction -> Amount -> String |       -- show one transaction line, with or without the entry details | ||||||
| showTransactionWithDescription t b = |       showtxn :: Bool -> Transaction -> Amount -> String | ||||||
|     (showEntryDescription $ Entry (date t) False "" (description t) "" [] "")  |       showtxn omitdesc t b = entrydesc ++ txn ++ bal ++ "\n" | ||||||
|     ++ (showTransactionFormatted t) |           where | ||||||
|     ++ (showBalance b) |             entrydesc = if omitdesc then replicate 32 ' ' else printf "%s %s " date desc | ||||||
|     ++ "\n" |             date = showDate $ da | ||||||
| 
 |             desc = printf "%-20s" $ elideRight 20 de :: String | ||||||
| showTransactionWithoutDescription :: Transaction -> Amount -> String |             txn = showRawTransaction $ RawTransaction a amt "" tt | ||||||
| showTransactionWithoutDescription t b =  |             bal = printf " %12s" (showAmountOrZero b) | ||||||
|     (replicate 32 ' ')  |             Transaction{date=da,description=de,account=a,amount=amt,ttype=tt} = t | ||||||
|     ++ (showTransactionFormatted t)  |  | ||||||
|     ++ (showBalance b) |  | ||||||
|     ++ "\n" |  | ||||||
| 
 |  | ||||||
| showTransactionFormatted :: Transaction -> String |  | ||||||
| showTransactionFormatted (Transaction eno d desc a amt ttype) =  |  | ||||||
|     showRawTransaction $ RawTransaction a amt "" ttype |  | ||||||
| 
 |  | ||||||
| showBalance :: Amount -> String |  | ||||||
| showBalance b = printf " %12s" (showAmountOrZero b) |  | ||||||
| 
 |  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user