Advertisement
NikolayPaskulov

Untitled

Oct 16th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.51 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. table = document.getElementById('table');
  10.  
  11. //Extract all items and clients from the main JSON and put them into two associative arrays
  12. function generateRef(obj) {
  13. for (var a = 0; a < obj.length; a++) {
  14. var currentDate = obj[a].docDate;
  15. if (checkIfDateIsValid(currentDate.slice(6, currentDate.length), currentDate.slice(4, 6), currentDate.slice(0, 4))) {
  16. fillclientsList(obj[a].client, obj[a]);
  17. for (var b = 0; b < obj[a].Items.length; b++) {
  18. checkItems(obj[a].client, obj[a].Items[b]);
  19. }
  20. }
  21. }
  22. }
  23.  
  24. function checkItems(client, march) {
  25. if (itemsList[march.name]) {
  26. if (itemsList[march.name][client]) {
  27. itemsList[march.name][client] = Number(itemsList[march.name][client]) + Number(march.quantity);
  28. } else {
  29. itemsList[march.name][client] = Number(march.quantity);
  30. }
  31. } else {
  32. itemsList[march.name] = {};
  33. itemsList[march.name][client] = Number(march.quantity);
  34. }
  35. }
  36.  
  37. function fillclientsList(name, obj) {
  38. if (clientsList[name]) {
  39. if (clientsList[name][obj.docNumber]) {
  40. return;
  41. } else {
  42. clientsList[name][obj.docNumber] = {
  43. docDate: obj.docDate,
  44. Items: obj.Items
  45. };
  46. }
  47. } else {
  48. clientsList[name] = {};
  49. clientsList[name][obj.docNumber] = {
  50. docDate: obj.docDate,
  51. Items: obj.Items
  52. };
  53. }
  54. }
  55.  
  56. function buildTable() {
  57.  
  58. function fillHeader() {
  59. var strArray = [];
  60. strArray.push('<div class="table-row"><div class="plain-cell">&nbsp;</div><div class="plain-cell">No</div>');
  61. for (var key in clientsList) {
  62. strArray.push('<div class="plain-cell no-print">' + key + '</div>')
  63. }
  64. strArray.push('<div class="plain-cell">Общо планирани</div><div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>');
  65. tableHeader.innerHTML = strArray.join('');
  66. tableHeader.children[0].style.width = (tableHeader.children[0].children.length * 80 + 140) + 'px';
  67. mainWrapper.style.width = (tableHeader.offsetWidth + 2) + 'px';
  68. docsWrapper.style.width = tableHeader.offsetWidth + 'px';
  69. }
  70. fillHeader();
  71.  
  72. function fillRows() {
  73. var strArray = [],
  74. counter = 1,
  75. totalRowItems = 0,
  76. totalColItems = 0,
  77. allItems = 0;
  78.  
  79. for (var key in itemsList) {
  80. strArray.push('<div class="table-row"><div class="plain-cell">' + key + '</div><div class="plain-cell">' + counter + '</div>')
  81. for (var name in clientsList) {
  82. if (itemsList[key][name]) {
  83. strArray.push('<div class="plain-cell no-print">' + itemsList[key][name] + '</div>')
  84. totalRowItems += Number(itemsList[key][name]);
  85. } else {
  86. strArray.push('<div class="plain-cell no-print">' + 0 + '</div>')
  87. }
  88. }
  89. var calculateQuantity = store[key].quantity - store[key].blocked - totalRowItems;
  90. strArray.push('<div class="plain-cell">' + totalRowItems + '</div><div class="plain-cell">' + store[key].quantity + '</div>' +
  91. '<div class="plain-cell">' + store[key].blocked + '</div>' +
  92. '<div class="plain-cell">' + calculateQuantity + '</div></div>');
  93. counter++;
  94. totalRowItems = 0;
  95. }
  96. table.innerHTML = strArray.join('');
  97.  
  98. printTotalQuantity(clientsList, itemsList);
  99. }
  100. fillRows();
  101. }
  102.  
  103. //Visualize all documents from client chosen by click on the header
  104. tableHeader.addEventListener('click', function (e) {
  105. var target = e.target.innerText,
  106. checkBiggerWidth = 0;
  107. if (target == 'No' || target == 'Общо планирани' || target == 'Количество от склад' ||
  108. target == 'Блокирани' || target == 'Разлика' || target.trim() == '') {
  109. return;
  110. }
  111.  
  112. changeHeaderAfterClick(target);
  113. alert(1);
  114. resizeConteiners(docsWrapper.childNodes[0].childElementCount * 80 + 140)
  115.  
  116.  
  117. if (target == 'Всички') {
  118. buildTable();
  119. docsWrapper.innerHTML = '';
  120. }
  121. });
  122.  
  123. function fillTableAfterClick(targetName) {
  124. var strArray = [],
  125. invoiceNumber = [],
  126. invoiceDate = [],
  127. iterationCounter = 0,
  128. isElementFound = false,
  129. countRowItems = 0,
  130. allItems = 0;
  131.  
  132. invoiceNumber.push('<div class="table-row no-print"><div class="plain-cell">Док.ном</div><div class="plain-cell">&nbsp;</div>')
  133. invoiceDate.push('<div class="table-row no-print"><div class="plain-cell">Дата</div><div class="plain-cell">&nbsp;</div>')
  134.  
  135. var itemsArr = extractItemsFromClient(clientsList[targetName]);
  136. for (var name in itemsArr) {
  137. strArray.push('<div class="table-row"><div class="plain-cell">' + name + '</div><div class="plain-cell">&nbsp;</div>');
  138.  
  139. for (var key in clientsList[targetName]) {
  140. if (iterationCounter < Object.keys(clientsList[targetName]).length) {
  141. var currentDate = clientsList[targetName][key].docDate;
  142. invoiceNumber.push('<div class="plain-cell">' + key + '</div>')
  143. invoiceDate.push('<div class="plain-cell">' + currentDate.slice(6, currentDate.length) + '.' + currentDate.slice(4, 6) + '.' + currentDate.slice(0, 4) + '</div>')
  144. }
  145.  
  146. for (var i = 0; i < clientsList[targetName][key].Items.length; i++) {
  147. if (clientsList[targetName][key].Items[i].name == name) {
  148. strArray.push('<div class="plain-cell no-print">' + clientsList[targetName][key].Items[i].quantity + '</div>');
  149. countRowItems += Number(clientsList[targetName][key].Items[i].quantity);
  150. isElementFound = true;
  151. break;
  152. }
  153. }
  154. if (!isElementFound) {
  155. strArray.push('<div class="plain-cell no-print">&nbsp;</div>');
  156. }
  157. isElementFound = false;
  158. iterationCounter++;
  159. }
  160. var quantitydifference = Number(store[name].quantity) - Number(store[name].blocked) - countRowItems;
  161. strArray.push('<div class="plain-cell">' + countRowItems + '</div><div class="plain-cell">' + store[name].quantity + '</div>' +
  162. '<div class="plain-cell">' + store[name].blocked + '</div><div class="plain-cell">' + quantitydifference + '</div></div>');
  163. allItems += countRowItems;
  164. countRowItems = 0;
  165. }
  166.  
  167. 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>')
  168. 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>')
  169. docsWrapper.innerHTML = invoiceNumber.join('') + invoiceDate.join('');
  170. table.innerHTML = strArray.join('');
  171. printTotalQuantity(clientsList[targetName]);
  172. }
  173.  
  174. function printTotalQuantity(obj, items) {
  175. var strArray = ['<div class="table-row"><div class="plain-cell">Общо</div><div class="plain-cell">&nbsp;</div>'],
  176. docQuantity = 0,
  177. allItems = 0;
  178.  
  179. for (var prop in obj) {
  180. if (items == undefined) {
  181. for (var h = 0; h < obj[prop].Items.length; h++) {
  182. docQuantity += Number(obj[prop].Items[h].quantity);
  183. }
  184. }
  185. else {
  186. for (var z in items) {
  187. if (items[z][prop]) {
  188. docQuantity += Number(items[z][prop]);
  189. }
  190. }
  191. }
  192. strArray.push('<div class="plain-cell no-print">' + docQuantity + '</div>');
  193. allItems += docQuantity;
  194. docQuantity = 0;
  195. }
  196. strArray.push('<div class="plain-cell">' + allItems + '</div></div>');
  197. table.innerHTML += strArray.join('');
  198. }
  199.  
  200. function extractItemsFromClient(client) {
  201. var result = {};
  202. for (var prop in client) {
  203. for (var i = 0; i < client[prop].Items.length; i++) {
  204. if (!result[client[prop].Items[i].name]) {
  205. result[client[prop].Items[i].name] = {};
  206. }
  207. }
  208. }
  209. return result;
  210. }
  211.  
  212. document.getElementById('show-reference').addEventListener('click', checkIfDatesInputAreActive);
  213.  
  214. document.getElementById('firstDate').addEventListener('change', fillSelectWithClients);
  215.  
  216. document.getElementById('secDate').addEventListener('change', fillSelectWithClients);
  217.  
  218. function fillSelectWithClients() {
  219. if (firstDateInput.value && secDateInput.value) {
  220. clientSelect.innerHTML = '<option>Всички</option>'
  221. itemsList = {};
  222. clientsList = {};
  223. generateRef(json);
  224.  
  225. for (var name in clientsList) {
  226. clientSelect.innerHTML += '<option>' + name + '</option';
  227. }
  228. }
  229. }
  230.  
  231. function changeHeaderAfterClick(name) {
  232. 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>' +
  233. '<div class="plain-cell">Количество от склад</div><div class="plain-cell">Блокирани</div><div class="plain-cell">Разлика</div></div>';
  234. fillTableAfterClick(name);
  235. tableHeader.childNodes[0].firstChild.innerText = 'Всички';
  236. }
  237.  
  238. function checkIfDatesInputAreActive() {
  239. var clientSelectValue = clientSelect.options[clientSelect.selectedIndex].innerText;
  240.  
  241. if (firstDateInput.value && secDateInput.value) {
  242. docsWrapper.innerHTML = '';
  243.  
  244. if (clientSelectValue != 'Всички') {
  245. changeHeaderAfterClick(clientSelectValue);
  246. fillTableAfterClick(clientSelectValue);
  247. return;
  248. }
  249.  
  250. clientsList = {};
  251. itemsList = {};
  252.  
  253. generateRef(json);
  254. buildTable();
  255. }
  256. }
  257.  
  258. function checkIfDateIsValid(day, month, year) {
  259. var firstDateInput = document.getElementById('firstDate'),
  260. secDateInput = document.getElementById('secDate'),
  261. regEx = /[0-9]+/g,
  262. firstDateArray,
  263. secDateArray;
  264.  
  265. firstDateArray = firstDateInput.value.match(regEx);
  266. secDateArray = secDateInput.value.match(regEx);
  267. if (firstDateArray && secDateArray) {
  268. var prevDate = new Date(firstDateArray[0], (firstDateArray[1] - 1), firstDateArray[2], 0, 0, 0, 0);
  269. var currentDate = new Date(secDateArray[0], (secDateArray[1] - 1), secDateArray[2], 0, 0, 0, 0);
  270.  
  271. if (prevDate > currentDate) {
  272. console.log(1);
  273. return;
  274. }
  275.  
  276. var productDate = new Date(year, (Number(month) - 1), Number(day), 0, 0, 0, 0);
  277. if (productDate.valueOf() >= prevDate.valueOf() && productDate.valueOf() <= currentDate.valueOf()) {
  278. return true;
  279. } else {
  280. return false;
  281. }
  282. }
  283. }
  284.  
  285. document.getElementById('checkBox').addEventListener('change', function () {
  286. console.log(this.checked);
  287.  
  288. if (!this.checked) {
  289. document.head.innerHTML += '<style>.no-print{display:none}</style>';
  290. resizeConteiners(table.offsetWidth);
  291. }
  292. else {
  293. document.head.removeChild(document.getElementsByTagName('style')[0]);
  294. resizeConteiners(table.childNodes[0].childElementCount * 80 + 140);
  295. }
  296.  
  297. });
  298.  
  299. function resizeConteiners(size) {
  300. docsWrapper.style.width = size + 'px';
  301. mainWrapper.style.width = size + 2 + 'px';
  302. tableHeader.childNodes[0].style.width = size + 'px';
  303. if (docsWrapper.childElementCount == 0) return;
  304. document.getElementById('empty-cell').style.width = (docsWrapper.offsetWidth - 700) + 'px';
  305. if (docsWrapper.children[0].children.length <= 7) { document.getElementById('empty-cell').style.display = 'none' }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement