revise.js 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. 'use strict';
  2. /**
  3. * 台账修订页面js
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const ckBillsSpread = window.location.pathname + '-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 transExpr(expr) {
  18. return $.trim(expr).replace('\t', '').replace('=', '').replace('%', '/100');
  19. }
  20. $(document).ready(() => {
  21. let stdXmj, stdGcl, searchLedger;
  22. autoFlashHeight();
  23. // 初始化spread
  24. const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
  25. const billsSheet = billsSpread.getActiveSheet();
  26. SpreadJsObj.initSheet(billsSheet, billsSpreadSetting);
  27. const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);
  28. const posSheet = posSpread.getActiveSheet();
  29. SpreadJsObj.initSheet(posSheet, posSpreadSetting);
  30. const posSearch = $.posSearch({selector: '#pos-search', searchSpread: posSpread});
  31. // 初始化 节点树结构
  32. const treeSetting = {
  33. id: 'ledger_id',
  34. pid: 'ledger_pid',
  35. order: 'order',
  36. level: 'level',
  37. rootId: -1,
  38. keys: ['id', 'tender_id', 'ledger_id'],
  39. calcFields: ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price'],
  40. };
  41. if (!isTz) {
  42. treeSetting.calcFields.push('deal_tp');
  43. }
  44. treeSetting.calcFun = function (node) {
  45. node.dgn_price = ZhCalc.round(ZhCalc.div(node.total_price, node.dgn_qty1), 2);
  46. };
  47. const billsTree = createNewPathTree('revise', treeSetting);
  48. billsTree.loadDatas(billsData);
  49. treeCalc.calculateAll(billsTree);
  50. // 加载至spread
  51. SpreadJsObj.loadSheetData(billsSheet, SpreadJsObj.DataType.Tree, billsTree);
  52. SpreadJsObj.loadTopAndSelect(billsSheet, ckBillsSpread);
  53. // 初始化 计量单元
  54. const pos = new PosData({ id: 'id', ledgerId: 'lid' });
  55. pos.loadDatas(posData);
  56. const billsTreeSpreadObj = {
  57. loadExprToInput(sheet) {
  58. const sel = sheet.getSelections()[0];
  59. const col = sheet.zh_setting.cols[sel.col], cell = sheet.getCell(sel.row, sel.col);
  60. if (col && col.type === 'Number') {
  61. const data = SpreadJsObj.getSelectObject(sheet);
  62. if (data) {
  63. $('#bills-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field]);
  64. if (col.field.indexOf('dgn') >= 0) {
  65. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (_.isString(data.b_code) && data.b_code !== ''));
  66. } else if (col.field === 'unit_price') {
  67. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0) || (_.isBoolean(data.used) && data.used === true));
  68. } else {
  69. $('#bills-expr').attr('readOnly', readOnly || cell.locked() || (data.children && data.children.length > 0));
  70. }
  71. } else {
  72. $('#bills-expr').val('').attr('readOnly', true);
  73. }
  74. } else {
  75. $('#bills-expr').val('').attr('readOnly', true);
  76. }
  77. },
  78. getDefaultSelectInfo: function (sheet) {
  79. const tree = sheet.zh_tree;
  80. if (!tree) return;
  81. const sel = sheet.getSelections()[0];
  82. const node = sheet.zh_tree.nodes[sel.row];
  83. if (!node) return;
  84. let count = 1;
  85. if (sel.rowCount > 1) {
  86. for (let r = 1; r < sel.rowCount; r++) {
  87. const rNode = sheet.zh_tree.nodes[sel.row + r];
  88. if (rNode.level > node.level) continue;
  89. if ((rNode.level < node.level) || (rNode.level === node.level && rNode.pid !== node.pid)) {
  90. toastr.warning('请选择同一节点下的节点,进行该操作');
  91. return;
  92. }
  93. count += 1;
  94. }
  95. }
  96. return [tree, node, count];
  97. },
  98. /**
  99. * 刷新顶部按钮是否可用
  100. * @param sheet
  101. * @param selections
  102. */
  103. refreshOperationValid: function (sheet, selection) {
  104. const setObjEnable = function (obj, enable) {
  105. if (enable) {
  106. obj.removeClass('disabled');
  107. } else {
  108. obj.addClass('disabled');
  109. }
  110. };
  111. const invalidAll = function () {
  112. setObjEnable($('a[type=add]'), false);
  113. setObjEnable($('a[type=delete]'), false);
  114. setObjEnable($('a[type=up-move]'), false);
  115. setObjEnable($('a[type=down-move]'), false);
  116. setObjEnable($('a[type=up-level]'), false);
  117. setObjEnable($('a[type=down-level]'), false);
  118. };
  119. const sel = selection ? selection[0] : sheet.getSelections()[0];
  120. const row = sel ? sel.row : -1;
  121. const tree = sheet.zh_tree;
  122. if (!tree) {
  123. invalidAll();
  124. return;
  125. }
  126. const first = sheet.zh_tree.nodes[row];
  127. if (!first) {
  128. invalidAll();
  129. return;
  130. }
  131. let last = first, sameParent = true, nodeUsed = first.used;
  132. if (sel.rowCount > 1 && first) {
  133. for (let r = 1; r < sel.rowCount; r++) {
  134. const rNode = tree.nodes[sel.row + r];
  135. if (!rNode) {
  136. sameParent = false;
  137. break;
  138. }
  139. nodeUsed = nodeUsed || rNode.used;
  140. if (rNode.level > first.level) continue;
  141. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  142. sameParent = false;
  143. break;
  144. }
  145. last = rNode;
  146. }
  147. }
  148. const preNode = tree.getPreSiblingNode(first);
  149. const valid = !sheet.zh_setting.readOnly;
  150. setObjEnable($('a[type=add]'), valid && first && first.level > 1);
  151. setObjEnable($('a[type=delete]'), valid && first && sameParent && first.level > 1 && !nodeUsed);
  152. setObjEnable($('a[type=up-move]'), valid && first && sameParent && first.level > 1 && preNode);
  153. setObjEnable($('a[type=down-move]'), valid && first && sameParent && first.level > 1 && !tree.isLastSibling(last));
  154. if (isTz) {
  155. const posRange = last ? pos.getLedgerPos(last.id) : [];
  156. setObjEnable($('a[type=up-level]'), valid && first && sameParent && tree.getParent(first) && !nodeUsed
  157. && first.level > 2 && ((!posRange || posRange.length === 0) || tree.isLastSibling(last)));
  158. const preNodePosRange = preNode ? pos.getLedgerPos(preNode.id) : [];
  159. setObjEnable($('a[type=down-level]'), valid && first && sameParent && !nodeUsed
  160. && first.level > 1 && preNode && (!preNodePosRange || preNodePosRange.length === 0));
  161. } else {
  162. setObjEnable($('#up-level'), valid && first && sameParent && first.level > 2 && tree.getParent(first));
  163. setObjEnable($('#down-level'), valid && first && sameParent && first.level > 1 && preNode);
  164. }
  165. setObjEnable($('#cut'), valid);
  166. setObjEnable($('#paste'), valid);
  167. },
  168. /**
  169. *
  170. * @param sheet
  171. * @param data
  172. */
  173. refreshTree: function (sheet, data) {
  174. SpreadJsObj.massOperationSheet(sheet, function () {
  175. const tree = sheet.zh_tree;
  176. // 处理删除
  177. if (data.delete) {
  178. data.delete.sort(function (a, b) {
  179. return b.deleteIndex - a.deleteIndex;
  180. });
  181. for (const d of data.delete) {
  182. sheet.deleteRows(d.deleteIndex, 1);
  183. }
  184. }
  185. // 处理新增
  186. if (data.create) {
  187. const newNodes = data.create;
  188. if (newNodes) {
  189. newNodes.sort(function (a, b) {
  190. return a.index - b.index;
  191. });
  192. for (const node of newNodes) {
  193. sheet.addRows(node.index, 1);
  194. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  195. }
  196. }
  197. }
  198. // 处理更新
  199. if (data.update) {
  200. const rows = [];
  201. for (const u of data.update) {
  202. rows.push(tree.nodes.indexOf(u));
  203. }
  204. SpreadJsObj.reLoadRowsData(sheet, rows);
  205. }
  206. // 处理展开
  207. if (data.expand) {
  208. const expanded = [];
  209. for (const e of data.expand) {
  210. if (expanded.indexOf(e) === -1) {
  211. const posterity = tree.getPosterity(e);
  212. for (const p of posterity) {
  213. sheet.setRowVisible(tree.nodes.indexOf(p), p.visible);
  214. expanded.push(p);
  215. }
  216. }
  217. }
  218. }
  219. });
  220. },
  221. selectionChanged: function (e, info) {
  222. if (info.newSelections[0].row !== info.oldSelections[0].row) {
  223. billsTreeSpreadObj.refreshOperationValid(info.sheet);
  224. posSpreadObj.loadCurPosData();
  225. SpreadJsObj.saveTopAndSelect(billsSheet, ckBillsSpread);
  226. posSearch.search($('#pos-keyword').val());
  227. }
  228. billsTreeSpreadObj.loadExprToInput(info.sheet);
  229. },
  230. /**
  231. * 新增节点
  232. * @param spread
  233. */
  234. baseOpr: function (sheet, type, addCount = 1) {
  235. const self = this;
  236. const [tree, node, count] = this.getDefaultSelectInfo(sheet);
  237. if (!tree || !node || !count) return;
  238. if (type === 'delete') {
  239. const parent = tree.getParent(node);
  240. const children = parent ? parent.children : tree.children;
  241. const index = children.indexOf(node);
  242. for (let i = 0; i < count; i++) {
  243. const child = children[i+index];
  244. if (tree.checkNodeUsed(child, pos)) {
  245. toastr.warning('选中的节点已计量,不可删除');
  246. return;
  247. }
  248. }
  249. } else if (type === 'up-level') {
  250. const parent = tree.getParent(node);
  251. const children = parent ? parent.children : tree.children;
  252. const index = children.indexOf(node);
  253. for (let i = index; i < children.length; i++) {
  254. const child = children[index];
  255. if (tree.checkNodeUsed(child, pos)) {
  256. if (i >= index + count) {
  257. toastr.warning('其后节点已计量,选中的节点不可升级');
  258. } else {
  259. toastr.warning('选中的节点已计量,不可升级');
  260. }
  261. return;
  262. }
  263. }
  264. } else if (type === 'down-level') {
  265. const parent = tree.getParent(node);
  266. const children = parent ? parent.children : tree.children;
  267. const index = children.indexOf(node);
  268. for (let i = 0; i < count; i++) {
  269. const child = children[i+index];
  270. if (tree.checkNodeUsed(child, pos)) {
  271. toastr.warning('选中的节点已计量,不可降级');
  272. return;
  273. }
  274. }
  275. }
  276. postData(window.location.pathname + '/update', {
  277. postType: type,
  278. postData: {
  279. id: node.ledger_id,
  280. count: type === 'add' ? addCount : count,
  281. }
  282. }, function (result) {
  283. const refreshData = tree.loadPostData(result);
  284. self.refreshTree(sheet, refreshData);
  285. if (type === 'delete') {
  286. const sel = sheet.getSelections()[0];
  287. if (sel) {
  288. sheet.setSelection(sel.row, sel.col, 1, sel.colCount);
  289. }
  290. } else if (['up-move', 'down-move'].indexOf(type) > -1) {
  291. const sel = sheet.getSelections()[0];
  292. if (sel) {
  293. sheet.setSelection(tree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  294. }
  295. } else if (type === 'add') {
  296. const sel = sheet.getSelections()[0];
  297. if (sel) {
  298. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  299. }
  300. }
  301. self.refreshOperationValid(sheet);
  302. });
  303. },
  304. /**
  305. * 编辑单元格响应事件
  306. * @param {Object} e
  307. * @param {Object} info
  308. */
  309. editEnded: function (e, info) {
  310. if (info.sheet.zh_setting) {
  311. const col = info.sheet.zh_setting.cols[info.col];
  312. const sortData = info.sheet.zh_dataType === 'tree' ? info.sheet.zh_tree.nodes : info.sheet.zh_data;
  313. const node = sortData[info.row];
  314. const data = {
  315. id: node.id,
  316. tender_id: node.tender_id,
  317. ledger_id: node.ledger_id
  318. };
  319. // 未改变值则不提交
  320. const orgValue = node[col.field];
  321. const newValue = trimInvalidChar(info.editingText);
  322. if (orgValue == info.editingText || ((!orgValue || orgValue === '') && (newValue === ''))) {
  323. return;
  324. }
  325. // 台账模式,检查计量单元相关
  326. if (isTz) {
  327. if (col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  328. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  329. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  330. if (!node.children || node.children.length ===0) {
  331. const lPos = pos.getLedgerPos(node.id);
  332. if (lPos && lPos.length > 0) {
  333. toastr.error('清单含有计量单元,不可修改施工图复核数量');
  334. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  335. return;
  336. }
  337. }
  338. }
  339. if (col.field === 'b_code' && (newValue === '' || !newValue)) {
  340. const lPos = pos.getLedgerPos(node.id);
  341. if (lPos && lPos.length > 0) {
  342. toastr.error('清单含有计量单元,请先删除计量单元,再删除清单编号');
  343. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  344. return;
  345. }
  346. }
  347. }
  348. // 获取更新数据
  349. if (info.editingText) {
  350. const text = newValue;
  351. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '' && text === '') {
  352. toastr.error('节点已计量,请勿删除编号');
  353. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  354. return;
  355. }
  356. if (col.type === 'Number') {
  357. const num = _.toNumber(text);
  358. if (_.isFinite(num)) {
  359. data[col.field] = num;
  360. } else {
  361. try {
  362. data[col.field] = math.evaluate(transExpr(text));
  363. } catch(err) {
  364. toastr.error('输入的表达式非法');
  365. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  366. return;
  367. }
  368. }
  369. } else {
  370. data[col.field] = text;
  371. }
  372. } else {
  373. if (node.used && (col.field === 'code' || col.field ==='b_code') && orgValue !== '') {
  374. toastr.error('节点已计量,请勿删除编号');
  375. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  376. return;
  377. }
  378. data[col.field] = null;
  379. }
  380. // 更新至服务器
  381. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  382. const refreshNode = billsTree.loadPostData(result);
  383. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  384. });
  385. }
  386. },
  387. clipboardPasting: function (e, info) {
  388. const tree = info.sheet.zh_tree, setting = info.sheet.zh_setting;
  389. info.cancel = true;
  390. if (!setting || !tree) return;
  391. // const range = info.cellRange;
  392. // for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  393. // const node = tree.nodes[iRow];
  394. // if (tree.checkNodeUsed(node, pos)) {
  395. // toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  396. // return;
  397. // }
  398. // }
  399. const pasteData = info.pasteData.html
  400. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  401. : (info.pasteData.text === ''
  402. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  403. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  404. const hint = {
  405. usedUp: {type: 'warning', msg: '节点已计量,不可修改单价'},
  406. usedCode: {type: 'warning', msg: '节点已计量,编号不可修改为空值'},
  407. invalidExpr: {type: 'warning', msg: '粘贴的表达式非法'},
  408. posCode: {type: 'warning', msg: '清单含有计量单元,不可粘贴清单编号为空'},
  409. posQty: {type: 'warning', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可粘贴'},
  410. parent: {type: 'warning', msg: '含有子项的清单,不可粘贴数量、单价、金额'},
  411. gcl: {type: 'warning', msg: '工程量清单,不可粘贴项目节数量'},
  412. posXmj: {type: 'warning', msg: '清单含有计量单元,不可粘贴项目节编号'},
  413. };
  414. const datas = [], filterNodes = [];
  415. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  416. const curRow = info.cellRange.row + iRow;
  417. const node = tree.nodes[curRow];
  418. if (!node) continue;
  419. let bPaste = false;
  420. const data = info.sheet.zh_tree.getNodeKeyData(node);
  421. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  422. const curCol = info.cellRange.col + iCol;
  423. const colSetting = info.sheet.zh_setting.cols[curCol];
  424. if (node.children && node.children.length > 0 && invalidFields.parent.indexOf(colSetting.field) >= 0) {
  425. toastMessageUniq(hint.parent);
  426. continue;
  427. }
  428. if (!_.isEmpty(node.b_code) && invalidFields.gcl.indexOf(colSetting.field) >= 0) {
  429. toastMessageUniq(hint.gcl);
  430. continue;
  431. }
  432. const lPos = pos.getLedgerPos(node.id);
  433. if (lPos && lPos.length > 0) {
  434. if (value === '' && colSetting.field === 'b_code') {
  435. toastMessageUniq(hint.posCode);
  436. continue;
  437. }
  438. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  439. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  440. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  441. toastMessageUniq(hint.posQty);
  442. continue;
  443. }
  444. if (value !== '' && invalidFields.posXmj.indexOf(colSetting.field) >= 0) {
  445. toastMessageUniq(hint.posXmj);
  446. continue;
  447. }
  448. }
  449. const value = trimInvalidChar(pasteData[iRow][iCol]);
  450. if (tree.checkNodeUsed(node, pos) && col.field === 'unit_price') {
  451. toastMessageUniq (hint.usedUp);
  452. continue;
  453. }
  454. if (colSetting.type === 'Number') {
  455. const num = _.toNumber(value);
  456. if (num) {
  457. data[colSetting.field] = num;
  458. } else {
  459. try {
  460. data[colSetting.field] = math.evaluate(transExpr(value));
  461. } catch (err) {
  462. toastMessageUniq(hint.invalidExpr);
  463. continue;
  464. }
  465. }
  466. } else {
  467. if (node.used && (col.field === 'code' || col.field ==='b_code')
  468. && data[colSetting.field] !== '' && value === '') {
  469. toastMessageUniq(hint.usedCode);
  470. continue;
  471. }
  472. data[colSetting.field] = value;
  473. }
  474. bPaste = true;
  475. }
  476. if (bPaste) {
  477. datas.push(data);
  478. } else {
  479. filterNodes.push(node);
  480. }
  481. }
  482. if (datas.length > 0) {
  483. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  484. const refreshNode = tree.loadPostData(result);
  485. if (refreshNode.update) {
  486. refreshNode.update = refreshNode.update.concat(filterNodes);
  487. }
  488. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  489. }, function () {
  490. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  491. });
  492. } else {
  493. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  494. }
  495. },
  496. clipboardPasted: function (e, info) {
  497. const hint = {
  498. usedUp: {type: 'error', msg: '节点已计量,不可修改单价'},
  499. usedCode: {type: 'error', msg: '节点已计量,编号不可修改为空值'},
  500. invalidExpr: {type: 'error', msg: '粘贴的表达式非法'},
  501. posCode: {type: 'error', msg: '清单含有计量单元,请先删除计量单元,再修改清单编号为空'},
  502. posQty: {type: 'error', msg: '清单含有计量单元,数量金额根据计量单元汇总计算所得,不可修改'},
  503. };
  504. const tree = info.sheet.zh_tree;
  505. if (!tree) { return; }
  506. const sortData = info.sheet.zh_tree.nodes;
  507. const datas = [], filterNodes = [];
  508. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  509. let bPaste = false;
  510. const curRow = info.cellRange.row + iRow;
  511. const node = sortData[curRow];
  512. if (node) {
  513. const data = info.sheet.zh_tree.getNodeKeyData(node);
  514. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  515. const curCol = info.cellRange.col + iCol;
  516. const colSetting = info.sheet.zh_setting.cols[curCol];
  517. const value = trimInvalidChar(info.sheet.getText(curRow, curCol));
  518. const lPos = pos.getLedgerPos(node.id);
  519. if (lPos && lPos.length > 0) {
  520. if (value === '' && colSetting.field === 'b_code') {
  521. toastMessageUniq(hint.posCode);
  522. continue;
  523. }
  524. if (colSetting.field === 'sgfh_qty' || colSetting.field === 'sgfh_tp' ||
  525. colSetting.field === 'sjcl_qty' || colSetting.field === 'sjcl_tp' ||
  526. colSetting.field === 'qtcl_qty' || colSetting.field === 'qtcl_tp') {
  527. toastMessageUniq(hint.posQty);
  528. continue;
  529. }
  530. }
  531. if (node.used && col.field === 'unit_price') {
  532. toastMessageUniq (hint.usedUp);
  533. continue;
  534. }
  535. if (colSetting.type === 'Number') {
  536. const num = _.toNumber(value);
  537. if (num) {
  538. data[colSetting.field] = num;
  539. } else {
  540. try {
  541. data[colSetting.field] = math.evaluate(transExpr(value));
  542. } catch (err) {
  543. toastMessageUniq(hint.invalidExpr);
  544. continue;
  545. }
  546. }
  547. } else {
  548. if (node.used && (col.field === 'code' || col.field ==='b_code')
  549. && data[colSetting.field] !== '' && value === '') {
  550. toastMessageUniq(hint.usedCode);
  551. continue;
  552. }
  553. data[colSetting.field] = value;
  554. }
  555. bPaste = true;
  556. }
  557. if (bPaste) {
  558. datas.push(data);
  559. } else {
  560. filterNodes.push(node);
  561. }
  562. }
  563. }
  564. if (datas.length > 0) {
  565. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  566. const refreshNode = tree.loadPostData(result);
  567. if (refreshNode.update) {
  568. refreshNode.update = refreshNode.update.concat(filterNodes);
  569. }
  570. billsTreeSpreadObj.refreshTree(info.sheet, refreshNode);
  571. }, function () {
  572. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  573. });
  574. } else {
  575. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  576. }
  577. },
  578. deletePress: function (sheet) {
  579. if (!sheet.zh_setting) return;
  580. const sel = sheet.getSelections()[0], datas = [];
  581. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  582. const node = sheet.zh_tree.nodes[iRow];
  583. if (sheet.zh_tree.checkNodeUsed(node, pos)) {
  584. toastr.warning('"' + node.code + node.b_code + ' ' + node.name +'"已计量,请勿修改');
  585. return;
  586. }
  587. const data = sheet.zh_tree.getNodeKeyData(node);
  588. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  589. const col = sheet.zh_setting.cols[iCol];
  590. data[col.field] = null;
  591. }
  592. datas.push(data);
  593. }
  594. if (datas.length > 0) {
  595. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  596. const refreshNode = sheet.zh_tree.loadPostData(result);
  597. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  598. });
  599. }
  600. },
  601. pasteBlock: function (spread, copyInfo) {
  602. const self = this;
  603. const sheet = spread.getActiveSheet();
  604. const [tree, node] = this.getDefaultSelectInfo(spread.getActiveSheet());
  605. postData(window.location.pathname + '/update', {
  606. postType: 'paste-block',
  607. postData: {
  608. id: tree.getNodeKey(node),
  609. tid: copyInfo.tid,
  610. block: copyInfo.block,
  611. }
  612. }, function (data) {
  613. pos.updateDatas(data.pos);
  614. const result = tree.loadPostData(data.ledger);
  615. self.refreshTree(sheet, result);
  616. const sel = sheet.getSelections()[0];
  617. if (sel) {
  618. sheet.setSelection(tree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  619. }
  620. self.refreshOperationValid(sheet);
  621. removeLocalCache(copyBlockTag);
  622. }, null, true);
  623. },
  624. topRowChanged: function (e, info) {
  625. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  626. },
  627. editStarting(e, info) {
  628. if (!info.sheet.zh_setting || !info.sheet.zh_tree) return;
  629. const col = info.sheet.zh_setting.cols[info.col];
  630. const node = info.sheet.zh_tree.nodes[info.row];
  631. if (!node) {
  632. info.cancel = true;
  633. return;
  634. }
  635. switch (col.field) {
  636. case 'code':
  637. const posRange = pos.getLedgerPos(node.id);
  638. return posRange && posRange.length > 0;
  639. case 'unit_price':
  640. info.cancel = (node.children && node.children.length > 0) || node.used;
  641. break;
  642. case 'sgfh_qty':
  643. case 'sgfh_tp':
  644. case 'sjcl_qty':
  645. case 'sjcl_tp':
  646. case 'qtcl_qty':
  647. case 'qtcl_tp':
  648. case 'deal_qty':
  649. case 'deal_tp':
  650. info.cancel = (node.children && node.children.length > 0);
  651. break;
  652. case 'dgn_qty1':
  653. case 'dgn_qty2':
  654. info.cancel = !_.isEmpty(node.b_code);
  655. break;
  656. }
  657. },
  658. cut: function (sheet, sel, callback) {
  659. if (!sheet || !sel) return;
  660. if (sel.colCount >= sheet.zh_setting.cols.length) {
  661. toastr.warning('请勿选中整行剪切');
  662. return;
  663. }
  664. const sortData = SpreadJsObj.getSortData(sheet), datas = [];
  665. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  666. const node = sortData[iRow];
  667. if (node) {
  668. const data = sheet.zh_tree.getNodeKeyData(node);
  669. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  670. const col = sheet.zh_setting.cols[iCol];
  671. if (col.field === 'b_code' || col.field === 'sgfh_qty' || col.field === 'sgfh_tp' ||
  672. col.field === 'sjcl_qty' || col.field === 'sjcl_tp' ||
  673. col.field === 'qtcl_qty' || col.field === 'qtcl_tp') {
  674. const lPos = pos.getLedgerPos(node.id);
  675. if (lPos && lPos.length > 0) {
  676. toastr.error('不可剪切');
  677. return;
  678. }
  679. }
  680. const style = sheet.getStyle(iRow, iCol);
  681. if (style.locked) {
  682. toastr.error('不可剪切');
  683. return;
  684. }
  685. const colSetting = sheet.zh_setting.cols[iCol];
  686. data[colSetting.field] = null;
  687. }
  688. datas.push(data);
  689. }
  690. }
  691. if (datas.length > 0) {
  692. callback();
  693. postData(window.location.pathname + '/update', {postType: 'update', postData: datas}, function (result) {
  694. const refreshNode = sheet.zh_tree.loadPostData(result);
  695. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  696. });
  697. }
  698. },
  699. sortCode: function (sheet) {
  700. const tree = sheet.zh_tree;
  701. if (!tree) return;
  702. const select = SpreadJsObj.getSelectObject(sheet);
  703. if (!select || !select.code || select.code === '') return;
  704. const recursiveSortCode = function (data, parentCode, children) {
  705. if (!children || children.length === 0) return;
  706. for (const [i, child] of children.entries()) {
  707. if (!child.b_code || child.b_code === '') {
  708. const code = parentCode + '-' + (i + 1);
  709. const cData = tree.getNodeKeyData(child);
  710. cData.code = code;
  711. data.push(cData);
  712. if (!tree.isLeafXmj(child)) {
  713. recursiveSortCode(data, code, child.children);
  714. }
  715. }
  716. }
  717. };
  718. const data = [];
  719. recursiveSortCode(data, select.code, select.children);
  720. if (data.length > 0) {
  721. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  722. const refreshNode = tree.loadPostData(result);
  723. billsTreeSpreadObj.refreshTree(sheet, refreshNode);
  724. })
  725. }
  726. }
  727. };
  728. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  729. billsTreeSpreadObj.loadExprToInput(billsSheet);
  730. billsSpread.bind(spreadNS.Events.SelectionChanged, billsTreeSpreadObj.selectionChanged);
  731. billsSpread.bind(spreadNS.Events.topRowChanged, billsTreeSpreadObj.topRowChanged);
  732. if (!readOnly) {
  733. // 增删上下移升降级
  734. $('a[name="base-opr"]').click(function () {
  735. billsTreeSpreadObj.baseOpr(billsSheet, this.getAttribute('type'));
  736. });
  737. $('a[name=cpc]').click(function () {
  738. billsSpread.commandManager().execute({
  739. cmd: this.getAttribute('type'),
  740. sheetName: billsSheet.name()
  741. });
  742. });
  743. $('#bills-expr').bind('change mouseleave', function () {
  744. if (this.readOnly) return;
  745. const expr = $(this);
  746. const select = SpreadJsObj.getSelectObject(billsSheet);
  747. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  748. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  749. const data = {
  750. id: select.id,
  751. tender_id: select.tender_id,
  752. ledger_id: select.ledger_id
  753. };
  754. if (newValue !== '') {
  755. const num = _.toNumber(newValue);
  756. if (num) {
  757. data[field] = num;
  758. } else {
  759. try {
  760. data[field] = math.evaluate(transExpr(newValue));
  761. } catch (err) {
  762. toastr.error('输入的表达式非法');
  763. return;
  764. }
  765. }
  766. } else {
  767. data[field] = null;
  768. }
  769. // 更新至服务器
  770. postData(window.location.pathname + '/update', {postType: 'update', postData: data}, function (result) {
  771. const refreshNode = billsTree.loadPostData(result);
  772. expr.val(select[field]);
  773. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  774. });
  775. });
  776. billsSpread.bind(spreadNS.Events.EditStarting, billsTreeSpreadObj.editStarting);
  777. billsSpread.bind(spreadNS.Events.EditEnded, billsTreeSpreadObj.editEnded);
  778. billsSpread.bind(spreadNS.Events.ClipboardPasting, billsTreeSpreadObj.clipboardPasting);
  779. billsSpread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  780. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  781. SpreadJsObj.Clipboard.setCopyData(copyText);
  782. });
  783. SpreadJsObj.addDeleteBind(billsSpread, billsTreeSpreadObj.deletePress);
  784. SpreadJsObj.addCutEvents(billsSpread, billsTreeSpreadObj.cut);
  785. let batchInsertObj;
  786. $.contextMenu.types.batchInsert = function (item, opt, root) {
  787. const self = this;
  788. if ($.isFunction(item.icon)) {
  789. item._icon = item.icon.call(this, this, $t, key, item);
  790. } else {
  791. if (typeof(item.icon) === 'string' && item.icon.substring(0, 3) === 'fa-') {
  792. // to enable font awesome
  793. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '--fa fa ' + item.icon;
  794. } else {
  795. item._icon = root.classNames.icon + ' ' + root.classNames.icon + '-' + item.icon;
  796. }
  797. }
  798. this.addClass(item._icon);
  799. 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>')
  800. .appendTo(this);
  801. const $input = $obj.find('input');
  802. const event = () => {
  803. if (self.hasClass('context-menu-disabled')) return;
  804. item.batchInsert($input[0], root);
  805. };
  806. $obj.on('click', event).keypress(function (e) {if (e.keyCode === 13) { event(); }});
  807. $input.click((e) => {e.stopPropagation();})
  808. .keyup((e) => {if (e.keyCode === 13) item.batchInsert($input[0], root);})
  809. .on('input', function () {this.value = this.value.replace(/[^\d]/g, '');});
  810. };
  811. // 右键菜单
  812. $.contextMenu({
  813. selector: '#bills-spread',
  814. build: function ($trigger, e) {
  815. const target = SpreadJsObj.safeRightClickSelection($trigger, e, billsSpread);
  816. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  817. },
  818. items: {
  819. 'create': {
  820. name: '新增',
  821. icon: 'fa-sign-in',
  822. callback: function (key, opt) {
  823. billsTreeSpreadObj.baseOpr(billsSheet, 'add');
  824. },
  825. disabled: function (key, opt) {
  826. const sheet = billsSheet;
  827. const selection = sheet.getSelections();
  828. const sel = selection ? selection[0] : sheet.getSelections()[0];
  829. const row = sel ? sel.row : -1;
  830. const tree = sheet.zh_tree;
  831. if (!tree) return true;
  832. const first = sheet.zh_tree.nodes[row];
  833. const valid = !sheet.zh_setting.readOnly;
  834. return !(valid && first && first.level > 1);
  835. }
  836. },
  837. 'delete': {
  838. name: '删除',
  839. icon: 'fa-remove',
  840. callback: function (key, opt) {
  841. billsTreeSpreadObj.baseOpr(billsSheet, 'delete');
  842. },
  843. disabled: function (key, opt) {
  844. const sheet = billsSheet;
  845. const selection = sheet.getSelections();
  846. const sel = selection ? selection[0] : sheet.getSelections()[0];
  847. const row = sel ? sel.row : -1;
  848. const tree = sheet.zh_tree;
  849. if (!tree) return true;
  850. const first = sheet.zh_tree.nodes[row];
  851. let last = first, sameParent = true, nodeUsed = first.used;
  852. if (sel.rowCount > 1 && first) {
  853. for (let r = 1; r < sel.rowCount; r++) {
  854. const rNode = tree.nodes[sel.row + r];
  855. if (!rNode) {
  856. sameParent = false;
  857. break;
  858. }
  859. nodeUsed = nodeUsed || rNode.used;
  860. if (rNode.level > first.level) continue;
  861. if ((rNode.level < first.level) || (rNode.level === first.level && rNode.pid !== first.pid)) {
  862. sameParent = false;
  863. break;
  864. }
  865. last = rNode;
  866. }
  867. }
  868. const valid = !sheet.zh_setting.readOnly;
  869. return !(valid && first && sameParent && !(first.level === 1 && first.node_type) && !nodeUsed);
  870. }
  871. },
  872. 'sprBase': '-----------',
  873. 'batchInsert': {
  874. name: '批量插入',
  875. type: 'batchInsert',
  876. value: '2',
  877. icon: 'fa-sign-in',
  878. batchInsert: function (obj, root) {
  879. if (_.toNumber(obj.value) > _.toNumber(obj.max)) {
  880. obj.value = obj.max;
  881. toastr.warning('批量插入不可多于' + obj.max);
  882. } else if (_.toNumber(obj.value) < _.toNumber(obj.min)) {
  883. obj.value = obj.min;
  884. toastr.warning('批量插入不可少于' + obj.min);
  885. } else {
  886. billsTreeSpreadObj.baseOpr(billsSheet, 'add', parseInt(obj.value));
  887. root.$menu.trigger('contextmenu:hide');
  888. }
  889. },
  890. disabled: function (key, opt) {
  891. const sheet = billsSheet;
  892. const selection = sheet.getSelections();
  893. const sel = selection ? selection[0] : sheet.getSelections()[0];
  894. const row = sel ? sel.row : -1;
  895. const tree = sheet.zh_tree;
  896. if (!tree) return true;
  897. const first = sheet.zh_tree.nodes[row];
  898. const valid = !sheet.zh_setting.readOnly;
  899. return !(valid && first && first.level > 1);
  900. }
  901. },
  902. 'batchInsertBillsPos': {
  903. name: '批量插入清单-计量单元',
  904. icon: 'fa-sign-in',
  905. disabled: function (key, opt) {
  906. if (!isTz) return true;
  907. const select = SpreadJsObj.getSelectObject(billsSheet);
  908. if (select) {
  909. if (select.code && select.code !== '') {
  910. return !billsTree.isLeafXmj(select);
  911. } else {
  912. const parent = billsTree.getParent(select);
  913. return !(parent && billsTree.isLeafXmj(parent));
  914. }
  915. } else {
  916. return false;
  917. }
  918. },
  919. callback: function (key, opt) {
  920. if (!batchInsertObj) {
  921. batchInsertObj = new BatchInsertBillsPosObj($('#batch'));
  922. } else {
  923. batchInsertObj.initView();
  924. }
  925. $('#batch').modal('show');
  926. }
  927. },
  928. 'sprBatch': '-----------',
  929. 'importGclBills2Xmj': {
  930. name: '导入工程量清单至项目节',
  931. icon: 'fa-file-excel-o',
  932. disabled: function (key, opt) {
  933. const node = SpreadJsObj.getSelectObject(billsSheet);
  934. return readOnly
  935. || (node.children && node.children.length > 0)
  936. || (!_.isNil(node.b_code) && node.b_code !== '')
  937. || billsTree.checkNodeUsed(node, pos);
  938. },
  939. callback: function (key, opt) {
  940. const node = SpreadJsObj.getSelectObject(billsSheet);
  941. importExcel.doImport({
  942. template: {
  943. hint: '工程量清单',
  944. url: '/template/导入工程量清单EXCEL格式.xls',
  945. },
  946. callback: function (sheet) {
  947. postDataCompress(window.location.pathname + '/upload-excel/gcl2xmj', {id: node.id, sheet: sheet}, function (result) {
  948. const sel = billsSheet.getSelections();
  949. const refreshNode = billsTree.loadPostData(result);
  950. billsTreeSpreadObj.refreshTree(billsSheet, refreshNode);
  951. if (refreshNode.create[0]) {
  952. if (sel && sel[0]) {
  953. billsSheet.setSelection(refreshNode.create[0].index, sel[0].col, sel[0].rowCount, sel[0].colCount);
  954. } else {
  955. billsSheet.setSelection(refreshNode.create[0].index, 0, 1, 1);
  956. }
  957. }
  958. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  959. }, null);
  960. }
  961. });
  962. }
  963. },
  964. }
  965. });
  966. }
  967. const posSpreadObj = {
  968. /**
  969. * 加载计量单元 根据当前台账选择节点
  970. */
  971. loadCurPosData: function () {
  972. const node = SpreadJsObj.getSelectObject(billsSheet);
  973. if (node) {
  974. const posData = pos.getLedgerPos(node.id) || [];
  975. SpreadJsObj.loadSheetData(posSheet, 'data', posData);
  976. } else {
  977. SpreadJsObj.loadSheetData(posSheet, 'data', []);
  978. }
  979. SpreadJsObj.resetFieldReadOnly(posSheet);
  980. },
  981. editStarting: function (e, info) {
  982. posSpreadObj.billsNode = SpreadJsObj.getSelectObject(billsSheet);
  983. },
  984. /**
  985. * 编辑单元格响应事件
  986. * @param {Object} e
  987. * @param {Object} info
  988. */
  989. editEnded: function (e, info) {
  990. if (!info.sheet.zh_setting) {
  991. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  992. return;
  993. }
  994. const posData = info.sheet.zh_data ? info.sheet.zh_data[info.row] : null;
  995. const col = info.sheet.zh_setting.cols[info.col];
  996. const orgText = posData ? posData[col.field] : null;
  997. const newText = trimInvalidChar(info.editingText);
  998. if (orgText === newText || ((!orgText || orgText === '') && (newText === ''))) return;
  999. const node = posSpreadObj.billsNode;
  1000. if (!node) {
  1001. toastr.error('数据错误,请选择台账节点后再试');
  1002. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1003. return;
  1004. } else if (newText && newText !== '' && node.children && node.children.length > 0) {
  1005. toastr.error('父节点不可插入计量单元');
  1006. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1007. return;
  1008. } else if (newText && newText !== '' && (!node.b_code || node.b_code === '')) {
  1009. toastr.error('项目节不可插入计量单元');
  1010. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1011. return;
  1012. }
  1013. const data = {postType: 'pos'};
  1014. if (col.field === 'name') {
  1015. if (newText === '' && posData) {
  1016. toastr.error('部位名称不可为空', 'error');
  1017. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1018. return;
  1019. } else if (!posData) {
  1020. if (newText && newText !== '') {
  1021. data.posPostType = 'add';
  1022. const sortData = info.sheet.zh_data;
  1023. const order = (!sortData || sortData.length === 0) ? 1 : Math.max(sortData[sortData.length - 1].porder + 1, sortData.length + 1);
  1024. data.postData = { name: newText, lid: node.id, porder: order};
  1025. } else {
  1026. return;
  1027. }
  1028. } else {
  1029. data.posPostType = 'update';
  1030. data.postData = {id: posData.id, name: newText};
  1031. }
  1032. } else if (!posData) {
  1033. toastr.warning('新增计量单元请先输入名称');
  1034. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1035. return;
  1036. } else {
  1037. data.posPostType = 'update';
  1038. data.postData = {id: posData.id};
  1039. if (col.type === 'Number') {
  1040. const num = _.toNumber(newText);
  1041. if (_.isFinite(num)) {
  1042. data.postData[col.field] = num;
  1043. } else {
  1044. try {
  1045. data.postData[col.field] = math.evaluate(transExpr(newText));
  1046. } catch(err) {
  1047. toastr.error('输入的表达式非法');
  1048. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1049. return;
  1050. }
  1051. }
  1052. } else {
  1053. data.postData[col.field] = newText;
  1054. }
  1055. }
  1056. postData(window.location.pathname + '/update', data, function (result) {
  1057. const updateRst = pos.updateDatas(result.pos);
  1058. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1059. if (updateRst.create.length > 0) {
  1060. posSpreadObj.loadCurPosData();
  1061. } else {
  1062. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1063. }
  1064. const loadResult = billsTree.loadPostData(result.ledger);
  1065. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1066. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1067. }, function () {
  1068. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1069. });
  1070. },
  1071. /**
  1072. * 删除按钮响应事件
  1073. * @param sheet
  1074. */
  1075. deletePress: function (sheet) {
  1076. if (!sheet.zh_settting) return;
  1077. const sortData = sheet.zh_data;
  1078. const datas = [], posSelects = [];
  1079. const sel = sheet.getSelections()[0];
  1080. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  1081. let bDel = false;
  1082. const node = sortData[iRow];
  1083. if (node) {
  1084. const data = {id: node.id};
  1085. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  1086. const colSetting = sheet.zh_setting.cols[iCol];
  1087. if (colSetting.field === 'name') {
  1088. toastr.error('部位名称不能为空');
  1089. return;
  1090. }
  1091. const style = sheet.getStyle(iRow, iCol);
  1092. if (!style.locked) {
  1093. const colSetting = sheet.zh_setting.cols[iCol];
  1094. data[colSetting.field] = null;
  1095. bDel = true;
  1096. }
  1097. }
  1098. if (bDel) {
  1099. datas.push(data);
  1100. posSelects.push(node);
  1101. }
  1102. }
  1103. }
  1104. if (datas.length > 0) {
  1105. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: datas}, function (result) {
  1106. pos.updateDatas(result.pos);
  1107. posSpreadObj.loadCurPosData();
  1108. const loadResult = billsTree.loadPostData(result.ledger);
  1109. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1110. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1111. }, function () {
  1112. posSpreadObj.loadCurPosData();
  1113. });
  1114. }
  1115. },
  1116. /**
  1117. * 删除 计量单元
  1118. * @param sheet
  1119. */
  1120. deletePos: function (sheet) {
  1121. const selection = sheet.getSelections();
  1122. const data = {
  1123. postType: 'pos',
  1124. posPostType: 'delete',
  1125. postData: [],
  1126. };
  1127. const row = selection[0].row, count = selection[0].rowCount;
  1128. const sortData = sheet.zh_data;
  1129. for (let iRow = 0; iRow < count; iRow++) {
  1130. const posData = sortData[iRow + row];
  1131. if (posData) {
  1132. if (posData.used) {
  1133. toastr.error('"' + posData.name + '"已计量,请勿删除');
  1134. return;
  1135. }
  1136. data.postData.push(sortData[iRow + row].id);
  1137. }
  1138. }
  1139. if (data.postData.length > 0) {
  1140. postData(window.location.pathname + '/update', data, function (result) {
  1141. pos.removeDatas(result.pos);
  1142. sheet.deleteRows(row, count);
  1143. const loadResult = billsTree.loadPostData(result.ledger);
  1144. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1145. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1146. });
  1147. }
  1148. },
  1149. clipboardPasting: function (e, info) {
  1150. if (!info.sheet.zh_setting) {
  1151. info.cancel = true;
  1152. return;
  1153. }
  1154. const range = info.cellRange;
  1155. for (let iRow = range.row; iRow < range.row + range.rowCount; iRow++) {
  1156. const posData = info.sheet.zh_data[iRow];
  1157. if (posData && posData.used) {
  1158. toastr.warning('"' + pos.name +'"已计量,请勿修改');
  1159. info.cancel = true;
  1160. return;
  1161. }
  1162. }
  1163. },
  1164. /**
  1165. * 粘贴单元格响应事件
  1166. * @param e
  1167. * @param info
  1168. */
  1169. clipboardPasted: function (e, info) {
  1170. const node = SpreadJsObj.getSelectObject(billsSheet);
  1171. if (node.code && (node.code !== '')) {
  1172. toastr.error('项目节不可含有节点明细');
  1173. posSpreadObj.loadCurPosData();
  1174. return;
  1175. }
  1176. if (node.children && (node.children.length > 0)) {
  1177. toastr.error('仅节点子项可以含有计量单元');
  1178. posSpreadObj.loadCurPosData();
  1179. return;
  1180. }
  1181. if (!info.sheet.zh_setting) {
  1182. posSpreadObj.loadCurPosData();
  1183. return;
  1184. }
  1185. const data = [];
  1186. const sortData = info.sheet.zh_data || [];
  1187. if (sortData.length === 0 || info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1188. if (info.cellRange.col !== 0) {
  1189. toastr.warning('新增计量单元请先输入名称');
  1190. posSpreadObj.loadCurPosData();
  1191. return;
  1192. }
  1193. }
  1194. let bHint = false;
  1195. const lastOrder = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1196. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  1197. let bPaste = true;
  1198. const curRow = info.cellRange.row + iRow;
  1199. const posData = curRow >= sortData.length ? {lid: node.id, porder: lastOrder + curRow - sortData.length} : {id: sortData[curRow].id, lid: node.id};
  1200. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  1201. const curCol = info.cellRange.col + iCol;
  1202. const colSetting = info.sheet.zh_setting.cols[curCol];
  1203. posData[colSetting.field] = trimInvalidChar(info.sheet.getText(curRow, curCol));
  1204. if (colSetting.type === 'Number') {
  1205. const num = _.toNumber(posData[colSetting.field]);
  1206. if (num) {
  1207. posData[colSetting.field] = num;
  1208. } else {
  1209. try {
  1210. posData[colSetting.field] = math.evaluate(transExpr(posData[colSetting.field]));
  1211. } catch (err) {
  1212. if (!bHint) {
  1213. toastr.warning('粘贴了非法表达式,已过滤');
  1214. bHint = true;
  1215. }
  1216. bPaste = false;
  1217. }
  1218. }
  1219. }
  1220. }
  1221. if (bPaste) {
  1222. data.push(posData);
  1223. }
  1224. }
  1225. if (data.length === 0) {
  1226. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1227. return;
  1228. }
  1229. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: data}, function (result) {
  1230. pos.updateDatas(result.pos);
  1231. posSpreadObj.loadCurPosData();
  1232. const loadResult = billsTree.loadPostData(result.ledger);
  1233. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1234. posSpreadObj.loadCurPosData();
  1235. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1236. }, function () {
  1237. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1238. });
  1239. },
  1240. selectionChanged: function (e, info) {
  1241. const col = info.sheet.zh_setting.cols[info.newSelections[0].col];
  1242. const cell = info.sheet.getCell(info.newSelections[0].col, info.newSelections[0].col);
  1243. if (col && col.type === 'Number') {
  1244. const data = SpreadJsObj.getSelectObject(info.sheet);
  1245. if (data) {
  1246. $('#pos-expr').val(data[col.field]).attr('field', col.field).attr('org', data[col.field])
  1247. .attr('row', info.newSelections[0].row).attr('readOnly', readOnly || cell.locked());
  1248. } else {
  1249. $('#pos-expr').val('').attr('readOnly', true);
  1250. }
  1251. } else {
  1252. $('#pos-expr').val('').attr('readOnly', true);
  1253. }
  1254. },
  1255. addPegs: function (pegs) {
  1256. if (!pegs || pegs.length <= 0) return;
  1257. const node = SpreadJsObj.getSelectObject(billsSheet);
  1258. if (!node) return;
  1259. const sheet = posSpread.getActiveSheet();
  1260. const sortData = sheet.zh_data || [];
  1261. let order = sortData.length > 0 ? sortData[sortData.length - 1].porder + 1 : 1;
  1262. pegs.forEach(function (p) {p.porder = ++order; p.lid = node.id});
  1263. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'paste', postData: pegs}, function (result) {
  1264. pos.updateDatas(result.pos);
  1265. posSpreadObj.loadCurPosData();
  1266. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1267. });
  1268. }
  1269. };
  1270. posSpreadObj.loadCurPosData();
  1271. SpreadJsObj.resetTopAndSelect(posSheet);
  1272. posSpread.bind(spreadNS.Events.SelectionChanged, posSpreadObj.selectionChanged);
  1273. if (!readOnly && isTz) {
  1274. $('#pos-expr').bind('change mouseleave', function () {
  1275. if (this.readOnly) return;
  1276. const expr = $(this);
  1277. const select = SpreadJsObj.getSelectObject(posSheet);
  1278. const row = posSheet.getSelections()[0].row;
  1279. const field = expr.attr('field'), orgValue = expr.attr('org'), newValue = trimInvalidChar(expr.val());
  1280. if (orgValue === newValue || (!orgValue && newValue == '')) { return; }
  1281. const data = {id: select.id};
  1282. if (newValue !== '') {
  1283. const num = _.toNumber(newValue);
  1284. if (num) {
  1285. data[field] = num;
  1286. } else {
  1287. try {
  1288. data[field] = math.evaluate(transExpr(newValue));
  1289. } catch (err) {
  1290. toastr.error('输入的表达式非法');
  1291. return;
  1292. }
  1293. }
  1294. } else {
  1295. data[field] = null;
  1296. }
  1297. // 更新至服务器
  1298. postData(window.location.pathname + '/update', {postType: 'pos', posPostType: 'update', postData: data}, function (result) {
  1299. const updateRst = pos.updateDatas(result.pos);
  1300. expr.val(select[field]);
  1301. // 刷新当前行, 不适用于新增(在非下一空白行新增)
  1302. SpreadJsObj.reLoadRowData(posSheet, row);
  1303. const loadResult = billsTree.loadPostData(result.ledger);
  1304. billsTreeSpreadObj.refreshTree(billsSheet, loadResult);
  1305. billsTreeSpreadObj.refreshOperationValid(billsSheet);
  1306. });
  1307. });
  1308. posSpread.bind(spreadNS.Events.EditStarting, posSpreadObj.editStarting);
  1309. posSpread.bind(spreadNS.Events.EditEnded, posSpreadObj.editEnded);
  1310. posSpread.bind(spreadNS.Events.ClipboardPasting, posSpreadObj.clipboardPasting);
  1311. posSpread.bind(spreadNS.Events.ClipboardPasted, posSpreadObj.clipboardPasted);
  1312. SpreadJsObj.addDeleteBind(posSpread, posSpreadObj.deletePress);
  1313. const mergePeg = NewMergePeg({ callback: posSpreadObj.addPegs });
  1314. $.contextMenu({
  1315. selector: '#pos-spread',
  1316. build: function ($trigger, e) {
  1317. const target = SpreadJsObj.safeRightClickSelection($trigger, e, posSpread);
  1318. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1319. },
  1320. items: {
  1321. 'delete': {
  1322. name: '删除',
  1323. icon: 'fa-remove',
  1324. disabled: function (key, opt) {
  1325. if (posSheet.zh_data) {
  1326. const selection = posSheet.getSelections();
  1327. return posSheet.zh_data.length < selection[0].row + selection[0].rowCount;
  1328. } else {
  1329. return true;
  1330. }
  1331. },
  1332. callback: function (key, opt) {
  1333. posSpreadObj.deletePos(posSheet);
  1334. }
  1335. },
  1336. 'merge-peg': {
  1337. name: '合并起讫桩号',
  1338. disabled: function (key, opt) {
  1339. const node = SpreadJsObj.getSelectObject(billsSheet);
  1340. return _.isNil(node) || _.isNil(node.b_code) || node.b_code === '';
  1341. },
  1342. callback: function (key, opt) {
  1343. mergePeg.show();
  1344. },
  1345. visible: function (key, opt) {
  1346. return isTz;
  1347. }
  1348. }
  1349. }
  1350. });
  1351. }
  1352. $.divResizer({
  1353. select: '#revise-resize',
  1354. callback: function () {
  1355. billsSpread.refresh();
  1356. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  1357. $(".sp-wrap").height(bcontent-30);
  1358. posSpread.refresh();
  1359. }
  1360. });
  1361. class DealBills {
  1362. constructor (selector, spreadSetting) {
  1363. const self = this;
  1364. this.loaded = false;
  1365. this.obj = $(selector)[0];
  1366. this.url = '/tender/' + window.location.pathname.split('/')[2] + '/deal';
  1367. this.spreadSetting = spreadSetting;
  1368. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1369. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  1370. if (!readOnly) {
  1371. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1372. const dealSheet = info.sheet;
  1373. const mainSheet = billsSheet;
  1374. const dealBills = SpreadJsObj.getSelectObject(dealSheet);
  1375. if (!dealBills) { return; }
  1376. const mainTree = mainSheet.zh_tree;
  1377. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1378. if (!mainNode || !mainTree) { return; }
  1379. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1380. toastr.warning('非最底层项目下,不应添加节点');
  1381. return;
  1382. }
  1383. postData(window.location.pathname + '/update', {
  1384. postType: 'add-deal',
  1385. postData: {
  1386. id: mainNode.ledger_id,
  1387. type: mainNode.code ? 'child' : 'next',
  1388. dealBills: {
  1389. b_code: dealBills.code, name: dealBills.name, unit: dealBills.unit,
  1390. unit_price: dealBills.unit_price,
  1391. }
  1392. },
  1393. }, function (result) {
  1394. const refreshData = mainTree.loadPostData(result);
  1395. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1396. const sel = mainSheet.getSelections()[0];
  1397. if (sel && refreshData.create[0]) {
  1398. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1399. }
  1400. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1401. billsSpread.focus();
  1402. posSpreadObj.loadCurPosData();
  1403. });
  1404. });
  1405. }
  1406. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1407. }
  1408. loadData (callback) {
  1409. if (this.loaded) {
  1410. if (callback) callback();
  1411. return;
  1412. }
  1413. const self = this;
  1414. postData(this.url+'/get-data', {}, function (data) {
  1415. self.data = data;
  1416. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1417. self.loaded = true;
  1418. if (callback) callback();
  1419. });
  1420. }
  1421. calculateData () {
  1422. if (this.data) {
  1423. for (const d of this.data) {
  1424. d.total_price = _.multiply(d.quantity, d.unit_price);
  1425. }
  1426. }
  1427. }
  1428. }
  1429. class BgBills {
  1430. constructor (selector, spreadSetting) {
  1431. this.loaded = false;
  1432. this.obj = $(selector)[0];
  1433. this.spreadSetting = spreadSetting;
  1434. this.spread = SpreadJsObj.createNewSpread(this.obj);
  1435. this.sheet = this.spread.getActiveSheet();
  1436. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  1437. if (!readOnly) {
  1438. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1439. const dealSheet = info.sheet;
  1440. const mainSheet = billsSheet;
  1441. const bgBills = SpreadJsObj.getSelectObject(dealSheet);
  1442. if (!bgBills) { return; }
  1443. const mainTree = mainSheet.zh_tree;
  1444. const mainNode = SpreadJsObj.getSelectObject(mainSheet);
  1445. if (!mainNode || !mainTree) { return; }
  1446. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1447. toastr.warning('非最底层项目下,不应添加变更清单');
  1448. return;
  1449. }
  1450. postData(window.location.pathname + '/update', {
  1451. postType: 'add-bg',
  1452. postData: {
  1453. id: mainNode.ledger_id,
  1454. type: mainNode.code ? 'child' : 'next',
  1455. dealBills: {
  1456. b_code: bgBills.code, name: bgBills.name, unit: bgBills.unit,
  1457. unit_price: bgBills.unit_price,
  1458. }
  1459. },
  1460. }, function (result) {
  1461. const refreshData = mainTree.loadPostData(result);
  1462. billsTreeSpreadObj.refreshTree(mainSheet, refreshData);
  1463. const sel = mainSheet.getSelections()[0];
  1464. if (sel && refreshData.create[0]) {
  1465. mainSheet.setSelection(mainTree.nodes.indexOf(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  1466. }
  1467. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1468. billsSpread.focus();
  1469. posSpreadObj.loadCurPosData();
  1470. });
  1471. });
  1472. }
  1473. SpreadJsObj.forbiddenSpreadContextMenu(selector, this.spread);
  1474. }
  1475. loadData () {
  1476. if (this.loaded) return;
  1477. const self = this;
  1478. postData('/tender/' + window.location.pathname.split('/')[2] +'/change/bills', {type: 'gather'}, function (data) {
  1479. self.data = data;
  1480. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
  1481. self.loaded = true;
  1482. });
  1483. }
  1484. }
  1485. class BatchInsertBillsPosObj {
  1486. constructor (obj) {
  1487. const self = this;
  1488. this.obj = obj;
  1489. this.billsCount = 6;
  1490. this.posCount = 1000;
  1491. // 初始化 清单编号窗口 参数
  1492. this.qdSpreadSetting = {
  1493. cols: [
  1494. {title: '编号', field: 'code', hAlign: 0, width: 80, formatter: '@'},
  1495. {title: '名称', field: 'name', hAlign: 0, width: 120, formatter: '@'},
  1496. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@'},
  1497. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  1498. {title: '图册号', field: 'name', hAlign: 0, width: 60, formatter: '@'},
  1499. ],
  1500. emptyRows: this.billsCount,
  1501. headRows: 1,
  1502. headRowHeight: [32],
  1503. headerFont: '12px 微软雅黑',
  1504. font: '12px 微软雅黑',
  1505. };
  1506. this.qdSpread = SpreadJsObj.createNewSpread($('.batch-l-t', this.obj)[0]);
  1507. // 初始化 部位数量复核表 参数
  1508. this.posSpreadSetting = {
  1509. cols: [
  1510. {title: '名称', field: 'bw', hAlign: 0, width: 80, formatter: '@'},
  1511. {title: '图册号', field: 'drawingCode', hAlign: 0, formatter: '@', width: 60},
  1512. ],
  1513. emptyRows: this.posCount,
  1514. headRows: 1,
  1515. headRowHeight: [32],
  1516. headerFont: '12px 微软雅黑',
  1517. font: '12px 微软雅黑',
  1518. };
  1519. for (let iNum = 1; iNum <= this.billsCount; iNum++) {
  1520. this.posSpreadSetting.cols.push(
  1521. {title: '清单' + iNum, field: 'bills' + iNum, hAlign: 2, width: 50}
  1522. )
  1523. }
  1524. this.posSpread = SpreadJsObj.createNewSpread($('.batch-l-b', this.obj)[0]);
  1525. // 初始化 签约节点 参数
  1526. this.dealSpreadSetting = {
  1527. cols: [
  1528. {title: '清单编号', field: 'code', width: 80, formatter: '@', readOnly: true},
  1529. {title: '名称', field: 'name', width: 120, formatter: '@', readOnly: true},
  1530. {title: '单位', field: 'unit', width: 50, formatter: '@', readOnly: true},
  1531. {title: '单价', field: 'unit_price', width: 50, readOnly: true},
  1532. ],
  1533. emptyRows: 0,
  1534. headRows: 1,
  1535. headRowHeight: [32],
  1536. headerFont: '12px 微软雅黑',
  1537. font: '12px 微软雅黑',
  1538. };
  1539. this.dealSpread = SpreadJsObj.createNewSpread($('.batch-r', this.obj)[0]);
  1540. // 初始化 清单编号、部位数量复核表 表格
  1541. this.initView();
  1542. SpreadJsObj.initSheet(this.dealSpread.getActiveSheet(), this.dealSpreadSetting);
  1543. SpreadJsObj.refreshColumnAlign(this.dealSpread.getActiveSheet());
  1544. // 拉取签约节点数据
  1545. dealBills.loadData(() => {
  1546. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1547. });
  1548. // 双击签约节点,自动添加到清单编号窗口
  1549. this.dealSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  1550. const deal = info.sheet.zh_data[info.row];
  1551. const qdSheet = self.qdSpread.getActiveSheet(), posSheet = self.posSpread.getActiveSheet();
  1552. const sel = qdSheet.getSelections()[0];
  1553. qdSheet.getCell(sel.row, 0).value(deal.code);
  1554. qdSheet.getCell(sel.row, 1).value(deal.name);
  1555. qdSheet.getCell(sel.row, 2).value(deal.unit);
  1556. qdSheet.getCell(sel.row, 3).value(deal.unit_price);
  1557. if (sel.row + 1 === qdSheet.getRowCount()) {
  1558. const count = sel.row + 2;
  1559. qdSheet.setRowCount(count);
  1560. qdSheet.getCell(sel.row + 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + count);
  1561. const colCount = posSheet.getColumnCount() + 1;
  1562. posSheet.setColumnCount(colCount);
  1563. posSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).text('清单' + count);
  1564. }
  1565. qdSheet.setSelection(sel.row + 1, sel.col, 1, 1);
  1566. qdSheet.getParent().focus();
  1567. });
  1568. this.qdSpread.bind(spreadNS.Events.ClipboardPasted, function (e, info) {
  1569. const billsCount = info.sheet.getRowCount(), posSheet = self.posSpread.getActiveSheet();
  1570. const count = posSheet.getColumnCount() - 2;
  1571. if (billsCount > count) {
  1572. posSheet.setColumnCount(billsCount + 2);
  1573. for (let i = count + 1; i <= billsCount; i++) {
  1574. info.sheet.getCell(i - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + i);
  1575. posSheet.getCell(0, i + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + i);
  1576. }
  1577. }
  1578. });
  1579. this.qdSpread.bind(spreadNS.Events.EditEnded, function (e, info) {
  1580. if (info.col === 0) {
  1581. const dealBills = self.dealSpread.getActiveSheet().zh_data;
  1582. if (dealBills && dealBills.length > 0) {
  1583. const bills = _.find(dealBills, {code: info.editingText});
  1584. if (bills) {
  1585. info.sheet.getCell(info.row, 1).value(bills.name);
  1586. info.sheet.getCell(info.row, 2).value(bills.unit);
  1587. info.sheet.getCell(info.row, 3).value(bills.unit_price);
  1588. }
  1589. }
  1590. }
  1591. });
  1592. this.obj.bind('shown.bs.modal', function () {
  1593. self.qdSpread.refresh();
  1594. self.posSpread.refresh();
  1595. self.dealSpread.refresh();
  1596. });
  1597. $('#batch-ok').click(function () {
  1598. const selection = billsSheet.getSelections();
  1599. const row = selection[0].row;
  1600. const select = billsTree.nodes[row];
  1601. if (select) {
  1602. const insertData = {};
  1603. insertData.batchType = (select.code && select.code !== '') ? 'child' : 'next';
  1604. insertData.id = select[billsTree.setting.id];
  1605. insertData.batchData = self.getBatchData();
  1606. postData(window.location.pathname + '/update', {postType: 'batch-insert', postData: insertData}, function (data) {
  1607. pos.updateDatas(data.pos);
  1608. const result = billsTree.loadPostData(data.ledger);
  1609. billsTreeSpreadObj.refreshTree(billsSheet, result);
  1610. billsTreeSpreadObj.refreshOperationValid(billsSheet, selection);
  1611. self.obj.modal('hide');
  1612. }, null, true);
  1613. }
  1614. });
  1615. $.contextMenu({
  1616. selector: '.batch-l-t',
  1617. build: function ($trigger, e) {
  1618. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.qdSpread);
  1619. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  1620. },
  1621. items: {
  1622. 'create': {
  1623. name: '新增行',
  1624. icon: 'fa-sign-in',
  1625. callback: function (key, opt) {
  1626. const qdSheet = self.qdSpread.getActiveSheet();
  1627. const posSheet = self.posSpread.getActiveSheet();
  1628. qdSheet.addRows(qdSheet.getRowCount(), 1);
  1629. const index = qdSheet.getRowCount();
  1630. qdSheet.getCell(qdSheet.getRowCount() - 1, 0, spreadNS.SheetArea.rowHeader).text('清单' + index);
  1631. posSheet.addColumns(posSheet.getColumnCount(), 1);
  1632. posSheet.getCell(0, index + 2 - 1, spreadNS.SheetArea.colHeader).text('清单' + index);
  1633. },
  1634. },
  1635. 'delete': {
  1636. name: '删除行',
  1637. icon: 'fa-remove',
  1638. callback: function (key, opt) {
  1639. const qdSheet = self.qdSpread.getActiveSheet();
  1640. const posSheet = self.posSpread.getActiveSheet();
  1641. const sel = qdSheet.getSelections()[0];
  1642. qdSheet.deleteRows(sel.row, sel.rowCount);
  1643. posSheet.deleteColumns(sel.row + 2, sel.rowCount);
  1644. for (let iRow = 0, iLen = qdSheet.getRowCount(); iRow < iLen; iRow++) {
  1645. qdSheet.getCell(iRow, 0, spreadNS.SheetArea.rowHeader).text('清单' + (iRow+1));
  1646. }
  1647. for (let iCol = 0, iLen = posSheet.getColumnCount() - 2; iCol < iLen; iCol++) {
  1648. posSheet.getCell(0, iCol + 2, spreadNS.SheetArea.colHeader).text('清单' + (iCol+1));
  1649. }
  1650. qdSheet.setSelection(sel.row, sel.col, 1, 1);
  1651. },
  1652. },
  1653. }
  1654. });
  1655. }
  1656. // 初始化左侧表格
  1657. initView () {
  1658. // 初始化 清单编号
  1659. const qdSheet = this.qdSpread.getActiveSheet();
  1660. SpreadJsObj.initSheet(qdSheet, this.qdSpreadSetting);
  1661. SpreadJsObj.refreshColumnAlign(qdSheet);
  1662. // 清理原有数据
  1663. SpreadJsObj.beginMassOperation(qdSheet);
  1664. qdSheet.clear(0, 0, qdSheet.getRowCount(), qdSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1665. for (let iRow = 1; iRow <= this.billsCount; iRow++) {
  1666. qdSheet.getCell(iRow - 1, 0, GC.Spread.Sheets.SheetArea.rowHeader).text('清单' + iRow);
  1667. }
  1668. qdSheet.setSelection(0, 0, 1 ,1);
  1669. SpreadJsObj.endMassOperation(qdSheet);
  1670. // 初始化 部位数量复核表
  1671. const posSheet = this.posSpread.getActiveSheet();
  1672. SpreadJsObj.initSheet(posSheet, this.posSpreadSetting);
  1673. SpreadJsObj.refreshColumnAlign(posSheet);
  1674. // 清理原有数据
  1675. SpreadJsObj.beginMassOperation(posSheet);
  1676. posSheet.setColumnWidth(0, 45, GC.Spread.Sheets.SheetArea.rowHeader);
  1677. posSheet.clear(0, 0, posSheet.getRowCount(), posSheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  1678. posSheet.setSelection(0, 0, 1 ,1);
  1679. SpreadJsObj.endMassOperation(posSheet);
  1680. // 检查签约节点数据,以工具栏数据为准
  1681. if (dealBills) {
  1682. SpreadJsObj.loadSheetData(this.dealSpread.getActiveSheet(), 'data', dealBills.data);
  1683. }
  1684. this.dealSpread.getActiveSheet().setSelection(0, 0, 1, 1);
  1685. }
  1686. // 获取界面数据
  1687. getBatchData () {
  1688. const result = [];
  1689. const qdSheet = this.qdSpread.getActiveSheet(), posSheet = this.posSpread.getActiveSheet();
  1690. for (let iRow = 0; iRow < qdSheet.getRowCount(); iRow++) {
  1691. if (qdSheet.getText(iRow, 0) === '') { continue; }
  1692. const qd = {
  1693. b_code: qdSheet.getText(iRow, 0),
  1694. name: qdSheet.getText(iRow, 1),
  1695. unit: qdSheet.getText(iRow, 2),
  1696. price: _.toNumber(qdSheet.getText(iRow, 3)),
  1697. pos: [],
  1698. };
  1699. result.push(qd);
  1700. for (let iPosRow = 0; iPosRow < posSheet.getRowCount(); iPosRow++) {
  1701. const value = _.toNumber(posSheet.getText(iPosRow, iRow + 2));
  1702. if (value !== 0 && !isNaN(value)) {
  1703. qd.pos.push({
  1704. name: posSheet.getText(iPosRow, 0),
  1705. drawing_code: posSheet.getText(iPosRow, 1),
  1706. quantity: value,
  1707. });
  1708. }
  1709. }
  1710. }
  1711. return result;
  1712. }
  1713. }
  1714. const dealBills = new DealBills('#deal-bills-spread', {
  1715. cols: [
  1716. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  1717. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1718. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1719. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1720. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1721. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1722. ],
  1723. emptyRows: 0,
  1724. headRows: 1,
  1725. headRowHeight: [32],
  1726. defaultRowHeight: 21,
  1727. headerFont: '12px 微软雅黑',
  1728. font: '12px 微软雅黑',
  1729. });
  1730. const bgBills = new BgBills('#bg-bills-spread', {
  1731. cols: [
  1732. {title: '清单编号', field: 'code', hAlign: 0, width: 85, formatter: '@', readOnly: true},
  1733. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1734. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  1735. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1736. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1737. {title: '金额', field: 'total_price', hAlign: 2, width: 50, readOnly: true},
  1738. ],
  1739. emptyRows: 0,
  1740. headRows: 1,
  1741. headRowHeight: [32],
  1742. defaultRowHeight: 21,
  1743. headerFont: '12px 微软雅黑',
  1744. font: '12px 微软雅黑',
  1745. });
  1746. $.divResizer({
  1747. select: '#revise-right-spr',
  1748. callback: function () {
  1749. billsSpread.refresh();
  1750. if (posSpread) {
  1751. posSpread.refresh();
  1752. }
  1753. if (stdXmj) {
  1754. stdXmj.spread.refresh();
  1755. }
  1756. if (stdGcl) {
  1757. stdGcl.spread.refresh();
  1758. }
  1759. if (dealBills) {
  1760. dealBills.spread.refresh();
  1761. }
  1762. if (bgBills) {
  1763. bgBills.spread.refresh();
  1764. }
  1765. if (searchLedger) {
  1766. searchLedger.spread.refresh();
  1767. }
  1768. }
  1769. });
  1770. $.subMenu({
  1771. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1772. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1773. key: 'menu.1.0.0',
  1774. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1775. callback: function (info) {
  1776. if (info.mini) {
  1777. $('.panel-title').addClass('fluid');
  1778. $('#sub-menu').removeClass('panel-sidebar');
  1779. } else {
  1780. $('.panel-title').removeClass('fluid');
  1781. $('#sub-menu').addClass('panel-sidebar');
  1782. }
  1783. autoFlashHeight();
  1784. billsSpread.refresh();
  1785. if (posSpread) {
  1786. posSpread.refresh();
  1787. }
  1788. if (stdXmj) {
  1789. stdXmj.spread.refresh();
  1790. }
  1791. if (stdGcl) {
  1792. stdGcl.spread.refresh();
  1793. }
  1794. if (dealBills) {
  1795. dealBills.spread.refresh();
  1796. }
  1797. if (bgBills) {
  1798. bgBills.spread.refresh();
  1799. }
  1800. if (searchLedger) {
  1801. searchLedger.spread.refresh();
  1802. }
  1803. }
  1804. });
  1805. const showSideTools = function (show) {
  1806. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  1807. if (show) {
  1808. right.show();
  1809. autoFlashHeight();
  1810. /**
  1811. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  1812. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  1813. * 故需要通过最终的parent.width再计算一次left.width
  1814. *
  1815. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  1816. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  1817. *
  1818. */
  1819. //left.css('width', parent.width() - right.outerWidth());
  1820. //left.css('width', parent.width() - right.outerWidth());
  1821. const percent = 100 - right.outerWidth() /parent.width() * 100;
  1822. left.css('width', percent + '%');
  1823. } else {
  1824. left.width(parent.width());
  1825. right.hide();
  1826. }
  1827. };
  1828. $('#content-tab').addClass('active');
  1829. $('#xd-content').addClass('active');
  1830. showSideTools(true);
  1831. billsSpread.refresh();
  1832. if (posSpread) {
  1833. posSpread.refresh();
  1834. }
  1835. const stdLibCellDoubleClick = function (e, info) {
  1836. const stdSheet = info.sheet;
  1837. const mainSheet = billsSheet;
  1838. if (!stdSheet.zh_setting || !stdSheet.zh_tree || !mainSheet.zh_tree) { return; }
  1839. const stdTree = stdSheet.zh_tree;
  1840. const stdNode = stdTree.nodes[info.row];
  1841. const mainTree = mainSheet.zh_tree;
  1842. const sel = mainSheet.getSelections()[0];
  1843. const mainNode = mainTree.nodes[sel.row];
  1844. if (!stdNode) return;
  1845. if (info.sheet.zh_setting.stdType === 'gcl') {
  1846. if (mainNode.code && mainNode.code !== '' && !mainTree.isLeafXmj(mainNode)) {
  1847. toastr.warning('非最底层项目下,不应添加节点');
  1848. return;
  1849. }
  1850. }
  1851. postData(window.location.pathname + '/update', {
  1852. postType: 'add-std',
  1853. postData: {
  1854. id: mainTree.getNodeKey(mainNode),
  1855. tender_id: mainNode.tender_id,
  1856. stdType: info.sheet.zh_setting.stdType,
  1857. stdLibId: stdNode.list_id,
  1858. stdNode: stdTree.getNodeKey(stdNode)
  1859. }
  1860. }, function (result) {
  1861. const refreshNode = mainTree.loadPostData(result);
  1862. billsTreeSpreadObj.refreshTree(mainSheet, refreshNode);
  1863. if (sel) {
  1864. if (refreshNode.create && refreshNode.create.length > 0) {
  1865. mainSheet.setSelection(refreshNode.create[refreshNode.create.length - 1].index, sel.col, sel.rowCount, sel.colCount);
  1866. } else {
  1867. const node = _.find(mainTree.nodes, {code: stdNode.code, name: stdNode.name});
  1868. if (node) {
  1869. mainSheet.setSelection(mainTree.nodes.indexOf(node), sel.col, sel.rowCount, sel.colCount);
  1870. }
  1871. }
  1872. }
  1873. billsTreeSpreadObj.refreshOperationValid(mainSheet);
  1874. billsSpread.focus();
  1875. posSpreadObj.loadCurPosData();
  1876. });
  1877. };
  1878. const stdXmjSetting = {
  1879. selector: '#std-xmj',
  1880. stdType: 'xmj',
  1881. treeSetting: {
  1882. id: 'chapter_id',
  1883. pid: 'pid',
  1884. order: 'order',
  1885. level: 'level',
  1886. rootId: -1,
  1887. keys: ['id', 'list_id', 'chapter_id'],
  1888. },
  1889. spreadSetting: {
  1890. cols: [
  1891. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1892. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1893. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1894. ],
  1895. treeCol: 0,
  1896. emptyRows: 0,
  1897. headRows: 1,
  1898. headRowHeight: [32],
  1899. defaultRowHeight: 21,
  1900. headerFont: '12px 微软雅黑',
  1901. font: '12px 微软雅黑',
  1902. headColWidth: [0],
  1903. },
  1904. cellDoubleClick: stdLibCellDoubleClick,
  1905. page: 'revise',
  1906. tid: window.location.pathname.split('/')[2],
  1907. };
  1908. const stdGclSetting = {
  1909. selector: '#std-gcl',
  1910. stdType: 'gcl',
  1911. treeSetting: {
  1912. id: 'bill_id',
  1913. pid: 'pid',
  1914. order: 'order',
  1915. level: 'level',
  1916. rootId: -1,
  1917. keys: ['id', 'list_id', 'bill_id']
  1918. },
  1919. spreadSetting: {
  1920. cols: [
  1921. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1922. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1923. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true}
  1924. ],
  1925. treeCol: 0,
  1926. emptyRows: 0,
  1927. headRows: 1,
  1928. headRowHeight: [32],
  1929. defaultRowHeight: 21,
  1930. headerFont: '12px 微软雅黑',
  1931. font: '12px 微软雅黑',
  1932. headColWidth: [0],
  1933. },
  1934. cellDoubleClick: stdLibCellDoubleClick,
  1935. page: 'revise',
  1936. tid: window.location.pathname.split('/')[2],
  1937. };
  1938. // 展开收起标准节点
  1939. $('a', '#side-menu').bind('click', function (e) {
  1940. e.preventDefault();
  1941. const tab = $(this), tabPanel = $(tab.attr('content'));
  1942. // 展开工具栏、切换标签
  1943. if (!tab.hasClass('active')) {
  1944. const close = $('.active', '#side-menu').length === 0;
  1945. $('a', '#side-menu').removeClass('active');
  1946. tab.addClass('active');
  1947. $('.tab-content .tab-pane').removeClass('active');
  1948. tabPanel.addClass('active');
  1949. showSideTools(tab.hasClass('active'));
  1950. if (tab.attr('content') === '#std-xmj') {
  1951. if (!stdXmj) {
  1952. stdXmj = new stdLib(stdXmjSetting);
  1953. }
  1954. stdXmj.spread.refresh();
  1955. } else if (tab.attr('content') === '#std-gcl') {
  1956. if (!stdGcl) {
  1957. stdGcl = new stdLib(stdGclSetting);
  1958. }
  1959. stdGcl.spread.refresh();
  1960. } else if (tab.attr('content') === '#deal-bills') {
  1961. dealBills.loadData();
  1962. dealBills.spread.refresh();
  1963. } else if (tab.attr('content') === '#bg-bills') {
  1964. bgBills.loadData();
  1965. bgBills.spread.refresh();
  1966. } else if (tab.attr('content') === '#search' && !searchLedger) {
  1967. if (!searchLedger) {
  1968. searchLedger = $.billsSearch({
  1969. selector: '#search',
  1970. searchSpread: billsSpread,
  1971. resultSpreadSetting: {
  1972. cols: [
  1973. {title: '项目节编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1974. {title: '清单编号', field: 'b_code', hAlign: 0, width: 120, formatter: '@', readOnly: true},
  1975. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@', readOnly: true},
  1976. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@', readOnly: true},
  1977. {title: '单价', field: 'unit_price', hAlign: 2, width: 50, readOnly: true},
  1978. {title: '数量', field: 'quantity', hAlign: 2, width: 50, readOnly: true},
  1979. ],
  1980. emptyRows: 0,
  1981. headRows: 1,
  1982. headRowHeight: [32],
  1983. defaultRowHeight: 21,
  1984. headerFont: '12px 微软雅黑',
  1985. font: '12px 微软雅黑',
  1986. },
  1987. afterLocated: function () {
  1988. posSpreadObj.loadCurPosData();
  1989. }
  1990. });
  1991. }
  1992. searchLedger.spread.refresh();
  1993. }
  1994. }
  1995. else {// 收起工具栏
  1996. tab.removeClass('active');
  1997. tabPanel.removeClass('active');
  1998. showSideTools(tab.hasClass('active'));
  1999. }
  2000. billsSpread.refresh();
  2001. if (posSpread) {
  2002. posSpread.refresh();
  2003. }
  2004. });
  2005. if (!readOnly) {
  2006. // 修订详情 保存
  2007. $('#save').click(function () {
  2008. const content = $('#content').val();
  2009. postData('save', { content: content }, function () {
  2010. $('#content').attr('org-value', content);
  2011. });
  2012. });
  2013. }
  2014. // 显示层次
  2015. (function (select, sheet) {
  2016. $(select).click(function () {
  2017. if (!sheet.zh_tree) return;
  2018. const tag = $(this).attr('tag');
  2019. const tree = sheet.zh_tree;
  2020. switch (tag) {
  2021. case "1":
  2022. case "2":
  2023. case "3":
  2024. case "4":
  2025. case "5":
  2026. tree.expandByLevel(parseInt(tag));
  2027. SpreadJsObj.refreshTreeRowVisible(sheet);
  2028. break;
  2029. case "last":
  2030. tree.expandByCustom(() => { return true; });
  2031. SpreadJsObj.refreshTreeRowVisible(sheet);
  2032. break;
  2033. case "leafXmj":
  2034. tree.expandToLeafXmj();
  2035. SpreadJsObj.refreshTreeRowVisible(sheet);
  2036. break;
  2037. }
  2038. });
  2039. })('a[name=showLevel]', billsSheet);
  2040. });