Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var itemsList = {},
- clientsList = {},
- store = {},
- prevRow,
- prevCol = 0,
- showReference = document.getElementById('show-reference'),
- tableHeader = document.getElementById('table-header'),
- docsWrapper = document.getElementById('docsWrapper'),
- firstDateInput = document.getElementById('firstDate'),
- secDateInput = document.getElementById('secDate'),
- checkBox = document.getElementById('checkBox'),
- clientsWithDocsBtn = document.getElementById('all-clients-with-docs'),
- table = document.getElementById('table');
- sendRequestToServer(['objects', 'ЛЕЙДИ СОФИЯ АД'], 'storesList');
- function sendRequestToServer(arr, wanted) {
- var request = new XMLHttpRequest();
- request.onreadystatechange = function () {
- if (request.readyState == 4 && request.status == 200) {
- if (wanted == 'storesList') {
- wanted = request.responseText;
- wanted = JSON.parse(wanted);
- fillStoresSelect(wanted);
- }
- if (wanted == 'store') {
- wanted = request.responseText;
- wanted = wanted.replace(/""/g, '"');
- wanted = JSON.parse(wanted);
- showReference.disabled = false;
- }
- if (wanted == 'documents') {
- wanted = JSON.parse(request.responseText);
- changeStore(wanted);
- generateRef(wanted);
- buildTable();
- fillSelectWithClients();
- }
- }
- }
- request.open("POST", "http://192.168.1.184:8000/pm", true);
- request.send(JSON.stringify(arr));
- }
- function fillStoresSelect(arr) {
- var option = document.createElement('option');
- for (var i = 0; i < arr.length; i++) {
- var optionClone = option.cloneNode(true);
- optionClone.innerText = arr[i].slice(arr[i].lastIndexOf(':') + 1);
- optionClone.value = '{"warehouse":' + '"' + arr[i] + '"}';
- document.getElementById('storesSelect').appendChild(optionClone);
- }
- }
- document.getElementById('storesSelect').onchange = function () {
- var selected = this.value;
- if (selected == 'Всички') {
- showReference.disabled = true;
- sendRequestToServer(['items', '{}'], 'store');
- } else {
- sendRequestToServer(['items', selected], 'store');
- }
- }
- var testArr = [];
- //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++) {
- fillclientsList(obj[a].partner, obj[a]);
- for (var b = 0; b < obj[a].Lines.length; b++) {
- checkItems(obj[a].partner, obj[a].Lines[b]);
- }
- }
- }
- //Convert store JSON into an object
- function changeStore(currentStore) {
- store = {};
- for (var i = 0; i < currentStore.length; i++) {
- store[currentStore[i].name] = currentStore[i];
- }
- }
- //on every merchandise if array fill clients that have it with whole quantity
- function checkItems(initialClient, march) {
- var client = (initialClient) ? initialClient.slice(initialClient.lastIndexOf(':') + 1) : 'No Name';
- var itemName = march.item.slice(march.item.lastIndexOf(':') + 1);
- if (itemsList[itemName]) {
- if (itemsList[itemName][client]) {
- (march.quantity) ? itemsList[itemName][client] = Number(itemsList[itemName][client]) + Number(march.quantity) : itemsList[itemName][client];
- } else {
- (march.quantity) ? itemsList[itemName][client] = Number(march.quantity) : 0;
- }
- } else {
- itemsList[itemName] = {};
- (march.quantity) ? itemsList[itemName][client] = Number(march.quantity) : 0;
- }
- }
- //all clients with theys documents and full info on document
- function fillclientsList(initalName, obj) {
- var name = (initalName) ? initalName.slice(initalName.lastIndexOf(':') + 1) : 'No Name';
- if (clientsList[name]) {
- if (clientsList[name][obj.num]) {
- var newDocNumber = obj.num + '(new(' + (Math.floor(Math.random() * 10000) + 1) + '))';
- clientsList[name][newDocNumber] = {
- date: obj.date
- }
- fillClientDocumentWithItems(newDocNumber);
- return;
- } else {
- clientsList[name][obj.num] = {
- date: obj.date,
- };
- }
- } else {
- clientsList[name] = {};
- clientsList[name][obj.num] = {
- date: obj.date
- };
- }
- fillClientDocumentWithItems(obj.num);
- function fillClientDocumentWithItems(number) {
- var Items = {};
- for (var i = 0; i < obj.Lines.length; i++) {
- if (Items[obj.Lines[i].item]) {
- Items[obj.Lines[i].item].quantity += (obj.Lines[i].quantity) ? Number(obj.Lines[i].quantity) : 0;
- } else {
- Items[obj.Lines[i].item] = {};
- Items[obj.Lines[i].item].quantity = (obj.Lines[i].quantity) ? Number(obj.Lines[i].quantity) : 0;
- }
- }
- clientsList[name][number]['Items'] = Items;
- }
- }
- function buildTable(reUse) {
- (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 (reUse) {
- for (var prop in clientsList[key]) {
- var date = clientsList[key][prop].date;
- 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(6, 2) + '.' + date.substr(4, 2) + '.' + date.substr(0, 4) + '</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></div>')
- docDate.push('<div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></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 (reUse) {
- docsWrapper.innerHTML = docArr.join('') + docDate.join('');
- }
- tableHeader.innerHTML = strArray.join('');
- resizeConteiners(tableHeader.children[0].children.length * 100 + 300);
- }());
- (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 (reUse) {
- var result = expandedRows(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"> </div>')
- }
- }
- var storeQuantity = 0,
- storeBlocked = 0;
- if (typeof (store[key]) != 'undefined') {
- storeQuantity = (typeof (store[key].quantity) == 'undefined') ? 0 : store[key].quantity;
- storeBlocked = (typeof (store[key].blocked) != 'undefined') ? store[key].blocked : 0;
- }
- quantitydifference = Number(storeQuantity) - storeBlocked - totalRowItems;
- strArray.push('<div class="plain-cell">' + totalRowItems + '</div><div class="plain-cell">' + storeQuantity + '</div>' +
- '<div class="plain-cell">' + storeBlocked + '</div>' +
- '<div class="plain-cell">' + quantitydifference + '</div></div>');
- counter++;
- totalRowItems = 0;
- }
- if (reUse) {
- table.innerHTML = setSerialNumberRow(clientsList, true);
- }
- else {
- table.innerHTML = setSerialNumberRow(clientsList, false);
- }
- table.innerHTML += strArray.join('');
- printTotalQuantity();
- }());
- }
- //Build row with serial number for each client or document
- function setSerialNumberRow(obj, isExpanded) {
- var strArr = [],
- prevProp,
- counter = 1;
- strArr.push('<div class="table-row no-print"><div class="plain-cell"> </div><div class="plain-cell">No</div>');
- for (var prop in obj) {
- strArr.push('<div class="plain-cell">' + counter + '</div>');
- counter++;
- if (isExpanded) {
- var countKeys = 0;
- for (var key in obj[prop]) {
- countKeys++;
- }
- for (var i = 0; i < countKeys - 1; i++) {
- strArr.push('<div class="plain-cell"> </div>');
- }
- }
- prevProp = prop;
- }
- strArr.push('<div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div><div class="plain-cell"> </div></div>');
- return strArr.join('');
- }
- //expands basic table after click on chechbox
- function expandedRows(obj, itemName) {
- var strArray = [];
- totalRowItems = 0;
- for (var key in obj) {
- 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"> </div>');
- }
- }
- return {
- html: strArray.join(''),
- quantity: totalRowItems
- }
- }
- //Visualize all documents from client chosen by click on the header
- tableHeader.onclick = function (e) {
- var target = (typeof (event) != 'undefined') ? event.srcElement.outerText : e.target.innerHTML,
- checkBiggerWidth = 0;
- if (target == 'No' || target == 'Общо планирани' || target == 'Количество от склад' ||
- target == 'Блокирани' || target == 'Разлика' || target == '' || target.length == 1) {
- return;
- }
- if (target == 'Всички') {
- clientsWithDocsBtn.disabled = false;
- clientsWithDocsBtn.checked = false;
- if (!checkBox.checked) {
- checkBox.click();
- }
- docsWrapper.style.display = 'none';
- buildTable();
- return;
- }
- docsWrapper.style.display = '';
- changeHeaderAfterClick(target);
- clientsWithDocsBtn.disabled = true;
- resizeConteiners(docsWrapper.childNodes[0].children.length * 100 + 300)
- };
- //after click on some cell change color of a cell row and col
- table.onclick = function (ev) {
- var target = (typeof (event) != 'undefined') ? event.srcElement : ev.target;
- if (target.className == 'plain-cell no-print') {
- var rows = document.querySelectorAll('#table .table-row');
- if (prevCol <= rows[0].children.length) {
- for (var z = 0; z < rows.length; z++) {
- rows[z].children[prevCol].style.backgroundColor = '';
- }
- }
- if (prevCol == checkForPrevCol(target.parentNode, target) && prevRow == target.parentNode) {
- prevRow.style.backgroundColor = '';
- prevCol = 0;
- return;
- }
- prevCol = checkForPrevCol(target.parentNode, target);
- for (var z = 0; z < rows.length; z++) {
- rows[z].children[prevCol].style.backgroundColor = 'lightBlue';
- }
- if (prevRow) {
- prevRow.style.backgroundColor = '';
- }
- prevRow = target.parentNode;
- prevRow.style.backgroundColor = 'lightblue';
- target.style.backgroundColor = '#bde0eb';
- }
- };
- function checkForPrevCol(parentEl, targetCell) {
- for (var i = 0; i < parentEl.children.length; i++) {
- if (parentEl.children[i] == targetCell) {
- return i;
- }
- }
- return -1;
- }
- //fill table with data for clicked client
- function fillTableAfterClick(targetName) {
- var strArray = [],
- invoiceNumber = [],
- invoiceDate = [],
- rowCounter = 1,
- iterationCounter = 0,
- countRowItems = 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>');
- var clientKeys = 0;
- for (var keys in clientsList[targetName]) {
- clientKeys++;
- }
- for (var key in clientsList[targetName]) {
- if (iterationCounter < clientKeys) {
- var currentDate = clientsList[targetName][key].date;
- 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 storeQuantity = 0,
- storeBlocked = 0;
- if (typeof (store[key]) != 'undefined') {
- storeQuantity = (typeof (store[key].quantity) == 'undefined') ? 0 : store[key].quantity;
- storeBlocked = (typeof (store[key].blocked) != 'undefined') ? store[key].blocked : 0;
- }
- strArray.push('<div class="plain-cell">' + countRowItems + '</div><div class="plain-cell">' + storeQuantity + '</div>' +
- '<div class="plain-cell">' + storeBlocked + '</div><div class="plain-cell">' + quantitydifference + '</div>');
- 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 = setSerialNumberRow(clientsList[targetName], false) + strArray.join('');
- printTotalQuantity();
- }
- //fill footer with calculated data from colums
- function printTotalQuantity() {
- var strArray = ['<div class="table-row"><div class="plain-cell">Общо</div><div class="plain-cell"> </div>'],
- sum = 0;
- for (var i = 2; i < table.children[0].children.length; i++) {
- for (var z = 1; z < table.children.length; z++) {
- sum += Number(table.children[z].children[i].innerHTML == ' ' ? '' : table.children[z].children[i].innerHTML);
- }
- if (i < table.children[0].children.length - 4) {
- strArray.push('<div class="plain-cell no-print">' + sum + '</div>')
- }
- else {
- strArray.push('<div class="plain-cell">' + sum + '</div>')
- }
- sum = 0;
- }
- strArray.push('</div>');
- table.innerHTML += strArray.join('');
- }
- //extract only items that are in all clients
- function extractItemsFromClient(client) {
- var result = {};
- for (var doc in client) {
- for (var item in client[doc].Items) {
- if (!result[item]) result[item] = {};
- }
- }
- return result;
- }
- showReference.onclick = checkIfDatesInputAreActive;
- function fillSelectWithClients() {
- var strArray = [];
- if (firstDateInput.value && secDateInput.value) {
- strArray.push('<option>Всички</option>');
- for (var name in clientsList) {
- strArray.push('<option>' + name + '</option>');
- }
- }
- document.getElementById('clientSelect').innerHTML = strArray.join('');
- }
- //change header with only clicked client name and empty cells; call fillTableAfterClick with client name
- 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);
- clientsWithDocsBtn.checked = true;
- clientsWithDocsBtn.disabled = true;
- tableHeader.childNodes[0].firstChild.innerHTML = 'Всички';
- }
- function checkIfDatesInputAreActive() {
- var clientSelectValue = document.getElementById('clientSelect').options[document.getElementById('clientSelect').selectedIndex].innerText,
- firstDateArr = (firstDateInput.value).split('-'),
- secDateArr = (secDateInput.value).split('-');
- if (firstDateInput.value && secDateInput.value) {
- checkBox.disabled = false;
- if (clientSelectValue != 'Всички') {
- docsWrapper.style.display = '';
- changeHeaderAfterClick(clientSelectValue);
- if (!checkBox.checked) {
- checkBox.click();
- }
- resizeConteiners(docsWrapper.children[0].children.length * 100 + 300)
- return;
- }
- clientsList = {};
- itemsList = {};
- //sending request for documents at given
- sendRequestToServer(['documents', 'ЛЕЙДИ СОФИЯ АД', firstDateArr.reverse().join(''), secDateArr.reverse().join(''), '', 'forward', '{}', 'false'], 'documents');
- if (!checkBox.checked) {
- checkBox.click();
- }
- if (clientsWithDocsBtn.disabled) {
- clientsWithDocsBtn.disabled = false;
- }
- if (clientsWithDocsBtn.checked) {
- clientsWithDocsBtn.checked = false;
- }
- docsWrapper.style.display = 'none';
- }
- }
- checkBox.onclick = 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.querySelectorAll('style')[0]);
- resizeConteiners(table.children[0].children.length * 100 + 300);
- if (clientsWithDocsBtn.checked) {
- docsWrapper.style.display = '';
- } else {
- docsWrapper.style.display = 'none';
- }
- }
- if (prevRow) {
- prevRow.style.backgroundColor = '';
- }
- };
- clientsWithDocsBtn.onclick = 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';
- document.getElementById('main-wrapper').style.width = size + 2 + 'px';
- tableHeader.childNodes[0].style.width = size + 'px';
- if (docsWrapper.children.length == 0) {
- return;
- }
- if (!document.getElementById('empty-cell')) {
- return;
- }
- if (docsWrapper.offsetWidth - 1000 < 0) {
- return;
- }
- document.getElementById('empty-cell').style.width = (docsWrapper.offsetWidth - 1000) + 'px';
- if (docsWrapper.children[0].children.length <= 7) {
- document.getElementById('empty-cell').style.display = 'none'
- }
- }
- var datePickerBtns = document.querySelectorAll('.open-date-picker');
- for (var i = 0; i < datePickerBtns.length; i++) {
- datePickerBtns[i].onclick = generateDateForm;
- }
- function generateDateForm(ev) {
- var datePicker = document.getElementById('date-picker');
- //Set the main structure of the date-picker element;
- datePicker.innerHTML = '<div><button>◄</button><span></span><button>►</button></div><span class="hor-line"></span><div id="date-picker-week"><span>Sun</span>' +
- '<span>Mon</span><span>Tue</span><span>Wed</span><span>Thu</span><span>Fri</span><span>Sat</span></div><span class="hor-line"></span><div id="date-picker-days"></div>';
- var monthNames = ["January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"],
- month = datePicker.querySelectorAll('span')[0],
- startOrEndDate = this.previousSibling,
- currentMonth = Number(startOrEndDate.value.substr(3, 2)) - 1,
- currentYear = startOrEndDate.value.substr(6, 4);
- if (datePicker.style.display == 'inline-block' && datePicker.style.left == Math.floor(this.offsetLeft - datePicker.offsetWidth / 2) + 'px') {
- datePicker.style.display = 'none';
- return;
- }
- datePicker.style.display = 'inline-block';
- datePicker.style.left = Math.floor(this.offsetLeft - (datePicker.offsetWidth / 2)) + 'px';
- datePicker.style.top = (this.offsetTop + 23) + 'px';
- month.innerHTML = monthNames[currentMonth] + ' - ' + currentYear;
- //Change months
- document.querySelectorAll('#date-picker button')[0].onclick = function () {
- if (currentMonth > 0) {
- currentMonth -= 1;
- }
- else {
- currentMonth = 11;
- currentYear -= 1;
- }
- month.innerHTML = monthNames[currentMonth] + ' - ' + currentYear;
- generateDays(currentMonth, currentYear);
- }
- document.querySelectorAll('#date-picker button')[1].onclick = function () {
- if (currentMonth < 11) {
- currentMonth += 1;
- }
- else {
- currentMonth = 0;
- currentYear += 1;
- }
- month.innerHTML = monthNames[currentMonth] + ' - ' + currentYear;
- generateDays(currentMonth, currentYear);
- }
- //Click on day to pick date
- datePicker.onclick = function (ev) {
- var target = (typeof (event) != 'undefined') ? event.srcElement : ev.target;
- if (target.className == 'active-day') {
- var inputMonth = (Number(currentMonth) + 1 > 9) ? Number(currentMonth) + 1 : 0 + '' + (Number(currentMonth) + 1),
- inputDay = (target.innerHTML > 9) ? target.innerHTML : 0 + '' + target.innerHTML;
- startOrEndDate.value = inputDay + '-' + inputMonth + '-' + currentYear;
- datePicker.style.display = 'none';
- }
- }
- //Add days from current month, previous month (if current month starts from week day different from Sunday)
- //and next month (if there is empty space at the end of the last row with days)
- function generateDays(chosenMonth, chosenYear) {
- var activeMonth = new Date(chosenYear, chosenMonth),
- currentMonthDays = getDaysInMonth(chosenMonth, chosenYear).length,
- prevMonthDays = getDaysInMonth(chosenMonth - 1, chosenYear).length,
- wrapper = [],
- nextMonthDays = 0;
- for (var z = 0; z < activeMonth.getDay() ; z++) {
- wrapper.push('<span class="inactive-day">' + ((prevMonthDays + 1) - (activeMonth.getDay() - z)) + '</span>');
- nextMonthDays++;
- }
- for (var i = 1; i <= currentMonthDays; i++) {
- wrapper.push('<span class="active-day">' + i + '</span>');
- nextMonthDays++;
- }
- lastDaysLength = (nextMonthDays <= 35) ? 35 : 42;
- lastDaysLength = ((nextMonthDays + activeMonth.getDay()) == 28) ? 28 : lastDaysLength;
- for (var h = 1; h <= lastDaysLength - nextMonthDays; h++) {
- wrapper.push('<span class="inactive-day">' + h + '</span>');
- }
- document.getElementById('date-picker-days').innerHTML = wrapper.join('');
- }
- generateDays(currentMonth, currentYear);
- //Get the number of days in month
- function getDaysInMonth(month, year) {
- var month = month;
- if (month < 0) {
- month = 11;
- year -= year;
- }
- var date = new Date(year, month, 1),
- days = [];
- while (date.getMonth() === month) {
- days.push(new Date(date));
- date.setDate(date.getDate() + 1);
- }
- return days;
- }
- document.onclick = function (ev) {
- var target = (typeof (event) != 'undefined') ? event.srcElement : ev.target;
- while (target) {
- if (target.id == 'date-picker' || target.className == 'open-date-picker') {
- return;
- };
- target = target.parentNode
- }
- datePicker.style.display = 'none';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement