123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- 'use strict';
- $(document).ready(() => {
- const payUtils = {
- tips: {
- name: function(data) {
- const tips = [];
- if (data) {
- if (data.pause) tips.push('当前项已停用');
- if (!data.is_yf) tips.push('当前项不参与本期应付计算');
- }
- return tips.join('<br/>');
- },
- range_tp: function (data) {
- if (!data || (!data.range_expr && !data.range_tp) || !data.dl_type) return '';
- if (data.dl_type === 1) {
- return '计提期限为(当 计量期数 ≥ ' + data.dl_count + ')';
- } else if (data.dl_type === 2) {
- switch (data.dl_tp_type) {
- case 'contract':
- return '计提期限为(累计合同计量 ≥ ' + data.dl_tp + ')';
- case 'qc':
- return '计提期限为(累计变更计量 ≥ ' + data.dl_tp + ')';
- case 'gather':
- return '计提期限为(累计完成计量 ≥ ' + data.dl_tp + ')';
- }
- }
- }
- },
- check: {
- isFixed: function(data) {
- return data.is_fixed;
- },
- isStarted: function (data) {
- return data.pre_used;
- },
- isYf: function(data) {
- return data.pay_type === 'bqyf';
- },
- isSf: function(data) {
- return data.pay_type === 'bqsf';
- }
- }
- };
- const payCalc = (function (b) {
- class PayCalc {
- constructor (bases) {
- this.percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
- this.bases = bases;
- this.bases.sort(function (a, b) {
- return a.sort - b.sort;
- });
- for (const b of this.bases) {
- b.reg = new RegExp(b.code, 'igm');
- }
- this.orderReg = /f\d+/ig;
- this.nodeReg = /<<[a-z0-9\-]+>>/ig;
- }
- trans2OrderExpr(expr, payTree) {
- const nodeParam = expr.match(this.nodeReg);
- if (nodeParam) {
- for (const op of nodeParam) {
- const id = op.substring(2, op.length - 2);
- const payNode = payTree.nodes.find(x => { return x.uuid === id; });
- expr = expr.replace(op, payNode ? `f${payTree.getNodeIndex(payNode) + 1}` || '' : 0);
- }
- }
- return expr;
- }
- trans2NodeExpr(expr, payTree) {
- const orderParam = expr.match(this.orderReg);
- if (orderParam) {
- for (const op of orderParam) {
- const order = parseInt(op.substring(1, op.length));
- const payNode = payTree.nodes[order - 1];
- expr = expr.replace(op, payNode ? `<<${payNode.uuid}>>` || '' : 0);
- }
- }
- return expr;
- }
- checkSfExpr() {
- }
- checkExpr() {
- }
- checkRangeExpr() {
- }
- checkStartExpr() {
- }
- getExprInfo(field, converse = false) {
- const exprField = [
- {qty: 'tp', expr: 'expr'},
- {qty: 'start_tp', expr: 'start_expr'},
- {qty: 'range_qty', expr: 'range_expr'},
- ];
- if (converse) return _.find(exprField, { expr: field });
- return _.find(exprField, {qty: field});
- }
- getLeafOrder(data, parentOrder) {
- if (!data) return [];
- const defaultResult = data.order ? [`f${data.order}`] : [];
- if (!data.expr) return defaultResult;
- const orderParam = data.expr.match(this.orderReg);
- if (!orderParam || orderParam.length === 0) return defaultResult;
- const result = [], payData = paySheet.zh_data || [];
- for (const op of orderParam) {
- const order = op.substring(1, op.length);
- if (data.order === parseInt(order) || op === parentOrder) {
- result.push(op);
- } else {
- const subOrderParam = this.getLeafOrder(payData[parseInt(order) -1], data.order ? `f${data.order}` : parentOrder);
- result.push(...subOrderParam);
- }
- }
- return result;
- }
- checkCircularExpr(expr, selfOrder) {
- const leafOrder = this.getLeafOrder({expr}, `f${selfOrder}`);
- if (leafOrder.indexOf(`f${selfOrder}`) >= 0 || leafOrder.indexOf(`F${selfOrder}`) >= 0) return true;
- return false;
- }
- calculateExpr(expr) {
- let formula = expr;
- for (const b of this.bases) {
- formula = formula.replace(b.reg, b.value);
- }
- const percent = formula.match(this.percentReg);
- if (percent) {
- for (const p of percent) {
- const v = math.evaluate(p.replace(new RegExp('%', 'gm'), '/100'));
- formula = formula.replace(p, v);
- }
- }
- try {
- const value = math.evaluate(formula);
- return value;
- } catch(err) {
- return 0;
- }
- }
- }
- return new PayCalc(b);
- })(calcBase);
- const payObj = (function() {
- const spread = SpreadJsObj.createNewSpread($('#pay-spread')[0]);
- const sheet = spread.getActiveSheet();
- const spreadSetting = {
- cols: [
- {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 230, formatter: '@', cellType: 'tree', getTip: payUtils.tips.name},
- {title: '本期金额(F)', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 100, type: 'Number'},
- {title: '截止上期金额', colSpan: '1', rowSpan: '1', field: 'pre_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
- {title: '截止本期金额', colSpan: '1', rowSpan: '1', field: 'end_tp', hAlign: 2, width: 100, readOnly: true, type: 'Number'},
- {title: '起扣金额', colSpan: '1', rowSpan: '1', field: 'start_tp', hAlign: 2, width: 100, type: 'Number'},
- {title: '付(扣)款限额', colSpan: '1', rowSpan: '1', field: 'range_tp', hAlign: 2, width: 100, cellType: 'tip', type: 'Number', getTip: payUtils.tips.range_tp},
- {title: '汇总', colSpan: '1', rowSpan: '1', field: 'is_yf', hAlign: 2, width: 60, readOnly: true, type: 'checkbox'},
- {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'},
- {title: '本期批注', colSpan: '1', rowSpan: '1', field: 'postil', hAlign: 0, width: 120, formatter: '@', cellType: 'autoTip'},
- ],
- emptyRows: 0,
- headRows: 1,
- headRowHeight: [32],
- headColWidth: [30],
- defaultRowHeight: 21,
- headerFont: '12px 微软雅黑',
- font: '12px 微软雅黑',
- readOnly: readOnly,
- localCache: {
- key: 'phase-pay',
- colWidth: true,
- },
- pos: SpreadJsObj.getObjPos($('#pay-spread')[0]),
- };
- sjsSettingObj.setFxTreeStyle(spreadSetting, sjsSettingObj.FxTreeStyle.phasePay);
- SpreadJsObj.initSheet(sheet, spreadSetting);
- const payTree = createNewPathTree('base', {
- id: 'tree_id', pid: 'tree_pid', order: 'tree_order',
- level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path',
- rootId: -1,
- });
- const payEvent = {
- refreshActn: function() {
- const setObjEnable = function (obj, enable) {
- if (enable) {
- obj.removeClass('disabled');
- } else {
- obj.addClass('disabled');
- }
- };
- const select = SpreadJsObj.getSelectObject(sheet);
- const preNode = payTree.getPreSiblingNode(select);
- setObjEnable($('a[name=base-opr][type=add]'), !readOnly && !payUtils.check.isSf(select) && !payUtils.check.isYf(select));
- const delValid = !payUtils.check.isFixed(select) && !payUtils.check.isStarted(select);
- setObjEnable($('a[name=base-opr][type=del]'), !readOnly && select && delValid);
- setObjEnable($('a[name=base-opr][type=up-move]'), !readOnly && select && !payUtils.check.isFixed(select) && preNode);
- setObjEnable($('a[name=base-opr][type=down-move]'), !readOnly && select && !payUtils.check.isFixed(select) && !this.payTree.isLastSibling(select));
- },
- loadExprToInput: function() {
- const sel = sheet.getSelections()[0];
- const col = sheet.zh_setting.cols[sel.col];
- const data = SpreadJsObj.getSelectObject(this.sheet);
- if (data) {
- if (col.field === 'tp') {
- $('#expr').val(data.expr).attr('field', 'expr').attr('org', data.expr)
- .attr('readOnly', readOnly|| payCol.readOnly.tp(data));
- } else if (col.field === 'stage_tp') {
- $('#expr').val(data.start_expr).attr('field', 'start_expr').attr('org', data.start_expr)
- .attr('readOnly', readOnly|| payCol.readOnly.sprice(data) || payBase.isYF(data));
- } else if (col.field === 'rprice') {
- $('#expr').val(data.range_expr).attr('field', 'range_expr').attr('org', data.range_expr)
- .attr('readOnly', readOnly|| payCol.readOnly.rprice(data) || payBase.isYF(data));
- } else {
- $('#expr').val('').attr('readOnly', true);
- }
- $('#expr').attr('data-row', sel.row);
- } else {
- $('#expr').val('').attr('readOnly', true);
- $('#expr').removeAttr('data-row');
- }
- },
- refreshTree: function (data) {
- SpreadJsObj.massOperationSheet(sheet, function () {
- const tree = sheet.zh_tree;
- // 处理删除
- if (data.delete) {
- data.delete.sort(function (a, b) {
- return b.deleteIndex - a.deleteIndex;
- });
- for (const d of data.delete) {
- sheet.deleteRows(d.deleteIndex, 1);
- }
- }
- // 处理新增
- if (data.create) {
- const newNodes = data.create;
- if (newNodes) {
- newNodes.sort(function (a, b) {
- return a.index - b.index;
- });
- for (const node of newNodes) {
- sheet.addRows(node.index, 1);
- SpreadJsObj.reLoadRowData(sheet, tree.nodes.indexOf(node), 1);
- }
- }
- }
- // 处理更新
- if (data.update) {
- const rows = [];
- for (const u of data.update) {
- rows.push(tree.nodes.indexOf(u));
- billsTag.refreshBillsTagView(u);
- }
- SpreadJsObj.reLoadRowsData(sheet, rows);
- }
- });
- },
- editStarting: function(e, info) {
- const col = info.sheet.zh_setting.cols[info.col];
- const select = SpreadJsObj.getSelectObject(info.sheet);
- switch (col.field) {
- case 'name':
- info.cancel = payUtils.check.isFixed(select);
- break;
- case 'tp':
- case 'is_gather':
- info.cancel = select.children && select.children.length > 0;
- break;
- case 'start_tp':
- case 'range_tp':
- info.cancel = (select.children && select.children.length > 0) || payUtils.check.isYf(select);
- break;
- }
- if (col.field === 'tp') {
- if (select.expr && select.expr !== '') {
- info.sheet.getCell(info.row, info.col).text(payCalc.trans2OrderExpr(select.expr, payTree));
- }
- } else if (col.field === 'start_tp') {
- if (select.start_expr && select.start_expr !== '') {
- info.sheet.getCell(info.row, info.col).text(select.start_expr);
- }
- } else if (col.field === 'range_tp') {
- if (select.range_expr && select.range_expr !== '') {
- info.sheet.getCell(info.row, info.col).text(select.range_expr);
- }
- }
- },
- editEnded: function(e, info) {
- if (!info.sheet.zh_setting) return;
- const select = SpreadJsObj.getSelectObject(info.sheet);
- const col = info.sheet.zh_setting.cols[info.col];
- if (col.field === 'is_gather') return;
- // 未改变值则不提交
- const validText = info.editingText ? info.editingText.replace('\n', '') : null;
- let orgValue;
- if (col.field === 'tp') {
- orgValue = select.expr ? payCalc.trans2OrderExpr(select.expr, payTree) : select.tp;
- } else if (col.field === 'start_tp') {
- orgValue = select.start_expr ? select.start_expr : select.start_tp;
- } else if (col.field === 'range_tp') {
- orgValue = select.range_expr ? select.range_expr : select.range_tp;
- } else {
- orgValue = select[col.field];
- }
- if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
- SpreadJsObj.reLoadRowData(info.sheet, info.row);
- return;
- }
- const data = { postType: 'update', postData: {} };
- switch(col.field) {
- case 'tp':
- const [tpValid, tpMsg] = payUtils.isSF(select)
- ? payCalc.checkSfExpr(validText, data.updateData, select.order)
- : payCalc.checkExpr(validText, data.updateData, select.order);
- if (!tpValid) {
- toastr.warning(tpMsg);
- SpreadJsObj.reLoadRowData(info.sheet, info.row);
- return;
- }
- break;
- case 'start_tp':
- const [sValid, sMsg] = payCalc.checkStartExpr(select, validText, data.updateData);
- if (!sValid) {
- toastr.warning(sMsg);
- SpreadJsObj.reLoadRowData(info.sheet, info.row);
- return;
- }
- break;
- case 'range_tp':
- const [rValid, rMsg] = payCalc.checkRangeExpr(select, validText, data.updateData);
- if (!rValid) {
- toastr.warning(rMsg);
- SpreadJsObj.reLoadRowData(info.sheet, info.row);
- return;
- }
- break;
- default:
- if (col.type === 'Number') {
- data.postData[col.field] = _.toNumber(validText) || 0;
- } else {
- data.postData[col.field] = validText || '';
- }
- break;
- }
- postData('update', data, function (result) {
- payEvent.reloadPays(result.reload);
- }, function () {
- SpreadJsObj.reLoadRowData(info.sheet, info.row);
- });
- },
- selectionChanged: function(e, info) {
- if (info.newSelections) {
- if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
- payEvent.refreshActn();
- }
- }
- payEvent.loadExprToInput();
- },
- baseOpr: function(type) {
- const node = SpreadJsObj.getSelectObject(sheet);
- if (type === 'del') {
- postData('update', {postType: 'del', postData: { id: node.tree_id }}, function(result) {
- payEvent.reloadPays(result.reload);
- });
- } else {
- postData('update', { postType: type, postData: { id: node.tree_id }}, function (result) {
- const refreshData = payTree.loadPostData(result);
- payEvent.refreshTree(sheet, refreshData);
- const sel = sheet.getSelections()[0];
- if (sel) {
- if (['up-move', 'down-move'].indexOf(type) > -1) {
- sheet.setSelection(payTree.getNodeIndex(node), sel.col, sel.rowCount, sel.colCount);
- SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(node)]);
- } else if (type === 'add') {
- sheet.setSelection(payTree.getNodeIndex(refreshData.create[0]), sel.col, sel.rowCount, sel.colCount);
- SpreadJsObj.reloadRowsBackColor(sheet, [sel.row, payTree.getNodeIndex(refreshData.create[0])]);
- }
- }
- self.refreshOperationValid(sheet);
- });
- }
- },
- reloadPays: function(datas){
- payTree.loadDatas(datas);
- SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Tree, payTree);
- payEvent.refreshActn();
- }
- };
- spread.bind(spreadNS.Events.SelectionChanged, payEvent.selectionChanged);
- if (!readOnly) {
- spread.bind(spreadNS.Events.EditStarting, payEvent.editStarting);
- spread.bind(spreadNS.Events.EditEnded, payEvent.editEnded);
- $('a[name="base-opr"]').click(function () {
- payEvent.baseOpr(this.getAttribute('type'));
- });
- }
- return { spread, sheet, payTree, loadDatas: payEvent.reloadPays }
- })();
- payObj.loadDatas(details);
- // todo 加载审批列表
- $.subMenu({
- menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
- toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
- key: 'menu.1.0.0',
- miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
- callback: function (info) {
- if (info.mini) {
- $('.panel-title').addClass('fluid');
- $('#sub-menu').removeClass('panel-sidebar');
- } else {
- $('.panel-title').removeClass('fluid');
- $('#sub-menu').addClass('panel-sidebar');
- }
- autoFlashHeight();
- payObj.spread.refresh();
- }
- });
- });
|