git ssb

0+

cel / ledger-scripts



Tree: f1521af2d73e96acaa4e96dfc60dbfc50373845a

Files: f1521af2d73e96acaa4e96dfc60dbfc50373845a / paypal2ledger

2921 bytesRaw
1#!/usr/bin/awk -f
2
3BEGIN {
4 FPAT = "([^,]*)|(\"[^\"]+\")"
5 txid = ""
6 prev_txid = ""
7 receipt_id = ""
8 tx_total = 0
9}
10
11$1 == "Date" { next }
12
13function unquote(str) {
14 if (match(str, /^".*"$/)) {
15 return substr(str, 2, RLENGTH - 2)
16 }
17 return str
18}
19
20function format_money(amount, currency) {
21 amount = sprintf("%0.2f", amount)
22 if(currency == "USD")
23 return "$" amount
24 else
25 return amount " " currency
26}
27
28function format_date(date) {
29# input: m/d/yyyy
30# output: yyyy/m/d
31 len = length(date)
32 year = substr(date, len-3)
33 return year "/" substr(date, 1, len-5)
34}
35
36function start_tx() {
37 payee = unquote($4)
38 payee_after = 0
39 if (tx_payee) {
40 payee2 = payee " ; " tx_payee
41 if (length(payee2) > 72) {
42 payee_after = 1
43 } else {
44 payee = payee2
45 }
46 }
47 printf "%s %s\n",
48 format_date(unquote($1)), payee
49 if (payee_after) {
50 printf " ; %s\n", tx_payee
51 }
52}
53
54function finish_tx() {
55 if (!tx_account) {
56 tx_account = "Expenses"
57 }
58
59 if (tx_starting) {
60 tx_total = -amt
61 }
62 if (tx_total) {
63 # extra expenses (e.g. shipping)
64 printf " %-32s %16s\n", tx_account,
65 format_money(tx_total, tx_currency)
66
67 # funds external source (e.g. bank account)
68 if (funds_source_amt) {
69 printf " %-32s %16s\n", funds_source,
70 format_money(funds_source_amt, currency)
71 tx_total = funds_source_amt
72 } else {
73 tx_total = 0
74 }
75 }
76 # amount paid by paypal balance
77 if (!tx_total) {
78 printf " %s\n", "Assets:PayPal"
79 }
80
81 tx_total = 0
82 funds_source_amt = 0
83 tx_account = ""
84 tx_payee = ""
85 tx_currency = ""
86}
87
88{
89 prev_txid = txid
90 prev_receipt_id = receipt_id
91 txid = unquote($13)
92}
93
94receipt_id = unquote($31) {
95 receipt_amt = unquote($36)
96}
97
98txid != prev_txid && receipt_id != prev_txid &&
99 txid != prev_receipt_id &&
100 (!receipt_id || receipt_id != prev_receipt_id) {
101 finish_tx()
102 printf "\n"
103}
104
105{
106 type = unquote($5)
107 status = unquote($6)
108 #print "TX", txid, "PTX", prev_txid, "R", receipt_id
109 currency = unquote($7)
110 amt = unquote($8)
111 gsub(/,/, "", amt)
112 tx_starting = 0
113}
114
115!tx_payee {
116 tx_payee = unquote($16)
117}
118
119!tx_currency {
120 tx_currency = currency
121}
122
123NR<3 || (txid != prev_txid && receipt_id != prev_txid &&
124 txid != prev_receipt_id &&
125 (!receipt_id || receipt_id != prev_receipt_id)) {
126 tx_starting = 1
127 tx_total -= amt ###
128 start_tx()
129}
130
131END {
132 finish_tx()
133}
134
135status != "Completed" {
136 printf " ; %s %s\n", txid, status
137 #next
138}
139
140type == "Shopping Cart Item" {
141 desc = unquote($16)
142 if (desc && desc != tx_payee) {
143 printf " ; %s\n", desc
144 }
145
146 printf " %-32s %16s\n", "Expenses:Shopping",
147 format_money(amt, currency)
148 amt *= -1
149}
150
151type ~ /Received/ {
152 tx_account = "Income"
153}
154
155type ~ /Withdraw Funds to a Bank Account|Credit to Credit Card/ {
156 tx_account = "Assets:" unquote($4)
157}
158
159{
160 tx_total += amt
161}
162
163type ~ /Donation Sent/ {
164 tx_account = "Expenses:Donations"
165}
166
167type ~ /Add Funds|Charge From Credit Card/ {
168 funds_source = "Assets:" unquote($4)
169 funds_source_amt = -amt
170}
171

Built with git-ssb-web