ToolVjpPr0 Dup

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

let token = "";

let inTransactionCodes = [];


let currentIndex = 0;
let outTransactionCodes = [];
const username = "FRT";
const password = "P/3Z2zeK";
const tvanBaseUrl = "https://tvan.fpt.com.vn/ftvan-mgmt/prod-api/apis/tvan";
const fromDate = "2022-04-01";
const toDate = "2022-08-05";
const invoiceTransType = "203";
const wrongNoticeTransType = "300";

const getRandomArbitrary = (min, max) => Math.random() * (max - min) + min;

const getRandomInt = (min, max) => {


min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};

const getMessageFromXml = (tagName, input) => {


let parser, xmlDoc;
parser = new DOMParser();
xmlDoc = parser.parseFromString(input, "text/xml");

return xmlDoc
.getElementsByTagName(tagName)[0]
.childNodes[0].nodeValue.replace(/\r\n/g, "");
};

const getToken = () =>


new Promise((resolve, reject) => {
try {
let request = new XMLHttpRequest();

request.open(`POST`, `${tvanBaseUrl}/auth/login`, true);

request.setRequestHeader(
"Content-Type",
"application/json;charset=UTF-8"
);

request.responseType = "json";

request.onreadystatechange = function () {
try {
if (this.readyState != 4) {
return;
}

if (
this.status != 200 ||
this.response.result.token.trim().length < 1
) {
throw new Error("Không thể đăng nhập");
}

token = this.response.result.token;
resolve(this.response.result.token);
} catch (error) {
reject(error);
}
};

request.send(JSON.stringify({ username, password }));


} catch (error) {
reject(error);
}
});

const searchEInvoiceTransaction = (enterpriseCode, invoiceSymbol, invoiceNo) =>


new Promise((resolve, reject) => {
try {
console.log(
`Start getting transaction num ${currentIndex + 1} - ${enterpriseCode} - $
{invoiceSymbol} - ${invoiceNo}`
);

if (token.length < 1) {
getToken()
.then(() => searchEInvoiceTransaction(enterpriseCode, invoiceSymbol,
invoiceNo))
.then((status) => resolve(status))
.catch((reason) => {
throw new Error(reason);
});
return;
}

let request = new XMLHttpRequest();


let payload = {
businessStatus: null,
einvoiceType: "",
enterpriseCode,
fromDate,
invoiceNo,
invoiceSymbol,
pageIndex: 1,
pageSize: 10,
toDate,
transType: "",
userEinvoice: "",
};

request.open(
`POST`,
`${tvanBaseUrl}/portal/report/search-einvoice-transaction`,
true
);
request.setRequestHeader("authorization", token);
request.setRequestHeader(
"Content-Type",
"application/json;charset=UTF-8"
);

request.responseType = "json";
request.onreadystatechange = function () {
if (this.readyState != 4) {
return;
}

if (this.status == 401) {
getToken()
.then(() => searchEInvoiceTransaction(enterpriseCode, invoiceSymbol,
invoiceNo))
.then((status) => resolve(status))
.catch((reason) => {
throw new Error(reason);
});
return;
}

if (this.status != 200) {
reject("Truy vấn thất bại");
}

if (this.response.result.length < 1) {
resolve("Không tồn tại mã giao dịch TVAN");
return;
}

this.response.result.forEach(async (invoice, index) => {


switch (invoice.f_business_status) {
case 31:
let status = await getReceiverTaxHis(invoice.f_ref_id_tvan,
enterpriseCode, invoiceSymbol, invoiceNo);
if (status == true) {
outTransactionCodes.push({
enterpriseCode,
invoiceSymbol,
invoiceNo,
transactionCode: invoice.f_ref_id_tvan,
});
}
break;
}
});

currentIndex++;
if (currentIndex < inTransactionCodes.length) {
let timeout = getRandomArbitrary(3, 5) * 1000 - 2000;
console.log(
`Next request number ${currentIndex + 1} in ${timeout} sec!`
);
setTimeout(crawlDataStatus, timeout);
}
};

request.send(JSON.stringify(payload));
} catch (error) {
reject(error);
}
});

