phase_pay_detail.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. }
  43. };
  44. const payCalc = (function (b) {
  45. class PayCalc {
  46. constructor (bases) {
  47. this.percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
  48. this.bases = bases;
  49. this.bases.sort(function (a, b) {
  50. return a.sort - b.sort;
  51. });
  52. for (const b of this.bases) {
  53. b.reg = new RegExp(b.code, 'igm');
  54. }
  55. this.orderReg = /f\d+/ig;
  56. this.nodeReg = /<<[a-z0-9\-]+>>/ig;
  57. }
  58. trans2OrderExpr(expr, payTree) {
  59. const nodeParam = expr.match(this.nodeReg);
  60. if (nodeParam) {
  61. for (const op of nodeParam) {
  62. const id = op.substring(2, op.length - 2);
  63. const payNode = payTree.nodes.find(x => { return x.uuid === id; });
  64. expr = expr.replace(op, payNode ? `f${payTree.getNodeIndex(payNode) + 1}` || '' : 0);
  65. }
  66. }
  67. return expr;
  68. }
  69. trans2NodeExpr(expr, payTree) {
  70. const orderParam = expr.match(this.orderReg);
  71. if (orderParam) {
  72. for (const op of orderParam) {
  73. const order = parseInt(op.substring(1, op.length));
  74. const payNode = payTree.nodes[order - 1];
  75. expr = expr.replace(op, payNode ? `<<${payNode.uuid}>>` || '' : 0);
  76. }
  77. }
  78. return expr;
  79. }
  80. checkSfExpr() {
  81. }
  82. checkExpr() {
  83. }
  84. checkRangeExpr() {
  85. }
  86. checkStartExpr() {
  87. }
  88. getExprInfo(field, converse = false) {
  89. const exprField = [
  90. {qty: 'tp', expr: 'expr'},
  91. {qty: 'start_tp', expr: 'start_expr'},
  92. {qty: 'range_qty', expr: 'range_expr'},
  93. ];
  94. if (converse) return _.find(exprField, { expr: field });
  95. return _.find(exprField, {qty: field});
  96. }
  97. getLeafOrder(data, parentOrder) {
  98. if (!data) return [];
  99. const defaultResult = data.order ? [`f${data.order}`] : [];
  100. if (!data.expr) return defaultResult;
  101. const orderParam = data.expr.match(this.orderReg);
  102. if (!orderParam || orderParam.length === 0) return defaultResult;
  103. const result = [], payData = paySheet.zh_data || [];
  104. for (const op of orderParam) {
  105. const order = op.substring(1, op.length);
  106. if (data.order === parseInt(order) || op === parentOrder) {
  107. result.push(op);
  108. } else {
  109. const subOrderParam = this.getLeafOrder(payData[parseInt(order) -1], data.order ? `f${data.order}` : parentOrder);
  110. result.push(...subOrderParam);
  111. }
  112. }
  113. return result;
  114. }
  115. checkCircularExpr(expr, selfOrder) {
  116. const leafOrder = this.getLeafOrder({expr}, `f${selfOrder}`);
  117. if (leafOrder.indexOf(`f${selfOrder}`) >= 0 || leafOrder.indexOf(`F${selfOrder}`) >= 0) return true;
  118. return false;
  119. }
  120. calculateExpr(expr) {
  121. let formula = expr;
  122. for (const b of this.bases) {
  123. formula = formula.replace(b.reg, b.value);
  124. }
  125. const percent = formula.match(this.percentReg);
  126. if (percent) {
  127. for (const p of percent) {
  128. const v = math.evaluate(p.replace(new RegExp('%', 'gm'), '/100'));
  129. formula = formula.replace(p, v);
  130. }
  131. }
  132. try {
  133. const value = math.evaluate(formula);
  134. return value;
  135. } catch(err) {
  136. return 0;
  137. }
  138. }
  139. }
  140. return new PayCalc(b);
  141. })(calcBase);
  142. const payObj = (function() {
  143. const spread = SpreadJsObj.createNewSpread($('#pay-spread')[0]);
  144. const sheet = spread.getActiveSheet();
  145. const spreadSetting = {
  146. cols: [
  147. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 230, formatter: '@', cellType: 'tree', getTip: payUtils.tips.name},
  148. {title: '本期金额(F)', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 100, type: 'Number'},
  149. {title: '截止上期金额', colSpan: '1', rowSpan: '1', field: 'pre_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
  150. {title: '截止本期金额', colSpan: '1', rowSpan: '1', field: 'end_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
  151. {title: '起扣金额', colSpan: '1', rowSpan: '1', field: 'start_tp', hAlign: 2, width: 100, type: 'Number'},
  152. {title: '付(扣)款限额', colSpan: '1', rowSpan: '1', field: 'range_tp', hAlign: 2, width: 100, cellType: 'tip', type: 'Number', getTip: payUtils.tips.range_tp},
  153. {title: '汇总', colSpan: '1', rowSpan: '1', field: 'is_yf', hAlign: 2, width: 60, readOnly: true, type: 'checkbox'},
  154. {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'},
  155. {title: '本期批注', colSpan: '1', rowSpan: '1', field: 'postil', hAlign: 0, width: 120, formatter: '@', cellType: 'autoTip'},
  156. ],
  157. emptyRows: 0,
  158. headRows: 1,
  159. headRowHeight: [32],
  160. headColWidth: [30],
  161. defaultRowHeight: 21,
  162. headerFont: '12px 微软雅黑',
  163. font: '12px 微软雅黑',
  164. readOnly: readOnly,
  165. localCache: {
  166. key: 'phase-pay',
  167. colWidth: true,
  168. },
  169. pos: SpreadJsObj.getObjPos($('#pay-spread')[0]),
  170. };
  171. sjsSettingObj.setFxTreeStyle(spreadSetting, sjsSettingObj.FxTreeStyle.phasePay);
  172. SpreadJsObj.initSheet(sheet, spreadSetting);
  173. const payTree = createNewPathTree('base', {
  174. id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
  175. level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
  176. rootId: -1,
  177. });
  178. const payEvent = {
  179. refreshActn: function() {
  180. const setObjEnable = function (obj, enable) {
  181. if (enable) {
  182. obj.removeClass('disabled');
  183. } else {
  184. obj.addClass('disabled');
  185. }
  186. };
  187. const select = SpreadJsObj.getSelectObject(sheet);
  188. const preNode = payTree.getPreSiblingNode(select);
  189. setObjEnable($('a[name=base-opr][type=add]'), !readOnly && !payUtils.check.isSf(select) && !payUtils.check.isYf(select));
  190. const delValid = !payUtils.check.isFixed(select) && !payUtils.check.isStarted(select);
  191. setObjEnable($('a[name=base-opr][type=del]'), !readOnly && select && delValid);
  192. setObjEnable($('a[name=base-opr][type=up-move]'), !readOnly && select && !payUtils.check.isFixed(select) && preNode);
  193. setObjEnable($('a[name=base-opr][type=down-move]'), !readOnly && select && !payUtils.check.isFixed(select) && !this.payTree.isLastSibling(select));
  194. },
  195. loadExprToInput: function() {
  196. const sel = sheet.getSelections()[0];
  197. const col = sheet.zh_setting.cols[sel.col];
  198. const data = SpreadJsObj.getSelectObject(this.sheet);
  199. if (data) {
  200. if (col.field === 'tp') {
  201. $('#expr').val(data.expr).attr('field', 'expr').attr('org', data.expr)
  202. .attr('readOnly', readOnly|| payCol.readOnly.tp(data));
  203. } else if (col.field === 'stage_tp') {
  204. $('#expr').val(data.start_expr).attr('field', 'start_expr').attr('org', data.start_expr)
  205. .attr('readOnly', readOnly|| payCol.readOnly.sprice(data) || payBase.isYF(data));
  206. } else if (col.field === 'rprice') {
  207. $('#expr').val(data.range_expr).attr('field', 'range_expr').attr('org', data.range_expr)
  208. .attr('readOnly', readOnly|| payCol.readOnly.rprice(data) || payBase.isYF(data));
  209. } else {
  210. $('#expr').val('').attr('readOnly', true);
  211. }
  212. $('#expr').attr('data-row', sel.row);
  213. } else {
  214. $('#expr').val('').attr('readOnly', true);
  215. $('#expr').removeAttr('data-row');
  216. }
  217. },
  218. refreshTree: function (data) {
  219. SpreadJsObj.massOperationSheet(sheet, function () {
  220. const tree = sheet.zh_tree;
  221. // 处理删除
  222. if (data.delete) {
  223. data.delete.sort(function (a, b) {
  224. return b.deleteIndex - a.deleteIndex;
  225. });
  226. for (const d of data.delete) {
  227. sheet.deleteRows(d.deleteIndex, 1);
  228. }
  229. }
  230. // 处理新增
  231. if (data.create) {
  232. const newNodes = data.create;
  233. if (newNodes) {
  234. newNodes.sort(function (a, b) {
  235. return a.index - b.index;
  236. });
  237. for (const node of newNodes) {
  238. sheet.addRows(node.index, 1);
  239. SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
  240. }
  241. }
  242. }
  243. // 处理更新
  244. if (data.update) {
  245. const rows = [];
  246. for (const u of data.update) {
  247. rows.push(tree.nodes.indexOf(u));
  248. billsTag.refreshBillsTagView(u);
  249. }
  250. SpreadJsObj.reLoadRowsData(sheet, rows);
  251. }
  252. });
  253. },
  254. editStarting: function(e, info) {
  255. const col = info.sheet.zh_setting.cols[info.col];
  256. const select = SpreadJsObj.getSelectObject(info.sheet);
  257. switch (col.field) {
  258. case 'name':
  259. info.cancel = payUtils.check.isFixed(select);
  260. break;
  261. case 'tp':
  262. case 'is_gather':
  263. info.cancel = select.children && select.children.length > 0;
  264. break;
  265. case 'start_tp':
  266. case 'range_tp':
  267. info.cancel = (select.children && select.children.length > 0) || payUtils.check.isYf(select);
  268. break;
  269. }
  270. if (col.field === 'tp') {
  271. if (select.expr && select.expr !== '') {
  272. info.sheet.getCell(info.row, info.col).text(payCalc.trans2OrderExpr(select.expr, payTree));
  273. }
  274. } else if (col.field === 'start_tp') {
  275. if (select.start_expr && select.start_expr !== '') {
  276. info.sheet.getCell(info.row, info.col).text(select.start_expr);
  277. }
  278. } else if (col.field === 'range_tp') {
  279. if (select.range_expr && select.range_expr !== '') {
  280. info.sheet.getCell(info.row, info.col).text(select.range_expr);
  281. }
  282. }
  283. },
  284. editEnded: function(e, info) {
  285. if (!info.sheet.zh_setting) return;
  286. const select = SpreadJsObj.getSelectObject(info.sheet);
  287. const col = info.sheet.zh_setting.cols[info.col];
  288. if (col.field === 'is_gather') return;
  289. // 未改变值则不提交
  290. const validText = info.editingText ? info.editingText.replace('\n', '') : null;
  291. let orgValue;
  292. if (col.field === 'tp') {
  293. orgValue = select.expr ? payCalc.trans2OrderExpr(select.expr, payTree) : select.tp;
  294. } else if (col.field === 'start_tp') {
  295. orgValue = select.start_expr ? select.start_expr : select.start_tp;
  296. } else if (col.field === 'range_tp') {
  297. orgValue = select.range_expr ? select.range_expr : select.range_tp;
  298. } else {
  299. orgValue = select[col.field];
  300. }
  301. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  302. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  303. return;
  304. }
  305. const data = { postType: 'update', postData: {} };
  306. switch(col.field) {
  307. case 'tp':
  308. const [tpValid, tpMsg] = payUtils.isSF(select)
  309. ? payCalc.checkSfExpr(validText, data.updateData, select.order)
  310. : payCalc.checkExpr(validText, data.updateData, select.order);
  311. if (!tpValid) {
  312. toastr.warning(tpMsg);
  313. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  314. return;
  315. }
  316. break;
  317. case 'start_tp':
  318. const [sValid, sMsg] = payCalc.checkStartExpr(select, validText, data.updateData);
  319. if (!sValid) {
  320. toastr.warning(sMsg);
  321. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  322. return;
  323. }
  324. break;
  325. case 'range_tp':
  326. const [rValid, rMsg] = payCalc.checkRangeExpr(select, validText, data.updateData);
  327. if (!rValid) {
  328. toastr.warning(rMsg);
  329. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  330. return;
  331. }
  332. break;
  333. default:
  334. if (col.type === 'Number') {
  335. data.postData[col.field] = _.toNumber(validText) || 0;
  336. } else {
  337. data.postData[col.field] = validText || '';
  338. }
  339. break;
  340. }
  341. postData('update', data, function (result) {
  342. payEvent.reloadPays(result.reload);
  343. }, function () {
  344. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  345. });
  346. },
  347. selectionChanged: function(e, info) {
  348. if (info.newSelections) {
  349. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  350. payEvent.refreshActn();
  351. }
  352. }
  353. payEvent.loadExprToInput();
  354. },
  355. baseOpr: function(type) {
  356. const node = SpreadJsObj.getSelectObject(sheet);
  357. if (type === 'del') {
  358. postData('update', {postType: 'del', postData: { id: node.tree_id }}, function(result) {
  359. payEvent.reloadPays(result.reload);
  360. });
  361. } else {
  362. postData('update', { postType: type, postData: { id: node.tree_id }}, function (result) {
  363. const refreshData = payTree.loadPostData(result);
  364. payEvent.refreshTree(sheet, refreshData);
  365. const sel = sheet.getSelections()[0];
  366. if (sel) {
  367. if (['up-move', 'down-move'].indexOf(type) > -1) {
  368. sheet.setSelection(payTree.getNodeIndex(node), sel.col, sel.rowCount, sel.colCount);
  369. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(node)]);
  370. } else if (type === 'add') {
  371. sheet.setSelection(payTree.getNodeIndex(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
  372. SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(refreshData.create[0])]);
  373. }
  374. }
  375. self.refreshOperationValid(sheet);
  376. });
  377. }
  378. },
  379. reloadPays: function(datas){
  380. payTree.loadDatas(datas);
  381. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Tree, payTree);
  382. payEvent.refreshActn();
  383. }
  384. };
  385. spread.bind(spreadNS.Events.SelectionChanged, payEvent.selectionChanged);
  386. if (!readOnly) {
  387. spread.bind(spreadNS.Events.EditStarting, payEvent.editStarting);
  388. spread.bind(spreadNS.Events.EditEnded, payEvent.editEnded);
  389. $('a[name="base-opr"]').click(function () {
  390. payEvent.baseOpr(this.getAttribute('type'));
  391. });
  392. }
  393. return { spread, sheet, payTree, loadDatas: payEvent.reloadPays }
  394. })();
  395. payObj.loadDatas(details);
  396. // todo 加载审批列表
  397. $.subMenu({
  398. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  399. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  400. key: 'menu.1.0.0',
  401. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  402. callback: function (info) {
  403. if (info.mini) {
  404. $('.panel-title').addClass('fluid');
  405. $('#sub-menu').removeClass('panel-sidebar');
  406. } else {
  407. $('.panel-title').removeClass('fluid');
  408. $('#sub-menu').addClass('panel-sidebar');
  409. }
  410. autoFlashHeight();
  411. payObj.spread.refresh();
  412. }
  413. });
  414. });