material.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 = _.isNumber(info.editingText) ? parseFloat(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. deletePress: function (sheet) {
  320. return;
  321. },
  322. clipboardPasted(e, info) {
  323. // const tree = info.sheet.zh_tree;
  324. // if (!tree) { return; }
  325. const hint = {
  326. codeEmpty: {type: 'error', msg: '请先输入编号'},
  327. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  328. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  329. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  330. numberCan: {type: 'error', msg: '请粘贴大于0并且小于3位小数的浮点数'},
  331. };
  332. console.log(info);
  333. const range = info.cellRange;
  334. const sortData = info.sheet.zh_data || [];
  335. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  336. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  337. return;
  338. }
  339. const data = [];
  340. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  341. let bPaste = true;
  342. const curRow = range.row + iRow;
  343. const materialData = {id: sortData[curRow].id};
  344. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  345. const curCol = range.col + iCol;
  346. const colSetting = info.sheet.zh_setting.cols[curCol];
  347. if (!colSetting) continue;
  348. let validText = info.sheet.getText(curRow, curCol);
  349. console.log(validText, isNaN(parseFloat(validText)));
  350. validText = _.isNumber(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  351. console.log(validText);
  352. const orgValue = sortData[curRow][colSetting.field];
  353. console.log(orgValue);
  354. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  355. continue;
  356. }
  357. materialData[colSetting.field] = validText;
  358. // if (colSetting.type === 'Number') {
  359. // const num = _.toNumber(materialData[colSetting.field]);
  360. // }
  361. }
  362. console.log(materialData);
  363. }
  364. if (data.length === 0) {
  365. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  366. return;
  367. }
  368. // if (info.sheet.zh_setting) {
  369. // const select = SpreadJsObj.getSelectObject(info.sheet);
  370. // const col = info.sheet.zh_setting.cols[info.col];
  371. // // 未改变值则不提交
  372. // const validText = info.editingText ? trimInvalidChar(info.editingText) : null;
  373. // const orgValue = select[col.field];
  374. // if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  375. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  376. // return;
  377. // }
  378. // // 判断部分值是否输入的是数字判断和数据计算
  379. // if (col.field === 'basic_price') {
  380. // if (isNaN(validText)) {
  381. // toastr.error('不能输入其它非数字类型字符');
  382. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  383. // return;
  384. // }
  385. // const num = parseFloat(validText);
  386. // if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  387. // toastr.error('请输入大于0并且小于3位小数的浮点数');
  388. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  389. // return;
  390. // }
  391. // }
  392. // if (col.field === 'msg_tp') {
  393. // if (isNaN(validText)) {
  394. // toastr.error('不能输入其它非数字类型字符');
  395. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  396. // return;
  397. // }
  398. // const num = parseFloat(validText);
  399. // if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  400. // toastr.error('请输入大于0并且小于3位小数的浮点数');
  401. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  402. // return;
  403. // }
  404. // }
  405. // if (col.field === 'm_up_risk') {
  406. // // 只能输入正整数
  407. // if (isNaN(validText)) {
  408. // toastr.error('不能输入其它非数字类型字符');
  409. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  410. // return;
  411. // }
  412. // const num = parseFloat(validText);
  413. // if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  414. // toastr.error('只能输入0-100的正整数');
  415. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  416. // return;
  417. // }
  418. // }
  419. // if (col.field === 'm_down_risk') {
  420. // // 只能输入正整数
  421. // if (isNaN(validText)) {
  422. // toastr.error('不能输入其它非数字类型字符');
  423. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  424. // return;
  425. // }
  426. // const num = parseFloat(validText);
  427. // if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  428. // toastr.error('只能输入0-100的正整数');
  429. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  430. // return;
  431. // }
  432. // }
  433. // if (col.field === 't_type') {
  434. // if (validText === 1) {
  435. // select.quantity = null;
  436. // select.expr = null;
  437. // select.m_tp = null;
  438. // }
  439. // }
  440. // select[col.field] = validText;
  441. // select.msg_spread = materialCol.getValue.msg_spread(select);
  442. // select.m_spread = materialCol.getValue.m_spread(select);
  443. // select.m_tp = materialCol.getValue.m_tp(select);
  444. // // 更新至服务器
  445. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  446. // m_tp = result.m_tp;
  447. // resetTpTable();
  448. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  449. // }, function () {
  450. // select[col.field] = orgValue;
  451. // SpreadJsObj.reLoadRowData(info.sheet, info.row);
  452. // });
  453. // }
  454. },
  455. setReadOnly: function(readOnly) {
  456. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  457. }
  458. };
  459. materialSpreadObj.refreshActn();
  460. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  461. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  462. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  463. // const sheet = materialSpread.getActiveSheet();
  464. // sheet.suspendPaint();
  465. // var range = sheet.getRange(-1, 8, -1, 1);
  466. // console.log(range);
  467. // range.cellType(new DatePickerCellType());
  468. // // range.formatter("yyyy-MM-dd");
  469. // sheet.resumePaint();
  470. if (!readOnly) {
  471. $('#add').click(materialSpreadObj.add);
  472. $('#del').click(materialSpreadObj.del);
  473. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  474. // 右键菜单
  475. $.contextMenu({
  476. selector: '#material-spread',
  477. build: function ($trigger, e) {
  478. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  479. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  480. },
  481. items: {
  482. 'create': {
  483. name: '新增材料',
  484. icon: 'fa-sign-in',
  485. callback: function (key, opt) {
  486. materialSpreadObj.add(materialSpread.getActiveSheet());
  487. },
  488. },
  489. 'delete': {
  490. name: '删除材料',
  491. icon: 'fa-remove',
  492. callback: function (key, opt) {
  493. materialSpreadObj.del(materialSpread.getActiveSheet());
  494. },
  495. disabled: function (key, opt) {
  496. const sheet = materialSpread.getActiveSheet();
  497. const select = SpreadJsObj.getSelectObject(sheet);
  498. materialSpreadObj.refreshActn();
  499. if (!readOnly && select && materialBase.isEdit(select)) {
  500. return false;
  501. } else {
  502. return true;
  503. }
  504. }
  505. },
  506. }
  507. });
  508. $('#changeRate').change(function () {
  509. const rate = parseInt($(this).val());
  510. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  511. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  512. const jzbqhs = ZhCalc.round(ZhCalc.mul(ZhCalc.add(pre_tp, m_tp), 1+rate/100), 2);
  513. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  514. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  515. });
  516. });
  517. $('#expr_select button').on('click', function () {
  518. const code = $(this).text();
  519. $('#expr').val($('#expr').val() + code);
  520. });
  521. const ExprObj = {
  522. _checkExprValid(expr, invalidParam) {
  523. if (!expr) return [true, null];
  524. const param = [];
  525. let num = '', base = '';
  526. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  527. if (/^[\d\.%]+/.test(expr[i])) {
  528. if (base !== '') {
  529. param.push({type: 'base', value: base});
  530. base = '';
  531. }
  532. num = num + expr[i];
  533. } else if (/^[a-z]/.test(expr[i])) {
  534. if (num !== '') {
  535. param.push({type: 'num', value: num});
  536. base = '';
  537. }
  538. base = base + expr[i];
  539. } else if (expr[i] === '(') {
  540. if (num !== '') {
  541. param.push({type: 'num', value: num});
  542. base = '';
  543. }
  544. if (base !== '') {
  545. param.push({type: 'base', value: base});
  546. base = '';
  547. }
  548. param.push({type: 'left', value: '('});
  549. } else if (expr[i] === ')') {
  550. if (num !== '') {
  551. param.push({type: 'num', value: num});
  552. base = '';
  553. }
  554. if (base !== '') {
  555. param.push({type: 'base', value: base});
  556. base = '';
  557. }
  558. param.push({type: 'right', value: ')'});
  559. } else if (/^[\+\-*\/]/.test(expr[i])) {
  560. if (num !== '') {
  561. param.push({type: 'num', value: num});
  562. base = '';
  563. }
  564. if (base !== '') {
  565. param.push({type: 'base', value: base});
  566. base = '';
  567. }
  568. param.push({type: 'calc', value: expr[i]});
  569. } else {
  570. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  571. }
  572. }
  573. if (num !== '') {
  574. param.push({type: 'num', value: num});
  575. base = '';
  576. }
  577. if (base !== '') {
  578. param.push({type: 'base', value: base});
  579. base = '';
  580. }
  581. if (param.length === 0) return true;
  582. if (param.length > 1) {
  583. if (param[0].value === '-') {
  584. param[1].value = '-' + param[1];
  585. }
  586. param.unshift();
  587. }
  588. const iLen = param.length;
  589. let iLeftCount = 0, iRightCount = 0;
  590. for (const [i, p] of param.entries()) {
  591. if (p.type === 'calc') {
  592. if (i === 0 || i === iLen - 1)
  593. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  594. }
  595. if (p.type === 'num') {
  596. num = p.value.replace('%', '');
  597. if (p.value.length - num.length > 1)
  598. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  599. num = _.toNumber(num);
  600. if (num === undefined || num === null || _.isNaN(num))
  601. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  602. if (i > 0) {
  603. if (param[i - 1].type !== 'calc') {
  604. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  605. } else if (param[i - 1].value === '/' && num === 0) {
  606. return [false, '输入的表达式非法:请勿除0'];
  607. }
  608. }
  609. }
  610. if (p.type === 'base') {
  611. const baseParam = _.find(calcBase, {code: p.value});
  612. if (!baseParam)
  613. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  614. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  615. return [false, '不可使用计算基数' + p.value];
  616. if (i > 0 && param[i - 1].type === 'calc')
  617. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  618. }
  619. if (p.type === 'left') {
  620. iLeftCount += 1;
  621. if (i !== 0 && param[i-1].type !== 'calc')
  622. return [false, '输入的表达式非法:(前应有运算符'];
  623. }
  624. if (p.type === 'right') {
  625. iRightCount += 1;
  626. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  627. return [false, '输入的表达式非法:)后应有运算符'];
  628. if (iRightCount > iLeftCount)
  629. return [false, '输入的表达式非法:")"前无对应的"("'];
  630. }
  631. }
  632. if (iLeftCount > iRightCount)
  633. return [false, '输入的表达式非法:"("后无对应的")"'];
  634. return [true, ''];
  635. },
  636. _checkExpr: function (text) {
  637. if (text) {
  638. const num = _.toNumber(text);
  639. if (num) {
  640. console.log(num);
  641. } else {
  642. const expr = text.replace('=', '').toLowerCase();
  643. const [valid, msg] = this._checkExprValid(expr);
  644. if (!valid) return [valid, msg];
  645. }
  646. }
  647. return [true, ''];
  648. },
  649. };
  650. $('#expr_btn').click(function () {
  651. const expr = $('#expr').val();
  652. // 判断表达式格式
  653. const [valid, msg] = ExprObj._checkExpr(expr);
  654. if (!valid) {
  655. toastr.error(msg);
  656. }
  657. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr }, function (result) {
  658. m_tp = result.m_tp;
  659. resetTpTable();
  660. const sheet = materialSpread.getActiveSheet();
  661. const select = SpreadJsObj.getSelectObject(sheet);
  662. const index = materialBillsData.indexOf(select);
  663. materialBillsData.splice(index, 1, result.info);
  664. SpreadJsObj.reLoadRowData(sheet, index);
  665. $('#bcyy').modal('hide');
  666. });
  667. })
  668. } else {
  669. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  670. }
  671. $.subMenu({
  672. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  673. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  674. key: 'menu.1.0.0',
  675. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  676. callback: function (info) {
  677. if (info.mini) {
  678. $('.panel-title').addClass('fluid');
  679. $('#sub-menu').removeClass('panel-sidebar');
  680. } else {
  681. $('.panel-title').removeClass('fluid');
  682. $('#sub-menu').addClass('panel-sidebar');
  683. }
  684. autoFlashHeight();
  685. materialSpread.refresh();
  686. }
  687. });
  688. });