material.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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.msg_tp'},
  108. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.remark'},
  109. {title: '|价差', colSpan: '|1', rowSpan: '|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', 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. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'},
  117. ],
  118. emptyRows: 0,
  119. headRows: 2,
  120. headRowHeight: [25, 25],
  121. defaultRowHeight: 21,
  122. headerFont: '12px 微软雅黑',
  123. font: '12px 微软雅黑',
  124. readOnly: readOnly,
  125. };
  126. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  127. spCol.readOnly = true;
  128. spCol.cellType = 'activeImageBtn';
  129. spCol.normalImg = '#ellipsis-icon';
  130. spCol.indent = 5;
  131. spCol.showImage = function (data) {
  132. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  133. return data.t_type === 2;
  134. };
  135. materialSpreadSetting.imageClick = function (data) {
  136. if (data.t_type === 2) {
  137. $('#bcyy').modal('show');
  138. $('#materialbillsId').val(data.id);
  139. $('#expr').val(data.expr);
  140. if (!readOnly) {
  141. $('#expr').attr('readOnly', false);
  142. $('#expr_btn').show();
  143. $('#expr_select').show();
  144. } else {
  145. $('#expr').attr('readOnly', true);
  146. $('#expr_btn').hide();
  147. $('#expr_select').hide();
  148. }
  149. }
  150. };
  151. const materialBase = {
  152. isUsed: function (data) {
  153. if (data.t_type === 2) {
  154. return data.mid === materialID;
  155. } else {
  156. const mlInfo = _.find(materialListData, { mb_id: data.id });
  157. return data.mid === materialID && mlInfo === undefined;
  158. }
  159. },
  160. isEdit: function (data) {
  161. // if (data.t_type === 2) {
  162. // return data.mid === materialID;
  163. // } else {
  164. // const mlInfo = _.find(materialListData, { mb_id: data.id });
  165. // return data.mid === materialID && mlInfo === undefined;
  166. // }
  167. return data.mid === materialID;
  168. },
  169. // isStage: function (data) {
  170. // return data.mid === materialID;
  171. // }
  172. }
  173. const materialCol = {
  174. getValue: {
  175. msg_spread: function (data) {
  176. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), 3);
  177. },
  178. m_spread : function (data) {
  179. const msg_spread = materialCol.getValue.msg_spread(data);
  180. 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));
  181. 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;
  182. },
  183. m_tp: function (data) {
  184. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), 2);
  185. }
  186. },
  187. readOnly: {
  188. isEdit: function (data) {
  189. return !(!readOnly && materialBase.isEdit(data));
  190. },
  191. isUsed: function (data) {
  192. return !(!readOnly && materialBase.isUsed(data));
  193. },
  194. remark: function () {
  195. return readOnly;
  196. },
  197. msg_tp: function () {
  198. return !(!readOnly && months.length === 0);
  199. }
  200. },
  201. };
  202. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  203. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  204. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  205. const materialSpreadObj = {
  206. refreshActn: function (rowCount = 1) {
  207. const setObjEnable = function (obj, enable) {
  208. if (enable) {
  209. obj.removeClass('disabled');
  210. } else {
  211. obj.addClass('disabled');
  212. }
  213. };
  214. const sheet = materialSpread.getActiveSheet();
  215. const select = SpreadJsObj.getSelectObject(sheet);
  216. // 还需判断是否已被调差清单调用
  217. setObjEnable($('#del'), !readOnly && select && materialBase.isUsed(select) && rowCount === 1);
  218. },
  219. add: function () {
  220. const sheet = materialSpread.getActiveSheet();
  221. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  222. if (result) {
  223. materialBillsData.push(result);
  224. // sheet.addRows(materialBillsData.length - 1, 1);
  225. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  226. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  227. sheet.setSelection(materialBillsData.length - 1, 0, 1, 1);
  228. materialSpreadObj.refreshActn();
  229. // 月信息价需要同时添加空白的list
  230. if (months.length > 0) {
  231. const one_month ={ mb_id: result.id, code: '', name: null, unit: null, };
  232. for (const m of months) {
  233. one_month[m] = null;
  234. }
  235. monthsList.push(one_month);
  236. materialMonthSpread.getActiveSheet().addRows(monthsList.length - 1, 1);
  237. SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), monthsList.length - 1);
  238. }
  239. }
  240. });
  241. },
  242. del: function () {
  243. const sheet = materialSpread.getActiveSheet();
  244. const select = SpreadJsObj.getSelectObject(sheet);
  245. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  246. m_tp = result.m_tp;
  247. resetTpTable();
  248. const index = materialBillsData.indexOf(select);
  249. materialBillsData.splice(index, 1);
  250. sheet.deleteRows(index, 1);
  251. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  252. const sel = sheet.getSelections();
  253. sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  254. materialSpreadObj.refreshActn();
  255. // 月信息价需要同时删除
  256. if (months.length > 0) {
  257. monthsList.splice(index, 1);
  258. materialMonthSpread.getActiveSheet().deleteRows(index, 1);
  259. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  260. }
  261. });
  262. },
  263. selectionChanged: function (e, info) {
  264. const sel = info.sheet.getSelections()[0];
  265. const col = info.sheet.zh_setting.cols[sel.col];
  266. materialSpreadObj.refreshActn(sel.rowCount);
  267. const data = SpreadJsObj.getSelectObject(info.sheet);
  268. materialSpreadObj.setReadOnly(true);
  269. },
  270. editEnded: function (e, info) {
  271. if (info.sheet.zh_setting) {
  272. const select = SpreadJsObj.getSelectObject(info.sheet);
  273. const col = info.sheet.zh_setting.cols[info.col];
  274. if (col.field === 'is_summary') {
  275. return;
  276. }
  277. // 未改变值则不提交
  278. const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  279. const orgValue = select[col.field];
  280. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  281. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  282. return;
  283. }
  284. // 判断部分值是否输入的是数字判断和数据计算
  285. if (col.field === 'basic_price') {
  286. if (isNaN(validText)) {
  287. toastr.error('不能输入其它非数字类型字符');
  288. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  289. return;
  290. }
  291. const num = parseFloat(validText);
  292. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  293. toastr.error('请输入大于0并且小于3位小数的浮点数');
  294. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  295. return;
  296. }
  297. }
  298. if (col.field === 'msg_tp') {
  299. if (isNaN(validText)) {
  300. toastr.error('不能输入其它非数字类型字符');
  301. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  302. return;
  303. }
  304. const num = parseFloat(validText);
  305. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  306. toastr.error('请输入大于0并且小于3位小数的浮点数');
  307. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  308. return;
  309. }
  310. }
  311. if (col.field === 'm_up_risk') {
  312. // 只能输入正整数
  313. if (isNaN(validText)) {
  314. toastr.error('不能输入其它非数字类型字符');
  315. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  316. return;
  317. }
  318. const num = parseFloat(validText);
  319. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  320. toastr.error('只能输入0-100的正整数');
  321. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  322. return;
  323. }
  324. }
  325. if (col.field === 'm_down_risk') {
  326. // 只能输入正整数
  327. if (isNaN(validText)) {
  328. toastr.error('不能输入其它非数字类型字符');
  329. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  330. return;
  331. }
  332. const num = parseFloat(validText);
  333. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  334. toastr.error('只能输入0-100的正整数');
  335. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  336. return;
  337. }
  338. }
  339. if (col.field === 't_type') {
  340. if (validText === 1) {
  341. select.quantity = null;
  342. select.expr = null;
  343. select.m_tp = null;
  344. }
  345. }
  346. select[col.field] = validText;
  347. select.msg_spread = materialCol.getValue.msg_spread(select);
  348. select.m_spread = materialCol.getValue.m_spread(select);
  349. select.m_tp = materialCol.getValue.m_tp(select);
  350. delete select.waitingLoading;
  351. // console.log(select);
  352. // 更新至服务器
  353. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  354. m_tp = result.m_tp;
  355. resetTpTable();
  356. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  357. // 判断如果是更改了编号,名称,单位,月信息价需要跟着改变值
  358. if (months.length > 0 && (col.field === 'code' || col.field === 'name' || col.field === 'unit')) {
  359. monthsList[info.row][col.field] = validText;
  360. SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), info.row);
  361. }
  362. }, function () {
  363. select[col.field] = orgValue;
  364. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  365. });
  366. }
  367. },
  368. buttonClicked: function (e, info) {
  369. if (info.sheet.zh_setting) {
  370. const select = SpreadJsObj.getSelectObject(info.sheet);
  371. const col = info.sheet.zh_setting.cols[info.col];
  372. if (materialCol.readOnly.isEdit(select)) {
  373. return;
  374. }
  375. if (col.field === 'is_summary') {
  376. if (info.sheet.isEditing()) {
  377. info.sheet.endEdit(true);
  378. }
  379. select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0;
  380. delete select.waitingLoading;
  381. // 更新至服务器
  382. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  383. m_tp = result.m_tp;
  384. resetTpTable();
  385. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  386. }, function () {
  387. select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1;
  388. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  389. });
  390. }
  391. }
  392. },
  393. deletePress: function (sheet) {
  394. return;
  395. },
  396. clipboardPasted(e, info) {
  397. const hint = {
  398. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  399. codeEmpty: {type: 'error', msg: '请先输入编号,并且不超过15个字符'},
  400. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  401. codeError: {type: 'error', msg: '编号为纯数字时,不能为小数'},
  402. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  403. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  404. numberCan: {type: 'error', msg: '请粘贴大于0并且小于3位小数的浮点数'},
  405. };
  406. const range = info.cellRange;
  407. const sortData = info.sheet.zh_data || [];
  408. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  409. toastMessageUniq(hint.cellError);
  410. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  411. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  412. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  413. return;
  414. }
  415. if (sortData.length > 0 && range.col + range.colCount > 18) {
  416. toastMessageUniq(hint.cellError);
  417. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  418. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  419. return;
  420. }
  421. const data = [];
  422. // const rowData = [];
  423. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  424. let bPaste = true;
  425. const curRow = range.row + iRow;
  426. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  427. const materialData = { id: sortData[curRow].id };
  428. const hintRow = range.rowCount > 1 ? curRow : '';
  429. let sameCol = 0;
  430. for (let iCol = 0; iCol < range.colCount; iCol++) {
  431. const curCol = range.col + iCol;
  432. const colSetting = info.sheet.zh_setting.cols[curCol];
  433. if (!colSetting) continue;
  434. let validText = info.sheet.getText(curRow, curCol);
  435. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  436. const orgValue = sortData[curRow][colSetting.field];
  437. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  438. sameCol++;
  439. if (range.colCount === sameCol) {
  440. bPaste = false;
  441. }
  442. continue;
  443. }
  444. if (colSetting.field === 'code' && (validText === '' || validText.length > 15)) {
  445. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  446. bPaste = false;
  447. continue;
  448. }
  449. if (colSetting.field === 'code' && validText.toString().indexOf('.') !== -1) {
  450. toastMessageUniq(getPasteHint(hint.codeError, hintRow));
  451. bPaste = false;
  452. continue;
  453. }
  454. if (colSetting.field === 'code' && _.find(sortData, { code: validText.toString() })) {
  455. toastMessageUniq(getPasteHint(hint.codeUsed, hintRow));
  456. bPaste = false;
  457. continue;
  458. }
  459. if (colSetting.field === 't_type' || colSetting.field === 'm_type') {
  460. continue;
  461. }
  462. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp'
  463. || colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  464. if (isNaN(validText)) {
  465. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  466. bPaste = false;
  467. continue;
  468. }
  469. const num = parseFloat(validText);
  470. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp') {
  471. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  472. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  473. bPaste = false;
  474. continue;
  475. }
  476. } else if (colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  477. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  478. toastMessageUniq(getPasteHint(hint.riskCan, hintRow));
  479. bPaste = false;
  480. continue;
  481. }
  482. }
  483. }
  484. materialData[colSetting.field] = validText;
  485. sortData[curRow][colSetting.field] = validText;
  486. }
  487. if (materialData.code === '' || materialData.code === null) {
  488. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  489. bPaste = false;
  490. }
  491. if (bPaste) {
  492. materialData.msg_spread = materialCol.getValue.msg_spread(sortData[curRow]);
  493. materialData.m_spread = materialCol.getValue.m_spread(sortData[curRow]);
  494. materialData.m_tp = materialCol.getValue.m_tp(sortData[curRow]);
  495. data.push(materialData);
  496. // rowData.push(curRow);
  497. } else {
  498. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  499. }
  500. }
  501. if (data.length === 0) {
  502. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  503. return;
  504. }
  505. // console.log(data);
  506. // 更新至服务器
  507. postData(window.location.pathname + '/save', { type:'paste', updateData: data }, function (result) {
  508. materialBillsData = result.info;
  509. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  510. // for (const row in rowData) {
  511. // materialBillsData.splice(index, 1, result.info[row]);
  512. // SpreadJsObj.reLoadRowData(info.sheet, row);
  513. // }
  514. if (months.length > 0) {
  515. for (const [i,m] of monthsList.entries()) {
  516. m.code = materialBillsData[i].code;
  517. m.name = materialBillsData[i].name;
  518. m.unit = materialBillsData[i].unit;
  519. }
  520. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  521. }
  522. m_tp = result.m_tp;
  523. resetTpTable();
  524. }, function () {
  525. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  526. return;
  527. });
  528. },
  529. setReadOnly: function(readOnly) {
  530. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  531. }
  532. };
  533. materialSpreadObj.refreshActn();
  534. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  535. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  536. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  537. const sheet = materialSpread.getActiveSheet();
  538. sheet.suspendPaint();
  539. // const basic_range = sheet.getRange(-1, 8, -1, 1);
  540. // basic_range.cellType(new DatePickerCellType());
  541. // basic_range.formatter("yyyy-MM-dd");
  542. // const msg_range = sheet.getRange(-1, 10, -1, 1);
  543. // msg_range.cellType(new DatePickerCellType());
  544. // msg_range.formatter("yyyy-MM-dd");
  545. sheet.resumePaint();
  546. const static_cols = [
  547. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: true},
  548. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 140, formatter: '@', readOnly: true},
  549. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true},
  550. {title: '来源地', colSpan: '1', rowSpan: '2', field: 'origin', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.isEdit'},
  551. {title: '平均单价', colSpan: '1', rowSpan: '2', field: 'average_msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue:'getValue.average_msg_tp'},
  552. ];
  553. // 月信息价方法集合
  554. const monthFunGather = {
  555. _setMonthHtml: function() {
  556. let html = '';
  557. let qihtml = '';
  558. for (const m of months) {
  559. html += '<div class="custom-control custom-checkbox mb-2">\n' +
  560. ' <input type="checkbox" name="del_month" value="' + m + '" class="custom-control-input" id="month_' + m + '">\n' +
  561. ' <label class="custom-control-label" for="month_' + m + '">' + m + '月</label>\n' +
  562. ' </div>';
  563. qihtml += parseInt(m.split('-')[1]) + '月,';
  564. }
  565. if (months.length > 0) {
  566. qihtml = '<span class="mx-2 text-muted">/</span>本期月信息价:' + qihtml;
  567. qihtml = qihtml.substring(0, qihtml.length-1);
  568. }
  569. $('#show_month').html(html);
  570. $('#qi-month').html(qihtml);
  571. },
  572. _monthHeaderSet: function() {
  573. const newMonths = [];
  574. for (const m of months) {
  575. const year = m.split('-')[0];
  576. const month = parseInt(m.split('-')[1]);
  577. let one = _.find(newMonths, { 'year': year });
  578. let oneIndex = _.findIndex(newMonths, { 'year': year });
  579. if (one) {
  580. one.month.push(month);
  581. newMonths.splice(oneIndex, 1, one);
  582. } else {
  583. one = {
  584. year: year,
  585. month: [ month ],
  586. };
  587. newMonths.push(one);
  588. }
  589. }
  590. const pushMonth = [];
  591. for(const mo of newMonths) {
  592. for (let i in mo.month) {
  593. i = parseInt(i);
  594. const newCols = {
  595. title: (i === 0 ? mo.year: '') + '|' + mo.month[i] + '月',
  596. colSpan: (i === 0 ? (mo.month.length === 1 ? '0' : mo.month.length) : '') + '|1',
  597. rowSpan: i === 0 ? '1|1' : '|1',
  598. field: mo.year + '-' + (mo.month[i] < 10 ? '0' + mo.month[i] : mo.month[i]),
  599. hAlign: 2, width: 60, type: '@', readOnly: 'readOnly.isEdit'};
  600. pushMonth.push(newCols);
  601. }
  602. }
  603. return pushMonth;
  604. },
  605. monthSheetReset: function () {
  606. const monthCols = monthFunGather._monthHeaderSet();
  607. const newMonthSpreadHeaderCols = static_cols.concat(monthCols);
  608. materialMonthSpreadSetting.cols = newMonthSpreadHeaderCols;
  609. // 表头变化需要重新绘制,不然报错;
  610. materialMonthSpread.getActiveSheet().reset();
  611. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  612. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  613. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  614. monthFunGather._setMonthHtml();
  615. }
  616. }
  617. const materialMonthSpread = SpreadJsObj.createNewSpread($('#material-month-spread')[0]);
  618. const materialMonthSpreadSetting = {
  619. cols: static_cols,
  620. emptyRows: 0,
  621. headRows: 2,
  622. headRowHeight: [25, 25],
  623. defaultRowHeight: 21,
  624. headerFont: '12px 微软雅黑',
  625. font: '12px 微软雅黑',
  626. readOnly: readOnly,
  627. };
  628. if (months.length > 0) {
  629. const monthCols = monthFunGather._monthHeaderSet();
  630. const monthSpreadHeaderCols = static_cols.concat(monthCols);
  631. materialMonthSpreadSetting.cols = monthSpreadHeaderCols;
  632. }
  633. const materialMonthCol = {
  634. getValue: {
  635. average_msg_tp: function (data) {
  636. let msg_tp = 0;
  637. let hadnum = 0;
  638. for (const m of months) {
  639. msg_tp += data[m];
  640. if (data[m] !== null && data[m] !== '' && data[m] !== 0) {
  641. hadnum++;
  642. }
  643. }
  644. const average_tp = hadnum !== 0 ? ZhCalc.round(ZhCalc.div(msg_tp, hadnum), 3) : ZhCalc.round(ZhCalc.div(msg_tp, months.length), 3);
  645. return average_tp;
  646. },
  647. },
  648. readOnly: {
  649. isEdit: function (data) {
  650. return readOnly;
  651. },
  652. },
  653. };
  654. materialMonthSpread.getActiveSheet().frozenColumnCount(5);
  655. materialMonthSpread.getActiveSheet().options.frozenlineColor = '#f1f1f1';
  656. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  657. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  658. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  659. const materialMonthSpreadObj = {
  660. editEnded: function (e, info) {
  661. if (info.sheet.zh_setting) {
  662. const select = SpreadJsObj.getSelectObject(info.sheet);
  663. const col = info.sheet.zh_setting.cols[info.col];
  664. // 未改变值则不提交
  665. const validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  666. const orgValue = select[col.field];
  667. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  668. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  669. return;
  670. }
  671. if (col.field === 'origin') {
  672. select[col.field] = validText;
  673. postData(window.location.pathname + '/month/save', { type:'updateOrigin', updateData: { mb_id: select.mb_id, value: validText } }, function (result) {
  674. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  675. materialBillsData = result.materialBillsData;
  676. }, function () {
  677. select[col.field] = orgValue;
  678. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  679. });
  680. } else {
  681. // 判断部分值是否输入的是数字判断和数据计算
  682. if (isNaN(validText)) {
  683. toastr.error('不能输入其它非数字类型字符');
  684. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  685. return;
  686. }
  687. const num = parseFloat(validText);
  688. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  689. toastr.error('请输入大于0并且小于3位小数的浮点数');
  690. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  691. return;
  692. }
  693. select[col.field] = validText;
  694. // 更新至服务器
  695. postData(window.location.pathname + '/month/save', { type:'update', updateData: { mb_id: select.mb_id, yearmonth: col.field, value: validText } }, function (result) {
  696. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  697. materialBillsData = result.materialBillsData;
  698. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  699. m_tp = result.m_tp;
  700. resetTpTable();
  701. }, function () {
  702. select[col.field] = orgValue;
  703. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  704. });
  705. }
  706. }
  707. },
  708. deletePress: function (sheet) {
  709. // if (sheet.zh_setting && sheet.zh_data) {
  710. // const sel = sheet.getSelections()[0];
  711. // if (!sel) return;
  712. //
  713. // const col = sheet.zh_setting.cols[sel.col];
  714. // const select = SpreadJsObj.getSelectObject(sheet);
  715. // const orgValue = select[col.field];
  716. // if (sel.colCount > 1 || sel.rowCount > 1) {
  717. // toastr.warning('请勿同时删除多行或多列数据');
  718. // }
  719. // console.log(sel, select, col);
  720. // if (orgValue === null || col.field === 'type' || col.field === 'is_summary') {
  721. // return;
  722. // }
  723. // select[col.field] = null;
  724. // select.calc_num = materialExponentCol.getValue.calc_num(select);
  725. // console.log(select);
  726. // 更新至服务器
  727. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  728. // ex_tp = result.ex_tp;
  729. // ex_expr = result.ex_expr;
  730. // resetExTpTable();
  731. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  732. // materialExponentData.splice(sel.row, 1, select);
  733. // }, function () {
  734. // select[col.field] = orgValue;
  735. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  736. // });
  737. // }
  738. return;
  739. },
  740. clipboardPasted(e, info) {
  741. const hint = {
  742. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  743. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  744. numberCan: {type: 'error', msg: '请粘贴大于0并且小于3位小数的浮点数'},
  745. };
  746. const range = info.cellRange;
  747. const sortData = info.sheet.zh_data || [];
  748. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  749. toastMessageUniq(hint.cellError);
  750. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  751. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  752. return;
  753. }
  754. if (sortData.length > 0 && range.col + range.colCount > 5 + months.length) {
  755. toastMessageUniq(hint.cellError);
  756. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  757. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  758. return;
  759. }
  760. const data = [];
  761. let pasteOrigin = false;
  762. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  763. let bPaste = true;
  764. const curRow = range.row + iRow;
  765. const materialMonthData = sortData[curRow];
  766. const hintRow = range.rowCount > 1 ? curRow : '';
  767. let sameCol = 0;
  768. for (let iCol = 0; iCol < range.colCount; iCol++) {
  769. const curCol = range.col + iCol;
  770. const colSetting = info.sheet.zh_setting.cols[curCol];
  771. if (!colSetting) continue;
  772. let validText = info.sheet.getText(curRow, curCol);
  773. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  774. const orgValue = sortData[curRow][colSetting.field];
  775. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  776. sameCol++;
  777. if (range.colCount === sameCol) {
  778. bPaste = false;
  779. }
  780. continue;
  781. }
  782. if (colSetting.field === 'origin') {
  783. pasteOrigin = true;
  784. } else {
  785. const num = parseFloat(validText);
  786. if (isNaN(validText)) {
  787. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  788. bPaste = false;
  789. continue;
  790. }
  791. if (validText !== null && (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num))) {
  792. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  793. bPaste = false;
  794. continue;
  795. }
  796. }
  797. materialMonthData[colSetting.field] = validText;
  798. sortData[curRow][colSetting.field] = validText;
  799. }
  800. if (bPaste) {
  801. data.push(materialMonthData);
  802. } else {
  803. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  804. }
  805. }
  806. if (data.length === 0) {
  807. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  808. return;
  809. }
  810. if (pasteOrigin) {
  811. // 更新至服务器
  812. postData(window.location.pathname + '/month/save', { type:'pasteOrigin', updateData: data }, function (result) {
  813. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  814. materialBillsData = result.materialBillsData;
  815. }, function () {
  816. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  817. return;
  818. });
  819. } else {
  820. // 更新至服务器
  821. postData(window.location.pathname + '/month/save', { type:'paste', updateData: data }, function (result) {
  822. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  823. materialBillsData = result.materialBillsData;
  824. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  825. m_tp = result.m_tp;
  826. resetTpTable();
  827. }, function () {
  828. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  829. return;
  830. });
  831. }
  832. },
  833. };
  834. materialMonthSpread.bind(spreadNS.Events.ClipboardPasted, materialMonthSpreadObj.clipboardPasted);
  835. SpreadJsObj.addDeleteBind(materialMonthSpread, materialMonthSpreadObj.deletePress);
  836. if (!readOnly) {
  837. $('#add').click(materialSpreadObj.add);
  838. $('#del').click(materialSpreadObj.del);
  839. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  840. materialSpread.bind(spreadNS.Events.ButtonClicked, materialSpreadObj.buttonClicked);
  841. materialMonthSpread.bind(spreadNS.Events.EditEnded, materialMonthSpreadObj.editEnded);
  842. // 右键菜单
  843. $.contextMenu({
  844. selector: '#material-spread',
  845. build: function ($trigger, e) {
  846. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  847. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  848. },
  849. items: {
  850. 'create': {
  851. name: '新增材料',
  852. icon: 'fa-sign-in',
  853. callback: function (key, opt) {
  854. materialSpreadObj.add(materialSpread.getActiveSheet());
  855. },
  856. },
  857. 'delete': {
  858. name: '删除材料',
  859. icon: 'fa-remove',
  860. callback: function (key, opt) {
  861. materialSpreadObj.del(materialSpread.getActiveSheet());
  862. },
  863. disabled: function (key, opt) {
  864. const sheet = materialSpread.getActiveSheet();
  865. const select = SpreadJsObj.getSelectObject(sheet);
  866. const sel = sheet.getSelections()[0];
  867. materialSpreadObj.refreshActn(sel.rowCount);
  868. if (!readOnly && select && materialBase.isUsed(select) && sel.rowCount === 1) {
  869. return false;
  870. } else {
  871. return true;
  872. }
  873. }
  874. },
  875. }
  876. });
  877. $('#changeRate').change(function () {
  878. const rate = parseInt($(this).val());
  879. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  880. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  881. const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), 2);
  882. const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), 2);
  883. const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), 2);
  884. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  885. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  886. $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  887. $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  888. });
  889. });
  890. $('#expr_select button').on('click', function () {
  891. const code = $(this).text();
  892. // $('#expr').val($('#expr').val() + code);
  893. $('#expr').insertAtCaret(code);
  894. });
  895. const ExprObj = {
  896. _checkExprValid(expr, invalidParam) {
  897. if (!expr) return [true, null];
  898. const param = [];
  899. let num = '', base = '';
  900. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  901. if (/^[\d\.%]+/.test(expr[i])) {
  902. if (base !== '') {
  903. param.push({type: 'base', value: base});
  904. base = '';
  905. }
  906. num = num + expr[i];
  907. } else if (/^[a-z]/.test(expr[i])) {
  908. if (num !== '') {
  909. param.push({type: 'num', value: num});
  910. num = '';
  911. }
  912. base = base + expr[i];
  913. } else if (expr[i] === '(') {
  914. if (num !== '') {
  915. param.push({type: 'num', value: num});
  916. num = '';
  917. }
  918. if (base !== '') {
  919. param.push({type: 'base', value: base});
  920. base = '';
  921. }
  922. param.push({type: 'left', value: '('});
  923. } else if (expr[i] === ')') {
  924. if (num !== '') {
  925. param.push({type: 'num', value: num});
  926. num = '';
  927. }
  928. if (base !== '') {
  929. param.push({type: 'base', value: base});
  930. base = '';
  931. }
  932. param.push({type: 'right', value: ')'});
  933. } else if (/^[\+\-*\/]/.test(expr[i])) {
  934. if (num !== '') {
  935. param.push({type: 'num', value: num});
  936. num = '';
  937. }
  938. if (base !== '') {
  939. param.push({type: 'base', value: base});
  940. base = '';
  941. }
  942. param.push({type: 'calc', value: expr[i]});
  943. } else {
  944. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  945. }
  946. }
  947. if (num !== '') {
  948. param.push({type: 'num', value: num});
  949. num = '';
  950. }
  951. if (base !== '') {
  952. param.push({type: 'base', value: base});
  953. base = '';
  954. }
  955. if (param.length === 0) return true;
  956. if (param.length > 1) {
  957. if (param[0].value === '-') {
  958. param[1].value = '-' + param[1];
  959. }
  960. param.unshift();
  961. }
  962. const iLen = param.length;
  963. let iLeftCount = 0, iRightCount = 0;
  964. for (const [i, p] of param.entries()) {
  965. if (p.type === 'calc') {
  966. if (i === 0 || i === iLen - 1)
  967. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  968. }
  969. if (p.type === 'num') {
  970. num = p.value.replace('%', '');
  971. if (p.value.length - num.length > 1)
  972. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  973. num = _.toNumber(num);
  974. if (num === undefined || num === null || _.isNaN(num))
  975. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  976. if (i > 0) {
  977. if (param[i - 1].type !== 'calc') {
  978. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  979. } else if (param[i - 1].value === '/' && num === 0) {
  980. return [false, '输入的表达式非法:请勿除0'];
  981. }
  982. }
  983. }
  984. if (p.type === 'base') {
  985. const baseParam = _.find(calcBase, {code: p.value});
  986. if (!baseParam)
  987. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  988. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  989. return [false, '不可使用计算基数' + p.value];
  990. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  991. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  992. }
  993. if (p.type === 'left') {
  994. iLeftCount += 1;
  995. if (i !== 0 && param[i-1].type !== 'calc')
  996. return [false, '输入的表达式非法:(前应有运算符'];
  997. }
  998. if (p.type === 'right') {
  999. iRightCount += 1;
  1000. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  1001. return [false, '输入的表达式非法:)后应有运算符'];
  1002. if (iRightCount > iLeftCount)
  1003. return [false, '输入的表达式非法:")"前无对应的"("'];
  1004. }
  1005. }
  1006. if (iLeftCount > iRightCount)
  1007. return [false, '输入的表达式非法:"("后无对应的")"'];
  1008. return [true, ''];
  1009. },
  1010. _checkExpr: function (text) {
  1011. if (text) {
  1012. const num = _.toNumber(text);
  1013. if (num) {
  1014. console.log(num);
  1015. } else {
  1016. const expr = text.replace('=', '').toLowerCase();
  1017. const [valid, msg] = this._checkExprValid(expr);
  1018. if (!valid) return [valid, msg];
  1019. }
  1020. }
  1021. return [true, ''];
  1022. },
  1023. };
  1024. $('#expr_btn').click(function () {
  1025. const expr = $('#expr').val();
  1026. // 判断表达式格式
  1027. const [valid, msg] = ExprObj._checkExpr(expr);
  1028. if (!valid) {
  1029. toastr.error(msg);
  1030. return false;
  1031. }
  1032. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr }, function (result) {
  1033. m_tp = result.m_tp;
  1034. resetTpTable();
  1035. const sheet = materialSpread.getActiveSheet();
  1036. const select = SpreadJsObj.getSelectObject(sheet);
  1037. const index = materialBillsData.indexOf(select);
  1038. materialBillsData.splice(index, 1, result.info);
  1039. SpreadJsObj.reLoadRowData(sheet, index);
  1040. $('#bcyy').modal('hide');
  1041. });
  1042. });
  1043. // 创建月信息价
  1044. $('#make-month').click(function() {
  1045. const yearmonth = $('#months').val();
  1046. const result = yearmonth.match(/^(\d{1,4})(-|\/)(\d{1,2})$/);
  1047. if (result === null) {
  1048. toastr.error('请选择正确的信息价月份');
  1049. return false;
  1050. }
  1051. //判断是否已存在当前月份
  1052. if (months.indexOf(yearmonth) !== -1) {
  1053. toastr.error('调差期已创建过本月的信息价');
  1054. return false;
  1055. }
  1056. postData(window.location.pathname + '/month/save', { type: 'add', updateData: { yearmonth: yearmonth } }, function (data) {
  1057. months.push(yearmonth);
  1058. months.sort();
  1059. monthsList = data.monthsList;
  1060. monthFunGather.monthSheetReset();
  1061. // 工料表单价显示也要更新
  1062. materialBillsData = data.materialBillsData;
  1063. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1064. m_tp = data.m_tp;
  1065. resetTpTable();
  1066. $('#add-month').modal('hide');
  1067. });
  1068. });
  1069. // 删除月信息价
  1070. $('#del-month').click(function () {
  1071. const del_month_array = [];
  1072. $("input[name='del_month']:checked").each(function () {
  1073. del_month_array.push($(this).val());
  1074. });
  1075. if (del_month_array.length === 0) {
  1076. toastr.error('请选择要移除的月信息价');
  1077. return false;
  1078. }
  1079. postData(window.location.pathname + '/month/save', { type: 'del', updateData: { del_yearmonth: del_month_array } }, function (data) {
  1080. for (const dm of del_month_array) {
  1081. // _.remove(materialMonthSpreadSetting.cols, function (n) {
  1082. // return n.field === dm;
  1083. // });
  1084. _.remove(months, function (n) {
  1085. return n === dm;
  1086. });
  1087. }
  1088. monthsList = data.monthsList;
  1089. monthFunGather.monthSheetReset();
  1090. // 工料表单价显示也要更新
  1091. materialBillsData = data.materialBillsData;
  1092. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1093. m_tp = data.m_tp;
  1094. resetTpTable();
  1095. $('#remove-month').modal('hide');
  1096. });
  1097. });
  1098. } else {
  1099. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  1100. }
  1101. $.subMenu({
  1102. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  1103. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  1104. key: 'menu.1.0.0',
  1105. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  1106. callback: function (info) {
  1107. if (info.mini) {
  1108. $('.panel-title').addClass('fluid');
  1109. $('#sub-menu').removeClass('panel-sidebar');
  1110. } else {
  1111. $('.panel-title').removeClass('fluid');
  1112. $('#sub-menu').addClass('panel-sidebar');
  1113. }
  1114. autoFlashHeight();
  1115. materialSpread.refresh();
  1116. }
  1117. });
  1118. $.divResizer({
  1119. select: '#right-spr',
  1120. callback: function () {
  1121. materialSpread.refresh();
  1122. materialMonthSpread.refresh();
  1123. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1124. setLocalCache('material_month_' + materialID, width);
  1125. }
  1126. });
  1127. // 展开收起月信息价并浏览器记住本期展开收起
  1128. $('a', '.right-nav').bind('click', function () {
  1129. //const main = $('#main-view'), tool = $('#tools-view');
  1130. const tab = $(this), tabPanel = $(tab.attr('content'));
  1131. if (!tab.hasClass('active')) {
  1132. $('a', '.side-menu').removeClass('active');
  1133. $('.tab-content .tab-select-show').removeClass('active');
  1134. tab.addClass('active');
  1135. tabPanel.addClass('active');
  1136. showSideTools(tab.hasClass('active'));
  1137. if (tab.attr('content') === '#month-tab') {
  1138. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  1139. setLocalCache('material_month_' + materialID, width);
  1140. materialMonthSpread.refresh();
  1141. }
  1142. } else {
  1143. removeLocalCache('material_month_' + materialID);
  1144. tab.removeClass('active');
  1145. tabPanel.removeClass('active');
  1146. showSideTools(tab.hasClass('active'));
  1147. }
  1148. materialSpread.refresh();
  1149. materialMonthSpread.refresh();
  1150. });
  1151. // 根据浏览器记录展开收起
  1152. if (getLocalCache('material_month_' + materialID)) {
  1153. const tab = $('.right-nav a[content="#month-tab"]'), tabPanel = $(tab.attr('content'));
  1154. $('a', '.side-menu').removeClass('active');
  1155. $('.tab-content .tab-select-show').removeClass('active');
  1156. tab.addClass('active');
  1157. tabPanel.addClass('active');
  1158. $('#right-view').width(getLocalCache('material_month_' + materialID) + '%');
  1159. showSideTools(tab.hasClass('active'));
  1160. materialSpread.refresh();
  1161. materialMonthSpread.refresh();
  1162. }
  1163. });