Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var itemsList = {},
- clientsList = {},
- tableHeader = document.getElementById('table-header'),
- docsWrapper = document.getElementById('docsWrapper'),
- mainWrapper = document.getElementById('main-wrapper'),
- firstDateInput = document.getElementById('firstDate'),
- clientSelect = document.getElementById('clientSelect'),
- secDateInput = document.getElementById('secDate'),
- checkBox = document.getElementById('checkBox'),
- clientsWithDocsBtn = document.getElementById('all-clients-with-docs'),
- table = document.getElementById('table');
- //Extract all items and clients from the main JSON and put them into two associative arrays
- function generateRef(obj) {
- for (var a = 0; a < obj.length; a++) {
- var currentDate = obj[a].docDate;
- if (checkIfDateIsValid(currentDate.slice(6, currentDate.length), currentDate.slice(4, 6), currentDate.slice(0, 4))) {
- fillclientsList(obj[a].client, obj[a]);
- for (var b = 0; b < obj[a].Items.length; b++) {
- checkItems(obj[a].client, obj[a].Items[b]);
- }
- }
- }
- }
- function checkItems(client, march) {
- if (itemsList[march.name]) {
- if (itemsList[march.name][client]) {
- itemsList[march.name][client] = Number(itemsList[march.name][client]) + Number(march.quantity);
- } else {
- itemsList[march.name][client] = Number(march.quantity);
- }
- } else {
- itemsList[march.name] = {};
- itemsList[march.name][client] = Number(march.quantity);
- }
- }
- function fillclientsList(name, obj) {
- var Items = {};
- if (clientsList[name]) {
- if (clientsList[name][obj.docNumber]) {
- return;
- } else {
- clientsList[name][obj.docNumber] = {
- docDate: obj.docDate,
- };
- }
- } else {
- clientsList[name] = {};
- clientsList[name][obj.docNumber] = {
- docDate: obj.docDate,
- };
- }
- for (var i = 0; i < obj.Items.length; i++) {
- if (Items[obj.Items[i].name]) {
- Items[obj.Items[i].name].quantity += Number(obj.Items[i].quantity);
- } else {
- Items[obj.Items[i].name] = {};
- Items[obj.Items[i].name].quantity = Number(obj.Items[i].quantity);
- }
- }
- clientsList[name][obj.docNumber]['Items'] = Items;
- }
- function buildTable(check) {
- function fillHeader() {
- var strArray = ['<div class="table-row"><div class="plain-cell"> </div><div class="plain-cell">No</div>'],
- docArr = ['<div class="table-row"><div class="plain-cell">Док. номер</div><div class="plain-cell"> </div>'],
- docDate = ['<div class="table-row"><div class="plain-cell">Док. дата</div><div class="plain-cell"> </div>'];
- for (var key in clientsList) {
- if (check) {
- for (var prop in clientsList[key]) {
- var date = clientsList[key][prop].docDate;
- strArray.push('<div class="plain-cell no-print">' + key + '</div>')
- docArr.push('<div class="plain-cell">' + prop + '</div>');
- docDate.push('<div class="plain-cell">' + date.substr(0, 4) + '.' + date.substr(4, 2) + '.' + date.substr(6, 2) + '</div>');
- }
- }
- else {
- strArray.push('<div class="plain-cell no-print">' + key + '</div>');
- }
- }
- docArr.push('<div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></div>')
- docDate.push('<div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></div>')
- strArray.push('<div class="plain-cell">Общо планирани</div><div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>');
- if (check) {
- docArr.push('</div>');
- docDate.push('</div>');
- docsWrapper.innerHTML = docArr.join('') + docDate.join('');
- }
- tableHeader.innerHTML = strArray.join('');
- resizeConteiners(tableHeader.children[0].children.length * 80 + 140);
- }
- fillHeader();
- function fillRows() {
- var strArray = [],
- counter = 1,
- totalRowItems = 0;
- for (var key in itemsList) {
- strArray.push('<div class="table-row"><div class="plain-cell">' + key + '</div><div class="plain-cell">' + counter + '</div>')
- for (var name in clientsList) {
- if (check) {
- var result = func(clientsList[name], key);
- strArray.push(result.html);
- totalRowItems += result.quantity;
- continue;
- }
- if (itemsList[key][name]) {
- strArray.push('<div class="plain-cell no-print">' + itemsList[key][name] + '</div>')
- totalRowItems += Number(itemsList[key][name]);
- } else {
- strArray.push('<div class="plain-cell no-print">' + 0 + '</div>')
- }
- }
- var calculateQuantity = store[key].quantity - store[key].blocked - totalRowItems;
- strArray.push('<div class="plain-cell">' + totalRowItems + '</div><div class="plain-cell">' + store[key].quantity + '</div>' +
- '<div class="plain-cell">' + store[key].blocked + '</div>' +
- '<div class="plain-cell">' + calculateQuantity + '</div></div>');
- counter++;
- totalRowItems = 0;
- }
- table.innerHTML = strArray.join('');
- if (check) {
- printTotalQuantity(clientsList, 1)
- }
- else {
- printTotalQuantity(clientsList, 3);
- }
- }
- fillRows();
- }
- function func(obj, itemName) {
- var strArray = [];
- totalRowItems = 0;
- for (var key in obj) {
- console.log(obj[key].Items[itemName]);
- if (obj[key].Items[itemName]) {
- strArray.push('<div class="plain-cell no-print">' + obj[key].Items[itemName].quantity + '</div>');
- totalRowItems += Number(obj[key].Items[itemName].quantity);
- } else {
- strArray.push('<div class="plain-cell no-print">' + 0 + '</div>');
- }
- }
- return {
- html: strArray.join(''),
- quantity: totalRowItems
- }
- }
- //Visualize all documents from client chosen by click on the header
- tableHeader.addEventListener('click', function (e) {
- var target = e.target.innerText,
- checkBiggerWidth = 0;
- if (target == 'No' || target == 'Общо планирани' || target == 'Количество от склад' ||
- target == 'Блокирани' || target == 'Разлика' || target.trim() == '') {
- return;
- }
- docsWrapper.style.display = '';
- changeHeaderAfterClick(target);
- clientsWithDocsBtn.disabled = true;
- resizeConteiners(docsWrapper.childNodes[0].childElementCount * 80 + 140)
- if (target == 'Всички') {
- clientsWithDocsBtn.disabled = false;
- if (!checkBox.checked) {
- checkBox.click();
- }
- if (clientsWithDocsBtn.checked) {
- buildTable(true);
- return;
- }
- docsWrapper.style.display = 'none';
- buildTable();
- }
- });
- function fillTableAfterClick(targetName) {
- var strArray = [],
- invoiceNumber = [],
- invoiceDate = [],
- rowCounter = 1,
- iterationCounter = 0,
- countRowItems = 0,
- allItems = 0;
- invoiceNumber.push('<div class="table-row no-print"><div class="plain-cell">Док.ном</div><div class="plain-cell"> </div>')
- invoiceDate.push('<div class="table-row no-print"><div class="plain-cell">Дата</div><div class="plain-cell"> </div>')
- var itemsArr = extractItemsFromClient(clientsList[targetName]);
- for (var name in itemsArr) {
- strArray.push('<div class="table-row"><div class="plain-cell">' + name + '</div><div class="plain-cell">'+ rowCounter +'</div>');
- for (var key in clientsList[targetName]) {
- if (iterationCounter < Object.keys(clientsList[targetName]).length) {
- var currentDate = clientsList[targetName][key].docDate;
- invoiceNumber.push('<div class="plain-cell">' + key + '</div>')
- invoiceDate.push('<div class="plain-cell">' + currentDate.slice(6, currentDate.length) + '.' + currentDate.slice(4, 6) + '.' + currentDate.slice(0, 4) + '</div>')
- }
- if (clientsList[targetName][key].Items[name]) {
- strArray.push('<div class="plain-cell no-print">' + clientsList[targetName][key].Items[name].quantity + '</div>');
- countRowItems += Number(clientsList[targetName][key].Items[name].quantity);
- } else {
- strArray.push('<div class="plain-cell no-print"> </div>');
- }
- iterationCounter++;
- }
- var quantitydifference = Number(store[name].quantity) - Number(store[name].blocked) - countRowItems;
- strArray.push('<div class="plain-cell">' + countRowItems + '</div><div class="plain-cell">' + store[name].quantity + '</div>' +
- '<div class="plain-cell">' + store[name].blocked + '</div><div class="plain-cell">' + quantitydifference + '</div>');
- allItems += countRowItems;
- strArray.push('</div>');
- countRowItems = 0;
- rowCounter++;
- }
- invoiceNumber.push('<div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></div>')
- invoiceDate.push('<div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></div>')
- docsWrapper.innerHTML = invoiceNumber.join('') + invoiceDate.join('');
- table.innerHTML = strArray.join('');
- printTotalQuantity(clientsList[targetName], 2);
- }
- function printTotalQuantity(obj, check) {
- var strArray = ['<div class="table-row"><div class="plain-cell">Общо</div><div class="plain-cell"> </div>'],
- docQuantity = 0,
- allItems = 0;
- if (check === 1) {
- for (var i in clientsList) {
- getQuantity(clientsList[i], true);
- }
- }
- else if (check === 2) {
- getQuantity(obj, true)
- }
- else if (check === 3) {
- for (var z in obj) {
- getQuantity(obj[z], false)
- }
- }
- function getQuantity(obj, allDocs) {
- for (var prop in obj) {
- for (var item in obj[prop].Items) {
- docQuantity += Number(obj[prop].Items[item].quantity);
- }
- if (allDocs) {
- strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
- allItems += docQuantity;
- docQuantity = 0;
- }
- }
- if (!allDocs) {
- strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
- allItems += docQuantity;
- docQuantity = 0;
- }
- }
- strArray.push('<div class="plain-cell">' + allItems + '</div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></div>');
- table.innerHTML += strArray.join('');
- }
- function extractItemsFromClient(client) {
- var result = {};
- for (var doc in client) {
- for (var item in client[doc].Items) {
- if (!result[item]) result[item] = {};
- }
- }
- return result;
- }
- document.getElementById('show-reference').addEventListener('click', checkIfDatesInputAreActive);
- document.getElementById('firstDate').addEventListener('change', fillSelectWithClients);
- document.getElementById('secDate').addEventListener('change', fillSelectWithClients);
- function fillSelectWithClients() {
- if (firstDateInput.value && secDateInput.value) {
- clientSelect.innerHTML = '<option>Всички</option>'
- itemsList = {};
- clientsList = {};
- generateRef(json);
- for (var name in clientsList) {
- clientSelect.innerHTML += '<option>' + name + '</option';
- }
- }
- }
- function changeHeaderAfterClick(name) {
- tableHeader.innerHTML = '<div class="table-row"><div class="plain-cell"> </div><div class="plain-cell">No</div><div class="plain-cell no-print">' + name + '</div><div class="plain-cell no-print" id="empty-cell"></div><div class="plain-cell">Общо планирани</div>' +
- '<div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>';
- fillTableAfterClick(name);
- tableHeader.childNodes[0].firstChild.innerText = 'Всички';
- }
- function checkIfDatesInputAreActive() {
- var clientSelectValue = clientSelect.options[clientSelect.selectedIndex].innerText;
- if (firstDateInput.value && secDateInput.value) {
- docsWrapper.innerHTML = '';
- checkBox.disabled = false;
- if (clientSelectValue != 'Всички') {
- changeHeaderAfterClick(clientSelectValue);
- fillTableAfterClick(clientSelectValue);
- if (!checkBox.checked) {
- checkBox.click();
- }
- resizeConteiners(docsWrapper.children[0].childElementCount * 80 + 140)
- return;
- }
- clientsList = {};
- itemsList = {};
- generateRef(json);
- if (!checkBox.checked) {
- checkBox.click();
- }
- if (clientsWithDocsBtn.checked) {
- if (clientsWithDocsBtn.disabled) {
- clientsWithDocsBtn.disabled = false;
- }
- clientsWithDocsBtn.click();
- }
- buildTable();
- }
- }
- function checkIfDateIsValid(day, month, year) {
- var firstDateInput = document.getElementById('firstDate'),
- secDateInput = document.getElementById('secDate'),
- regEx = /[0-9]+/g,
- firstDateArray,
- secDateArray;
- firstDateArray = firstDateInput.value.match(regEx);
- secDateArray = secDateInput.value.match(regEx);
- if (firstDateArray && secDateArray) {
- var prevDate = new Date(firstDateArray[0], (firstDateArray[1] - 1), firstDateArray[2], 0, 0, 0, 0);
- var currentDate = new Date(secDateArray[0], (secDateArray[1] - 1), secDateArray[2], 0, 0, 0, 0);
- if (prevDate > currentDate) {
- return;
- }
- var productDate = new Date(year, (Number(month) - 1), Number(day), 0, 0, 0, 0);
- if (productDate.valueOf() >= prevDate.valueOf() && productDate.valueOf() <= currentDate.valueOf()) {
- return true;
- } else {
- return false;
- }
- }
- }
- checkBox.addEventListener('change', function () {
- if (!this.checked) {
- document.head.innerHTML += '<style>.no-print{display:none}</style>';
- resizeConteiners(table.offsetWidth);
- docsWrapper.style.display = 'none';
- }
- else {
- document.head.removeChild(document.getElementsByTagName('style')[0]);
- resizeConteiners(table.childNodes[0].childElementCount * 80 + 140);
- if (clientsWithDocsBtn.checked) {
- docsWrapper.style.display = '';
- } else {
- docsWrapper.style.display = 'none';
- }
- }
- });
- clientsWithDocsBtn.addEventListener('change', function () {
- if (this.checked) {
- buildTable(true)
- docsWrapper.style.display = '';
- if (!checkBox.checked) checkBox.click();
- }
- else {
- buildTable();
- resizeConteiners(table.offsetWidth);
- docsWrapper.style.display = 'none';
- }
- });
- function resizeConteiners(size) {
- docsWrapper.style.width = size + 'px';
- mainWrapper.style.width = size + 2 + 'px';
- tableHeader.childNodes[0].style.width = size + 'px';
- if (docsWrapper.childElementCount == 0) {
- return;
- }
- if (document.getElementById('empty-cell') == undefined) {
- return;
- }
- document.getElementById('empty-cell').style.width = (docsWrapper.offsetWidth - 700) + 'px';
- if (docsWrapper.children[0].children.length <= 7) {
- document.getElementById('empty-cell').style.display = 'none'
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement