Advertisement
NikolayPaskulov

Untitled

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