Created
July 3, 2021 13:47
-
-
Save opdo/e4b13f696b81d78cd77224138d115abd to your computer and use it in GitHub Desktop.
Test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("https://ai.marketing/service/api/v1/user/robot/stat/sales/", { | |
"headers": { | |
"accept": "application/json, text/plain, */*", | |
"accept-language": "en-US,en;q=0.9,vi;q=0.8", | |
"content-type": "application/json;charset=UTF-8", | |
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Microsoft Edge\";v=\"91\", \"Chromium\";v=\"91\"", | |
"sec-ch-ua-mobile": "?0", | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-origin" | |
}, | |
"referrer": "https://ai.marketing/user/sales/", | |
"referrerPolicy": "strict-origin-when-cross-origin", | |
"body": "{\"page\":1,\"limit\":100000,\"userLang\":\"en\"}", | |
"method": "POST", | |
"mode": "cors", | |
"credentials": "include" | |
}).then(response => response.json()) | |
.then(data => Statistic(data)); | |
function Statistic(result) | |
{ | |
let data = {}; | |
for (let i = 0; i < result.data.list.length; i++) | |
{ | |
let element = result.data.list[i]; | |
let amount = element.sum; | |
let paymentDelay = result.data.servicesCashBack[String(element.traffic_source_id)].payment_delay; | |
let dateReturnMoney = (new Date(element.created_at)).addDays(paymentDelay).toDateString(); | |
if (data[dateReturnMoney] === undefined) data[dateReturnMoney] = 0; | |
data[dateReturnMoney] = (parseFloat(data[dateReturnMoney]) + parseFloat(amount)).toFixed(2); | |
} | |
var keyOrdered = Object.keys(data).sort(function(a, b) { | |
return (dateConverter(a) - dateConverter(b)); | |
}); | |
var excelData = [["Ngay", "So tien"]]; | |
for (let i = 0; i < keyOrdered.length; i++) { | |
let key = keyOrdered[i]; | |
excelData.push([key, data[key]]); | |
} | |
exportToCsv(excelData); | |
} | |
exportToCsv = function(Results) { | |
var CsvString = ""; | |
Results.forEach(function(RowItem, RowIndex) { | |
RowItem.forEach(function(ColItem, ColIndex) { | |
CsvString += ColItem + ','; | |
}); | |
CsvString += "\r\n"; | |
}); | |
CsvString = "data:application/csv," + encodeURIComponent(CsvString); | |
var x = document.createElement("A"); | |
x.setAttribute("href", CsvString ); | |
x.setAttribute("download","dutinh.csv"); | |
document.body.appendChild(x); | |
x.click(); | |
} | |
function dateConverter(date) { | |
let dateParts = date.split("/"); | |
let dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]); | |
return dateObject; | |
} | |
Date.prototype.addDays = function(days) { | |
var date = new Date(this.valueOf()); | |
date.setDate(date.getDate() + days); | |
return date; | |
} | |
Date.prototype.toDateString = function() { | |
var mm = this.getMonth() + 1; // getMonth() is zero-based | |
var dd = this.getDate(); | |
return [(dd>9 ? '' : '0') + dd, | |
(mm>9 ? '' : '0') + mm, | |
this.getFullYear() | |
].join('/'); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment