Skip to content

Commit

Permalink
Merge pull request #1764 from matiaspando/feature/generateCSV
Browse files Browse the repository at this point in the history
Generate CVS file from transaction history
  • Loading branch information
cmgustavo committed Nov 13, 2014
2 parents 27f26d4 + 225fefe commit 8036bb9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
73 changes: 73 additions & 0 deletions js/controllers/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ angular.module('copayApp.controllers').controller('HistoryController',

$rootScope.title = 'History';
$scope.loading = false;
$scope.generating = false;
$scope.lastShowed = false;

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



$scope.selectPage = function(page) {
$scope.currentPage = page;
$scope.update();
};



$scope.downloadHistory = function() {

if (window.cordova) {
log.info('Not available on mobile');
return;
}
var w = $rootScope.wallet;
if (!w) return;

$scope.generating = true;
w.getTransactionHistory(function(err, res) {
if (err) throw err;

if (!res) {
return;
}

var unit = w.settings.unitName;
var data = res.items;
var filename = "copay_history.csv";
var csvContent = "data:text/csv;charset=utf-8,";
csvContent += "Date,Amount(" + unit + "),Action,AddressTo,Comment\n";

data.forEach(function(it, index) {
var dataString = formatDate(it.minedTs || it.sentTs) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment);
csvContent += index < data.length ? dataString + "\n" : dataString;
});

var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", filename);

link.click();
$scope.generating = false;
$scope.$digest();

function formatDate(date) {
var dateObj = new Date(date);
if (!dateObj) {
log.error('Error formating a date');
return 'DateError'
}
if (!dateObj.toJSON()) {
return '';
}

return dateObj.toJSON().substring(0, 10);
}

function formatString(str) {
if (!str) return '';

if (str.indexOf('"') !== -1) {
//replace all
str = str.replace(new RegExp('"', 'g'), '\'');
}

//escaping commas
str = '\"' + str + '\"';

return str;
}
});

};



$scope.update = function() {
$scope.getTransactions();
};
Expand Down
13 changes: 11 additions & 2 deletions views/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ <h1 class="hide-for-large-up">{{$root.title}}</h1>
<div
class="row"
ng-if="blockchain_txs[0].txid">
<div class="large-12 columns">

<div class="large-12 columns"></div>
<div class="panel"
ng-repeat="btx in blockchain_txs | orderBy:'-ts'" ng-click="btx.showDetails = !btx.showDetails">
<div class="row size-14">
Expand Down Expand Up @@ -99,6 +98,16 @@ <h1 class="hide-for-large-up">{{$root.title}}</h1>
<a href="http://{{getShortNetworkName()}}.insight.is/tx/{{btx.txid}}" target="_blank" class="right"> More details <i class="icon-arrow-right2 vm"></i> </a>
</div>
</div>
</div>
<div ng-if="generating" class="right size-12">
<i class="fi-bitcoin-circle icon-rotate spinner"></i>
<span translate>Generating file...</span>
</div>
<div ng-if="!generating" class="right size-12">
<a class="text-gray" href="#!/history" ng-click="downloadHistory();">
<i class="fi-download"></i>
<span translate>Download</span>
</a>
</div>
<pagination page="currentPage" total-items="totalItems" items-per-page="itemsPerPage" on-select-page="selectPage(page)" max-size="10" />
</div>
Expand Down

0 comments on commit 8036bb9

Please sign in to comment.