const getReceiverTaxHis = (transactionCode, enterpriseCode, invoiceSymbol,


invoiceNo) =>
new Promise((resolve, reject) => {
try {
if (token.length < 1) {
getToken()
.then(() => searchEInvoiceTransaction(enterpriseCode, invoiceSymbol,
invoiceNo))
.then((status) => resolve(status))
.catch((reason) => {
throw new Error(reason);
});
return;
}

let request = new XMLHttpRequest();


let payload = {
refId: transactionCode,
refIdTvan: transactionCode,
};

request.open(
`POST`,
`${tvanBaseUrl}/portal/report/get-receiver-tax-his`,
true
);
request.setRequestHeader("authorization", token);
request.setRequestHeader(
"Content-Type",
"application/json;charset=UTF-8"
);

request.responseType = "json";

request.onreadystatechange = function () {
if (this.readyState != 4) {
return;
}

if (this.status == 401) {
getToken()
.then(() => getReceiverTaxHis(transactionCode, enterpriseCode,
invoiceSymbol, invoiceNo))
.then((status) => resolve(status))
.catch((reason) => {
throw new Error(reason);
});
return;
}

if (this.status != 200) {
reject("Truy vấn thất bại");
}
if (this.response.result.length < 1) {
resolve(false);
return;
}

let sortedResult = this.response.result.sort((a, b) => {


return new Date(b.ngay_cqt_ky) - new Date(a.ngay_cqt_ky);
});

if (sortedResult[0].kq_ktdl_ma == null) {
resolve(false);
return;
}

switch (sortedResult[0].kq_ktdl_ma.toString().trim().toLowerCase()) {
case "2":
resolve(true);
break;
default:
resolve(false);
break;
}
};

request.send(JSON.stringify(payload));
} catch (error) {
reject(error);
}
});

const downloadResult = () => {


if ((outTransactionCodes || []).length > 0) {
let excelTable = `<!DOCTYPE html><html><body><table><!--<thead><tr><th>Mã giao
dịch</th><th>Trạng thái</th></tr></thead>--><tbody>`;
outTransactionCodes.forEach((value) => {
excelTable += `<tr>
<td>${value.enterpriseCode}</td>
<td>${value.invoiceSymbol}</td>
<td>${value.invoiceNo}</td>
<td>${value.transactionCode}</td>
</tr>`;
});
excelTable += `</tbody></table></body></html>`;
window.URL = window.URL || window.webkitURL;
let downloadATag = document.createElement("a");
downloadATag.setAttribute(
"href",
window.URL.createObjectURL(
new Blob([excelTable], { type: "text/html" })
)
);
downloadATag.setAttribute("download", "ketqua.html");
downloadATag.click();
}
}

const crawlDataStatus = () => {


let transactionCode = inTransactionCodes[currentIndex]
.replace(/\r\n/g, "")
.replace(/\\n/g, "")
.replace(/\\r/g, "")
.replace(/\s/g, "");
let inputData = transactionCode.split(";");
searchEInvoiceTransaction(inputData[0], inputData[1], inputData[2])
.catch((reason) => console.error(reason));
};
const reset = () => {
inTransactionCodes = [];
currentIndex = 0;
outTransactionCodes = [];
};

