· 5 years ago · Sep 14, 2020, 07:20 AM
1const API = {
2 organizationList: '/orgsList',
3 analytics: '/api3/analytics',
4 orgReqs: '/api3/reqBase',
5 buhForms: '/api3/buh',
6};
7const handleError = (errCode) =>
8 alert(`Error: сервер прислал ошибку с кодом ${errCode} `);
9
10async function run() {
11 try {
12 const orgOgrns = await sendRequest(API.organizationList);
13 const ogrns = orgOgrns.join(',');
14 const requisites = await sendRequest(`${API.orgReqs}?ogrn=${ogrns}`);
15 const orgsMap = reqsToMap(requisites);
16 const analytics = await sendRequest(`${API.analytics}121212?ogrn=${ogrns}`);
17
18 addInOrgsMap(orgsMap, analytics, 'analytics');
19 const buh = await sendRequest(`${API.buhForms}?ogrn=${ogrns}`);
20
21 addInOrgsMap(orgsMap, buh, 'buhForms');
22 render(orgsMap, orgOgrns);
23 } catch (e) {
24 handleError(e);
25 }
26}
27
28run();
29
30function sendRequest(url) {
31 return new Promise(async (res, rej) => {
32 const response = await fetch(url);
33 if (response.ok) {
34 if (response.status === 200) {
35 res(await response.json());
36 }
37 if (response.status > 300) {
38 rej(response.status);
39 }
40 }
41 });
42}
43
44function reqsToMap(requisites) {
45 return requisites.reduce((acc, item) => {
46 acc[item.ogrn] = item;
47 return acc;
48 }, {});
49}
50
51function addInOrgsMap(orgsMap, additionalInfo, key) {
52 for (const item of additionalInfo) {
53 orgsMap[item.ogrn][key] = item[key];
54 }
55}
56
57function render(organizationsInfo, organizationsOrder) {
58 const table = document.getElementById('organizations');
59 table.classList.remove('hide');
60
61 const template = document.getElementById('orgTemplate');
62 const container = table.querySelector('tbody');
63
64 organizationsOrder.forEach((item) => {
65 renderOrganization(organizationsInfo[item], template, container);
66 });
67}
68
69function renderOrganization(orgInfo, template, container) {
70 const clone = document.importNode(template.content, true);
71 const name = clone.querySelector('.name');
72 const indebtedness = clone.querySelector('.indebtedness');
73 const money = clone.querySelector('.money');
74 const address = clone.querySelector('.address');
75
76 name.textContent =
77 (orgInfo.UL && orgInfo.UL.legalName && orgInfo.UL.legalName.short) || '';
78 indebtedness.textContent = formatMoney(orgInfo.analytics.s1002 || 0);
79
80 if (
81 orgInfo.buhForms &&
82 orgInfo.buhForms.length &&
83 orgInfo.buhForms[orgInfo.buhForms.length - 1] &&
84 orgInfo.buhForms[orgInfo.buhForms.length - 1].year === 2017
85 ) {
86 money.textContent = formatMoney(
87 (orgInfo.buhForms[orgInfo.buhForms.length - 1].form2 &&
88 orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0] &&
89 orgInfo.buhForms[orgInfo.buhForms.length - 1].form2[0].endValue) ||
90 0
91 );
92 } else {
93 money.textContent = '—';
94 }
95
96 const addressFromServer = orgInfo.UL.legalAddress.parsedAddressRF;
97 address.textContent = createAddress(addressFromServer);
98
99 container.appendChild(clone);
100}
101
102function formatMoney(money) {
103 let formatted = money.toFixed(2);
104 formatted = formatted.replace('.', ',');
105
106 const rounded = money.toFixed(0);
107 const numLen = rounded.length;
108 for (let i = numLen - 3; i > 0; i -= 3) {
109 formatted = `${formatted.slice(0, i)} ${formatted.slice(i)}`;
110 }
111
112 return `${formatted} ₽`;
113}
114
115function createAddress(address) {
116 const addressToRender = [];
117 if (address.regionName) {
118 addressToRender.push(createAddressItem('regionName'));
119 }
120 if (address.city) {
121 addressToRender.push(createAddressItem('city'));
122 }
123 if (address.street) {
124 addressToRender.push(createAddressItem('street'));
125 }
126 if (address.house) {
127 addressToRender.push(createAddressItem('house'));
128 }
129
130 return addressToRender.join(', ');
131
132 function createAddressItem(key) {
133 return `${address[key].topoShortName}. ${address[key].topoValue}`;
134 }
135}
136