material.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. $(document).ready(() => {
  21. autoFlashHeight();
  22. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  23. const materialSpreadSetting = {
  24. cols: [
  25. {title: '调差类型', colSpan: '1', rowSpan: '2', field: 't_type', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.t_type, cellTypeKey: 1},
  26. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  27. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  28. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  29. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 180, formatter: '@', readOnly: 'readOnly.isEdit'},
  30. {title: '工料分类', colSpan: '1', rowSpan: '2', field: 'm_type', hAlign: 0, width: 60, readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.m_type, cellTypeKey: 2},
  31. {title: '本期应耗数量', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  32. {title: '基准价', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  33. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  34. {title: '本期信息价|单价', colSpan: '3|1', rowSpan: '1|1', field: 'msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  35. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isEdit'},
  36. {title: '|价差', colSpan: '1', rowSpan: '1|1', field: 'msg_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.msg_spread'},
  37. {title: '本期材料调差|上涨幅度(%)', colSpan: '4|1', rowSpan: '1|1', field: 'm_up_risk', hAlign: 2, width: 100, type: 'Number', readOnly: 'readOnly.isEdit'},
  38. {title: '|下跌幅度(%)', colSpan: '|1', rowSpan: '|1', field: 'm_down_risk', hAlign: 2, width: 100, type: 'Number', readOnly: 'readOnly.isEdit'},
  39. {title: '|有效价差', colSpan: '|1', rowSpan: '|1', field: 'm_spread', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  40. {title: '|调整金额', colSpan: '|1', rowSpan: '1|1', field: 'm_tp', hAlign: 2, width: 80, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  41. {title: '截止上期调差金额', colSpan: '1', rowSpan: '2', field: 'pre_tp', hAlign: 2, width: 120, type: 'Number', readOnly: true},
  42. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.remark'},
  43. ],
  44. emptyRows: 0,
  45. headRows: 2,
  46. headRowHeight: [25, 25],
  47. defaultRowHeight: 21,
  48. headerFont: '12px 微软雅黑',
  49. font: '12px 微软雅黑',
  50. readOnly: readOnly,
  51. };
  52. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  53. spCol.readOnly = true;
  54. spCol.cellType = 'activeImageBtn';
  55. spCol.normalImg = '#ellipsis-icon';
  56. spCol.indent = 5;
  57. spCol.showImage = function (data) {
  58. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  59. return data.t_type === 2;
  60. };
  61. materialSpreadSetting.imageClick = function (data) {
  62. if (data.t_type === 2) {
  63. $('#bcyy').modal('show');
  64. console.log(data);
  65. $('#materialbillsId').val(data.id);
  66. $('#expr').val(data.expr);
  67. if (!readOnly && data.mid === materialID) {
  68. $('#expr').attr('readOnly', false);
  69. $('#expr_btn').show();
  70. $('#expr_select').show();
  71. } else {
  72. $('#expr').attr('readOnly', true);
  73. $('#expr_btn').hide();
  74. $('#expr_select').hide();
  75. }
  76. }
  77. };
  78. const materialBase = {
  79. isEdit: function (data) {
  80. if (data.t_type === 2) {
  81. return data.mid === materialID;
  82. } else {
  83. const mlInfo = materialListData.find(function (item) {
  84. return item.mb_id === data.id;
  85. });
  86. return data.mid === materialID && mlInfo === undefined;
  87. }
  88. },
  89. // isStage: function (data) {
  90. // return data.mid === materialID;
  91. // }
  92. }
  93. const materialCol = {
  94. getValue: {
  95. msg_spread: function (data) {
  96. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), 2);
  97. },
  98. m_spread : function (data) {
  99. const msg_spread = materialCol.getValue.msg_spread(data);
  100. 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));
  101. return Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? ZhCalc.round(ZhCalc.sub(msg_spread, cor), 2) : ZhCalc.round(ZhCalc.add(msg_spread, cor), 2)) : 0;
  102. },
  103. m_tp: function (data) {
  104. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), 2);
  105. }
  106. },
  107. readOnly: {
  108. isEdit: function (data) {
  109. return !(!readOnly && materialBase.isEdit(data));
  110. },
  111. remark: function () {
  112. return readOnly;
  113. },
  114. },
  115. };
  116. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  117. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  118. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  119. const materialSpreadObj = {
  120. refreshActn: function () {
  121. const setObjEnable = function (obj, enable) {
  122. if (enable) {
  123. obj.removeClass('disabled');
  124. } else {
  125. obj.addClass('disabled');
  126. }
  127. };
  128. const sheet = materialSpread.getActiveSheet();
  129. const select = SpreadJsObj.getSelectObject(sheet);
  130. // 还需判断是否已被调差清单调用
  131. setObjEnable($('#del'), !readOnly && select && materialBase.isEdit(select));
  132. },
  133. add: function () {
  134. const sheet = materialSpread.getActiveSheet();
  135. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  136. if (result) {
  137. materialBillsData.push(result);
  138. sheet.addRows(materialBillsData.length - 1, 1);
  139. SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  140. sheet.setSelection(materialBillsData.length - 1, 0, 1, 1);
  141. materialSpreadObj.refreshActn();
  142. }
  143. });
  144. },
  145. del: function () {
  146. const sheet = materialSpread.getActiveSheet();
  147. const select = SpreadJsObj.getSelectObject(sheet);
  148. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  149. const index = materialBillsData.indexOf(select);
  150. materialBillsData.splice(index, 1);
  151. sheet.deleteRows(index, 1);
  152. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  153. const sel = sheet.getSelections();
  154. sheet.setSelection(index > 0 ? index - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  155. materialSpreadObj.refreshActn();
  156. });
  157. },
  158. selectionChanged: function (e, info) {
  159. materialSpreadObj.refreshActn();
  160. const sel = info.sheet.getSelections()[0];
  161. const col = info.sheet.zh_setting.cols[sel.col];
  162. const data = SpreadJsObj.getSelectObject(info.sheet);
  163. materialSpreadObj.setReadOnly(true);
  164. },
  165. editEnded: function (e, info) {
  166. if (info.sheet.zh_setting) {
  167. const select = SpreadJsObj.getSelectObject(info.sheet);
  168. const col = info.sheet.zh_setting.cols[info.col];
  169. // 未改变值则不提交
  170. const validText = info.editingText ? (typeof(info.editingText) === 'String' ? info.editingText.replace('\n', '') : info.editingText) : null;
  171. const orgValue = select[col.field];
  172. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  173. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  174. return;
  175. }
  176. // 判断部分值是否输入的是数字判断和数据计算
  177. if (col.field === 'basic_price') {
  178. if (isNaN(validText)) {
  179. toastr.error('不能输入其它非数字类型字符');
  180. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  181. return;
  182. }
  183. const num = parseFloat(validText);
  184. if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  185. toastr.error('请输入大于0并且小于3位小数的浮点数');
  186. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  187. return;
  188. }
  189. }
  190. if (col.field === 'msg_tp') {
  191. if (isNaN(validText)) {
  192. toastr.error('不能输入其它非数字类型字符');
  193. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  194. return;
  195. }
  196. const num = parseFloat(validText);
  197. if (num < 0 || !/^\d+(\.\d{1,3})?$/.test(num)) {
  198. toastr.error('请输入大于0并且小于3位小数的浮点数');
  199. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  200. return;
  201. }
  202. }
  203. if (col.field === 'm_up_risk') {
  204. // 只能输入正整数
  205. if (isNaN(validText)) {
  206. toastr.error('不能输入其它非数字类型字符');
  207. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  208. return;
  209. }
  210. const num = parseFloat(validText);
  211. if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  212. toastr.error('只能输入0-100的正整数');
  213. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  214. return;
  215. }
  216. }
  217. if (col.field === 'm_down_risk') {
  218. // 只能输入正整数
  219. if (isNaN(validText)) {
  220. toastr.error('不能输入其它非数字类型字符');
  221. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  222. return;
  223. }
  224. const num = parseFloat(validText);
  225. if (num < 0 || num > 100 || !/^\d+$/.test(num)) {
  226. toastr.error('只能输入0-100的正整数');
  227. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  228. return;
  229. }
  230. }
  231. select[col.field] = validText;
  232. select.msg_spread = materialCol.getValue.msg_spread(select);
  233. select.m_spread = materialCol.getValue.m_spread(select);
  234. select.m_tp = materialCol.getValue.m_tp(select);
  235. // 更新至服务器
  236. postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  237. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  238. }, function () {
  239. select[col.field] = orgValue;
  240. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  241. });
  242. }
  243. },
  244. setReadOnly: function(readOnly) {
  245. SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  246. }
  247. };
  248. materialSpreadObj.refreshActn();
  249. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  250. if (!readOnly) {
  251. $('#add').click(materialSpreadObj.add);
  252. $('#del').click(materialSpreadObj.del);
  253. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  254. // 右键菜单
  255. $.contextMenu({
  256. selector: '#material-spread',
  257. build: function ($trigger, e) {
  258. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  259. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  260. },
  261. items: {
  262. 'create': {
  263. name: '新增材料',
  264. icon: 'fa-sign-in',
  265. callback: function (key, opt) {
  266. materialSpreadObj.add(materialSpread.getActiveSheet());
  267. },
  268. },
  269. 'delete': {
  270. name: '删除材料',
  271. icon: 'fa-remove',
  272. callback: function (key, opt) {
  273. materialSpreadObj.del(materialSpread.getActiveSheet());
  274. },
  275. disabled: function (key, opt) {
  276. const sheet = materialSpread.getActiveSheet();
  277. const select = SpreadJsObj.getSelectObject(sheet);
  278. materialSpreadObj.refreshActn();
  279. if (!readOnly && select && materialBase.isEdit(select)) {
  280. return false;
  281. } else {
  282. return true;
  283. }
  284. }
  285. },
  286. }
  287. });
  288. $('#changeRate').change(function () {
  289. const rate = parseInt($(this).val());
  290. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  291. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), 2);
  292. const jzbqhs = ZhCalc.round(ZhCalc.mul(ZhCalc.add(pre_tp, m_tp), 1+rate/100), 2);
  293. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  294. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  295. });
  296. });
  297. $('#expr_select button').on('click', function () {
  298. const code = $(this).text();
  299. $('#expr').val($('#expr').val() + code);
  300. });
  301. const ExprObj = {
  302. _checkExprValid(expr, invalidParam) {
  303. if (!expr) return [true, null];
  304. const param = [];
  305. let num = '', base = '';
  306. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  307. if (/^[\d\.%]+/.test(expr[i])) {
  308. if (base !== '') {
  309. param.push({type: 'base', value: base});
  310. base = '';
  311. }
  312. num = num + expr[i];
  313. } else if (/^[a-z]/.test(expr[i])) {
  314. if (num !== '') {
  315. param.push({type: 'num', value: num});
  316. base = '';
  317. }
  318. base = base + expr[i];
  319. } else if (expr[i] === '(') {
  320. if (num !== '') {
  321. param.push({type: 'num', value: num});
  322. base = '';
  323. }
  324. if (base !== '') {
  325. param.push({type: 'base', value: base});
  326. base = '';
  327. }
  328. param.push({type: 'left', value: '('});
  329. } else if (expr[i] === ')') {
  330. if (num !== '') {
  331. param.push({type: 'num', value: num});
  332. base = '';
  333. }
  334. if (base !== '') {
  335. param.push({type: 'base', value: base});
  336. base = '';
  337. }
  338. param.push({type: 'right', value: ')'});
  339. } else if (/^[\+\-*\/]/.test(expr[i])) {
  340. if (num !== '') {
  341. param.push({type: 'num', value: num});
  342. base = '';
  343. }
  344. if (base !== '') {
  345. param.push({type: 'base', value: base});
  346. base = '';
  347. }
  348. param.push({type: 'calc', value: expr[i]});
  349. } else {
  350. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  351. }
  352. }
  353. if (num !== '') {
  354. param.push({type: 'num', value: num});
  355. base = '';
  356. }
  357. if (base !== '') {
  358. param.push({type: 'base', value: base});
  359. base = '';
  360. }
  361. if (param.length === 0) return true;
  362. if (param.length > 1) {
  363. if (param[0].value === '-') {
  364. param[1].value = '-' + param[1];
  365. }
  366. param.unshift();
  367. }
  368. const iLen = param.length;
  369. let iLeftCount = 0, iRightCount = 0;
  370. for (const [i, p] of param.entries()) {
  371. if (p.type === 'calc') {
  372. if (i === 0 || i === iLen - 1)
  373. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  374. }
  375. if (p.type === 'num') {
  376. num = p.value.replace('%', '');
  377. if (p.value.length - num.length > 1)
  378. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  379. num = _.toNumber(num);
  380. if (num === undefined || num === null || _.isNaN(num))
  381. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  382. if (i > 0) {
  383. if (param[i - 1].type !== 'calc') {
  384. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  385. } else if (param[i - 1].value === '/' && num === 0) {
  386. return [false, '输入的表达式非法:请勿除0'];
  387. }
  388. }
  389. }
  390. if (p.type === 'base') {
  391. const baseParam = _.find(calcBase, {code: p.value});
  392. if (!baseParam)
  393. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  394. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  395. return [false, '不可使用计算基数' + p.value];
  396. if (i > 0 && param[i - 1].type === 'calc')
  397. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  398. }
  399. if (p.type === 'left') {
  400. iLeftCount += 1;
  401. if (i !== 0 && param[i-1].type !== 'calc')
  402. return [false, '输入的表达式非法:(前应有运算符'];
  403. }
  404. if (p.type === 'right') {
  405. iRightCount += 1;
  406. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  407. return [false, '输入的表达式非法:)后应有运算符'];
  408. if (iRightCount > iLeftCount)
  409. return [false, '输入的表达式非法:")"前无对应的"("'];
  410. }
  411. }
  412. if (iLeftCount > iRightCount)
  413. return [false, '输入的表达式非法:"("后无对应的")"'];
  414. return [true, ''];
  415. },
  416. _checkExpr: function (text) {
  417. if (text) {
  418. const num = _.toNumber(text);
  419. if (num) {
  420. console.log(num);
  421. } else {
  422. const expr = text.replace('=', '').toLowerCase();
  423. const [valid, msg] = this._checkExprValid(expr);
  424. if (!valid) return [valid, msg];
  425. }
  426. }
  427. return [true, ''];
  428. },
  429. };
  430. $('#expr_btn').click(function () {
  431. const expr = $('#expr').val();
  432. // 判断表达式格式
  433. const [valid, msg] = ExprObj._checkExpr(expr);
  434. console.log(msg);
  435. if (!valid) {
  436. toastr.error(msg);
  437. }
  438. })
  439. } else {
  440. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  441. }
  442. $.subMenu({
  443. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  444. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  445. //key: 'stage.compare.memu.1.0.0',
  446. callback: function (info) {
  447. if (info.mini) {
  448. $('.panel-title').addClass('fluid');
  449. $('#sub-menu').removeClass('panel-sidebar');
  450. } else {
  451. $('.panel-title').removeClass('fluid');
  452. $('#sub-menu').addClass('panel-sidebar');
  453. }
  454. autoFlashHeight();
  455. materialSpread.refresh();
  456. }
  457. });
  458. });