settle_select.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. function getGxbyText(data) {
  2. const def = thirdParty.gxby.find(function (x) {
  3. return x.value === data.gxby_status;
  4. });
  5. return def ? def.name : '';
  6. }
  7. function getDaglText(data) {
  8. const def = thirdParty.dagl.find(function (x) {
  9. return x.value === data.dagl_status;
  10. });
  11. return def ? def.name : '';
  12. }
  13. const ckBillsSpread = window.location.pathname + '-billsSelect';
  14. $(document).ready(() => {
  15. autoFlashHeight();
  16. let searchLedger;
  17. const settleTreeSetting = {
  18. id: 'ledger_id',
  19. pid: 'ledger_pid',
  20. order: 'order',
  21. level: 'level',
  22. rootId: -1,
  23. keys: ['id', 'tender_id', 'ledger_id'],
  24. stageId: 'id',
  25. autoExpand: 3,
  26. markExpandKey: 'settle-select-expand',
  27. markExpandSubKey: window.location.pathname.split('/')[2],
  28. calcFields: ['total_price', 'end_contract_tp', 'end_qc_tp', 'end_gather_tp', 'end_correct_tp'],
  29. calcFun: function(node) {
  30. if (!node.children || node.children.length === 0) {
  31. node.end_gather_qty = ZhCalc.add(node.end_contract_qty, node.end_qc_qty);
  32. if (node.end_contract_qty) {
  33. node.end_correct_tp = ZhCalc.add(node.end_qc_tp, ZhCalc.mul(node.end_contract_qty, node.unit_price, tenderInfo.decimal.tp));
  34. } else {
  35. node.end_correct_tp = node.end_gather_tp;
  36. }
  37. }
  38. node.end_gather_tp = ZhCalc.add(node.end_contract_tp, node.end_qc_tp);
  39. node.end_gather_percent = ZhCalc.mul(ZhCalc.div(node.end_gather_tp, node.end_final_tp), 100, 2);
  40. node.end_correct_percent = ZhCalc.mul(ZhCalc.div(node.end_correct_tp, node.end_final_tp), 100, 2);
  41. }
  42. };
  43. const settleTree = createNewPathTree('stage', settleTreeSetting);
  44. const settlePosSetting = {
  45. id: 'id', ledgerId: 'lid',
  46. calcFun: function(pos) {
  47. pos.end_gather_qty = ZhCalc.add(pos.end_contract_qty, pos.end_qc_qty);
  48. pos.sum = ZhCalc.add(pos.end_qc_qty, pos.quantity);
  49. pos.end_gather_percent = ZhCalc.mul(ZhCalc.div(pos.end_gather_qty, pos.sum), 100, 2);
  50. }
  51. };
  52. const settlePos = new StagePosData(settlePosSetting);
  53. const slSpread = SpreadJsObj.createNewSpread($('#settle-bills')[0]);
  54. const slSheet = slSpread.getActiveSheet();
  55. slSheet.frozenColumnCount(billsSpreadSetting.cols.findIndex(x => { return x.field === 'total_price'; }) + 1);
  56. slSheet.options.frozenlineColor = '#93b5e4';
  57. const ratioCol = billsSpreadSetting.cols.find(x => {return x.field === 'end_final_1_percent' || x.field === 'end_correct_1_percent'});
  58. if (ratioCol) ratioCol.field = tenderInfo.display.stage.correct ? 'end_correct_1_percent' : 'end_final_1_percent';
  59. billsSpreadSetting.getColor = function (sheet, data, row, col, defaultColor) {
  60. if (!data) return defaultColor;
  61. if (data.children && data.children.length > 0) return defaultColor;
  62. if (col.field === 'gxby') {
  63. const def = thirdParty.gxby.find(function (x) {
  64. return x.value === data.gxby_status;
  65. });
  66. if (def && def.color) return def.color;
  67. } else if (col.field === 'dagl') {
  68. const def = thirdParty.dagl.find(function (x) {
  69. return x.value === data.dagl_status;
  70. });
  71. if (def && def.color) return def.color;
  72. }
  73. };
  74. sjsSettingObj.setFxTreeStyle(billsSpreadSetting, sjsSettingObj.FxTreeStyle.jz);
  75. sjsSettingObj.set3FCols(billsSpreadSetting.cols, [
  76. {field: 'gxby', getValue: getGxbyText, url_field: 'gxby_url'},
  77. {field: 'dagl', getValue: getDaglText, url_field: 'dagl_url'},
  78. ]);
  79. billsSpreadSetting.getColor = function (sheet, data, row, col, defaultColor) {
  80. if (data) {
  81. if (col.field === 'gxby') {
  82. const def = thirdParty.gxby.find(function (x) {
  83. return x.value === data.gxby_status;
  84. });
  85. if (def && def.color) return def.color;
  86. } else if (col.field === 'dagl') {
  87. const def = thirdParty.dagl.find(function (x) {
  88. return x.value === data.dagl_status;
  89. });
  90. if (def && def.color) return def.color;
  91. }
  92. return data.selected && data.undone ? spreadColor.stage.over : defaultColor;
  93. } else {
  94. return defaultColor;
  95. }
  96. };
  97. SpreadJsObj.initSheet(slSheet, billsSpreadSetting);
  98. const spSpread = SpreadJsObj.createNewSpread($('#settle-pos')[0]);
  99. const spSheet = spSpread.getActiveSheet();
  100. spSheet.frozenColumnCount(posSpreadSetting.cols.findIndex(x => { return x.field === 'total_price'; }) + 1);
  101. spSheet.options.frozenlineColor = '#93b5e4';
  102. posSpreadSetting.getColor = function (sheet, data, row, col, defaultColor) {
  103. if (!data) return defaultColor;
  104. if (col.field === 'gxby') {
  105. const def = thirdParty.gxby.find(function (x) {
  106. return x.value === data.gxby_status;
  107. });
  108. if (def && def.color) return def.color;
  109. } else if (col.field === 'dagl') {
  110. const def = thirdParty.dagl.find(function (x) {
  111. return x.value === data.dagl_status;
  112. });
  113. if (def && def.color) return def.color;
  114. }
  115. };
  116. sjsSettingObj.set3FCols(posSpreadSetting.cols, [
  117. {field: 'gxby', getValue: getGxbyText, url_field: 'gxby_url'},
  118. {field: 'dagl', getValue: getDaglText, url_field: 'dagl_url'},
  119. ]);
  120. posSpreadSetting.getColor = function (sheet, data, row, col, defaultColor) {
  121. if (data) {
  122. if (col.field === 'gxby') {
  123. const def = thirdParty.gxby.find(function (x) {
  124. return x.value === data.gxby_status;
  125. });
  126. if (def && def.color) return def.color;
  127. } else if (col.field === 'dagl') {
  128. const def = thirdParty.dagl.find(function (x) {
  129. return x.value === data.dagl_status;
  130. });
  131. if (def && def.color) return def.color;
  132. }
  133. return data.selected && data.undone ? spreadColor.stage.over : defaultColor;
  134. } else {
  135. return defaultColor;
  136. }
  137. };
  138. SpreadJsObj.initSheet(spSheet, posSpreadSetting);
  139. const settleCheck = {
  140. _analysisPos(pos) {
  141. pos.undoneDeal = pos.quantity ? !checkZero(ZhCalc.sub(pos.end_contract_qty, pos.quantity)) : false;
  142. pos.undone = !!pos.undoneDeal || !!pos.undoneChange;
  143. },
  144. _analysisNode(node) {
  145. if (node.undoneDeal === undefined) node.undoneDeal = false;
  146. if (node.undoneChange === undefined) node.undoneChange = false;
  147. if (node.children && node.children.length > 0) {
  148. for (const child of node.children) {
  149. this._analysisNode(child);
  150. if (child.undoneDeal) node.undoneDeal = true;
  151. if (child.undoneChange) node.undoneChange = true;
  152. }
  153. node.undone = !!node.undoneDeal || !!node.undoneChange;
  154. } else {
  155. const posRange = settlePos.getLedgerPos(node.id);
  156. if (posRange && posRange.length > 0) {
  157. for (const pos of posRange) {
  158. this._analysisPos(pos);
  159. if (pos.undoneDeal) node.undoneDeal = true;
  160. if (pos.undoneChange) node.undoneChange = true;
  161. }
  162. node.undone = !!node.undoneDeal || !!node.undoneChange;
  163. } else {
  164. node.undoneDeal = node.end_contract_qty
  165. ? !checkZero(ZhCalc.sub(node.end_contract_qty, node.quantity))
  166. : !checkZero(ZhCalc.sub(node.end_contract_tp, node.total_price));
  167. node.undone = !!node.undoneDeal || !!node.undoneChange;
  168. }
  169. }
  170. },
  171. init() {
  172. for (const node of settleTree.children) {
  173. this._analysisNode(node);
  174. }
  175. }
  176. };
  177. const settleBillsObj = {
  178. loadRelaData: function() {
  179. SpreadJsObj.saveTopAndSelect(slSheet, ckBillsSpread);
  180. SpreadJsObj.resetTopAndSelect(spSheet);
  181. settlePosObj.loadCurPosData();
  182. },
  183. selectionChanged: function(e, info) {
  184. if (!info.oldSelections || !info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  185. settleBillsObj.loadRelaData();
  186. }
  187. },
  188. topRowChanged(e, info) {
  189. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  190. },
  191. buttonClicked: function(e, info) {
  192. if (!info.sheet.zh_setting) return;
  193. const col = info.sheet.zh_setting.cols[info.col];
  194. if (col.field !== 'selected') return;
  195. const node = SpreadJsObj.getSelectObject(info.sheet);
  196. if (node.undone && !node.selected) {
  197. let msg = '选择的节点不可结算';
  198. if (node.undoneDeal) msg = msg + ',合同未计量完';
  199. if (node.undoneChange) msg = msg + ',变更令未调用完';
  200. toastr.warning(msg);
  201. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  202. return;
  203. }
  204. if (!node.selected) {
  205. const parents = settleTree.getAllParents(node);
  206. for (const p of parents) {
  207. if (p.selected) {
  208. toastr.warning(`父项${p.code || ''}以勾选,勿需重复勾选子项`);
  209. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  210. return;
  211. }
  212. }
  213. }
  214. const update = {};
  215. if (!node.selected) {
  216. update.add = [{ lid: node.id }];
  217. const posterity = settleTree.getPosterity(node);
  218. for (const p of posterity) {
  219. if (p.selected) {
  220. if (!update.del) update.del = [];
  221. update.del.push({ lid: node.id });
  222. }
  223. if (!p.children || p.children.length === 0) {
  224. const posRange = settlePos.getLedgerPos(p.id);
  225. if (posRange && posRange.length > 0) {
  226. for (const p of posRange) {
  227. if (p.selected) {
  228. if (!update.del) update.del = [];
  229. update.del.push({ pid: p.id });
  230. }
  231. }
  232. }
  233. }
  234. }
  235. } else {
  236. update.del = [{ lid: node.id }];
  237. }
  238. postData(window.location.pathname + '/update', update, result => {
  239. node.selected = !node.selected;
  240. if (result.del) {
  241. let refreshRow = [], refreshPos = false;
  242. for (const d of result.del) {
  243. if (d.lid) {
  244. const sbi = settleTree.nodes.findIndex(x => { return x.id === d.lid; });
  245. settleTree.nodes[sbi].selected = false;
  246. refreshRow.push(sbi);
  247. } else if (d.pid) {
  248. const sp = settlePos.getPos(d.pid);
  249. if (sp) {
  250. sp.selected = false;
  251. if (sp.lid === node.id) refreshPos = false;
  252. }
  253. }
  254. }
  255. if (refreshRow.length > 0) SpreadJsObj.reLoadRowsData(info.sheet, refreshRow);
  256. if (refreshPos) settlePosObj.loadCurPosData();
  257. }
  258. }, () => {
  259. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  260. });
  261. },
  262. };
  263. slSpread.bind(spreadNS.Events.SelectionChanged, settleBillsObj.selectionChanged);
  264. slSpread.bind(spreadNS.Events.TopRowChanged, settleBillsObj.topRowChanged);
  265. if (!readOnly) {
  266. slSpread.bind(spreadNS.Events.ButtonClicked, settleBillsObj.buttonClicked);
  267. }
  268. const settlePosObj = {
  269. loadCurPosData: function() {
  270. const billsNode = SpreadJsObj.getSelectObject(slSheet);
  271. if (billsNode) {
  272. spSheet.zh_setting.readOnly = readOnly;
  273. const posRange = settlePos.getLedgerPos(billsNode.id) || [];
  274. SpreadJsObj.loadSheetData(spSheet, SpreadJsObj.DataType.Data, posRange, readOnly);
  275. if (posRange.length > 0) SpreadJsObj.locateData(spSheet, posRange[0]);
  276. } else {
  277. spSheet.zh_setting.readOnly = true;
  278. SpreadJsObj.loadSheetData(spSheet, SpreadJsObj.DataType.Data, [], true);
  279. }
  280. },
  281. buttonClicked: function(e, info) {
  282. if (!info.sheet.zh_setting) return;
  283. const col = info.sheet.zh_setting.cols[info.col];
  284. if (col.field !== 'selected') return;
  285. const node = SpreadJsObj.getSelectObject(info.sheet);
  286. if (node.undone && !node.selected) {
  287. let msg = '选择的节点不可结算';
  288. if (node.undoneDeal) msg = msg + ',合同未计量完';
  289. if (node.undoneChange) msg = msg + ',变更令未调用完';
  290. toastr.warning(msg);
  291. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  292. return;
  293. }
  294. if (!node.selected) {
  295. const billsNode = settleTree.nodes.find(x => { return x.id === node.lid });
  296. const parents = settleTree.getAllParents(billsNode);
  297. for (const p of parents) {
  298. if (p.selected) {
  299. toastr.warning(`所属清单的父项${p.code || ''}以勾选,勿需重复勾选计量单元`);
  300. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  301. return;
  302. }
  303. }
  304. }
  305. const update = {};
  306. if (!node.selected) {
  307. update.add = [{ pid: node.id }];
  308. } else {
  309. update.del = [{ pid: node.id }];
  310. }
  311. postData(window.location.pathname + '/update', update, result => {
  312. node.selected = !node.selected;
  313. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  314. }, () => {
  315. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  316. });
  317. },
  318. };
  319. if (!readOnly) {
  320. spSpread.bind(spreadNS.Events.ButtonClicked, settlePosObj.buttonClicked);
  321. }
  322. postData('load', {filter: 'stageBills;stagePos;settleSelect;tag;settleChange'}, function(result) {
  323. for (const select of result.settleSelect) {
  324. if (select.pid) {
  325. const sp = result.stagePos.find(x => { return x.id === select.pid });
  326. if (sp) sp.selected = true;
  327. } else {
  328. const sb = result.stageBills.find(x => { return x.id === select.lid });
  329. if (sb) sb.selected = true;
  330. }
  331. }
  332. for (const change of result.settleChange) {
  333. const sb = result.stageBills.find(x => { return x.id === change.gcl_id });
  334. if (sb) sb.undoneChange = true;
  335. if (change.mx_id) {
  336. const sp = result.stagePos.find(x => { return x.id === change.mx_id });
  337. if (sp) sp.undoneChange = true;
  338. }
  339. }
  340. settleTree.loadDatas(result.stageBills);
  341. treeCalc.calculateAll(settleTree);
  342. settlePos.loadDatas(result.stagePos);
  343. settlePos.calculateAll();
  344. settleCheck.init();
  345. SpreadJsObj.loadSheetData(slSheet, SpreadJsObj.DataType.Tree, settleTree);
  346. SpreadJsObj.loadTopAndSelect(slSpread.getActiveSheet(), ckBillsSpread);
  347. settlePosObj.loadCurPosData();
  348. });
  349. // 展开收起工具栏
  350. $('a', '.right-nav').bind('click', function () {
  351. const tab = $(this), tabPanel = $(tab.attr('content'));
  352. if (!tab.hasClass('active')) {
  353. $('a', '.side-menu').removeClass('active');
  354. $('.tab-content .tab-select-show').removeClass('active');
  355. tab.addClass('active');
  356. tabPanel.addClass('active');
  357. showSideTools(tab.hasClass('active'));
  358. if (tab.attr('content') === '#search' && !searchLedger) {
  359. searchLedger = $.billsSearch({
  360. selector: '#search',
  361. searchSpread: slSpread,
  362. searchOver: true,
  363. searchEmpty: true,
  364. resultSpreadSetting: {
  365. cols: [
  366. {title: '项目节编号', field: 'code', hAlign: 0, width: 90, formatter: '@'},
  367. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@'},
  368. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@'},
  369. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@'},
  370. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  371. {title: '数量', field: 'quantity', hAlign: 2, width: 50},
  372. ],
  373. emptyRows: 0,
  374. headRows: 1,
  375. headRowHeight: [32],
  376. headColWidth: [30],
  377. defaultRowHeight: 21,
  378. headerFont: '12px 微软雅黑',
  379. font: '12px 微软雅黑',
  380. selectedBackColor: '#fffacd',
  381. readOnly: true,
  382. },
  383. afterLocated: function () {
  384. settlePosObj.loadCurPosData();
  385. },
  386. customSearch: [
  387. {
  388. key: 'err', title: '结算错误', valid: true, parent: true,
  389. check: function (node) {
  390. const billsError = !!node.selected && node.undone;
  391. if (billsError) return billsError;
  392. const posRange = !node.children || node.children.length === 0 ? (settlePos.getLedgerPos(node.id) || []) : [];
  393. for (const pos of posRange) {
  394. if (!!pos.selected && pos.undone) return true;
  395. }
  396. return false;
  397. }
  398. },
  399. ],
  400. });
  401. searchLedger.spread.refresh();
  402. }
  403. } else {
  404. tab.removeClass('active');
  405. tabPanel.removeClass('active');
  406. showSideTools(tab.hasClass('active'));
  407. }
  408. slSpread.refresh();
  409. spSpread.refresh();
  410. });
  411. $.subMenu({
  412. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  413. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  414. key: 'menu.1.0.0',
  415. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  416. callback: function (info) {
  417. if (info.mini) {
  418. $('.panel-title').addClass('fluid');
  419. $('#sub-menu').removeClass('panel-sidebar');
  420. } else {
  421. $('.panel-title').removeClass('fluid');
  422. $('#sub-menu').addClass('panel-sidebar');
  423. }
  424. autoFlashHeight();
  425. }
  426. });
  427. // 加载上下窗口resizer
  428. $.divResizer({
  429. select: '#main-resize',
  430. callback: function () {
  431. slSpread.refresh();
  432. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  433. $(".sp-wrap").height(bcontent-30);
  434. spSpread.refresh();
  435. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  436. }
  437. });
  438. // 工具栏resizer
  439. $.divResizer({
  440. select: '#right-spr',
  441. callback: function () {
  442. slSpread.refresh();
  443. spSpread.refresh();
  444. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  445. }
  446. });
  447. // 显示层次
  448. (function (select, sheet) {
  449. $(select).click(function () {
  450. const tag = $(this).attr('tag');
  451. const tree = sheet.zh_tree;
  452. if (!tree) return;
  453. setTimeout(() => {
  454. showWaitingView();
  455. switch (tag) {
  456. case "1":
  457. case "2":
  458. case "3":
  459. case "4":
  460. case "5":
  461. tree.expandByLevel(parseInt(tag));
  462. SpreadJsObj.refreshTreeRowVisible(sheet);
  463. break;
  464. case "last":
  465. tree.expandByCustom(() => { return true; });
  466. SpreadJsObj.refreshTreeRowVisible(sheet);
  467. break;
  468. }
  469. closeWaitingView();
  470. }, 100);
  471. });
  472. })('a[name=showLevel]', slSheet);
  473. });