Skip to content

Commit 8036bb9

Browse files
committed
Merge pull request #1764 from matiaspando/feature/generateCSV
Generate CVS file from transaction history
2 parents 27f26d4 + 225fefe commit 8036bb9

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

js/controllers/history.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ angular.module('copayApp.controllers').controller('HistoryController',
99

1010
$rootScope.title = 'History';
1111
$scope.loading = false;
12+
$scope.generating = false;
1213
$scope.lastShowed = false;
1314

1415
$scope.currentPage = 1;
@@ -19,11 +20,83 @@ angular.module('copayApp.controllers').controller('HistoryController',
1920
$scope.alternativeCurrency = [];
2021

2122

23+
2224
$scope.selectPage = function(page) {
2325
$scope.currentPage = page;
2426
$scope.update();
2527
};
2628

29+
30+
31+
$scope.downloadHistory = function() {
32+
33+
if (window.cordova) {
34+
log.info('Not available on mobile');
35+
return;
36+
}
37+
var w = $rootScope.wallet;
38+
if (!w) return;
39+
40+
$scope.generating = true;
41+
w.getTransactionHistory(function(err, res) {
42+
if (err) throw err;
43+
44+
if (!res) {
45+
return;
46+
}
47+
48+
var unit = w.settings.unitName;
49+
var data = res.items;
50+
var filename = "copay_history.csv";
51+
var csvContent = "data:text/csv;charset=utf-8,";
52+
csvContent += "Date,Amount(" + unit + "),Action,AddressTo,Comment\n";
53+
54+
data.forEach(function(it, index) {
55+
var dataString = formatDate(it.minedTs || it.sentTs) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment);
56+
csvContent += index < data.length ? dataString + "\n" : dataString;
57+
});
58+
59+
var encodedUri = encodeURI(csvContent);
60+
var link = document.createElement("a");
61+
link.setAttribute("href", encodedUri);
62+
link.setAttribute("download", filename);
63+
64+
link.click();
65+
$scope.generating = false;
66+
$scope.$digest();
67+
68+
function formatDate(date) {
69+
var dateObj = new Date(date);
70+
if (!dateObj) {
71+
log.error('Error formating a date');
72+
return 'DateError'
73+
}
74+
if (!dateObj.toJSON()) {
75+
return '';
76+
}
77+
78+
return dateObj.toJSON().substring(0, 10);
79+
}
80+
81+
function formatString(str) {
82+
if (!str) return '';
83+
84+
if (str.indexOf('"') !== -1) {
85+
//replace all
86+
str = str.replace(new RegExp('"', 'g'), '\'');
87+
}
88+
89+
//escaping commas
90+
str = '\"' + str + '\"';
91+
92+
return str;
93+
}
94+
});
95+
96+
};
97+
98+
99+
27100
$scope.update = function() {
28101
$scope.getTransactions();
29102
};

views/history.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ <h1 class="hide-for-large-up">{{$root.title}}</h1>
1111
<div
1212
class="row"
1313
ng-if="blockchain_txs[0].txid">
14-
<div class="large-12 columns">
15-
14+
<div class="large-12 columns"></div>
1615
<div class="panel"
1716
ng-repeat="btx in blockchain_txs | orderBy:'-ts'" ng-click="btx.showDetails = !btx.showDetails">
1817
<div class="row size-14">
@@ -99,6 +98,16 @@ <h1 class="hide-for-large-up">{{$root.title}}</h1>
9998
<a href="http://{{getShortNetworkName()}}.insight.is/tx/{{btx.txid}}" target="_blank" class="right"> More details <i class="icon-arrow-right2 vm"></i> </a>
10099
</div>
101100
</div>
101+
</div>
102+
<div ng-if="generating" class="right size-12">
103+
<i class="fi-bitcoin-circle icon-rotate spinner"></i>
104+
<span translate>Generating file...</span>
105+
</div>
106+
<div ng-if="!generating" class="right size-12">
107+
<a class="text-gray" href="#!/history" ng-click="downloadHistory();">
108+
<i class="fi-download"></i>
109+
<span translate>Download</span>
110+
</a>
102111
</div>
103112
<pagination page="currentPage" total-items="totalItems" items-per-page="itemsPerPage" on-select-page="selectPage(page)" max-size="10" />
104113
</div>

0 commit comments

Comments
 (0)