rpt_gather_memory.js 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('../lib/ledger');
  10. const PayCalculator = require('../lib/pay_calc');
  11. const auditConst = require('../const/audit');
  12. const payConst = require('../const/deal_pay');
  13. const changeConst = require('../const/change');
  14. const moment = require('moment');
  15. const indexPre = 'id_';
  16. const normalPrefix = 't_', sumPrefix = 's_', specPrefix = 'ts_';
  17. const gatherUtils = {
  18. completeGatherData: function (datas, completeDatas) {
  19. for (const data of datas) {
  20. for (const cd of completeDatas) {
  21. data[cd.prefix + 'id'] = cd.id;
  22. data[cd.prefix + 'name'] = cd.name;
  23. data[cd.prefix + 'category'] = cd.category;
  24. }
  25. }
  26. },
  27. gatherStage: function (tender, gatherNode, sourceNode, prefix, helper) {
  28. gatherNode[prefix + 'id'] = tender.id;
  29. gatherNode[prefix + 'name'] = tender.name;
  30. gatherNode[prefix + 'category'] = tender.category;
  31. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  32. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  33. gatherNode[prefix + "deal_qty"] = helper.add(gatherNode[prefix + "deal_qty"], sourceNode.deal_qty);
  34. gatherNode[prefix + "deal_tp"] = helper.add(gatherNode[prefix + "deal_tp"], sourceNode.deal_tp);
  35. gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);
  36. gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);
  37. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  38. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  39. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  40. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  41. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  42. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  43. gatherNode[prefix + 'contract_pc_tp'] = helper.add(gatherNode[prefix + 'contract_pc_tp'], sourceNode.contract_pc_tp);
  44. gatherNode[prefix + 'qc_pc_tp'] = helper.add(gatherNode[prefix + 'qc_pc_tp'], sourceNode.qc_pc_tp);
  45. gatherNode[prefix + 'pc_tp'] = helper.add(gatherNode[prefix + 'pc_tp'], sourceNode.pc_tp);
  46. gatherNode[prefix + "pre_contract_qty"] = helper.add(gatherNode[prefix + "pre_contract_qty"], sourceNode.pre_contract_qty);
  47. gatherNode[prefix + "pre_contract_tp"] = helper.add(gatherNode[prefix + "pre_contract_tp"], sourceNode.pre_contract_tp);
  48. gatherNode[prefix + "pre_qc_qty"] = helper.add(gatherNode[prefix + "pre_qc_qty"], sourceNode.pre_qc_qty);
  49. gatherNode[prefix + "pre_qc_tp"] = helper.add(gatherNode[prefix + "pre_qc_tp"], sourceNode.pre_qc_tp);
  50. gatherNode[prefix + "pre_gather_qty"] = helper.add(gatherNode[prefix + "pre_gather_qty"], sourceNode.pre_gather_qty);
  51. gatherNode[prefix + "pre_gather_tp"] = helper.add(gatherNode[prefix + "pre_gather_tp"], sourceNode.pre_gather_tp);
  52. gatherNode[prefix + "end_contract_qty"] = helper.add(gatherNode[prefix + "end_contract_qty"], sourceNode.end_contract_qty);
  53. gatherNode[prefix + "end_contract_tp"] = helper.add(gatherNode[prefix + "end_contract_tp"], sourceNode.end_contract_tp);
  54. gatherNode[prefix + "end_qc_qty"] = helper.add(gatherNode[prefix + "end_qc_qty"], sourceNode.end_qc_qty);
  55. gatherNode[prefix + "end_qc_tp"] = helper.add(gatherNode[prefix + "end_qc_tp"], sourceNode.end_qc_tp);
  56. gatherNode[prefix + "end_gather_qty"] = helper.add(gatherNode[prefix + "end_gather_qty"], sourceNode.end_gather_qty);
  57. gatherNode[prefix + "end_gather_tp"] = helper.add(gatherNode[prefix + "end_gather_tp"], sourceNode.end_gather_tp);
  58. gatherNode[prefix + "deal_dgn_qty1"] = helper.add(gatherNode[prefix + "deal_dgn_qty1"], sourceNode.deal_dgn_qty1);
  59. gatherNode[prefix + "deal_dgn_qty2"] = helper.add(gatherNode[prefix + "deal_dgn_qty2"], sourceNode.deal_dgn_qty2);
  60. gatherNode[prefix + "c_dgn_qty1"] = helper.add(gatherNode[prefix + "c_dgn_qty1"], sourceNode.c_dgn_qty1);
  61. gatherNode[prefix + "c_dgn_qty2"] = helper.add(gatherNode[prefix + "c_dgn_qty2"], sourceNode.c_dgn_qty2);
  62. gatherNode[prefix + "due_qc_qty"] = helper.add(gatherNode[prefix + "due_qc_qty"], sourceNode.due_qc_qty);
  63. gatherNode[prefix + "due_qc_tp"] = helper.add(gatherNode[prefix + "due_qc_tp"], sourceNode.due_qc_tp);
  64. gatherNode[prefix + "due_qc_minus_qty"] = helper.add(gatherNode[prefix + "due_qc_minus_qty"], sourceNode.due_qc_minus_qty);
  65. gatherNode[prefix + "due_final_qty"] = helper.add(gatherNode[prefix + "due_final_qty"], sourceNode.due_final_qty);
  66. gatherNode[prefix + "due_final_tp"] = helper.add(gatherNode[prefix + "due_final_tp"], sourceNode.due_final_tp);
  67. gatherNode.s_qty = helper.add(gatherNode.s_qty, sourceNode.quantity);
  68. gatherNode.s_tp = helper.add(gatherNode.s_tp, sourceNode.total_price);
  69. gatherNode.s_deal_qty = helper.add(gatherNode.s_deal_qty, sourceNode.deal_qty);
  70. gatherNode.s_deal_tp = helper.add(gatherNode.s_deal_tp, sourceNode.deal_tp);
  71. gatherNode.s_dgn_qty1 = helper.add(gatherNode.s_dgn_qty1, sourceNode.dgn_qty1);
  72. gatherNode.s_dgn_qty2 = helper.add(gatherNode.s_dgn_qty2, sourceNode.dgn_qty2);
  73. gatherNode.s_contract_qty = helper.add(gatherNode.s_contract_qty, sourceNode.contract_qty);
  74. gatherNode.s_contract_tp = helper.add(gatherNode.s_contract_tp, sourceNode.contract_tp);
  75. gatherNode.s_qc_qty = helper.add(gatherNode.s_qc_qty, sourceNode.qc_qty);
  76. gatherNode.s_qc_tp = helper.add(gatherNode.s_qc_tp, sourceNode.qc_tp);
  77. gatherNode.s_gather_qty = helper.add(gatherNode.s_gather_qty, sourceNode.gather_qty);
  78. gatherNode.s_gather_tp = helper.add(gatherNode.s_gather_tp, sourceNode.gather_tp);
  79. gatherNode.s_contract_pc_tp = helper.add(gatherNode.s_contract_pc_tp, sourceNode.contract_pc_tp);
  80. gatherNode.s_qc_pc_tp = helper.add(gatherNode.s_qc_pc_tp, sourceNode.qc_pc_tp);
  81. gatherNode.s_pc_tp = helper.add(gatherNode.s_pc_tp, sourceNode.pc_tp);
  82. gatherNode.s_pre_contract_qty = helper.add(gatherNode.s_pre_contract_qty, sourceNode.pre_contract_qty);
  83. gatherNode.s_pre_contract_tp = helper.add(gatherNode.s_pre_contract_tp, sourceNode.pre_contract_tp);
  84. gatherNode.s_pre_qc_qty = helper.add(gatherNode.s_pre_qc_qty, sourceNode.pre_qc_qty);
  85. gatherNode.s_pre_qc_tp = helper.add(gatherNode.s_pre_qc_tp, sourceNode.pre_qc_tp);
  86. gatherNode.s_pre_gather_qty = helper.add(gatherNode.s_pre_gather_qty, sourceNode.pre_gather_qty);
  87. gatherNode.s_pre_gather_tp = helper.add(gatherNode.s_pre_gather_tp, sourceNode.pre_gather_tp);
  88. gatherNode.s_end_contract_qty = helper.add(gatherNode.s_end_contract_qty, sourceNode.end_contract_qty);
  89. gatherNode.s_end_contract_tp = helper.add(gatherNode.s_end_contract_tp, sourceNode.end_contract_tp);
  90. gatherNode.s_end_qc_qty = helper.add(gatherNode.s_end_qc_qty, sourceNode.end_qc_qty);
  91. gatherNode.s_end_qc_tp = helper.add(gatherNode.s_end_qc_tp, sourceNode.end_qc_tp);
  92. gatherNode.s_end_gather_qty = helper.add(gatherNode.s_end_gather_qty, sourceNode.end_gather_qty);
  93. gatherNode.s_end_gather_tp = helper.add(gatherNode.s_end_gather_tp, sourceNode.end_gather_tp);
  94. gatherNode.s_deal_dgn_qty1 = helper.add(gatherNode.s_deal_dgn_qty1, sourceNode.deal_dgn_qty1);
  95. gatherNode.s_deal_dgn_qty2 = helper.add(gatherNode.s_deal_dgn_qty2, sourceNode.deal_dgn_qty2);
  96. gatherNode.s_c_dgn_qty1 = helper.add(gatherNode.s_c_dgn_qty1, sourceNode.c_dgn_qty1);
  97. gatherNode.s_c_dgn_qty2 = helper.add(gatherNode.s_c_dgn_qty2, sourceNode.c_dgn_qty2);
  98. gatherNode.s_due_qc_qty = helper.add(gatherNode.s_due_qc_qty, sourceNode.due_qc_qty);
  99. gatherNode.s_due_qc_tp = helper.add(gatherNode.s_due_qc_tp, sourceNode.due_qc_tp);
  100. gatherNode.s_due_qc_minus_qty = helper.add(gatherNode.s_due_qc_minus_qty, sourceNode.due_qc_minus_qty);
  101. gatherNode.s_due_final_qty = helper.add(gatherNode.s_due_final_qty, sourceNode.due_final_qty);
  102. gatherNode.s_due_final_tp = helper.add(gatherNode.s_due_final_tp, sourceNode.due_final_tp);
  103. },
  104. gatherZone: function (tender, gatherNode, sourceNode, prefix, helper) {
  105. gatherNode[prefix + 'id'] = tender.id;
  106. gatherNode[prefix + 'name'] = tender.name;
  107. gatherNode[prefix + 'category'] = tender.category;
  108. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  109. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  110. gatherNode[prefix + "deal_qty"] = helper.add(gatherNode[prefix + "deal_qty"], sourceNode.deal_qty);
  111. gatherNode[prefix + "deal_tp"] = helper.add(gatherNode[prefix + "deal_tp"], sourceNode.deal_tp);
  112. gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);
  113. gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);
  114. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  115. gatherNode[prefix + "contract_tp"] = helper.add(gatherNode[prefix + "contract_tp"], sourceNode.contract_tp);
  116. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  117. gatherNode[prefix + "qc_tp"] = helper.add(gatherNode[prefix + "qc_tp"], sourceNode.qc_tp);
  118. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  119. gatherNode[prefix + "gather_tp"] = helper.add(gatherNode[prefix + "gather_tp"], sourceNode.gather_tp);
  120. gatherNode[prefix + 'pc_tp'] = helper.add(gatherNode[prefix + 'pc_tp'], sourceNode.pc_tp);
  121. gatherNode[prefix + "deal_dgn_qty1"] = helper.add(gatherNode[prefix + "deal_dgn_qty1"], sourceNode.deal_dgn_qty1);
  122. gatherNode[prefix + "deal_dgn_qty2"] = helper.add(gatherNode[prefix + "deal_dgn_qty2"], sourceNode.deal_dgn_qty2);
  123. gatherNode[prefix + "c_dgn_qty1"] = helper.add(gatherNode[prefix + "c_dgn_qty1"], sourceNode.c_dgn_qty1);
  124. gatherNode[prefix + "c_dgn_qty2"] = helper.add(gatherNode[prefix + "c_dgn_qty2"], sourceNode.c_dgn_qty2);
  125. gatherNode.s_qty = helper.add(gatherNode.s_qty, sourceNode.quantity);
  126. gatherNode.s_tp = helper.add(gatherNode.s_tp, sourceNode.total_price);
  127. gatherNode.s_deal_qty = helper.add(gatherNode.s_deal_qty, sourceNode.deal_qty);
  128. gatherNode.s_deal_tp = helper.add(gatherNode.s_deal_tp, sourceNode.deal_tp);
  129. gatherNode.s_contract_qty = helper.add(gatherNode.s_contract_qty, sourceNode.contract_qty);
  130. gatherNode.s_contract_tp = helper.add(gatherNode.s_contract_tp, sourceNode.contract_tp);
  131. gatherNode.s_qc_qty = helper.add(gatherNode.s_qc_qty, sourceNode.qc_qty);
  132. gatherNode.s_qc_tp = helper.add(gatherNode.s_qc_tp, sourceNode.qc_tp);
  133. gatherNode.s_gather_qty = helper.add(gatherNode.s_gather_qty, sourceNode.gather_qty);
  134. gatherNode.s_gather_tp = helper.add(gatherNode.s_gather_tp, sourceNode.gather_tp);
  135. gatherNode.s_pc_tp = helper.add(gatherNode.s_pc_tp, sourceNode.pc_tp);
  136. },
  137. gatherLedger: function (tender, gatherNode, sourceNode, prefix, helper) {
  138. gatherNode[prefix + 'id'] = tender.id;
  139. gatherNode[prefix + 'name'] = tender.name;
  140. gatherNode[prefix + 'category'] = tender.category;
  141. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  142. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  143. gatherNode[prefix + "deal_qty"] = helper.add(gatherNode[prefix + "deal_qty"], sourceNode.deal_qty);
  144. gatherNode[prefix + "deal_tp"] = helper.add(gatherNode[prefix + "deal_tp"], sourceNode.deal_tp);
  145. gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);
  146. gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);
  147. gatherNode.s_qty = helper.add(gatherNode.s_qty, sourceNode.quantity);
  148. gatherNode.s_tp = helper.add(gatherNode.s_tp, sourceNode.total_price);
  149. gatherNode.s_deal_qty = helper.add(gatherNode.s_deal_qty, sourceNode.deal_qty);
  150. gatherNode.s_deal_tp = helper.add(gatherNode.s_deal_tp, sourceNode.deal_tp);
  151. },
  152. gatherSpecial: function (gatherNode, sourceNode, prefix, helper) {
  153. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  154. gatherNode[prefix + "tp"] = helper.add(gatherNode[prefix + "tp"], sourceNode.total_price);
  155. gatherNode[prefix + "dgn_qty1"] = helper.add(gatherNode[prefix + "dgn_qty1"], sourceNode.dgn_qty1);
  156. gatherNode[prefix + "dgn_qty2"] = helper.add(gatherNode[prefix + "dgn_qty2"], sourceNode.dgn_qty2);
  157. },
  158. gatherStagePos: function (tender, gatherNode, sourceNode, prefix, helper) {
  159. gatherNode[prefix + 'id'] = tender.id;
  160. gatherNode[prefix + 'name'] = tender.name;
  161. gatherNode[prefix + 'category'] = tender.category;
  162. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  163. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  164. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  165. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  166. gatherNode[prefix + "pre_contract_qty"] = helper.add(gatherNode[prefix + "pre_contract_qty"], sourceNode.pre_contract_qty);
  167. gatherNode[prefix + "pre_qc_qty"] = helper.add(gatherNode[prefix + "pre_qc_qty"], sourceNode.pre_qc_qty);
  168. gatherNode[prefix + "pre_gather_qty"] = helper.add(gatherNode[prefix + "pre_gather_qty"], sourceNode.pre_gather_qty);
  169. gatherNode[prefix + "end_contract_qty"] = helper.add(gatherNode[prefix + "end_contract_qty"], sourceNode.end_contract_qty);
  170. gatherNode[prefix + "end_qc_qty"] = helper.add(gatherNode[prefix + "end_qc_qty"], sourceNode.end_qc_qty);
  171. gatherNode[prefix + "end_gather_qty"] = helper.add(gatherNode[prefix + "end_gather_qty"], sourceNode.end_gather_qty);
  172. gatherNode[prefix + "due_qc_qty"] = helper.add(gatherNode[prefix + "due_qc_qty"], sourceNode.due_qc_qty);
  173. gatherNode[prefix + "due_final_qty"] = helper.add(gatherNode[prefix + "due_final_qty"], sourceNode.due_final_qty);
  174. gatherNode[prefix + "due_qc_minus_qty"] = helper.add(gatherNode[prefix + "due_qc_minus_qty"], sourceNode.due_qc_minus_qty);
  175. gatherNode.s_qty = helper.add(gatherNode.s_qty, sourceNode.quantity);
  176. gatherNode.s_contract_qty = helper.add(gatherNode.s_contract_qty, sourceNode.contract_qty);
  177. gatherNode.s_qc_qty = helper.add(gatherNode.s_qc_qty, sourceNode.qc_qty);
  178. gatherNode.s_gather_qty = helper.add(gatherNode.s_gather_qty, sourceNode.gather_qty);
  179. gatherNode.s_pre_contract_qty = helper.add(gatherNode.s_pre_contract_qty, sourceNode.pre_contract_qty);
  180. gatherNode.s_pre_qc_qty = helper.add(gatherNode.s_pre_qc_qty, sourceNode.pre_qc_qty);
  181. gatherNode.s_pre_gather_qty = helper.add(gatherNode.s_pre_gather_qty, sourceNode.pre_gather_qty);
  182. gatherNode.s_end_contract_qty = helper.add(gatherNode.s_end_contract_qty, sourceNode.end_contract_qty);
  183. gatherNode.s_end_qc_qty = helper.add(gatherNode.s_end_qc_qty, sourceNode.end_qc_qty);
  184. gatherNode.s_end_gather_qty = helper.add(gatherNode.s_end_gather_qty, sourceNode.end_gather_qty);
  185. gatherNode.s_due_qc_qty = helper.add(gatherNode.s_due_qc_qty, sourceNode.due_qc_qty);
  186. gatherNode.s_due_final_qty = helper.add(gatherNode.s_due_final_qty, sourceNode.due_final_qty);
  187. gatherNode.s_due_qc_minus_qty = helper.add(gatherNode.s_due_qc_minus_qty, sourceNode.due_qc_minus_qty);
  188. },
  189. gatherZonePos: function (tender, gatherNode, sourceNode, prefix, helper) {
  190. gatherNode[prefix + 'id'] = tender.id;
  191. gatherNode[prefix + 'name'] = tender.name;
  192. gatherNode[prefix + 'category'] = tender.category;
  193. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  194. gatherNode[prefix + "contract_qty"] = helper.add(gatherNode[prefix + "contract_qty"], sourceNode.contract_qty);
  195. gatherNode[prefix + "qc_qty"] = helper.add(gatherNode[prefix + "qc_qty"], sourceNode.qc_qty);
  196. gatherNode[prefix + "gather_qty"] = helper.add(gatherNode[prefix + "gather_qty"], sourceNode.gather_qty);
  197. gatherNode.s_qty = helper.add(gatherNode.s_qty, sourceNode.quantity);
  198. gatherNode.s_contract_qty = helper.add(gatherNode.s_contract_qty, sourceNode.contract_qty);
  199. gatherNode.s_qc_qty = helper.add(gatherNode.s_qc_qty, sourceNode.qc_qty);
  200. gatherNode.s_gather_qty = helper.add(gatherNode.s_gather_qty, sourceNode.gather_qty);
  201. },
  202. gatherPos: function (tender, gatherNode, sourceNode, prefix, helper) {
  203. gatherNode[prefix + 'id'] = tender.id;
  204. gatherNode[prefix + 'name'] = tender.name;
  205. gatherNode[prefix + 'category'] = tender.category;
  206. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  207. gatherNode.s_qty = helper.add(gatherNode.s_qty, sourceNode.quantity);
  208. },
  209. gatherSpecialPos: function (gatherNode, sourceNode, prefix, helper) {
  210. gatherNode[prefix + "qty"] = helper.add(gatherNode[prefix + "qty"], sourceNode.quantity);
  211. },
  212. };
  213. const rmUtils = require('../lib/rm/rm_utils');
  214. module.exports = app => {
  215. class RptGatherMemory extends app.BaseService {
  216. /**
  217. * 构造函数
  218. *
  219. * @param {Object} ctx - egg全局context
  220. * @return {void}
  221. */
  222. constructor(ctx) {
  223. super(ctx);
  224. this.clearCache();
  225. }
  226. clearCache() {
  227. this.resultTree = null;
  228. this.resultPos = null;
  229. this.resultTenderInfo = [];
  230. this.resultDealPay = [];
  231. this.resultDealBills = [];
  232. this.resultStageJgcl = [];
  233. this.resultStageYjcl = [];
  234. this.resultStageBonus = [];
  235. this.resultStageOther = [];
  236. this.resultStageSafeProd = [];
  237. this.resultStageTempLand = [];
  238. }
  239. _checkFieldsExistReg(source, regStr) {
  240. const reg = new RegExp(regStr, 'igm');
  241. for (const s of source) {
  242. if (reg.test(s)) {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. async _getValidStages(tenderId, sort = 'desc') {
  249. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  250. where: { tid: tenderId },
  251. orders: [['order', sort]],
  252. });
  253. if (stages.length !== 0) {
  254. const lastStage = stages[0];
  255. if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  256. stages.splice(0, 1);
  257. }
  258. }
  259. return stages;
  260. }
  261. async _getCheckedStages(tenderId) {
  262. const stages = await this.db.select(this.ctx.service.stage.tableName, {
  263. where: { tid: tenderId },
  264. orders: [['order', 'desc']],
  265. });
  266. if (stages.length !== 0) {
  267. const lastStage = stages[0];
  268. if (lastStage.status !== auditConst.stage.status.checked) {
  269. stages.splice(0, 1);
  270. }
  271. }
  272. return stages;
  273. }
  274. _checkSpecialTender(tender, special) {
  275. if (special) {
  276. for (const spec of special) {
  277. if (tender[spec.key] === true) return spec.key;
  278. }
  279. }
  280. return '';
  281. }
  282. async _getTimeZoneStages(tender, zone) {
  283. const times = zone.split(' - ');
  284. if (times.length !== 2) throw '选择的汇总周期无效';
  285. const beginTime = moment(times[0], 'YYYY-MM');
  286. const endTime = moment(times[1], 'YYYY-MM');
  287. const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
  288. let preStage, endStage;
  289. for (const stage of stages) {
  290. const sTime = moment(stage.s_time, 'YYYY-MM');
  291. if (sTime.isBetween(beginTime, endTime, null, '[]')) {
  292. validStages.push(stage);
  293. } else if (sTime.isBefore(beginTime)) {
  294. if (!preStage || moment(preStage.s_time, 'YYYY-MM').isBefore(sTime)) preStage = stage;
  295. } else if (sTime.isAfter(endTime)) {
  296. if (!endStage || moment(endStage.s_time, 'YYYY-MM').isAfter(sTime)) endStage = stage;
  297. }
  298. }
  299. return [validStages, preStage, endStage];
  300. }
  301. async _getOrderZoneStages (tender, zone) {
  302. let [iBegin, iEnd] = zone.split(':');
  303. iBegin = this.ctx.helper._.toInteger(iBegin) || 0;
  304. iEnd = this.ctx.helper._.toInteger(iEnd) || 0;
  305. const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
  306. let preStage, endStage;
  307. for (const stage of stages) {
  308. if (stage.order < iBegin) {
  309. if (!preStage || preStage.order < stage.order) preStage = stage;
  310. } else if (stage.order > iEnd) {
  311. if (!endStage || endStage.order > stage.order) endStage = stage;
  312. } else {
  313. validStages.push(stage);
  314. }
  315. }
  316. return [validStages, preStage, endStage];
  317. }
  318. async _getCustomZoneStages(tender, zone, timeType) {
  319. const times = zone.split(' - ');
  320. if (times.length !== 2) throw '选择的汇总周期无效';
  321. const beginTime = moment(times[0], 'YYYY-MM-DD');
  322. const endTime = moment(times[1], 'YYYY-MM-DD');
  323. const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
  324. let preStage, endStage;
  325. for (const stage of stages) {
  326. let sTime;
  327. switch (timeType) {
  328. case 'final-checked':
  329. if (stage.status !== auditConst.stage.status.checked) continue;
  330. const finalAudit = await this.ctx.service.stageAudit.getLastestAuditor(stage.id, stage.times, stage.status);
  331. if (!finalAudit) continue;
  332. sTime = moment(finalAudit.end_time).format('YYYY-MM-DD');
  333. break;
  334. case 'period-end':
  335. sTime = stage.period ? (stage.period.split(' ~ ')[1] ? stage.period.split(' ~ ')[1] : stage.period) : '';
  336. break;
  337. default:
  338. throw '配置参数错误';
  339. }
  340. stage.checked_day = sTime;
  341. stage.checked_time = moment(sTime, 'YYYY-MM-DD');
  342. if (stage.checked_time.isBetween(beginTime, endTime, null, '[]')) {
  343. validStages.push(stage);
  344. } else if (stage.checked_time.isBefore(beginTime)) {
  345. if (!preStage || preStage.checked_time.isBefore(stage.checked_time)) preStage = stage;
  346. } else if (stage.checked_time.isAfter(endTime)) {
  347. if (!endStage || endStage.checked_time.isAfter(stage.checked_time)) endStage = stage;
  348. }
  349. }
  350. return [validStages, preStage, endStage];
  351. }
  352. async _getMonthStages(tender, month) {
  353. const stages = await this._getValidStages(tender.id, 'asc'), validStages = [];
  354. for (const stage of stages) {
  355. if (stage.s_time === month) validStages.push(stage);
  356. }
  357. const preStage = stages.length > 0 ? stages.find(x => { return x.order === stages[0].order - 1; }) : null;
  358. const endStage = stages.length > 0 ? stages.find(x => { return x.order === stages[stages.length - 1].order + 1; }) : null;
  359. return [validStages, preStage, endStage];
  360. }
  361. /**
  362. * 台账数据
  363. */
  364. async _gatherStagesData(completeData, tender, stages, preStage) {
  365. const resultPos = this.resultPos;
  366. const helper = this.ctx.helper;
  367. completeData.id = tender.id;
  368. completeData.name = tender.name;
  369. /**
  370. * 汇总并合并 相关数据
  371. * @param {Array} index - 主数据
  372. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  373. */
  374. const sumAssignRelaData = function (index, rela) {
  375. const loadFields = function (datas, fields, prefix, relaId) {
  376. for (const d of datas) {
  377. const key = indexPre + d[relaId];
  378. const m = index[key];
  379. if (m) {
  380. for (const f of fields) {
  381. if (d[f] !== undefined) {
  382. m[prefix + f] = helper.add(m[prefix + f], d[f]);
  383. }
  384. }
  385. }
  386. }
  387. };
  388. for (const r of rela) {
  389. loadFields(r.data, r.fields, r.prefix, r.relaId);
  390. }
  391. };
  392. const billsTree = new Ledger.billsTree(this.ctx, {
  393. id: 'ledger_id',
  394. pid: 'ledger_pid',
  395. order: 'order',
  396. level: 'level',
  397. rootId: -1,
  398. keys: ['id', 'tender_id', 'ledger_id'],
  399. stageId: 'id',
  400. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  401. calc: function (node) {
  402. if (node.children && node.children.length === 0) {
  403. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  404. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  405. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  406. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  407. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  408. }
  409. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  410. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  411. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  412. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  413. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  414. }
  415. });
  416. const billsData = await this.ctx.service.ledger.getData(tender.id);
  417. const dgnData = await this.ctx.service.stageBillsDgn.getDgnData(tender.id);
  418. for (const d of dgnData) {
  419. const l = this.ctx.helper._.find(billsData, {id: d.id});
  420. this.ctx.helper._.assignIn(l, d);
  421. }
  422. const pos = new Ledger.pos({
  423. id: 'id', ledgerId: 'lid',
  424. calc: function (node) {
  425. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  426. node.gather_tp = helper.sum([node.contract_tp, node.qc_tp, node.pc_tp]);
  427. node.end_contract_tp = helper.sum([node.pre_contract_tp, node.contract_tp, node.contract_pc_tp]);
  428. node.end_qc_tp = helper.sum([node.pre_qc_tp, node.qc_tp, node.qc_pc_tp]);
  429. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  430. },
  431. });
  432. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tender.id} });
  433. let billsIndexData = {};
  434. for (const bd of billsData) {
  435. billsIndexData[indexPre + bd.id] = bd;
  436. }
  437. let posIndexData = {};
  438. for (const p of posData) {
  439. posIndexData[indexPre + p.id] = p;
  440. }
  441. if (preStage) {
  442. await this.ctx.service.stage.doCheckStage(preStage);
  443. const endStage = await this.ctx.service.stageBillsFinal.getFinalData(tender, preStage.order);
  444. sumAssignRelaData(billsIndexData, [
  445. { data: endStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  446. ]);
  447. const endStagePos = await this.ctx.service.stagePosFinal.getFinalData(tender, preStage.order);
  448. sumAssignRelaData(posIndexData, [
  449. {data: endStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'lid'},
  450. ]);
  451. }
  452. for (const stage of stages) {
  453. await this.ctx.service.stage.doCheckStage(stage);
  454. const curStage = stage.readOnly
  455. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  456. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  457. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  458. sumAssignRelaData(billsIndexData, [
  459. {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'},
  460. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  461. ]);
  462. const curStagePos = stage.readOnly
  463. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  464. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  465. sumAssignRelaData(posIndexData, [
  466. {data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid'},
  467. ]);
  468. }
  469. billsTree.loadDatas(billsData);
  470. billsTree.calculateAll();
  471. pos.loadDatas(posData);
  472. pos.calculateAll();
  473. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  474. gatherUtils.gatherStage(tender, gatherNode, sourceNode, completeData.prefix, helper);
  475. }, function (gatherNode, sourceNode) {
  476. const posRange = pos.getLedgerPos(sourceNode.id);
  477. if (!posRange || posRange.length === 0) return;
  478. resultPos.loadGatherPos(gatherNode.id, posRange, (gatherPos, sourcePos) => {
  479. gatherUtils.gatherStagePos(tender, gatherPos, sourcePos, completeData.prefix, helper);
  480. });
  481. });
  482. }
  483. async _gatherZoneData(tender, completeData, zone) {
  484. const [stages, preStage, endStage] = await this._getTimeZoneStages(tender, zone);
  485. await this._gatherStagesData(completeData, tender, stages, preStage);
  486. }
  487. async _gatherIndexZoneData(tender, completeData, stageZone) {
  488. const [stages, preStage, endStage] = await this._getOrderZoneStages(tender, stageZone);
  489. await this._gatherStagesData(completeData, tender, stages, preStage);
  490. }
  491. async _gatherCustomZoneData(tender, completeData, zone, timeType) {
  492. const [stages, preStage, endStage] = await this._getCustomZoneStages(tender, zone, timeType);
  493. await this._gatherStagesData(completeData, tender, stages, preStage);
  494. }
  495. async _gatherMonthsData(tender, completeData, month) {
  496. const [stages, preStage, endStage] = await this._getMonthStages(tender, month);
  497. await this._gatherStagesData(completeData, tender, stages, preStage);
  498. }
  499. async _gatherStageData(completeData, tender, stage, hasPre) {
  500. const helper = this.ctx.helper;
  501. const resultPos = this.resultPos;
  502. completeData.id = tender.id;
  503. completeData.name = tender.name;
  504. const billsTree = new Ledger.billsTree(this.ctx, {
  505. id: 'ledger_id',
  506. pid: 'ledger_pid',
  507. order: 'order',
  508. level: 'level',
  509. rootId: -1,
  510. keys: ['id', 'tender_id', 'ledger_id'],
  511. stageId: 'id',
  512. calcFields: ['deal_tp', 'total_price',
  513. 'contract_tp', 'qc_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'gather_tp',
  514. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  515. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp',
  516. 'due_qc_tp', 'due_final_tp',
  517. ],
  518. calc: function (node) {
  519. if (node.children && node.children.length === 0) {
  520. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  521. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  522. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  523. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  524. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  525. node.due_final_qty = helper.add(node.quantity, node.due_qc_qty);
  526. }
  527. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  528. node.gather_tp = helper.sum([node.contract_tp, node.qc_tp, node.pc_tp]);
  529. node.end_contract_tp = helper.sum([node.pre_contract_tp, node.contract_tp, node.contract_pc_tp]);
  530. node.end_qc_tp = helper.sum([node.pre_qc_tp, node.qc_tp, node.qc_pc_tp]);
  531. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  532. node.due_final_tp = helper.add(node.total_price, node.due_qc_tp);
  533. }
  534. });
  535. const billsData = await this.ctx.service.ledger.getData(tender.id);
  536. const dgnData = await this.ctx.service.stageBillsDgn.getDgnData(tender.id);
  537. const changeBillsData = await this.ctx.service.changeAuditList.getBillsSum(tender.id);
  538. this.ctx.helper.assignRelaData(billsData, [
  539. { data: dgnData, fields: ['deal_dgn_qty1', 'deal_dgn_qty2', 'c_dgn_qty1', 'c_dgn_qty2'], prefix: '', relaId: 'id' },
  540. { data: changeBillsData, fields: ['qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'due_', relaId: 'gcl_id' },
  541. ]);
  542. const pos = new Ledger.pos({
  543. id: 'id', ledgerId: 'lid',
  544. calc: function (node) {
  545. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  546. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  547. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  548. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  549. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  550. node.due_final_qty = helper.add(node.quantity, node.due_qc_qty);
  551. },
  552. });
  553. const posData = await this.ctx.service.pos.getPosData({ tid: tender.id });
  554. const changePosData = await this.ctx.service.changeAuditList.getPosSum(tender.id);
  555. this.ctx.helper.assignRelaData(posData, [
  556. { data: changePosData, fields: ['qc_qty', 'qc_minus_qty'], prefix: 'due_', relaId: 'mx_id' },
  557. ]);
  558. if (stage) {
  559. await this.ctx.service.stage.doCheckStage(stage);
  560. const curStage = stage.readOnly
  561. ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  562. : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
  563. const preStage = hasPre && stage.preCheckedStage ? await this.ctx.service.stageBillsFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  564. const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
  565. this.ctx.helper.assignRelaData(billsData, [
  566. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  567. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  568. { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
  569. ]);
  570. const curStagePos = stage.readOnly
  571. ? await this.ctx.service.stagePos.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
  572. : await this.ctx.service.stagePos.getLastestStageData2(tender.id, stage.id);
  573. const preStagePos = hasPre && stage.preCheckedStage ? await this.ctx.service.stagePosFinal.getFinalData(tender, stage.preCheckedStage.order) : [];
  574. this.ctx.helper.assignRelaData(posData, [
  575. { data: curStagePos, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  576. { data: preStagePos, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  577. ]);
  578. }
  579. billsTree.loadDatas(billsData);
  580. billsTree.calculateAll();
  581. pos.loadDatas(posData);
  582. pos.calculateAll();
  583. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  584. gatherUtils.gatherStage(tender, gatherNode, sourceNode, completeData.prefix, helper);
  585. }, function (gatherNode, sourceNode) {
  586. const posRange = pos.getLedgerPos(sourceNode.id);
  587. if (!posRange || posRange.length === 0) return;
  588. resultPos.loadGatherPos(gatherNode.id, posRange, (gatherPos, sourcePos) => {
  589. gatherUtils.gatherStagePos(tender, gatherPos, sourcePos, completeData.prefix, helper);
  590. });
  591. });
  592. }
  593. async _gatherMonthData(tender, completeData, month, hasPre) {
  594. const stages = await this._getValidStages(tender.id);
  595. const stage = this.ctx.helper._.find(stages, {s_time: month});
  596. await this._gatherStageData(completeData, tender, stage, hasPre);
  597. }
  598. async _gatherIndexData(tender, completeData, index, hasPre) {
  599. const stages = await this._getValidStages(tender.id);
  600. const stage = this.ctx.helper._.find(stages, {order: index});
  601. await this._gatherStageData(completeData, tender, stage, hasPre);
  602. }
  603. async _gatherFinalData(tender, completeData, hasPre) {
  604. const stages = await this._getValidStages(tender.id);
  605. await this._gatherStageData(completeData, tender, stages[0], hasPre);
  606. }
  607. async _gatherCheckedFinalData(tender, completeData, hasPre) {
  608. const stages = await this._getCheckedStages(tender.id);
  609. await this._gatherStageData(completeData, tender, stages[0], hasPre);
  610. }
  611. async _loadGatherLedger(tender) {
  612. const billsTree = new Ledger.billsTree(this.ctx, {
  613. id: 'ledger_id',
  614. pid: 'ledger_pid',
  615. order: 'order',
  616. level: 'level',
  617. rootId: -1,
  618. keys: ['id', 'tender_id', 'ledger_id'],
  619. stageId: 'id',
  620. calcFields: ['deal_tp', 'total_price'],
  621. });
  622. const billsData = await this.ctx.service.ledger.getData(tender.id);
  623. billsTree.loadDatas(billsData);
  624. billsTree.calculateAll();
  625. return billsTree;
  626. }
  627. async _loadGatherPos(tender) {
  628. const pos = new Ledger.pos({ id: 'id', ledgerId: 'lid' });
  629. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tender.id } });
  630. pos.loadDatas(posData);
  631. return pos;
  632. }
  633. async _gatherLedgerData(tender, completeData) {
  634. const resultPos = this.resultPos;
  635. const helper = this.ctx.helper;
  636. const billsTree = await this._loadGatherLedger(tender);
  637. const pos = await this._loadGatherPos(tender);
  638. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  639. gatherUtils.gatherLedger(tender, gatherNode, sourceNode, completeData.prefix, helper);
  640. }, function (gatherNode, sourceNode) {
  641. const posRange = pos.getLedgerPos(sourceNode.id);
  642. if (!posRange || posRange.length === 0) return;
  643. resultPos.loadGatherPos(posRange, (gatherPos, sourcePos) => {
  644. gatherUtils.gatherPos(gatherPos, sourcePos, completeData.prefix, helper);
  645. });
  646. });
  647. }
  648. async _gatherSpecialData(tender, sKey) {
  649. const resultPos = this.resultPos;
  650. const helper = this.ctx.helper;
  651. const billsTree = await this._loadGatherLedger(tender);
  652. const pos = await this._loadGatherPos(tender);
  653. this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
  654. gatherUtils.gatherSpecial(gatherNode, sourceNode, 'ts_' + sKey + '_', helper);
  655. }, function (gatherNode, sourceNode) {
  656. const posRange = pos.getLedgerPos(sourceNode.id);
  657. if (!posRange || posRange.length === 0) return;
  658. resultPos.loadGatherPos(gatherNode.id, posRange, (gatherPos, sourcePos) => {
  659. gatherUtils.gatherSpecialPos(gatherPos, sourcePos, 'ts_' + sKey + '_', helper);
  660. });
  661. });
  662. }
  663. async _doGatherStageData(memFieldKeys, gsDefine, gsCustom) {
  664. if (this.resultTree || this.resultPos) return;
  665. if (!gsDefine || !gsDefine.enable) return;
  666. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return;
  667. this.resultTree = new Ledger.gatherTree(this.ctx, {
  668. id: 'id',
  669. pid: 'pid',
  670. order: 'order',
  671. level: 'level',
  672. rootId: -1
  673. });
  674. this.resultPos = new Ledger.gatherPos({ id: 'id', ledgerId: 'lid' });
  675. const gsSetting = JSON.parse(gsDefine.setting);
  676. let commonIndex = 0;
  677. const completeDatas = [];
  678. for (const t of gsCustom.tenders) {
  679. const specialKey = this._checkSpecialTender(t, gsSetting.special);
  680. const tender = await this.ctx.service.tender.getCheckTender(t.tid);
  681. if (specialKey === '') {
  682. const completeData = {
  683. prefix: 't_' + commonIndex + '_',
  684. };
  685. completeDatas.push(completeData);
  686. switch (gsSetting.type) {
  687. case 'month':
  688. await this._gatherMonthData(tender, completeData, gsCustom.month, gsSetting.hasPre);
  689. break;
  690. case 'months':
  691. await this._gatherMonthsData(tender, completeData, gsCustom.months);
  692. break;
  693. case 'zone':
  694. await this._gatherZoneData(tender, completeData, gsCustom.zone);
  695. break;
  696. case 'final':
  697. await this._gatherFinalData(tender, completeData, gsSetting.hasPre);
  698. break;
  699. case 'checked-final':
  700. await this._gatherCheckedFinalData(tender, completeData, gsSetting.hasPre);
  701. break;
  702. case 'ledger':
  703. await this._gatherLedgerData(tender, completeData);
  704. break;
  705. case 'stage':
  706. await this._gatherIndexData(tender, completeData, gsCustom.stage, gsSetting.hasPre);
  707. break;
  708. case 'stage-zone':
  709. await this._gatherIndexZoneData(tender, completeData, gsCustom.stage_zone);
  710. break;
  711. case 'custom-zone':
  712. await this._gatherCustomZoneData(tender, completeData, gsCustom.custom_zone, gsSetting.timeType);
  713. break;
  714. }
  715. commonIndex++;
  716. } else {
  717. await this._gatherSpecialData(tender, specialKey);
  718. }
  719. }
  720. this.resultTree.resortChildrenDefault();
  721. gatherUtils.completeGatherData(this.resultTree.nodes, completeDatas);
  722. gatherUtils.completeGatherData(this.resultPos.datas, completeDatas);
  723. }
  724. async getGatherStageBills(memFieldKeys, gsDefine, gsCustom, showLevel) {
  725. await this._doGatherStageData(memFieldKeys, gsDefine, gsCustom);
  726. return this.resultTree
  727. ? (showLevel ? this.resultTree.getDefaultDatasByLevel(this.ctx.tender.rpt_show_level) : this.resultTree.getDefaultDatas())
  728. : [];
  729. }
  730. async getGatherStagePos(memFieldKeys, gsDefine, gsCustom) {
  731. await this._doGatherStageData(memFieldKeys, gsDefine, gsCustom);
  732. return this.resultPos ? this.resultPos.getDatas() : [];
  733. }
  734. /**
  735. * 标段信息
  736. */
  737. async _getBaseTenderInfo(tender) {
  738. const info = {
  739. id: tender.id,
  740. name: tender.name,
  741. deal_tp: tender.deal_tp,
  742. tp: tender.total_price
  743. };
  744. const deal_bills_sum = await this.ctx.service.dealBills.getSum(tender.id);
  745. info.deal_bills_tp = deal_bills_sum ? deal_bills_sum.total_price : 0;
  746. if (tender.ledger_status === auditConst.ledger.status.uncheck || tender.ledger_status === auditConst.ledger.status.checkNo) {
  747. const sum = await this.ctx.service.ledger.addUp({tender_id: tender.id/*, is_leaf: true*/});
  748. info.tp = sum.total_price;
  749. info.deal_tp = sum.deal_tp;
  750. }
  751. info.deal_info = tender.info.deal_info;
  752. info.construction_unit = tender.info.construction_unit;
  753. info.tech_param = tender.info.tech_param;
  754. info.deal_param = tender.info.deal_param;
  755. info.pay_account = tender.info.pay_account;
  756. info.deal_param.contractPriceWithoutZL = this.ctx.helper.sub(info.deal_param.contractPrice,
  757. info.deal_param.zanLiePrice);
  758. info.bid_info = tender.info.bid_info;
  759. return info;
  760. }
  761. async _getStageTenderInfo(stage, info) {
  762. if (stage) {
  763. const helper = this.ctx.helper;
  764. await this.ctx.service.stage.doCheckStage(stage);
  765. await this.ctx.service.stage.checkStageGatherData(stage, this.ctx.session.sessionUser.is_admin);
  766. info.pre_contract_tp = stage.pre_contract_tp;
  767. info.pre_qc_tp = stage.pre_qc_tp;
  768. info.pre_positive_qc_tp = stage.pre_positive_qc_tp;
  769. info.pre_negative_qc_tp = stage.pre_negative_qc_tp;
  770. info.pre_gather_tp = helper.add(info.pre_contract_tp, info.pre_qc_tp);
  771. info.contract_tp = stage.contract_tp;
  772. info.qc_tp = stage.qc_tp;
  773. info.positive_qc_tp = stage.positive_qc_tp;
  774. info.positive_qc_pc_tp = stage.positive_qc_pc_tp;
  775. info.negative_qc_tp = stage.negative_qc_tp;
  776. info.negative_qc_pc_tp = stage.negative_qc_pc_tp;
  777. info.contract_pc_tp = stage.contract_pc_tp;
  778. info.qc_pc_tp = stage.qc_pc_tp;
  779. info.pc_tp = stage.pc_tp;
  780. info.gather_tp = helper.sum([info.contract_tp, info.qc_tp, info.pc_tp]);
  781. info.end_contract_tp = helper.sum([info.pre_contract_tp, info.contract_tp, info.contract_pc_tp]);
  782. info.end_qc_tp = helper.sum([info.pre_qc_tp, info.qc_tp, info.qc_pc_tp]);
  783. info.end_positive_qc_tp = helper.sum([info.pre_positive_qc_tp, info.positive_qc_tp, info.positive_qc_pc_tp]);
  784. info.end_negative_qc_tp = helper.sum([info.pre_negative_qc_tp, info.negative_qc_tp, info.negative_qc_pc_tp]);
  785. info.end_gather_tp = helper.add(info.pre_gather_tp, info.gather_tp);
  786. info.yf_tp = stage.yf_tp;
  787. info.pre_yf_tp = stage.pre_yf_tp;
  788. info.end_yf_tp = helper.add(stage.yf_tp, stage.pre_yf_tp);
  789. info.sf_tp = stage.sf_tp;
  790. info.pre_sf_tp = stage.pre_sf_tp;
  791. info.end_sf_tp = helper.add(stage.sf_tp, stage.pre_sf_tp);
  792. }
  793. }
  794. async _getStagesTenderInfo(stages, info, preStage) {
  795. const helper = this.ctx.helper;
  796. if (preStage) {
  797. info.pre_contract_tp = helper.add(preStage.contract_tp, preStage.pre_contract_tp);
  798. info.pre_qc_tp = helper.add(preStage.qc_tp, preStage.pre_qc_tp);
  799. info.pre_positive_qc_tp = helper.add(preStage.positive_qc_tp, preStage.pre_positive_qc_tp);
  800. info.pre_negative_qc_tp = helper.add(preStage.negative_qc_tp, preStage.pre_negative_qc_tp);
  801. info.pre_gather_tp = helper.add(info.pre_contract_tp, info.pre_qc_tp);
  802. info.pre_yf_tp = helper.add(preStage.yf_tp, preStage.pre_yf_tp);
  803. info.pre_sf_tp = helper.add(preStage.sf_tp, preStage.pre_sf_tp);
  804. }
  805. for (const stage of stages) {
  806. await this.ctx.service.stage.doCheckStage(stage);
  807. await this.ctx.service.stage.checkStageGatherData(stage, this.ctx.session.sessionUser.is_admin);
  808. info.contract_tp = helper.add(info.contract_tp, stage.contract_tp);
  809. info.qc_tp = helper.add(info.qc_tp, stage.qc_tp);
  810. info.positive_qc_tp = helper.add(info.positive_qc_tp, stage.positive_qc_tp);
  811. info.positive_qc_pc_tp = helper.add(info.positive_qc_tp, stage.positive_qc_pc_tp);
  812. info.negative_qc_tp = helper.add(info.negative_qc_tp, stage.negative_qc_tp);
  813. info.negative_qc_pc_tp = helper.add(info.negative_qc_pc_tp, stage.negative_qc_pc_tp);
  814. info.contract_pc_tp = helper.add(info.contract_pc_tp, stage.contract_pc_tp);
  815. info.qc_pc_tp = helper.add(info.qc_pc_tp, stage.qc_pc_tp);
  816. info.pc_tp = helper.add(info.pc_tp, stage.pc_tp);
  817. info.yf_tp = helper.add(info.yf_tp, stage.yf_tp);
  818. info.sf_tp = helper.add(info.sf_tp, stage.sf_tp);
  819. }
  820. info.gather_tp = helper.sum([info.contract_tp, info.qc_tp, info.pc_tp]);
  821. info.end_contract_tp = helper.sum([info.pre_contract_tp, info.contract_tp, info.contract_pc_tp]);
  822. info.end_qc_tp = helper.sum([info.pre_qc_tp, info.qc_tp, info.qc_pc_tp]);
  823. info.end_positive_qc_tp = helper.sum([info.pre_positive_qc_tp, info.positive_qc_tp, info.positive_qc_pc_tp]);
  824. info.end_negative_qc_tp = helper.sum([info.pre_negative_qc_tp, info.negative_qc_tp, info.negative_qc_pc_tp]);
  825. info.end_gather_tp = helper.add(info.pre_gather_tp, info.gather_tp);
  826. info.end_yf_tp = helper.add(info.pre_yf_tp, info.yf_tp);
  827. info.end_sf_tp = helper.add(info.pre_sf_tp, info.sf_tp);
  828. }
  829. async _gatherZoneTenderInfo(tender, index, zone) {
  830. const info = await this._getBaseTenderInfo(tender);
  831. const [stages, preStage, endStage] = await this._getTimeZoneStages(tender, zone);
  832. await this._getStagesTenderInfo(stages, info, preStage);
  833. this.resultTenderInfo.push(info);
  834. }
  835. async _gatherOrderZoneTenderInfo(tender, index, stageZone) {
  836. const info = await this._getBaseTenderInfo(tender);
  837. const [stages, preStage, endStage] = await this._getOrderZoneStages(tender, stageZone);
  838. await this._getStagesTenderInfo(stages, info, preStage);
  839. this.resultTenderInfo.push(info);
  840. }
  841. async _gatherCustomZoneTenderInfo(tender, index, zone, timeType) {
  842. const info = await this._getBaseTenderInfo(tender);
  843. const [stages, preStage, endStage] = await this._getCustomZoneStages(tender, zone, timeType);
  844. await this._getStagesTenderInfo(stages, info, preStage);
  845. this.resultTenderInfo.push(info);
  846. }
  847. async _gatherMonthTenderInfo(tender, index, month, hasPre) {
  848. const info = await this._getBaseTenderInfo(tender);
  849. const stages = await this._getValidStages(tender.id);
  850. const stage = this.ctx.helper._.find(stages, {s_time: month});
  851. await this._getStageTenderInfo(stage, info);
  852. this.resultTenderInfo.push(info);
  853. }
  854. async _gatherOrderTenderInfo(tender, index, order, hasPre) {
  855. const info = await this._getBaseTenderInfo(tender);
  856. const stages = await this._getValidStages(tender.id);
  857. const stage = this.ctx.helper._.find(stages, {order: order});
  858. await this._getStageTenderInfo(stage, info);
  859. this.resultTenderInfo.push(info);
  860. }
  861. async _gatherFinalTenderInfo(tender, index, hasPre) {
  862. const info = await this._getBaseTenderInfo(tender);
  863. const stages = await this._getValidStages(tender.id);
  864. await this._getStageTenderInfo(stages[0], info);
  865. this.resultTenderInfo.push(info);
  866. }
  867. async _gatherCheckedFinalTenderInfo(tender, index, hasPre) {
  868. const info = await this._getBaseTenderInfo(tender);
  869. const stages = await this._getCheckedStages(tender.id);
  870. await this._getStageTenderInfo(stages[0], info);
  871. this.resultTenderInfo.push(info);
  872. }
  873. async _gatherLedgerTenderInfo(tender, index) {
  874. const info = await this._getBaseTenderInfo(tender);
  875. this.resultTenderInfo.push(info);
  876. }
  877. async _gatherSpecialTenderInfo(tender, sKey) {
  878. const info = await this._getBaseTenderInfo(tender);
  879. info.spec = sKey;
  880. this.resultTenderInfo.push(info);
  881. }
  882. async getGatherTenderInfo(memFieldKeys, gsDefine, gsCustom) {
  883. if (!gsDefine || !gsDefine.enable) return [];
  884. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  885. this.resultTenderInfo = [];
  886. const gsSetting = JSON.parse(gsDefine.setting);
  887. let commonIndex = 0;
  888. for (const t of gsCustom.tenders) {
  889. const specialKey = this._checkSpecialTender(t, gsSetting.special);
  890. const tender = await this.ctx.service.tender.getCheckTender(t.tid);
  891. if (specialKey === '') {
  892. switch (gsSetting.type) {
  893. case 'month':
  894. await this._gatherMonthTenderInfo(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
  895. break;
  896. case 'zone':
  897. await this._gatherZoneTenderInfo(tender, commonIndex, gsCustom.zone);
  898. break;
  899. case 'final':
  900. await this._gatherFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  901. break;
  902. case 'checked-final':
  903. await this._gatherCheckedFinalTenderInfo(tender, commonIndex, gsSetting.hasPre);
  904. break;
  905. case 'ledger':
  906. await this._gatherLedgerTenderInfo(tender, commonIndex);
  907. break;
  908. case 'stage':
  909. await this._gatherOrderTenderInfo(tender, commonIndex, gsCustom.stage, gsSetting.hasPre);
  910. break;
  911. case 'stage-zone':
  912. await this._gatherOrderZoneTenderInfo(tender, commonIndex, gsCustom.stage_zone);
  913. break;
  914. case 'custom-zone':
  915. await this._gatherCustomZoneTenderInfo(tender, commonIndex, gsCustom.custom_zone, gsSetting.timeType);
  916. break;
  917. }
  918. commonIndex++;
  919. } else {
  920. await this._gatherSpecialTenderInfo(tender, specialKey);
  921. }
  922. }
  923. return this.resultTenderInfo;
  924. }
  925. /**
  926. * 合同支付
  927. */
  928. _gatherPayRecord(dealPay, fun) {
  929. let rdp;
  930. if (dealPay.ptype !== payConst.payType.normal) {
  931. rdp = this.ctx.helper._.find(this.resultDealPay, {ptype: dealPay.ptype});
  932. } else {
  933. rdp = this.ctx.helper._.find(this.resultDealPay, {
  934. name: dealPay.name,
  935. minus: dealPay.minus ? true : false,
  936. is_yf: dealPay.is_yf ? true : false,
  937. });
  938. }
  939. if (!rdp) {
  940. rdp = {
  941. ptype: dealPay.ptype,
  942. name: dealPay.name,
  943. minus: dealPay.minus ? true : false,
  944. is_yf: dealPay.is_yf ? true : false,
  945. };
  946. this.resultDealPay.push(rdp);
  947. }
  948. if (fun) fun(rdp, dealPay);
  949. }
  950. async _checkStagePayCalc(tender, stage, dealPay) {
  951. if (!stage.readOnly) {
  952. // 计算 本期金额
  953. const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
  954. await payCalculator.calculateAll(dealPay);
  955. }
  956. }
  957. async _gatherStagePay(completeData, tender, stage, hasPre) {
  958. if (stage) {
  959. completeData.id = tender.id;
  960. completeData.name = tender.name;
  961. const helper = this.ctx.helper;
  962. await this.ctx.service.stage.doCheckStage(stage);
  963. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  964. await this._checkStagePayCalc(tender, stage, dealPay);
  965. for (const dp of dealPay) {
  966. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  967. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  968. gatherData[completeData.prefix + 'id'] = tender.id;
  969. gatherData[completeData.prefix + 'name'] = tender.name;
  970. gatherData[completeData.prefix + 'tp'] = helper.add(gatherData[completeData.prefix + 'tp'], sourceData.tp);
  971. gatherData[completeData.prefix + 'pre_tp'] = helper.add(gatherData[completeData.prefix + 'pre_tp'], sourceData.pre_tp);
  972. gatherData[completeData.prefix + 'end_tp'] = helper.add(gatherData[completeData.prefix + 'end_tp'], sourceData.end_tp);
  973. gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
  974. gatherData['s_' + 'pre_tp'] = helper.add(gatherData['s_' + 'pre_tp'], sourceData.pre_tp);
  975. gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.end_tp);
  976. });
  977. }
  978. }
  979. }
  980. async _gatherStagesPay(completeData, tender, stages, preStage) {
  981. const helper = this.ctx.helper;
  982. completeData.id = tender.id;
  983. completeData.name = tender.name;
  984. if (preStage) await this.ctx.service.stage.doCheckStage(preStage);
  985. const preDealPay = preStage ? await this.ctx.service.stagePay.getStagePays(preStage) : [];
  986. for (const dp of preDealPay) {
  987. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  988. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  989. gatherData[completeData.prefix + 'id'] = tender.id;
  990. gatherData[completeData.prefix + 'name'] = tender.name;
  991. gatherData[completeData.prefix + 'pre_tp'] = sourceData.end_tp;
  992. gatherData.s_pre_tp = helper.add(gatherData.s_pre_tp, sourceData.end_tp);
  993. gatherData[completeData.prefix + 'end_tp'] = sourceData.end_tp;
  994. gatherData.s_end_tp = helper.add(gatherData.s_end_tp, sourceData.end_tp);
  995. });
  996. }
  997. for (const stage of stages) {
  998. await this.ctx.service.stage.doCheckStage(stage);
  999. const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
  1000. await this._checkStagePayCalc(tender, stage, dealPay);
  1001. for (const dp of dealPay) {
  1002. dp.end_tp = helper.add(dp.pre_tp, dp.tp);
  1003. this._gatherPayRecord(dp, function (gatherData, sourceData) {
  1004. gatherData[completeData.prefix + 'id'] = tender.id;
  1005. gatherData[completeData.prefix + 'name'] = tender.name;
  1006. gatherData[completeData.prefix + 'tp'] = helper.add(gatherData[completeData.prefix + 'tp'], sourceData.tp);
  1007. gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
  1008. gatherData[completeData.prefix + 'end_tp'] = helper.add(gatherData[completeData.prefix + 'end_tp'], sourceData.tp);
  1009. gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.tp);
  1010. });
  1011. }
  1012. }
  1013. }
  1014. async _gatherZoneStagePay(sTender, completeData, zone) {
  1015. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1016. const [stages, preStage, endStage] = await this._getTimeZoneStages(tender, zone);
  1017. await this._gatherStagesPay(completeData, tender, stages, preStage);
  1018. }
  1019. async _gatherOrderZoneStagePay(sTender, completeData, stageZone) {
  1020. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1021. const [stages, preStage, endStage] = await this._getOrderZoneStages(tender, stageZone);
  1022. await this._gatherStagesPay(completeData, tender, stages, preStage);
  1023. }
  1024. async _gatherCustomZoneStagePay(sTender, completeData, zone, timeType) {
  1025. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1026. const [stages, preStage, endStage] = await this._getCustomZoneStages(tender, zone, timeType);
  1027. await this._gatherStagesPay(completeData, tender, stages, preStage);
  1028. }
  1029. async _gatherMonthStagePay(sTender, completeData, month, hasPre) {
  1030. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1031. const stages = await this._getValidStages(tender.id);
  1032. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1033. await this._gatherStagePay(completeData, tender, stage, hasPre);
  1034. }
  1035. async _gatherOrderStagePay(sTender, completeData, order, hasPre) {
  1036. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1037. const stages = await this._getValidStages(tender.id);
  1038. const stage = this.ctx.helper._.find(stages, {order: order});
  1039. await this._gatherStagePay(completeData, tender, stage, hasPre);
  1040. }
  1041. async _gatherFinalStagePay(sTender, completeData, hasPre) {
  1042. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1043. const stages = await this._getValidStages(tender.id);
  1044. await this._gatherStagePay(completeData, tender, stages[0], hasPre);
  1045. }
  1046. async _gatherCheckedFinalStagePay(sTender, completeData, hasPre) {
  1047. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1048. const stages = await this._getCheckedStages(tender.id);
  1049. await this._gatherStagePay(completeData, tender, stages[0], hasPre);
  1050. }
  1051. async getGatherStagePay(memFieldKeys, gsDefine, gsCustom) {
  1052. if (!gsDefine || !gsDefine.enable) return [];
  1053. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1054. this.resultDealPay = [];
  1055. const gsSetting = JSON.parse(gsDefine.setting);
  1056. let commonIndex = 0;
  1057. const completeDatas = [];
  1058. for (const tender of gsCustom.tenders) {
  1059. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  1060. if (specialKey === '') {
  1061. const completeData = {
  1062. prefix: 't_' + commonIndex + '_',
  1063. };
  1064. completeDatas.push(completeData);
  1065. switch (gsSetting.type) {
  1066. case 'month':
  1067. await this._gatherMonthStagePay(tender, completeData, gsCustom.month, gsSetting.hasPre);
  1068. break;
  1069. case 'zone':
  1070. await this._gatherZoneStagePay(tender, completeData, gsCustom.zone);
  1071. break;
  1072. case 'final':
  1073. await this._gatherFinalStagePay(tender, completeData, gsSetting.hasPre);
  1074. break;
  1075. case 'checked-final':
  1076. await this._gatherCheckedFinalStagePay(tender, completeData, gsSetting.hasPre);
  1077. break;
  1078. case 'stage':
  1079. await this._gatherOrderStagePay(tender, completeData, gsCustom.stage, gsSetting.hasPre);
  1080. break;
  1081. case 'stage-zone':
  1082. await this._gatherOrderZoneStagePay(tender, completeData, gsCustom.stage_zone);
  1083. break;
  1084. case 'custom-zone':
  1085. await this._gatherCustomZoneStagePay(tender, completeData, gsCustom.custom_zone, gsSetting.timeType);
  1086. break;
  1087. }
  1088. commonIndex++;
  1089. }
  1090. // 合同支付,只有在每一期中有数据,故特殊标段直接不汇总合同支付
  1091. }
  1092. gatherUtils.completeGatherData(this.resultDealPay, completeDatas);
  1093. return this.resultDealPay;
  1094. }
  1095. /**
  1096. * 签约清单
  1097. */
  1098. async _gatherDealBills(tender, fun) {
  1099. const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({
  1100. where: { tender_id: tender.id }
  1101. });
  1102. for (const db of dealBills) {
  1103. let rdb = this.ctx.helper._.find(this.resultDealBills, {
  1104. code: db.code,
  1105. name: db.name,
  1106. unit: db.unit,
  1107. unit_price: db.unit_price ? db.unit_price: 0,
  1108. });
  1109. if (!rdb) {
  1110. rdb = {
  1111. code: db.code,
  1112. name: db.name,
  1113. unit: db.unit,
  1114. unit_price: db.unit_price ? db.unit_price: 0,
  1115. };
  1116. this.resultDealBills.push(rdb);
  1117. }
  1118. if (fun) fun(rdb, db);
  1119. }
  1120. }
  1121. async _gatherCommonDealBills(sTender, completeData) {
  1122. const self = this;
  1123. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1124. completeData.id = tender.id;
  1125. completeData.name = tender.name;
  1126. await this._gatherDealBills(tender, function (gatherData, sourceData) {
  1127. gatherData[completeData.prefix + 'id'] = tender.id;
  1128. gatherData[completeData.prefix + 'name'] = tender.name;
  1129. gatherData[completeData.prefix + 'qty'] = sourceData.quantity;
  1130. gatherData[completeData.prefix + 'tp'] = sourceData.total_price;
  1131. gatherData['s_' + 'qty'] = self.ctx.helper.add(gatherData['s_' + 'qty'], sourceData.quantity);
  1132. gatherData['s_' + 'tp'] = self.ctx.helper.add(gatherData['s_' + 'tp'], sourceData.total_price);
  1133. });
  1134. }
  1135. async _gatherSpecialDealBills(sTender, sKey) {
  1136. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1137. await this._gatherDealBills(tender, function (gatherData, sourceData) {
  1138. const prefix = 'ts_' + sKey + '_';
  1139. gatherData[prefix + 'qty'] = sourceData.quantity;
  1140. gatherData[prefix + 'tp'] = sourceData.total_price;
  1141. });
  1142. }
  1143. async getGatherDealBills(memFieldKeys, gsDefine, gsCustom) {
  1144. if (!gsDefine || !gsDefine.enable) return [];
  1145. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1146. this.resultDealBills = [];
  1147. const gsSetting = JSON.parse(gsDefine.setting);
  1148. let commonIndex = 0;
  1149. const completeDatas = [];
  1150. for (const tender of gsCustom.tenders) {
  1151. const specialKey = this._checkSpecialTender(tender, gsSetting.special);
  1152. if (specialKey === '') {
  1153. const completeData = {
  1154. prefix: 't_' + commonIndex + '_',
  1155. };
  1156. completeDatas.push(completeData);
  1157. await this._gatherCommonDealBills(tender, completeData);
  1158. commonIndex++;
  1159. } else {
  1160. await this._gatherSpecialDealBills(tender, specialKey);
  1161. }
  1162. }
  1163. gatherUtils.completeGatherData(this.resultDealBills, completeDatas);
  1164. return this.resultDealBills;
  1165. }
  1166. _getChangeConstName(define, value) {
  1167. for (const prop in define) {
  1168. if (define[prop].value === value) {
  1169. return define[prop].name;
  1170. }
  1171. }
  1172. return '';
  1173. }
  1174. async _gatherChange(tender, hasUsed) {
  1175. const self = this;
  1176. try {
  1177. const info = await this.ctx.service.tenderInfo.getTenderInfo(tender.id);
  1178. const decimal = info.decimal, ctx = this.ctx;
  1179. const where = { tid: tender.id, valid: 1 };
  1180. if (this.ctx.session.sessionProject.page_show.isOnlyChecked) where.status = auditConst.flow.status.checked;
  1181. const change = await this.ctx.service.change.getAllDataByCondition({ where, orders: [['in_time', 'desc']] });
  1182. for (const c of change) {
  1183. c.tender_name = tender.name;
  1184. const types = ctx.helper._.map(c.type.split(','), function (t) {
  1185. return self._getChangeConstName(changeConst.type, ctx.helper._.toInteger(t));
  1186. });
  1187. c.type = types.join(';');
  1188. c.class = this._getChangeConstName(changeConst.class, c.class);
  1189. c.quality = this._getChangeConstName(changeConst.quality, c.quality);
  1190. c.charge = this._getChangeConstName(changeConst.charge, c.charge);
  1191. c.attachments = await ctx.service.changeAtt.getChangeAttachment(c.cid);
  1192. const names = ctx.helper._.map(c.attachments, function (x) {
  1193. return x.filename + x.fileext;
  1194. });
  1195. c.attNames = names.join('\n');
  1196. }
  1197. const changeBills = hasUsed
  1198. ? await this.ctx.service.stageChangeFinal.getChangeBillsWithUsedQty(tender.id, this.ctx.session.sessionProject.page_show.isOnlyChecked)
  1199. : await this.ctx.service.changeAuditList.getChangeAuditBills(tender.id, this.ctx.session.sessionProject.page_show.isOnlyChecked);
  1200. for (const d of changeBills) {
  1201. d.tender_name = tender.name;
  1202. d.o_qty = d.oamount;
  1203. d.o_tp = this.ctx.helper.mul(d.o_qty, d.unit_price, decimal.tp);
  1204. d.o_qty2 = d.oamount2;
  1205. d.o_tp2 = this.ctx.helper.mul(d.o_qty2, d.unit_price, decimal.tp);
  1206. d.c_qty = d.camount;
  1207. d.c_tp = this.ctx.helper.mul(d.c_qty, d.unit_price, decimal.tp);
  1208. d.s_qty = d.samount ? parseFloat(d.samount) : 0;
  1209. d.s_tp = this.ctx.helper.mul(d.s_qty, d.unit_price, decimal.tp);
  1210. d.sp_qty = d.spamount;
  1211. d.sp_tp = this.ctx.helper.mul(d.sp_qty, d.unit_price, decimal.tp);
  1212. d.used_tp = this.ctx.helper.mul(d.used_qty, d.unit_price, decimal.tp);
  1213. d.is_used = !!d.used_qty;
  1214. const auditAmount = d.audit_amount ? d.audit_amount.split(',') : [];
  1215. const relaChange = ctx.helper._.find(change, {cid: d.cid});
  1216. d.relaChange = relaChange;
  1217. if (relaChange) {
  1218. if (d.is_used) relaChange.is_used = true;
  1219. relaChange.used_tp = ctx.helper.add(relaChange.used_tp, d.used_tp);
  1220. }
  1221. for (const [i, aa] of auditAmount.entries()) {
  1222. const amountField = 'qty_' + (i+1), tpField = 'tp_' + (i+1);
  1223. d[amountField] = aa ? parseFloat(aa) : 0;
  1224. d[tpField] = ctx.helper.mul(d[amountField], d.unit_price, decimal.tp);
  1225. if (relaChange) {
  1226. relaChange[tpField] = ctx.helper.add(relaChange[tpField], d[tpField]);
  1227. }
  1228. }
  1229. }
  1230. for (const c of change) {
  1231. c.is_used = !!c.is_used;
  1232. c.used_ratio = c.total_price ? ctx.helper.mul(ctx.helper.div(c.used_tp, c.total_price), 100) : 0;
  1233. }
  1234. for (const cb of changeBills) {
  1235. cb.is_used_bgl = cb.relaChange ? cb.relaChange.is_used : cb.is_used;
  1236. delete cb.relaChange;
  1237. }
  1238. change.sort(function (a, b) {
  1239. return a.code.localeCompare(b.code);
  1240. });
  1241. changeBills.sort(function (a, b) {
  1242. const aCIndex = change.findIndex(function (c) {
  1243. return c.cid === a.cid;
  1244. });
  1245. const bCIndex = change.findIndex(function (c) {
  1246. return c.cid === b.cid;
  1247. });
  1248. return aCIndex === bCIndex
  1249. ? ctx.helper.compareCode(a.code, b.code)
  1250. : aCIndex - bCIndex;
  1251. });
  1252. return [change, changeBills];
  1253. } catch(err) {
  1254. this.ctx.helper.log(err);
  1255. throw err;
  1256. return [[], []];
  1257. }
  1258. }
  1259. async getGatherChange(memFieldKeys, gsDefine, gsCustom) {
  1260. if (!gsDefine || !gsDefine.enable) return [];
  1261. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1262. const result = { mem_gather_change: [], mem_gather_change_bills: [] };
  1263. const hasUsed = rmUtils.checkFieldsExist(memFieldKeys, ['used_qty', 'is_used_bgl', 'is_used', 'used_ratio']);
  1264. for (const t of gsCustom.tenders) {
  1265. const tender = await this.ctx.service.tender.getCheckTender(t.tid);
  1266. const [change, changeBills] = await this._gatherChange(tender, hasUsed);
  1267. result.mem_gather_change.push(...change);
  1268. result.mem_gather_change_bills.push(...changeBills);
  1269. }
  1270. return result;
  1271. }
  1272. async _gatherStagesChange(completeData, tender, stages, preStage, gatherType = 'flow'){
  1273. const result = [];
  1274. const findR = function(source) {
  1275. let r = result.find(x => { return x.cbid === source.cbid});
  1276. if (!r) {
  1277. r = { cid: source.cid, cbid: source.cbid, qty: 0, pre_qty: 0 };
  1278. result.push(r);
  1279. }
  1280. return r;
  1281. };
  1282. const helper = this.ctx.helper;
  1283. if (preStage) {
  1284. const preStageChange = await this.ctx.service.stageChangeFinal.getEndStageData(preStage.tid, preStage.order);
  1285. if (gatherType === 'flow') {
  1286. result.push(...preStageChange);
  1287. } else {
  1288. preStageChange.forEach(sc => {
  1289. const r = findR(sc);
  1290. r.pre_qty = helper.add(r.pre_qty, sc.qty);
  1291. });
  1292. }
  1293. }
  1294. for (const stage of stages) {
  1295. let curStageChange;
  1296. if (stage.status === auditConst.stage.status.checked) {
  1297. curStageChange = await this.ctx.service.stageChangeFinal.getAllDataByCondition({ where: { tid: stage.tid, sid: stage.id } });
  1298. } else {
  1299. await this.ctx.service.stage.doCheckStage(stage);
  1300. curStageChange = await this.ctx.service.stageChange.getCurStageData(stage.tid, stage.id, stage.curTimes, stage.curOrder);
  1301. }
  1302. if (gatherType === 'flow') {
  1303. result.push(...curStageChange);
  1304. } else {
  1305. curStageChange.forEach(sc => {
  1306. const r = findR(sc);
  1307. r.qty = helper.add(r.qty, sc.qty);
  1308. });
  1309. }
  1310. }
  1311. result.forEach(r => { r.end_qty = helper.add(r.pre_qty, r.qty); });
  1312. return result;
  1313. }
  1314. async _gatherCustomZoneStageChange(sTender, completeData, zone, timeType, gatherType) {
  1315. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1316. const [stages, preStage, endStage] = await this._getCustomZoneStages(tender, zone, timeType);
  1317. const result = await this._gatherStagesChange(completeData, tender, stages, preStage, gatherType);
  1318. this.resultStageChange.push(...result);
  1319. }
  1320. async _gatherStageChange(completeData, tender, stage, hasPre, gatherType = 'flow'){
  1321. if (!stage) return [];
  1322. const helper = this.ctx.helper;
  1323. await this.ctx.service.stage.doCheckStage(stage);
  1324. const curStageChange = await this.ctx.service.stageChange.getCurStageData(stage.tid, stage.id, stage.curTimes, stage.curOrder);
  1325. const preStageChange = hasPre && stage.preCheckedStage ? await this.ctx.service.stageChangeFinal.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  1326. if (gatherType === 'flow') return [...preStageChange, ...curStageChange];
  1327. const result = [];
  1328. const findR = function(source) {
  1329. let r = result.find(x => { return x.cbid === source.cbid});
  1330. if (!r) {
  1331. r = { cid: source.cid, cbid: source.cbid, qty: 0, pre_qty: 0 };
  1332. result.push(r);
  1333. }
  1334. return r;
  1335. };
  1336. curStageChange.forEach(sc => {
  1337. const r = findR(sc);
  1338. r.qty = helper.add(r.qty, sc.qty);
  1339. });
  1340. preStageChange.forEach(sc => {
  1341. const r = findR(sc);
  1342. r.pre_qty = helper.add(r.pre_qty, sc.qty);
  1343. });
  1344. result.forEach(r => { r.end_qty = helper.add(r.pre_qty, r.qty); });
  1345. return result;
  1346. }
  1347. async _gatherMonthStageChange(sTender, completeData, month, hasPre, gatherType) {
  1348. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1349. const stages = await this._getValidStages(tender.id);
  1350. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1351. const result = await this._gatherStageChange(completeData, tender, stage, hasPre, gatherType);
  1352. this.resultStageChange.push(...result);
  1353. }
  1354. async _gatherOrderStageChange(sTender, completeData, order, hasPre, gatherType) {
  1355. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1356. const stages = await this._getValidStages(tender.id);
  1357. const stage = this.ctx.helper._.find(stages, {order: order});
  1358. const result = await this._gatherStageChange(completeData, tender, stage, hasPre, gatherType);
  1359. this.resultStageChange.push(...result);
  1360. }
  1361. async _gatherFinalStageChange(sTender, completeData, hasPre, gatherType) {
  1362. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1363. const stages = await this._getValidStages(tender.id);
  1364. const result = await this._gatherStageChange(completeData, tender, stages[0], hasPre, gatherType);
  1365. this.resultStageChange.push(...result);
  1366. }
  1367. async _gatherCheckedFinalStageChange(sTender, completeData, hasPre, gatherType) {
  1368. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1369. const stages = await this._getCheckedStages(tender.id);
  1370. const result = await this._gatherStageChange(completeData, tender, stages[0], hasPre, gatherType);
  1371. this.resultStageChange.push(...result);
  1372. }
  1373. async getGatherStageChange(memFieldKeys, gsDefine, gsCustom) {
  1374. if (!gsDefine || !gsDefine.enable) return [];
  1375. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1376. const gsSetting = JSON.parse(gsDefine.setting);
  1377. this.resultStageChange = [];
  1378. let commonIndex = 0;
  1379. const completeDatas = [];
  1380. for (const tender of gsCustom.tenders) {
  1381. const completeData = { prefix: 't_' + commonIndex + '_' };
  1382. completeDatas.push(completeData);
  1383. switch (gsSetting.type) {
  1384. case 'month':
  1385. await this._gatherMonthStageChange(tender, completeData, gsCustom.month, gsSetting.hasPre, gsSetting.gather);
  1386. break;
  1387. case 'final':
  1388. await this._gatherFinalStageChange(tender, completeData, gsSetting.hasPre, gsSetting.gather);
  1389. break;
  1390. case 'checked-final':
  1391. await this._gatherCheckedFinalStageChange(tender, completeData, gsSetting.hasPre, gsSetting.gather);
  1392. break;
  1393. case 'stage':
  1394. await this._gatherOrderStageChange(tender, completeData, gsCustom.stage, gsSetting.hasPre, gsSetting.gather);
  1395. break;
  1396. case 'custom-zone':
  1397. await this._gatherCustomZoneStageChange(tender, completeData, gsCustom.custom_zone, gsSetting.timeType, gsSetting.gather);
  1398. break;
  1399. }
  1400. commonIndex++;
  1401. }
  1402. return this.resultStageChange;
  1403. }
  1404. async getGatherAdvancePay(memFieldKeys, gsDefine, gsCustom) {
  1405. if (!gsDefine || !gsDefine.enable) return [];
  1406. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1407. const result = [];
  1408. for (const t of gsCustom.tenders) {
  1409. // const tender = await this.ctx.service.tender.getCheckTender(t.tid);
  1410. const advance_pay = await this.ctx.service.advance.getAllDataByCondition({ where: { tid: t.tid } });
  1411. result.push(...advance_pay);
  1412. }
  1413. return result;
  1414. }
  1415. async _gatherStageJgcl(tender, stage) {
  1416. const data = await this.ctx.service.stageJgcl.getStageData(stage);
  1417. const preData = stage.preCheckedStage ? await this.ctx.service.stageJgcl.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  1418. for (const d of data) {
  1419. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  1420. if (pd) {
  1421. d.pre_arrive_qty = pd.arrive_qty;
  1422. d.pre_arrive_tp = pd.arrive_tp;
  1423. d.pre_deduct_qty = pd.deduct_qty;
  1424. d.pre_deduct_tp = pd.deduct_tp;
  1425. }
  1426. d.end_arrive_qty = this.ctx.helper.add(d.pre_arrive_qty, d.arrive_qty);
  1427. d.end_arrive_tp = this.ctx.helper.add(d.pre_arrive_tp, d.arrive_tp);
  1428. d.end_deduct_qty = this.ctx.helper.add(d.pre_deduct_qty, d.deduct_qty);
  1429. d.end_deduct_tp = this.ctx.helper.add(d.pre_deduct_tp, d.deduct_tp);
  1430. d.tender_name = tender.name;
  1431. }
  1432. this.resultStageJgcl.push(...data);
  1433. }
  1434. async _gatherMonthStageJgcl(sTender, month) {
  1435. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1436. const stages = await this._getValidStages(tender.id);
  1437. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1438. await this._gatherStageJgcl(tender, stage);
  1439. }
  1440. async _gatherOrderStageJgcl(sTender, order) {
  1441. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1442. const stages = await this._getValidStages(tender.id);
  1443. const stage = this.ctx.helper._.find(stages, {order: order});
  1444. await this._gatherStageJgcl(tender, stage);
  1445. }
  1446. async _gatherFinalStageJgcl(sTender) {
  1447. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1448. const stages = await this._getValidStages(tender.id);
  1449. await this._gatherStageJgcl(tender, stages[0]);
  1450. }
  1451. async _gatherCheckedFinalStageJgcl(sTender) {
  1452. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1453. const stages = await this._getCheckedStages(tender.id);
  1454. await this._gatherStageJgcl(tender, stages[0]);
  1455. }
  1456. async getGatherStageJgcl(memFieldKeys, gsDefine, gsCustom) {
  1457. if (!gsDefine || !gsDefine.enable) return [];
  1458. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1459. this.resultStageJgcl = [];
  1460. const gsSetting = JSON.parse(gsDefine.setting);
  1461. for (const tender of gsCustom.tenders) {
  1462. switch (gsSetting.type) {
  1463. case 'month':
  1464. await this._gatherMonthStageJgcl(tender, gsCustom.month);
  1465. break;
  1466. case 'final':
  1467. await this._gatherFinalStageJgcl(tender);
  1468. break;
  1469. case 'checked-final':
  1470. await this._gatherCheckedFinalStageJgcl(tender);
  1471. break;
  1472. case 'stage':
  1473. await this._gatherOrderStageJgcl(tender, gsCustom.stage);
  1474. break;
  1475. }
  1476. }
  1477. return this.resultStageJgcl;
  1478. }
  1479. async _gatherStageYjcl(tender, stage) {
  1480. const data = await this.ctx.service.stageYjcl.getStageData(stage);
  1481. for (const d of data) {
  1482. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  1483. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  1484. }
  1485. this.resultStageYjcl.push(...data);
  1486. }
  1487. async _gatherMonthStageYjcl(sTender, month) {
  1488. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1489. const stages = await this._getValidStages(tender.id);
  1490. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1491. await this._gatherStageYjcl(tender, stage);
  1492. }
  1493. async _gatherOrderStageYjcl(sTender, order) {
  1494. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1495. const stages = await this._getValidStages(tender.id);
  1496. const stage = this.ctx.helper._.find(stages, {order: order});
  1497. await this._gatherStageYjcl(tender, stage);
  1498. }
  1499. async _gatherFinalStageYjcl(sTender) {
  1500. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1501. const stages = await this._getValidStages(tender.id);
  1502. await this._gatherStageYjcl(tender, stages[0]);
  1503. }
  1504. async _gatherCheckedFinalStageYjcl(sTender) {
  1505. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1506. const stages = await this._getCheckedStages(tender.id);
  1507. await this._gatherStageYjcl(tender, stages[0]);
  1508. }
  1509. async getGatherStageYjcl(memFieldKeys, gsDefine, gsCustom) {
  1510. if (!gsDefine || !gsDefine.enable) return [];
  1511. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1512. this.resultStageYjcl = [];
  1513. const gsSetting = JSON.parse(gsDefine.setting);
  1514. for (const tender of gsCustom.tenders) {
  1515. switch (gsSetting.type) {
  1516. case 'month':
  1517. await this._gatherMonthStageYjcl(tender, gsCustom.month);
  1518. break;
  1519. case 'final':
  1520. await this._gatherFinalStageYjcl(tender);
  1521. break;
  1522. case 'checked-final':
  1523. await this._gatherCheckedFinalStageYjcl(tender);
  1524. break;
  1525. case 'stage':
  1526. await this._gatherOrderStageYjcl(tender, gsCustom.stage);
  1527. break;
  1528. }
  1529. }
  1530. return this.resultStageYjcl;
  1531. }
  1532. async _gatherStageBonus(tender, stage) {
  1533. const data = await this.ctx.service.stageBonus.getEndStageData(tender.id, stage.order);
  1534. for (const d of data) {
  1535. const names = this.ctx.helper._.map(d.proof_file, function (x) {
  1536. return x.filename + x.fileext;
  1537. });
  1538. d.proof = names.join('\n');
  1539. d.tender_name = tender.name;
  1540. }
  1541. this.resultStageBonus.push(...data);
  1542. }
  1543. async _gatherMonthStageBonus(sTender, month) {
  1544. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1545. const stages = await this._getValidStages(tender.id);
  1546. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1547. await this._gatherStageBonus(tender, stage);
  1548. }
  1549. async _gatherOrderStageBonus(sTender, order) {
  1550. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1551. const stages = await this._getValidStages(tender.id);
  1552. const stage = this.ctx.helper._.find(stages, {order: order});
  1553. await this._gatherStageBonus(tender, stage);
  1554. }
  1555. async _gatherFinalStageBonus(sTender) {
  1556. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1557. const stages = await this._getValidStages(tender.id);
  1558. await this._gatherStageBonus(tender, stages[0]);
  1559. }
  1560. async _gatherCheckedFinalStageBonus(sTender) {
  1561. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1562. const stages = await this._getCheckedStages(tender.id);
  1563. await this._gatherStageBonus(tender, stages[0]);
  1564. }
  1565. async getGatherStageBonus(memFieldKeys, gsDefine, gsCustom) {
  1566. if (!gsDefine || !gsDefine.enable) return [];
  1567. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1568. this.resultStageBonus = [];
  1569. const gsSetting = JSON.parse(gsDefine.setting);
  1570. for (const tender of gsCustom.tenders) {
  1571. switch (gsSetting.type) {
  1572. case 'month':
  1573. await this._gatherMonthStageBonus(tender, gsCustom.month);
  1574. break;
  1575. case 'final':
  1576. await this._gatherFinalStageBonus(tender);
  1577. break;
  1578. case 'checked-final':
  1579. await this._gatherCheckedFinalStageBonus(tender);
  1580. break;
  1581. case 'stage':
  1582. await this._gatherOrderStageBonus(tender, gsCustom.stage);
  1583. break;
  1584. }
  1585. }
  1586. return this.resultStageBonus;
  1587. }
  1588. async _gatherStageOther(tender, stage) {
  1589. const data = await this.ctx.service.stageOther.getStageData(stage);
  1590. const preData = stage.preCheckedStage ? await this.ctx.service.stageOther.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  1591. for (const d of data) {
  1592. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  1593. if (pd) {
  1594. d.pre_tp = pd.tp;
  1595. }
  1596. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  1597. d.tender_name = tender.name;
  1598. }
  1599. this.resultStageOther.push(...data);
  1600. }
  1601. async _gatherMonthStageOther(sTender, month) {
  1602. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1603. const stages = await this._getValidStages(tender.id);
  1604. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1605. await this._gatherStageOther(tender, stage);
  1606. }
  1607. async _gatherOrderStageOther(sTender, order) {
  1608. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1609. const stages = await this._getValidStages(tender.id);
  1610. const stage = this.ctx.helper._.find(stages, {order: order});
  1611. await this._gatherStageOther(tender, stage);
  1612. }
  1613. async _gatherFinalStageOther(sTender) {
  1614. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1615. const stages = await this._getValidStages(tender.id);
  1616. await this._gatherStageOther(tender, stages[0]);
  1617. }
  1618. async _gatherCheckedFinalStageOther(sTender) {
  1619. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1620. const stages = await this._getCheckedStages(tender.id);
  1621. await this._gatherStageOther(tender, stages[0]);
  1622. }
  1623. async getGatherStageOther(memFieldKeys, gsDefine, gsCustom) {
  1624. if (!gsDefine || !gsDefine.enable) return [];
  1625. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1626. this.resultStageOther = [];
  1627. const gsSetting = JSON.parse(gsDefine.setting);
  1628. for (const tender of gsCustom.tenders) {
  1629. switch (gsSetting.type) {
  1630. case 'month':
  1631. await this._gatherMonthStageOther(tender, gsCustom.month);
  1632. break;
  1633. case 'final':
  1634. await this._gatherFinalStageOther(tender);
  1635. break;
  1636. case 'checked-final':
  1637. await this._gatherCheckedFinalStageOther(tender);
  1638. break;
  1639. case 'stage':
  1640. await this._gatherOrderStageOther(tender, gsCustom.stage);
  1641. break;
  1642. }
  1643. }
  1644. return this.resultStageOther;
  1645. }
  1646. async _gatherStageSafeProd(tender, stage) {
  1647. const data = await this.ctx.service.stageSafeProd.getStageData(stage);
  1648. const preData = stage.preCheckedStage ? await this.ctx.service.stageSafeProd.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  1649. for (const d of data) {
  1650. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  1651. if (pd) {
  1652. d.pre_qty = pd.qty;
  1653. d.pre_tp = pd.tp;
  1654. }
  1655. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  1656. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  1657. }
  1658. const helper = this.ctx.helper;
  1659. for (const d of data) {
  1660. let gd = this.resultStageSafeProd.find(x => {
  1661. return x.name === d.name && x.unit === d.unit && helper.numEqual(x.unit_price, d.unit_price);
  1662. });
  1663. if (!gd) {
  1664. gd = { name: d.name, unit: d.unit, unit_price: d.unit_price };
  1665. this.resultStageSafeProd.push(gd);
  1666. }
  1667. gd.s_quantity = helper.add(gd.s_quantity, d.quantity);
  1668. gd.s_total_price = helper.add(gd.s_total_price, d.total_price);
  1669. gd.s_qty = helper.add(gd.s_qty, d.qty);
  1670. gd.s_tp = helper.add(gd.s_tp, d.tp);
  1671. gd.s_pre_qty = helper.add(gd.s_pre_qty, d.pre_qty);
  1672. gd.s_pre_tp = helper.add(gd.s_pre_tp, d.pre_tp);
  1673. gd.s_end_qty = helper.add(gd.s_end_qty, d.end_qty);
  1674. gd.s_end_tp = helper.add(gd.s_end_tp, d.end_tp);
  1675. }
  1676. }
  1677. async _gatherMonthStageSafeProd(sTender, month) {
  1678. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1679. const stages = await this._getValidStages(tender.id);
  1680. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1681. await this._gatherStageSafeProd(tender, stage);
  1682. }
  1683. async _gatherOrderStageSafeProd(sTender, order) {
  1684. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1685. const stages = await this._getValidStages(tender.id);
  1686. const stage = this.ctx.helper._.find(stages, {order: order});
  1687. await this._gatherStageSafeProd(tender, stage);
  1688. }
  1689. async _gatherFinalStageSafeProd(sTender) {
  1690. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1691. const stages = await this._getValidStages(tender.id);
  1692. await this._gatherStageSafeProd(tender, stages[0]);
  1693. }
  1694. async _gatherCheckedFinalStageSafeProd(sTender) {
  1695. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1696. const stages = await this._getCheckedStages(tender.id);
  1697. await this._gatherStageSafeProd(tender, stages[0]);
  1698. }
  1699. async getGatherStageSafeProd(memFieldKeys, gsDefine, gsCustom) {
  1700. if (!gsDefine || !gsDefine.enable) return [];
  1701. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1702. this.resultStageSafeProd = [];
  1703. const gsSetting = JSON.parse(gsDefine.setting);
  1704. for (const tender of gsCustom.tenders) {
  1705. switch (gsSetting.type) {
  1706. case 'month':
  1707. await this._gatherMonthStageSafeProd(tender, gsCustom.month);
  1708. break;
  1709. case 'final':
  1710. await this._gatherFinalStageSafeProd(tender);
  1711. break;
  1712. case 'checked-final':
  1713. await this._gatherCheckedFinalStageSafeProd(tender);
  1714. break;
  1715. case 'stage':
  1716. await this._gatherOrderStageSafeProd(tender, gsCustom.stage);
  1717. break;
  1718. }
  1719. }
  1720. return this.resultStageSafeProd;
  1721. }
  1722. async _gatherStageTempLand(tender, stage) {
  1723. const data = await this.ctx.service.stageTempLand.getStageData(stage);
  1724. const preData = stage.preCheckedStage ? await this.ctx.service.stageTempLand.getEndStageData(stage.tid, stage.preCheckedStage.order) : [];
  1725. for (const d of data) {
  1726. const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
  1727. if (pd) {
  1728. d.pre_qty = pd.qty;
  1729. d.pre_tp = pd.tp;
  1730. }
  1731. d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
  1732. d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
  1733. }
  1734. const helper = this.ctx.helper;
  1735. for (const d of data) {
  1736. let gd = this.resultStageTempLand.find(x => {
  1737. return x.name === d.name && x.unit === d.unit && helper.numEqual(x.unit_price, d.unit_price);
  1738. });
  1739. if (!gd) {
  1740. gd = { name: d.name, unit: d.unit, unit_price: d.unit_price };
  1741. this.resultStageTempLand.push(gd);
  1742. }
  1743. gd.s_qty = helper.add(gd.s_qty, d.qty);
  1744. gd.s_tp = helper.add(gd.s_tp, d.tp);
  1745. gd.s_pre_qty = helper.add(gd.s_pre_qty, d.pre_qty);
  1746. gd.s_pre_tp = helper.add(gd.s_pre_tp, d.pre_tp);
  1747. gd.s_end_qty = helper.add(gd.s_end_qty, d.end_qty);
  1748. gd.s_end_tp = helper.add(gd.s_end_tp, d.end_tp);
  1749. }
  1750. }
  1751. async _gatherMonthStageTempLand(sTender, month) {
  1752. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1753. const stages = await this._getValidStages(tender.id);
  1754. const stage = this.ctx.helper._.find(stages, {s_time: month});
  1755. await this._gatherStageTempLand(tender, stage);
  1756. }
  1757. async _gatherOrderStageTempLand(sTender, order) {
  1758. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1759. const stages = await this._getValidStages(tender.id);
  1760. const stage = this.ctx.helper._.find(stages, {order: order});
  1761. await this._gatherStageTempLand(tender, stage);
  1762. }
  1763. async _gatherFinalStageTempLand(sTender) {
  1764. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1765. const stages = await this._getValidStages(tender.id);
  1766. await this._gatherStageTempLand(tender, stages[0]);
  1767. }
  1768. async _gatherCheckedFinalStageTempLand(sTender) {
  1769. const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
  1770. const stages = await this._getCheckedStages(tender.id);
  1771. await this._gatherStageTempLand(tender, stages[0]);
  1772. }
  1773. async getGatherStageTempLand(memFieldKeys, gsDefine, gsCustom) {
  1774. if (!gsDefine || !gsDefine.enable) return [];
  1775. if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
  1776. this.resultStageTempLand = [];
  1777. const gsSetting = JSON.parse(gsDefine.setting);
  1778. for (const tender of gsCustom.tenders) {
  1779. switch (gsSetting.type) {
  1780. case 'month':
  1781. await this._gatherMonthStageTempLand(tender, gsCustom.month);
  1782. break;
  1783. case 'final':
  1784. await this._gatherFinalStageTempLand(tender);
  1785. break;
  1786. case 'checked-final':
  1787. await this._gatherCheckedFinalStageTempLand(tender);
  1788. break;
  1789. case 'stage':
  1790. await this._gatherOrderStageTempLand(tender, gsCustom.stage);
  1791. break;
  1792. }
  1793. }
  1794. return this.resultStageTempLand;
  1795. }
  1796. }
  1797. return RptGatherMemory;
  1798. };