Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ФТ-202, Афанасьев, Исаков #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 96 additions & 104 deletions static/focus.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,126 @@
const API = {
organizationList: "/orgsList",
analytics: "/api3/analytics",
orgReqs: "/api3/reqBase",
buhForms: "/api3/buh",
organizationList: "/orgsList",
analytics: "/api3/analytics",
orgReqs: "/api3/reqBase",
buhForms: "/api3/buh",
};

function run() {
sendRequest(API.organizationList, (orgOgrns) => {
const ogrns = orgOgrns.join(",");
sendRequest(`${API.orgReqs}?ogrn=${ogrns}`, (requisites) => {
const orgsMap = reqsToMap(requisites);
sendRequest(`${API.analytics}?ogrn=${ogrns}`, (analytics) => {
addInOrgsMap(orgsMap, analytics, "analytics");
sendRequest(`${API.buhForms}?ogrn=${ogrns}`, (buh) => {
addInOrgsMap(orgsMap, buh, "buhForms");
render(orgsMap, orgOgrns);
});
});
});
});
async function run() {
const orgOgrns = await sendRequest(API.organizationList);
const ogrns = { ogrn: orgOgrns.join(",") };
const [requisites, analytics, buh] = await Promise.all([
sendRequest(API.orgReqs, ogrns),
sendRequest(API.analytics, ogrns),
sendRequest(API.buhForms, ogrns),
]);
const orgsMap = reqsToMap(requisites);
addInOrgsMap(orgsMap, analytics, "analytics");
addInOrgsMap(orgsMap, buh, "buhForms");
render(orgsMap, orgOgrns);
}

run();

function sendRequest(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);

xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
callback(JSON.parse(xhr.response));
}
}
};

xhr.send();
function sendRequest(url, query = {}) {
return fetch(url + "?" + new URLSearchParams(query), { method: "GET" }).then(
(x) => {
if (x.ok) {
return x.json();
}
alert(x.status + " " + x.statusText);
}
);
}

function reqsToMap(requisites) {
return requisites.reduce((acc, item) => {
acc[item.ogrn] = item;
return acc;
}, {});
return requisites.reduce((acc, item) => {
acc[item.ogrn] = item;
return acc;
}, {});
}

function addInOrgsMap(orgsMap, additionalInfo, key) {
for (const item of additionalInfo) {
orgsMap[item.ogrn][key] = item[key];
}
for (const item of additionalInfo) {
orgsMap[item.ogrn][key] = item[key];
}
}

function render(organizationsInfo, organizationsOrder) {
const table = document.getElementById("organizations");
table.classList.remove("hide");
const table = document.getElementById("organizations");
table.classList.remove("hide");

const template = document.getElementById("orgTemplate");
const container = table.querySelector("tbody");
const template = document.getElementById("orgTemplate");
const container = table.querySelector("tbody");

organizationsOrder.forEach((item) => {
renderOrganization(organizationsInfo[item], template, container);
});
organizationsOrder.forEach((item) => {
renderOrganization(organizationsInfo[item], template, container);
});
}

function renderOrganization(orgInfo, template, container) {
const clone = document.importNode(template.content, true);
const name = clone.querySelector(".name");
const indebtedness = clone.querySelector(".indebtedness");
const money = clone.querySelector(".money");
const address = clone.querySelector(".address");

name.textContent =
(orgInfo.UL && orgInfo.UL.legalName && orgInfo.UL.legalName.short) ||
"";
indebtedness.textContent = formatMoney(orgInfo.analytics.s1002 || 0);

if (
orgInfo.buhForms &&
orgInfo.buhForms.length &&
orgInfo.buhForms[orgInfo.buhForms.length - 1] &&
orgInfo.buhForms[orgInfo.buhForms.length - 1].year === 2017
) {
money.textContent = formatMoney(
(orgInfo.buhForms[orgInfo.buhForms.length - 1].form2 &&
orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] &&
orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0]
.endValue) ||
0
);
} else {
money.textContent = "—";
}

const addressFromServer = orgInfo.UL.legalAddress.parsedAddressRF;
address.textContent = createAddress(addressFromServer);

container.appendChild(clone);
const clone = document.importNode(template.content, true);
const name = clone.querySelector(".name");
const indebtedness = clone.querySelector(".indebtedness");
const money = clone.querySelector(".money");
const address = clone.querySelector(".address");

name.textContent =
(orgInfo.UL && orgInfo.UL.legalName && orgInfo.UL.legalName.short) || "";
indebtedness.textContent = formatMoney(orgInfo.analytics.s1002 || 0);

if (
orgInfo.buhForms &&
orgInfo.buhForms.length &&
orgInfo.buhForms[orgInfo.buhForms.length - 1] &&
orgInfo.buhForms[orgInfo.buhForms.length - 1].year === 2017
) {
money.textContent = formatMoney(
(orgInfo.buhForms[orgInfo.buhForms.length - 1].form2 &&
orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] &&
orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0].endValue) ||
0
);
} else {
money.textContent = "—";
}

const addressFromServer = orgInfo.UL.legalAddress.parsedAddressRF;
address.textContent = createAddress(addressFromServer);

container.appendChild(clone);
}

function formatMoney(money) {
let formatted = money.toFixed(2);
formatted = formatted.replace(".", ",");
let formatted = money.toFixed(2);
formatted = formatted.replace(".", ",");

const rounded = money.toFixed(0);
const numLen = rounded.length;
for (let i = numLen - 3; i > 0; i -= 3) {
formatted = `${formatted.slice(0, i)} ${formatted.slice(i)}`;
}
const rounded = money.toFixed(0);
const numLen = rounded.length;
for (let i = numLen - 3; i > 0; i -= 3) {
formatted = `${formatted.slice(0, i)} ${formatted.slice(i)}`;
}

return `${formatted} ₽`;
return `${formatted} ₽`;
}

function createAddress(address) {
const addressToRender = [];
if (address.regionName) {
addressToRender.push(createAddressItem("regionName"));
}
if (address.city) {
addressToRender.push(createAddressItem("city"));
}
if (address.street) {
addressToRender.push(createAddressItem("street"));
}
if (address.house) {
addressToRender.push(createAddressItem("house"));
}

return addressToRender.join(", ");

function createAddressItem(key) {
return `${address[key].topoShortName}. ${address[key].topoValue}`;
}
const addressToRender = [];
if (address.regionName) {
addressToRender.push(createAddressItem("regionName"));
}
if (address.city) {
addressToRender.push(createAddressItem("city"));
}
if (address.street) {
addressToRender.push(createAddressItem("street"));
}
if (address.house) {
addressToRender.push(createAddressItem("house"));
}

return addressToRender.join(", ");

function createAddressItem(key) {
return `${address[key].topoShortName}. ${address[key].topoValue}`;
}
}