settle_select.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. if (pos.settle_status === undefined) pos.settle_status = 0;
  142. pos.undoneDeal = pos.quantity ? !checkZero(ZhCalc.sub(pos.end_contract_qty, pos.quantity)) : false;
  143. pos.undone = !!pos.undoneDeal || !!pos.undoneChange;
  144. },
  145. _analysisNode(node) {
  146. if (node.undoneDeal === undefined) node.undoneDeal = false;
  147. if (node.undoneChange === undefined) node.undoneChange = false;
  148. if (node.settle_status === undefined) node.settle_status = 0;
  149. const status = [];
  150. if (node.children && node.children.length > 0) {
  151. for (const child of node.children) {
  152. this._analysisNode(child);
  153. if (child.undoneDeal) node.undoneDeal = true;
  154. if (child.undoneChange) node.undoneChange = true;
  155. if (status.indexOf(child.settle_status) < 0) status.push(child.settle_status);
  156. }
  157. node.undone = !!node.undoneDeal || !!node.undoneChange;
  158. node.settle_status = status.length === 1 ? status[0] : 1;
  159. } else {
  160. const posRange = settlePos.getLedgerPos(node.id);
  161. if (posRange && posRange.length > 0) {
  162. for (const pos of posRange) {
  163. this._analysisPos(pos);
  164. if (pos.undoneDeal) node.undoneDeal = true;
  165. if (pos.undoneChange) node.undoneChange = true;
  166. if (status.indexOf(pos.settle_status) < 0) status.push(pos.settle_status);
  167. }
  168. node.undone = !!node.undoneDeal || !!node.undoneChange;
  169. node.settle_status = status.length === 1 ? status[0] : 1;
  170. } else {
  171. node.undoneDeal = node.end_contract_qty
  172. ? !checkZero(ZhCalc.sub(node.end_contract_qty, node.quantity))
  173. : !checkZero(ZhCalc.sub(node.end_contract_tp, node.total_price));
  174. node.undone = !!node.undoneDeal || !!node.undoneChange;
  175. }
  176. }
  177. },
  178. init() {
  179. for (const node of settleTree.children) {
  180. this._analysisNode(node);
  181. }
  182. }
  183. };
  184. const settleBillsObj = {
  185. loadRelaData: function() {
  186. SpreadJsObj.saveTopAndSelect(slSheet, ckBillsSpread);
  187. SpreadJsObj.resetTopAndSelect(spSheet);
  188. settlePosObj.loadCurPosData();
  189. },
  190. selectionChanged: function(e, info) {
  191. if (!info.oldSelections || !info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  192. settleBillsObj.loadRelaData();
  193. }
  194. },
  195. topRowChanged(e, info) {
  196. SpreadJsObj.saveTopAndSelect(info.sheet, ckBillsSpread);
  197. },
  198. buttonClicked: function(e, info) {
  199. if (!info.sheet.zh_setting) return;
  200. const col = info.sheet.zh_setting.cols[info.col];
  201. if (col.field !== 'selected') return;
  202. const node = SpreadJsObj.getSelectObject(info.sheet);
  203. if (node.undone && !node.selected) {
  204. let msg = '选择的节点不可结算';
  205. if (node.undoneDeal) msg = msg + ',合同未计量完';
  206. if (node.undoneChange) msg = msg + ',变更令未调用完';
  207. toastr.warning(msg);
  208. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  209. return;
  210. }
  211. if (node.settle_status === 2) {
  212. toastr.warning('已结算数据,请勿重复结算');
  213. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  214. return;
  215. }
  216. if (!node.selected) {
  217. const parents = settleTree.getAllParents(node);
  218. for (const p of parents) {
  219. if (p.selected) {
  220. toastr.warning(`父项${p.code || ''}以勾选,勿需重复勾选子项`);
  221. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  222. return;
  223. }
  224. }
  225. }
  226. const update = {};
  227. if (!node.selected) {
  228. update.add = [{ lid: node.id }];
  229. const posterity = settleTree.getPosterity(node);
  230. for (const p of posterity) {
  231. if (p.selected) {
  232. if (!update.del) update.del = [];
  233. update.del.push({ lid: p.id });
  234. }
  235. if (!p.children || p.children.length === 0) {
  236. const posRange = settlePos.getLedgerPos(p.id);
  237. if (posRange && posRange.length > 0) {
  238. for (const p of posRange) {
  239. if (p.selected) {
  240. if (!update.del) update.del = [];
  241. update.del.push({ pid: p.id });
  242. }
  243. }
  244. }
  245. }
  246. }
  247. } else {
  248. update.del = [{ lid: node.id }];
  249. }
  250. postData(window.location.pathname + '/update', update, result => {
  251. node.selected = !node.selected;
  252. let refreshRow = [info.row], refreshPos = false;
  253. if (result.del) {
  254. for (const d of result.del) {
  255. if (d.lid) {
  256. const sbi = settleTree.nodes.findIndex(x => { return x.id === d.lid; });
  257. settleTree.nodes[sbi].selected = false;
  258. refreshRow.push(sbi);
  259. } else if (d.pid) {
  260. const sp = settlePos.getPos(d.pid);
  261. if (sp) {
  262. sp.selected = false;
  263. if (sp.lid === node.id) refreshPos = false;
  264. }
  265. }
  266. }
  267. }
  268. if (refreshRow.length > 0) SpreadJsObj.reLoadRowsData(info.sheet, refreshRow);
  269. if (refreshPos) settlePosObj.loadCurPosData();
  270. }, () => {
  271. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  272. });
  273. },
  274. };
  275. slSpread.bind(spreadNS.Events.SelectionChanged, settleBillsObj.selectionChanged);
  276. slSpread.bind(spreadNS.Events.TopRowChanged, settleBillsObj.topRowChanged);
  277. if (!readOnly) {
  278. slSpread.bind(spreadNS.Events.ButtonClicked, settleBillsObj.buttonClicked);
  279. }
  280. const settlePosObj = {
  281. loadCurPosData: function() {
  282. const billsNode = SpreadJsObj.getSelectObject(slSheet);
  283. if (billsNode) {
  284. spSheet.zh_setting.readOnly = readOnly;
  285. const posRange = settlePos.getLedgerPos(billsNode.id) || [];
  286. SpreadJsObj.loadSheetData(spSheet, SpreadJsObj.DataType.Data, posRange, readOnly);
  287. if (posRange.length > 0) SpreadJsObj.locateData(spSheet, posRange[0]);
  288. } else {
  289. spSheet.zh_setting.readOnly = true;
  290. SpreadJsObj.loadSheetData(spSheet, SpreadJsObj.DataType.Data, [], true);
  291. }
  292. },
  293. buttonClicked: function(e, info) {
  294. if (!info.sheet.zh_setting) return;
  295. const col = info.sheet.zh_setting.cols[info.col];
  296. if (col.field !== 'selected') return;
  297. const node = SpreadJsObj.getSelectObject(info.sheet);
  298. if (node.undone && !node.selected) {
  299. let msg = '选择的节点不可结算';
  300. if (node.undoneDeal) msg = msg + ',合同未计量完';
  301. if (node.undoneChange) msg = msg + ',变更令未调用完';
  302. toastr.warning(msg);
  303. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  304. return;
  305. }
  306. if (node.settle_status === 2) {
  307. toastr.warning('已结算数据,请勿重复结算');
  308. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  309. return;
  310. }
  311. if (!node.selected) {
  312. const billsNode = settleTree.nodes.find(x => { return x.id === node.lid });
  313. const parents = settleTree.getAllParents(billsNode);
  314. for (const p of parents) {
  315. if (p.selected) {
  316. toastr.warning(`所属清单的父项${p.code || ''}以勾选,勿需重复勾选计量单元`);
  317. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  318. return;
  319. }
  320. }
  321. }
  322. const update = {};
  323. if (!node.selected) {
  324. update.add = [{ pid: node.id }];
  325. } else {
  326. update.del = [{ pid: node.id }];
  327. }
  328. postData(window.location.pathname + '/update', update, result => {
  329. node.selected = !node.selected;
  330. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  331. }, () => {
  332. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  333. });
  334. },
  335. };
  336. if (!readOnly) {
  337. spSpread.bind(spreadNS.Events.ButtonClicked, settlePosObj.buttonClicked);
  338. }
  339. postData('load', {filter: 'stageBills;stagePos;settleSelect;tag;settleChange'}, function(result) {
  340. for (const select of result.settleSelect) {
  341. if (select.pid) {
  342. const sp = result.stagePos.find(x => { return x.id === select.pid });
  343. if (sp) sp.selected = true;
  344. } else {
  345. const sb = result.stageBills.find(x => { return x.id === select.lid });
  346. if (sb) sb.selected = true;
  347. }
  348. }
  349. for (const change of result.settleChange) {
  350. const sb = result.stageBills.find(x => { return x.id === change.gcl_id });
  351. if (sb) sb.undoneChange = true;
  352. if (change.mx_id) {
  353. const sp = result.stagePos.find(x => { return x.id === change.mx_id });
  354. if (sp) sp.undoneChange = true;
  355. }
  356. }
  357. settleTree.loadDatas(result.stageBills);
  358. treeCalc.calculateAll(settleTree);
  359. settlePos.loadDatas(result.stagePos);
  360. settlePos.calculateAll();
  361. settleCheck.init();
  362. SpreadJsObj.loadSheetData(slSheet, SpreadJsObj.DataType.Tree, settleTree);
  363. SpreadJsObj.loadTopAndSelect(slSpread.getActiveSheet(), ckBillsSpread);
  364. settlePosObj.loadCurPosData();
  365. });
  366. // 展开收起工具栏
  367. $('a', '.right-nav').bind('click', function () {
  368. const tab = $(this), tabPanel = $(tab.attr('content'));
  369. if (!tab.hasClass('active')) {
  370. $('a', '.side-menu').removeClass('active');
  371. $('.tab-content .tab-select-show').removeClass('active');
  372. tab.addClass('active');
  373. tabPanel.addClass('active');
  374. showSideTools(tab.hasClass('active'));
  375. if (tab.attr('content') === '#search' && !searchLedger) {
  376. searchLedger = $.billsSearch({
  377. selector: '#search',
  378. searchSpread: slSpread,
  379. searchOver: true,
  380. searchEmpty: true,
  381. resultSpreadSetting: {
  382. cols: [
  383. {title: '项目节编号', field: 'code', hAlign: 0, width: 90, formatter: '@'},
  384. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@'},
  385. {title: '名称', field: 'name', width: 150, hAlign: 0, formatter: '@'},
  386. {title: '单位', field: 'unit', width: 50, hAlign: 1, formatter: '@'},
  387. {title: '单价', field: 'unit_price', hAlign: 2, width: 50},
  388. {title: '数量', field: 'quantity', hAlign: 2, width: 50},
  389. ],
  390. emptyRows: 0,
  391. headRows: 1,
  392. headRowHeight: [32],
  393. headColWidth: [30],
  394. defaultRowHeight: 21,
  395. headerFont: '12px 微软雅黑',
  396. font: '12px 微软雅黑',
  397. selectedBackColor: '#fffacd',
  398. readOnly: true,
  399. },
  400. afterLocated: function () {
  401. settlePosObj.loadCurPosData();
  402. },
  403. customSearch: [
  404. {
  405. key: 'err', title: '结算错误', valid: true, parent: true,
  406. check: function (node) {
  407. const billsError = !!node.selected && node.undone;
  408. if (billsError) return billsError;
  409. const posRange = !node.children || node.children.length === 0 ? (settlePos.getLedgerPos(node.id) || []) : [];
  410. for (const pos of posRange) {
  411. if (!!pos.selected && pos.undone) return true;
  412. }
  413. return false;
  414. }
  415. },
  416. ],
  417. });
  418. searchLedger.spread.refresh();
  419. }
  420. } else {
  421. tab.removeClass('active');
  422. tabPanel.removeClass('active');
  423. showSideTools(tab.hasClass('active'));
  424. }
  425. slSpread.refresh();
  426. spSpread.refresh();
  427. });
  428. $.subMenu({
  429. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  430. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  431. key: 'menu.1.0.0',
  432. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  433. callback: function (info) {
  434. if (info.mini) {
  435. $('.panel-title').addClass('fluid');
  436. $('#sub-menu').removeClass('panel-sidebar');
  437. } else {
  438. $('.panel-title').removeClass('fluid');
  439. $('#sub-menu').addClass('panel-sidebar');
  440. }
  441. autoFlashHeight();
  442. }
  443. });
  444. // 加载上下窗口resizer
  445. $.divResizer({
  446. select: '#main-resize',
  447. callback: function () {
  448. slSpread.refresh();
  449. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  450. $(".sp-wrap").height(bcontent-30);
  451. spSpread.refresh();
  452. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  453. }
  454. });
  455. // 工具栏resizer
  456. $.divResizer({
  457. select: '#right-spr',
  458. callback: function () {
  459. slSpread.refresh();
  460. spSpread.refresh();
  461. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  462. if (searchLedger) searchLedger.spread.refresh();
  463. }
  464. });
  465. // 显示层次
  466. (function (select, sheet) {
  467. $(select).click(function () {
  468. const tag = $(this).attr('tag');
  469. const tree = sheet.zh_tree;
  470. if (!tree) return;
  471. setTimeout(() => {
  472. showWaitingView();
  473. switch (tag) {
  474. case "1":
  475. case "2":
  476. case "3":
  477. case "4":
  478. case "5":
  479. tree.expandByLevel(parseInt(tag));
  480. SpreadJsObj.refreshTreeRowVisible(sheet);
  481. break;
  482. case "last":
  483. tree.expandByCustom(() => { return true; });
  484. SpreadJsObj.refreshTreeRowVisible(sheet);
  485. break;
  486. }
  487. closeWaitingView();
  488. }, 100);
  489. });
  490. })('a[name=showLevel]', slSheet);
  491. });