Advertisement
NikolayPaskulov

Untitled

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