Advertisement
NikolayPaskulov

Untitled

Oct 17th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.72 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] += Number(obj.Items[i].quantity);
  58. } else {
  59. Items[obj.Items[i].name] = {};
  60. Items[obj.Items[i].name] = 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] + '</div>');
  146. totalRowItems += Number(obj[key].Items[itemName]);
  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. iterationCounter = 0,
  190. countRowItems = 0,
  191. allItems = 0;
  192.  
  193. invoiceNumber.push('<div class="table-row no-print"><div class="plain-cell">Док.ном</div><div class="plain-cell">&nbsp;</div>')
  194. invoiceDate.push('<div class="table-row no-print"><div class="plain-cell">Дата</div><div class="plain-cell">&nbsp;</div>')
  195.  
  196. var itemsArr = extractItemsFromClient(clientsList[targetName]);
  197. for (var name in itemsArr) {
  198. strArray.push('<div class="table-row"><div class="plain-cell">' + name + '</div><div class="plain-cell">&nbsp;</div>');
  199.  
  200. for (var key in clientsList[targetName]) {
  201. if (iterationCounter < Object.keys(clientsList[targetName]).length) {
  202. var currentDate = clientsList[targetName][key].docDate;
  203. invoiceNumber.push('<div class="plain-cell">' + key + '</div>')
  204. invoiceDate.push('<div class="plain-cell">' + currentDate.slice(6, currentDate.length) + '.' + currentDate.slice(4, 6) + '.' + currentDate.slice(0, 4) + '</div>')
  205. }
  206.  
  207. for (var i = 0; i < clientsList[targetName][key].Items.length; i++) {
  208. if (clientsList[targetName][key].Items[i].name == name) {
  209. strArray.push('<div class="plain-cell no-print">' + clientsList[targetName][key].Items[i].quantity + '</div>');
  210. countRowItems += Number(clientsList[targetName][key].Items[i].quantity);
  211. break;
  212. }
  213. if (i == clientsList[targetName][key].Items.length - 1) {
  214. strArray.push('<div class="plain-cell no-print">&nbsp;</div>');
  215. }
  216. }
  217. iterationCounter++;
  218. }
  219. var quantitydifference = Number(store[name].quantity) - Number(store[name].blocked) - countRowItems;
  220. strArray.push('<div class="plain-cell">' + countRowItems + '</div><div class="plain-cell">' + store[name].quantity + '</div>' +
  221. '<div class="plain-cell">' + store[name].blocked + '</div><div class="plain-cell">' + quantitydifference + '</div>');
  222. allItems += countRowItems;
  223. strArray.push('</div>');
  224. countRowItems = 0;
  225. }
  226. 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>')
  227. 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>')
  228. docsWrapper.innerHTML = invoiceNumber.join('') + invoiceDate.join('');
  229. table.innerHTML = strArray.join('');
  230. printTotalQuantity(clientsList[targetName], 2);
  231. }
  232.  
  233. function printTotalQuantity(obj, check) {
  234. var strArray = ['<div class="table-row"><div class="plain-cell">Общо</div><div class="plain-cell">&nbsp;</div>'],
  235. docQuantity = 0,
  236. allItems = 0;
  237.  
  238.  
  239. if (check === 1) {
  240. for (var i in clientsList) {
  241. getQuantity(clientsList[i], true);
  242. }
  243. }
  244. else if (check === 2) {
  245. getQuantity(obj, true)
  246. }
  247.  
  248. else if (check === 3) {
  249. for (var z in obj) {
  250. getQuantity(obj[z], false)
  251. }
  252. }
  253.  
  254. function getQuantity(obj, allDocs) {
  255. for (var prop in obj) {
  256. for (var h = 0; h < obj[prop].Items.length; h++) {
  257. docQuantity += Number(obj[prop].Items[h].quantity);
  258. }
  259. if (allDocs) {
  260. strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
  261. allItems += docQuantity;
  262. docQuantity = 0;
  263. }
  264. }
  265. if (!allDocs) {
  266. strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
  267. allItems += docQuantity;
  268. docQuantity = 0;
  269. }
  270. }
  271. 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>');
  272. table.innerHTML += strArray.join('');
  273. }
  274.  
  275. function extractItemsFromClient(client) {
  276. var result = {};
  277. for (var prop in client) {
  278. for (var i = 0; i < client[prop].Items.length; i++) {
  279. if (!result[client[prop].Items[i].name]) {
  280. result[client[prop].Items[i].name] = {};
  281. }
  282. }
  283. }
  284. return result;
  285. }
  286.  
  287. document.getElementById('show-reference').addEventListener('click', checkIfDatesInputAreActive);
  288.  
  289. document.getElementById('firstDate').addEventListener('change', fillSelectWithClients);
  290.  
  291. document.getElementById('secDate').addEventListener('change', fillSelectWithClients);
  292.  
  293. function fillSelectWithClients() {
  294. if (firstDateInput.value && secDateInput.value) {
  295. clientSelect.innerHTML = '<option>Всички</option>'
  296. itemsList = {};
  297. clientsList = {};
  298. generateRef(json);
  299.  
  300. for (var name in clientsList) {
  301. clientSelect.innerHTML += '<option>' + name + '</option';
  302. }
  303. }
  304. }
  305.  
  306. function changeHeaderAfterClick(name) {
  307. 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>' +
  308. '<div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>';
  309. fillTableAfterClick(name);
  310. tableHeader.childNodes[0].firstChild.innerText = 'Всички';
  311. }
  312.  
  313. function checkIfDatesInputAreActive() {
  314. var clientSelectValue = clientSelect.options[clientSelect.selectedIndex].innerText;
  315. if (firstDateInput.value && secDateInput.value) {
  316. docsWrapper.innerHTML = '';
  317. checkBox.disabled = false;
  318. if (clientSelectValue != 'Всички') {
  319. changeHeaderAfterClick(clientSelectValue);
  320. fillTableAfterClick(clientSelectValue);
  321. if (!checkBox.checked) {
  322. checkBox.click();
  323. }
  324. resizeConteiners(docsWrapper.children[0].childElementCount * 80 + 140)
  325. return;
  326. }
  327.  
  328. clientsList = {};
  329. itemsList = {};
  330.  
  331. generateRef(json);
  332. if (!checkBox.checked) {
  333. checkBox.click();
  334. }
  335. if (clientsWithDocsBtn.checked) {
  336. if (clientsWithDocsBtn.disabled) {
  337. clientsWithDocsBtn.disabled = false;
  338. }
  339. clientsWithDocsBtn.click();
  340. }
  341. buildTable();
  342. }
  343. }
  344.  
  345. function checkIfDateIsValid(day, month, year) {
  346. var firstDateInput = document.getElementById('firstDate'),
  347. secDateInput = document.getElementById('secDate'),
  348. regEx = /[0-9]+/g,
  349. firstDateArray,
  350. secDateArray;
  351.  
  352. firstDateArray = firstDateInput.value.match(regEx);
  353. secDateArray = secDateInput.value.match(regEx);
  354. if (firstDateArray && secDateArray) {
  355. var prevDate = new Date(firstDateArray[0], (firstDateArray[1] - 1), firstDateArray[2], 0, 0, 0, 0);
  356. var currentDate = new Date(secDateArray[0], (secDateArray[1] - 1), secDateArray[2], 0, 0, 0, 0);
  357.  
  358. if (prevDate > currentDate) {
  359. return;
  360. }
  361.  
  362. var productDate = new Date(year, (Number(month) - 1), Number(day), 0, 0, 0, 0);
  363. if (productDate.valueOf() >= prevDate.valueOf() && productDate.valueOf() <= currentDate.valueOf()) {
  364. return true;
  365. } else {
  366. return false;
  367. }
  368. }
  369. }
  370.  
  371. checkBox.addEventListener('change', function () {
  372. if (!this.checked) {
  373. document.head.innerHTML += '<style>.no-print{display:none}</style>';
  374. resizeConteiners(table.offsetWidth);
  375. docsWrapper.style.display = 'none';
  376. }
  377. else {
  378. document.head.removeChild(document.getElementsByTagName('style')[0]);
  379. resizeConteiners(table.childNodes[0].childElementCount * 80 + 140);
  380. if (clientsWithDocsBtn.checked) {
  381. docsWrapper.style.display = '';
  382. } else {
  383. docsWrapper.style.display = 'none';
  384. }
  385. }
  386.  
  387. });
  388.  
  389. clientsWithDocsBtn.addEventListener('change', function () {
  390. if (this.checked) {
  391. buildTable(true)
  392. docsWrapper.style.display = '';
  393. if (!checkBox.checked) checkBox.click();
  394. }
  395. else {
  396. buildTable();
  397. resizeConteiners(table.offsetWidth);
  398. docsWrapper.style.display = 'none';
  399. }
  400. });
  401.  
  402. function resizeConteiners(size) {
  403. docsWrapper.style.width = size + 'px';
  404. mainWrapper.style.width = size + 2 + 'px';
  405. tableHeader.childNodes[0].style.width = size + 'px';
  406.  
  407. if (docsWrapper.childElementCount == 0) {
  408. return;
  409. }
  410. if (document.getElementById('empty-cell') == undefined) {
  411. return;
  412. }
  413. document.getElementById('empty-cell').style.width = (docsWrapper.offsetWidth - 700) + 'px';
  414. if (docsWrapper.children[0].children.length <= 7) {
  415. document.getElementById('empty-cell').style.display = 'none'
  416. }
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement