var lastNum = 1;
var sum = 0;
var regexp = /\B(?=(\d{3})+(?!\d))/g;
var chargeDateList=[];
var chargeWayList=[];
var cashList=[];
var text = "";
var years = [2017,2018,2019,2020,2021,2022,2023,2024,2025,2026];
for(var j=years.length-1; j>=0;j--){
$.ajax({
url: '/Cash/GetChargeList',
type: 'GET',
data: { Page: 1, StartDate: years[j]+'.01.01', EndDate: years[j]+'.12.31'},
dataType: 'html',
async: false,
success: function (data) {
var pageNum = $(data).find(".pagination__last")[0].getAttribute("onClick");
if(pageNum != null){
lastNum = pageNum.replace(/[^0-9]/g,"");
}
},
error: function (xhr, status, error) {
ajaxErrorHandler(xhr, status, error);
return;
}
});
for(var i=1;i<=lastNum;i++){
$.ajax({
url: '/Cash/GetChargeList',
type: 'GET',
data: { Page: i, StartDate: years[j]+'.01.01', EndDate: years[j]+'.12.31'},
dataType: 'html',
async: false,
success: function (data) {
$(data).find("td.list__price").each(function(){
var $cash = $(this)
cashList.push($cash.text().replace(/[^0-9]/g,""))
});
$(data).find("td.list__date").each(function(){
var $cash = $(this)
chargeDateList.push($cash.text())
});
$(data).find("td.list__way").each(function(){
var $cash = $(this)
chargeWayList.push($cash.text())
});
},
error: function (xhr, status, error) {
ajaxErrorHandler(xhr, status, error);
return;
}
});
}
}
(function(console) {
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.html'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
})(console)
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
//한글 처리를 해주기 위해 BOM 추가하기
const BOM = "\uFEFF";
csv = BOM + csv;
csvFile = new Blob([csv], { type: "text/csv" });
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
cashList.forEach(function(cash){
sum += Number(cash);
});
var csv = [];
var row = [];
row.push(
"충전일자",
"충전금액",
"충전수단",
);
csv.push(row.join(","));
for(var i =cashList.length-1; i>=0;i--){
console.log(chargeDateList[i] + " " + cashList[i].replace(regexp, ',') + "원 " + chargeWayList[i]);
text = text + "\n"+chargeDateList[i] + " " + cashList[i].replace(regexp, ',') + "원 " + chargeWayList[i];
row = [];
row.push(
chargeDateList[i],
cashList[i],
chargeWayList[i],
);
csv.push(row.join(","));
}
sum = sum.toString().replace(regexp, ',');
text = text + "\n" +"총합 : " + sum;
console.log("현재까지 "+sum+"원 사용하셨습니다");
alert("현재까지 "+sum+"원 사용하셨습니다")
function downloadResult() {
downloadCSV(csv.join("\n"),"로아현질내역.csv")
}
const el = document.createElement('button');
el.id = 'downloadBtn';
el.style = 'width: 100%; height: 64px; text-align: center';
el.innerText = '사용내역 다운로드';
el.onclick = downloadResult;
document.body.prepend(el);