material_exponent.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. const is_numeric = (value) => {
  2. if (typeof(value) === 'object') {
  3. return false;
  4. } else {
  5. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  6. }
  7. };
  8. function getPasteHint (str, row = '') {
  9. let returnObj = str;
  10. if (row) {
  11. returnObj.msg = '指数清单第' + (row+1) + '行' + str.msg;
  12. }
  13. return returnObj;
  14. }
  15. function resetExTpTable() {
  16. const rate = $('#changeRate').val();
  17. const bqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), 2);
  18. const jzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, bqhs), 2);
  19. $('#tp_set').find('td').eq(3).text(ZhCalc.round(ex_tp, 2));
  20. $('#tp_set').find('td').eq(4).text(ZhCalc.round(ZhCalc.add(ex_pre_tp, ex_tp), 2));
  21. $('#rate_set').find('td').eq(3).text(bqhs !== 0 ? bqhs : '');
  22. $('#rate_set').find('td').eq(4).text(jzbqhs !== 0 ? jzbqhs : '');
  23. $('#ex_expr').html(ex_expr);
  24. }
  25. $(document).ready(() => {
  26. autoFlashHeight();
  27. const materialExponentSpread = SpreadJsObj.createNewSpread($('#material-exponent-spread')[0]);
  28. const materialExponentSpreadSetting = {
  29. cols: [
  30. {title: '类型', colSpan: '1', rowSpan: '2', field: 'type', hAlign: 1, width: 60, formatter: '@', readOnly: true,cellType: 'customizeCombo', comboItems: materialType.ex_type, cellTypeKey: 1},
  31. {title: '符号', colSpan: '1', rowSpan: '2', field: 'symbol', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  32. {title: '符号说明', colSpan: '1', rowSpan: '2', field: 'symbol_desc', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  33. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  34. {title: '加权系数', colSpan: '1', rowSpan: '2', field: 'weight_num', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isConstant'},
  35. {title: '基本价格指数', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 120, readOnly: 'readOnly.isEdit'},
  36. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.isEdit'},
  37. {title: '现行价格指数', colSpan: '1', rowSpan: '2', field: 'm_price', hAlign: 2, width: 120, type: 'Number', readOnly: 'readOnly.remark'},
  38. {title: '计算值', colSpan: '1', rowSpan: '2', field: 'calc_num', hAlign: 2, width: 80, formatter: '@', readOnly: true},
  39. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.remark'},
  40. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'},
  41. ],
  42. emptyRows: 0,
  43. headRows: 2,
  44. headRowHeight: [25, 25],
  45. defaultRowHeight: 21,
  46. headerFont: '12px 微软雅黑',
  47. font: '12px 微软雅黑',
  48. readOnly: readOnly,
  49. };
  50. const materialExponentBase = {
  51. isUsed: function (data) {
  52. if (data.type === 2) {
  53. return data.mid === materialID || data.weight_num === null;
  54. } else {
  55. return false;
  56. }
  57. },
  58. isEdit: function (data) {
  59. return data.mid === materialID && data.type === 2;
  60. },
  61. isConstant: function (data) {
  62. return (materialOrder === 1 && data.type === 1) || (data.mid === materialID && data.type === 2);
  63. }
  64. };
  65. const materialExponentCol = {
  66. getValue: {
  67. calc_num : function (data) {
  68. const calc_num = data.basic_price > 0 ? ZhCalc.mul(data.weight_num, ZhCalc.div(data.m_price, data.basic_price)) : 0;
  69. return calc_num > 0 ? ZhCalc.round(calc_num, 3) : 0;
  70. },
  71. },
  72. readOnly: {
  73. isEdit: function (data) {
  74. return !(!readOnly && materialExponentBase.isEdit(data));
  75. },
  76. isUsed: function (data) {
  77. return !(!readOnly && materialExponentBase.isUsed(data));
  78. },
  79. remark: function (data) {
  80. return !(!readOnly && data.type === 2);
  81. },
  82. isConstant: function (data) {
  83. return !(!readOnly && materialExponentBase.isConstant(data));
  84. }
  85. },
  86. };
  87. SpreadJsObj.initSpreadSettingEvents(materialExponentSpreadSetting, materialExponentCol);
  88. SpreadJsObj.initSheet(materialExponentSpread.getActiveSheet(), materialExponentSpreadSetting);
  89. SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialExponentData);
  90. const materialExponentSpreadObj = {
  91. refreshActn: function (rowCount = 1) {
  92. const setObjEnable = function (obj, enable) {
  93. if (enable) {
  94. obj.removeClass('disabled');
  95. } else {
  96. obj.addClass('disabled');
  97. }
  98. };
  99. const sheet = materialExponentSpread.getActiveSheet();
  100. const select = SpreadJsObj.getSelectObject(sheet);
  101. // 还需判断是否已被调差清单调用
  102. setObjEnable($('#del'), !readOnly && select && materialExponentBase.isUsed(select) && rowCount === 1);
  103. },
  104. add: function () {
  105. const sheet = materialExponentSpread.getActiveSheet();
  106. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  107. if (result) {
  108. materialExponentData.push(result);
  109. sheet.addRows(materialExponentData.length - 1, 1);
  110. SpreadJsObj.reLoadRowData(sheet, materialExponentData.length - 1);
  111. sheet.setSelection(materialExponentData.length - 1, 0, 1, 1);
  112. materialExponentSpreadObj.refreshActn();
  113. }
  114. });
  115. },
  116. del: function () {
  117. const sheet = materialExponentSpread.getActiveSheet();
  118. const select = SpreadJsObj.getSelectObject(sheet);
  119. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  120. ex_tp = result.ex_tp;
  121. ex_expr = result.ex_expr;
  122. resetExTpTable();
  123. const index = materialExponentData.indexOf(select);
  124. materialExponentData.splice(index, 1);
  125. sheet.deleteRows(index, 1);
  126. const sel = sheet.getSelections();
  127. sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  128. materialExponentSpreadObj.refreshActn();
  129. });
  130. },
  131. selectionChanged: function (e, info) {
  132. const sel = info.sheet.getSelections()[0];
  133. const col = info.sheet.zh_setting.cols[sel.col];
  134. materialExponentSpreadObj.refreshActn(sel.rowCount);
  135. const data = SpreadJsObj.getSelectObject(info.sheet);
  136. materialExponentSpreadObj.setReadOnly(true);
  137. },
  138. editEnded: function (e, info) {
  139. if (info.sheet.zh_setting) {
  140. const select = SpreadJsObj.getSelectObject(info.sheet);
  141. const col = info.sheet.zh_setting.cols[info.col];
  142. if (col.field === 'is_summary') {
  143. return;
  144. }
  145. // 未改变值则不提交
  146. const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  147. const orgValue = select[col.field];
  148. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  149. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  150. return;
  151. }
  152. // 判断部分值是否输入的是数字判断和数据计算
  153. if (col.field === 'basic_price' || col.field === 'm_price') {
  154. if (isNaN(validText)) {
  155. toastr.error('不能输入其它非数字类型字符');
  156. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  157. return;
  158. }
  159. const num = parseFloat(validText);
  160. if (validText !== null && (num < 0 || !/^(\d{1,10}|\d{1,7}\.\d{1,3})?$/.test(num))) {
  161. toastr.error('请输入10位以内有效数字并且小于3位小数的浮点数');
  162. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  163. return;
  164. }
  165. }
  166. if (col.field === 'weight_num') {
  167. if (isNaN(validText)) {
  168. toastr.error('不能输入其它非数字类型字符');
  169. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  170. return;
  171. }
  172. const num = parseFloat(validText);
  173. if (validText !== null && (num < 0 || num >= 1 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  174. toastr.error('请输入0~1范围内并且小于3位小数的浮点数');
  175. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  176. return;
  177. }
  178. const total_weight = ZhCalc.add(ZhCalc.sub(_.sumBy(materialExponentData, 'weight_num'), parseFloat(orgValue)), num);
  179. if (total_weight > 1) {
  180. toastr.error('加权系数总和不能大于1');
  181. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  182. return;
  183. }
  184. }
  185. select[col.field] = validText;
  186. select.calc_num = materialExponentCol.getValue.calc_num(select);
  187. // console.log(select);
  188. // 更新至服务器
  189. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  190. ex_tp = result.ex_tp;
  191. ex_expr = result.ex_expr;
  192. resetExTpTable();
  193. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  194. materialExponentData.splice(info.row, 1, select);
  195. }, function () {
  196. select[col.field] = orgValue;
  197. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  198. });
  199. }
  200. },
  201. buttonClicked: function (e, info) {
  202. if (info.sheet.zh_setting) {
  203. const select = SpreadJsObj.getSelectObject(info.sheet);
  204. const col = info.sheet.zh_setting.cols[info.col];
  205. if (materialExponentCol.readOnly.isEdit(select)) {
  206. return;
  207. }
  208. if (col.field === 'is_summary') {
  209. if (info.sheet.isEditing()) {
  210. info.sheet.endEdit(true);
  211. }
  212. select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0;
  213. // 更新至服务器
  214. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  215. ex_tp = result.ex_tp;
  216. ex_expr = result.ex_expr;
  217. resetExTpTable();
  218. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  219. }, function () {
  220. select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1;
  221. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  222. });
  223. }
  224. }
  225. },
  226. deletePress: function (sheet) {
  227. return;
  228. },
  229. clipboardPasted(e, info) {
  230. const hint = {
  231. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  232. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  233. numberCan: {type: 'error', msg: '请粘贴10位以内有效数字并且小于3位小数的浮点数'},
  234. numberCan2: {type: 'error', msg: '请粘贴0~1范围内并且小于3位小数的浮点数'},
  235. weightNumberCan: {type: 'error', msg: '粘贴的加权系数总和不能大于1'},
  236. };
  237. const range = info.cellRange;
  238. const sortData = info.sheet.zh_data || [];
  239. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  240. toastMessageUniq(hint.cellError);
  241. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  242. SpreadJsObj.reLoadSheetHeader(materialExponentSpread.getActiveSheet());
  243. SpreadJsObj.reLoadSheetData(materialExponentSpread.getActiveSheet());
  244. return;
  245. }
  246. if (sortData.length > 0 && range.col + range.colCount > 10) {
  247. toastMessageUniq(hint.cellError);
  248. SpreadJsObj.reLoadSheetHeader(materialExponentSpread.getActiveSheet());
  249. SpreadJsObj.reLoadSheetData(materialExponentSpread.getActiveSheet());
  250. return;
  251. }
  252. const data = [];
  253. // const rowData = [];
  254. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  255. let bPaste = true;
  256. const curRow = range.row + iRow;
  257. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  258. const materialExData = { id: sortData[curRow].id };
  259. const hintRow = range.rowCount > 1 ? curRow : '';
  260. let sameCol = 0;
  261. for (let iCol = 0; iCol < range.colCount; iCol++) {
  262. const curCol = range.col + iCol;
  263. const colSetting = info.sheet.zh_setting.cols[curCol];
  264. if (!colSetting) continue;
  265. let validText = info.sheet.getText(curRow, curCol);
  266. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  267. const orgValue = sortData[curRow][colSetting.field];
  268. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  269. sameCol++;
  270. if (range.colCount === sameCol) {
  271. bPaste = false;
  272. }
  273. continue;
  274. }
  275. if (colSetting.field === 'basic_price' || colSetting.field === 'm_price') {
  276. if (isNaN(validText)) {
  277. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  278. bPaste = false;
  279. continue;
  280. }
  281. const num = parseFloat(validText);
  282. if (validText !== null && (num < 0 || !/^(\d{1,10}|\d{1,7}\.\d{1,3})?$/.test(num))) {
  283. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  284. bPaste = false;
  285. continue;
  286. }
  287. }
  288. if (colSetting.field === 'weight_num') {
  289. if (isNaN(validText)) {
  290. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  291. bPaste = false;
  292. continue;
  293. }
  294. const num = parseFloat(validText);
  295. if (validText !== null && (num < 0 || num >= 1 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  296. toastMessageUniq(getPasteHint(hint.numberCan2, hintRow));
  297. bPaste = false;
  298. continue;
  299. }
  300. const total_weight = ZhCalc.add(ZhCalc.sub(_.sumBy(materialExponentData, 'weight_num'), parseFloat(orgValue)), num);
  301. console.log(total_weight, _.sumBy(materialExponentData, 'weight_num'), orgValue, num);
  302. if (total_weight > 1) {
  303. toastMessageUniq(getPasteHint(hint.weightNumberCan, hintRow));
  304. bPaste = false;
  305. continue;
  306. }
  307. }
  308. materialExData[colSetting.field] = validText;
  309. sortData[curRow][colSetting.field] = validText;
  310. }
  311. if (bPaste) {
  312. materialExData.calc_num = materialExponentCol.getValue.calc_num(sortData[curRow]);
  313. data.push(materialExData);
  314. // rowData.push(curRow);
  315. } else {
  316. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  317. }
  318. }
  319. if (data.length === 0) {
  320. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  321. return;
  322. }
  323. // console.log(data);
  324. // 更新至服务器
  325. postData(window.location.pathname + '/save', { type:'paste', updateData: data }, function (result) {
  326. materialExponentData = result.info;
  327. SpreadJsObj.loadSheetData(materialExponentSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialExponentData);
  328. ex_tp = result.ex_tp;
  329. ex_expr = result.ex_expr;
  330. resetExTpTable();
  331. }, function () {
  332. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  333. return;
  334. });
  335. },
  336. setReadOnly: function(readOnly) {
  337. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  338. }
  339. };
  340. materialExponentSpreadObj.refreshActn();
  341. materialExponentSpread.bind(spreadNS.Events.SelectionChanged, materialExponentSpreadObj.selectionChanged);
  342. materialExponentSpread.bind(spreadNS.Events.ClipboardPasted, materialExponentSpreadObj.clipboardPasted);
  343. SpreadJsObj.addDeleteBind(materialExponentSpread, materialExponentSpreadObj.deletePress);
  344. if (!readOnly) {
  345. $('#add').click(materialExponentSpreadObj.add);
  346. $('#del').click(materialExponentSpreadObj.del);
  347. materialExponentSpread.bind(spreadNS.Events.EditEnded, materialExponentSpreadObj.editEnded);
  348. materialExponentSpread.bind(spreadNS.Events.ButtonClicked, materialExponentSpreadObj.buttonClicked);
  349. // 右键菜单
  350. $.contextMenu({
  351. selector: '#material-exponent-spread',
  352. build: function ($trigger, e) {
  353. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialExponentSpread);
  354. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  355. },
  356. items: {
  357. 'create': {
  358. name: '新增',
  359. icon: 'fa-sign-in',
  360. callback: function (key, opt) {
  361. materialExponentSpreadObj.add(materialExponentSpread.getActiveSheet());
  362. },
  363. },
  364. 'delete': {
  365. name: '删除',
  366. icon: 'fa-remove',
  367. callback: function (key, opt) {
  368. materialExponentSpreadObj.del(materialExponentSpread.getActiveSheet());
  369. },
  370. disabled: function (key, opt) {
  371. const sheet = materialExponentSpread.getActiveSheet();
  372. const select = SpreadJsObj.getSelectObject(sheet);
  373. const sel = sheet.getSelections()[0];
  374. materialExponentSpreadObj.refreshActn(sel.rowCount);
  375. if (!readOnly && select && materialExponentBase.isUsed(select) && sel.rowCount === 1) {
  376. return false;
  377. } else {
  378. return true;
  379. }
  380. }
  381. },
  382. }
  383. });
  384. // 调差基数选中
  385. $('.calc_select').on('click', function () {
  386. // 如果是选中则清除其余2个的选中
  387. const code = $(this).val();
  388. for (const calc of ex_calc) {
  389. calc.select = $(this).is(':checked') && code === calc.code ? true : false;
  390. if (!calc.select) {
  391. $('.calc_select[value="'+ calc.code +'"]').prop('checked', false);
  392. }
  393. }
  394. postData(window.location.pathname + '/save', { type:'ex_calc', updateData: ex_calc }, function (result) {
  395. ex_tp = result.ex_tp;
  396. ex_expr = result.ex_expr;
  397. resetExTpTable();
  398. });
  399. });
  400. $('#changeRate').change(function () {
  401. const rate = parseInt($(this).val());
  402. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  403. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  404. const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), 2);
  405. const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), 2);
  406. const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), 2);
  407. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  408. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  409. $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  410. $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  411. });
  412. });
  413. }
  414. $.divResizer({
  415. select: '#right-spr',
  416. callback: function () {
  417. materialExponentSpread.refresh();
  418. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  419. // setLocalCache('material_month_' + materialID, width);
  420. }
  421. });
  422. // 展开收起月信息价并浏览器记住本期展开收起
  423. $('a', '.right-nav').bind('click', function () {
  424. //const main = $('#main-view'), tool = $('#tools-view');
  425. const tab = $(this), tabPanel = $(tab.attr('content'));
  426. if (!tab.hasClass('active')) {
  427. $('a', '.side-menu').removeClass('active');
  428. $('.tab-content .tab-select-show').removeClass('active');
  429. tab.addClass('active');
  430. tabPanel.addClass('active');
  431. showSideTools(tab.hasClass('active'));
  432. if (tab.attr('content') === '#base-tab') {
  433. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  434. // setLocalCache('material_month_' + materialID, width);
  435. // materialMonthSpread.refresh();
  436. }
  437. } else {
  438. // removeLocalCache('material_month_' + materialID);
  439. tab.removeClass('active');
  440. tabPanel.removeClass('active');
  441. showSideTools(tab.hasClass('active'));
  442. }
  443. // materialSpread.refresh();
  444. materialExponentSpread.refresh();
  445. });
  446. });