material.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. 'use strict';
  2. /**
  3. * 材料调差 - 调差工料
  4. *
  5. * @author Mai
  6. * @date 2019/1/16
  7. * @version
  8. */
  9. function loadUpdateMaterials(newMaterial, fields) {
  10. const newMaterials = newMaterial instanceof Array ? newMaterial : [newMaterial];
  11. for (const nm of newMaterials) {
  12. const om = _.find(materialBillsData, {id: nm.id});
  13. for (const prop in nm) {
  14. if (!fields || fields.indexOf(prop) >= 0) {
  15. om[prop] = nm[prop];
  16. }
  17. }
  18. }
  19. }
  20. // function DatePickerCellType() {
  21. // }
  22. // DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  23. // DatePickerCellType.prototype.createEditorElement = function () {
  24. // //Create input presenter.
  25. // var input = document.createElement("input");
  26. // return input;
  27. // };
  28. // DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect) {
  29. // //Initialize input editor.
  30. // if (editorContext) {
  31. // const $editor = $(editorContext);
  32. // GC.Spread.Sheets.CellTypes.Base.prototype.activateEditor.apply(this, arguments);
  33. // $editor.datepicker({
  34. // language: 'zh',
  35. // });
  36. // $editor.css("position", "absolute");
  37. // $editor.attr("gcUIElement", "gcEditingInput");
  38. // $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  39. // }
  40. // }
  41. // // DatePickerCellType.prototype.deactivateEditor = function (editorContext) {
  42. // // //Remove input editor when end editor status.
  43. // // if (editorContext) {
  44. // // var element = editorContext;
  45. // // $(element).datepicker().data('datepicker').hide();
  46. // // $(element).datepicker().data('datepicker').destroy();
  47. // // // $(element).datepicker("hide");
  48. // // // $(element).datepicker("destroy");
  49. // // }
  50. // // GC.Spread.Sheets.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments)
  51. // // };
  52. // // DatePickerCellType.prototype.setEditorValue = function (editor, value) {
  53. // // //Sync value from Cell value to editor value.
  54. // // console.log($(editor), value);
  55. // // // $(editor).datepicker("setDate", value);
  56. // // $(editor).datepicker().data('datepicker').selectDate(value);
  57. // // };
  58. // // DatePickerCellType.prototype.getEditorValue = function (editor) {
  59. // // //Sync value from editor value to cell value.
  60. // // $(editor).datepicker({
  61. // // onSelect: function onSelect(fd, date) {
  62. // // console.log(fd);
  63. // // $(editor).val(fd);
  64. // // }
  65. // // });
  66. // // // return $(editor).datepicker("getDate");
  67. // // return $(editor).val();
  68. // // };
  69. // DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect) {
  70. // if (editorContext) {
  71. // const $editor = $(editorContext);
  72. // $editor.css("width", cellRect.width - 1);
  73. // $editor.css("height", cellRect.height - 3);
  74. // }
  75. // };
  76. function resetTpTable() {
  77. const rate = $('#changeRate').val();
  78. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  79. const jzbqhs = ZhCalc.round(ZhCalc.mul(ZhCalc.add(pre_tp, m_tp), 1+rate/100), 2);
  80. $('#tp_set').find('td').eq(1).text(ZhCalc.round(m_tp, 2));
  81. $('#tp_set').find('td').eq(2).text(ZhCalc.round(ZhCalc.add(pre_tp, m_tp), 2));
  82. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  83. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  84. }
  85. $(document).ready(() => {
  86. autoFlashHeight();
  87. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  88. const materialSpreadSetting = {
  89. cols: [
  90. {title: '调差类型', colSpan: '1', rowSpan: '2', field: 't_type', hAlign: 1, width: 80, formatter: '@', readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.t_type, cellTypeKey: 1},
  91. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  92. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  93. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  94. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  95. {title: '工料分类', colSpan: '1', rowSpan: '2', field: 'm_type', hAlign: 1, width: 60, readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.m_type, cellTypeKey: 2},
  96. {title: '本期应耗数量', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  97. {title: '基准价', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  98. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  99. {title: '本期信息价|单价', colSpan: '3|1', rowSpan: '1|1', field: 'msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  100. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  101. {title: '|价差', colSpan: '1', rowSpan: '1|1', field: 'msg_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.msg_spread'},
  102. {title: '本期材料调差|上涨幅度(%)', colSpan: '4|1', rowSpan: '1|1', field: 'm_up_risk', hAlign: 2, width: 100, type: 'Number', readOnly: 'readOnly.isEdit'},
  103. {title: '|下跌幅度(%)', colSpan: '|1', rowSpan: '|1', field: 'm_down_risk', hAlign: 2, width: 100, type: 'Number', readOnly: 'readOnly.isEdit'},
  104. {title: '|有效价差', colSpan: '|1', rowSpan: '|1', field: 'm_spread', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  105. {title: '|调整金额', colSpan: '|1', rowSpan: '1|1', field: 'm_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  106. {title: '截止上期调差金额', colSpan: '1', rowSpan: '2', field: 'pre_tp', hAlign: 2, width: 120, type: 'Number', readOnly: true},
  107. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.remark'},
  108. ],
  109. emptyRows: 0,
  110. headRows: 2,
  111. headRowHeight: [25, 25],
  112. defaultRowHeight: 21,
  113. headerFont: '12px 微软雅黑',
  114. font: '12px 微软雅黑',
  115. readOnly: readOnly,
  116. };
  117. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  118. spCol.readOnly = true;
  119. spCol.cellType = 'activeImageBtn';
  120. spCol.normalImg = '#ellipsis-icon';
  121. spCol.indent = 5;
  122. spCol.showImage = function (data) {
  123. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  124. return data.t_type === 2;
  125. };
  126. materialSpreadSetting.imageClick = function (data) {
  127. if (data.t_type === 2) {
  128. $('#bcyy').modal('show');
  129. $('#materialbillsId').val(data.id);
  130. $('#expr').val(data.expr);
  131. if (!readOnly && data.mid === materialID) {
  132. $('#expr').attr('readOnly', false);
  133. $('#expr_btn').show();
  134. $('#expr_select').show();
  135. } else {
  136. $('#expr').attr('readOnly', true);
  137. $('#expr_btn').hide();
  138. $('#expr_select').hide();
  139. }
  140. }
  141. };
  142. const materialBase = {
  143. isEdit: function (data) {
  144. if (data.t_type === 2) {
  145. return data.mid === materialID;
  146. } else {
  147. const mlInfo = materialListData.find(function (item) {
  148. return item.mb_id === data.id;
  149. });
  150. return data.mid === materialID && mlInfo === undefined;
  151. }
  152. },
  153. // isStage: function (data) {
  154. // return data.mid === materialID;
  155. // }
  156. }
  157. const materialCol = {
  158. getValue: {
  159. msg_spread: function (data) {
  160. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), 2);
  161. },
  162. m_spread : function (data) {
  163. const msg_spread = materialCol.getValue.msg_spread(data);
  164. const cor = msg_spread >= 0 ? ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_up_risk, 100)) : ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_down_risk, 100));
  165. return Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? ZhCalc.round(ZhCalc.sub(msg_spread, cor), 2) : ZhCalc.round(ZhCalc.add(msg_spread, cor), 2)) : 0;
  166. },
  167. m_tp: function (data) {
  168. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), 2);
  169. }
  170. },
  171. readOnly: {
  172. isEdit: function (data) {
  173. return !(!readOnly && materialBase.isEdit(data));
  174. },
  175. remark: function () {
  176. return readOnly;
  177. },
  178. },
  179. };
  180. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  181. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  182. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  183. const materialSpreadObj = {
  184. refreshActn: function () {
  185. const setObjEnable = function (obj, enable) {
  186. if (enable) {
  187. obj.removeClass('disabled');
  188. } else {
  189. obj.addClass('disabled');
  190. }
  191. };
  192. const sheet = materialSpread.getActiveSheet();
  193. const select = SpreadJsObj.getSelectObject(sheet);
  194. // 还需判断是否已被调差清单调用
  195. setObjEnable($('#del'), !readOnly && select && materialBase.isEdit(select));
  196. },
  197. add: function () {
  198. const sheet = materialSpread.getActiveSheet();
  199. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  200. if (result) {
  201. materialBillsData.push(result);
  202. sheet.addRows(materialBillsData.length - 1, 1);
  203. SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  204. sheet.setSelection(materialBillsData.length - 1, 0, 1, 1);
  205. materialSpreadObj.refreshActn();
  206. }
  207. });
  208. },
  209. del: function () {
  210. const sheet = materialSpread.getActiveSheet();
  211. const select = SpreadJsObj.getSelectObject(sheet);
  212. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  213. m_tp = result.m_tp;
  214. resetTpTable();
  215. const index = materialBillsData.indexOf(select);
  216. materialBillsData.splice(index, 1);
  217. sheet.deleteRows(index, 1);
  218. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  219. const sel = sheet.getSelections();
  220. sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  221. materialSpreadObj.refreshActn();
  222. });
  223. },
  224. selectionChanged: function (e, info) {
  225. materialSpreadObj.refreshActn();
  226. const sel = info.sheet.getSelections()[0];
  227. const col = info.sheet.zh_setting.cols[sel.col];
  228. const data = SpreadJsObj.getSelectObject(info.sheet);
  229. materialSpreadObj.setReadOnly(true);
  230. },
  231. editEnded: function (e, info) {
  232. if (info.sheet.zh_setting) {
  233. const select = SpreadJsObj.getSelectObject(info.sheet);
  234. const col = info.sheet.zh_setting.cols[info.col];
  235. // 未改变值则不提交
  236. const validText = !isNaN(info.editingText) ? info.editingText : (info.editingText ? trimInvalidChar(info.editingText) : null);
  237. const orgValue = select[col.field];
  238. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  239. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  240. return;
  241. }
  242. // 判断部分值是否输入的是数字判断和数据计算
  243. if (col.field === 'basic_price') {
  244. if (isNaN(validText)) {
  245. toastr.error('不能输入其它非数字类型字符');
  246. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  247. return;
  248. }
  249. const num = parseFloat(validText);
  250. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  251. toastr.error('请输入大于0并且小于3位小数的浮点数');
  252. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  253. return;
  254. }
  255. }
  256. if (col.field === 'msg_tp') {
  257. if (isNaN(validText)) {
  258. toastr.error('不能输入其它非数字类型字符');
  259. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  260. return;
  261. }
  262. const num = parseFloat(validText);
  263. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  264. toastr.error('请输入大于0并且小于3位小数的浮点数');
  265. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  266. return;
  267. }
  268. }
  269. if (col.field === 'm_up_risk') {
  270. // 只能输入正整数
  271. if (isNaN(validText)) {
  272. toastr.error('不能输入其它非数字类型字符');
  273. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  274. return;
  275. }
  276. const num = parseFloat(validText);
  277. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  278. toastr.error('只能输入0-100的正整数');
  279. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  280. return;
  281. }
  282. }
  283. if (col.field === 'm_down_risk') {
  284. // 只能输入正整数
  285. if (isNaN(validText)) {
  286. toastr.error('不能输入其它非数字类型字符');
  287. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  288. return;
  289. }
  290. const num = parseFloat(validText);
  291. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  292. toastr.error('只能输入0-100的正整数');
  293. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  294. return;
  295. }
  296. }
  297. if (col.field === 't_type') {
  298. if (validText === 1) {
  299. select.quantity = null;
  300. select.expr = null;
  301. select.m_tp = null;
  302. }
  303. }
  304. select[col.field] = validText;
  305. select.msg_spread = materialCol.getValue.msg_spread(select);
  306. select.m_spread = materialCol.getValue.m_spread(select);
  307. select.m_tp = materialCol.getValue.m_tp(select);
  308. // 更新至服务器
  309. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  310. m_tp = result.m_tp;
  311. resetTpTable();
  312. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  313. }, function () {
  314. select[col.field] = orgValue;
  315. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  316. });
  317. }
  318. },
  319. // clipboardPasted(e, info) {
  320. // console.log(info)
  321. // if (info.sheet.zh_setting) {
  322. // const select = SpreadJsObj.getSelectObject(info.sheet);
  323. // const col = info.sheet.zh_setting.cols[info.col];
  324. // // 未改变值则不提交
  325. // const validText = info.editingText ? trimInvalidChar(info.editingText) : null;
  326. // console.log(validText);
  327. // const orgValue = select[col.field];
  328. // if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  329. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  330. // return;
  331. // }
  332. // // 判断部分值是否输入的是数字判断和数据计算
  333. // if (col.field === 'basic_price') {
  334. // if (isNaN(validText)) {
  335. // toastr.error('不能输入其它非数字类型字符');
  336. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  337. // return;
  338. // }
  339. // const num = parseFloat(validText);
  340. // if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  341. // toastr.error('请输入大于0并且小于3位小数的浮点数');
  342. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  343. // return;
  344. // }
  345. // }
  346. // if (col.field === 'msg_tp') {
  347. // if (isNaN(validText)) {
  348. // toastr.error('不能输入其它非数字类型字符');
  349. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  350. // return;
  351. // }
  352. // const num = parseFloat(validText);
  353. // if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  354. // toastr.error('请输入大于0并且小于3位小数的浮点数');
  355. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  356. // return;
  357. // }
  358. // }
  359. // if (col.field === 'm_up_risk') {
  360. // // 只能输入正整数
  361. // if (isNaN(validText)) {
  362. // toastr.error('不能输入其它非数字类型字符');
  363. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  364. // return;
  365. // }
  366. // const num = parseFloat(validText);
  367. // if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  368. // toastr.error('只能输入0-100的正整数');
  369. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  370. // return;
  371. // }
  372. // }
  373. // if (col.field === 'm_down_risk') {
  374. // // 只能输入正整数
  375. // if (isNaN(validText)) {
  376. // toastr.error('不能输入其它非数字类型字符');
  377. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  378. // return;
  379. // }
  380. // const num = parseFloat(validText);
  381. // if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  382. // toastr.error('只能输入0-100的正整数');
  383. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  384. // return;
  385. // }
  386. // }
  387. // if (col.field === 't_type') {
  388. // if (validText === 1) {
  389. // select.quantity = null;
  390. // select.expr = null;
  391. // select.m_tp = null;
  392. // }
  393. // }
  394. // select[col.field] = validText;
  395. // select.msg_spread = materialCol.getValue.msg_spread(select);
  396. // select.m_spread = materialCol.getValue.m_spread(select);
  397. // select.m_tp = materialCol.getValue.m_tp(select);
  398. // // 更新至服务器
  399. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  400. // m_tp = result.m_tp;
  401. // resetTpTable();
  402. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  403. // }, function () {
  404. // select[col.field] = orgValue;
  405. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  406. // });
  407. // }
  408. // },
  409. setReadOnly: function(readOnly) {
  410. SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  411. }
  412. };
  413. materialSpreadObj.refreshActn();
  414. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  415. // materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  416. // const sheet = materialSpread.getActiveSheet();
  417. // sheet.suspendPaint();
  418. // var range = sheet.getRange(-1, 8, -1, 1);
  419. // console.log(range);
  420. // range.cellType(new DatePickerCellType());
  421. // // range.formatter("yyyy-MM-dd");
  422. // sheet.resumePaint();
  423. if (!readOnly) {
  424. $('#add').click(materialSpreadObj.add);
  425. $('#del').click(materialSpreadObj.del);
  426. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  427. // 右键菜单
  428. $.contextMenu({
  429. selector: '#material-spread',
  430. build: function ($trigger, e) {
  431. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  432. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  433. },
  434. items: {
  435. 'create': {
  436. name: '新增材料',
  437. icon: 'fa-sign-in',
  438. callback: function (key, opt) {
  439. materialSpreadObj.add(materialSpread.getActiveSheet());
  440. },
  441. },
  442. 'delete': {
  443. name: '删除材料',
  444. icon: 'fa-remove',
  445. callback: function (key, opt) {
  446. materialSpreadObj.del(materialSpread.getActiveSheet());
  447. },
  448. disabled: function (key, opt) {
  449. const sheet = materialSpread.getActiveSheet();
  450. const select = SpreadJsObj.getSelectObject(sheet);
  451. materialSpreadObj.refreshActn();
  452. if (!readOnly && select && materialBase.isEdit(select)) {
  453. return false;
  454. } else {
  455. return true;
  456. }
  457. }
  458. },
  459. }
  460. });
  461. $('#changeRate').change(function () {
  462. const rate = parseInt($(this).val());
  463. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  464. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  465. const jzbqhs = ZhCalc.round(ZhCalc.mul(ZhCalc.add(pre_tp, m_tp), 1+rate/100), 2);
  466. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  467. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  468. });
  469. });
  470. $('#expr_select button').on('click', function () {
  471. const code = $(this).text();
  472. $('#expr').val($('#expr').val() + code);
  473. });
  474. const ExprObj = {
  475. _checkExprValid(expr, invalidParam) {
  476. if (!expr) return [true, null];
  477. const param = [];
  478. let num = '', base = '';
  479. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  480. if (/^[\d\.%]+/.test(expr[i])) {
  481. if (base !== '') {
  482. param.push({type: 'base', value: base});
  483. base = '';
  484. }
  485. num = num + expr[i];
  486. } else if (/^[a-z]/.test(expr[i])) {
  487. if (num !== '') {
  488. param.push({type: 'num', value: num});
  489. base = '';
  490. }
  491. base = base + expr[i];
  492. } else if (expr[i] === '(') {
  493. if (num !== '') {
  494. param.push({type: 'num', value: num});
  495. base = '';
  496. }
  497. if (base !== '') {
  498. param.push({type: 'base', value: base});
  499. base = '';
  500. }
  501. param.push({type: 'left', value: '('});
  502. } else if (expr[i] === ')') {
  503. if (num !== '') {
  504. param.push({type: 'num', value: num});
  505. base = '';
  506. }
  507. if (base !== '') {
  508. param.push({type: 'base', value: base});
  509. base = '';
  510. }
  511. param.push({type: 'right', value: ')'});
  512. } else if (/^[\+\-*\/]/.test(expr[i])) {
  513. if (num !== '') {
  514. param.push({type: 'num', value: num});
  515. base = '';
  516. }
  517. if (base !== '') {
  518. param.push({type: 'base', value: base});
  519. base = '';
  520. }
  521. param.push({type: 'calc', value: expr[i]});
  522. } else {
  523. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  524. }
  525. }
  526. if (num !== '') {
  527. param.push({type: 'num', value: num});
  528. base = '';
  529. }
  530. if (base !== '') {
  531. param.push({type: 'base', value: base});
  532. base = '';
  533. }
  534. if (param.length === 0) return true;
  535. if (param.length > 1) {
  536. if (param[0].value === '-') {
  537. param[1].value = '-' + param[1];
  538. }
  539. param.unshift();
  540. }
  541. const iLen = param.length;
  542. let iLeftCount = 0, iRightCount = 0;
  543. for (const [i, p] of param.entries()) {
  544. if (p.type === 'calc') {
  545. if (i === 0 || i === iLen - 1)
  546. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  547. }
  548. if (p.type === 'num') {
  549. num = p.value.replace('%', '');
  550. if (p.value.length - num.length > 1)
  551. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  552. num = _.toNumber(num);
  553. if (num === undefined || num === null || _.isNaN(num))
  554. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  555. if (i > 0) {
  556. if (param[i - 1].type !== 'calc') {
  557. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  558. } else if (param[i - 1].value === '/' && num === 0) {
  559. return [false, '输入的表达式非法:请勿除0'];
  560. }
  561. }
  562. }
  563. if (p.type === 'base') {
  564. const baseParam = _.find(calcBase, {code: p.value});
  565. if (!baseParam)
  566. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  567. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  568. return [false, '不可使用计算基数' + p.value];
  569. if (i > 0 && param[i - 1].type === 'calc')
  570. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  571. }
  572. if (p.type === 'left') {
  573. iLeftCount += 1;
  574. if (i !== 0 && param[i-1].type !== 'calc')
  575. return [false, '输入的表达式非法:(前应有运算符'];
  576. }
  577. if (p.type === 'right') {
  578. iRightCount += 1;
  579. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  580. return [false, '输入的表达式非法:)后应有运算符'];
  581. if (iRightCount > iLeftCount)
  582. return [false, '输入的表达式非法:")"前无对应的"("'];
  583. }
  584. }
  585. if (iLeftCount > iRightCount)
  586. return [false, '输入的表达式非法:"("后无对应的")"'];
  587. return [true, ''];
  588. },
  589. _checkExpr: function (text) {
  590. if (text) {
  591. const num = _.toNumber(text);
  592. if (num) {
  593. console.log(num);
  594. } else {
  595. const expr = text.replace('=', '').toLowerCase();
  596. const [valid, msg] = this._checkExprValid(expr);
  597. if (!valid) return [valid, msg];
  598. }
  599. }
  600. return [true, ''];
  601. },
  602. };
  603. $('#expr_btn').click(function () {
  604. const expr = $('#expr').val();
  605. // 判断表达式格式
  606. const [valid, msg] = ExprObj._checkExpr(expr);
  607. if (!valid) {
  608. toastr.error(msg);
  609. }
  610. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr }, function (result) {
  611. m_tp = result.m_tp;
  612. resetTpTable();
  613. const sheet = materialSpread.getActiveSheet();
  614. const select = SpreadJsObj.getSelectObject(sheet);
  615. const index = materialBillsData.indexOf(select);
  616. materialBillsData.splice(index, 1, result.info);
  617. SpreadJsObj.reLoadRowData(sheet, index);
  618. $('#bcyy').modal('hide');
  619. });
  620. })
  621. } else {
  622. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  623. }
  624. $.subMenu({
  625. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  626. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  627. key: 'menu.1.0.0',
  628. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  629. callback: function (info) {
  630. if (info.mini) {
  631. $('.panel-title').addClass('fluid');
  632. $('#sub-menu').removeClass('panel-sidebar');
  633. } else {
  634. $('.panel-title').removeClass('fluid');
  635. $('#sub-menu').addClass('panel-sidebar');
  636. }
  637. autoFlashHeight();
  638. materialSpread.refresh();
  639. }
  640. });
  641. });