From 46ff73aa023884b7b288116542ee160032232b63 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 3 Jul 2015 15:35:34 -0300 Subject: [PATCH] Adds CSV history --- public/views/walletHome.html | 8 +++- src/js/controllers/index.js | 70 ++++++++++++++++++++++++++++++++ src/js/controllers/walletHome.js | 2 +- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/public/views/walletHome.html b/public/views/walletHome.html index 98f86ab02..00daaa1b9 100644 --- a/public/views/walletHome.html +++ b/public/views/walletHome.html @@ -445,7 +445,7 @@ -
+
@@ -495,6 +495,12 @@
+
+ + + Download CSV file + +
diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index dd742aba4..ff21b8300 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -535,6 +535,76 @@ angular.module('copayApp.controllers').controller('indexController', function($r } }; + this.csvHistory = function() { + + 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; + } + + if (isCordova) { + log.info('Not available on mobile'); + return; + } + var fc = profileService.focusedClient; + if (!fc.isComplete()) return; + var self = this; + $log.debug('Generating CSV from History'); + self.setOngoingProcess('generatingCSV', true); + $timeout(function() { + fc.getTxHistory({ + skip: 0, + limit: 1000000000 + }, function(err, txs) { + self.setOngoingProcess('generatingCSV', false); + if (err) { + $log.debug('TxHistory ERROR:', err); + } else { + $log.debug('Wallet Transaction History:', txs); + + self.satToUnit = 1 / self.unitToSatoshi; + var data = txs; + var filename = "copay_history.csv"; + var csvContent = "data:text/csv;charset=utf-8,"; + csvContent += "Date,Amount (" + self.unitName + "),Action,AddressTo,Comment\n"; + + data.forEach(function(it, index) { + var dataString = formatDate(it.time * 1000) + ',' + strip(it.amount * self.satToUnit) + ',' + it.action + ',' + formatString(it.addressTo) + ',' + formatString(it.message); + 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(); + } + $rootScope.$apply(); + }); + }); + }; self.clientError = function(err) { if (isCordova) { diff --git a/src/js/controllers/walletHome.js b/src/js/controllers/walletHome.js index ee93614b6..ff167ebbe 100644 --- a/src/js/controllers/walletHome.js +++ b/src/js/controllers/walletHome.js @@ -1054,7 +1054,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi if (confirmed) self._doSendAll(amount); }); - } + }; /* Start setup */