Advertisement
NikolayPaskulov

Untitled

Oct 20th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.08 KB | None | 0 0
  1. var itemsList = {},
  2. clientsList = {},
  3. prevRow,
  4. prevCol = 0,
  5. tableHeader = document.getElementById('table-header'),
  6. docsWrapper = document.getElementById('docsWrapper'),
  7. mainWrapper = document.getElementById('main-wrapper'),
  8. firstDateInput = document.getElementById('firstDate'),
  9. clientSelect = document.getElementById('clientSelect'),
  10. secDateInput = document.getElementById('secDate'),
  11. checkBox = document.getElementById('checkBox'),
  12. clientsWithDocsBtn = document.getElementById('all-clients-with-docs'),
  13. table = document.getElementById('table');
  14.  
  15. //Extract all items and clients from the main JSON and put them into two associative arrays
  16. function generateRef(obj) {
  17. for (var a = 0; a < obj.length; a++) {
  18. var currentDate = obj[a].docDate;
  19. if (checkIfDateIsValid(currentDate.slice(6, currentDate.length), currentDate.slice(4, 6), currentDate.slice(0, 4))) {
  20. fillclientsList(obj[a].client, obj[a]);
  21. for (var b = 0; b < obj[a].Items.length; b++) {
  22. checkItems(obj[a].client, obj[a].Items[b]);
  23. }
  24. }
  25. }
  26. }
  27.  
  28. //on every merchandise if array fill clients that have it with whole quantity
  29. function checkItems(client, march) {
  30. if (itemsList[march.name]) {
  31. if (itemsList[march.name][client]) {
  32. itemsList[march.name][client] = Number(itemsList[march.name][client]) + Number(march.quantity);
  33. } else {
  34. itemsList[march.name][client] = Number(march.quantity);
  35. }
  36. } else {
  37. itemsList[march.name] = {};
  38. itemsList[march.name][client] = Number(march.quantity);
  39. }
  40. }
  41.  
  42. //all clients with theys documents and full info on document
  43. function fillclientsList(name, obj) {
  44. var Items = {};
  45. if (clientsList[name]) {
  46. if (clientsList[name][obj.docNumber]) {
  47. return;
  48. } else {
  49. clientsList[name][obj.docNumber] = {
  50. docDate: obj.docDate,
  51. };
  52. }
  53. } else {
  54. clientsList[name] = {};
  55. clientsList[name][obj.docNumber] = {
  56. docDate: obj.docDate,
  57. };
  58. }
  59. for (var i = 0; i < obj.Items.length; i++) {
  60. if (Items[obj.Items[i].name]) {
  61. Items[obj.Items[i].name].quantity += Number(obj.Items[i].quantity);
  62. } else {
  63. Items[obj.Items[i].name] = {};
  64. Items[obj.Items[i].name].quantity = Number(obj.Items[i].quantity);
  65. }
  66. }
  67. clientsList[name][obj.docNumber]['Items'] = Items;
  68.  
  69. }
  70.  
  71. function buildTable(reUse) {
  72. function fillHeader() {
  73. var strArray = ['<div class="table-row"><div class="plain-cell">&nbsp;</div><div class="plain-cell">No</div>'],
  74. docArr = ['<div class="table-row"><div class="plain-cell">Док. номер</div><div class="plain-cell">&nbsp;</div>'],
  75. docDate = ['<div class="table-row"><div class="plain-cell">Док. дата</div><div class="plain-cell">&nbsp;</div>'];
  76. for (var key in clientsList) {
  77. if (reUse) {
  78. for (var prop in clientsList[key]) {
  79. var date = clientsList[key][prop].docDate;
  80. strArray.push('<div class="plain-cell no-print">' + key + '</div>')
  81. docArr.push('<div class="plain-cell">' + prop + '</div>');
  82. docDate.push('<div class="plain-cell">' + date.substr(0, 4) + '.' + date.substr(4, 2) + '.' + date.substr(6, 2) + '</div>');
  83. }
  84. }
  85. else {
  86. strArray.push('<div class="plain-cell no-print">' + key + '</div>');
  87. }
  88. }
  89. docArr.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>')
  90. docDate.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>')
  91. strArray.push('<div class="plain-cell">Общо планирани</div><div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>');
  92. if (reUse) {
  93. docArr.push('</div>');
  94. docDate.push('</div>');
  95. docsWrapper.innerHTML = docArr.join('') + docDate.join('');
  96. }
  97. tableHeader.innerHTML = strArray.join('');
  98. resizeConteiners(tableHeader.children[0].children.length * 70 + 150);
  99. }
  100. fillHeader();
  101.  
  102. function fillRows() {
  103. var strArray = [],
  104. counter = 1,
  105. totalRowItems = 0;
  106.  
  107. for (var key in itemsList) {
  108.  
  109. strArray.push('<div class="table-row"><div class="plain-cell">' + key + '</div><div class="plain-cell">' + counter + '</div>')
  110. for (var name in clientsList) {
  111. if (reUse) {
  112. var result = expandedRows(clientsList[name], key);
  113. strArray.push(result.html);
  114. totalRowItems += result.quantity;
  115. continue;
  116. }
  117. if (itemsList[key][name]) {
  118. strArray.push('<div class="plain-cell no-print">' + itemsList[key][name] + '</div>')
  119. totalRowItems += Number(itemsList[key][name]);
  120. } else {
  121. strArray.push('<div class="plain-cell no-print">&nbsp;</div>')
  122. }
  123. }
  124. var calculateQuantity = store[key].quantity - store[key].blocked - totalRowItems;
  125. strArray.push('<div class="plain-cell">' + totalRowItems + '</div><div class="plain-cell">' + store[key].quantity + '</div>' +
  126. '<div class="plain-cell">' + store[key].blocked + '</div>' +
  127. '<div class="plain-cell">' + calculateQuantity + '</div></div>');
  128. counter++;
  129. totalRowItems = 0;
  130. }
  131. while (table.firstChild) {
  132. table.removeChild(table.firstChild);
  133. }
  134. table.innerHTML = strArray.join('');
  135. if (reUse) {
  136. printTotalQuantity(clientsList, 1)
  137. }
  138. else {
  139. printTotalQuantity(clientsList, 3);
  140. }
  141. }
  142. fillRows();
  143. }
  144.  
  145. //expands basic table after click on chechbox
  146. function expandedRows(obj, itemName) {
  147. var strArray = [];
  148. totalRowItems = 0;
  149. for (var key in obj) {
  150. if (obj[key].Items[itemName]) {
  151. strArray.push('<div class="plain-cell no-print">' + obj[key].Items[itemName].quantity + '</div>');
  152. totalRowItems += Number(obj[key].Items[itemName].quantity);
  153. } else {
  154. strArray.push('<div class="plain-cell no-print">&nbsp;</div>');
  155. }
  156. }
  157. return {
  158. html: strArray.join(''),
  159. quantity: totalRowItems
  160. }
  161. }
  162.  
  163.  
  164. //Visualize all documents from client chosen by click on the header
  165. tableHeader.addEventListener('click', function (e) {
  166. var target = e.target.innerText,
  167. checkBiggerWidth = 0;
  168. if (target == 'No' || target == 'Общо планирани' || target == 'Количество от склад' ||
  169. target == 'Блокирани' || target == 'Разлика' || target.trim() == '') {
  170. return;
  171. }
  172. docsWrapper.style.display = '';
  173. changeHeaderAfterClick(target);
  174. clientsWithDocsBtn.disabled = true;
  175. resizeConteiners(docsWrapper.childNodes[0].childElementCount * 70 + 150)
  176.  
  177. if (target == 'Всички') {
  178. clientsWithDocsBtn.disabled = false;
  179. if (!checkBox.checked) {
  180. checkBox.click();
  181. }
  182. if (clientsWithDocsBtn.checked) {
  183. buildTable(true);
  184. return;
  185. }
  186. docsWrapper.style.display = 'none';
  187. buildTable();
  188. }
  189. });
  190.  
  191. //after click on some cell change color of a cell row and col
  192. table.addEventListener('click', function (ev) {
  193. if (ev.target.className == 'plain-cell no-print') {
  194. var currentCellIndex = ev.target.previousSibling,
  195. rows = table.getElementsByClassName('table-row');
  196.  
  197.  
  198. if (prevCol <= rows[0].children.length) {
  199. for (var z = 0; z < rows.length; z++) {
  200. rows[z].children[prevCol].style.backgroundColor = '';
  201. }
  202. }
  203. prevCol = Array.prototype.indexOf.call(ev.target.parentNode.childNodes, ev.target);
  204. for (var z = 0; z < rows.length; z++) {
  205. rows[z].children[prevCol].style.backgroundColor = 'lightBlue';
  206. }
  207.  
  208. if (prevRow) {
  209. prevRow.style.backgroundColor = '';
  210. }
  211. prevRow = ev.target.parentNode;
  212. prevRow.style.backgroundColor = 'lightblue';
  213. ev.target.style.backgroundColor = '#bde0eb';
  214. }
  215. });
  216.  
  217. //fill table with data for clicked client
  218. function fillTableAfterClick(targetName) {
  219. var strArray = [],
  220. invoiceNumber = [],
  221. invoiceDate = [],
  222. rowCounter = 1,
  223. iterationCounter = 0,
  224. countRowItems = 0,
  225. allItems = 0;
  226.  
  227. invoiceNumber.push('<div class="table-row no-print"><div class="plain-cell">Док.ном</div><div class="plain-cell">&nbsp;</div>')
  228. invoiceDate.push('<div class="table-row no-print"><div class="plain-cell">Дата</div><div class="plain-cell">&nbsp;</div>')
  229.  
  230. var itemsArr = extractItemsFromClient(clientsList[targetName]);
  231. for (var name in itemsArr) {
  232. strArray.push('<div class="table-row"><div class="plain-cell">' + name + '</div><div class="plain-cell">' + rowCounter + '</div>');
  233.  
  234. for (var key in clientsList[targetName]) {
  235. if (iterationCounter < Object.keys(clientsList[targetName]).length) {
  236. var currentDate = clientsList[targetName][key].docDate;
  237. invoiceNumber.push('<div class="plain-cell">' + key + '</div>')
  238. invoiceDate.push('<div class="plain-cell">' + currentDate.slice(6, currentDate.length) + '.' + currentDate.slice(4, 6) + '.' + currentDate.slice(0, 4) + '</div>')
  239. }
  240. if (clientsList[targetName][key].Items[name]) {
  241. strArray.push('<div class="plain-cell no-print">' + clientsList[targetName][key].Items[name].quantity + '</div>');
  242. countRowItems += Number(clientsList[targetName][key].Items[name].quantity);
  243. } else {
  244. strArray.push('<div class="plain-cell no-print">&nbsp;</div>');
  245. }
  246. iterationCounter++;
  247. }
  248. var quantitydifference = Number(store[name].quantity) - Number(store[name].blocked) - countRowItems;
  249. strArray.push('<div class="plain-cell">' + countRowItems + '</div><div class="plain-cell">' + store[name].quantity + '</div>' +
  250. '<div class="plain-cell">' + store[name].blocked + '</div><div class="plain-cell">' + quantitydifference + '</div>');
  251. allItems += countRowItems;
  252. strArray.push('</div>');
  253. countRowItems = 0;
  254. rowCounter++;
  255. }
  256.  
  257. invoiceNumber.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>')
  258. invoiceDate.push('<div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>')
  259. docsWrapper.innerHTML = invoiceNumber.join('') + invoiceDate.join('');
  260. table.innerHTML = strArray.join('');
  261. printTotalQuantity(clientsList[targetName], 2);
  262. }
  263.  
  264. //fill footer with calculated data from colums
  265. function printTotalQuantity(obj, check) {
  266. var strArray = ['<div class="table-row"><div class="plain-cell">Общо</div><div class="plain-cell">&nbsp;</div>'],
  267. docQuantity = 0,
  268. allItems = 0;
  269.  
  270.  
  271. if (check === 1) {
  272. for (var i in clientsList) {
  273. getQuantity(clientsList[i], true);
  274. }
  275. }
  276. else if (check === 2) {
  277. getQuantity(obj, true)
  278. }
  279.  
  280. else if (check === 3) {
  281. for (var z in obj) {
  282. getQuantity(obj[z], false)
  283. }
  284. }
  285.  
  286. function getQuantity(obj, allDocs) {
  287. for (var prop in obj) {
  288. for (var item in obj[prop].Items) {
  289. docQuantity += Number(obj[prop].Items[item].quantity);
  290. }
  291. if (allDocs) {
  292. strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
  293. allItems += docQuantity;
  294. docQuantity = 0;
  295. }
  296. }
  297. if (!allDocs) {
  298. strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
  299. allItems += docQuantity;
  300. docQuantity = 0;
  301. }
  302. }
  303. strArray.push('<div class="plain-cell">' + allItems + '</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div><div class="plain-cell">&nbsp;</div></div>');
  304. table.innerHTML += strArray.join('');
  305. }
  306.  
  307. //extract only items that are in all clients
  308. function extractItemsFromClient(client) {
  309. var result = {};
  310. for (var doc in client) {
  311. for (var item in client[doc].Items) {
  312. if (!result[item]) result[item] = {};
  313. }
  314. }
  315. return result;
  316. }
  317.  
  318. document.getElementById('show-reference').addEventListener('click', checkIfDatesInputAreActive);
  319.  
  320. document.getElementById('firstDate').addEventListener('change', fillSelectWithClients);
  321.  
  322. document.getElementById('secDate').addEventListener('change', fillSelectWithClients);
  323.  
  324.  
  325. function fillSelectWithClients() {
  326. var strArray = [];
  327. if (firstDateInput.value && secDateInput.value) {
  328. strArray.push('<option>Всички</option>');
  329. itemsList = {};
  330. clientsList = {};
  331. generateRef(json);
  332.  
  333. for (var name in clientsList) {
  334. strArray.push('<option>' + name + '</option>');
  335. }
  336. }
  337. clientSelect.innerHTML = strArray.join('');
  338. }
  339.  
  340. //change header with only clicked client name and empty cells; call fillTableAfterClick with client name
  341. function changeHeaderAfterClick(name) {
  342. tableHeader.innerHTML = '<div class="table-row"><div class="plain-cell">&nbsp;</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>' +
  343. '<div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>';
  344. fillTableAfterClick(name);
  345. tableHeader.childNodes[0].firstChild.innerText = 'Всички';
  346. }
  347.  
  348. function checkIfDatesInputAreActive() {
  349. var clientSelectValue = clientSelect.options[clientSelect.selectedIndex].innerText;
  350. if (firstDateInput.value && secDateInput.value) {
  351. checkBox.disabled = false;
  352. if (clientSelectValue != 'Всички') {
  353. docsWrapper.style.display = '';
  354. changeHeaderAfterClick(clientSelectValue);
  355. fillTableAfterClick(clientSelectValue);
  356. if (!checkBox.checked) {
  357. checkBox.click();
  358. }
  359. resizeConteiners(docsWrapper.children[0].childElementCount * 70 + 150)
  360. return;
  361. }
  362.  
  363. clientsList = {};
  364. itemsList = {};
  365.  
  366. generateRef(json);
  367. if (!checkBox.checked) {
  368. checkBox.click();
  369. }
  370. if (clientsWithDocsBtn.checked) {
  371. clientsWithDocsBtn.click();
  372. }
  373. if (clientsWithDocsBtn.disabled) {
  374. clientsWithDocsBtn.disabled = false;
  375. }
  376. docsWrapper.style.display = 'none';
  377. buildTable();
  378. }
  379. }
  380.  
  381. //find only dates that are in range
  382. function checkIfDateIsValid(day, month, year) {
  383. var firstDateInput = document.getElementById('firstDate'),
  384. secDateInput = document.getElementById('secDate'),
  385. regEx = /[0-9]+/g,
  386. firstDateArray,
  387. secDateArray;
  388.  
  389. firstDateArray = firstDateInput.value.match(regEx);
  390. secDateArray = secDateInput.value.match(regEx);
  391. if (firstDateArray && secDateArray) {
  392. var prevDate = new Date(firstDateArray[0], (firstDateArray[1] - 1), firstDateArray[2], 0, 0, 0, 0);
  393. var currentDate = new Date(secDateArray[0], (secDateArray[1] - 1), secDateArray[2], 0, 0, 0, 0);
  394.  
  395. if (prevDate > currentDate) {
  396. return;
  397. }
  398.  
  399. var productDate = new Date(year, (Number(month) - 1), Number(day), 0, 0, 0, 0);
  400. if (productDate.valueOf() >= prevDate.valueOf() && productDate.valueOf() <= currentDate.valueOf()) {
  401. return true;
  402. } else {
  403. return false;
  404. }
  405. }
  406. }
  407.  
  408. checkBox.addEventListener('change', function () {
  409. if (!this.checked) {
  410. document.head.innerHTML += '<style>.no-print{display:none}</style>';
  411. resizeConteiners(table.offsetWidth);
  412. docsWrapper.style.display = 'none';
  413. }
  414. else {
  415. document.head.removeChild(document.getElementsByTagName('style')[0]);
  416. resizeConteiners(table.childNodes[0].childElementCount * 70 + 150);
  417. if (clientsWithDocsBtn.checked) {
  418. docsWrapper.style.display = '';
  419. } else {
  420. docsWrapper.style.display = 'none';
  421. }
  422. }
  423.  
  424. });
  425.  
  426. clientsWithDocsBtn.addEventListener('change', function () {
  427. if (this.checked) {
  428. buildTable(true)
  429. docsWrapper.style.display = '';
  430. if (!checkBox.checked) checkBox.click();
  431. }
  432. else {
  433. buildTable();
  434. resizeConteiners(table.offsetWidth);
  435. docsWrapper.style.display = 'none';
  436. }
  437. });
  438.  
  439. function resizeConteiners(size) {
  440. docsWrapper.style.width = size + 'px';
  441. mainWrapper.style.width = size + 2 + 'px';
  442. tableHeader.childNodes[0].style.width = size + 'px';
  443.  
  444. if (docsWrapper.childElementCount == 0) {
  445. return;
  446. }
  447. if (document.getElementById('empty-cell') == undefined) {
  448. return;
  449. }
  450. document.getElementById('empty-cell').style.width = (docsWrapper.offsetWidth - 640) + 'px';
  451. if (docsWrapper.children[0].children.length <= 7) {
  452. document.getElementById('empty-cell').style.display = 'none'
  453. }
  454. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement