material.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. function getPasteHint (str, row = '') {
  86. let returnObj = str;
  87. if (row) {
  88. returnObj.msg = '工料第' + (row+1) + '行' + str.msg;
  89. }
  90. return returnObj;
  91. }
  92. const is_numeric = (value) => {
  93. if (typeof(value) === 'object') {
  94. return false;
  95. } else {
  96. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  97. }
  98. };
  99. $(document).ready(() => {
  100. autoFlashHeight();
  101. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  102. const materialSpreadSetting = {
  103. cols: [
  104. {title: '调差类型', colSpan: '1', rowSpan: '2', field: 't_type', hAlign: 1, width: 80, formatter: '@', readOnly: 'readOnly.isUsed', cellType: 'customizeCombo', comboItems: materialType.t_type, cellTypeKey: 1},
  105. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isUsed'},
  106. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  107. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  108. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  109. {title: '工料分类', colSpan: '1', rowSpan: '2', field: 'm_type', hAlign: 1, width: 60, readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.m_type, cellTypeKey: 2},
  110. {title: '本期应耗数量', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  111. {title: '基准价', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  112. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  113. {title: '本期信息价|单价', colSpan: '3|1', rowSpan: '1|1', field: 'msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  114. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  115. {title: '|价差', colSpan: '1', rowSpan: '1|1', field: 'msg_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.msg_spread'},
  116. {title: '本期材料调差|上涨幅度(%)', colSpan: '4|1', rowSpan: '1|1', field: 'm_up_risk', hAlign: 2, width: 100, type: 'Number', readOnly: 'readOnly.isEdit'},
  117. {title: '|下跌幅度(%)', colSpan: '|1', rowSpan: '|1', field: 'm_down_risk', hAlign: 2, width: 100, type: 'Number', readOnly: 'readOnly.isEdit'},
  118. {title: '|有效价差', colSpan: '|1', rowSpan: '|1', field: 'm_spread', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  119. {title: '|调整金额', colSpan: '|1', rowSpan: '1|1', field: 'm_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  120. {title: '截止上期调差金额', colSpan: '1', rowSpan: '2', field: 'pre_tp', hAlign: 2, width: 120, type: 'Number', readOnly: true},
  121. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.remark'},
  122. ],
  123. emptyRows: 0,
  124. headRows: 2,
  125. headRowHeight: [25, 25],
  126. defaultRowHeight: 21,
  127. headerFont: '12px 微软雅黑',
  128. font: '12px 微软雅黑',
  129. readOnly: readOnly,
  130. };
  131. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  132. spCol.readOnly = true;
  133. spCol.cellType = 'activeImageBtn';
  134. spCol.normalImg = '#ellipsis-icon';
  135. spCol.indent = 5;
  136. spCol.showImage = function (data) {
  137. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  138. return data.t_type === 2;
  139. };
  140. materialSpreadSetting.imageClick = function (data) {
  141. if (data.t_type === 2) {
  142. $('#bcyy').modal('show');
  143. $('#materialbillsId').val(data.id);
  144. $('#expr').val(data.expr);
  145. if (!readOnly && data.mid === materialID) {
  146. $('#expr').attr('readOnly', false);
  147. $('#expr_btn').show();
  148. $('#expr_select').show();
  149. } else {
  150. $('#expr').attr('readOnly', true);
  151. $('#expr_btn').hide();
  152. $('#expr_select').hide();
  153. }
  154. }
  155. };
  156. const materialBase = {
  157. isUsed: function (data) {
  158. if (data.t_type === 2) {
  159. return data.mid === materialID;
  160. } else {
  161. const mlInfo = _.find(materialListData, { mb_id: data.id });
  162. return data.mid === materialID && mlInfo === undefined;
  163. }
  164. },
  165. isEdit: function (data) {
  166. // if (data.t_type === 2) {
  167. // return data.mid === materialID;
  168. // } else {
  169. // const mlInfo = _.find(materialListData, { mb_id: data.id });
  170. // return data.mid === materialID && mlInfo === undefined;
  171. // }
  172. return data.mid === materialID;
  173. },
  174. // isStage: function (data) {
  175. // return data.mid === materialID;
  176. // }
  177. }
  178. const materialCol = {
  179. getValue: {
  180. msg_spread: function (data) {
  181. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), 3);
  182. },
  183. m_spread : function (data) {
  184. const msg_spread = materialCol.getValue.msg_spread(data);
  185. 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));
  186. return Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? ZhCalc.round(ZhCalc.sub(msg_spread, cor), 3) : ZhCalc.round(ZhCalc.add(msg_spread, cor), 3)) : 0;
  187. },
  188. m_tp: function (data) {
  189. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), 2);
  190. }
  191. },
  192. readOnly: {
  193. isEdit: function (data) {
  194. return !(!readOnly && materialBase.isEdit(data));
  195. },
  196. isUsed: function (data) {
  197. return !(!readOnly && materialBase.isUsed(data));
  198. },
  199. remark: function () {
  200. return readOnly;
  201. },
  202. },
  203. };
  204. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  205. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  206. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  207. const materialSpreadObj = {
  208. refreshActn: function () {
  209. const setObjEnable = function (obj, enable) {
  210. if (enable) {
  211. obj.removeClass('disabled');
  212. } else {
  213. obj.addClass('disabled');
  214. }
  215. };
  216. const sheet = materialSpread.getActiveSheet();
  217. const select = SpreadJsObj.getSelectObject(sheet);
  218. // 还需判断是否已被调差清单调用
  219. setObjEnable($('#del'), !readOnly && select && materialBase.isEdit(select));
  220. },
  221. add: function () {
  222. const sheet = materialSpread.getActiveSheet();
  223. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  224. if (result) {
  225. materialBillsData.push(result);
  226. sheet.addRows(materialBillsData.length - 1, 1);
  227. SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  228. sheet.setSelection(materialBillsData.length - 1, 0, 1, 1);
  229. materialSpreadObj.refreshActn();
  230. }
  231. });
  232. },
  233. del: function () {
  234. const sheet = materialSpread.getActiveSheet();
  235. const select = SpreadJsObj.getSelectObject(sheet);
  236. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  237. m_tp = result.m_tp;
  238. resetTpTable();
  239. const index = materialBillsData.indexOf(select);
  240. materialBillsData.splice(index, 1);
  241. sheet.deleteRows(index, 1);
  242. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  243. const sel = sheet.getSelections();
  244. sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  245. materialSpreadObj.refreshActn();
  246. });
  247. },
  248. selectionChanged: function (e, info) {
  249. materialSpreadObj.refreshActn();
  250. const sel = info.sheet.getSelections()[0];
  251. const col = info.sheet.zh_setting.cols[sel.col];
  252. const data = SpreadJsObj.getSelectObject(info.sheet);
  253. materialSpreadObj.setReadOnly(true);
  254. },
  255. editEnded: function (e, info) {
  256. if (info.sheet.zh_setting) {
  257. const select = SpreadJsObj.getSelectObject(info.sheet);
  258. const col = info.sheet.zh_setting.cols[info.col];
  259. // 未改变值则不提交
  260. const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  261. const orgValue = select[col.field];
  262. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  263. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  264. return;
  265. }
  266. // 判断部分值是否输入的是数字判断和数据计算
  267. if (col.field === 'basic_price') {
  268. if (isNaN(validText)) {
  269. toastr.error('不能输入其它非数字类型字符');
  270. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  271. return;
  272. }
  273. const num = parseFloat(validText);
  274. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  275. toastr.error('请输入大于0并且小于3位小数的浮点数');
  276. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  277. return;
  278. }
  279. }
  280. if (col.field === 'msg_tp') {
  281. if (isNaN(validText)) {
  282. toastr.error('不能输入其它非数字类型字符');
  283. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  284. return;
  285. }
  286. const num = parseFloat(validText);
  287. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  288. toastr.error('请输入大于0并且小于3位小数的浮点数');
  289. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  290. return;
  291. }
  292. }
  293. if (col.field === 'm_up_risk') {
  294. // 只能输入正整数
  295. if (isNaN(validText)) {
  296. toastr.error('不能输入其它非数字类型字符');
  297. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  298. return;
  299. }
  300. const num = parseFloat(validText);
  301. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  302. toastr.error('只能输入0-100的正整数');
  303. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  304. return;
  305. }
  306. }
  307. if (col.field === 'm_down_risk') {
  308. // 只能输入正整数
  309. if (isNaN(validText)) {
  310. toastr.error('不能输入其它非数字类型字符');
  311. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  312. return;
  313. }
  314. const num = parseFloat(validText);
  315. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  316. toastr.error('只能输入0-100的正整数');
  317. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  318. return;
  319. }
  320. }
  321. if (col.field === 't_type') {
  322. if (validText === 1) {
  323. select.quantity = null;
  324. select.expr = null;
  325. select.m_tp = null;
  326. }
  327. }
  328. select[col.field] = validText;
  329. select.msg_spread = materialCol.getValue.msg_spread(select);
  330. select.m_spread = materialCol.getValue.m_spread(select);
  331. select.m_tp = materialCol.getValue.m_tp(select);
  332. // 更新至服务器
  333. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  334. m_tp = result.m_tp;
  335. resetTpTable();
  336. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  337. }, function () {
  338. select[col.field] = orgValue;
  339. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  340. });
  341. }
  342. },
  343. deletePress: function (sheet) {
  344. return;
  345. },
  346. clipboardPasted(e, info) {
  347. const hint = {
  348. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  349. codeEmpty: {type: 'error', msg: '请先输入编号,并且不超过15个字符'},
  350. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  351. codeError: {type: 'error', msg: '编号为纯数字时,不能为小数'},
  352. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  353. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  354. numberCan: {type: 'error', msg: '请粘贴大于0并且小于3位小数的浮点数'},
  355. };
  356. const range = info.cellRange;
  357. const sortData = info.sheet.zh_data || [];
  358. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  359. toastMessageUniq(hint.cellError);
  360. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  361. return;
  362. }
  363. const data = [];
  364. // const rowData = [];
  365. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  366. let bPaste = true;
  367. const curRow = range.row + iRow;
  368. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  369. const materialData = { id: sortData[curRow].id };
  370. const hintRow = range.rowCount > 1 ? curRow : '';
  371. let sameCol = 0;
  372. for (let iCol = 0; iCol < range.colCount; iCol++) {
  373. const curCol = range.col + iCol;
  374. const colSetting = info.sheet.zh_setting.cols[curCol];
  375. if (!colSetting) continue;
  376. let validText = info.sheet.getText(curRow, curCol);
  377. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  378. const orgValue = sortData[curRow][colSetting.field];
  379. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  380. sameCol++;
  381. if (range.colCount === sameCol) {
  382. bPaste = false;
  383. }
  384. continue;
  385. }
  386. if (colSetting.field === 'code' && (validText === '' || validText.length > 15)) {
  387. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  388. bPaste = false;
  389. continue;
  390. }
  391. if (colSetting.field === 'code' && validText.toString().indexOf('.') !== -1) {
  392. toastMessageUniq(getPasteHint(hint.codeError, hintRow));
  393. bPaste = false;
  394. continue;
  395. }
  396. if (colSetting.field === 'code' && _.find(sortData, { code: validText })) {
  397. toastMessageUniq(getPasteHint(hint.codeUsed, hintRow));
  398. bPaste = false;
  399. continue;
  400. }
  401. if (colSetting.field === 't_type' || colSetting.field === 'm_type') {
  402. continue;
  403. }
  404. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp'
  405. || colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  406. if (isNaN(validText)) {
  407. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  408. bPaste = false;
  409. continue;
  410. }
  411. const num = parseFloat(validText);
  412. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp') {
  413. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  414. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  415. bPaste = false;
  416. continue;
  417. }
  418. } else if (colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  419. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  420. toastMessageUniq(getPasteHint(hint.riskCan, hintRow));
  421. bPaste = false;
  422. continue;
  423. }
  424. }
  425. }
  426. materialData[colSetting.field] = validText;
  427. }
  428. if (materialData.code === '' || materialData.code === null) {
  429. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  430. bPaste = false;
  431. }
  432. if (bPaste) {
  433. materialData.msg_spread = materialCol.getValue.msg_spread(sortData[curRow]);
  434. materialData.m_spread = materialCol.getValue.m_spread(sortData[curRow]);
  435. materialData.m_tp = materialCol.getValue.m_tp(sortData[curRow]);
  436. data.push(materialData);
  437. // rowData.push(curRow);
  438. } else {
  439. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  440. }
  441. }
  442. if (data.length === 0) {
  443. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  444. return;
  445. }
  446. console.log(data);
  447. // 更新至服务器
  448. postData(window.location.pathname + '/save', { type:'paste', updateData: data }, function (result) {
  449. materialBillsData = result.info;
  450. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  451. // for (const row in rowData) {
  452. // materialBillsData.splice(index, 1, result.info[row]);
  453. // SpreadJsObj.reLoadRowData(info.sheet, row);
  454. // }
  455. m_tp = result.m_tp;
  456. resetTpTable();
  457. }, function () {
  458. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  459. return;
  460. });
  461. },
  462. setReadOnly: function(readOnly) {
  463. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  464. }
  465. };
  466. materialSpreadObj.refreshActn();
  467. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  468. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  469. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  470. // const sheet = materialSpread.getActiveSheet();
  471. // sheet.suspendPaint();
  472. // var range = sheet.getRange(-1, 8, -1, 1);
  473. // console.log(range);
  474. // range.cellType(new DatePickerCellType());
  475. // // range.formatter("yyyy-MM-dd");
  476. // sheet.resumePaint();
  477. if (!readOnly) {
  478. $('#add').click(materialSpreadObj.add);
  479. $('#del').click(materialSpreadObj.del);
  480. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  481. // 右键菜单
  482. $.contextMenu({
  483. selector: '#material-spread',
  484. build: function ($trigger, e) {
  485. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  486. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  487. },
  488. items: {
  489. 'create': {
  490. name: '新增材料',
  491. icon: 'fa-sign-in',
  492. callback: function (key, opt) {
  493. materialSpreadObj.add(materialSpread.getActiveSheet());
  494. },
  495. },
  496. 'delete': {
  497. name: '删除材料',
  498. icon: 'fa-remove',
  499. callback: function (key, opt) {
  500. materialSpreadObj.del(materialSpread.getActiveSheet());
  501. },
  502. disabled: function (key, opt) {
  503. const sheet = materialSpread.getActiveSheet();
  504. const select = SpreadJsObj.getSelectObject(sheet);
  505. materialSpreadObj.refreshActn();
  506. if (!readOnly && select && materialBase.isEdit(select)) {
  507. return false;
  508. } else {
  509. return true;
  510. }
  511. }
  512. },
  513. }
  514. });
  515. $('#changeRate').change(function () {
  516. const rate = parseInt($(this).val());
  517. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  518. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  519. const jzbqhs = ZhCalc.round(ZhCalc.mul(ZhCalc.add(pre_tp, m_tp), 1+rate/100), 2);
  520. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  521. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  522. });
  523. });
  524. $('#expr_select button').on('click', function () {
  525. const code = $(this).text();
  526. $('#expr').val($('#expr').val() + code);
  527. });
  528. const ExprObj = {
  529. _checkExprValid(expr, invalidParam) {
  530. if (!expr) return [true, null];
  531. const param = [];
  532. let num = '', base = '';
  533. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  534. if (/^[\d\.%]+/.test(expr[i])) {
  535. if (base !== '') {
  536. param.push({type: 'base', value: base});
  537. base = '';
  538. }
  539. num = num + expr[i];
  540. } else if (/^[a-z]/.test(expr[i])) {
  541. if (num !== '') {
  542. param.push({type: 'num', value: num});
  543. base = '';
  544. }
  545. base = base + expr[i];
  546. } else if (expr[i] === '(') {
  547. if (num !== '') {
  548. param.push({type: 'num', value: num});
  549. base = '';
  550. }
  551. if (base !== '') {
  552. param.push({type: 'base', value: base});
  553. base = '';
  554. }
  555. param.push({type: 'left', value: '('});
  556. } else if (expr[i] === ')') {
  557. if (num !== '') {
  558. param.push({type: 'num', value: num});
  559. base = '';
  560. }
  561. if (base !== '') {
  562. param.push({type: 'base', value: base});
  563. base = '';
  564. }
  565. param.push({type: 'right', value: ')'});
  566. } else if (/^[\+\-*\/]/.test(expr[i])) {
  567. if (num !== '') {
  568. param.push({type: 'num', value: num});
  569. base = '';
  570. }
  571. if (base !== '') {
  572. param.push({type: 'base', value: base});
  573. base = '';
  574. }
  575. param.push({type: 'calc', value: expr[i]});
  576. } else {
  577. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  578. }
  579. }
  580. if (num !== '') {
  581. param.push({type: 'num', value: num});
  582. base = '';
  583. }
  584. if (base !== '') {
  585. param.push({type: 'base', value: base});
  586. base = '';
  587. }
  588. if (param.length === 0) return true;
  589. if (param.length > 1) {
  590. if (param[0].value === '-') {
  591. param[1].value = '-' + param[1];
  592. }
  593. param.unshift();
  594. }
  595. const iLen = param.length;
  596. let iLeftCount = 0, iRightCount = 0;
  597. for (const [i, p] of param.entries()) {
  598. if (p.type === 'calc') {
  599. if (i === 0 || i === iLen - 1)
  600. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  601. }
  602. if (p.type === 'num') {
  603. num = p.value.replace('%', '');
  604. if (p.value.length - num.length > 1)
  605. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  606. num = _.toNumber(num);
  607. if (num === undefined || num === null || _.isNaN(num))
  608. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  609. if (i > 0) {
  610. if (param[i - 1].type !== 'calc') {
  611. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  612. } else if (param[i - 1].value === '/' && num === 0) {
  613. return [false, '输入的表达式非法:请勿除0'];
  614. }
  615. }
  616. }
  617. if (p.type === 'base') {
  618. const baseParam = _.find(calcBase, {code: p.value});
  619. if (!baseParam)
  620. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  621. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  622. return [false, '不可使用计算基数' + p.value];
  623. if (i > 0 && param[i - 1].type === 'calc')
  624. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  625. }
  626. if (p.type === 'left') {
  627. iLeftCount += 1;
  628. if (i !== 0 && param[i-1].type !== 'calc')
  629. return [false, '输入的表达式非法:(前应有运算符'];
  630. }
  631. if (p.type === 'right') {
  632. iRightCount += 1;
  633. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  634. return [false, '输入的表达式非法:)后应有运算符'];
  635. if (iRightCount > iLeftCount)
  636. return [false, '输入的表达式非法:")"前无对应的"("'];
  637. }
  638. }
  639. if (iLeftCount > iRightCount)
  640. return [false, '输入的表达式非法:"("后无对应的")"'];
  641. return [true, ''];
  642. },
  643. _checkExpr: function (text) {
  644. if (text) {
  645. const num = _.toNumber(text);
  646. if (num) {
  647. console.log(num);
  648. } else {
  649. const expr = text.replace('=', '').toLowerCase();
  650. const [valid, msg] = this._checkExprValid(expr);
  651. if (!valid) return [valid, msg];
  652. }
  653. }
  654. return [true, ''];
  655. },
  656. };
  657. $('#expr_btn').click(function () {
  658. const expr = $('#expr').val();
  659. // 判断表达式格式
  660. const [valid, msg] = ExprObj._checkExpr(expr);
  661. if (!valid) {
  662. toastr.error(msg);
  663. }
  664. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr }, function (result) {
  665. m_tp = result.m_tp;
  666. resetTpTable();
  667. const sheet = materialSpread.getActiveSheet();
  668. const select = SpreadJsObj.getSelectObject(sheet);
  669. const index = materialBillsData.indexOf(select);
  670. materialBillsData.splice(index, 1, result.info);
  671. SpreadJsObj.reLoadRowData(sheet, index);
  672. $('#bcyy').modal('hide');
  673. });
  674. })
  675. } else {
  676. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  677. }
  678. $.subMenu({
  679. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  680. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  681. key: 'menu.1.0.0',
  682. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  683. callback: function (info) {
  684. if (info.mini) {
  685. $('.panel-title').addClass('fluid');
  686. $('#sub-menu').removeClass('panel-sidebar');
  687. } else {
  688. $('.panel-title').removeClass('fluid');
  689. $('#sub-menu').addClass('panel-sidebar');
  690. }
  691. autoFlashHeight();
  692. materialSpread.refresh();
  693. }
  694. });
  695. });