const inputDialog = () => {


reset();
// let inputTrans = prompt(
// 'Nhập danh sách mã giao dịch phân tách bằng dấu ","',
// ""
// );
let inputTrans = `0315275368;K22THC;03291945,
0315275368;K22THC;03594241,
0315275368;K22THC;03526244,
0315275368;K22THC;02905020,
0315275368;K22THC;03138905,
0311609355-004;K22TDA;00051592,
0315275368;K22THC;03487854,
0315275368;K22THC;03553621,
0315275368;K22THC;03239131,
0315275368;K22THC;03255369,
0315275368;K22THC;03665281,
0315275368;K22THC;03031696,
0315275368;K22THC;03067618,
0315275368;K22THC;00000119,
0315275368;K22THC;03291940,
0315275368;K22THC;03266990,
0315275368;K22THC;03397290,
0315275368;K22THC;03603701,
0315275368;K22THC;03268798,
0315275368;K22THC;03618200,
0315275368;K22THC;03359237,
0315275368;K22THC;03359244,
0315275368;K22THC;03359243,
0315275368;K22THC;02992950,
0315275368;K22THC;03168314,
0315275368;K22THC;02906034,
0311609355;K22THC;00122633,
0315275368;K22THC;03216565,
0315275368;K22THC;03196178,
0315275368;K22THC;02934475,
0315275368;K22THC;02807391,
0315275368;K22THC;03487955,
0315275368;K22THC;03214634,
0315275368;K22THC;03213226,
0315275368;K22THC;02850289,
0315275368;K22THC;03154054,
0315275368;K22THC;03596142,
0315275368-002;K22TTG;00014706,
0315275368;K22THC;03238623,
0311609355-004;K22TDA;00051555,
0315275368;K22THC;03759522,
0315275368;K22THC;03485380,
0315275368;K22THC;03227344,
0315275368;K22THC;02833844,
0315275368;K22THC;03734066,
0315275368;K22THC;03553522,
0315275368;K22THC;03179808,
0315275368;K22THC;03734273,
0315275368;K22THC;03104975,
0315275368;K22THC;02894281,
0315275368;K22THC;03079929,
0315275368;K22THC;03665179,
0315275368;K22THC;02906916,
0315275368;K22THC;01256386,
0315275368;K22THC;00000124,
0315275368;K22THC;00000130,
0315275368;K22THC;00000167,
0315275368;K22THC;03711959,
0315275368;K22THC;03485754,
0315275368;K22THC;03421185,
0315275368;K22THC;03709222,
0315275368;K22THC;03487909,
0315275368;K22THC;00464089,
0315275368;K22THC;00000128,
0315275368;K22THC;00000098,
0315275368;K22THC;00000088,
0315275368;K22THC;00000092,
0315275368;K22THC;00000132,
0315275368;K22THC;00000155,
0315275368;K22THC;00000145,
0315275368;K22THC;00000104,
0315275368;K22THC;00000139,
0315275368;K22THC;00000150,
0315275368;K22THC;00000148,
0315275368;K22THC;00000149,
0315275368;K22THC;03740899,
0315275368;K22THC;00000204,
0315275368;K22THC;00000172,
0315275368;K22THC;00000194,
0315275368;K22THC;00000109,
0315275368;K22THC;00000146,
0315275368;K22THC;00000196,
0315275368;K22THC;00000103,
0315275368;K22THC;00000153,
0315275368;K22THC;00000185,
0315275368;K22THC;00000091,
0315275368;K22THC;00016064,
0315275368;K22THC;00000136,
0315275368;K22THC;00000187,
0315275368;K22THC;02838495,
0315275368;K22THC;00000200,
0315275368;K22THC;00000118,
0315275368;K22THC;00000073,
0315275368;K22THC;00000110,
0315275368;K22THC;00000178,
0315275368;K22THC;00000138,
0315275368;K22THC;00000182,
0315275368;K22THC;00000180,
0315275368;K22THC;00000113,
0315275368;K22THC;00000179,
0315275368;K22THC;00000114,
0315275368;K22THC;00000102,
0315275368;K22THC;00000147,
0315275368;K22THC;00000198,
0315275368;K22THC;00000075,
0315275368;K22THC;02828423,
0315275368;K22THC;03231558,
0315275368;K22THC;03711961,
0315275368;K22THC;02912737,
0315275368;K22THC;03506854,
0315275368;K22THC;03037791,
0315275368;K22THC;02895213,
0315275368;K22THC;03711574,
0315275368;K22THC;03596434,
0315275368;K22THC;03665276,
0315275368;K22THC;03711785,
0315275368;K22THC;03359245,
0315275368;K22THC;03410505,
0315275368;K22THC;03646179,
0311609355-001;K22THN;00094788,
0315275368;K22THC;03553528,
0315275368;K22THC;03414194,
0315275368;K22THC;03506476,
0315275368;K22THC;02993002,
0315275368;K22THC;03267622,
0315275368;K22THC;03646591,
0315275368;K22THC;03400049,
0315275368;K22THC;03526204,
0315275368;K22THC;03031719,
0315275368;K22THC;03759091,
0315275368;K22THC;02972624,
0315275368;K22THC;03104930,
0315275368;K22THC;03392846,
0315275368;K22THC;03267609,
0315275368;K22THC;02878812,
0315275368;K22THC;03639332,
0315275368;K22THC;03575415,
0315275368;K22THC;03758207,
0315275368;K22THC;03506892,
0315275368;K22THC;03079870,
0311609355-001;K22THN;00094785,
0315275368;K22THC;03487956,
0315275368;K22THC;02893609,
0315275368;K22THC;02961515,
0315275368;K22THC;02833809,
0315275368;K22THC;02861357,
0315275368;K22THC;03005699,
0315275368;K22THC;03487961,
0315275368;K22THC;00795314,
0315275368;K22THC;02833783,
0315275368;K22THC;03760086,
0315275368;K22THC;02895510,
0315275368;K22THC;03443588,
0315275368;K22THC;02905139,
0311609355;K22THC;00123085,
0315275368;K22THC;03485340,
0315275368;K22THC;03596424,
0315275368;K22THC;03090770,
0315275368;K22THC;03060296,
0315275368;K22THC;03266733,
0315275368;K22THC;03758588,
0315275368;K22THC;03266137,
0315275368;K22THC;03639438,
0311609355;K22THC;00136138,
0315275368;K22THC;03102277,
0315275368;K22THC;03088728,
0311609355;K22THC;00146010,
0311609355-001;K22THN;00123045,
0315275368;K22THC;03759071,
0311609355-001;K22THN;00094787,
0315275368;K22THC;00000097,
0315275368;K22THC;00000077,
0315275368;K22THC;00000090,
0315275368;K22THC;00000066,
0315275368;K22THC;00000191,
0315275368;K22THC;00000142,
0315275368;K22THC;00000086,
0315275368;K22THC;00000101,
0315275368;K22THC;00000071,
0315275368;K22THC;00000099,
0315275368;K22THC;00000159,
0315275368;K22THC;00000201,
0315275368;K22THC;00000161,
0315275368;K22THC;00000126,
0315275368;K22THC;00000064,
0315275368;K22THC;00000165,
0315275368;K22THC;00000192,
0315275368;K22THC;00000125,
0315275368;K22THC;00000158,
0315275368;K22THC;00000202,
0315275368;K22THC;00000193,
`;
if (!(inputTrans != null && inputTrans.trim().length > 0)) {
alert("Không có đầu vào dữ liệu cần crawl");
return false;
}
inputTrans = inputTrans
.replace(/\s/g, "")
.replace(/\r\n/g, "")
.replace(/\\n/g, "")
.replace(/\\r/g, "");

inTransactionCodes = inputTrans
.split(",")
.filter(
(x) => x != null && x != undefined && x.length > 0 && x.trim().length > 0
);
if ((inTransactionCodes || []).length < 1) {
console.log("Không có dữ liệu đầu vào cần truy vấn");
return false;
}
};

const startCrawler = () => {


if (inputDialog() != false) {
crawlDataStatus();
}
};

You might also like