phase_pay_detail.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. 'use strict';
  2. const showSideTools = function (show) {
  3. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  4. if (show) {
  5. right.show();
  6. autoFlashHeight();
  7. /**
  8. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  9. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  10. * 故需要通过最终的parent.width再计算一次left.width
  11. *
  12. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  13. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  14. *
  15. */
  16. //left.css('width', parent.width() - right.outerWidth());
  17. //left.css('width', parent.width() - right.outerWidth());
  18. const percent = 100 - right.outerWidth() /parent.width() * 100;
  19. left.css('width', percent + '%');
  20. } else {
  21. left.width(parent.width());
  22. right.hide();
  23. }
  24. };
  25. $(document).ready(() => {
  26. autoFlashHeight();
  27. const payUtils = {
  28. tips: {
  29. name: function(data) {
  30. const tips = [];
  31. if (data) {
  32. if (data.pause) tips.push('当前项已停用');
  33. if (!data.is_yf) tips.push('当前项不参与本期应付计算');
  34. }
  35. return tips.join('<br/>');
  36. },
  37. range_tp: function (data) {
  38. if (!data || (!data.range_expr && !data.range_tp) || !data.dl_type) return '';
  39. if (data.dl_type === 1) {
  40. return '计提期限为(当 计量期数 ≥ ' + data.dl_count + ')';
  41. } else if (data.dl_type === 2) {
  42. switch (data.dl_tp_type) {
  43. case 'contract':
  44. return '计提期限为(累计合同计量 ≥ ' + data.dl_tp + ')';
  45. case 'qc':
  46. return '计提期限为(累计变更计量 ≥ ' + data.dl_tp + ')';
  47. case 'gather':
  48. return '计提期限为(累计完成计量 ≥ ' + data.dl_tp + ')';
  49. }
  50. }
  51. }
  52. },
  53. check: {
  54. isFixed: function(data) {
  55. return data.is_fixed;
  56. },
  57. isStarted: function (data) {
  58. return data.pre_used;
  59. },
  60. isYf: function(data) {
  61. return data.pay_type === 'bqyf';
  62. },
  63. isSf: function(data) {
  64. return data.pay_type === 'bqsf';
  65. },
  66. isGatherValid: function(data) {
  67. return !data.pay_type && (!data.children || data.children.length === 0);
  68. },
  69. isOwner: function(data) {
  70. return data.create_user_id === userID;
  71. },
  72. isFinish: function(data) {
  73. return data.pre_finish;
  74. },
  75. isYB: function() {
  76. return userID === phasePay.create_user_id;
  77. },
  78. isOld: function(data) {
  79. return data.phase_id !== data.create_phase_id;
  80. },
  81. isLock: function (data) {
  82. const result = !!lockPayExpr && payUtils.check.isStarted(data) && payCalc.hasBase(data.expr);
  83. return result;
  84. },
  85. tpReadOnly: function(data) {
  86. return payUtils.check.isYf(data) || payUtils.check.isLock(data);
  87. },
  88. startTpReadOnly: function(data) {
  89. if (payUtils.check.isOld(data)) {
  90. return payUtils.check.isStarted(data) || !payUtils.check.isYB(data) || payUtils.check.isLock(data);
  91. } else {
  92. return payUtils.check.isWC(data) || payUtils.check.isSF(data) || payUtils.check.isYf(data) || !(payUtils.check.isOwner(data) || payUtils.check.isYB());
  93. }
  94. }
  95. },
  96. menuVisible: {
  97. pause: function (data) {
  98. if (payUtils.check.isOld(data)) {
  99. return payUtils.check.isYB();
  100. } else {
  101. return payUtils.check.isOwner(data) || payUtils.check.isYB();
  102. }
  103. },
  104. deadline: function (data) {
  105. if (payUtils.check.isOld(data)) {
  106. return !payUtils.check.isFinish(data) && payUtils.check.isYB();
  107. } else {
  108. return payUtils.check.isOwner(data) || payUtils.check.isYB();
  109. }
  110. }
  111. },
  112. };
  113. const payCalc = (function (b, a) {
  114. class PayCalc {
  115. constructor (bases, add) {
  116. this.percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
  117. this.bases = bases;
  118. this.bases.sort(function (a, b) {
  119. return a.sort - b.sort;
  120. });
  121. for (const b of this.bases) {
  122. b.reg = new RegExp(b.code, 'igm');
  123. }
  124. this.addBase = add;
  125. this.orderReg = /f\d+/ig;
  126. this.nodeReg = /<<[a-z0-9\-]+>>/ig;
  127. }
  128. hasBase(expr) {
  129. if (!expr) return false;
  130. for (const b of this.bases) {
  131. if (data.expr.indexOf(b.code) >= 0) return true;
  132. }
  133. return false;
  134. }
  135. trans2OrderExpr(expr, payTree) {
  136. const nodeParam = expr.match(this.nodeReg);
  137. if (nodeParam) {
  138. for (const op of nodeParam) {
  139. const id = op.substring(2, op.length - 2);
  140. const payNode = payTree.nodes.find(x => { return x.uuid === id; });
  141. expr = expr.replace(op, payNode ? `f${payTree.getNodeIndex(payNode) + 1}` || '' : 0);
  142. }
  143. }
  144. return expr;
  145. }
  146. trans2NodeExpr(expr, payTree) {
  147. const orderParam = expr.match(this.orderReg);
  148. if (orderParam) {
  149. for (const op of orderParam) {
  150. const order = parseInt(op.substring(1, op.length));
  151. const payNode = payTree.nodes[order - 1];
  152. expr = expr.replace(op, payNode ? `<<${payNode.uuid}>>` || '' : 0);
  153. }
  154. }
  155. return expr;
  156. }
  157. checkExprValid(expr, invalidParam, selfId, payTree) {
  158. if (!expr) return [true, ''];
  159. const param = [];
  160. let num = '', base = '';
  161. let fixedIdParam;
  162. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  163. const subExpr = expr.substring(i, expr.length);
  164. if (/^[\d\.%]+/.test(expr[i])) {
  165. if (base !== '') {
  166. param.push({type: 'base', value: base});
  167. base = '';
  168. }
  169. num = num + expr[i];
  170. } else if (this.nodeReg.test(subExpr)) {
  171. if (num !== '') {
  172. param.push({type: 'num', value: num});
  173. num = '';
  174. }
  175. if (base !== '') {
  176. param.push({type: 'base', value: base});
  177. base = '';
  178. }
  179. // const node = this.nodeReg.exec(subExpr);
  180. const node = subExpr.match(this.nodeReg);
  181. param.push({type: 'node', value: node[0]});
  182. i = i + node[0].length - 1;
  183. } else if (/^[a-z]/.test(expr[i])) {
  184. if (num !== '') {
  185. param.push({type: 'num', value: num});
  186. num = '';
  187. }
  188. base = base + expr[i];
  189. } else if (expr[i] === '(') {
  190. if (num !== '') {
  191. param.push({type: 'num', value: num});
  192. num = '';
  193. }
  194. if (base !== '') {
  195. param.push({type: 'base', value: base});
  196. base = '';
  197. }
  198. param.push({type: 'left', value: '('});
  199. } else if (expr[i] === ')') {
  200. if (num !== '') {
  201. param.push({type: 'num', value: num});
  202. num = '';
  203. }
  204. if (base !== '') {
  205. param.push({type: 'base', value: base});
  206. base = '';
  207. }
  208. param.push({type: 'right', value: ')'});
  209. } else if (/^[\+\-*\/]/.test(expr[i])) {
  210. if (num !== '') {
  211. param.push({type: 'num', value: num});
  212. num = '';
  213. }
  214. if (base !== '') {
  215. param.push({type: 'base', value: base});
  216. base = '';
  217. }
  218. param.push({type: 'calc', value: expr[i]});
  219. } else {
  220. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  221. }
  222. }
  223. if (num !== '') {
  224. param.push({type: 'num', value: num});
  225. num = '';
  226. }
  227. if (base !== '') {
  228. param.push({type: 'base', value: base});
  229. base = '';
  230. }
  231. if (param.length === 0) return [true, ''];
  232. if (param.length > 1) {
  233. if (param[0].value === '-' && param[1].type === 'num') {
  234. param[1].value = '-' + param[1].value;
  235. param.shift();
  236. }
  237. }
  238. const iLen = param.length;
  239. let iLeftCount = 0, iRightCount = 0;
  240. for (const [i, p] of param.entries()) {
  241. if (p.type === 'calc') {
  242. if (i === 0 || i === iLen - 1)
  243. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  244. }
  245. if (p.type === 'num') {
  246. num = p.value.replace('%', '');
  247. if (p.value.length - num.length > 1)
  248. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  249. num = _.toNumber(num);
  250. if (num === undefined || num === null || _.isNaN(num))
  251. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  252. if (i > 0) {
  253. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  254. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  255. } else if (param[i - 1].value === '/' && num === 0) {
  256. return [false, '输入的表达式非法:请勿除0'];
  257. }
  258. }
  259. }
  260. if (p.type === 'base') {
  261. const baseParam = _.find(calcBase, {code: p.value});
  262. if (!baseParam)
  263. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  264. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  265. return [false, '不可使用计算基数' + p.value];
  266. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  267. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  268. }
  269. if (p.type === 'node') {
  270. if (!selfId) return [false, '输入的表达式错误:不支持行号引用'];
  271. if ([`<<${selfId}>>`].indexOf(p.value) >= 0) return [false, '输入的表达式非法:请勿引用自己'];
  272. if (!fixedIdParam) {
  273. fixedIdParam = payTree.nodes.filter(x => { return x.is_fixed; }).map(x => { return `<<${x.uuid}>>`});
  274. }
  275. if (fixedIdParam.indexOf(p.value) >= 0) return [false, '输入的表达式非法:请勿引用固定项'];
  276. }
  277. if (p.type === 'left') {
  278. iLeftCount += 1;
  279. if (i !== 0 && param[i-1].type !== 'calc')
  280. return [false, '输入的表达式非法:(前应有运算符'];
  281. }
  282. if (p.type === 'right') {
  283. iRightCount += 1;
  284. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  285. return [false, '输入的表达式非法:)后应有运算符'];
  286. if (iRightCount > iLeftCount)
  287. return [false, '输入的表达式非法:")"前无对应的"("'];
  288. }
  289. }
  290. if (iLeftCount > iRightCount)
  291. return [false, '输入的表达式非法:"("后无对应的")"'];
  292. if (selfId) {
  293. const circular = payCalc.checkCircularExpr(expr, selfId, payTree);
  294. // 当前循环计算不检查父项
  295. if (circular) return [false, '输入的表达式非法:循环引用'];
  296. }
  297. return [true, ''];
  298. }
  299. checkSfExpr(text, data, payNode, payTree) {
  300. if (text) {
  301. const num = _.toNumber(text);
  302. if (num) {
  303. data.expr = num;
  304. } else {
  305. const expr = this.trans2NodeExpr($.trim(text).replace('\t', '').replace('=', '').toLowerCase(), payTree);
  306. const [valid, msg] = this.checkExprValid(expr, [], payNode.uuid, payTree);
  307. if (!valid) return [valid, msg];
  308. data.expr = expr;
  309. }
  310. } else {
  311. data.tp = 0;
  312. data.expr = '';
  313. }
  314. return [true, ''];
  315. }
  316. checkExpr(text, data, payNode, payTree) {
  317. if (text) {
  318. const num = _.toNumber(text);
  319. if (num) {
  320. data.tp = num;
  321. data.expr = '';
  322. } else {
  323. const expr = this.trans2NodeExpr($.trim(text).replace('\t', '').replace('=', '').toLowerCase(), payTree);
  324. const [valid, msg] = this.checkExprValid(expr, ['bqyf'], payNode.uuid, payTree);
  325. if (!valid) return [valid, msg];
  326. data.expr = expr;
  327. data.tp = 0;
  328. }
  329. } else {
  330. data.tp = 0;
  331. data.expr = '';
  332. }
  333. return [true, ''];
  334. }
  335. checkRangeExpr(payNode, text, data) {
  336. if (!payNode) return [false, '数据错误'];
  337. const num = text ? _.toNumber(text) : 0;
  338. let expr = text ? (num ? '' : text) : '';
  339. expr = expr ? $.trim(expr).replace('\t', '').replace('=', '').toLowerCase() : '';
  340. const [valid, msg] = this.checkExprValid(expr, ['bqwc', 'ybbqwc', 'bqht', 'bqbg', 'bqyf']);
  341. if (!valid) return [valid, msg];
  342. if (payUtils.check.isStarted(payNode)) {
  343. if (payUtils.check.isSf(payNode)) {
  344. const value = expr ? payCalc.calculateExpr(expr) : num;
  345. if (payNode.pre_tp && value < payNode.pre_tp) return [false, '截止上期已计量' + payNode.pre_tp + ',扣款限额请勿少于改值'];
  346. data.range_tp = num;
  347. data.range_expr = expr;
  348. return [true, ''];
  349. } else {
  350. // if (payNode.pre_finish) return [false, '已达扣款限额,请勿修改'];
  351. // const value = expr ? payCalc.calculateExpr(expr) : num;
  352. // if (payNode.pre_tp && value < payNode.pre_tp) return [false, '截止上期已计量' + payNode.pre_tp + ',扣款限额请勿少于改值'];
  353. // data.range_tp = num;
  354. // data.range_expr = expr;
  355. return [false, '已经开始使用,请勿修改扣款限额'];
  356. }
  357. } else {
  358. data.range_tp = num;
  359. data.range_expr = expr;
  360. return [true, ''];
  361. }
  362. }
  363. checkStartExpr(payNode, text, data) {
  364. if (!payNode) return [false, '数据错误'];
  365. const num = text ? _.toNumber(text) : 0;
  366. let expr = text ? (num ? '' : text) : '';
  367. expr = expr ? $.trim(expr).replace('\t', '').replace('=', '').toLowerCase() : '';
  368. const [valid, msg] = this.checkExprValid(expr, ['bqwc', 'ybbqwc', 'bqht', 'bqbg', 'bqyf']);
  369. if (!valid) return [valid, msg];
  370. if (payUtils.check.isStarted(payNode)) {
  371. return [false, '已经开始计量,请勿修改起扣金额'];
  372. } else {
  373. if (this.addBase.pre_gather_tp) {
  374. const value = expr ? payCalc.calculateExpr(expr) : num;
  375. if (this.addBase.pre_gather_tp && value < this.addBase.pre_gather_tp)
  376. return [false, '起扣金额请勿少于本期完成截止上期计量金额' + this.addBase.pre_gather_tp];
  377. data.start_tp = num;
  378. data.start_expr = expr;
  379. return [true, ''];
  380. } else {
  381. data.start_tp = num;
  382. data.start_expr = expr;
  383. return [true, ''];
  384. }
  385. }
  386. }
  387. getExprInfo(field, converse = false) {
  388. const exprField = [
  389. {qty: 'tp', expr: 'expr'},
  390. {qty: 'start_tp', expr: 'start_expr'},
  391. {qty: 'range_qty', expr: 'range_expr'},
  392. ];
  393. if (converse) return _.find(exprField, { expr: field });
  394. return _.find(exprField, {qty: field});
  395. }
  396. getLeafOrder(data, parentReg, tree) {
  397. if (!data) return [];
  398. const defaultResult = data.uuid ? [`<<${data.uuid}>>`] : [];
  399. if (!data.expr) return defaultResult;
  400. const nodeParam = data.expr.match(this.nodeReg);
  401. if (!nodeParam || nodeParam.length === 0) return defaultResult;
  402. const result = [];
  403. for (const op of nodeParam) {
  404. const id = op.substring(2, op.length - 2);
  405. if (data.uuid === id || op === parentReg) {
  406. result.push(op);
  407. } else {
  408. const payNode = tree.nodes.find(x => {return x.uuid === id; });
  409. const subOrderParam = this.getLeafOrder(payNode, data.uuid ? `<<${data.uuid}>>` : parentReg, tree);
  410. result.push(...subOrderParam);
  411. }
  412. }
  413. return result;
  414. }
  415. checkCircularExpr(expr, selfId, tree) {
  416. const leafOrder = this.getLeafOrder({expr}, `<<${selfId}>>`, tree);
  417. if (leafOrder.indexOf(`<<${selfId}>>`) >= 0) return true;
  418. return false;
  419. }
  420. calculateExpr(expr) {
  421. let formula = expr;
  422. for (const b of this.bases) {
  423. formula = formula.replace(b.reg, b.value);
  424. }
  425. const percent = formula.match(this.percentReg);
  426. if (percent) {
  427. for (const p of percent) {
  428. const v = math.evaluate(p.replace(new RegExp('%', 'gm'), '/100'));
  429. formula = formula.replace(p, v);
  430. }
  431. }
  432. try {
  433. const value = ZhCalc.mathCalcExpr(formula);
  434. return value;
  435. } catch(err) {
  436. return 0;
  437. }
  438. }
  439. refreshBaseHtml() {
  440. const html = [];
  441. for (const [i, b] of this.bases.entries()) {
  442. html.push('<tr>');
  443. html.push(`<td>${i+1}</td><td>${b.name}</td><td>${b.code}</td>`);
  444. if (b.code === 'bqyf') {
  445. html.push('<td class="text-right">--</td>');
  446. } else {
  447. html.push(`<td class="text-right">${b.formatValue}</td>`);
  448. }
  449. html.push('</tr>');
  450. }
  451. $('#base-list').html(html.join(''));
  452. }
  453. reloadBase(bases, add) {
  454. this.bases = bases;
  455. this.refreshBaseHtml();
  456. this.bases.sort(function (a, b) {
  457. return a.sort - b.sort;
  458. });
  459. for (const b of this.bases) {
  460. b.reg = new RegExp(b.code, 'igm');
  461. }
  462. this.addBase = add;
  463. }
  464. }
  465. return new PayCalc(b, a);
  466. })(calcBase, addBase);
  467. const payObj = (function() {
  468. const spread = SpreadJsObj.createNewSpread($('#pay-spread')[0]);
  469. const sheet = spread.getActiveSheet();
  470. const spreadSetting = {
  471. cols: [
  472. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 230, formatter: '@', cellType: 'tree', getTip: payUtils.tips.name},
  473. {title: '本期金额(F)', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 100, type: 'Number'},
  474. {title: '截止上期金额', colSpan: '1', rowSpan: '1', field: 'pre_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
  475. {title: '截止本期金额', colSpan: '1', rowSpan: '1', field: 'end_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
  476. {title: '起扣金额', colSpan: '1', rowSpan: '1', field: 'start_tp', hAlign: 2, width: 100, type: 'Number'},
  477. {title: '付(扣)款限额', colSpan: '1', rowSpan: '1', field: 'range_tp', hAlign: 2, width: 100, cellType: 'tip', type: 'Number', getTip: payUtils.tips.range_tp},
  478. {title: '汇总', colSpan: '1', rowSpan: '1', field: 'is_gather', hAlign: 1, width: 60, cellType: 'signalCheckbox', show: payUtils.check.isGatherValid},
  479. {title: '本期批注', colSpan: '1', rowSpan: '1', field: 'postil', hAlign: 0, width: 120, formatter: '@', cellType: 'autoTip'},
  480. ],
  481. emptyRows: 0,
  482. headRows: 1,
  483. headRowHeight: [32],
  484. headColWidth: [30],
  485. defaultRowHeight: 21,
  486. headerFont: '12px 微软雅黑',
  487. font: '12px 微软雅黑',
  488. readOnly: readOnly,
  489. localCache: {
  490. key: 'phase-pay',
  491. colWidth: true,
  492. },
  493. pos: SpreadJsObj.getObjPos($('#pay-spread')[0]),
  494. };
  495. sjsSettingObj.setFxTreeStyle(spreadSetting, sjsSettingObj.FxTreeStyle.phasePay);
  496. SpreadJsObj.initSheet(sheet, spreadSetting);
  497. const payTree = createNewPathTree('base', {
  498. id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
  499. level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
  500. rootId: -1,
  501. });
  502. const payEvent = {
  503. refreshActn: function() {
  504. const setObjEnable = function (obj, enable) {
  505. if (enable) {
  506. obj.removeClass('disabled');
  507. } else {
  508. obj.addClass('disabled');
  509. }
  510. };
  511. const select = SpreadJsObj.getSelectObject(sheet);
  512. if (!select) {
  513. setObjEnable($('a[name=base-opr][type=add]'), false);
  514. setObjEnable($('a[name=base-opr][type=del]'), false);
  515. setObjEnable($('a[name=base-opr][type=up-move]'), false);
  516. setObjEnable($('a[name=base-opr][type=down-move]'), false);
  517. return;
  518. }
  519. const preNode = payTree.getPreSiblingNode(select);
  520. setObjEnable($('a[name=base-opr][type=add]'), !readOnly && !payUtils.check.isSf(select) && !payUtils.check.isYf(select));
  521. const delValid = !payUtils.check.isFixed(select) && !payUtils.check.isStarted(select);
  522. setObjEnable($('a[name=base-opr][type=delete]'), !readOnly && delValid);
  523. setObjEnable($('a[name=base-opr][type=up-move]'), !readOnly && !payUtils.check.isFixed(select) && preNode);
  524. setObjEnable($('a[name=base-opr][type=down-move]'), !readOnly && !payUtils.check.isFixed(select) && !payTree.isLastSibling(select));
  525. },
  526. loadExprToInput: function() {
  527. const sel = sheet.getSelections()[0];
  528. const col = sheet.zh_setting.cols[sel.col];
  529. const data = SpreadJsObj.getSelectObject(sheet);
  530. if (data && (!data.children || data.children.length === 0)) {
  531. if (col.field === 'tp') {
  532. const expr = payCalc.trans2OrderExpr(data.expr, payTree);
  533. $('#pay-expr').val(expr).attr('field', 'expr').attr('org', expr)
  534. .attr('readOnly', readOnly|| payUtils.check.tpReadOnly(data));
  535. } else if (col.field === 'start_tp') {
  536. const expr = payCalc.trans2OrderExpr(data.start_expr, payTree) || data.start_tp;
  537. $('#pay-expr').val(expr).attr('field', 'start_expr').attr('org', expr)
  538. .attr('readOnly', readOnly|| payUtils.check.startTpReadOnly(data) || payUtils.check.isYf(data));
  539. } else if (col.field === 'range_tp') {
  540. const expr = payCalc.trans2OrderExpr(data.range_expr, payTree);
  541. $('#pay-expr').val(expr).attr('field', 'range_expr').attr('org', expr)
  542. .attr('readOnly', readOnly|| payUtils.check.rangeTpReadOnly(data) || payUtils.check.isYf(data));
  543. } else {
  544. $('#pay-expr').val('').attr('readOnly', true);
  545. }
  546. $('#pay-expr').attr('data-row', sel.row);
  547. } else {
  548. $('#pay-expr').val('').attr('readOnly', true);
  549. $('#pay-expr').removeAttr('data-row');
  550. }
  551. },
  552. refreshTree: function (data) {
  553. SpreadJsObj.massOperationSheet(sheet, function () {
  554. const tree = sheet.zh_tree;
  555. // 处理删除
  556. if (data.delete) {
  557. data.delete.sort(function (a, b) {
  558. return b.deleteIndex - a.deleteIndex;
  559. });
  560. for (const d of data.delete) {
  561. sheet.deleteRows(d.deleteIndex, 1);
  562. }
  563. }
  564. // 处理新增
  565. if (data.create) {
  566. const newNodes = data.create;
  567. if (newNodes) {
  568. newNodes.sort(function (a, b) {
  569. return a.index - b.index;
  570. });
  571. for (const node of newNodes) {
  572. sheet.addRows(node.index, 1);
  573. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  574. }
  575. }
  576. }
  577. // 处理更新
  578. if (data.update) {
  579. const rows = [];
  580. for (const u of data.update) {
  581. rows.push(tree.nodes.indexOf(u));
  582. }
  583. SpreadJsObj.reLoadRowsData(sheet, rows);
  584. }
  585. });
  586. },
  587. editStarting: function(e, info) {
  588. const col = info.sheet.zh_setting.cols[info.col];
  589. const select = SpreadJsObj.getSelectObject(info.sheet);
  590. switch (col.field) {
  591. case 'name':
  592. info.cancel = payUtils.check.isFixed(select);
  593. break;
  594. case 'tp':
  595. case 'is_gather':
  596. info.cancel = select.children && select.children.length > 0;
  597. break;
  598. case 'start_tp':
  599. case 'range_tp':
  600. info.cancel = (select.children && select.children.length > 0) || payUtils.check.isYf(select);
  601. break;
  602. case 'is_gather':
  603. info.cancel = true;
  604. break;
  605. }
  606. if (col.field === 'tp') {
  607. if (select.expr && select.expr !== '') {
  608. info.sheet.getCell(info.row, info.col).text(payCalc.trans2OrderExpr(select.expr, payTree));
  609. }
  610. } else if (col.field === 'start_tp') {
  611. if (select.start_expr && select.start_expr !== '') {
  612. info.sheet.getCell(info.row, info.col).text(select.start_expr);
  613. }
  614. } else if (col.field === 'range_tp') {
  615. if (select.range_expr && select.range_expr !== '') {
  616. info.sheet.getCell(info.row, info.col).text(select.range_expr);
  617. }
  618. }
  619. },
  620. editEnded: function(e, info) {
  621. if (!info.sheet.zh_setting) return;
  622. const select = SpreadJsObj.getSelectObject(info.sheet);
  623. const col = info.sheet.zh_setting.cols[info.col];
  624. if (col.field === 'is_gather') return;
  625. // 未改变值则不提交
  626. const validText = info.editingText ? info.editingText.replace('\n', '') : '';
  627. let orgValue;
  628. if (col.field === 'tp') {
  629. orgValue = select.expr ? payCalc.trans2OrderExpr(select.expr, payTree) : select.tp;
  630. } else if (col.field === 'start_tp') {
  631. orgValue = select.start_expr ? select.start_expr : select.start_tp;
  632. } else if (col.field === 'range_tp') {
  633. orgValue = select.range_expr ? select.range_expr : select.range_tp;
  634. } else {
  635. orgValue = select[col.field];
  636. }
  637. orgValue = orgValue || '';
  638. if (orgValue == validText) {
  639. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  640. return;
  641. }
  642. const data = { postType: 'update', postData: { id: select.id } };
  643. switch(col.field) {
  644. case 'tp':
  645. const [tpValid, tpMsg] = payUtils.check.isSf(select)
  646. ? payCalc.checkSfExpr(validText, data.postData, select, payTree)
  647. : payCalc.checkExpr(validText, data.postData, select, payTree);
  648. if (!tpValid) {
  649. toastr.warning(tpMsg);
  650. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  651. return;
  652. }
  653. break;
  654. case 'start_tp':
  655. const [sValid, sMsg] = payCalc.checkStartExpr(select, validText, data.postData);
  656. if (!sValid) {
  657. toastr.warning(sMsg);
  658. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  659. return;
  660. }
  661. break;
  662. case 'range_tp':
  663. const [rValid, rMsg] = payCalc.checkRangeExpr(select, validText, data.postData);
  664. if (!rValid) {
  665. toastr.warning(rMsg);
  666. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  667. return;
  668. }
  669. break;
  670. default:
  671. if (col.type === 'Number') {
  672. data.postData[col.field] = _.toNumber(validText) || 0;
  673. } else {
  674. data.postData[col.field] = validText || '';
  675. }
  676. break;
  677. }
  678. postData('update', data, function (result) {
  679. if (result.reload) {
  680. payEvent.reloadPays(result.reload);
  681. } else {
  682. const refreshData = payTree.loadPostData(result);
  683. payEvent.refreshTree(refreshData);
  684. }
  685. }, function () {
  686. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  687. });
  688. },
  689. selectionChanged: function(e, info) {
  690. if (info.newSelections) {
  691. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  692. payEvent.refreshActn();
  693. }
  694. }
  695. payEvent.loadExprToInput();
  696. },
  697. buttonClicked: function (e, info) {
  698. if (!info.sheet.zh_setting) return;
  699. const select = SpreadJsObj.getSelectObject(info.sheet);
  700. const col = info.sheet.zh_setting.cols[info.col];
  701. if (col.field !== 'is_gather') return;
  702. if (!payUtils.check.isGatherValid(select)) return;
  703. if (info.sheet.isEditing()) info.sheet.endEdit(true);
  704. const data = {
  705. postType: 'update',
  706. postData: { id: select.id },
  707. };
  708. data.postData[col.field] = info.sheet.getValue(info.row, info.col) || 0;
  709. // 更新至服务器
  710. postData('update', data, function (result) {
  711. if (result.reload) payEvent.reloadPays(result.reload);
  712. });
  713. },
  714. deletePress: function (sheet) {
  715. if (!sheet.zh_setting) return;
  716. const sel = sheet.getSelections()[0];
  717. if (!sel) return;
  718. const col = sheet.zh_setting.cols[sel.col];
  719. if (col.readOnly === true) return;
  720. if (sel.colCount > 1) toastr.warning('请勿同时删除多列数据');
  721. const data = { postType: 'update', postData: [] };
  722. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow ++) {
  723. const node = sheet.zh_tree.nodes[iRow];
  724. if (node && (!node.pay_type || col.field === 'postil')) {
  725. const updateData = { id: node.id };
  726. switch(col.field) {
  727. case 'tp':
  728. updateData.tp = 0;
  729. updateData.expr = '';
  730. break;
  731. case 'start_tp':
  732. updateData.start_tp = 0;
  733. updateData.start_expr = '';
  734. break;
  735. case 'range_tp':
  736. updateData.range_tp = 0;
  737. updateData.range_expr = '';
  738. break;
  739. case 'is_gather':
  740. updateData.is_gather = 0;
  741. break;
  742. default:
  743. updateData[col.field] = col.type === 'Number' ? 0 : '';
  744. }
  745. data.postData.push(updateData);
  746. }
  747. }
  748. postData('update', data, function (result) {
  749. if (result.reload) {
  750. payEvent.reloadPays(result.reload);
  751. } else {
  752. const refreshData = payTree.loadPostData(result);
  753. payEvent.refreshTree(refreshData);
  754. }
  755. }, function () {
  756. SpreadJsObj.reLoadRowData(sheet, sel.row, sel.rowCount);
  757. });
  758. },
  759. baseOpr: function(type) {
  760. const self = this;
  761. const node = SpreadJsObj.getSelectObject(sheet);
  762. if (type === 'delete') {
  763. postData('update', { postType: 'delete', postData: { id: node.tree_id }}, function(result) {
  764. payEvent.reloadPays(result.reload);
  765. });
  766. } else {
  767. postData('update', { postType: type, postData: { id: node.tree_id }}, function (result) {
  768. const refreshData = payTree.loadPostData(result);
  769. payEvent.refreshTree(refreshData);
  770. const sel = sheet.getSelections()[0];
  771. if (sel) {
  772. if (['up-move', 'down-move'].indexOf(type) > -1) {
  773. sheet.setSelection(payTree.getNodeIndex(node), sel.col, sel.rowCount, sel.colCount);
  774. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(node)]);
  775. } else if (type === 'add') {
  776. sheet.setSelection(payTree.getNodeIndex(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  777. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(refreshData.create[0])]);
  778. }
  779. }
  780. self.refreshActn(sheet);
  781. });
  782. }
  783. },
  784. calculateAll: function() {
  785. postData('update', { postType: 'calc', postData: {}}, function (result) {
  786. payEvent.reloadPays(result.reload);
  787. });
  788. },
  789. reloadCalcBase: function() {
  790. postData('update', { postType: 'refreshBase', postData: {}}, function (result) {
  791. payEvent.reloadPays(result.reload);
  792. payCalc.reloadBase(result.calcBase, result.addBase);
  793. });
  794. },
  795. reloadPays: function(datas){
  796. payTree.loadDatas(datas);
  797. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Tree, payTree);
  798. payEvent.refreshActn();
  799. }
  800. };
  801. spread.bind(spreadNS.Events.SelectionChanged, payEvent.selectionChanged);
  802. if (!readOnly) {
  803. spread.bind(spreadNS.Events.EditStarting, payEvent.editStarting);
  804. spread.bind(spreadNS.Events.EditEnded, payEvent.editEnded);
  805. spread.bind(spreadNS.Events.ButtonClicked, payEvent.buttonClicked);
  806. SpreadJsObj.addDeleteBind(spread, payEvent.deletePress);
  807. $('a[name="base-opr"]').click(function () {
  808. payEvent.baseOpr(this.getAttribute('type'));
  809. });
  810. $('#calc-all').click(function() {
  811. payEvent.calculateAll();
  812. });
  813. $('#reload-calc-base').click(function() {
  814. payEvent.reloadCalcBase();
  815. });
  816. const deadlineObj = {
  817. payNode: null,
  818. refreshHint: function() {
  819. const dlType = $('[name=dl-type]:checked').val();
  820. const dt = deadlineType[dlType];
  821. if (dlType && dt) {
  822. const dlValue = $('#dl-value').val();
  823. $('#range-hint').text(`当 ${dt.name} >= ${dlValue} 时 `);
  824. $('#dl-hint').show();
  825. } else {
  826. $('#dl-hint').hide();
  827. }
  828. },
  829. initView: function(data) {
  830. this.payNode = data;
  831. $('#dl-pay-name').html(data.name);
  832. // 模式
  833. if (data.dl_type) {
  834. $('[name=dl-type][value=' + data.dl_type +']')[0].checked = true;
  835. } else {
  836. $('#dl-type-none')[0].checked = true;
  837. }
  838. $('#dl-value').val(data.dl_value);
  839. this.refreshHint();
  840. },
  841. getDlCount: function() {
  842. try {
  843. const result = parseInt($('#dl-value').val());
  844. if (result <= 0) throw '限制值请输入正整数';
  845. return result;
  846. } catch (err) {
  847. toastr.warning('限制值请输入正整数');
  848. return 0;
  849. }
  850. },
  851. getDlTp: function() {
  852. try {
  853. const result = parseFloat($('#dl-value').val());
  854. return result;
  855. } catch (err) {
  856. toastr.warning('限制值请输入数值');
  857. return 0;
  858. }
  859. },
  860. getUpdateData: function() {
  861. const result = { postType: 'update', postData: { id: this.payNode.id } };
  862. result.postData.dl_type = $('[name=dl-type]:checked').val();
  863. if (result.postData.dl_type) {
  864. if (result.postData.dl_type === deadlineType.phaseCount.key) {
  865. result.postData.dl_value = this.getDlCount();
  866. if (result.postData.dl_value < phasePay.phase_order) {
  867. toastr.warning(`已计量至第${phasePay.phase_order}期,计提期限不可小于该期`);
  868. return null;
  869. }
  870. } else if (result.postData.dl_type === deadlineType.stageCount.key) {
  871. result.postData.dl_value = this.getDlCount();
  872. if (result.postData.dl_value < maxStageOrder) {
  873. toastr.warning(`已计量至第${maxStageOrder}期,计提期限不可小于该期`);
  874. return null;
  875. }
  876. } else {
  877. result.postData.dl_value = this.getDlTp();
  878. const dt = deadlineType[result.postData.dl_type];
  879. if (!dt) {
  880. toastr.warning('限制模式错误,请刷新页面重试');
  881. return null;
  882. }
  883. const compareValue = payCalc.addBase[`pre_${dt.key}_tp`];
  884. if (result.postData.dl_type < compareValue) {
  885. toastr.warning(`截止上期,${dt.name}已计量${compareValue},计提期限不可小于该值`);
  886. return null;
  887. }
  888. }
  889. } else {
  890. result.postData.dl_value = 0;
  891. }
  892. return result;
  893. },
  894. };
  895. $('[name=dl-type]').change(deadlineObj.refreshHint);
  896. $('#dl-value').change(deadlineObj.refreshHint);
  897. $('#deadline-ok').click(function() {
  898. const updateData = deadlineObj.getUpdateData();
  899. if (!updateData) return;
  900. postData('update', updateData, function(result) {
  901. payEvent.reloadPays(result.reload);
  902. $('#deadline').modal('hide');
  903. });
  904. });
  905. // 右键菜单
  906. $.contextMenu({
  907. selector: '#pay-spread',
  908. build: function ($trigger, e) {
  909. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  910. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  911. },
  912. items: {
  913. 'start': {
  914. name: '启用',
  915. icon: 'fa-play',
  916. callback: function (key, opt) {
  917. const select = SpreadJsObj.getSelectObject(sheet);
  918. const data = {
  919. postType: 'update',
  920. postData: { id: select.id, is_pause: 0 }
  921. };
  922. // 更新至服务器
  923. postData('update', data, function (result) {
  924. payEvent.reloadPays(result.reload);
  925. });
  926. },
  927. visible: function (key, opt) {
  928. const select = SpreadJsObj.getSelectObject(sheet);
  929. return (!select.children || select.children.length ===0) && !readOnly && select.is_pause && payUtils.menuVisible.pause(select);
  930. }
  931. },
  932. 'stop': {
  933. name: '停用',
  934. icon: 'fa-pause',
  935. callback: function (key, opt) {
  936. const select = SpreadJsObj.getSelectObject(sheet);
  937. const data = {
  938. postType: 'update',
  939. postData: { id: select.id, is_pause: 1 }
  940. };
  941. // 更新至服务器
  942. postData('update', data, function (result) {
  943. payEvent.reloadPays(result.reload);
  944. });
  945. },
  946. visible: function (key, opt) {
  947. const select = SpreadJsObj.getSelectObject(sheet);
  948. return (!select.children || select.children.length === 0) && !readOnly && !select.is_pause && payUtils.menuVisible.pause(select);
  949. },
  950. },
  951. 'setDeadline': {
  952. name: '设置计提期限',
  953. icon: 'fa-clipboard',
  954. callback: function (key, opt) {
  955. const select = SpreadJsObj.getSelectObject(sheet);
  956. if (select.range_tp) {
  957. deadlineObj.initView(select);
  958. $('#deadline').modal('show');
  959. } else {
  960. toastr.warning('计提期限用于达到条件时,即刻计量至付(扣)款限额,应先设置付(扣)款限额');
  961. }
  962. },
  963. visible: function (key, opt) {
  964. const select = SpreadJsObj.getSelectObject(sheet);
  965. return (!select.children || select.children.length === 0) && !readOnly && payUtils.menuVisible.deadline(select);
  966. }
  967. },
  968. }
  969. });
  970. }
  971. return { spread, sheet, payTree, loadDatas: payEvent.reloadPays }
  972. })();
  973. payObj.loadDatas(details);
  974. const payFile = $.ledger_att({
  975. selector: '#fujian',
  976. key: 'uuid',
  977. masterKey: 'rela_id',
  978. uploadUrl: 'file/upload',
  979. deleteUrl: 'file/delete',
  980. checked: false,
  981. zipName: `附件.zip`,
  982. readOnly: false,
  983. fileIdType: 'string',
  984. fileInfo: {
  985. user_name: 'user_name',
  986. user_id: 'user_id',
  987. create_time: 'create_time',
  988. },
  989. getCurHint: function(node) {
  990. return`${node.name || ''}`;
  991. },
  992. locate: function (att) {
  993. if (!att) return;
  994. SpreadJsObj.locateTreeNode(payObj.sheet, att.node.tree_id, true);
  995. payFile.getCurAttHtml(att.node);
  996. }
  997. });
  998. // 展开收起标准清单
  999. $('a', '#side-menu').bind('click', function (e) {
  1000. e.preventDefault();
  1001. const tab = $(this), tabPanel = $(tab.attr('content'));
  1002. // 展开工具栏、切换标签
  1003. if (!tab.hasClass('active')) {
  1004. // const close = $('.active', '#side-menu').length === 0;
  1005. $('a', '#side-menu').removeClass('active');
  1006. $('.tab-content .tab-select-show.tab-pane.active').removeClass('active');
  1007. tab.addClass('active');
  1008. tabPanel.addClass('active');
  1009. // $('.tab-content .tab-pane').removeClass('active');
  1010. showSideTools(tab.hasClass('active'));
  1011. } else { // 收起工具栏
  1012. tab.removeClass('active');
  1013. tabPanel.removeClass('active');
  1014. showSideTools(tab.hasClass('active'));
  1015. }
  1016. payObj.spread.refresh();
  1017. });
  1018. postData('load', {filter: 'file'}, function(result) {
  1019. for (const f of result.file) {
  1020. f.node = payObj.payTree.datas.find(x => { return x.uuid === f.rela_id; });
  1021. }
  1022. payFile.loadDatas(result.file);
  1023. payFile.getCurAttHtml(SpreadJsObj.getSelectObject(payObj.sheet));
  1024. });
  1025. // 工具栏spr
  1026. $.divResizer({
  1027. select: '#right-spr',
  1028. callback: function () {
  1029. payObj.spread.refresh();
  1030. }
  1031. });
  1032. // todo 加载审批列表
  1033. $.subMenu({
  1034. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1035. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1036. key: 'menu.1.0.0',
  1037. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1038. callback: function (info) {
  1039. if (info.mini) {
  1040. $('.panel-title').addClass('fluid');
  1041. $('#sub-menu').removeClass('panel-sidebar');
  1042. } else {
  1043. $('.panel-title').removeClass('fluid');
  1044. $('#sub-menu').addClass('panel-sidebar');
  1045. }
  1046. autoFlashHeight();
  1047. payObj.spread.refresh();
  1048. }
  1049. });
  1050. });