material.js 34 KB

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