Advertisement
NikolayPaskulov

Untitled

Nov 27th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. var requests = 0,
  2. requestMonthSorted = {};
  3.  
  4. document.getElementById('start-procedura').onclick = function () {
  5. var fromDate = document.getElementById('procedura-from-date').innerHTML,
  6. toDate = document.getElementById('procedura-to-date').innerHTML,
  7. toDateAsDate = new Date(toDate.slice(0, 4), Number(toDate.slice(4)), 0, 0, 0, 0, 0, 0),
  8. month = (String(toDateAsDate.getMonth()).length == 1) ? '0' + (toDateAsDate.getMonth() + 1) : String(toDateAsDate.getMonth() + 1),
  9. day = (String(toDateAsDate.getDate()).length == 1) ? '0' + toDateAsDate.getDate() : String(toDateAsDate.getDate());
  10.  
  11. document.getElementById('col-wrapper').innerHTML = '';
  12.  
  13. var allRequests = ['{"doc":"-Ф"}', '{"doc":"-ФЕ"}', '{"doc":"-П"}', '{"doc":"-ФИ"}', '{"doc":"-ФИЕ"}', '{"doc":"-КП"}', '{"doc":"-НА"}', '{"doc":"+Ф"}', '{"doc":"+ФЕ"}', '{"doc":"-П"}', '{"doc":"+МД"}'];
  14. console.time('Изпращане на заявките')
  15. for (var i = 0; i < allRequests.length; i++) {
  16. sendRequest(["documents", "ЛЕЙДИ СОФИЯ АД", fromDate + '01', toDateAsDate.getFullYear() + month + day, "", "forward", allRequests[i], "false"], false, function (pokupki, prodagbi) {
  17. console.timeEnd('Изпращане на заявките')
  18. console.time('Обработка на заявките');
  19. calculateDataToMonths(pokupki, prodagbi, fromDate, toDate);
  20. console.timeEnd('Обработка на заявките');
  21. });
  22. }
  23.  
  24. };
  25.  
  26. function calculateDataToMonths(pokupki, prodagbi, startDate, endDate) {
  27. var proceduraData = {};
  28. proceduraData['pokupki'] = {};
  29. proceduraData['prodagbi'] = {};
  30.  
  31. for (var a = 0; a < pokupki.length; a++) {
  32. var month = pokupki[a].date.slice(4, 6),
  33. year = pokupki[a].date.slice(0, 4);
  34.  
  35. if (year == startDate.slice(0, 4) || year == endDate.slice(0, 4)) {
  36. if (proceduraData.pokupki[month]) {
  37. proceduraData.pokupki[month].push(pokupki[a]);
  38. } else {
  39. proceduraData.pokupki[month] = [];
  40. proceduraData.pokupki[month].push(pokupki[a]);
  41. }
  42. }
  43. }
  44.  
  45. for (var a = 0; a < prodagbi.length; a++) {
  46. var month = prodagbi[a].date.slice(4, 6),
  47. year = prodagbi[a].date.slice(0, 4);
  48. if (year == startDate.slice(0, 4) || year == endDate.slice(0, 4)) {
  49. if (proceduraData.prodagbi[month]) {
  50. proceduraData.prodagbi[month].push(prodagbi[a]);
  51. } else {
  52. proceduraData.prodagbi[month] = [];
  53. proceduraData.prodagbi[month].push(prodagbi[a]);
  54. }
  55. }
  56. }
  57.  
  58. calculateDeclarationData(proceduraData, startDate, endDate);
  59. }
  60.  
  61. function calculateDeclarationData(proceduraData, startDate, endDate) {
  62. var posiblePeriods = calculatePosiblePeriods(startDate.slice(4,6), endDate.slice(4,6)),
  63. pokupkiFooter = [0, 0, 0, 0, 0, 0, 0],
  64. prodagbiFooter = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  65.  
  66. for (var i = 0; i < posiblePeriods.length; i++) {
  67. var pokupkiForPer = proceduraData.pokupki[posiblePeriods[i]],
  68. prodagbiForPer = proceduraData.prodagbi[posiblePeriods[i]];
  69.  
  70. if (pokupkiForPer) {
  71. for (var a = 0; a < pokupkiForPer.length; a++) {
  72. var currentDoc = calculatePokupkiInput(0, pokupkiForPer[a].header);
  73. pokupkiFooter[0] = (currentDoc[9]) ? pokupkiFooter[0] + Number(currentDoc[9]) : pokupkiFooter[0];
  74. pokupkiFooter[1] = (currentDoc[10]) ? pokupkiFooter[1] + Number(currentDoc[10]) : pokupkiFooter[1];
  75. pokupkiFooter[2] = (currentDoc[11]) ? pokupkiFooter[2] + Number(currentDoc[11]) : pokupkiFooter[2];
  76. pokupkiFooter[3] = (currentDoc[12]) ? pokupkiFooter[3] + Number(currentDoc[12]) : pokupkiFooter[3];
  77. pokupkiFooter[4] = (currentDoc[13]) ? pokupkiFooter[4] + Number(currentDoc[13]) : pokupkiFooter[4];
  78. pokupkiFooter[5] = (currentDoc[14]) ? pokupkiFooter[5] + Number(currentDoc[14]) : pokupkiFooter[5];
  79. pokupkiFooter[6] = (currentDoc[15]) ? pokupkiFooter[6] + Number(currentDoc[15]) : pokupkiFooter[6];
  80. }
  81.  
  82. }
  83. if (prodagbiForPer) {
  84. for (var b = 0; b < prodagbiForPer.length; b++) {
  85. var currentDoc = calculateInputDocuments(prodagbiForPer[b]);
  86. prodagbiFooter[1] = (currentDoc[9]) ? prodagbiFooter[1] + Number(currentDoc[9]) : prodagbiFooter[1];
  87. prodagbiFooter[2] = (currentDoc[10]) ? prodagbiFooter[2] + Number(currentDoc[10]) : prodagbiFooter[2];
  88. prodagbiFooter[3] = (currentDoc[11]) ? prodagbiFooter[3] + Number(currentDoc[11]) : prodagbiFooter[3];
  89. prodagbiFooter[4] = (currentDoc[12]) ? prodagbiFooter[4] + Number(currentDoc[12]) : prodagbiFooter[4];
  90. prodagbiFooter[5] = (currentDoc[13]) ? prodagbiFooter[5] + Number(currentDoc[13]) : prodagbiFooter[5];
  91. prodagbiFooter[6] = (currentDoc[14]) ? prodagbiFooter[6] + Number(currentDoc[14]) : prodagbiFooter[6];
  92. prodagbiFooter[7] = (currentDoc[15]) ? prodagbiFooter[7] + Number(currentDoc[15]) : prodagbiFooter[7];
  93. prodagbiFooter[8] = (currentDoc[16]) ? prodagbiFooter[8] + Number(currentDoc[16]) : prodagbiFooter[8];
  94. prodagbiFooter[9] = (currentDoc[17]) ? prodagbiFooter[9] + Number(currentDoc[17]) : prodagbiFooter[9];
  95. prodagbiFooter[10] = (currentDoc[18]) ? prodagbiFooter[10] + Number(currentDoc[18]) : prodagbiFooter[10];
  96. prodagbiFooter[11] = (currentDoc[19]) ? prodagbiFooter[11] + Number(currentDoc[19]) : prodagbiFooter[11];
  97. prodagbiFooter[12] = (currentDoc[20]) ? prodagbiFooter[12] + Number(currentDoc[20]) : prodagbiFooter[12];
  98. prodagbiFooter[13] = (currentDoc[21]) ? prodagbiFooter[13] + Number(currentDoc[21]) : prodagbiFooter[13];
  99. prodagbiFooter[14] = (currentDoc[22]) ? prodagbiFooter[14] + Number(currentDoc[22]) : prodagbiFooter[14];
  100. prodagbiFooter[15] = (currentDoc[23]) ? prodagbiFooter[15] + Number(currentDoc[23]) : prodagbiFooter[15];
  101. prodagbiFooter[16] = (currentDoc[24]) ? prodagbiFooter[16] + Number(currentDoc[24]) : prodagbiFooter[16];
  102. prodagbiFooter[17] = (currentDoc[25]) ? prodagbiFooter[17] + Number(currentDoc[25]) : prodagbiFooter[17];
  103. }
  104. }
  105.  
  106. startDeclaration('11111111', prodagbiFooter, pokupkiFooter, function (obj) {
  107. buildProceduraCol(obj, posiblePeriods[i]);
  108. });
  109.  
  110. pokupkiFooter = [0, 0, 0, 0, 0, 0, 0];
  111. prodagbiFooter = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  112. }
  113. }
  114.  
  115. function calculatePosiblePeriods(startMonth, endMonth) {
  116. var arr = [],
  117. counter = 0;
  118. for (var i = Number(startMonth) ; i <= Number(endMonth) ; i++) {
  119. var month = (String(Number(startMonth) + counter).length == 1) ? '0' + (Number(startMonth) + counter) : String(Number(startMonth) + counter);
  120. arr.push(month);
  121. counter++;
  122. }
  123. return arr;
  124. }
  125.  
  126. function buildProceduraCol(obj, month) {
  127. var colWrapper = document.getElementById('col-wrapper'),
  128. months = { '01': 'Януари', '02': 'Февруари', '03': 'Март', '04': 'Април', '05': 'Май', '06': 'Юни', '07': 'Юли', '08': 'Август', '09': 'Септември', '10': 'Октомври', '11': 'Ноември', '12': 'Декември' };
  129.  
  130. var str = '<div class="procedura-col"><div class="procedura-col-cell">' + months[month] + '</div><div class="procedura-col-cell">&nbsp;</div><div class="procedura-col-cell">' + obj['01'] + '</div>' +
  131. '<div class="procedura-col-cell">' + obj['20'] + '</div><div class="procedura-col-cell">' + obj['11'] + '</div><div class="procedura-col-cell">' + obj['21'] + '</div><div class="procedura-col-cell">' + obj['12'] + '</div>' +
  132. '<div class="procedura-col-cell">' + obj['22'] + '</div><div class="procedura-col-cell">' + obj['23'] + '</div><div class="procedura-col-cell">' + obj['13'] + '</div><div class="procedura-col-cell">' + obj['24'] + '</div>' +
  133. '<div class="procedura-col-cell">' + obj['14'] + '</div><div class="procedura-col-cell">' + obj['15'] + '</div><div class="procedura-col-cell">' + obj['16'] + '</div><div class="procedura-col-cell">' + obj['17'] + '</div>' +
  134. '<div class="procedura-col-cell">' + obj['18'] + '</div><div class="procedura-col-cell">' + obj['19'] + '</div><div class="procedura-col-cell">' + obj['30'] + '</div><div class="procedura-col-cell">' + obj['31'] + '</div>' +
  135. '<div class="procedura-col-cell">' + obj['41'] + '</div><div class="procedura-col-cell">' + obj['32'] + '</div><div class="procedura-col-cell">' + obj['42'] + '</div><div class="procedura-col-cell">' + obj['43'] + '</div>' +
  136. '<div class="procedura-col-cell">' + obj['33'] + '</div><div class="procedura-col-cell">' + obj['40'] + '</div><div class="procedura-col-cell">' + obj['50'] + '</div><div class="procedura-col-cell">' + obj['60'] + '</div>' +
  137. '<div class="procedura-col-cell">' + obj['70'] + '</div><div class="procedura-col-cell">' + obj['71'] + '</div></div>';
  138.  
  139. colWrapper.innerHTML += str;
  140. }
  141.  
  142.  
  143. function calculatePokupkiInput(rowCounter, doc, ID, date) {
  144. var arr = [],
  145. type = doc.type,
  146. sumWithoutVat = ((Number(doc.sum) - ((doc.vat) ? Number(doc.vat) : 0))).toFixed(2) || '&nbsp;',
  147. client = doc.partner.slice(doc.partner.lastIndexOf(':') + 1),
  148. vat = Number(doc.vat).toFixed(2) || '&nbsp;';
  149.  
  150. arr[0] = rowCounter;
  151. arr[1] = '9999';
  152. arr[2] = (doc.НОММД && doc.НОММД != 0 && doc.НОММД != '') ? doc.НОММД : doc.ВИДДОК;
  153. arr[3] = (doc.НОММД && doc.НОММД != 0 && doc.НОММД != '') ? doc.НОММД : doc.docNumber;
  154. arr[4] = date;
  155. arr[5] = (ID != '') ? ID : '999999999999999'; // ако няма се поставят 999999999999999;
  156. arr[6] = client;
  157. arr[7] = doc.comment || 'Покупки';
  158. arr[8] = (type == 51) ? '01' : (type == 52) ? '02' : '';
  159. arr[9] = (type == 3 || type == 18 || type == 21 || type == 43) ? sumWithoutVat : (type == 35 || type == 36) ? doc.СУМАБДК : '';
  160. arr[10] = (type == 1 || type == 17 || type == 20 || type == 44 || type == 45) ? sumWithoutVat : (type == 35 || type == 36) ? doc.СУМАДК : '';
  161. arr[11] = (type == 1 || type == 17 || type == 20 || type == 44 || type == 45) ? vat : (type == 35 || type == 36) ? (doc.ДДСДК != 0) ? doc.ДДСДК : doc.СУМАДК * 0.2 : '';
  162. arr[12] = (type == 35 || type == 36) ? doc.СУМАЧДК : '';
  163. arr[13] = (type == 35 || type == 36) ? (doc.ДДСДК != 0) ? doc.ДДСДК : doc.СУМАЧДК * 0.2 : '';
  164. arr[14] = (type == 33 || type == 34) ? vat : '';
  165. arr[15] = (type == 4 || type == 38) ? doc.sum : '';
  166.  
  167. return arr;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement