Skip to content

Commit

Permalink
Add option to swap sign of amounts
Browse files Browse the repository at this point in the history
For CSV files where outflows are positive and inflows are negative,
let's add a checkbox for swapping the sign in the output.

This fixes halloffame#15.
  • Loading branch information
legoscia committed Mar 5, 2017
1 parent fc408df commit 605ad3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
7 changes: 5 additions & 2 deletions app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ angular.element(document).ready ->
Memo: 'Memo'
Outflow: 'Amount'
Inflow: 'Amount'
# Need to use an object here, not just a boolean.
# See http://stackoverflow.com/questions/18642371/checkbox-not-binding-to-scope-in-angularjs
$scope.sign = {swap: false}
$scope.data_object = new DataObject()

$scope.$watch 'data.source', (newValue, oldValue) ->
$scope.data_object.parse_csv(newValue) if newValue && newValue.length > 0

$scope.export = (limit) -> $scope.data_object.converted_json(limit, $scope.ynab_map)
$scope.csvString = -> $scope.data_object.converted_csv(null, $scope.ynab_map)
$scope.export = (limit) -> $scope.data_object.converted_json(limit, $scope.ynab_map, $scope.sign.swap)
$scope.csvString = -> $scope.data_object.converted_csv(null, $scope.ynab_map, $scope.sign.swap)
$scope.downloadFile = ->
a = document.createElement('a')
a.href = 'data:attachment/csv;base64,' + btoa(unescape(encodeURIComponent($scope.csvString())))
Expand Down
14 changes: 9 additions & 5 deletions data_object.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ class window.DataObject
# pass in false or null to do all.
# lookup: hash definition of YNAB column names to selected base column names. Lets us
# convert the uploaded CSV file into the columns that YNAB expects.
converted_json: (limit, lookup) ->
converted_json: (limit, lookup, swap_sign) ->
return nil if @base_json == null
value = []
if swap_sign
sign = -1
else
sign = 1

# TODO: You might want to check for errors. Papaparse has an errors field.
if @base_json.data
Expand All @@ -55,13 +59,13 @@ class window.DataObject
# the rest are just returned as they are.
switch col
when 'Outflow'
number = numberfy(cell)
number = numberfy(cell) * sign
if lookup['Outflow'] == lookup['Inflow']
tmp_row[col] = Math.abs(number) if number < 0
else
tmp_row[col] = number
when 'Inflow'
number = numberfy(cell)
number = numberfy(cell) * sign
if lookup['Outflow'] == lookup['Inflow']
tmp_row[col] = number if number > 0
else
Expand All @@ -71,11 +75,11 @@ class window.DataObject
value.push(tmp_row)
value

converted_csv: (limit, lookup) ->
converted_csv: (limit, lookup, swap_sign) ->
return nil if @base_json == null
# Papa.unparse string
string = ynab_cols.join(',') + "\n"
@.converted_json(limit, lookup).forEach (row) ->
@.converted_json(limit, lookup, swap_sign).forEach (row) ->
row_values = []
ynab_cols.forEach (col) ->
row_values.push row[col]
Expand Down
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<select ng-model="ynab_map[col]" ng-options="f for f in data_object.fields()"></select>
</th>
</tr>
<tr>
<td colspan='{{ynab_cols.length}}'>
<label><input type="checkbox" ng-model="sign.swap" />Swap positive / negative amounts</label>
</td>
</tr>
</thead>
<tbody>
<tr ng-repeat='row in export(10)'>
Expand Down

0 comments on commit 605ad3d

Please sign in to comment.