change_revise.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  1. 'use strict';
  2. /**
  3. * 台账修订页面js
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const ckBillsSpread = '/tender/' + window.location.pathname.split('/')[2] + '/change/revise-billsSelect';
  10. const invalidFields = {
  11. parent: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'unit_price'],
  12. gcl: ['dgn_qty1', 'dgn_qty2'],
  13. posCode: ['b_code'],
  14. posCalc: ['sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp'],
  15. posXmj: ['code'],
  16. };
  17. function getExprInfo (field) {
  18. const exprField = [
  19. {qty: 'sgfh_qty', expr: 'sgfh_expr'},
  20. {qty: 'sjcl_qty', expr: 'sjcl_expr'},
  21. {qty: 'qtcl_qty', expr: 'qtcl_expr'},
  22. ];
  23. return _.find(exprField, {qty: field});
  24. }
  25. function transExpr(expr) {
  26. return $.trim(expr).replace('\t', '').replace('=', '').replace('%', '/100');
  27. }
  28. const copyBlockTag = 'zh.calc.copyBlock';
  29. const checkOption = {
  30. sibling: { enable: 1 },
  31. empty_code: { enable: 1 },
  32. calc: {
  33. enable: 1,
  34. fields: ['sgfh_qty', 'qtcl_qty', 'sjcl_qty', 'quantity'],
  35. },
  36. zero: { enable: 1 },
  37. zeroPos: { enable: 1 },
  38. tp: {
  39. enable: 1,
  40. fields: [
  41. {qty: 'sgfh_qty', tp: 'sgfh_tp'},
  42. {qty: 'sjcl_qty', tp: 'sjcl_tp'},
  43. {qty: 'qtcl_qty', tp: 'qtcl_tp'},
  44. {qty: 'quantity', tp: 'total_price'},
  45. {qty: 'deal_qty', tp: 'deal_tp'},
  46. ],
  47. },
  48. same_code: { enable: 1 },
  49. };
  50. $(document).ready(() => {
  51. let stdXmj, stdGcl, searchLedger;
  52. autoFlashHeight();
  53. if (!billsSpreadSetting.readOnly) {
  54. for (const col of billsSpreadSetting.cols) {
  55. if (!col.readOnly || col.readOnly === false)
  56. col.readOnly = 'readOnly.isChangeAdd';
  57. }
  58. }
  59. if (!posSpreadSetting.readOnly) {
  60. for (const col of posSpreadSetting.cols) {
  61. if (!col.readOnly || col.readOnly === false)
  62. col.readOnly = 'readOnly.isChangeAdd';
  63. }
  64. }
  65. const billsCol = {
  66. readOnly: {
  67. isChangeAdd: function (data) {
  68. return !readOnly && !data.ccid;
  69. }
  70. }
  71. };
  72. const posCol = {
  73. readOnly: {
  74. isChangeAdd: function (data) {
  75. return !readOnly && !data.ccid;
  76. }
  77. }
  78. };
  79. // 初始化spread
  80. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  81. const billsSheet = billsSpread.getActiveSheet();
  82. billsSpreadSetting.rowHeader = [
  83. {
  84. rowHeaderType: 'circle',
  85. setting: {
  86. size: 5,
  87. indent: 16,
  88. getColor: function (index, data) {
  89. if (!data) return;
  90. if(!data.ccid && !data.cid) return;
  91. return '#dc3545';
  92. }
  93. },
  94. },
  95. ];
  96. sjsSettingObj.setFxTreeStyle(billsSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  97. if (thousandth) sjsSettingObj.setTpThousandthFormat(billsSpreadSetting);
  98. SpreadJsObj.initSpreadSettingEvents(billsSpreadSetting, billsCol);
  99. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  100. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  101. const posSheet = posSpread.getActiveSheet();
  102. posSpreadSetting.rowHeader = [
  103. {
  104. rowHeaderType: 'circle',
  105. setting: {
  106. size: 5,
  107. indent: 16,
  108. getColor: function (index, data) {
  109. if (!data) return;
  110. if(!data.ccid) return;
  111. return '#dc3545';
  112. }
  113. },
  114. },
  115. ];
  116. sjsSettingObj.setGridSelectStyle(posSpreadSetting);
  117. if (thousandth) sjsSettingObj.setTpThousandthFormat(posSpreadSetting);
  118. SpreadJsObj.initSpreadSettingEvents(posSpreadSetting, posCol);
  119. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  120. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  121. const errorList = $.cs_errorList({
  122. tabSelector: '#error-list-tab',
  123. selector: '#error-list',
  124. relaSpread: billsSpread,
  125. storeKey: 'change-revise-error-' + window.location.pathname.split('/')[2],
  126. afterLocated: function () {
  127. posSpreadObj.loadCurPosData();
  128. },
  129. afterShow: function () {
  130. billsSpread.refresh();
  131. if (posSpread) posSpread.refresh();
  132. },
  133. });
  134. const checkList = $.ledger_checkList({
  135. id: 'check-list',
  136. tabSelector: '#check-list-tab',
  137. selector: '#check-list',
  138. relaSpread: billsSpread,
  139. storeKey: 'change-revise-check-' + window.location.pathname.split('/')[2],
  140. checkType: getCheckType(checkOption),
  141. afterLocated: function () {
  142. posSpreadObj.loadCurPosData();
  143. },
  144. afterShow: function () {
  145. billsSpread.refresh();
  146. if (posSpread) posSpread.refresh();
  147. },
  148. });
  149. const sumLoadMiss = $.sumLoadMiss({
  150. tabSelector: '#sum-load-miss-tab',
  151. selector: '#sum-load-miss',
  152. relaSpread: billsSpread,
  153. storeKey: 'change-revise-slm-' + window.location.pathname.split('/')[2],
  154. id: 'revise-slm',
  155. afterLocated: function () {
  156. posSpreadObj.loadCurPosData();
  157. },
  158. afterShow: function () {
  159. billsSpread.refresh();
  160. if (posSpread) posSpread.refresh();
  161. },
  162. });
  163. // 初始化 节点树结构
  164. const treeSetting = {
  165. id: 'ledger_id',
  166. pid: 'ledger_pid',
  167. order: 'order',
  168. level: 'level',
  169. rootId: -1,
  170. keys: ['id', 'tender_id', 'ledger_id'],
  171. calcFields: ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'],
  172. autoExpand: 3,
  173. markExpandKey: 'change-bills-expand',
  174. markExpandSubKey: window.location.pathname.split('/')[2],
  175. };
  176. if (!isTz) {
  177. treeSetting.calcFields.push('deal_tp');
  178. }
  179. treeSetting.calcFun = function (node) {
  180. if (node && node.b_code) {
  181. const posData = pos.getLedgerPos(node.id) || [];
  182. if (posData.length > 0) {
  183. let sgfh_qty = 0;
  184. let sjcl_qty = 0;
  185. let qtcl_qty = 0;
  186. for (const np of posData) {
  187. sgfh_qty = ZhCalc.add(sgfh_qty, np.sgfh_qty);
  188. sjcl_qty = ZhCalc.add(sjcl_qty, np.sjcl_qty);
  189. qtcl_qty = ZhCalc.add(qtcl_qty, np.qtcl_qty);
  190. }
  191. if (!_.isEqual(node.sgfh_qty, sgfh_qty) || !_.isEqual(node.sjcl_qty, sjcl_qty) || !_.isEqual(node.qtcl_qty, qtcl_qty)) {
  192. node.sgfh_qty = sgfh_qty;
  193. node.sjcl_qty = sjcl_qty;
  194. node.qtcl_qty = qtcl_qty;
  195. node.quantity = ZhCalc.sum([node.sgfh_qty, node.sjcl_qty, node.qtcl_qty]);
  196. node.sgfh_tp = ZhCalc.mul(sgfh_qty, node.unit_price, decimal.tp) || 0;
  197. node.sjcl_tp = ZhCalc.mul(sjcl_qty, node.unit_price, decimal.tp) || 0;
  198. node.qtcl_tp = ZhCalc.mul(qtcl_qty, node.unit_price, decimal.tp) || 0;
  199. node.total_price = ZhCalc.mul(node.quantity, node.unit_price, decimal.tp) || 0;
  200. }
  201. }
  202. }
  203. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  204. };
  205. const billsTree = createNewPathTree('revise', treeSetting);
  206. // 初始化 计量单元
  207. const pos = new PosData({ id: 'id', ledgerId: 'lid' });
  208. // 清单 相关方法&绑定spreadjs事件
  209. const billsTreeSpreadObj = {
  210. loadExprToInput(sheet) {
  211. const sel = sheet.getSelections()[0];
  212. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  213. if (col && col.type === 'Number') {
  214. const data = SpreadJsObj.getSelectObject(sheet);
  215. if (data) {
  216. const exprInfo = getExprInfo(col.field);
  217. if (exprInfo) {
  218. $('#bills-expr').val(data[exprInfo.expr] ? data[exprInfo.expr] : data[col.field])
  219. .attr('field', col.field).attr('org', data[col.field]);
  220. } else {
  221. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field]);
  222. }
  223. if (col.field.indexOf('dgn') >= 0) {
  224. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (_.isString(data.b_code) && data.b_code !== '') || !data.ccid);
  225. } else if (col.field === 'unit_price') {
  226. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || (_.isBoolean(data.used) && data.used === true) || !data.ccid);
  227. } else {
  228. const nodePos = pos.getLedgerPos(data.id);
  229. if (nodePos && nodePos.length > 0) {
  230. $('#bills-expr').val('').attr('readOnly', true);
  231. $('#bills-expr').removeAttr('data-row');
  232. } else {
  233. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || !data.ccid);
  234. }
  235. }
  236. $('#bills-expr').attr('data-row', sel.row);
  237. } else {
  238. $('#bills-expr').val('').attr('readOnly', true);
  239. $('#bills-expr').removeAttr('data-row');
  240. }
  241. } else {
  242. $('#bills-expr').val('').attr('readOnly', true);
  243. $('#bills-expr').removeAttr('data-row');
  244. }
  245. },
  246. getDefaultSelectInfo: function (sheet) {
  247. const tree = sheet.zh_tree;
  248. if (!tree) return;
  249. const sel = sheet.getSelections()[0];
  250. const node = sheet.zh_tree.nodes[sel.row];
  251. if (!node) return;
  252. let count = 1;
  253. if (sel.rowCount > 1) {
  254. for (let r = 1; r < sel.rowCount; r++) {
  255. const rNode = sheet.zh_tree.nodes[sel.row + r];
  256. if (rNode.level > node.level) continue;
  257. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  258. toastr.warning('请选择同一节点下的节点,进行该操作');
  259. return;
  260. }
  261. count += 1;
  262. }
  263. }
  264. return [tree, node, count];
  265. },
  266. /**
  267. * 刷新顶部按钮是否可用
  268. * @param sheet
  269. * @param selections
  270. */
  271. refreshOperationValid: function (sheet, selection) {
  272. const setObjEnable = function (obj, enable) {
  273. if (enable) {
  274. obj.removeClass('disabled');
  275. } else {
  276. obj.addClass('disabled');
  277. }
  278. };
  279. const invalidAll = function () {
  280. setObjEnable($('a[name=base-opr][type=add]'), false);
  281. setObjEnable($('a[name=base-opr][type=delete]'), false);
  282. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  283. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  284. setObjEnable($('a[name=base-opr][type=up-level]'), false);
  285. setObjEnable($('a[name=base-opr][type=down-level]'), false);
  286. };
  287. const sel = selection ? selection[0] : sheet.getSelections()[0];
  288. const row = sel ? sel.row : -1;
  289. const tree = sheet.zh_tree;
  290. if (!tree) {
  291. invalidAll();
  292. return;
  293. }
  294. const first = sheet.zh_tree.nodes[row];
  295. if (!first) {
  296. invalidAll();
  297. return;
  298. }
  299. let last = first, sameParent = true, nodeUsed = first.used;
  300. if (sel.rowCount > 1 && first) {
  301. for (let r = 1; r < sel.rowCount; r++) {
  302. const rNode = tree.nodes[sel.row + r];
  303. if (!rNode) {
  304. sameParent = false;
  305. break;
  306. }
  307. if (!rNode.ccid) {
  308. sameParent = false;
  309. break;
  310. }
  311. nodeUsed = nodeUsed || rNode.used;
  312. if (rNode.level > first.level) continue;
  313. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  314. sameParent = false;
  315. break;
  316. }
  317. last = rNode;
  318. }
  319. }
  320. const preNode = tree.getPreSiblingNode(first);
  321. const siblings = tree.getChildren(tree.getParent(first));
  322. let upPower = true;
  323. for (let order = last.order; order < siblings.length - 1; order ++) {
  324. if (!siblings[order].ccid) {
  325. upPower = false;
  326. break;
  327. }
  328. }
  329. const valid = !sheet.zh_setting.readOnly;
  330. setObjEnable($('a[name=base-opr][type=add]'), valid && first && first.level > 1);
  331. setObjEnable($('a[name=base-opr][type=delete]'), valid && first && sameParent && first.level > 1 && !nodeUsed && first.ccid);
  332. setObjEnable($('a[name=base-opr][type=up-move]'), valid && first && sameParent && first.level > 1 && preNode && first.ccid);
  333. setObjEnable($('a[name=base-opr][type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last) && first.ccid);
  334. const posRange = last ? pos.getLedgerPos(last.id) : [];
  335. setObjEnable($('a[name=base-opr][type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed
  336. && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)) && upPower && first.ccid);
  337. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  338. setObjEnable($('a[name=base-opr][type=down-level]'), valid && first && sameParent
  339. && first.level > 1 && preNode && (preNode.children.length > 0 || (preNode.children.length === 0 && preNode.ccid && (!preNodePosRange || preNodePosRange.length === 0))) && !preNode.used && first.ccid);
  340. setObjEnable($('#cut'), valid);
  341. setObjEnable($('#paste'), valid);
  342. },
  343. /**
  344. *
  345. * @param sheet
  346. * @param data
  347. */
  348. refreshTree: function (sheet, data) {
  349. SpreadJsObj.massOperationSheet(sheet, function () {
  350. const tree = sheet.zh_tree;
  351. // 处理删除
  352. if (data.delete) {
  353. data.delete.sort(function (a, b) {
  354. return b.deleteIndex - a.deleteIndex;
  355. });
  356. for (const d of data.delete) {
  357. sheet.deleteRows(d.deleteIndex, 1);
  358. }
  359. }
  360. // 处理新增
  361. if (data.create) {
  362. const newNodes = data.create;
  363. if (newNodes) {
  364. newNodes.sort(function (a, b) {
  365. return a.index - b.index;
  366. });
  367. for (const node of newNodes) {
  368. sheet.addRows(node.index, 1);
  369. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  370. }
  371. }
  372. }
  373. // 处理更新
  374. if (data.update) {
  375. const rows = [];
  376. for (const u of data.update) {
  377. rows.push(tree.nodes.indexOf(u));
  378. }
  379. SpreadJsObj.reLoadRowsData(sheet, rows);
  380. }
  381. // 处理展开
  382. if (data.expand) {
  383. const expanded = [];
  384. for (const e of data.expand) {
  385. if (expanded.indexOf(e) === -1) {
  386. const posterity = tree.getPosterity(e);
  387. for (const p of posterity) {
  388. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  389. expanded.push(p);
  390. }
  391. }
  392. }
  393. }
  394. });
  395. },
  396. selectionChanged: function (e, info) {
  397. if (info.newSelections) {
  398. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row || info.newSelections[0].rowCount !== info.oldSelections[0].rowCount) {
  399. billsTreeSpreadObj.refreshOperationValid(info.sheet);
  400. billsTreeSpreadObj.refreshPosData();
  401. }
  402. }
  403. billsTreeSpreadObj.loadExprToInput(info.sheet);
  404. },
  405. refreshPosData: function() {
  406. SpreadJsObj.resetTopAndSelect(posSheet);
  407. posSpreadObj.loadCurPosData();
  408. SpreadJsObj.saveTopAndSelect(billsSheet, ckBillsSpread);
  409. posSearch.search($('#pos-keyword').val());
  410. },
  411. /**
  412. * 新增节点
  413. * @param spread
  414. */
  415. baseOpr: function (sheet, type, addCount = 1) {
  416. const self = this;
  417. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  418. if (!tree || !node || !count) return;
  419. if (type === 'delete') {
  420. const parent = tree.getParent(node);
  421. const children = parent ? parent.children : tree.children;
  422. const index = children.indexOf(node);
  423. for (let i = 0; i < count; i++) {
  424. const child = children[i+index];
  425. if (tree.checkNodeUsed(child, pos)) {
  426. toastr.warning('选中的节点已计量,不可删除');
  427. return;
  428. }
  429. }
  430. } else if (type === 'up-level') {
  431. const parent = tree.getParent(node);
  432. const children = parent ? parent.children : tree.children;
  433. const index = children.indexOf(node);
  434. for (let i = index; i < children.length; i++) {
  435. const child = children[index];
  436. if (tree.checkNodeUsed(child, pos)) {
  437. if (i >= index + count) {
  438. toastr.warning('其后节点已计量,选中的节点不可升级');
  439. } else {
  440. toastr.warning('选中的节点已计量,不可升级');
  441. }
  442. return;
  443. }
  444. }
  445. } else if (type === 'down-level') {
  446. const parent = tree.getParent(node);
  447. const children = parent ? parent.children : tree.children;
  448. const index = children.indexOf(node);
  449. for (let i = 0; i < count; i++) {
  450. const child = children[i+index];
  451. if (tree.checkNodeUsed(child, pos)) {
  452. toastr.warning('选中的节点已计量,不可降级');
  453. return;
  454. }
  455. }
  456. }
  457. if (type === 'delete') {
  458. deleteAfterHint(function () {
  459. postData(window.location.pathname + '/update', {
  460. postType: type,
  461. postData: {
  462. id: node.ledger_id,
  463. count: type === 'add' ? addCount : count,
  464. }
  465. }, function (result) {
  466. const refreshData = tree.loadPostData(result);
  467. self.refreshTree(sheet, refreshData);
  468. const sel = sheet.getSelections()[0];
  469. if (sel) {
  470. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  471. billsTreeSpreadObj.refreshPosData();
  472. }
  473. self.refreshOperationValid(sheet);
  474. });
  475. });
  476. } else {
  477. postData(window.location.pathname + '/update', {
  478. postType: type,
  479. postData: {
  480. id: node.ledger_id,
  481. count: type === 'add' ? addCount : count,
  482. }
  483. }, function (result) {
  484. const refreshData = tree.loadPostData(result);
  485. self.refreshTree(sheet, refreshData);
  486. if (['up-move', 'down-move'].indexOf(type) > -1) {
  487. const sel = sheet.getSelections()[0];
  488. if (sel) {
  489. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  490. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(node)]);
  491. }
  492. } else if (type === 'add') {
  493. const sel = sheet.getSelections()[0];
  494. if (sel) {
  495. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  496. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(refreshData.create[0])]);
  497. billsTreeSpreadObj.refreshPosData();
  498. }
  499. }
  500. self.refreshOperationValid(sheet);
  501. });
  502. }
  503. },
  504. /**
  505. * 编辑单元格响应事件
  506. * @param {Object} e
  507. * @param {Object} info
  508. */
  509. editEnded: function (e, info) {
  510. if (info.sheet.zh_setting) {
  511. const col = info.sheet.zh_setting.cols[info.col];
  512. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  513. const node = sortData[info.row];
  514. const data = {
  515. id: node.id,
  516. tender_id: node.tender_id,
  517. ledger_id: node.ledger_id
  518. };
  519. // 未改变值则不提交
  520. const orgValue = node[col.field];
  521. const newValue = trimInvalidChar(info.editingText);
  522. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  523. return;
  524. }
  525. // 台账模式,检查计量单元相关
  526. if (isTz) {
  527. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  528. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  529. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  530. if (!node.children || node.children.length ===0) {
  531. const lPos = pos.getLedgerPos(node.id);
  532. if (lPos && lPos.length > 0) {
  533. toastr.error('清单含有计量单元,不可修改施工图复核数量');
  534. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  535. return;
  536. }
  537. }
  538. }
  539. if (col.field === 'b_code' && (newValue === '' || !newValue)) {
  540. const lPos = pos.getLedgerPos(node.id);
  541. if (lPos && lPos.length > 0) {
  542. toastr.error('清单含有计量单元,请先删除计量单元,再删除清单编号');
  543. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  544. return;
  545. }
  546. }
  547. }
  548. // 获取更新数据
  549. const exprInfo = getExprInfo(col.field);
  550. if (info.editingText) {
  551. const text = newValue;
  552. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '' && text === '') {
  553. toastr.error('节点已计量,请勿删除编号');
  554. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  555. return;
  556. }
  557. if (col.type === 'Number') {
  558. const num = _.toNumber(text);
  559. if (_.isFinite(num)) {
  560. data[col.field] = num;
  561. if (exprInfo) {
  562. data[exprInfo.expr] = '';
  563. }
  564. } else {
  565. try {
  566. data[col.field] = math.evaluate(transExpr(text));
  567. if (exprInfo) {
  568. data[exprInfo.expr] = newValue;
  569. }
  570. } catch(err) {
  571. toastr.error('输入的表达式非法');
  572. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  573. return;
  574. }
  575. }
  576. } else {
  577. data[col.field] = text;
  578. }
  579. } else {
  580. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '') {
  581. toastr.error('节点已计量,请勿删除编号');
  582. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  583. return;
  584. }
  585. data[col.field] = null;
  586. if (exprInfo) {
  587. data[exprInfo.expr] = '';
  588. }
  589. }
  590. // 更新至服务器
  591. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  592. const refreshNode = billsTree.loadPostData(result);
  593. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  594. });
  595. }
  596. },
  597. clipboardPasting: function (e, info) {
  598. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  599. info.cancel = true;
  600. if (!setting || !tree) return;
  601. // const range = info.cellRange;
  602. // for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  603. // const node = tree.nodes[iRow];
  604. // if (tree.checkNodeUsed(node, pos)) {
  605. // toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  606. // return;
  607. // }
  608. // }
  609. const pasteData = info.pasteData.html
  610. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  611. : (info.pasteData.text === ''
  612. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  613. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  614. const hint = {
  615. usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'},
  616. usedCode: {type: 'warning', msg: '节点已计量,编号不可修改为空值'},
  617. invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'},
  618. posCode: {type: 'warning', msg: '清单含有计量单元,不可粘贴清单编号为空'},
  619. posQty: {type: 'warning', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可粘贴'},
  620. parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'},
  621. gcl: {type: 'warning', msg: '工程量清单,不可粘贴项目节数量'},
  622. posXmj: {type: 'warning', msg: '清单含有计量单元,不可粘贴项目节编号'},
  623. sameParent: {type: 'warning', msg: '仅可粘贴同层节点'},
  624. };
  625. const datas = [], filterNodes = [];
  626. let pid, level, filterRow = 0;
  627. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  628. const curRow = info.cellRange.row + iRow;
  629. const node = tree.nodes[curRow];
  630. if (!node) continue;
  631. if (!pid) pid = node.ledger_pid;
  632. if (!level) level = node.level;
  633. if (node.ledger_pid !== pid) {
  634. toastMessageUniq(hint.sameParent);
  635. filterRow+=1;
  636. continue;
  637. }
  638. if (node.level < level) break;
  639. let bPaste = false;
  640. const data = info.sheet.zh_tree.getNodeKeyData(node);
  641. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  642. const curCol = info.cellRange.col + iCol;
  643. const colSetting = info.sheet.zh_setting.cols[curCol];
  644. const value = trimInvalidChar(pasteData[iRow-filterRow][iCol]);
  645. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  646. toastMessageUniq(hint.parent);
  647. continue;
  648. }
  649. if (!_.isEmpty(node.b_code) && invalidFields.gcl.indexOf(colSetting.field) >= 0) {
  650. toastMessageUniq(hint.gcl);
  651. continue;
  652. }
  653. const lPos = pos.getLedgerPos(node.id);
  654. if (lPos && lPos.length > 0) {
  655. if (value === '' && colSetting.field === 'b_code') {
  656. toastMessageUniq(hint.posCode);
  657. continue;
  658. }
  659. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  660. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  661. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  662. toastMessageUniq(hint.posQty);
  663. continue;
  664. }
  665. if (value !== '' && invalidFields.posXmj.indexOf(colSetting.field) >= 0) {
  666. toastMessageUniq(hint.posXmj);
  667. continue;
  668. }
  669. }
  670. if (tree.checkNodeUsed(node, pos) && colSetting.field === 'unit_price') {
  671. toastMessageUniq (hint.usedUp);
  672. continue;
  673. }
  674. if (colSetting.type === 'Number') {
  675. const num = _.toNumber(value);
  676. if (num) {
  677. data[colSetting.field] = num;
  678. } else {
  679. try {
  680. data[colSetting.field] = math.evaluate(transExpr(value));
  681. const exprInfo = getExprInfo(colSetting.field);
  682. if (exprInfo) {
  683. data[exprInfo.expr] = value;
  684. }
  685. bPaste = true;
  686. } catch (err) {
  687. toastMessageUniq(hint.invalidExpr);
  688. continue;
  689. }
  690. }
  691. } else {
  692. if (node.used && (colSetting.field === 'code' || colSetting.field ==='b_code')
  693. && data[colSetting.field] !== '' && value === '') {
  694. toastMessageUniq(hint.usedCode);
  695. continue;
  696. }
  697. data[colSetting.field] = value;
  698. }
  699. bPaste = true;
  700. }
  701. if (bPaste) {
  702. datas.push(data);
  703. } else {
  704. filterNodes.push(node);
  705. }
  706. }
  707. if (datas.length > 0) {
  708. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  709. const refreshNode = tree.loadPostData(result);
  710. if (refreshNode.update) {
  711. refreshNode.update = refreshNode.update.concat(filterNodes);
  712. }
  713. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  714. }, function () {
  715. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  716. });
  717. } else {
  718. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  719. }
  720. },
  721. clipboardPasted: function (e, info) {
  722. const hint = {
  723. usedUp: {type: 'error', msg: '节点已计量,不可修改单价'},
  724. usedCode: {type: 'error', msg: '节点已计量,编号不可修改为空值'},
  725. invalidExpr: {type: 'error', msg: '粘贴的表达式非法'},
  726. posCode: {type: 'error', msg: '清单含有计量单元,请先删除计量单元,再修改清单编号为空'},
  727. posQty: {type: 'error', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可修改'},
  728. };
  729. const tree = info.sheet.zh_tree;
  730. if (!tree) { return; }
  731. const sortData = info.sheet.zh_tree.nodes;
  732. const datas = [], filterNodes = [];
  733. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  734. let bPaste = false;
  735. const curRow = info.cellRange.row + iRow;
  736. const node = sortData[curRow];
  737. if (node) {
  738. const data = info.sheet.zh_tree.getNodeKeyData(node);
  739. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  740. const curCol = info.cellRange.col + iCol;
  741. const colSetting = info.sheet.zh_setting.cols[curCol];
  742. const value = trimInvalidChar(info.sheet.getText(curRow, curCol));
  743. const lPos = pos.getLedgerPos(node.id);
  744. if (lPos && lPos.length > 0) {
  745. if (value === '' && colSetting.field === 'b_code') {
  746. toastMessageUniq(hint.posCode);
  747. continue;
  748. }
  749. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  750. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  751. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  752. toastMessageUniq(hint.posQty);
  753. continue;
  754. }
  755. }
  756. if (node.used && col.field === 'unit_price') {
  757. toastMessageUniq (hint.usedUp);
  758. continue;
  759. }
  760. if (colSetting.type === 'Number') {
  761. const num = _.toNumber(value);
  762. if (num) {
  763. data[colSetting.field] = num;
  764. } else {
  765. try {
  766. data[colSetting.field] = math.evaluate(transExpr(value));
  767. } catch (err) {
  768. toastMessageUniq(hint.invalidExpr);
  769. continue;
  770. }
  771. }
  772. } else {
  773. if (node.used && (col.field === 'code' || col.field ==='b_code')
  774. && data[colSetting.field] !== '' && value === '') {
  775. toastMessageUniq(hint.usedCode);
  776. continue;
  777. }
  778. data[colSetting.field] = value;
  779. }
  780. bPaste = true;
  781. }
  782. if (bPaste) {
  783. datas.push(data);
  784. } else {
  785. filterNodes.push(node);
  786. }
  787. }
  788. }
  789. if (datas.length > 0) {
  790. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  791. const refreshNode = tree.loadPostData(result);
  792. if (refreshNode.update) {
  793. refreshNode.update = refreshNode.update.concat(filterNodes);
  794. }
  795. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  796. }, function () {
  797. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  798. });
  799. } else {
  800. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  801. }
  802. },
  803. deletePress: function (sheet) {
  804. if (!sheet.zh_setting) return;
  805. const sel = sheet.getSelections()[0], datas = [];
  806. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  807. let bDel = false;
  808. const node = sheet.zh_tree.nodes[iRow];
  809. const data = sheet.zh_tree.getNodeKeyData(node);
  810. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  811. const col = sheet.zh_setting.cols[iCol];
  812. const style = sheet.getStyle(iRow, iCol);
  813. if (style.locked || (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) >= 0 && node.b_code)) continue;
  814. if (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) < 0 && sheet.zh_tree.checkNodeUsed(node, pos)) {
  815. toastr.warning('"' + (node.code || '') + (node.b_code || '') + ' ' + node.name +'"已计量,请勿修改');
  816. return;
  817. }
  818. data[col.field] = null;
  819. const exprInfo = getExprInfo(col.field);
  820. if (exprInfo) {
  821. data[exprInfo.expr] = '';
  822. }
  823. bDel = true;
  824. }
  825. if (bDel) datas.push(data);
  826. }
  827. if (datas.length > 0) {
  828. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  829. const refreshNode = sheet.zh_tree.loadPostData(result);
  830. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  831. billsTreeSpreadObj.loadExprToInput(sheet);
  832. });
  833. }
  834. },
  835. pasteBlock: function (spread, copyInfo) {
  836. const self = this;
  837. const sheet = spread.getActiveSheet();
  838. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  839. postData(window.location.pathname + '/update', {
  840. postType: 'paste-block',
  841. postData: {
  842. id: tree.getNodeKey(node),
  843. tid: copyInfo.tid,
  844. block: copyInfo.block,
  845. }
  846. }, function (data) {
  847. pos.updateDatas(data.pos);
  848. const result = tree.loadPostData(data.ledger);
  849. self.refreshTree(sheet, result);
  850. const sel = sheet.getSelections()[0];
  851. if (sel) {
  852. sheet.setSelection(tree.nodes.indexOf(result.create[0]), sel.col, sel.rowCount, sel.colCount);
  853. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(result.create[0])]);
  854. }
  855. self.refreshOperationValid(sheet);
  856. removeLocalCache(copyBlockTag);
  857. }, null, true);
  858. },
  859. topRowChanged: function (e, info) {
  860. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  861. },
  862. editStarting(e, info) {
  863. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  864. const col = info.sheet.zh_setting.cols[info.col];
  865. const node = info.sheet.zh_tree.nodes[info.row];
  866. if (!node) {
  867. info.cancel = true;
  868. return;
  869. }
  870. const exprInfo = getExprInfo(col.field);
  871. if (exprInfo) {
  872. if (node[exprInfo.expr] && node[exprInfo.expr] !== '') {
  873. info.sheet.getCell(info.row, info.col).text(node[exprInfo.expr]);
  874. }
  875. }
  876. switch (col.field) {
  877. case 'code':
  878. const posRange = pos.getLedgerPos(node.id);
  879. info.cancel = posRange && posRange.length > 0;
  880. break;
  881. case 'unit_price':
  882. info.cancel = (node.children && node.children.length > 0) || node.used;
  883. break;
  884. case 'sgfh_qty':
  885. case 'sgfh_tp':
  886. case 'sjcl_qty':
  887. case 'sjcl_tp':
  888. case 'qtcl_qty':
  889. case 'qtcl_tp':
  890. case 'deal_qty':
  891. case 'deal_tp':
  892. info.cancel = (node.children && node.children.length > 0);
  893. break;
  894. case 'dgn_qty1':
  895. case 'dgn_qty2':
  896. info.cancel = !_.isEmpty(node.b_code);
  897. break;
  898. }
  899. },
  900. cut: function (sheet, sel, callback) {
  901. if (!sheet || !sel) return;
  902. if (sel.colCount >= sheet.zh_setting.cols.length) {
  903. toastr.warning('请勿选中整行剪切');
  904. return;
  905. }
  906. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  907. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  908. const node = sortData[iRow];
  909. if (node) {
  910. const data = sheet.zh_tree.getNodeKeyData(node);
  911. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  912. const col = sheet.zh_setting.cols[iCol];
  913. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  914. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  915. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  916. const lPos = pos.getLedgerPos(node.id);
  917. if (lPos && lPos.length > 0) {
  918. toastr.error('不可剪切');
  919. return;
  920. }
  921. }
  922. const style = sheet.getStyle(iRow, iCol);
  923. if (style.locked) {
  924. toastr.error('不可剪切');
  925. return;
  926. }
  927. const colSetting = sheet.zh_setting.cols[iCol];
  928. data[colSetting.field] = null;
  929. const exprInfo = getExprInfo(colSetting.field);
  930. if (exprInfo) {
  931. data[exprInfo.expr] = '';
  932. }
  933. }
  934. datas.push(data);
  935. }
  936. }
  937. if (datas.length > 0) {
  938. callback();
  939. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  940. const refreshNode = sheet.zh_tree.loadPostData(result);
  941. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  942. });
  943. }
  944. },
  945. sortCode: function (sheet) {
  946. const tree = sheet.zh_tree;
  947. if (!tree) return;
  948. const select = SpreadJsObj.getSelectObject(sheet);
  949. if (!select || !select.code || select.code === '') return;
  950. const recursiveSortCode = function (data, parentCode, children) {
  951. if (!children || children.length === 0) return;
  952. for (const [i, child] of children.entries()) {
  953. if (!child.b_code || child.b_code === '') {
  954. const code = parentCode + '-' + (i + 1);
  955. const cData = tree.getNodeKeyData(child);
  956. cData.code = code;
  957. data.push(cData);
  958. if (!tree.isLeafXmj(child)) {
  959. recursiveSortCode(data, code, child.children);
  960. }
  961. }
  962. }
  963. };
  964. const data = [];
  965. recursiveSortCode(data, select.code, select.children);
  966. if (data.length > 0) {
  967. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  968. const refreshNode = tree.loadPostData(result);
  969. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  970. })
  971. }
  972. }
  973. };
  974. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  975. billsTreeSpreadObj.loadExprToInput(billsSheet);
  976. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  977. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  978. if (!readOnly) {
  979. // 增删上下移升降级
  980. $('a[name="base-opr"]').click(function () {
  981. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  982. });
  983. $('a[name=cpc]').click(function () {
  984. billsSpread.commandManager().execute({
  985. cmd: this.getAttribute('type'),
  986. sheetName: billsSheet.name()
  987. });
  988. });
  989. $('#bills-expr').bind('change onblur', function () {
  990. if (this.readOnly) return;
  991. const expr = $(this);
  992. const row = expr.attr('data-row') ? _.toInteger(expr.attr('data-row')) : -1;
  993. const select = billsTree.getItemsByIndex(row);
  994. if (!select) return;
  995. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  996. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  997. const data = {
  998. id: select.id,
  999. tender_id: select.tender_id,
  1000. ledger_id: select.ledger_id
  1001. };
  1002. const exprInfo = getExprInfo(field);
  1003. if (newValue !== '') {
  1004. const num = _.toNumber(newValue);
  1005. if (num) {
  1006. data[field] = num;
  1007. if (exprInfo) {
  1008. data[exprInfo.expr] = '';
  1009. }
  1010. } else {
  1011. try {
  1012. data[field] = math.evaluate(transExpr(newValue));
  1013. if (exprInfo) {
  1014. data[exprInfo.expr] = newValue;
  1015. }
  1016. } catch (err) {
  1017. toastr.error('输入的表达式非法');
  1018. return;
  1019. }
  1020. }
  1021. } else {
  1022. data[field] = null;
  1023. if (exprInfo) {
  1024. data[exprInfo.expr] = '';
  1025. }
  1026. }
  1027. // 更新至服务器
  1028. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  1029. const refreshNode = billsTree.loadPostData(result);
  1030. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  1031. });
  1032. });
  1033. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  1034. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  1035. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  1036. billsSpread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  1037. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  1038. SpreadJsObj.Clipboard.setCopyData(copyText);
  1039. });
  1040. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  1041. SpreadJsObj.addCutEvents(billsSpread, billsTreeSpreadObj.cut);
  1042. }
  1043. // 右键菜单
  1044. let batchInsertObj;
  1045. $.contextMenu.types.batchInsert = function (item, opt, root) {
  1046. const self = this;
  1047. if ($.isFunction(item.icon)) {
  1048. item._icon = item.icon.call(this, this, $t, key, item);
  1049. } else {
  1050. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  1051. // to enable font awesome
  1052. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  1053. } else {
  1054. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  1055. }
  1056. }
  1057. this.addClass(item._icon);
  1058. const $obj = $('<div>' + item.name + '<input class="text-right ml-1 mr-1" type="tel" max="20" min="1" value="' + item.value + '" style="width: 30px; height: 18px; padding-right: 4px;">行</div>')
  1059. .appendTo(this);
  1060. const $input = $obj.find('input');
  1061. const event = () => {
  1062. if (self.hasClass('context-menu-disabled')) return;
  1063. item.batchInsert($input[0], root);
  1064. };
  1065. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  1066. $input.click((e) => {e.stopPropagation();})
  1067. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  1068. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  1069. };
  1070. const billsContextMenuOptions = {
  1071. selector: '#bills-spread',
  1072. build: function ($trigger, e) {
  1073. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  1074. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1075. },
  1076. items: {}
  1077. };
  1078. if (!readOnly) {
  1079. billsContextMenuOptions.items.create = {
  1080. name: '新增',
  1081. icon: 'fa-sign-in',
  1082. callback: function (key, opt) {
  1083. billsTreeSpreadObj.baseOpr(billsSheet, 'add');
  1084. },
  1085. disabled: function (key, opt) {
  1086. const sheet = billsSheet;
  1087. const selection = sheet.getSelections();
  1088. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1089. const row = sel ? sel.row : -1;
  1090. const tree = sheet.zh_tree;
  1091. if (!tree) return true;
  1092. const first = sheet.zh_tree.nodes[row];
  1093. const valid = !sheet.zh_setting.readOnly;
  1094. return !(valid && first && first.level > 1);
  1095. }
  1096. };
  1097. billsContextMenuOptions.items.delete = {
  1098. name: '删除',
  1099. icon: 'fa-remove',
  1100. callback: function (key, opt) {
  1101. billsTreeSpreadObj.baseOpr(billsSheet, 'delete');
  1102. },
  1103. disabled: function (key, opt) {
  1104. const sheet = billsSheet;
  1105. const selection = sheet.getSelections();
  1106. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1107. const row = sel ? sel.row : -1;
  1108. const tree = sheet.zh_tree;
  1109. if (!tree) return true;
  1110. const first = sheet.zh_tree.nodes[row];
  1111. let last = first, sameParent = true, nodeUsed = first.used;
  1112. if (sel.rowCount > 1 && first) {
  1113. for (let r = 1; r < sel.rowCount; r++) {
  1114. const rNode = tree.nodes[sel.row + r];
  1115. if (!rNode) {
  1116. sameParent = false;
  1117. break;
  1118. }
  1119. if (!rNode.ccid) {
  1120. return true;
  1121. }
  1122. nodeUsed = nodeUsed || rNode.used;
  1123. if (rNode.level > first.level) continue;
  1124. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  1125. sameParent = false;
  1126. break;
  1127. }
  1128. last = rNode;
  1129. }
  1130. }
  1131. const valid = !sheet.zh_setting.readOnly;
  1132. return !(valid && first && sameParent && !(first.level === 1 && first.node_type) && !nodeUsed && first.ccid);
  1133. }
  1134. };
  1135. billsContextMenuOptions.items.sprBase = '----';
  1136. }
  1137. billsContextMenuOptions.items.copyBlock = {
  1138. name: '复制整块',
  1139. icon: 'fa-files-o',
  1140. callback: function (key, opt) {
  1141. const copyBlockList = [];
  1142. const sheet = billsSheet;
  1143. const sel = sheet.getSelections()[0];
  1144. let iRow = sel.row;
  1145. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1146. while (iRow < sel.row + sel.rowCount) {
  1147. const node = sheet.zh_tree.nodes[iRow];
  1148. if (node.ledger_pid !== pid) {
  1149. toastr.error('仅可同时选中同层节点');
  1150. return;
  1151. }
  1152. const posterity = sheet.zh_tree.getPosterity(node);
  1153. iRow += posterity.length + 1;
  1154. posterity.unshift(node);
  1155. copyBlockList.push(sheet.zh_tree.getDefaultData(posterity));
  1156. }
  1157. for (const cbl of copyBlockList) {
  1158. for (const b of cbl) {
  1159. const posRange = pos.getLedgerPos(b.id);
  1160. if (posRange && posRange.length > 0) b.pos = posRange;
  1161. }
  1162. }
  1163. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  1164. },
  1165. visible: function (key, opt) {
  1166. const sheet = billsSheet;
  1167. const selection = sheet.getSelections();
  1168. const row = selection[0].row;
  1169. const select = billsTree.nodes[row];
  1170. return select;
  1171. },
  1172. disabled: function (key, opt) {
  1173. const sheet = billsSheet;
  1174. const selection = sheet.getSelections();
  1175. const row = selection[0].row;
  1176. const select = billsTree.nodes[row];
  1177. return select && select.level <= 1;
  1178. }
  1179. };
  1180. billsContextMenuOptions.items.copyBlockXmj = {
  1181. name: '复制整块(只复制项目节)',
  1182. icon: 'fa-files-o',
  1183. callback: function (key, opt) {
  1184. const copyBlockList = [];
  1185. const sheet = billsSheet;
  1186. const sel = sheet.getSelections()[0];
  1187. let iRow = sel.row;
  1188. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1189. while (iRow < sel.row + sel.rowCount) {
  1190. const node = sheet.zh_tree.nodes[iRow];
  1191. if (node.ledger_pid !== pid) {
  1192. toastr.error('仅可同时选中同层节点');
  1193. return;
  1194. }
  1195. const posterity = sheet.zh_tree.getPosterity(node);
  1196. iRow += posterity.length + 1;
  1197. const copyPosterity = posterity.filter(x => { return !x.b_code; });
  1198. copyPosterity.unshift(node);
  1199. const copyData = sheet.zh_tree.getDefaultData(copyPosterity);
  1200. for (const p of copyData) {
  1201. const children = copyData.filter(y => {return y.ledger_pid === p.ledger_id}) || [];
  1202. p.is_leaf = children.length === 0;
  1203. }
  1204. copyBlockList.push(copyData);
  1205. }
  1206. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  1207. },
  1208. visible: function (key, opt) {
  1209. const sheet = billsSheet;
  1210. const selection = sheet.getSelections();
  1211. const row = selection[0].row;
  1212. const select = billsTree.nodes[row];
  1213. return select;
  1214. },
  1215. disabled: function (key, opt) {
  1216. const sheet = billsSheet;
  1217. const selection = sheet.getSelections();
  1218. const row = selection[0].row;
  1219. const select = billsTree.nodes[row];
  1220. return select && select.level <= 1;
  1221. }
  1222. };
  1223. if (!readOnly) {
  1224. billsContextMenuOptions.items.pasteBlock = {
  1225. name: '粘贴整块',
  1226. icon: 'fa-clipboard',
  1227. disabled: function (key, opt) {
  1228. //const block = treeOperationObj.block || [];
  1229. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1230. return !(copyInfo && copyInfo.block && copyInfo.block.length > 0);
  1231. },
  1232. callback: function (key, opt) {
  1233. //const block = treeOperationObj.block || [];
  1234. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1235. if (copyInfo.block.length > 0) {
  1236. billsTreeSpreadObj.pasteBlock(billsSpread, copyInfo);
  1237. } else {
  1238. document.execCommand('paste');
  1239. }
  1240. },
  1241. visible: function (key, opt) {
  1242. return !readOnly;
  1243. }
  1244. };
  1245. billsContextMenuOptions.items.sprBlock = '----';
  1246. }
  1247. if (!readOnly) {
  1248. billsContextMenuOptions.items.batchInsert = {
  1249. name: '批量插入',
  1250. type: 'batchInsert',
  1251. value: '2',
  1252. icon: 'fa-sign-in',
  1253. batchInsert: function (obj, root) {
  1254. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  1255. obj.value = obj.max;
  1256. toastr.warning('批量插入不可多于' + obj.max);
  1257. } else if (_.toNumber(obj.value) < _.toNumber(obj.min)) {
  1258. obj.value = obj.min;
  1259. toastr.warning('批量插入不可少于' + obj.min);
  1260. } else {
  1261. billsTreeSpreadObj.baseOpr(billsSheet, 'add', parseInt(obj.value));
  1262. root.$menu.trigger('contextmenu:hide');
  1263. }
  1264. },
  1265. disabled: function (key, opt) {
  1266. const sheet = billsSheet;
  1267. const selection = sheet.getSelections();
  1268. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1269. const row = sel ? sel.row : -1;
  1270. const tree = sheet.zh_tree;
  1271. if (!tree) return true;
  1272. const first = sheet.zh_tree.nodes[row];
  1273. const valid = !sheet.zh_setting.readOnly;
  1274. return !(valid && first && first.level > 1);
  1275. }
  1276. };
  1277. billsContextMenuOptions.items.batchInsertBillsPos = {
  1278. name: '批量插入清单-计量单元',
  1279. icon: 'fa-sign-in',
  1280. visible: function (key, opt) {
  1281. return false;
  1282. },
  1283. disabled: function (key, opt) {
  1284. if (!isTz) return true;
  1285. const select = SpreadJsObj.getSelectObject(billsSheet);
  1286. if (select) {
  1287. if (select.code && select.code !== '') {
  1288. return !billsTree.isLeafXmj(select);
  1289. } else {
  1290. const parent = billsTree.getParent(select);
  1291. return !(parent && billsTree.isLeafXmj(parent));
  1292. }
  1293. } else {
  1294. return false;
  1295. }
  1296. },
  1297. callback: function (key, opt) {
  1298. if (!batchInsertObj) {
  1299. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  1300. } else {
  1301. batchInsertObj.initView();
  1302. }
  1303. $('#batch').modal('show');
  1304. }
  1305. };
  1306. }
  1307. billsContextMenuOptions.items.sprTag = '----';
  1308. billsContextMenuOptions.items.showLast = {
  1309. name: '显示至最底层',
  1310. callback: function (key, opt, menu, e) {
  1311. const node = SpreadJsObj.getSelectObject(billsSheet);
  1312. setTimeout(() => {
  1313. showWaitingView();
  1314. billsTree.expandByCustom(x => {
  1315. return x.expanded || (x.id === node.id) || (x.full_path.indexOf(node.ledger_id + '-') >= 0);
  1316. });
  1317. SpreadJsObj.refreshTreeRowVisible(billsSheet);
  1318. closeWaitingView();
  1319. }, 100);
  1320. },
  1321. };
  1322. $.contextMenu(billsContextMenuOptions);
  1323. // 计量单元 相关方法&绑定spreadjs事件
  1324. const posSpreadObj = {
  1325. refreshOperationValid: function (sheet, selection) {
  1326. const setObjEnable = function (obj, enable) {
  1327. if (enable) {
  1328. obj.removeClass('disabled');
  1329. } else {
  1330. obj.addClass('disabled');
  1331. }
  1332. };
  1333. const invalidAll = function () {
  1334. setObjEnable($('a[name=pos-opr][type=up-move]'), false);
  1335. setObjEnable($('a[name=pos-opr][type=down-move]'), false);
  1336. };
  1337. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1338. const row = sel ? sel.row : -1;
  1339. const first = sheet.zh_data[row];
  1340. if (!first) {
  1341. invalidAll();
  1342. return;
  1343. }
  1344. let last = first;
  1345. if (sel.rowCount > 1 && first) {
  1346. for (let r = 1; r < sel.rowCount; r++) {
  1347. const rNode = sheet.zh_data[sel.row + r];
  1348. if (!rNode) break;
  1349. last = rNode;
  1350. }
  1351. }
  1352. const preNode = sheet.zh_data[row - 1];
  1353. const valid = !sheet.zh_setting.readOnly;
  1354. setObjEnable($('a[name=pos-opr][type=up-move]'), valid && first && preNode && first.ccid);
  1355. setObjEnable($('a[name=pos-opr][type=down-move]'), valid && first && (sheet.zh_data.indexOf(last) < sheet.zh_data.length - 1) && first.ccid);
  1356. },
  1357. loadExprToInput: function () {
  1358. const sel = posSheet.getSelections()[0];
  1359. if (!sel) return;
  1360. const col = posSheet.zh_setting.cols[sel.col];
  1361. const cell = posSheet.getCell(sel.col, sel.col);
  1362. if (col && col.type === 'Number') {
  1363. const data = SpreadJsObj.getSelectObject(posSheet);
  1364. if (data) {
  1365. const exprInfo = getExprInfo(col.field);
  1366. const value = exprInfo
  1367. ? (data[exprInfo.expr] ? data[exprInfo.expr] : data[col.field])
  1368. : data[col.field];
  1369. $('#pos-expr').val(value).attr('field', col.field).attr('org', data[col.field])
  1370. .attr('readOnly', readOnly || (!data.ccid && cell.locked())).attr('data-row', sel.row);
  1371. } else {
  1372. $('#pos-expr').val('').attr('readOnly', true);
  1373. $('#pos-expr').removeAttr('data-row');
  1374. }
  1375. } else {
  1376. $('#pos-expr').val('').attr('readOnly', true);
  1377. $('#pos-expr').removeAttr('data-row');
  1378. }
  1379. },
  1380. /**
  1381. * 加载计量单元 根据当前台账选择节点
  1382. */
  1383. loadCurPosData: function () {
  1384. const node = SpreadJsObj.getSelectObject(billsSheet);
  1385. // SpreadJsObj.initSpreadSettingEvents(posSpreadSetting, posCol);
  1386. // SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  1387. if (node) {
  1388. const posData = pos.getLedgerPos(node.id) || [];
  1389. SpreadJsObj.loadSheetData(posSheet, 'data', posData);
  1390. posSheet.zh_setting.readOnly = readOnly || (node.used && posData.length === 0);
  1391. } else {
  1392. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  1393. posSheet.zh_setting.readOnly = true;
  1394. }
  1395. SpreadJsObj.massOperationSheet(posSheet, function () {
  1396. if (posSheet.zh_setting) {
  1397. posSheet.zh_setting.cols.forEach(function (col, i) {
  1398. for (let iRow = 0; iRow < posSheet.getRowCount(); iRow++) {
  1399. posSheet.getCell(iRow, i).locked((posSheet.zh_data[iRow] && !posSheet.zh_data[iRow].ccid) || posSheet.zh_setting.readOnly || false);
  1400. }
  1401. });
  1402. }
  1403. });
  1404. // SpreadJsObj.refreshSheetReadOnly(posSheet);
  1405. posSpreadObj.loadExprToInput();
  1406. posSpreadObj.refreshOperationValid(posSheet);
  1407. },
  1408. baseOpr: function (sheet, type) {
  1409. const data = {
  1410. postType: 'pos',
  1411. posPostType: type === 'delete' ? type : 'update',
  1412. postData: [],
  1413. };
  1414. const selection = sheet.getSelections();
  1415. const row = selection[0].row, count = selection[0].rowCount;
  1416. const first = sheet.zh_data[row];
  1417. if (type === 'delete') {
  1418. for (let iRow = 0; iRow < count; iRow++) {
  1419. const posData = sheet.zh_data[iRow + row];
  1420. if (posData) {
  1421. if (posData.used) {
  1422. toastr.error('"' + posData.name + '"已计量,请勿删除');
  1423. return;
  1424. }
  1425. data.postData.push(sheet.zh_data[iRow + row].id);
  1426. }
  1427. }
  1428. } else if (type === 'up-move') {
  1429. const pre = sheet.zh_data[row - 1], preUpdate = {id: pre.id};
  1430. for (let iRow = 0; iRow < count; iRow++) {
  1431. const posData = sheet.zh_data[iRow + row];
  1432. if (posData) {
  1433. data.postData.push({id: posData.id, lid: posData.lid, porder: sheet.zh_data[iRow + row - 1].porder});
  1434. preUpdate.porder = posData.porder;
  1435. }
  1436. }
  1437. data.postData.push(preUpdate);
  1438. } else if (type === 'down-move') {
  1439. const next = sheet.zh_data[row + count], nextUpdate = {id: next.id};
  1440. for (let iRow = count - 1; iRow >= 0; iRow--) {
  1441. const posData = sheet.zh_data[iRow + row];
  1442. if (posData) {
  1443. data.postData.push({id: posData.id, lid: posData.lid, porder: sheet.zh_data[iRow + row + 1].porder});
  1444. nextUpdate.porder = posData.porder;
  1445. }
  1446. }
  1447. data.postData.push(nextUpdate);
  1448. }
  1449. if (data.postData.length > 0) {
  1450. postData(window.location.pathname + '/update', data, function (result) {
  1451. if (type === 'delete') {
  1452. pos.removeDatas(result.pos);
  1453. sheet.deleteRows(row, count);
  1454. const loadResult = billsTree.loadPostData(result.ledger);
  1455. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1456. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1457. posSpreadObj.refreshOperationValid(posSheet);
  1458. } else {
  1459. pos.updateDatas(result.pos);
  1460. const sel = selection[0];
  1461. if (sel) {
  1462. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  1463. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, sheet.zh_data.indexOf(first)]);
  1464. }
  1465. posSpreadObj.loadCurPosData();
  1466. }
  1467. });
  1468. }
  1469. },
  1470. editStarting: function (e, info) {
  1471. posSpreadObj.billsNode = SpreadJsObj.getSelectObject(billsSheet);
  1472. const sel = info.sheet.getSelections();
  1473. if (!sel || !sel[0]) return;
  1474. const col = info.sheet.zh_setting.cols[sel[0].col];
  1475. const node = SpreadJsObj.getSelectObject(info.sheet);
  1476. const exprInfo = getExprInfo(col.field);
  1477. if (node && exprInfo) {
  1478. if (node[exprInfo.expr] && node[exprInfo.expr] !== '') {
  1479. info.sheet.getCell(info.row, info.col).text(node[exprInfo.expr]);
  1480. }
  1481. }
  1482. },
  1483. /**
  1484. * 编辑单元格响应事件
  1485. * @param {Object} e
  1486. * @param {Object} info
  1487. */
  1488. editEnded: function (e, info) {
  1489. if (!info.sheet.zh_setting) {
  1490. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1491. return;
  1492. }
  1493. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  1494. const col = info.sheet.zh_setting.cols[info.col];
  1495. const orgText = posData ? posData[col.field] : null;
  1496. const newText = trimInvalidChar(info.editingText);
  1497. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) return;
  1498. const node = posSpreadObj.billsNode;
  1499. if (!node) {
  1500. toastr.error('数据错误,请选择台账节点后再试');
  1501. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1502. return;
  1503. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  1504. toastr.error('父节点不可插入计量单元');
  1505. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1506. return;
  1507. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  1508. toastr.error('项目节不可插入计量单元');
  1509. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1510. return;
  1511. }
  1512. const data = {postType: 'pos'};
  1513. if (col.field === 'name') {
  1514. if (newText === '' && posData) {
  1515. toastr.error('部位名称不可为空', 'error');
  1516. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1517. return;
  1518. } else if (!posData) {
  1519. if (newText && newText !== '') {
  1520. data.posPostType = 'add';
  1521. const sortData = info.sheet.zh_data;
  1522. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  1523. data.postData = { name: newText, lid: node.id, porder: order};
  1524. } else {
  1525. return;
  1526. }
  1527. } else {
  1528. data.posPostType = 'update';
  1529. data.postData = {id: posData.id, name: newText};
  1530. }
  1531. } else if (!posData) {
  1532. toastr.warning('新增计量单元请先输入名称');
  1533. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1534. return;
  1535. } else {
  1536. data.posPostType = 'update';
  1537. data.postData = {id: posData.id};
  1538. if (col.type === 'Number') {
  1539. const exprInfo = getExprInfo(col.field);
  1540. const num = _.toNumber(newText);
  1541. if (_.isFinite(num)) {
  1542. data.postData[col.field] = num;
  1543. if (exprInfo) {
  1544. data.postData[exprInfo.expr] = '';
  1545. }
  1546. } else {
  1547. try {
  1548. data.postData[col.field] = math.evaluate(transExpr(newText));
  1549. if (exprInfo) {
  1550. data.postData[exprInfo.expr] = newText;
  1551. }
  1552. } catch(err) {
  1553. toastr.error('输入的表达式非法');
  1554. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1555. return;
  1556. }
  1557. }
  1558. } else {
  1559. data.postData[col.field] = newText;
  1560. }
  1561. }
  1562. postData(window.location.pathname + '/update', data, function (result) {
  1563. const updateRst = pos.updateDatas(result.pos);
  1564. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1565. if (updateRst.create.length > 0) {
  1566. posSpreadObj.loadCurPosData();
  1567. } else {
  1568. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1569. }
  1570. const loadResult = billsTree.loadPostData(result.ledger);
  1571. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1572. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1573. }, function () {
  1574. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1575. });
  1576. },
  1577. /**
  1578. * 删除按钮响应事件
  1579. * @param sheet
  1580. */
  1581. deletePress: function (sheet) {
  1582. if (!sheet.zh_setting) return;
  1583. const sortData = sheet.zh_data;
  1584. const datas = [], posSelects = [];
  1585. const sel = sheet.getSelections()[0];
  1586. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  1587. let bDel = false;
  1588. const node = sortData[iRow];
  1589. if (node) {
  1590. const data = {id: node.id};
  1591. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  1592. const colSetting = sheet.zh_setting.cols[iCol];
  1593. if (colSetting.field === 'name') {
  1594. toastr.error('部位名称不能为空');
  1595. return;
  1596. }
  1597. const style = sheet.getStyle(iRow, iCol);
  1598. if (!style.locked) {
  1599. const colSetting = sheet.zh_setting.cols[iCol];
  1600. data[colSetting.field] = null;
  1601. const exprInfo = getExprInfo(colSetting.field);
  1602. if (exprInfo) {
  1603. data[exprInfo.expr] = '';
  1604. }
  1605. bDel = true;
  1606. }
  1607. }
  1608. if (bDel) {
  1609. datas.push(data);
  1610. posSelects.push(node);
  1611. }
  1612. }
  1613. }
  1614. if (datas.length > 0) {
  1615. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: datas}, function (result) {
  1616. pos.updateDatas(result.pos);
  1617. posSpreadObj.loadCurPosData();
  1618. const loadResult = billsTree.loadPostData(result.ledger);
  1619. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1620. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1621. }, function () {
  1622. posSpreadObj.loadCurPosData();
  1623. });
  1624. }
  1625. },
  1626. /**
  1627. * 删除 计量单元
  1628. * @param sheet
  1629. */
  1630. deletePos: function (sheet) {
  1631. const selection = sheet.getSelections();
  1632. const data = {
  1633. postType: 'pos',
  1634. posPostType: 'delete',
  1635. postData: [],
  1636. };
  1637. const row = selection[0].row, count = selection[0].rowCount;
  1638. const sortData = sheet.zh_data;
  1639. for (let iRow = 0; iRow < count; iRow++) {
  1640. const posData = sortData[iRow + row];
  1641. if (posData) {
  1642. if (posData.used) {
  1643. toastr.error('"' + posData.name + '"已计量,请勿删除');
  1644. return;
  1645. }
  1646. data.postData.push(sortData[iRow + row].id);
  1647. }
  1648. }
  1649. if (data.postData.length > 0) {
  1650. postData(window.location.pathname + '/update', data, function (result) {
  1651. pos.removeDatas(result.pos);
  1652. sheet.deleteRows(row, count);
  1653. const loadResult = billsTree.loadPostData(result.ledger);
  1654. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1655. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1656. });
  1657. }
  1658. },
  1659. /**
  1660. * 粘贴单元格响应事件
  1661. * @param e
  1662. * @param info
  1663. */
  1664. clipboardPasted: function (e, info) {
  1665. if (info.sheet.getColumnCount() > info.sheet.zh_setting.cols.length) {
  1666. info.sheet.setColumnCount(info.sheet.zh_setting.cols.length);
  1667. }
  1668. const node = SpreadJsObj.getSelectObject(billsSheet);
  1669. if (node.code && (node.code !== '')) {
  1670. toastr.error('项目节不可含有节点明细');
  1671. posSpreadObj.loadCurPosData();
  1672. return;
  1673. }
  1674. if (node.children && (node.children.length > 0)) {
  1675. toastr.error('仅节点子项可以含有计量单元');
  1676. posSpreadObj.loadCurPosData();
  1677. return;
  1678. }
  1679. if (!info.sheet.zh_setting) {
  1680. posSpreadObj.loadCurPosData();
  1681. return;
  1682. }
  1683. const data = [];
  1684. const sortData = info.sheet.zh_data || [];
  1685. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1686. if (info.cellRange.col !== 0) {
  1687. toastr.warning('新增计量单元请先输入名称');
  1688. posSpreadObj.loadCurPosData();
  1689. return;
  1690. }
  1691. }
  1692. let bHint = false;
  1693. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1694. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  1695. let bPaste = true;
  1696. const curRow = info.cellRange.row + iRow;
  1697. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  1698. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  1699. const curCol = info.cellRange.col + iCol;
  1700. const colSetting = info.sheet.zh_setting.cols[curCol];
  1701. if (!colSetting) continue;
  1702. posData[colSetting.field] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1703. if (colSetting.type === 'Number') {
  1704. const num = _.toNumber(posData[colSetting.field]);
  1705. if (num) {
  1706. posData[colSetting.field] = num;
  1707. } else {
  1708. try {
  1709. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  1710. const exprInfo = getExprInfo(colSetting.field);
  1711. if (exprInfo) {
  1712. posData[exprInfo.expr] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1713. }
  1714. } catch (err) {
  1715. if (!bHint) {
  1716. toastr.warning('粘贴了非法表达式,已过滤');
  1717. bHint = true;
  1718. }
  1719. bPaste = false;
  1720. }
  1721. }
  1722. }
  1723. }
  1724. if (bPaste) {
  1725. data.push(posData);
  1726. }
  1727. }
  1728. if (data.length === 0) {
  1729. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1730. return;
  1731. }
  1732. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: data}, function (result) {
  1733. pos.updateDatas(result.pos);
  1734. posSpreadObj.loadCurPosData();
  1735. const loadResult = billsTree.loadPostData(result.ledger);
  1736. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1737. posSpreadObj.loadCurPosData();
  1738. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1739. }, function () {
  1740. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1741. });
  1742. },
  1743. selectionChanged: function (e, info) {
  1744. posSpreadObj.loadExprToInput();
  1745. posSpreadObj.refreshOperationValid(posSheet);
  1746. },
  1747. addPegs: function (pegs) {
  1748. if (!pegs || pegs.length <= 0) return;
  1749. const node = SpreadJsObj.getSelectObject(billsSheet);
  1750. if (!node) return;
  1751. const sheet = posSpread.getActiveSheet();
  1752. const sortData = sheet.zh_data || [];
  1753. let order = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1754. pegs.forEach(function (p) {p.porder = ++order; p.lid = node.id});
  1755. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: pegs}, function (result) {
  1756. pos.updateDatas(result.pos);
  1757. posSpreadObj.loadCurPosData();
  1758. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1759. });
  1760. }
  1761. };
  1762. posSpread.bind(spreadNS.Events.SelectionChanged, posSpreadObj.selectionChanged);
  1763. if (!readOnly && isTz) {
  1764. $('a[name="pos-opr"]').click(function () {
  1765. posSpreadObj.baseOpr(posSheet, this.getAttribute('type'));
  1766. });
  1767. $('#pos-expr').bind('change onblur', function () {
  1768. if (this.readOnly) return;
  1769. const expr = $(this);
  1770. const row = expr.attr('data-row') ? _.toInteger(expr.attr('data-row')) : -1;
  1771. const select = posSheet.zh_data ? posSheet.zh_data[row] : null;
  1772. if (!select) return;
  1773. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  1774. if (orgValue === newValue || (!orgValue && newValue == '')) return;
  1775. const data = {id: select.id};
  1776. const exprInfo = getExprInfo(field);
  1777. if (newValue !== '') {
  1778. const num = _.toNumber(newValue);
  1779. if (num) {
  1780. data[field] = num;
  1781. if (exprInfo) data[exprInfo.expr] = '';
  1782. } else {
  1783. try {
  1784. data[field] = math.evaluate(transExpr(newValue));
  1785. if (exprInfo) data[exprInfo.expr] = newValue;
  1786. } catch (err) {
  1787. toastr.error('输入的表达式非法');
  1788. return;
  1789. }
  1790. }
  1791. } else {
  1792. data[field] = null;
  1793. if (exprInfo) data[exprInfo.expr] = '';
  1794. }
  1795. // 更新至服务器
  1796. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: data}, function (result) {
  1797. const updateRst = pos.updateDatas(result.pos);
  1798. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1799. SpreadJsObj.reLoadRowData(posSheet, row);
  1800. const loadResult = billsTree.loadPostData(result.ledger);
  1801. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1802. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1803. });
  1804. });
  1805. posSpread.bind(spreadNS.Events.EditStarting, posSpreadObj.editStarting);
  1806. posSpread.bind(spreadNS.Events.EditEnded, posSpreadObj.editEnded);
  1807. posSpread.bind(spreadNS.Events.ClipboardPasted, posSpreadObj.clipboardPasted);
  1808. SpreadJsObj.addDeleteBind(posSpread, posSpreadObj.deletePress);
  1809. $.contextMenu({
  1810. selector: '#pos-spread',
  1811. build: function ($trigger, e) {
  1812. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  1813. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1814. },
  1815. items: {
  1816. 'delete': {
  1817. name: '删除',
  1818. icon: 'fa-remove',
  1819. disabled: function (key, opt) {
  1820. if (posSheet.zh_data) {
  1821. const selection = posSheet.getSelections();
  1822. return (posSheet.zh_data.length < selection[0].row + selection[0].rowCount) || (posSheet.zh_data[selection[0].row] && !posSheet.zh_data[selection[0].row].ccid);
  1823. } else {
  1824. return true;
  1825. }
  1826. },
  1827. callback: function (key, opt) {
  1828. posSpreadObj.deletePos(posSheet);
  1829. }
  1830. },
  1831. }
  1832. });
  1833. }
  1834. // 加载清单&计量单元数据
  1835. const preUrl = window.location.pathname.split('/').slice(0, 4).join('/');
  1836. postData(preUrl + '/defaultBills', { from: 'revise' }, function (result) {
  1837. billsTree.loadDatas(result.bills);
  1838. pos.loadDatas(result.pos);
  1839. treeCalc.calculateAll(billsTree);
  1840. SpreadJsObj.loadSheetData(billsSheet, SpreadJsObj.DataType.Tree, billsTree);
  1841. SpreadJsObj.loadTopAndSelect(billsSheet, ckBillsSpread);
  1842. checkShowLast(result.bills.length);
  1843. posSpreadObj.loadCurPosData();
  1844. SpreadJsObj.resetTopAndSelect(posSheet);
  1845. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1846. billsTreeSpreadObj.loadExprToInput(billsSheet);
  1847. checkList.loadHisCheckData();
  1848. }, null);
  1849. $.divResizer({
  1850. select: '#revise-resize',
  1851. callback: function () {
  1852. billsSpread.refresh();
  1853. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1854. $(".sp-wrap").height(bcontent-30);
  1855. posSpread.refresh();
  1856. }
  1857. });
  1858. class DealBills {
  1859. constructor (selector, spreadSetting) {
  1860. const self = this;
  1861. this.loaded = false;
  1862. this.obj = $(selector)[0];
  1863. this.url = '/tender/' + window.location.pathname.split('/')[2] + '/deal';
  1864. this.spreadSetting = spreadSetting;
  1865. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1866. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1867. if (!readOnly) {
  1868. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1869. const dealSheet = info.sheet;
  1870. const mainSheet = billsSheet;
  1871. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1872. if (!dealBills) { return; }
  1873. const mainTree = mainSheet.zh_tree;
  1874. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1875. if (!mainNode || !mainTree) { return; }
  1876. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1877. toastr.warning('非最底层项目下,不应添加节点');
  1878. return;
  1879. }
  1880. postData(window.location.pathname + '/update', {
  1881. postType: 'add-deal',
  1882. postData: {
  1883. id: mainNode.ledger_id,
  1884. type: mainNode.code ? 'child' : 'next',
  1885. dealBills: {
  1886. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1887. unit_price: dealBills.unit_price,
  1888. }
  1889. },
  1890. }, function (result) {
  1891. const refreshData = mainTree.loadPostData(result);
  1892. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1893. const sel = mainSheet.getSelections()[0];
  1894. if (sel && refreshData.create[0]) {
  1895. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1896. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, mainTree.nodes.indexOf(refreshData.create[0])]);
  1897. }
  1898. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1899. billsSpread.focus();
  1900. posSpreadObj.loadCurPosData();
  1901. });
  1902. });
  1903. }
  1904. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1905. }
  1906. loadData (callback) {
  1907. if (this.loaded) {
  1908. if (callback) callback();
  1909. return;
  1910. }
  1911. const self = this;
  1912. postData(this.url+'/get-data', {}, function (data) {
  1913. self.data = data;
  1914. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1915. self.loaded = true;
  1916. if (callback) callback();
  1917. });
  1918. }
  1919. calculateData () {
  1920. if (this.data) {
  1921. for (const d of this.data) {
  1922. d.total_price = _.multiply(d.quantity, d.unit_price);
  1923. }
  1924. }
  1925. }
  1926. }
  1927. class BatchInsertBillsPosObj {
  1928. constructor (obj) {
  1929. const self = this;
  1930. this.obj = obj;
  1931. this.billsCount = 6;
  1932. this.posCount = 1000;
  1933. this.filter = getLocalCache('zh-calc-batch-filter');
  1934. if (!this.filter) {
  1935. this.filter = '1';
  1936. }
  1937. $('input[name=batch-filter]')[0].checked = this.filter && this.filter != '0';
  1938. // 初始化 清单编号窗口 参数
  1939. this.qdSpreadSetting = {
  1940. cols: [
  1941. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1942. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1943. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1944. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1945. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1946. ],
  1947. emptyRows: this.billsCount,
  1948. headRows: 1,
  1949. headRowHeight: [32],
  1950. headerFont: '12px 微软雅黑',
  1951. font: '12px 微软雅黑',
  1952. };
  1953. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1954. // 初始化 部位数量复核表 参数
  1955. this.posSpreadSetting = {
  1956. cols: [
  1957. {title: '名称', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1958. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1959. ],
  1960. emptyRows: this.posCount,
  1961. headRows: 1,
  1962. headRowHeight: [32],
  1963. headerFont: '12px 微软雅黑',
  1964. font: '12px 微软雅黑',
  1965. };
  1966. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1967. this.posSpreadSetting.cols.push(
  1968. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1969. )
  1970. }
  1971. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1972. // 初始化 签约节点 参数
  1973. this.dealSpreadSetting = {
  1974. cols: [
  1975. {title: '清单编号', field: 'code', width: 80, hAlign: 0, formatter: '@', readOnly: true},
  1976. {title: '名称', field: 'name', width: 230, hAlign: 0, formatter: '@', readOnly: true},
  1977. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1978. {title: '单价', field: 'unit_price', hAlign: 2, width: 60, readOnly: true},
  1979. ],
  1980. emptyRows: 0,
  1981. headRows: 1,
  1982. headRowHeight: [32],
  1983. headerFont: '12px 微软雅黑',
  1984. font: '12px 微软雅黑',
  1985. };
  1986. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1987. // 初始化 清单编号、部位数量复核表 表格
  1988. this.initView();
  1989. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1990. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1991. // 拉取签约节点数据
  1992. dealBills.loadData(() => {
  1993. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1994. });
  1995. // 双击签约节点,自动添加到清单编号窗口
  1996. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1997. const deal = info.sheet.zh_data[info.row];
  1998. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  1999. const sel = qdSheet.getSelections()[0];
  2000. qdSheet.getCell(sel.row, 0).value(deal.code);
  2001. qdSheet.getCell(sel.row, 1).value(deal.name);
  2002. qdSheet.getCell(sel.row, 2).value(deal.unit);
  2003. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  2004. if (sel.row + 1 === qdSheet.getRowCount()) {
  2005. const count = sel.row + 2;
  2006. qdSheet.setRowCount(count);
  2007. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  2008. const colCount = posSheet.getColumnCount() + 1;
  2009. posSheet.setColumnCount(colCount);
  2010. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('清单' + count);
  2011. }
  2012. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  2013. qdSheet.getParent().focus();
  2014. });
  2015. this.qdSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  2016. const billsCount = info.sheet.getRowCount(), posSheet = self.posSpread.getActiveSheet();
  2017. const count = posSheet.getColumnCount() - 2;
  2018. if (billsCount > count) {
  2019. posSheet.setColumnCount(billsCount + 2);
  2020. for (let i = count + 1; i <= billsCount; i++) {
  2021. info.sheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  2022. posSheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  2023. }
  2024. }
  2025. if (info.cellRange.col === 0 && info.cellRange.colCount === 1) {
  2026. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  2027. if (dealBills && dealBills.length > 0) {
  2028. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  2029. const curRow = iRow + info.cellRange.row;
  2030. const bills = _.find(dealBills, {code: info.sheet.getText(curRow, 0)});
  2031. if (bills) {
  2032. info.sheet.getCell(curRow, 1).value(bills.name);
  2033. info.sheet.getCell(curRow, 2).value(bills.unit);
  2034. info.sheet.getCell(curRow, 3).value(bills.unit_price);
  2035. }
  2036. }
  2037. }
  2038. }
  2039. });
  2040. this.posSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  2041. const billsCount = info.sheet.getColumnCount() - 2, qdSheet = self.qdSpread.getActiveSheet();
  2042. const count = qdSheet.getRowCount();
  2043. if (billsCount > count) {
  2044. qdSheet.setRowCount(billsCount);
  2045. for (let i = count + 1; i <= billsCount; i++) {
  2046. qdSheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  2047. info.sheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  2048. }
  2049. }
  2050. });
  2051. this.qdSpread.bind(spreadNS.Events.EditEnded, function (e, info) {
  2052. if (info.col === 0) {
  2053. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  2054. if (dealBills && dealBills.length > 0) {
  2055. const bills = _.find(dealBills, {code: info.editingText});
  2056. if (bills) {
  2057. info.sheet.getCell(info.row, 1).value(bills.name);
  2058. info.sheet.getCell(info.row, 2).value(bills.unit);
  2059. info.sheet.getCell(info.row, 3).value(bills.unit_price);
  2060. }
  2061. }
  2062. }
  2063. });
  2064. this.obj.bind('shown.bs.modal', function () {
  2065. self.qdSpread.refresh();
  2066. self.posSpread.refresh();
  2067. self.dealSpread.refresh();
  2068. });
  2069. $('#batch-ok').click(function () {
  2070. const selection = billsSheet.getSelections();
  2071. const sel = selection[0];
  2072. const row = selection[0].row;
  2073. const select = billsTree.nodes[row];
  2074. if (select) {
  2075. const insertData = {};
  2076. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  2077. insertData.id = select[billsTree.setting.id];
  2078. insertData.batchData = self.getBatchData();
  2079. if (insertData.batchData.length > 0) {
  2080. postData(window.location.pathname + '/update', {
  2081. postType: 'batch-insert',
  2082. postData: insertData
  2083. }, function (data) {
  2084. pos.updateDatas(data.pos);
  2085. const result = billsTree.loadPostData(data.ledger);
  2086. billsTreeSpreadObj.refreshTree(billsSheet, result);
  2087. billsSheet.setSelection(result.create[0].index, sel.col, sel.rowCount, sel.colCount);
  2088. SpreadJsObj.reloadRowsBackColor(billsSheet, [sel.row, result.create[0].index]);
  2089. billsTreeSpreadObj.refreshOperationValid(billsSheet, selection);
  2090. posSpreadObj.loadCurPosData();
  2091. self.obj.modal('hide');
  2092. }, null, true);
  2093. } else {
  2094. self.obj.modal('hide');
  2095. }
  2096. }
  2097. });
  2098. $('input[name=batch-filter]').change(function () {
  2099. setLocalCache('zh-calc-batch-filter', this.checked ? 1 : 0);
  2100. });
  2101. $.contextMenu({
  2102. selector: '.batch-l-t',
  2103. build: function ($trigger, e) {
  2104. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.qdSpread);
  2105. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  2106. },
  2107. items: {
  2108. 'create': {
  2109. name: '新增行',
  2110. icon: 'fa-sign-in',
  2111. callback: function (key, opt) {
  2112. const qdSheet = self.qdSpread.getActiveSheet();
  2113. const posSheet = self.posSpread.getActiveSheet();
  2114. qdSheet.addRows(qdSheet.getRowCount(), 1);
  2115. const index = qdSheet.getRowCount();
  2116. qdSheet.getCell(qdSheet.getRowCount() - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + index);
  2117. posSheet.addColumns(posSheet.getColumnCount(), 1);
  2118. posSheet.getCell(0, index + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + index);
  2119. },
  2120. },
  2121. 'delete': {
  2122. name: '删除行',
  2123. icon: 'fa-remove',
  2124. callback: function (key, opt) {
  2125. const qdSheet = self.qdSpread.getActiveSheet();
  2126. const posSheet = self.posSpread.getActiveSheet();
  2127. const sel = qdSheet.getSelections()[0];
  2128. qdSheet.deleteRows(sel.row, sel.rowCount);
  2129. posSheet.deleteColumns(sel.row + 2, sel.rowCount);
  2130. for (let iRow = 0, iLen = qdSheet.getRowCount(); iRow < iLen; iRow++) {
  2131. qdSheet.getCell(iRow, 0, spreadNS.SheetArea.rowHeader).text('清单' + (iRow+1));
  2132. }
  2133. for (let iCol = 0, iLen = posSheet.getColumnCount() - 2; iCol < iLen; iCol++) {
  2134. posSheet.getCell(0, iCol + 2, spreadNS.SheetArea.colHeader).text('清单' + (iCol+1));
  2135. }
  2136. qdSheet.setSelection(sel.row, sel.col, 1, 1);
  2137. },
  2138. },
  2139. }
  2140. });
  2141. }
  2142. // 初始化左侧表格
  2143. initView () {
  2144. // 初始化 清单编号
  2145. const qdSheet = this.qdSpread.getActiveSheet();
  2146. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  2147. SpreadJsObj.refreshColumnAlign(qdSheet);
  2148. // 清理原有数据
  2149. SpreadJsObj.beginMassOperation(qdSheet);
  2150. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  2151. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  2152. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  2153. }
  2154. qdSheet.setSelection(0, 0, 1 ,1);
  2155. SpreadJsObj.endMassOperation(qdSheet);
  2156. // 初始化 部位数量复核表
  2157. const posSheet = this.posSpread.getActiveSheet();
  2158. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  2159. SpreadJsObj.refreshColumnAlign(posSheet);
  2160. // 清理原有数据
  2161. SpreadJsObj.beginMassOperation(posSheet);
  2162. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  2163. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  2164. posSheet.setSelection(0, 0, 1 ,1);
  2165. SpreadJsObj.endMassOperation(posSheet);
  2166. // 检查签约节点数据,以工具栏数据为准
  2167. if (dealBills) {
  2168. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  2169. }
  2170. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  2171. }
  2172. // 获取界面数据
  2173. getBatchData () {
  2174. const result = [];
  2175. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  2176. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  2177. if (qdSheet.getText(iRow, 0) === '') { continue; }
  2178. const qd = {
  2179. b_code: qdSheet.getText(iRow, 0),
  2180. name: qdSheet.getText(iRow, 1),
  2181. unit: qdSheet.getText(iRow, 2),
  2182. price: _.toNumber(qdSheet.getText(iRow, 3)),
  2183. pos: [],
  2184. };
  2185. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  2186. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  2187. if (value !== 0 && !isNaN(value)) {
  2188. qd.pos.push({
  2189. name: posSheet.getText(iPosRow, 0),
  2190. drawing_code: posSheet.getText(iPosRow, 1),
  2191. quantity: value, porder: qd.pos.length + 1,
  2192. });
  2193. }
  2194. }
  2195. if (!$('input[name=batch-filter]')[0].checked || qd.pos.length > 0) result.push(qd);
  2196. }
  2197. return result;
  2198. }
  2199. }
  2200. const dealBills = new DealBills('#deal-bills-spread', {
  2201. cols: [
  2202. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  2203. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2204. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  2205. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  2206. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  2207. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  2208. ],
  2209. emptyRows: 0,
  2210. headRows: 1,
  2211. headRowHeight: [32],
  2212. headColWidth: [30],
  2213. defaultRowHeight: 21,
  2214. headerFont: '12px 微软雅黑',
  2215. font: '12px 微软雅黑',
  2216. selectedBackColor: '#fffacd',
  2217. });
  2218. $.divResizer({
  2219. select: '#revise-right-spr',
  2220. callback: function () {
  2221. billsSpread.refresh();
  2222. if (posSpread) posSpread.refresh();
  2223. if (stdXmj) stdXmj.spread.refresh();
  2224. if (stdGcl) stdGcl.spread.refresh();
  2225. if (dealBills) dealBills.spread.refresh();
  2226. if (searchLedger) searchLedger.spread.refresh();
  2227. if (errorList) errorList.spread.refresh();
  2228. if (checkList) checkList.spread.refresh();
  2229. if (sumLoadMiss) sumLoadMiss.spread.refresh();
  2230. }
  2231. });
  2232. $.subMenu({
  2233. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  2234. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  2235. key: 'menu.1.0.0',
  2236. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  2237. callback: function (info) {
  2238. if (info.mini) {
  2239. $('.panel-title').addClass('fluid');
  2240. $('#sub-menu').removeClass('panel-sidebar');
  2241. } else {
  2242. $('.panel-title').removeClass('fluid');
  2243. $('#sub-menu').addClass('panel-sidebar');
  2244. }
  2245. autoFlashHeight();
  2246. billsSpread.refresh();
  2247. if (posSpread) posSpread.refresh();
  2248. if (stdXmj) stdXmj.spread.refresh();
  2249. if (stdGcl) stdGcl.spread.refresh();
  2250. if (dealBills) dealBills.spread.refresh();
  2251. if (searchLedger) searchLedger.spread.refresh();
  2252. if (errorList) errorList.spread.refresh();
  2253. if (checkList) checkList.spread.refresh();
  2254. if (sumLoadMiss) sumLoadMiss.spread.refresh();
  2255. }
  2256. });
  2257. // showSideTools(true);
  2258. billsSpread.refresh();
  2259. if (posSpread) posSpread.refresh();
  2260. const stdLibCellDoubleClick = function (e, info) {
  2261. const stdSheet = info.sheet;
  2262. const mainSheet = billsSheet;
  2263. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  2264. const stdTree = stdSheet.zh_tree;
  2265. const stdNode = stdTree.nodes[info.row];
  2266. const mainTree = mainSheet.zh_tree;
  2267. const sel = mainSheet.getSelections()[0];
  2268. const mainNode = mainTree.nodes[sel.row];
  2269. if (!stdNode) return;
  2270. if (info.sheet.zh_setting.stdType === 'gcl') {
  2271. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  2272. toastr.warning('非最底层项目下,不应添加节点');
  2273. return;
  2274. }
  2275. }
  2276. postData(window.location.pathname + '/update', {
  2277. postType: 'add-std',
  2278. postData: {
  2279. id: mainTree.getNodeKey(mainNode),
  2280. tender_id: mainNode.tender_id,
  2281. stdType: info.sheet.zh_setting.stdType,
  2282. stdLibId: stdNode.list_id,
  2283. stdNode: stdTree.getNodeKey(stdNode)
  2284. }
  2285. }, function (result) {
  2286. const refreshNode = mainTree.loadPostData(result);
  2287. billsTreeSpreadObj.refreshTree(mainSheet, refreshNode);
  2288. if (sel) {
  2289. if (refreshNode.create && refreshNode.create.length > 0) {
  2290. mainSheet.setSelection(refreshNode.create[refreshNode.create.length - 1].index, sel.col, sel.rowCount, sel.colCount);
  2291. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, refreshNode.create[refreshNode.create.length - 1].index]);
  2292. } else {
  2293. const node = _.find(mainTree.nodes, {code: stdNode.code, name: stdNode.name});
  2294. if (node) {
  2295. mainSheet.setSelection(mainTree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  2296. SpreadJsObj.reloadRowsBackColor(mainSheet, [sel.row, mainTree.nodes.indexOf(node)]);
  2297. }
  2298. }
  2299. }
  2300. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  2301. billsSpread.focus();
  2302. posSpreadObj.loadCurPosData();
  2303. });
  2304. };
  2305. const stdXmjSetting = {
  2306. selector: '#std-xmj',
  2307. stdType: 'xmj',
  2308. libs: stdChapters,
  2309. treeSetting: {
  2310. id: 'chapter_id',
  2311. pid: 'pid',
  2312. order: 'order',
  2313. level: 'level',
  2314. rootId: -1,
  2315. keys: ['id', 'list_id', 'chapter_id'],
  2316. },
  2317. spreadSetting: {
  2318. cols: [
  2319. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  2320. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2321. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  2322. ],
  2323. treeCol: 0,
  2324. emptyRows: 0,
  2325. headRows: 1,
  2326. headRowHeight: [32],
  2327. defaultRowHeight: 21,
  2328. headerFont: '12px 微软雅黑',
  2329. font: '12px 微软雅黑',
  2330. headColWidth: [30],
  2331. selectedBackColor: '#fffacd',
  2332. },
  2333. cellDoubleClick: stdLibCellDoubleClick,
  2334. page: 'revise',
  2335. tid: window.location.pathname.split('/')[2],
  2336. };
  2337. const stdGclSetting = {
  2338. selector: '#std-gcl',
  2339. stdType: 'gcl',
  2340. libs: stdBills,
  2341. treeSetting: {
  2342. id: 'bill_id',
  2343. pid: 'pid',
  2344. order: 'order',
  2345. level: 'level',
  2346. rootId: -1,
  2347. keys: ['id', 'list_id', 'bill_id']
  2348. },
  2349. spreadSetting: {
  2350. cols: [
  2351. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  2352. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2353. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  2354. ],
  2355. treeCol: 0,
  2356. emptyRows: 0,
  2357. headRows: 1,
  2358. headRowHeight: [32],
  2359. defaultRowHeight: 21,
  2360. headerFont: '12px 微软雅黑',
  2361. font: '12px 微软雅黑',
  2362. headColWidth: [30],
  2363. selectedBackColor: '#fffacd',
  2364. },
  2365. cellDoubleClick: stdLibCellDoubleClick,
  2366. page: 'revise',
  2367. tid: window.location.pathname.split('/')[2],
  2368. };
  2369. // 展开收起标准节点
  2370. $('a', '#side-menu').bind('click', function (e) {
  2371. e.preventDefault();
  2372. const tab = $(this), tabPanel = $(tab.attr('content'));
  2373. // 展开工具栏、切换标签
  2374. if (!tab.hasClass('active')) {
  2375. const close = $('.active', '#side-menu').length === 0;
  2376. $('a', '#side-menu').removeClass('active');
  2377. tab.addClass('active');
  2378. $('.tab-content .tab-pane').removeClass('active');
  2379. tabPanel.addClass('active');
  2380. showSideTools(tab.hasClass('active'));
  2381. if (tab.attr('content') === '#std-xmj') {
  2382. if (!stdXmj) {
  2383. stdXmj = $.stdLib(stdXmjSetting);
  2384. }
  2385. stdXmj.spread.refresh();
  2386. } else if (tab.attr('content') === '#std-gcl') {
  2387. if (!stdGcl) {
  2388. stdGcl = $.stdLib(stdGclSetting);
  2389. }
  2390. stdGcl.spread.refresh();
  2391. } else if (tab.attr('content') === '#deal-bills') {
  2392. dealBills.loadData();
  2393. dealBills.spread.refresh();
  2394. } else if (tab.attr('content') === '#search') {
  2395. if (!searchLedger) {
  2396. searchLedger = $.billsSearch({
  2397. selector: '#search',
  2398. searchSpread: billsSpread,
  2399. searchOver: true,
  2400. searchEmpty: true,
  2401. resultSpreadSetting: {
  2402. cols: [
  2403. {title: '项目节编号', field: 'code', hAlign: 0, width: 90, formatter: '@', readOnly: true},
  2404. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
  2405. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@', readOnly: true},
  2406. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  2407. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  2408. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  2409. ],
  2410. emptyRows: 0,
  2411. headRows: 1,
  2412. headRowHeight: [32],
  2413. headColWidth: [30],
  2414. defaultRowHeight: 21,
  2415. headerFont: '12px 微软雅黑',
  2416. font: '12px 微软雅黑',
  2417. selectedBackColor: '#fffacd',
  2418. },
  2419. afterLocated: function () {
  2420. posSpreadObj.loadCurPosData();
  2421. },
  2422. customSearch: [
  2423. {
  2424. key: 'revise', title: '新增部位', valid: true, parent: true,
  2425. check: function (node) {
  2426. if (node.ccid || node.cid) {
  2427. return true;
  2428. } else {
  2429. return false;
  2430. }
  2431. }
  2432. }
  2433. ],
  2434. });
  2435. }
  2436. searchLedger.spread.refresh();
  2437. } else if (tab.attr('content') === '#error-list') {
  2438. errorList.spread.refresh();
  2439. } else if (tab.attr('content') === '#check-list') {
  2440. checkList.spread.refresh();
  2441. } else if (tab.attr('content') === '#sum-load-miss') {
  2442. sumLoadMiss.spread.refresh();
  2443. }
  2444. }
  2445. else {// 收起工具栏
  2446. tab.removeClass('active');
  2447. tabPanel.removeClass('active');
  2448. showSideTools(tab.hasClass('active'));
  2449. }
  2450. billsSpread.refresh();
  2451. if (posSpread) posSpread.refresh();
  2452. });
  2453. // 显示层次
  2454. (function (select, sheet) {
  2455. $(select).click(function () {
  2456. if (!sheet.zh_tree) return;
  2457. const tag = $(this).attr('tag');
  2458. const tree = sheet.zh_tree;
  2459. setTimeout(() => {
  2460. showWaitingView();
  2461. switch (tag) {
  2462. case "1":
  2463. case "2":
  2464. case "3":
  2465. case "4":
  2466. case "5":
  2467. tree.expandByLevel(parseInt(tag));
  2468. SpreadJsObj.refreshTreeRowVisible(sheet);
  2469. break;
  2470. case "last":
  2471. tree.expandByCustom(() => { return true; });
  2472. SpreadJsObj.refreshTreeRowVisible(sheet);
  2473. break;
  2474. case "leafXmj":
  2475. tree.expandToLeafXmj();
  2476. SpreadJsObj.refreshTreeRowVisible(sheet);
  2477. break;
  2478. }
  2479. closeWaitingView();
  2480. }, 100);
  2481. });
  2482. })('a[name=showLevel]', billsSheet);
  2483. const dataChecker = DataChecker({
  2484. checkUrl: window.location.pathname + '/check',
  2485. completeData: function (data) {
  2486. pos.updateDatas({update: data.source.pos});
  2487. const loadResult = billsTree.loadPostData({update: data.source.bills});
  2488. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  2489. posSpreadObj.loadCurPosData();
  2490. for (const e of data.error) {
  2491. e.serialNo = billsTree.getNodeIndex(billsTree.getItems(e.ledger_id)) + 1;
  2492. }
  2493. },
  2494. errorList: errorList,
  2495. });
  2496. $('#ledger-check2').click(() => {
  2497. const result = ledgerCheck2({
  2498. ledgerTree: billsTree,
  2499. ledgerPos: pos,
  2500. checkList: checkList,
  2501. decimal: decimal,
  2502. checkOption: checkOption,
  2503. });
  2504. check2Viewing({
  2505. extra: ZhCalc.div(billsTree.datas.length + pos.datas.length, 10000, 0),
  2506. randomWait: true,
  2507. prefix: 'check2-',
  2508. checks: result,
  2509. checkList: checkList,
  2510. })
  2511. });
  2512. });