material.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. console.log(!isNaN(info.editingText));
  237. const validText = !isNaN(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  238. console.log(validText);
  239. const orgValue = select[col.field];
  240. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  241. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  242. return;
  243. }
  244. // 判断部分值是否输入的是数字判断和数据计算
  245. if (col.field === 'basic_price') {
  246. if (isNaN(validText)) {
  247. toastr.error('不能输入其它非数字类型字符');
  248. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  249. return;
  250. }
  251. const num = parseFloat(validText);
  252. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  253. toastr.error('请输入大于0并且小于3位小数的浮点数');
  254. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  255. return;
  256. }
  257. }
  258. if (col.field === 'msg_tp') {
  259. if (isNaN(validText)) {
  260. toastr.error('不能输入其它非数字类型字符');
  261. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  262. return;
  263. }
  264. const num = parseFloat(validText);
  265. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  266. toastr.error('请输入大于0并且小于3位小数的浮点数');
  267. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  268. return;
  269. }
  270. }
  271. if (col.field === 'm_up_risk') {
  272. // 只能输入正整数
  273. if (isNaN(validText)) {
  274. toastr.error('不能输入其它非数字类型字符');
  275. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  276. return;
  277. }
  278. const num = parseFloat(validText);
  279. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  280. toastr.error('只能输入0-100的正整数');
  281. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  282. return;
  283. }
  284. }
  285. if (col.field === 'm_down_risk') {
  286. // 只能输入正整数
  287. if (isNaN(validText)) {
  288. toastr.error('不能输入其它非数字类型字符');
  289. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  290. return;
  291. }
  292. const num = parseFloat(validText);
  293. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  294. toastr.error('只能输入0-100的正整数');
  295. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  296. return;
  297. }
  298. }
  299. if (col.field === 't_type') {
  300. if (validText === 1) {
  301. select.quantity = null;
  302. select.expr = null;
  303. select.m_tp = null;
  304. }
  305. }
  306. select[col.field] = validText;
  307. select.msg_spread = materialCol.getValue.msg_spread(select);
  308. select.m_spread = materialCol.getValue.m_spread(select);
  309. select.m_tp = materialCol.getValue.m_tp(select);
  310. // 更新至服务器
  311. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  312. m_tp = result.m_tp;
  313. resetTpTable();
  314. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  315. }, function () {
  316. select[col.field] = orgValue;
  317. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  318. });
  319. }
  320. },
  321. clipboardPasted(e, info) {
  322. const tree = info.sheet.zh_tree;
  323. if (!tree) { return; }
  324. console.log(info);
  325. // if (info.sheet.zh_setting) {
  326. // const select = SpreadJsObj.getSelectObject(info.sheet);
  327. // const col = info.sheet.zh_setting.cols[info.col];
  328. // // 未改变值则不提交
  329. // const validText = info.editingText ? trimInvalidChar(info.editingText) : null;
  330. // const orgValue = select[col.field];
  331. // if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  332. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  333. // return;
  334. // }
  335. // // 判断部分值是否输入的是数字判断和数据计算
  336. // if (col.field === 'basic_price') {
  337. // if (isNaN(validText)) {
  338. // toastr.error('不能输入其它非数字类型字符');
  339. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  340. // return;
  341. // }
  342. // const num = parseFloat(validText);
  343. // if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  344. // toastr.error('请输入大于0并且小于3位小数的浮点数');
  345. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  346. // return;
  347. // }
  348. // }
  349. // if (col.field === 'msg_tp') {
  350. // if (isNaN(validText)) {
  351. // toastr.error('不能输入其它非数字类型字符');
  352. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  353. // return;
  354. // }
  355. // const num = parseFloat(validText);
  356. // if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  357. // toastr.error('请输入大于0并且小于3位小数的浮点数');
  358. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  359. // return;
  360. // }
  361. // }
  362. // if (col.field === 'm_up_risk') {
  363. // // 只能输入正整数
  364. // if (isNaN(validText)) {
  365. // toastr.error('不能输入其它非数字类型字符');
  366. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  367. // return;
  368. // }
  369. // const num = parseFloat(validText);
  370. // if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  371. // toastr.error('只能输入0-100的正整数');
  372. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  373. // return;
  374. // }
  375. // }
  376. // if (col.field === 'm_down_risk') {
  377. // // 只能输入正整数
  378. // if (isNaN(validText)) {
  379. // toastr.error('不能输入其它非数字类型字符');
  380. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  381. // return;
  382. // }
  383. // const num = parseFloat(validText);
  384. // if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  385. // toastr.error('只能输入0-100的正整数');
  386. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  387. // return;
  388. // }
  389. // }
  390. // if (col.field === 't_type') {
  391. // if (validText === 1) {
  392. // select.quantity = null;
  393. // select.expr = null;
  394. // select.m_tp = null;
  395. // }
  396. // }
  397. // select[col.field] = validText;
  398. // select.msg_spread = materialCol.getValue.msg_spread(select);
  399. // select.m_spread = materialCol.getValue.m_spread(select);
  400. // select.m_tp = materialCol.getValue.m_tp(select);
  401. // // 更新至服务器
  402. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  403. // m_tp = result.m_tp;
  404. // resetTpTable();
  405. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  406. // }, function () {
  407. // select[col.field] = orgValue;
  408. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  409. // });
  410. // }
  411. },
  412. setReadOnly: function(readOnly) {
  413. SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  414. }
  415. };
  416. materialSpreadObj.refreshActn();
  417. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  418. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  419. // const sheet = materialSpread.getActiveSheet();
  420. // sheet.suspendPaint();
  421. // var range = sheet.getRange(-1, 8, -1, 1);
  422. // console.log(range);
  423. // range.cellType(new DatePickerCellType());
  424. // // range.formatter("yyyy-MM-dd");
  425. // sheet.resumePaint();
  426. if (!readOnly) {
  427. $('#add').click(materialSpreadObj.add);
  428. $('#del').click(materialSpreadObj.del);
  429. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  430. // 右键菜单
  431. $.contextMenu({
  432. selector: '#material-spread',
  433. build: function ($trigger, e) {
  434. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  435. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  436. },
  437. items: {
  438. 'create': {
  439. name: '新增材料',
  440. icon: 'fa-sign-in',
  441. callback: function (key, opt) {
  442. materialSpreadObj.add(materialSpread.getActiveSheet());
  443. },
  444. },
  445. 'delete': {
  446. name: '删除材料',
  447. icon: 'fa-remove',
  448. callback: function (key, opt) {
  449. materialSpreadObj.del(materialSpread.getActiveSheet());
  450. },
  451. disabled: function (key, opt) {
  452. const sheet = materialSpread.getActiveSheet();
  453. const select = SpreadJsObj.getSelectObject(sheet);
  454. materialSpreadObj.refreshActn();
  455. if (!readOnly && select && materialBase.isEdit(select)) {
  456. return false;
  457. } else {
  458. return true;
  459. }
  460. }
  461. },
  462. }
  463. });
  464. $('#changeRate').change(function () {
  465. const rate = parseInt($(this).val());
  466. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  467. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  468. const jzbqhs = ZhCalc.round(ZhCalc.mul(ZhCalc.add(pre_tp, m_tp), 1+rate/100), 2);
  469. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  470. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  471. });
  472. });
  473. $('#expr_select button').on('click', function () {
  474. const code = $(this).text();
  475. $('#expr').val($('#expr').val() + code);
  476. });
  477. const ExprObj = {
  478. _checkExprValid(expr, invalidParam) {
  479. if (!expr) return [true, null];
  480. const param = [];
  481. let num = '', base = '';
  482. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  483. if (/^[\d\.%]+/.test(expr[i])) {
  484. if (base !== '') {
  485. param.push({type: 'base', value: base});
  486. base = '';
  487. }
  488. num = num + expr[i];
  489. } else if (/^[a-z]/.test(expr[i])) {
  490. if (num !== '') {
  491. param.push({type: 'num', value: num});
  492. base = '';
  493. }
  494. base = base + expr[i];
  495. } else if (expr[i] === '(') {
  496. if (num !== '') {
  497. param.push({type: 'num', value: num});
  498. base = '';
  499. }
  500. if (base !== '') {
  501. param.push({type: 'base', value: base});
  502. base = '';
  503. }
  504. param.push({type: 'left', value: '('});
  505. } else if (expr[i] === ')') {
  506. if (num !== '') {
  507. param.push({type: 'num', value: num});
  508. base = '';
  509. }
  510. if (base !== '') {
  511. param.push({type: 'base', value: base});
  512. base = '';
  513. }
  514. param.push({type: 'right', value: ')'});
  515. } else if (/^[\+\-*\/]/.test(expr[i])) {
  516. if (num !== '') {
  517. param.push({type: 'num', value: num});
  518. base = '';
  519. }
  520. if (base !== '') {
  521. param.push({type: 'base', value: base});
  522. base = '';
  523. }
  524. param.push({type: 'calc', value: expr[i]});
  525. } else {
  526. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  527. }
  528. }
  529. if (num !== '') {
  530. param.push({type: 'num', value: num});
  531. base = '';
  532. }
  533. if (base !== '') {
  534. param.push({type: 'base', value: base});
  535. base = '';
  536. }
  537. if (param.length === 0) return true;
  538. if (param.length > 1) {
  539. if (param[0].value === '-') {
  540. param[1].value = '-' + param[1];
  541. }
  542. param.unshift();
  543. }
  544. const iLen = param.length;
  545. let iLeftCount = 0, iRightCount = 0;
  546. for (const [i, p] of param.entries()) {
  547. if (p.type === 'calc') {
  548. if (i === 0 || i === iLen - 1)
  549. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  550. }
  551. if (p.type === 'num') {
  552. num = p.value.replace('%', '');
  553. if (p.value.length - num.length > 1)
  554. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  555. num = _.toNumber(num);
  556. if (num === undefined || num === null || _.isNaN(num))
  557. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  558. if (i > 0) {
  559. if (param[i - 1].type !== 'calc') {
  560. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  561. } else if (param[i - 1].value === '/' && num === 0) {
  562. return [false, '输入的表达式非法:请勿除0'];
  563. }
  564. }
  565. }
  566. if (p.type === 'base') {
  567. const baseParam = _.find(calcBase, {code: p.value});
  568. if (!baseParam)
  569. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  570. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  571. return [false, '不可使用计算基数' + p.value];
  572. if (i > 0 && param[i - 1].type === 'calc')
  573. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  574. }
  575. if (p.type === 'left') {
  576. iLeftCount += 1;
  577. if (i !== 0 && param[i-1].type !== 'calc')
  578. return [false, '输入的表达式非法:(前应有运算符'];
  579. }
  580. if (p.type === 'right') {
  581. iRightCount += 1;
  582. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  583. return [false, '输入的表达式非法:)后应有运算符'];
  584. if (iRightCount > iLeftCount)
  585. return [false, '输入的表达式非法:")"前无对应的"("'];
  586. }
  587. }
  588. if (iLeftCount > iRightCount)
  589. return [false, '输入的表达式非法:"("后无对应的")"'];
  590. return [true, ''];
  591. },
  592. _checkExpr: function (text) {
  593. if (text) {
  594. const num = _.toNumber(text);
  595. if (num) {
  596. console.log(num);
  597. } else {
  598. const expr = text.replace('=', '').toLowerCase();
  599. const [valid, msg] = this._checkExprValid(expr);
  600. if (!valid) return [valid, msg];
  601. }
  602. }
  603. return [true, ''];
  604. },
  605. };
  606. $('#expr_btn').click(function () {
  607. const expr = $('#expr').val();
  608. // 判断表达式格式
  609. const [valid, msg] = ExprObj._checkExpr(expr);
  610. if (!valid) {
  611. toastr.error(msg);
  612. }
  613. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr }, function (result) {
  614. m_tp = result.m_tp;
  615. resetTpTable();
  616. const sheet = materialSpread.getActiveSheet();
  617. const select = SpreadJsObj.getSelectObject(sheet);
  618. const index = materialBillsData.indexOf(select);
  619. materialBillsData.splice(index, 1, result.info);
  620. SpreadJsObj.reLoadRowData(sheet, index);
  621. $('#bcyy').modal('hide');
  622. });
  623. })
  624. } else {
  625. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  626. }
  627. $.subMenu({
  628. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  629. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  630. key: 'menu.1.0.0',
  631. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  632. callback: function (info) {
  633. if (info.mini) {
  634. $('.panel-title').addClass('fluid');
  635. $('#sub-menu').removeClass('panel-sidebar');
  636. } else {
  637. $('.panel-title').removeClass('fluid');
  638. $('#sub-menu').addClass('panel-sidebar');
  639. }
  640. autoFlashHeight();
  641. materialSpread.refresh();
  642. }
  643. });
  644. });