material.js 62 KB

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