Advertisement
NikolayPaskulov

Untitled

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