material.js 32 KB

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