change_revise.js 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  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. tp: {
  38. enable: 1,
  39. fields: [
  40. {qty: 'sgfh_qty', tp: 'sgfh_tp'},
  41. {qty: 'sjcl_qty', tp: 'sjcl_tp'},
  42. {qty: 'qtcl_qty', tp: 'qtcl_tp'},
  43. {qty: 'quantity', tp: 'total_price'},
  44. {qty: 'deal_qty', tp: 'deal_tp'},
  45. ],
  46. },
  47. };
  48. $(document).ready(() => {
  49. let stdXmj, stdGcl, searchLedger;
  50. autoFlashHeight();
  51. if (!billsSpreadSetting.readOnly) {
  52. for (const col of billsSpreadSetting.cols) {
  53. if (!col.readOnly || col.readOnly === false)
  54. col.readOnly = 'readOnly.isChangeAdd';
  55. }
  56. }
  57. if (!posSpreadSetting.readOnly) {
  58. for (const col of posSpreadSetting.cols) {
  59. if (!col.readOnly || col.readOnly === false)
  60. col.readOnly = 'readOnly.isChangeAdd';
  61. }
  62. }
  63. const billsCol = {
  64. readOnly: {
  65. isChangeAdd: function (data) {
  66. return !readOnly && !data.ccid;
  67. }
  68. }
  69. };
  70. const posCol = {
  71. readOnly: {
  72. isChangeAdd: function (data) {
  73. return !readOnly && !data.ccid;
  74. }
  75. }
  76. };
  77. // 初始化spread
  78. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  79. const billsSheet = billsSpread.getActiveSheet();
  80. billsSpreadSetting.rowHeader = [
  81. {
  82. rowHeaderType: 'circle',
  83. setting: {
  84. size: 5,
  85. indent: 16,
  86. getColor: function (index, data) {
  87. if (!data) return;
  88. if(!data.ccid && !data.cid) return;
  89. return '#dc3545';
  90. }
  91. },
  92. },
  93. ];
  94. sjsSettingObj.setFxTreeStyle(billsSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  95. if (thousandth) sjsSettingObj.setTpThousandthFormat(billsSpreadSetting);
  96. SpreadJsObj.initSpreadSettingEvents(billsSpreadSetting, billsCol);
  97. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  98. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  99. const posSheet = posSpread.getActiveSheet();
  100. posSpreadSetting.rowHeader = [
  101. {
  102. rowHeaderType: 'circle',
  103. setting: {
  104. size: 5,
  105. indent: 16,
  106. getColor: function (index, data) {
  107. if (!data) return;
  108. if(!data.ccid) return;
  109. return '#dc3545';
  110. }
  111. },
  112. },
  113. ];
  114. sjsSettingObj.setGridSelectStyle(posSpreadSetting);
  115. if (thousandth) sjsSettingObj.setTpThousandthFormat(posSpreadSetting);
  116. SpreadJsObj.initSpreadSettingEvents(posSpreadSetting, posCol);
  117. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  118. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  119. const errorList = $.cs_errorList({
  120. tabSelector: '#error-list-tab',
  121. selector: '#error-list',
  122. relaSpread: billsSpread,
  123. storeKey: 'change-revise-error-' + window.location.pathname.split('/')[2],
  124. afterLocated: function () {
  125. posSpreadObj.loadCurPosData();
  126. },
  127. afterShow: function () {
  128. billsSpread.refresh();
  129. if (posSpread) posSpread.refresh();
  130. },
  131. });
  132. const checkList = $.ledger_checkList({
  133. id: 'check-list',
  134. tabSelector: '#check-list-tab',
  135. selector: '#check-list',
  136. relaSpread: billsSpread,
  137. storeKey: 'change-revise-check-' + window.location.pathname.split('/')[2],
  138. checkType: getCheckType(checkOption),
  139. afterLocated: function () {
  140. posSpreadObj.loadCurPosData();
  141. },
  142. afterShow: function () {
  143. billsSpread.refresh();
  144. if (posSpread) posSpread.refresh();
  145. },
  146. });
  147. const sumLoadMiss = $.sumLoadMiss({
  148. tabSelector: '#sum-load-miss-tab',
  149. selector: '#sum-load-miss',
  150. relaSpread: billsSpread,
  151. storeKey: 'change-revise-slm-' + window.location.pathname.split('/')[2],
  152. id: 'revise-slm',
  153. afterLocated: function () {
  154. posSpreadObj.loadCurPosData();
  155. },
  156. afterShow: function () {
  157. billsSpread.refresh();
  158. if (posSpread) posSpread.refresh();
  159. },
  160. });
  161. // 初始化 节点树结构
  162. const treeSetting = {
  163. id: 'ledger_id',
  164. pid: 'ledger_pid',
  165. order: 'order',
  166. level: 'level',
  167. rootId: -1,
  168. keys: ['id', 'tender_id', 'ledger_id'],
  169. calcFields: ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'],
  170. autoExpand: 3,
  171. markExpandKey: 'change-bills-expand',
  172. markExpandSubKey: window.location.pathname.split('/')[2],
  173. };
  174. if (!isTz) {
  175. treeSetting.calcFields.push('deal_tp');
  176. }
  177. treeSetting.calcFun = function (node) {
  178. if (node && node.b_code) {
  179. const posData = pos.getLedgerPos(node.id) || [];
  180. if (posData.length > 0) {
  181. let sgfh_qty = 0;
  182. let sjcl_qty = 0;
  183. let qtcl_qty = 0;
  184. for (const np of posData) {
  185. sgfh_qty = ZhCalc.add(sgfh_qty, np.sgfh_qty);
  186. sjcl_qty = ZhCalc.add(sjcl_qty, np.sjcl_qty);
  187. qtcl_qty = ZhCalc.add(qtcl_qty, np.qtcl_qty);
  188. }
  189. if (!_.isEqual(node.sgfh_qty, sgfh_qty) || !_.isEqual(node.sjcl_qty, sjcl_qty) || !_.isEqual(node.qtcl_qty, qtcl_qty)) {
  190. node.sgfh_qty = sgfh_qty;
  191. node.sjcl_qty = sjcl_qty;
  192. node.qtcl_qty = qtcl_qty;
  193. node.quantity = ZhCalc.sum([node.sgfh_qty, node.sjcl_qty, node.qtcl_qty]);
  194. node.sgfh_tp = ZhCalc.mul(sgfh_qty, node.unit_price, decimal.tp) || 0;
  195. node.sjcl_tp = ZhCalc.mul(sjcl_qty, node.unit_price, decimal.tp) || 0;
  196. node.qtcl_tp = ZhCalc.mul(qtcl_qty, node.unit_price, decimal.tp) || 0;
  197. node.total_price = ZhCalc.mul(node.quantity, node.unit_price, decimal.tp) || 0;
  198. }
  199. }
  200. }
  201. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  202. };
  203. const billsTree = createNewPathTree('revise', treeSetting);
  204. // 初始化 计量单元
  205. const pos = new PosData({ id: 'id', ledgerId: 'lid' });
  206. // 清单 相关方法&绑定spreadjs事件
  207. const billsTreeSpreadObj = {
  208. loadExprToInput(sheet) {
  209. const sel = sheet.getSelections()[0];
  210. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  211. if (col && col.type === 'Number') {
  212. const data = SpreadJsObj.getSelectObject(sheet);
  213. if (data) {
  214. const exprInfo = getExprInfo(col.field);
  215. if (exprInfo) {
  216. $('#bills-expr').val(data[exprInfo.expr] ? data[exprInfo.expr] : data[col.field])
  217. .attr('field', col.field).attr('org', data[col.field]);
  218. } else {
  219. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field]);
  220. }
  221. if (col.field.indexOf('dgn') >= 0) {
  222. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (_.isString(data.b_code) && data.b_code !== '') || !data.ccid);
  223. } else if (col.field === 'unit_price') {
  224. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || (_.isBoolean(data.used) && data.used === true) || !data.ccid);
  225. } else {
  226. const nodePos = pos.getLedgerPos(data.id);
  227. if (nodePos && nodePos.length > 0) {
  228. $('#bills-expr').val('').attr('readOnly', true);
  229. $('#bills-expr').removeAttr('data-row');
  230. } else {
  231. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || !data.ccid);
  232. }
  233. }
  234. $('#bills-expr').attr('data-row', sel.row);
  235. } else {
  236. $('#bills-expr').val('').attr('readOnly', true);
  237. $('#bills-expr').removeAttr('data-row');
  238. }
  239. } else {
  240. $('#bills-expr').val('').attr('readOnly', true);
  241. $('#bills-expr').removeAttr('data-row');
  242. }
  243. },
  244. getDefaultSelectInfo: function (sheet) {
  245. const tree = sheet.zh_tree;
  246. if (!tree) return;
  247. const sel = sheet.getSelections()[0];
  248. const node = sheet.zh_tree.nodes[sel.row];
  249. if (!node) return;
  250. let count = 1;
  251. if (sel.rowCount > 1) {
  252. for (let r = 1; r < sel.rowCount; r++) {
  253. const rNode = sheet.zh_tree.nodes[sel.row + r];
  254. if (rNode.level > node.level) continue;
  255. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  256. toastr.warning('请选择同一节点下的节点,进行该操作');
  257. return;
  258. }
  259. count += 1;
  260. }
  261. }
  262. return [tree, node, count];
  263. },
  264. /**
  265. * 刷新顶部按钮是否可用
  266. * @param sheet
  267. * @param selections
  268. */
  269. refreshOperationValid: function (sheet, selection) {
  270. const setObjEnable = function (obj, enable) {
  271. if (enable) {
  272. obj.removeClass('disabled');
  273. } else {
  274. obj.addClass('disabled');
  275. }
  276. };
  277. const invalidAll = function () {
  278. setObjEnable($('a[name=base-opr][type=add]'), false);
  279. setObjEnable($('a[name=base-opr][type=delete]'), false);
  280. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  281. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  282. setObjEnable($('a[name=base-opr][type=up-level]'), false);
  283. setObjEnable($('a[name=base-opr][type=down-level]'), false);
  284. };
  285. const sel = selection ? selection[0] : sheet.getSelections()[0];
  286. const row = sel ? sel.row : -1;
  287. const tree = sheet.zh_tree;
  288. if (!tree) {
  289. invalidAll();
  290. return;
  291. }
  292. const first = sheet.zh_tree.nodes[row];
  293. if (!first) {
  294. invalidAll();
  295. return;
  296. }
  297. let last = first, sameParent = true, nodeUsed = first.used;
  298. if (sel.rowCount > 1 && first) {
  299. for (let r = 1; r < sel.rowCount; r++) {
  300. const rNode = tree.nodes[sel.row + r];
  301. if (!rNode) {
  302. sameParent = false;
  303. break;
  304. }
  305. if (!rNode.ccid) {
  306. sameParent = false;
  307. break;
  308. }
  309. nodeUsed = nodeUsed || rNode.used;
  310. if (rNode.level > first.level) continue;
  311. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  312. sameParent = false;
  313. break;
  314. }
  315. last = rNode;
  316. }
  317. }
  318. const preNode = tree.getPreSiblingNode(first);
  319. const siblings = tree.getChildren(tree.getParent(first));
  320. let upPower = true;
  321. for (let order = last.order; order < siblings.length - 1; order ++) {
  322. if (!siblings[order].ccid) {
  323. upPower = false;
  324. break;
  325. }
  326. }
  327. const valid = !sheet.zh_setting.readOnly;
  328. setObjEnable($('a[name=base-opr][type=add]'), valid && first && first.level > 1);
  329. setObjEnable($('a[name=base-opr][type=delete]'), valid && first && sameParent && first.level > 1 && !nodeUsed && first.ccid);
  330. setObjEnable($('a[name=base-opr][type=up-move]'), valid && first && sameParent && first.level > 1 && preNode && first.ccid);
  331. setObjEnable($('a[name=base-opr][type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last) && first.ccid);
  332. const posRange = last ? pos.getLedgerPos(last.id) : [];
  333. setObjEnable($('a[name=base-opr][type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed
  334. && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)) && upPower && first.ccid);
  335. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  336. setObjEnable($('a[name=base-opr][type=down-level]'), valid && first && sameParent
  337. && first.level > 1 && preNode && (preNode.children.length > 0 || (preNode.children.length === 0 && preNode.ccid && (!preNodePosRange || preNodePosRange.length === 0))) && !preNode.used && first.ccid);
  338. setObjEnable($('#cut'), valid);
  339. setObjEnable($('#paste'), valid);
  340. },
  341. /**
  342. *
  343. * @param sheet
  344. * @param data
  345. */
  346. refreshTree: function (sheet, data) {
  347. SpreadJsObj.massOperationSheet(sheet, function () {
  348. const tree = sheet.zh_tree;
  349. // 处理删除
  350. if (data.delete) {
  351. data.delete.sort(function (a, b) {
  352. return b.deleteIndex - a.deleteIndex;
  353. });
  354. for (const d of data.delete) {
  355. sheet.deleteRows(d.deleteIndex, 1);
  356. }
  357. }
  358. // 处理新增
  359. if (data.create) {
  360. const newNodes = data.create;
  361. if (newNodes) {
  362. newNodes.sort(function (a, b) {
  363. return a.index - b.index;
  364. });
  365. for (const node of newNodes) {
  366. sheet.addRows(node.index, 1);
  367. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  368. }
  369. }
  370. }
  371. // 处理更新
  372. if (data.update) {
  373. const rows = [];
  374. for (const u of data.update) {
  375. rows.push(tree.nodes.indexOf(u));
  376. }
  377. SpreadJsObj.reLoadRowsData(sheet, rows);
  378. }
  379. // 处理展开
  380. if (data.expand) {
  381. const expanded = [];
  382. for (const e of data.expand) {
  383. if (expanded.indexOf(e) === -1) {
  384. const posterity = tree.getPosterity(e);
  385. for (const p of posterity) {
  386. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  387. expanded.push(p);
  388. }
  389. }
  390. }
  391. }
  392. });
  393. },
  394. selectionChanged: function (e, info) {
  395. if (info.newSelections) {
  396. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row || info.newSelections[0].rowCount !== info.oldSelections[0].rowCount) {
  397. billsTreeSpreadObj.refreshOperationValid(info.sheet);
  398. billsTreeSpreadObj.refreshPosData();
  399. }
  400. }
  401. billsTreeSpreadObj.loadExprToInput(info.sheet);
  402. },
  403. refreshPosData: function() {
  404. SpreadJsObj.resetTopAndSelect(posSheet);
  405. posSpreadObj.loadCurPosData();
  406. SpreadJsObj.saveTopAndSelect(billsSheet, ckBillsSpread);
  407. posSearch.search($('#pos-keyword').val());
  408. },
  409. /**
  410. * 新增节点
  411. * @param spread
  412. */
  413. baseOpr: function (sheet, type, addCount = 1) {
  414. const self = this;
  415. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  416. if (!tree || !node || !count) return;
  417. if (type === 'delete') {
  418. const parent = tree.getParent(node);
  419. const children = parent ? parent.children : tree.children;
  420. const index = children.indexOf(node);
  421. for (let i = 0; i < count; i++) {
  422. const child = children[i+index];
  423. if (tree.checkNodeUsed(child, pos)) {
  424. toastr.warning('选中的节点已计量,不可删除');
  425. return;
  426. }
  427. }
  428. } else if (type === 'up-level') {
  429. const parent = tree.getParent(node);
  430. const children = parent ? parent.children : tree.children;
  431. const index = children.indexOf(node);
  432. for (let i = index; i < children.length; i++) {
  433. const child = children[index];
  434. if (tree.checkNodeUsed(child, pos)) {
  435. if (i >= index + count) {
  436. toastr.warning('其后节点已计量,选中的节点不可升级');
  437. } else {
  438. toastr.warning('选中的节点已计量,不可升级');
  439. }
  440. return;
  441. }
  442. }
  443. } else if (type === 'down-level') {
  444. const parent = tree.getParent(node);
  445. const children = parent ? parent.children : tree.children;
  446. const index = children.indexOf(node);
  447. for (let i = 0; i < count; i++) {
  448. const child = children[i+index];
  449. if (tree.checkNodeUsed(child, pos)) {
  450. toastr.warning('选中的节点已计量,不可降级');
  451. return;
  452. }
  453. }
  454. }
  455. if (type === 'delete') {
  456. deleteAfterHint(function () {
  457. postData(window.location.pathname + '/update', {
  458. postType: type,
  459. postData: {
  460. id: node.ledger_id,
  461. count: type === 'add' ? addCount : count,
  462. }
  463. }, function (result) {
  464. const refreshData = tree.loadPostData(result);
  465. self.refreshTree(sheet, refreshData);
  466. const sel = sheet.getSelections()[0];
  467. if (sel) {
  468. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  469. billsTreeSpreadObj.refreshPosData();
  470. }
  471. self.refreshOperationValid(sheet);
  472. });
  473. });
  474. } else {
  475. postData(window.location.pathname + '/update', {
  476. postType: type,
  477. postData: {
  478. id: node.ledger_id,
  479. count: type === 'add' ? addCount : count,
  480. }
  481. }, function (result) {
  482. const refreshData = tree.loadPostData(result);
  483. self.refreshTree(sheet, refreshData);
  484. if (['up-move', 'down-move'].indexOf(type) > -1) {
  485. const sel = sheet.getSelections()[0];
  486. if (sel) {
  487. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  488. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(node)]);
  489. }
  490. } else if (type === 'add') {
  491. const sel = sheet.getSelections()[0];
  492. if (sel) {
  493. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  494. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(refreshData.create[0])]);
  495. billsTreeSpreadObj.refreshPosData();
  496. }
  497. }
  498. self.refreshOperationValid(sheet);
  499. });
  500. }
  501. },
  502. /**
  503. * 编辑单元格响应事件
  504. * @param {Object} e
  505. * @param {Object} info
  506. */
  507. editEnded: function (e, info) {
  508. if (info.sheet.zh_setting) {
  509. const col = info.sheet.zh_setting.cols[info.col];
  510. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  511. const node = sortData[info.row];
  512. const data = {
  513. id: node.id,
  514. tender_id: node.tender_id,
  515. ledger_id: node.ledger_id
  516. };
  517. // 未改变值则不提交
  518. const orgValue = node[col.field];
  519. const newValue = trimInvalidChar(info.editingText);
  520. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  521. return;
  522. }
  523. // 台账模式,检查计量单元相关
  524. if (isTz) {
  525. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  526. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  527. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  528. if (!node.children || node.children.length ===0) {
  529. const lPos = pos.getLedgerPos(node.id);
  530. if (lPos && lPos.length > 0) {
  531. toastr.error('清单含有计量单元,不可修改施工图复核数量');
  532. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  533. return;
  534. }
  535. }
  536. }
  537. if (col.field === 'b_code' && (newValue === '' || !newValue)) {
  538. const lPos = pos.getLedgerPos(node.id);
  539. if (lPos && lPos.length > 0) {
  540. toastr.error('清单含有计量单元,请先删除计量单元,再删除清单编号');
  541. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  542. return;
  543. }
  544. }
  545. }
  546. // 获取更新数据
  547. const exprInfo = getExprInfo(col.field);
  548. if (info.editingText) {
  549. const text = newValue;
  550. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '' && text === '') {
  551. toastr.error('节点已计量,请勿删除编号');
  552. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  553. return;
  554. }
  555. if (col.type === 'Number') {
  556. const num = _.toNumber(text);
  557. if (_.isFinite(num)) {
  558. data[col.field] = num;
  559. if (exprInfo) {
  560. data[exprInfo.expr] = '';
  561. }
  562. } else {
  563. try {
  564. data[col.field] = math.evaluate(transExpr(text));
  565. if (exprInfo) {
  566. data[exprInfo.expr] = newValue;
  567. }
  568. } catch(err) {
  569. toastr.error('输入的表达式非法');
  570. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  571. return;
  572. }
  573. }
  574. } else {
  575. data[col.field] = text;
  576. }
  577. } else {
  578. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '') {
  579. toastr.error('节点已计量,请勿删除编号');
  580. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  581. return;
  582. }
  583. data[col.field] = null;
  584. if (exprInfo) {
  585. data[exprInfo.expr] = '';
  586. }
  587. }
  588. // 更新至服务器
  589. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  590. const refreshNode = billsTree.loadPostData(result);
  591. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  592. });
  593. }
  594. },
  595. clipboardPasting: function (e, info) {
  596. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  597. info.cancel = true;
  598. if (!setting || !tree) return;
  599. // const range = info.cellRange;
  600. // for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  601. // const node = tree.nodes[iRow];
  602. // if (tree.checkNodeUsed(node, pos)) {
  603. // toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  604. // return;
  605. // }
  606. // }
  607. const pasteData = info.pasteData.html
  608. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  609. : (info.pasteData.text === ''
  610. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  611. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  612. const hint = {
  613. usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'},
  614. usedCode: {type: 'warning', msg: '节点已计量,编号不可修改为空值'},
  615. invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'},
  616. posCode: {type: 'warning', msg: '清单含有计量单元,不可粘贴清单编号为空'},
  617. posQty: {type: 'warning', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可粘贴'},
  618. parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'},
  619. gcl: {type: 'warning', msg: '工程量清单,不可粘贴项目节数量'},
  620. posXmj: {type: 'warning', msg: '清单含有计量单元,不可粘贴项目节编号'},
  621. sameParent: {type: 'warning', msg: '仅可粘贴同层节点'},
  622. };
  623. const datas = [], filterNodes = [];
  624. let pid, level, filterRow = 0;
  625. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  626. const curRow = info.cellRange.row + iRow;
  627. const node = tree.nodes[curRow];
  628. if (!node) continue;
  629. if (!pid) pid = node.ledger_pid;
  630. if (!level) level = node.level;
  631. if (node.ledger_pid !== pid) {
  632. toastMessageUniq(hint.sameParent);
  633. filterRow+=1;
  634. continue;
  635. }
  636. if (node.level < level) break;
  637. let bPaste = false;
  638. const data = info.sheet.zh_tree.getNodeKeyData(node);
  639. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  640. const curCol = info.cellRange.col + iCol;
  641. const colSetting = info.sheet.zh_setting.cols[curCol];
  642. const value = trimInvalidChar(pasteData[iRow-filterRow][iCol]);
  643. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  644. toastMessageUniq(hint.parent);
  645. continue;
  646. }
  647. if (!_.isEmpty(node.b_code) && invalidFields.gcl.indexOf(colSetting.field) >= 0) {
  648. toastMessageUniq(hint.gcl);
  649. continue;
  650. }
  651. const lPos = pos.getLedgerPos(node.id);
  652. if (lPos && lPos.length > 0) {
  653. if (value === '' && colSetting.field === 'b_code') {
  654. toastMessageUniq(hint.posCode);
  655. continue;
  656. }
  657. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  658. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  659. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  660. toastMessageUniq(hint.posQty);
  661. continue;
  662. }
  663. if (value !== '' && invalidFields.posXmj.indexOf(colSetting.field) >= 0) {
  664. toastMessageUniq(hint.posXmj);
  665. continue;
  666. }
  667. }
  668. if (tree.checkNodeUsed(node, pos) && colSetting.field === 'unit_price') {
  669. toastMessageUniq (hint.usedUp);
  670. continue;
  671. }
  672. if (colSetting.type === 'Number') {
  673. const num = _.toNumber(value);
  674. if (num) {
  675. data[colSetting.field] = num;
  676. } else {
  677. try {
  678. data[colSetting.field] = math.evaluate(transExpr(value));
  679. const exprInfo = getExprInfo(colSetting.field);
  680. if (exprInfo) {
  681. data[exprInfo.expr] = value;
  682. }
  683. bPaste = true;
  684. } catch (err) {
  685. toastMessageUniq(hint.invalidExpr);
  686. continue;
  687. }
  688. }
  689. } else {
  690. if (node.used && (colSetting.field === 'code' || colSetting.field ==='b_code')
  691. && data[colSetting.field] !== '' && value === '') {
  692. toastMessageUniq(hint.usedCode);
  693. continue;
  694. }
  695. data[colSetting.field] = value;
  696. }
  697. bPaste = true;
  698. }
  699. if (bPaste) {
  700. datas.push(data);
  701. } else {
  702. filterNodes.push(node);
  703. }
  704. }
  705. if (datas.length > 0) {
  706. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  707. const refreshNode = tree.loadPostData(result);
  708. if (refreshNode.update) {
  709. refreshNode.update = refreshNode.update.concat(filterNodes);
  710. }
  711. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  712. }, function () {
  713. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  714. });
  715. } else {
  716. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  717. }
  718. },
  719. clipboardPasted: function (e, info) {
  720. const hint = {
  721. usedUp: {type: 'error', msg: '节点已计量,不可修改单价'},
  722. usedCode: {type: 'error', msg: '节点已计量,编号不可修改为空值'},
  723. invalidExpr: {type: 'error', msg: '粘贴的表达式非法'},
  724. posCode: {type: 'error', msg: '清单含有计量单元,请先删除计量单元,再修改清单编号为空'},
  725. posQty: {type: 'error', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可修改'},
  726. };
  727. const tree = info.sheet.zh_tree;
  728. if (!tree) { return; }
  729. const sortData = info.sheet.zh_tree.nodes;
  730. const datas = [], filterNodes = [];
  731. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  732. let bPaste = false;
  733. const curRow = info.cellRange.row + iRow;
  734. const node = sortData[curRow];
  735. if (node) {
  736. const data = info.sheet.zh_tree.getNodeKeyData(node);
  737. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  738. const curCol = info.cellRange.col + iCol;
  739. const colSetting = info.sheet.zh_setting.cols[curCol];
  740. const value = trimInvalidChar(info.sheet.getText(curRow, curCol));
  741. const lPos = pos.getLedgerPos(node.id);
  742. if (lPos && lPos.length > 0) {
  743. if (value === '' && colSetting.field === 'b_code') {
  744. toastMessageUniq(hint.posCode);
  745. continue;
  746. }
  747. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  748. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  749. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  750. toastMessageUniq(hint.posQty);
  751. continue;
  752. }
  753. }
  754. if (node.used && col.field === 'unit_price') {
  755. toastMessageUniq (hint.usedUp);
  756. continue;
  757. }
  758. if (colSetting.type === 'Number') {
  759. const num = _.toNumber(value);
  760. if (num) {
  761. data[colSetting.field] = num;
  762. } else {
  763. try {
  764. data[colSetting.field] = math.evaluate(transExpr(value));
  765. } catch (err) {
  766. toastMessageUniq(hint.invalidExpr);
  767. continue;
  768. }
  769. }
  770. } else {
  771. if (node.used && (col.field === 'code' || col.field ==='b_code')
  772. && data[colSetting.field] !== '' && value === '') {
  773. toastMessageUniq(hint.usedCode);
  774. continue;
  775. }
  776. data[colSetting.field] = value;
  777. }
  778. bPaste = true;
  779. }
  780. if (bPaste) {
  781. datas.push(data);
  782. } else {
  783. filterNodes.push(node);
  784. }
  785. }
  786. }
  787. if (datas.length > 0) {
  788. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  789. const refreshNode = tree.loadPostData(result);
  790. if (refreshNode.update) {
  791. refreshNode.update = refreshNode.update.concat(filterNodes);
  792. }
  793. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  794. }, function () {
  795. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  796. });
  797. } else {
  798. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  799. }
  800. },
  801. deletePress: function (sheet) {
  802. if (!sheet.zh_setting) return;
  803. const sel = sheet.getSelections()[0], datas = [];
  804. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  805. let bDel = false;
  806. const node = sheet.zh_tree.nodes[iRow];
  807. const data = sheet.zh_tree.getNodeKeyData(node);
  808. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  809. const col = sheet.zh_setting.cols[iCol];
  810. const style = sheet.getStyle(iRow, iCol);
  811. if (style.locked || (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) >= 0 && node.b_code)) continue;
  812. if (['dgn_qty1', 'dgn_qty2'].indexOf(col.field) < 0 && sheet.zh_tree.checkNodeUsed(node, pos)) {
  813. toastr.warning('"' + (node.code || '') + (node.b_code || '') + ' ' + node.name +'"已计量,请勿修改');
  814. return;
  815. }
  816. data[col.field] = null;
  817. const exprInfo = getExprInfo(col.field);
  818. if (exprInfo) {
  819. data[exprInfo.expr] = '';
  820. }
  821. bDel = true;
  822. }
  823. if (bDel) datas.push(data);
  824. }
  825. if (datas.length > 0) {
  826. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  827. const refreshNode = sheet.zh_tree.loadPostData(result);
  828. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  829. billsTreeSpreadObj.loadExprToInput(sheet);
  830. });
  831. }
  832. },
  833. pasteBlock: function (spread, copyInfo) {
  834. const self = this;
  835. const sheet = spread.getActiveSheet();
  836. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  837. postData(window.location.pathname + '/update', {
  838. postType: 'paste-block',
  839. postData: {
  840. id: tree.getNodeKey(node),
  841. tid: copyInfo.tid,
  842. block: copyInfo.block,
  843. }
  844. }, function (data) {
  845. pos.updateDatas(data.pos);
  846. const result = tree.loadPostData(data.ledger);
  847. self.refreshTree(sheet, result);
  848. const sel = sheet.getSelections()[0];
  849. if (sel) {
  850. sheet.setSelection(tree.nodes.indexOf(result.create[0]), sel.col, sel.rowCount, sel.colCount);
  851. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, tree.nodes.indexOf(result.create[0])]);
  852. }
  853. self.refreshOperationValid(sheet);
  854. removeLocalCache(copyBlockTag);
  855. }, null, true);
  856. },
  857. topRowChanged: function (e, info) {
  858. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  859. },
  860. editStarting(e, info) {
  861. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  862. const col = info.sheet.zh_setting.cols[info.col];
  863. const node = info.sheet.zh_tree.nodes[info.row];
  864. if (!node) {
  865. info.cancel = true;
  866. return;
  867. }
  868. const exprInfo = getExprInfo(col.field);
  869. if (exprInfo) {
  870. if (node[exprInfo.expr] && node[exprInfo.expr] !== '') {
  871. info.sheet.getCell(info.row, info.col).text(node[exprInfo.expr]);
  872. }
  873. }
  874. switch (col.field) {
  875. case 'code':
  876. const posRange = pos.getLedgerPos(node.id);
  877. info.cancel = posRange && posRange.length > 0;
  878. break;
  879. case 'unit_price':
  880. info.cancel = (node.children && node.children.length > 0) || node.used;
  881. break;
  882. case 'sgfh_qty':
  883. case 'sgfh_tp':
  884. case 'sjcl_qty':
  885. case 'sjcl_tp':
  886. case 'qtcl_qty':
  887. case 'qtcl_tp':
  888. case 'deal_qty':
  889. case 'deal_tp':
  890. info.cancel = (node.children && node.children.length > 0);
  891. break;
  892. case 'dgn_qty1':
  893. case 'dgn_qty2':
  894. info.cancel = !_.isEmpty(node.b_code);
  895. break;
  896. }
  897. },
  898. cut: function (sheet, sel, callback) {
  899. if (!sheet || !sel) return;
  900. if (sel.colCount >= sheet.zh_setting.cols.length) {
  901. toastr.warning('请勿选中整行剪切');
  902. return;
  903. }
  904. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  905. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  906. const node = sortData[iRow];
  907. if (node) {
  908. const data = sheet.zh_tree.getNodeKeyData(node);
  909. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  910. const col = sheet.zh_setting.cols[iCol];
  911. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  912. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  913. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  914. const lPos = pos.getLedgerPos(node.id);
  915. if (lPos && lPos.length > 0) {
  916. toastr.error('不可剪切');
  917. return;
  918. }
  919. }
  920. const style = sheet.getStyle(iRow, iCol);
  921. if (style.locked) {
  922. toastr.error('不可剪切');
  923. return;
  924. }
  925. const colSetting = sheet.zh_setting.cols[iCol];
  926. data[colSetting.field] = null;
  927. const exprInfo = getExprInfo(colSetting.field);
  928. if (exprInfo) {
  929. data[exprInfo.expr] = '';
  930. }
  931. }
  932. datas.push(data);
  933. }
  934. }
  935. if (datas.length > 0) {
  936. callback();
  937. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  938. const refreshNode = sheet.zh_tree.loadPostData(result);
  939. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  940. });
  941. }
  942. },
  943. sortCode: function (sheet) {
  944. const tree = sheet.zh_tree;
  945. if (!tree) return;
  946. const select = SpreadJsObj.getSelectObject(sheet);
  947. if (!select || !select.code || select.code === '') return;
  948. const recursiveSortCode = function (data, parentCode, children) {
  949. if (!children || children.length === 0) return;
  950. for (const [i, child] of children.entries()) {
  951. if (!child.b_code || child.b_code === '') {
  952. const code = parentCode + '-' + (i + 1);
  953. const cData = tree.getNodeKeyData(child);
  954. cData.code = code;
  955. data.push(cData);
  956. if (!tree.isLeafXmj(child)) {
  957. recursiveSortCode(data, code, child.children);
  958. }
  959. }
  960. }
  961. };
  962. const data = [];
  963. recursiveSortCode(data, select.code, select.children);
  964. if (data.length > 0) {
  965. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  966. const refreshNode = tree.loadPostData(result);
  967. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  968. })
  969. }
  970. }
  971. };
  972. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  973. billsTreeSpreadObj.loadExprToInput(billsSheet);
  974. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  975. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  976. if (!readOnly) {
  977. // 增删上下移升降级
  978. $('a[name="base-opr"]').click(function () {
  979. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  980. });
  981. $('a[name=cpc]').click(function () {
  982. billsSpread.commandManager().execute({
  983. cmd: this.getAttribute('type'),
  984. sheetName: billsSheet.name()
  985. });
  986. });
  987. $('#bills-expr').bind('change onblur', function () {
  988. if (this.readOnly) return;
  989. const expr = $(this);
  990. const row = expr.attr('data-row') ? _.toInteger(expr.attr('data-row')) : -1;
  991. const select = billsTree.getItemsByIndex(row);
  992. if (!select) return;
  993. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  994. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  995. const data = {
  996. id: select.id,
  997. tender_id: select.tender_id,
  998. ledger_id: select.ledger_id
  999. };
  1000. const exprInfo = getExprInfo(field);
  1001. if (newValue !== '') {
  1002. const num = _.toNumber(newValue);
  1003. if (num) {
  1004. data[field] = num;
  1005. if (exprInfo) {
  1006. data[exprInfo.expr] = '';
  1007. }
  1008. } else {
  1009. try {
  1010. data[field] = math.evaluate(transExpr(newValue));
  1011. if (exprInfo) {
  1012. data[exprInfo.expr] = newValue;
  1013. }
  1014. } catch (err) {
  1015. toastr.error('输入的表达式非法');
  1016. return;
  1017. }
  1018. }
  1019. } else {
  1020. data[field] = null;
  1021. if (exprInfo) {
  1022. data[exprInfo.expr] = '';
  1023. }
  1024. }
  1025. // 更新至服务器
  1026. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  1027. const refreshNode = billsTree.loadPostData(result);
  1028. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  1029. });
  1030. });
  1031. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  1032. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  1033. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  1034. billsSpread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  1035. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  1036. SpreadJsObj.Clipboard.setCopyData(copyText);
  1037. });
  1038. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  1039. SpreadJsObj.addCutEvents(billsSpread, billsTreeSpreadObj.cut);
  1040. }
  1041. // 右键菜单
  1042. let batchInsertObj;
  1043. $.contextMenu.types.batchInsert = function (item, opt, root) {
  1044. const self = this;
  1045. if ($.isFunction(item.icon)) {
  1046. item._icon = item.icon.call(this, this, $t, key, item);
  1047. } else {
  1048. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  1049. // to enable font awesome
  1050. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  1051. } else {
  1052. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  1053. }
  1054. }
  1055. this.addClass(item._icon);
  1056. 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>')
  1057. .appendTo(this);
  1058. const $input = $obj.find('input');
  1059. const event = () => {
  1060. if (self.hasClass('context-menu-disabled')) return;
  1061. item.batchInsert($input[0], root);
  1062. };
  1063. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  1064. $input.click((e) => {e.stopPropagation();})
  1065. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  1066. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  1067. };
  1068. const billsContextMenuOptions = {
  1069. selector: '#bills-spread',
  1070. build: function ($trigger, e) {
  1071. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  1072. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1073. },
  1074. items: {}
  1075. };
  1076. if (!readOnly) {
  1077. billsContextMenuOptions.items.create = {
  1078. name: '新增',
  1079. icon: 'fa-sign-in',
  1080. callback: function (key, opt) {
  1081. billsTreeSpreadObj.baseOpr(billsSheet, 'add');
  1082. },
  1083. disabled: function (key, opt) {
  1084. const sheet = billsSheet;
  1085. const selection = sheet.getSelections();
  1086. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1087. const row = sel ? sel.row : -1;
  1088. const tree = sheet.zh_tree;
  1089. if (!tree) return true;
  1090. const first = sheet.zh_tree.nodes[row];
  1091. const valid = !sheet.zh_setting.readOnly;
  1092. return !(valid && first && first.level > 1);
  1093. }
  1094. };
  1095. billsContextMenuOptions.items.delete = {
  1096. name: '删除',
  1097. icon: 'fa-remove',
  1098. callback: function (key, opt) {
  1099. billsTreeSpreadObj.baseOpr(billsSheet, 'delete');
  1100. },
  1101. disabled: function (key, opt) {
  1102. const sheet = billsSheet;
  1103. const selection = sheet.getSelections();
  1104. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1105. const row = sel ? sel.row : -1;
  1106. const tree = sheet.zh_tree;
  1107. if (!tree) return true;
  1108. const first = sheet.zh_tree.nodes[row];
  1109. let last = first, sameParent = true, nodeUsed = first.used;
  1110. if (sel.rowCount > 1 && first) {
  1111. for (let r = 1; r < sel.rowCount; r++) {
  1112. const rNode = tree.nodes[sel.row + r];
  1113. if (!rNode) {
  1114. sameParent = false;
  1115. break;
  1116. }
  1117. if (!rNode.ccid) {
  1118. return true;
  1119. }
  1120. nodeUsed = nodeUsed || rNode.used;
  1121. if (rNode.level > first.level) continue;
  1122. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  1123. sameParent = false;
  1124. break;
  1125. }
  1126. last = rNode;
  1127. }
  1128. }
  1129. const valid = !sheet.zh_setting.readOnly;
  1130. return !(valid && first && sameParent && !(first.level === 1 && first.node_type) && !nodeUsed && first.ccid);
  1131. }
  1132. };
  1133. billsContextMenuOptions.items.sprBase = '----';
  1134. }
  1135. billsContextMenuOptions.items.copyBlock = {
  1136. name: '复制整块',
  1137. icon: 'fa-files-o',
  1138. callback: function (key, opt) {
  1139. const copyBlockList = [];
  1140. const sheet = billsSheet;
  1141. const sel = sheet.getSelections()[0];
  1142. let iRow = sel.row;
  1143. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1144. while (iRow < sel.row + sel.rowCount) {
  1145. const node = sheet.zh_tree.nodes[iRow];
  1146. if (node.ledger_pid !== pid) {
  1147. toastr.error('仅可同时选中同层节点');
  1148. return;
  1149. }
  1150. const posterity = sheet.zh_tree.getPosterity(node);
  1151. iRow += posterity.length + 1;
  1152. posterity.unshift(node);
  1153. copyBlockList.push(sheet.zh_tree.getDefaultData(posterity));
  1154. }
  1155. for (const cbl of copyBlockList) {
  1156. for (const b of cbl) {
  1157. const posRange = pos.getLedgerPos(b.id);
  1158. if (posRange && posRange.length > 0) b.pos = posRange;
  1159. }
  1160. }
  1161. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  1162. },
  1163. visible: function (key, opt) {
  1164. const sheet = billsSheet;
  1165. const selection = sheet.getSelections();
  1166. const row = selection[0].row;
  1167. const select = billsTree.nodes[row];
  1168. return select;
  1169. },
  1170. disabled: function (key, opt) {
  1171. const sheet = billsSheet;
  1172. const selection = sheet.getSelections();
  1173. const row = selection[0].row;
  1174. const select = billsTree.nodes[row];
  1175. return select && select.level <= 1;
  1176. }
  1177. };
  1178. billsContextMenuOptions.items.copyBlockXmj = {
  1179. name: '复制整块(只复制项目节)',
  1180. icon: 'fa-files-o',
  1181. callback: function (key, opt) {
  1182. const copyBlockList = [];
  1183. const sheet = billsSheet;
  1184. const sel = sheet.getSelections()[0];
  1185. let iRow = sel.row;
  1186. const pid = sheet.zh_tree.nodes[iRow].ledger_pid;
  1187. while (iRow < sel.row + sel.rowCount) {
  1188. const node = sheet.zh_tree.nodes[iRow];
  1189. if (node.ledger_pid !== pid) {
  1190. toastr.error('仅可同时选中同层节点');
  1191. return;
  1192. }
  1193. const posterity = sheet.zh_tree.getPosterity(node);
  1194. iRow += posterity.length + 1;
  1195. const copyPosterity = posterity.filter(x => { return !x.b_code; });
  1196. copyPosterity.unshift(node);
  1197. const copyData = sheet.zh_tree.getDefaultData(copyPosterity);
  1198. for (const p of copyData) {
  1199. const children = copyData.filter(y => {return y.ledger_pid === p.ledger_id}) || [];
  1200. p.is_leaf = children.length === 0;
  1201. }
  1202. copyBlockList.push(copyData);
  1203. }
  1204. setLocalCache(copyBlockTag, JSON.stringify({ block: copyBlockList }));
  1205. },
  1206. visible: function (key, opt) {
  1207. const sheet = billsSheet;
  1208. const selection = sheet.getSelections();
  1209. const row = selection[0].row;
  1210. const select = billsTree.nodes[row];
  1211. return select;
  1212. },
  1213. disabled: function (key, opt) {
  1214. const sheet = billsSheet;
  1215. const selection = sheet.getSelections();
  1216. const row = selection[0].row;
  1217. const select = billsTree.nodes[row];
  1218. return select && select.level <= 1;
  1219. }
  1220. };
  1221. if (!readOnly) {
  1222. billsContextMenuOptions.items.pasteBlock = {
  1223. name: '粘贴整块',
  1224. icon: 'fa-clipboard',
  1225. disabled: function (key, opt) {
  1226. //const block = treeOperationObj.block || [];
  1227. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1228. return !(copyInfo && copyInfo.block && copyInfo.block.length > 0);
  1229. },
  1230. callback: function (key, opt) {
  1231. //const block = treeOperationObj.block || [];
  1232. const copyInfo = JSON.parse(getLocalCache(copyBlockTag));
  1233. if (copyInfo.block.length > 0) {
  1234. billsTreeSpreadObj.pasteBlock(billsSpread, copyInfo);
  1235. } else {
  1236. document.execCommand('paste');
  1237. }
  1238. },
  1239. visible: function (key, opt) {
  1240. return !readOnly;
  1241. }
  1242. };
  1243. billsContextMenuOptions.items.sprBlock = '----';
  1244. }
  1245. if (!readOnly) {
  1246. billsContextMenuOptions.items.batchInsert = {
  1247. name: '批量插入',
  1248. type: 'batchInsert',
  1249. value: '2',
  1250. icon: 'fa-sign-in',
  1251. batchInsert: function (obj, root) {
  1252. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  1253. obj.value = obj.max;
  1254. toastr.warning('批量插入不可多于' + obj.max);
  1255. } else if (_.toNumber(obj.value) < _.toNumber(obj.min)) {
  1256. obj.value = obj.min;
  1257. toastr.warning('批量插入不可少于' + obj.min);
  1258. } else {
  1259. billsTreeSpreadObj.baseOpr(billsSheet, 'add', parseInt(obj.value));
  1260. root.$menu.trigger('contextmenu:hide');
  1261. }
  1262. },
  1263. disabled: function (key, opt) {
  1264. const sheet = billsSheet;
  1265. const selection = sheet.getSelections();
  1266. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1267. const row = sel ? sel.row : -1;
  1268. const tree = sheet.zh_tree;
  1269. if (!tree) return true;
  1270. const first = sheet.zh_tree.nodes[row];
  1271. const valid = !sheet.zh_setting.readOnly;
  1272. return !(valid && first && first.level > 1);
  1273. }
  1274. };
  1275. billsContextMenuOptions.items.batchInsertBillsPos = {
  1276. name: '批量插入清单-计量单元',
  1277. icon: 'fa-sign-in',
  1278. visible: function (key, opt) {
  1279. return false;
  1280. },
  1281. disabled: function (key, opt) {
  1282. if (!isTz) return true;
  1283. const select = SpreadJsObj.getSelectObject(billsSheet);
  1284. if (select) {
  1285. if (select.code && select.code !== '') {
  1286. return !billsTree.isLeafXmj(select);
  1287. } else {
  1288. const parent = billsTree.getParent(select);
  1289. return !(parent && billsTree.isLeafXmj(parent));
  1290. }
  1291. } else {
  1292. return false;
  1293. }
  1294. },
  1295. callback: function (key, opt) {
  1296. if (!batchInsertObj) {
  1297. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  1298. } else {
  1299. batchInsertObj.initView();
  1300. }
  1301. $('#batch').modal('show');
  1302. }
  1303. };
  1304. }
  1305. billsContextMenuOptions.items.sprTag = '----';
  1306. billsContextMenuOptions.items.showLast = {
  1307. name: '显示至最底层',
  1308. callback: function (key, opt, menu, e) {
  1309. const node = SpreadJsObj.getSelectObject(billsSheet);
  1310. setTimeout(() => {
  1311. showWaitingView();
  1312. billsTree.expandByCustom(x => {
  1313. return x.expanded || (x.id === node.id) || (x.full_path.indexOf(node.ledger_id + '-') >= 0);
  1314. });
  1315. SpreadJsObj.refreshTreeRowVisible(billsSheet);
  1316. closeWaitingView();
  1317. }, 100);
  1318. },
  1319. };
  1320. $.contextMenu(billsContextMenuOptions);
  1321. // 计量单元 相关方法&绑定spreadjs事件
  1322. const posSpreadObj = {
  1323. refreshOperationValid: function (sheet, selection) {
  1324. const setObjEnable = function (obj, enable) {
  1325. if (enable) {
  1326. obj.removeClass('disabled');
  1327. } else {
  1328. obj.addClass('disabled');
  1329. }
  1330. };
  1331. const invalidAll = function () {
  1332. setObjEnable($('a[name=pos-opr][type=up-move]'), false);
  1333. setObjEnable($('a[name=pos-opr][type=down-move]'), false);
  1334. };
  1335. const sel = selection ? selection[0] : sheet.getSelections()[0];
  1336. const row = sel ? sel.row : -1;
  1337. const first = sheet.zh_data[row];
  1338. if (!first) {
  1339. invalidAll();
  1340. return;
  1341. }
  1342. let last = first;
  1343. if (sel.rowCount > 1 && first) {
  1344. for (let r = 1; r < sel.rowCount; r++) {
  1345. const rNode = sheet.zh_data[sel.row + r];
  1346. if (!rNode) break;
  1347. last = rNode;
  1348. }
  1349. }
  1350. const preNode = sheet.zh_data[row - 1];
  1351. const valid = !sheet.zh_setting.readOnly;
  1352. setObjEnable($('a[name=pos-opr][type=up-move]'), valid && first && preNode && first.ccid);
  1353. setObjEnable($('a[name=pos-opr][type=down-move]'), valid && first && (sheet.zh_data.indexOf(last) < sheet.zh_data.length - 1) && first.ccid);
  1354. },
  1355. loadExprToInput: function () {
  1356. const sel = posSheet.getSelections()[0];
  1357. if (!sel) return;
  1358. const col = posSheet.zh_setting.cols[sel.col];
  1359. const cell = posSheet.getCell(sel.col, sel.col);
  1360. if (col && col.type === 'Number') {
  1361. const data = SpreadJsObj.getSelectObject(posSheet);
  1362. if (data) {
  1363. console.log(data);
  1364. const exprInfo = getExprInfo(col.field);
  1365. const value = exprInfo
  1366. ? (data[exprInfo.expr] ? data[exprInfo.expr] : data[col.field])
  1367. : data[col.field];
  1368. console.log(cell.locked(), !data.ccid);
  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. treeSetting: {
  2309. id: 'chapter_id',
  2310. pid: 'pid',
  2311. order: 'order',
  2312. level: 'level',
  2313. rootId: -1,
  2314. keys: ['id', 'list_id', 'chapter_id'],
  2315. },
  2316. spreadSetting: {
  2317. cols: [
  2318. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  2319. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2320. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  2321. ],
  2322. treeCol: 0,
  2323. emptyRows: 0,
  2324. headRows: 1,
  2325. headRowHeight: [32],
  2326. defaultRowHeight: 21,
  2327. headerFont: '12px 微软雅黑',
  2328. font: '12px 微软雅黑',
  2329. headColWidth: [30],
  2330. selectedBackColor: '#fffacd',
  2331. },
  2332. cellDoubleClick: stdLibCellDoubleClick,
  2333. page: 'revise',
  2334. tid: window.location.pathname.split('/')[2],
  2335. };
  2336. const stdGclSetting = {
  2337. selector: '#std-gcl',
  2338. stdType: 'gcl',
  2339. treeSetting: {
  2340. id: 'bill_id',
  2341. pid: 'pid',
  2342. order: 'order',
  2343. level: 'level',
  2344. rootId: -1,
  2345. keys: ['id', 'list_id', 'bill_id']
  2346. },
  2347. spreadSetting: {
  2348. cols: [
  2349. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  2350. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  2351. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  2352. ],
  2353. treeCol: 0,
  2354. emptyRows: 0,
  2355. headRows: 1,
  2356. headRowHeight: [32],
  2357. defaultRowHeight: 21,
  2358. headerFont: '12px 微软雅黑',
  2359. font: '12px 微软雅黑',
  2360. headColWidth: [30],
  2361. selectedBackColor: '#fffacd',
  2362. },
  2363. cellDoubleClick: stdLibCellDoubleClick,
  2364. page: 'revise',
  2365. tid: window.location.pathname.split('/')[2],
  2366. };
  2367. // 展开收起标准节点
  2368. $('a', '#side-menu').bind('click', function (e) {
  2369. e.preventDefault();
  2370. const tab = $(this), tabPanel = $(tab.attr('content'));
  2371. // 展开工具栏、切换标签
  2372. if (!tab.hasClass('active')) {
  2373. const close = $('.active', '#side-menu').length === 0;
  2374. $('a', '#side-menu').removeClass('active');
  2375. tab.addClass('active');
  2376. $('.tab-content .tab-pane').removeClass('active');
  2377. tabPanel.addClass('active');
  2378. showSideTools(tab.hasClass('active'));
  2379. if (tab.attr('content') === '#std-xmj') {
  2380. if (!stdXmj) {
  2381. stdXmj = new stdLib(stdXmjSetting);
  2382. }
  2383. stdXmj.spread.refresh();
  2384. } else if (tab.attr('content') === '#std-gcl') {
  2385. if (!stdGcl) {
  2386. stdGcl = new stdLib(stdGclSetting);
  2387. }
  2388. stdGcl.spread.refresh();
  2389. } else if (tab.attr('content') === '#deal-bills') {
  2390. dealBills.loadData();
  2391. dealBills.spread.refresh();
  2392. } else if (tab.attr('content') === '#search') {
  2393. if (!searchLedger) {
  2394. searchLedger = $.billsSearch({
  2395. selector: '#search',
  2396. searchSpread: billsSpread,
  2397. searchOver: true,
  2398. searchEmpty: true,
  2399. resultSpreadSetting: {
  2400. cols: [
  2401. {title: '项目节编号', field: 'code', hAlign: 0, width: 90, formatter: '@', readOnly: true},
  2402. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
  2403. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@', readOnly: true},
  2404. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  2405. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  2406. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  2407. ],
  2408. emptyRows: 0,
  2409. headRows: 1,
  2410. headRowHeight: [32],
  2411. headColWidth: [30],
  2412. defaultRowHeight: 21,
  2413. headerFont: '12px 微软雅黑',
  2414. font: '12px 微软雅黑',
  2415. selectedBackColor: '#fffacd',
  2416. },
  2417. afterLocated: function () {
  2418. posSpreadObj.loadCurPosData();
  2419. },
  2420. customSearch: [
  2421. {
  2422. key: 'revise', title: '新增部位', valid: true, parent: true,
  2423. check: function (node) {
  2424. if (node.ccid || node.cid) {
  2425. return true;
  2426. } else {
  2427. return false;
  2428. }
  2429. }
  2430. }
  2431. ],
  2432. });
  2433. }
  2434. searchLedger.spread.refresh();
  2435. } else if (tab.attr('content') === '#error-list') {
  2436. errorList.spread.refresh();
  2437. } else if (tab.attr('content') === '#check-list') {
  2438. checkList.spread.refresh();
  2439. } else if (tab.attr('content') === '#sum-load-miss') {
  2440. sumLoadMiss.spread.refresh();
  2441. }
  2442. }
  2443. else {// 收起工具栏
  2444. tab.removeClass('active');
  2445. tabPanel.removeClass('active');
  2446. showSideTools(tab.hasClass('active'));
  2447. }
  2448. billsSpread.refresh();
  2449. if (posSpread) posSpread.refresh();
  2450. });
  2451. // 显示层次
  2452. (function (select, sheet) {
  2453. $(select).click(function () {
  2454. if (!sheet.zh_tree) return;
  2455. const tag = $(this).attr('tag');
  2456. const tree = sheet.zh_tree;
  2457. setTimeout(() => {
  2458. showWaitingView();
  2459. switch (tag) {
  2460. case "1":
  2461. case "2":
  2462. case "3":
  2463. case "4":
  2464. case "5":
  2465. tree.expandByLevel(parseInt(tag));
  2466. SpreadJsObj.refreshTreeRowVisible(sheet);
  2467. break;
  2468. case "last":
  2469. tree.expandByCustom(() => { return true; });
  2470. SpreadJsObj.refreshTreeRowVisible(sheet);
  2471. break;
  2472. case "leafXmj":
  2473. tree.expandToLeafXmj();
  2474. SpreadJsObj.refreshTreeRowVisible(sheet);
  2475. break;
  2476. }
  2477. closeWaitingView();
  2478. }, 100);
  2479. });
  2480. })('a[name=showLevel]', billsSheet);
  2481. const dataChecker = DataChecker({
  2482. checkUrl: window.location.pathname + '/check',
  2483. completeData: function (data) {
  2484. pos.updateDatas({update: data.source.pos});
  2485. const loadResult = billsTree.loadPostData({update: data.source.bills});
  2486. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  2487. posSpreadObj.loadCurPosData();
  2488. for (const e of data.error) {
  2489. e.serialNo = billsTree.getNodeIndex(billsTree.getItems(e.ledger_id)) + 1;
  2490. }
  2491. },
  2492. errorList: errorList,
  2493. });
  2494. $('#ledger-check2').click(() => {
  2495. const result = ledgerCheck2({
  2496. ledgerTree: billsTree,
  2497. ledgerPos: pos,
  2498. checkList: checkList,
  2499. decimal: decimal,
  2500. checkOption: checkOption,
  2501. });
  2502. check2Viewing({
  2503. extra: ZhCalc.div(billsTree.datas.length + pos.datas.length, 10000, 0),
  2504. randomWait: true,
  2505. prefix: 'check2-',
  2506. checks: result,
  2507. checkList: checkList,
  2508. })
  2509. });
  2510. });