material.js 35 KB

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