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