phase_pay_detail.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. 'use strict';
  2. $(document).ready(() => {
  3. const payUtils = {
  4. tips: {
  5. name: function(data) {
  6. const tips = [];
  7. if (data) {
  8. if (data.pause) tips.push('当前项已停用');
  9. if (!data.is_yf) tips.push('当前项不参与本期应付计算');
  10. }
  11. return tips.join('<br/>');
  12. },
  13. range_tp: function (data) {
  14. if (!data || (!data.range_expr && !data.range_tp) || !data.dl_type) return '';
  15. if (data.dl_type === 1) {
  16. return '计提期限为(当 计量期数 ≥ ' + data.dl_count + ')';
  17. } else if (data.dl_type === 2) {
  18. switch (data.dl_tp_type) {
  19. case 'contract':
  20. return '计提期限为(累计合同计量 ≥ ' + data.dl_tp + ')';
  21. case 'qc':
  22. return '计提期限为(累计变更计量 ≥ ' + data.dl_tp + ')';
  23. case 'gather':
  24. return '计提期限为(累计完成计量 ≥ ' + data.dl_tp + ')';
  25. }
  26. }
  27. }
  28. },
  29. check: {
  30. isFixed: function(data) {
  31. return data.is_fixed;
  32. },
  33. isStarted: function (data) {
  34. return data.pre_used;
  35. },
  36. isYf: function(data) {
  37. return data.pay_type === 'bqyf';
  38. },
  39. isSf: function(data) {
  40. return data.pay_type === 'bqsf';
  41. },
  42. isGatherValid: function(data) {
  43. return !data.pay_type && (!data.children || data.children.length === 0);
  44. }
  45. }
  46. };
  47. const payCalc = (function (b, a) {
  48. class PayCalc {
  49. constructor (bases, add) {
  50. this.percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
  51. this.bases = bases;
  52. this.bases.sort(function (a, b) {
  53. return a.sort - b.sort;
  54. });
  55. for (const b of this.bases) {
  56. b.reg = new RegExp(b.code, 'igm');
  57. }
  58. this.addBase = add;
  59. this.orderReg = /f\d+/ig;
  60. this.nodeReg = /<<[a-z0-9\-]+>>/ig;
  61. }
  62. trans2OrderExpr(expr, payTree) {
  63. const nodeParam = expr.match(this.nodeReg);
  64. if (nodeParam) {
  65. for (const op of nodeParam) {
  66. const id = op.substring(2, op.length - 2);
  67. const payNode = payTree.nodes.find(x => { return x.uuid === id; });
  68. expr = expr.replace(op, payNode ? `f${payTree.getNodeIndex(payNode) + 1}` || '' : 0);
  69. }
  70. }
  71. return expr;
  72. }
  73. trans2NodeExpr(expr, payTree) {
  74. const orderParam = expr.match(this.orderReg);
  75. if (orderParam) {
  76. for (const op of orderParam) {
  77. const order = parseInt(op.substring(1, op.length));
  78. const payNode = payTree.nodes[order - 1];
  79. expr = expr.replace(op, payNode ? `<<${payNode.uuid}>>` || '' : 0);
  80. }
  81. }
  82. return expr;
  83. }
  84. checkExprValid(expr, invalidParam, selfId, payTree) {
  85. if (!expr) return [true, ''];
  86. const param = [];
  87. let num = '', base = '';
  88. let fixedIdParam;
  89. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  90. const subExpr = expr.substring(i, expr.length);
  91. if (/^[\d\.%]+/.test(expr[i])) {
  92. if (base !== '') {
  93. param.push({type: 'base', value: base});
  94. base = '';
  95. }
  96. num = num + expr[i];
  97. } else if (this.nodeReg.test(subExpr)) {
  98. if (num !== '') {
  99. param.push({type: 'num', value: num});
  100. num = '';
  101. }
  102. if (base !== '') {
  103. param.push({type: 'base', value: base});
  104. base = '';
  105. }
  106. // const node = this.nodeReg.exec(subExpr);
  107. const node = subExpr.match(this.nodeReg);
  108. param.push({type: 'node', value: node[0]});
  109. i = i + node[0].length - 1;
  110. } else if (/^[a-z]/.test(expr[i])) {
  111. if (num !== '') {
  112. param.push({type: 'num', value: num});
  113. num = '';
  114. }
  115. base = base + expr[i];
  116. } else if (expr[i] === '(') {
  117. if (num !== '') {
  118. param.push({type: 'num', value: num});
  119. num = '';
  120. }
  121. if (base !== '') {
  122. param.push({type: 'base', value: base});
  123. base = '';
  124. }
  125. param.push({type: 'left', value: '('});
  126. } else if (expr[i] === ')') {
  127. if (num !== '') {
  128. param.push({type: 'num', value: num});
  129. num = '';
  130. }
  131. if (base !== '') {
  132. param.push({type: 'base', value: base});
  133. base = '';
  134. }
  135. param.push({type: 'right', value: ')'});
  136. } else if (/^[\+\-*\/]/.test(expr[i])) {
  137. if (num !== '') {
  138. param.push({type: 'num', value: num});
  139. num = '';
  140. }
  141. if (base !== '') {
  142. param.push({type: 'base', value: base});
  143. base = '';
  144. }
  145. param.push({type: 'calc', value: expr[i]});
  146. } else {
  147. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  148. }
  149. }
  150. if (num !== '') {
  151. param.push({type: 'num', value: num});
  152. num = '';
  153. }
  154. if (base !== '') {
  155. param.push({type: 'base', value: base});
  156. base = '';
  157. }
  158. if (param.length === 0) return [true, ''];
  159. if (param.length > 1) {
  160. if (param[0].value === '-' && param[1].type === 'num') {
  161. param[1].value = '-' + param[1].value;
  162. param.shift();
  163. }
  164. }
  165. const iLen = param.length;
  166. let iLeftCount = 0, iRightCount = 0;
  167. for (const [i, p] of param.entries()) {
  168. if (p.type === 'calc') {
  169. if (i === 0 || i === iLen - 1)
  170. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  171. }
  172. if (p.type === 'num') {
  173. num = p.value.replace('%', '');
  174. if (p.value.length - num.length > 1)
  175. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  176. num = _.toNumber(num);
  177. if (num === undefined || num === null || _.isNaN(num))
  178. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  179. if (i > 0) {
  180. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  181. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  182. } else if (param[i - 1].value === '/' && num === 0) {
  183. return [false, '输入的表达式非法:请勿除0'];
  184. }
  185. }
  186. }
  187. if (p.type === 'base') {
  188. const baseParam = _.find(calcBase, {code: p.value});
  189. if (!baseParam)
  190. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  191. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  192. return [false, '不可使用计算基数' + p.value];
  193. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  194. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  195. }
  196. if (p.type === 'node') {
  197. if (!selfId) return [false, '输入的表达式错误:不支持行号引用'];
  198. if ([`<<${selfId}>>`].indexOf(p.value) >= 0) return [false, '输入的表达式非法:请勿引用自己'];
  199. if (!fixedIdParam) {
  200. fixedIdParam = payTree.nodes.filter(x => { return x.is_fixed; }).map(x => { return `<<${x.uuid}>>`});
  201. }
  202. if (fixedIdParam.indexOf(p.value) >= 0) return [false, '输入的表达式非法:请勿引用固定项'];
  203. }
  204. if (p.type === 'left') {
  205. iLeftCount += 1;
  206. if (i !== 0 && param[i-1].type !== 'calc')
  207. return [false, '输入的表达式非法:(前应有运算符'];
  208. }
  209. if (p.type === 'right') {
  210. iRightCount += 1;
  211. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  212. return [false, '输入的表达式非法:)后应有运算符'];
  213. if (iRightCount > iLeftCount)
  214. return [false, '输入的表达式非法:")"前无对应的"("'];
  215. }
  216. }
  217. if (iLeftCount > iRightCount)
  218. return [false, '输入的表达式非法:"("后无对应的")"'];
  219. if (selfId) {
  220. const circular = payCalc.checkCircularExpr(expr, selfId, payTree);
  221. // 当前循环计算不检查父项
  222. if (circular) return [false, '输入的表达式非法:循环引用'];
  223. }
  224. return [true, ''];
  225. }
  226. checkSfExpr(text, data, payNode, payTree) {
  227. if (text) {
  228. const num = _.toNumber(text);
  229. if (num) {
  230. data.expr = num;
  231. } else {
  232. const expr = this.trans2NodeExpr($.trim(text).replace('\t', '').replace('=', '').toLowerCase(), payTree);
  233. const [valid, msg] = this.checkExprValid(expr, [], payNode.uuid, payTree);
  234. if (!valid) return [valid, msg];
  235. data.expr = expr;
  236. }
  237. } else {
  238. data.tp = 0;
  239. data.expr = '';
  240. }
  241. return [true, ''];
  242. }
  243. checkExpr(text, data, payNode, payTree) {
  244. if (text) {
  245. const num = _.toNumber(text);
  246. if (num) {
  247. data.tp = num;
  248. data.expr = '';
  249. } else {
  250. const expr = this.trans2NodeExpr($.trim(text).replace('\t', '').replace('=', '').toLowerCase(), payTree);
  251. const [valid, msg] = this.checkExprValid(expr, ['bqyf'], payNode.uuid, payTree);
  252. if (!valid) return [valid, msg];
  253. data.expr = expr;
  254. data.tp = 0;
  255. }
  256. } else {
  257. data.tp = 0;
  258. data.expr = '';
  259. }
  260. return [true, ''];
  261. }
  262. checkRangeExpr(payNode, text, data) {
  263. if (!payNode) return [false, '数据错误'];
  264. const num = text ? _.toNumber(text) : 0;
  265. let expr = text ? (num ? '' : text) : '';
  266. expr = expr ? $.trim(expr).replace('\t', '').replace('=', '').toLowerCase() : '';
  267. const [valid, msg] = this.checkExprValid(expr, ['bqwc', 'ybbqwc', 'bqht', 'bqbg', 'bqyf']);
  268. if (!valid) return [valid, msg];
  269. if (payUtils.check.isStarted(payNode)) {
  270. if (payUtils.check.isSf(payNode)) {
  271. const value = expr ? payCalc.calculateExpr(expr) : num;
  272. if (payNode.pre_tp && value < payNode.pre_tp) return [false, '截止上期已计量' + payNode.pre_tp + ',扣款限额请勿少于改值'];
  273. data.range_tp = num;
  274. data.range_expr = expr;
  275. return [true, ''];
  276. } else {
  277. // if (payNode.pre_finish) return [false, '已达扣款限额,请勿修改'];
  278. // const value = expr ? payCalc.calculateExpr(expr) : num;
  279. // if (payNode.pre_tp && value < payNode.pre_tp) return [false, '截止上期已计量' + payNode.pre_tp + ',扣款限额请勿少于改值'];
  280. // data.rprice = num;
  281. // data.rexpr = expr;
  282. return [false, '已经开始使用,请勿修改扣款限额'];
  283. }
  284. } else {
  285. data.range_tp = num;
  286. data.range_expr = expr;
  287. return [true, ''];
  288. }
  289. }
  290. checkStartExpr(payNode, text, data) {
  291. if (!payNode) return [false, '数据错误'];
  292. const num = text ? _.toNumber(text) : 0;
  293. let expr = text ? (num ? '' : text) : '';
  294. expr = expr ? $.trim(expr).replace('\t', '').replace('=', '').toLowerCase() : '';
  295. const [valid, msg] = this.checkExprValid(expr, ['bqwc', 'ybbqwc', 'bqht', 'bqbg', 'bqyf']);
  296. if (!valid) return [valid, msg];
  297. if (payUtils.check.isStarted(payNode)) {
  298. return [false, '已经开始计量,请勿修改起扣金额'];
  299. } else {
  300. if (this.addBase.pre_gather_tp) {
  301. const value = expr ? payCalc.calculateExpr(expr) : num;
  302. if (this.addBase.pre_gather_tp && value < this.addBase.pre_gather_tp)
  303. return [false, '起扣金额请勿少于本期完成截止上期计量金额' + this.addBase.pre_gather_tp];
  304. data.start_tp = num;
  305. data.start_expr = expr;
  306. return [true, ''];
  307. } else {
  308. data.start_tp = num;
  309. data.start_expr = expr;
  310. return [true, ''];
  311. }
  312. }
  313. }
  314. getExprInfo(field, converse = false) {
  315. const exprField = [
  316. {qty: 'tp', expr: 'expr'},
  317. {qty: 'start_tp', expr: 'start_expr'},
  318. {qty: 'range_qty', expr: 'range_expr'},
  319. ];
  320. if (converse) return _.find(exprField, { expr: field });
  321. return _.find(exprField, {qty: field});
  322. }
  323. getLeafOrder(data, parentReg, tree) {
  324. if (!data) return [];
  325. const defaultResult = data.uuid ? [`<<${data.uuid}>>`] : [];
  326. if (!data.expr) return defaultResult;
  327. const nodeParam = data.expr.match(this.nodeReg);
  328. if (!nodeParam || nodeParam.length === 0) return defaultResult;
  329. const result = [];
  330. for (const op of nodeParam) {
  331. const id = op.substring(2, op.length - 2);
  332. if (data.uuid === id || op === parentReg) {
  333. result.push(op);
  334. } else {
  335. const payNode = tree.nodes.find(x => {return x.uuid === id; });
  336. const subOrderParam = this.getLeafOrder(payNode, data.uuid ? `<<${data.uuid}>>` : parentReg, tree);
  337. result.push(...subOrderParam);
  338. }
  339. }
  340. return result;
  341. }
  342. checkCircularExpr(expr, selfId, tree) {
  343. const leafOrder = this.getLeafOrder({expr}, `<<${selfId}>>`, tree);
  344. if (leafOrder.indexOf(`<<${selfId}>>`) >= 0) return true;
  345. return false;
  346. }
  347. calculateExpr(expr) {
  348. let formula = expr;
  349. for (const b of this.bases) {
  350. formula = formula.replace(b.reg, b.value);
  351. }
  352. const percent = formula.match(this.percentReg);
  353. if (percent) {
  354. for (const p of percent) {
  355. const v = math.evaluate(p.replace(new RegExp('%', 'gm'), '/100'));
  356. formula = formula.replace(p, v);
  357. }
  358. }
  359. try {
  360. const value = ZhCalc.mathCalcExpr(formula);
  361. return value;
  362. } catch(err) {
  363. return 0;
  364. }
  365. }
  366. }
  367. return new PayCalc(b, a);
  368. })(calcBase, addBase);
  369. const payObj = (function() {
  370. const spread = SpreadJsObj.createNewSpread($('#pay-spread')[0]);
  371. const sheet = spread.getActiveSheet();
  372. const spreadSetting = {
  373. cols: [
  374. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 230, formatter: '@', cellType: 'tree', getTip: payUtils.tips.name},
  375. {title: '本期金额(F)', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 100, type: 'Number'},
  376. {title: '截止上期金额', colSpan: '1', rowSpan: '1', field: 'pre_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
  377. {title: '截止本期金额', colSpan: '1', rowSpan: '1', field: 'end_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
  378. {title: '起扣金额', colSpan: '1', rowSpan: '1', field: 'start_tp', hAlign: 2, width: 100, type: 'Number'},
  379. {title: '付(扣)款限额', colSpan: '1', rowSpan: '1', field: 'range_tp', hAlign: 2, width: 100, cellType: 'tip', type: 'Number', getTip: payUtils.tips.range_tp},
  380. {title: '汇总', colSpan: '1', rowSpan: '1', field: 'is_gather', hAlign: 1, width: 60, cellType: 'signalCheckbox', show: payUtils.check.isGatherValid},
  381. {title: '附件', colSpan: '1', rowSpan: '1', field: 'attachment', hAlign: 0, width: 60, readOnly: true, cellType: 'imageBtn', normalImg: '#rela-file-icon', hoverImg: '#rela-file-hover', getValue: 'getValue.attachment'},
  382. {title: '本期批注', colSpan: '1', rowSpan: '1', field: 'postil', hAlign: 0, width: 120, formatter: '@', cellType: 'autoTip'},
  383. ],
  384. emptyRows: 0,
  385. headRows: 1,
  386. headRowHeight: [32],
  387. headColWidth: [30],
  388. defaultRowHeight: 21,
  389. headerFont: '12px 微软雅黑',
  390. font: '12px 微软雅黑',
  391. readOnly: readOnly,
  392. localCache: {
  393. key: 'phase-pay',
  394. colWidth: true,
  395. },
  396. pos: SpreadJsObj.getObjPos($('#pay-spread')[0]),
  397. };
  398. sjsSettingObj.setFxTreeStyle(spreadSetting, sjsSettingObj.FxTreeStyle.phasePay);
  399. SpreadJsObj.initSheet(sheet, spreadSetting);
  400. const payTree = createNewPathTree('base', {
  401. id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
  402. level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
  403. rootId: -1,
  404. });
  405. const payEvent = {
  406. refreshActn: function() {
  407. const setObjEnable = function (obj, enable) {
  408. if (enable) {
  409. obj.removeClass('disabled');
  410. } else {
  411. obj.addClass('disabled');
  412. }
  413. };
  414. const select = SpreadJsObj.getSelectObject(sheet);
  415. if (!select) {
  416. setObjEnable($('a[name=base-opr][type=add]'), false);
  417. setObjEnable($('a[name=base-opr][type=del]'), false);
  418. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  419. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  420. return;
  421. }
  422. const preNode = payTree.getPreSiblingNode(select);
  423. setObjEnable($('a[name=base-opr][type=add]'), !readOnly && !payUtils.check.isSf(select) && !payUtils.check.isYf(select));
  424. const delValid = !payUtils.check.isFixed(select) && !payUtils.check.isStarted(select);
  425. setObjEnable($('a[name=base-opr][type=del]'), !readOnly && delValid);
  426. setObjEnable($('a[name=base-opr][type=up-move]'), !readOnly && !payUtils.check.isFixed(select) && preNode);
  427. setObjEnable($('a[name=base-opr][type=down-move]'), !readOnly && !payUtils.check.isFixed(select) && !payTree.isLastSibling(select));
  428. },
  429. loadExprToInput: function() {
  430. const sel = sheet.getSelections()[0];
  431. const col = sheet.zh_setting.cols[sel.col];
  432. const data = SpreadJsObj.getSelectObject(this.sheet);
  433. if (data) {
  434. if (col.field === 'tp') {
  435. $('#expr').val(data.expr).attr('field', 'expr').attr('org', data.expr)
  436. .attr('readOnly', readOnly|| payCol.readOnly.tp(data));
  437. } else if (col.field === 'stage_tp') {
  438. $('#expr').val(data.start_expr).attr('field', 'start_expr').attr('org', data.start_expr)
  439. .attr('readOnly', readOnly|| payCol.readOnly.sprice(data) || payBase.isYF(data));
  440. } else if (col.field === 'rprice') {
  441. $('#expr').val(data.range_expr).attr('field', 'range_expr').attr('org', data.range_expr)
  442. .attr('readOnly', readOnly|| payCol.readOnly.rprice(data) || payBase.isYF(data));
  443. } else {
  444. $('#expr').val('').attr('readOnly', true);
  445. }
  446. $('#expr').attr('data-row', sel.row);
  447. } else {
  448. $('#expr').val('').attr('readOnly', true);
  449. $('#expr').removeAttr('data-row');
  450. }
  451. },
  452. refreshTree: function (data) {
  453. SpreadJsObj.massOperationSheet(sheet, function () {
  454. const tree = sheet.zh_tree;
  455. // 处理删除
  456. if (data.delete) {
  457. data.delete.sort(function (a, b) {
  458. return b.deleteIndex - a.deleteIndex;
  459. });
  460. for (const d of data.delete) {
  461. sheet.deleteRows(d.deleteIndex, 1);
  462. }
  463. }
  464. // 处理新增
  465. if (data.create) {
  466. const newNodes = data.create;
  467. if (newNodes) {
  468. newNodes.sort(function (a, b) {
  469. return a.index - b.index;
  470. });
  471. for (const node of newNodes) {
  472. sheet.addRows(node.index, 1);
  473. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  474. }
  475. }
  476. }
  477. // 处理更新
  478. if (data.update) {
  479. const rows = [];
  480. for (const u of data.update) {
  481. rows.push(tree.nodes.indexOf(u));
  482. }
  483. SpreadJsObj.reLoadRowsData(sheet, rows);
  484. }
  485. });
  486. },
  487. editStarting: function(e, info) {
  488. const col = info.sheet.zh_setting.cols[info.col];
  489. const select = SpreadJsObj.getSelectObject(info.sheet);
  490. switch (col.field) {
  491. case 'name':
  492. info.cancel = payUtils.check.isFixed(select);
  493. break;
  494. case 'tp':
  495. case 'is_gather':
  496. info.cancel = select.children && select.children.length > 0;
  497. break;
  498. case 'start_tp':
  499. case 'range_tp':
  500. info.cancel = (select.children && select.children.length > 0) || payUtils.check.isYf(select);
  501. break;
  502. case 'is_gather':
  503. info.cancel = true;
  504. break;
  505. }
  506. if (col.field === 'tp') {
  507. if (select.expr && select.expr !== '') {
  508. info.sheet.getCell(info.row, info.col).text(payCalc.trans2OrderExpr(select.expr, payTree));
  509. }
  510. } else if (col.field === 'start_tp') {
  511. if (select.start_expr && select.start_expr !== '') {
  512. info.sheet.getCell(info.row, info.col).text(select.start_expr);
  513. }
  514. } else if (col.field === 'range_tp') {
  515. if (select.range_expr && select.range_expr !== '') {
  516. info.sheet.getCell(info.row, info.col).text(select.range_expr);
  517. }
  518. }
  519. },
  520. editEnded: function(e, info) {
  521. if (!info.sheet.zh_setting) return;
  522. const select = SpreadJsObj.getSelectObject(info.sheet);
  523. const col = info.sheet.zh_setting.cols[info.col];
  524. if (col.field === 'is_gather') return;
  525. // 未改变值则不提交
  526. const validText = info.editingText ? info.editingText.replace('\n', '') : '';
  527. let orgValue;
  528. if (col.field === 'tp') {
  529. orgValue = select.expr ? payCalc.trans2OrderExpr(select.expr, payTree) : select.tp;
  530. } else if (col.field === 'start_tp') {
  531. orgValue = select.start_expr ? select.start_expr : select.start_tp;
  532. } else if (col.field === 'range_tp') {
  533. orgValue = select.range_expr ? select.range_expr : select.range_tp;
  534. } else {
  535. orgValue = select[col.field];
  536. }
  537. orgValue = orgValue || '';
  538. if (orgValue == validText) {
  539. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  540. return;
  541. }
  542. const data = { postType: 'update', postData: { id: select.id } };
  543. switch(col.field) {
  544. case 'tp':
  545. const [tpValid, tpMsg] = payUtils.check.isSf(select)
  546. ? payCalc.checkSfExpr(validText, data.postData, select, payTree)
  547. : payCalc.checkExpr(validText, data.postData, select, payTree);
  548. if (!tpValid) {
  549. toastr.warning(tpMsg);
  550. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  551. return;
  552. }
  553. break;
  554. case 'start_tp':
  555. const [sValid, sMsg] = payCalc.checkStartExpr(select, validText, data.postData);
  556. if (!sValid) {
  557. toastr.warning(sMsg);
  558. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  559. return;
  560. }
  561. break;
  562. case 'range_tp':
  563. const [rValid, rMsg] = payCalc.checkRangeExpr(select, validText, data.postData);
  564. if (!rValid) {
  565. toastr.warning(rMsg);
  566. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  567. return;
  568. }
  569. break;
  570. default:
  571. if (col.type === 'Number') {
  572. data.postData[col.field] = _.toNumber(validText) || 0;
  573. } else {
  574. data.postData[col.field] = validText || '';
  575. }
  576. break;
  577. }
  578. postData('update', data, function (result) {
  579. if (result.reload) {
  580. payEvent.reloadPays(result.reload);
  581. } else {
  582. const refreshData = payTree.loadPostData(result);
  583. payEvent.refreshTree(refreshData);
  584. }
  585. }, function () {
  586. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  587. });
  588. },
  589. selectionChanged: function(e, info) {
  590. if (info.newSelections) {
  591. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  592. payEvent.refreshActn();
  593. }
  594. }
  595. payEvent.loadExprToInput();
  596. },
  597. buttonClicked: function (e, info) {
  598. if (!info.sheet.zh_setting) return;
  599. const select = SpreadJsObj.getSelectObject(info.sheet);
  600. const col = info.sheet.zh_setting.cols[info.col];
  601. if (col.field !== 'is_gather') return;
  602. if (!payUtils.check.isGatherValid(select)) return;
  603. if (info.sheet.isEditing()) info.sheet.endEdit(true);
  604. const data = {
  605. postType: 'update',
  606. postData: { id: select.id },
  607. };
  608. data.postData[col.field] = info.sheet.getValue(info.row, info.col) || 0;
  609. // 更新至服务器
  610. postData('update', data, function (result) {
  611. if (result.reload) payEvent.reloadPays(result.reload);
  612. });
  613. },
  614. deletePress: function (sheet) {
  615. if (!sheet.zh_setting) return;
  616. const sel = sheet.getSelections()[0];
  617. if (!sel) return;
  618. const col = sheet.zh_setting.cols[sel.col];
  619. if (col.readOnly === true) return;
  620. if (sel.colCount > 1) toastr.warning('请勿同时删除多列数据');
  621. const data = { postType: 'update', postData: [] };
  622. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow ++) {
  623. const node = sheet.zh_tree.nodes[iRow];
  624. if (node && (!node.pay_type || col.field === 'postil')) {
  625. const updateData = { id: node.id };
  626. switch(col.field) {
  627. case 'tp':
  628. updateData.tp = 0;
  629. updateData.expr = '';
  630. break;
  631. case 'start_tp':
  632. updateData.start_tp = 0;
  633. updateData.start_expr = '';
  634. break;
  635. case 'range_tp':
  636. updateData.range_tp = 0;
  637. updateData.range_expr = '';
  638. break;
  639. case 'is_gather':
  640. updateData.is_gather = 0;
  641. break;
  642. default:
  643. updateData[col.field] = col.type === 'Number' ? 0 : '';
  644. }
  645. data.postData.push(updateData);
  646. }
  647. }
  648. postData('update', data, function (result) {
  649. if (result.reload) {
  650. payEvent.reloadPays(result.reload);
  651. } else {
  652. const refreshData = payTree.loadPostData(result);
  653. payEvent.refreshTree(refreshData);
  654. }
  655. }, function () {
  656. SpreadJsObj.reLoadRowData(sheet, sel.row, sel.rowCount);
  657. });
  658. },
  659. baseOpr: function(type) {
  660. const self = this;
  661. const node = SpreadJsObj.getSelectObject(sheet);
  662. if (type === 'delete') {
  663. postData('update', { postType: 'delete', postData: { id: node.tree_id }}, function(result) {
  664. payEvent.reloadPays(result.reload);
  665. });
  666. } else {
  667. postData('update', { postType: type, postData: { id: node.tree_id }}, function (result) {
  668. const refreshData = payTree.loadPostData(result);
  669. payEvent.refreshTree(refreshData);
  670. const sel = sheet.getSelections()[0];
  671. if (sel) {
  672. if (['up-move', 'down-move'].indexOf(type) > -1) {
  673. sheet.setSelection(payTree.getNodeIndex(node), sel.col, sel.rowCount, sel.colCount);
  674. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(node)]);
  675. } else if (type === 'add') {
  676. sheet.setSelection(payTree.getNodeIndex(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  677. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(refreshData.create[0])]);
  678. }
  679. }
  680. self.refreshActn(sheet);
  681. });
  682. }
  683. },
  684. reloadPays: function(datas){
  685. payTree.loadDatas(datas);
  686. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Tree, payTree);
  687. payEvent.refreshActn();
  688. }
  689. };
  690. spread.bind(spreadNS.Events.SelectionChanged, payEvent.selectionChanged);
  691. if (!readOnly) {
  692. spread.bind(spreadNS.Events.EditStarting, payEvent.editStarting);
  693. spread.bind(spreadNS.Events.EditEnded, payEvent.editEnded);
  694. spread.bind(spreadNS.Events.ButtonClicked, payEvent.buttonClicked);
  695. SpreadJsObj.addDeleteBind(spread, payEvent.deletePress);
  696. $('a[name="base-opr"]').click(function () {
  697. payEvent.baseOpr(this.getAttribute('type'));
  698. });
  699. $('')
  700. }
  701. return { spread, sheet, payTree, loadDatas: payEvent.reloadPays }
  702. })();
  703. payObj.loadDatas(details);
  704. // todo 加载审批列表
  705. $.subMenu({
  706. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  707. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  708. key: 'menu.1.0.0',
  709. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  710. callback: function (info) {
  711. if (info.mini) {
  712. $('.panel-title').addClass('fluid');
  713. $('#sub-menu').removeClass('panel-sidebar');
  714. } else {
  715. $('.panel-title').removeClass('fluid');
  716. $('#sub-menu').addClass('panel-sidebar');
  717. }
  718. autoFlashHeight();
  719. payObj.spread.refresh();
  720. }
  721. });
  722. });