summaryrefslogtreecommitdiff
path: root/sort-and-sanitize-amex-export.hs
blob: f0be45cc3d8b62928bf8b1584ce19d1ce2b107bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Text.CSV
import Text.Printf (printf)

import Data.List (sort, isPrefixOf, groupBy)

import System.Environment (getArgs)

normAmount :: Field -> Field
normAmount amt | amt == "" = ""
    | otherwise = printf "$%.2f" (read amt :: Double)

compressWhitespace :: String -> String
compressWhitespace x = map head $ groupSpaces x
	where groupSpaces "" = [""]
	      groupSpaces x = groupBy (\x y -> x==' ' && y==' ') x

mungePayment :: Field -> Field
mungePayment x | x == "PAYMENT RECEIVED - THANK YOU" = "Assets:Bank:SFLC:Checking:FirstRepublic"
    | isPrefixOf "FEDEX# " x = "Expenses:Office & Postage:FedEx"
    | isPrefixOf "JUNCTION NETWORKS " x = "Expenses:Telephony:VoIP:Junction"
    | isPrefixOf "T-MOBILE" x = "Expenses:Telephony:Mobile:T-Mobile"
    | isPrefixOf "SPEAKEASY SEATTLE WA" x = "Expenses:Internet:Speakeasy"
    | isPrefixOf "TELIAX " x = "Expenses:Telephony:VoIP:Teliax"
    | isPrefixOf "VERIZON" x = "Expenses:Telephony:PSTN:Verizon"
    | isPrefixOf "VOICEPULSE " x = "Expenses:Telephony:VoIP:VoicePulse"
    | otherwise = "Expenses:Raw:" ++ compressWhitespace x

newDesc :: String -> String -> String
newDesc merch refno = compressWhitespace merch ++ parenRef refno
    where
        parenRef r | "Reference: " `isPrefixOf` r = concat [" (",drop 11 r,")"]
                   | otherwise = concat [" (",r,")"]
massageMerchant :: [Field] -> [Field]
massageMerchant [a,b,c,d,e] = [a,newDesc d b,normAmount c,"",e]
massageMerchant xs = xs

sanitySort :: [Record] -> [Record]
sanitySort ts = map (massageMerchant) (sort ts)

main = do
        args <- getArgs
	result <- parseCSVFromFile (head args)
	case result of
		Left e -> putStrLn ("Parse fail: " ++ show e)
		Right ts -> putStrLn $ printCSV (sanitySort ts)