se_safe_prod.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. 'use strict';
  2. /**
  3. * 安全生产
  4. *
  5. * @author Mai
  6. * @date 2021/10/21
  7. * @version
  8. */
  9. $(document).ready(() => {
  10. autoFlashHeight();
  11. const safeSpreadSetting = {
  12. cols: [
  13. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 350, formatter: '@'},
  14. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', cellType: 'unit'},
  15. {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 60, type: 'Number'},
  16. {title: '计划|数量', colSpan: '2|1', rowSpan: '1|1', field: 'quantity', hAlign: 2, width: 60, type: 'Number'},
  17. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'total_price', hAlign: 2, width: 60, type: 'Number', readOnly: true},
  18. {title: '本期|数量', colSpan: '2|1', rowSpan: '1|1', field: 'qty', hAlign: 2, width: 60, type: 'Number'},
  19. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'tp', hAlign: 2, width: 60, type: 'Number', readOnly: true},
  20. {title: '截止本期|数量', colSpan: '2|1', rowSpan: '1|1', field: 'end_qty', hAlign: 2, width: 60, type: 'Number', readOnly: true},
  21. {title: '|金额', colSpan: '|1', rowSpan: '|1', field: 'end_tp', hAlign: 2, width: 60, type: 'Number', readOnly: true},
  22. {title: '备注', colSpan: '1', rowSpan: '2', field: 'memo', hAlign: 0, width: 180, formatter: '@', cellType: 'ellipsisAutoTip'}
  23. ],
  24. emptyRows: readOnly ? 0 : 3,
  25. headRows: 2,
  26. headRowHeight: [25, 25],
  27. defaultRowHeight: 21,
  28. headerFont: '12px 微软雅黑',
  29. font: '12px 微软雅黑',
  30. readOnly: readOnly,
  31. localCache: {
  32. key: 'stage-extra-temp',
  33. colWidth: true,
  34. },
  35. getColor: function (sheet, data, row, col, defaultColor) {
  36. if (!data || !data.quantity) return defaultColor;
  37. return data.quantity >= 0
  38. ? data.end_qty > data.quantity ? '#f8d7da' : defaultColor
  39. : data.end_qty < data.quantity ? '#f8d7da' : defaultColor;
  40. }
  41. };
  42. const safeSpread = SpreadJsObj.createNewSpread($('#safe-prod-spread')[0]);
  43. const safeSheet = safeSpread.getActiveSheet();
  44. if (thousandth) sjsSettingObj.setTpThousandthFormat(safeSpreadSetting);
  45. SpreadJsObj.initSheet(safeSheet, safeSpreadSetting);
  46. $.subMenu({
  47. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  48. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  49. key: 'menu.1.0.0',
  50. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  51. callback: function (info) {
  52. if (info.mini) {
  53. $('.panel-title').addClass('fluid');
  54. $('#sub-menu').removeClass('panel-sidebar');
  55. } else {
  56. $('.panel-title').removeClass('fluid');
  57. $('#sub-menu').addClass('panel-sidebar');
  58. }
  59. autoFlashHeight();
  60. safeSpread.refresh();
  61. }
  62. });
  63. class SeSafe {
  64. constructor () {
  65. this.data = [];
  66. }
  67. resortData() {
  68. this.data.sort(function (a, b) {
  69. return a.order - b.order;
  70. });
  71. }
  72. calculateAll() {
  73. for (const d of this.data) {
  74. d.end_qty = ZhCalc.add(d.pre_qty, d.qty);
  75. d.end_tp = ZhCalc.add(d.pre_tp, d.tp);
  76. }
  77. }
  78. getMaxOrder() {
  79. return this.data.length > 0 ? this.data[this.data.length - 1].order : 0;
  80. }
  81. loadDatas(datas) {
  82. this.data = datas;
  83. this.calculateAll();
  84. this.resortData();
  85. }
  86. loadUpdateData(updateData) {
  87. if (updateData.add) {
  88. for (const a of updateData.add) {
  89. this.data.push(a);
  90. }
  91. }
  92. if (updateData.update) {
  93. for (const u of updateData.update) {
  94. const d = this.data.find(function (x) {
  95. return u.id === x.id;
  96. });
  97. if (d) {
  98. _.assign(d, u);
  99. } else {
  100. this.data.push(d);
  101. }
  102. }
  103. }
  104. if (updateData.del) {
  105. _.remove(this.data, function (d) {
  106. return updateData.del.indexOf(d.id) >= 0;
  107. });
  108. }
  109. this.calculateAll();
  110. this.resortData();
  111. }
  112. sum () {
  113. const result = {
  114. total_price: 0,
  115. tp: 0,
  116. end_tp: 0,
  117. };
  118. for (const d of this.data) {
  119. result.total_price = ZhCalc.add(result.total_price, d.total_price);
  120. result.tp = ZhCalc.add(result.tp, d.tp);
  121. result.end_tp = ZhCalc.add(result.end_tp, d.end_tp);
  122. }
  123. return result;
  124. }
  125. }
  126. const seSafeObj = new SeSafe();
  127. const refreshSum = function () {
  128. const sum = seSafeObj.sum();
  129. const html = [];
  130. const getTrHtml = function (name, value) {
  131. return '<tr><td>' + name + '</td><td class="text-right">' + (!checkZero(value) ? value : '') + ' </td></tr>';
  132. };
  133. html.push(getTrHtml('金额', sum.total_price));
  134. html.push(getTrHtml('本期金额', sum.tp));
  135. html.push(getTrHtml('截止本期金额', sum.end_tp));
  136. $('#sum').html(html.join(' '));
  137. };
  138. postData(window.location.pathname + '/load', null, function (result) {
  139. seSafeObj.loadDatas(result);
  140. SpreadJsObj.loadSheetData(safeSheet, SpreadJsObj.DataType.Data, seSafeObj.data);
  141. refreshSum();
  142. });
  143. if (!readOnly) {
  144. const seSafeOprObj = {
  145. /**
  146. * 删除按钮响应事件
  147. * @param sheet
  148. */
  149. deletePress: function (sheet) {
  150. if (!sheet.zh_setting || readOnly) return;
  151. const sortData = sheet.zh_data;
  152. const datas = [];
  153. const sels = sheet.getSelections();
  154. if (!sels || !sels[0]) return;
  155. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  156. let bDel = false;
  157. const node = sortData[iRow];
  158. if (node) {
  159. const data = {id: node.id};
  160. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  161. const colSetting = sheet.zh_setting.cols[iCol];
  162. if (colSetting.field === 'name') {
  163. toastr.error('名称不能为空,如需删除请使用右键删除');
  164. return;
  165. }
  166. const style = sheet.getStyle(iRow, iCol);
  167. if (!style.locked) {
  168. const colSetting = sheet.zh_setting.cols[iCol];
  169. data[colSetting.field] = null;
  170. bDel = true;
  171. }
  172. }
  173. if (bDel) {
  174. datas.push(data);
  175. }
  176. }
  177. }
  178. if (datas.length > 0) {
  179. postData(window.location.pathname + '/update', {update: datas}, function (result) {
  180. seSafeObj.loadUpdateData(result);
  181. SpreadJsObj.reLoadSheetData(safeSheet);
  182. refreshSum();
  183. }, function () {
  184. SpreadJsObj.reLoadSheetData(safeSheet);
  185. });
  186. }
  187. },
  188. delete: function (sheet) {
  189. if (!sheet.zh_setting || readOnly) return;
  190. const sortData = sheet.zh_data;
  191. const datas = [];
  192. const sels = sheet.getSelections();
  193. if (!sels || !sels[0]) return;
  194. const hint = {
  195. isOld: {type: 'warning', msg: '该数据已计量,不可删除'},
  196. invalidDel: {type: 'warning', msg: '该数据为往期新增,只有原报可删除'},
  197. };
  198. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  199. const node = sortData[iRow];
  200. if (node.pre_used || !checkZero(node.end_tp)) {
  201. toastMessageUniq(hint.isOld);
  202. continue;
  203. } else {
  204. if (node.add_sid !== stageId && stageUserId !== userID) {
  205. toastMessageUniq(hint.invalidDel);
  206. continue;
  207. }
  208. datas.push(node.id);
  209. }
  210. }
  211. if (datas.length > 0) {
  212. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  213. seSafeObj.loadUpdateData(result);
  214. SpreadJsObj.reLoadSheetData(safeSheet);
  215. refreshSum();
  216. }, function () {
  217. SpreadJsObj.reLoadSheetData(safeSheet);
  218. });
  219. }
  220. },
  221. insert: function(sheet) {
  222. if (!sheet.zh_setting || !sheet.zh_data) return;
  223. const node = SpreadJsObj.getSelectObject(sheet);
  224. postData(window.location.pathname + '/update', {insert: { select: node.id, count: 1 }}, function (result) {
  225. seSafeObj.loadUpdateData(result);
  226. SpreadJsObj.reLoadSheetData(sheet);
  227. refreshSum();
  228. }, function () {
  229. SpreadJsObj.reLoadSheetData(sheet);
  230. });
  231. },
  232. editEnded: function (e, info) {
  233. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  234. const node = info.sheet.zh_data[info.row];
  235. const col = info.sheet.zh_setting.cols[info.col];
  236. const data = {};
  237. if (node) {
  238. data.update = {};
  239. data.update.id = node.id;
  240. const oldValue = node ? node[col.field] : null;
  241. const newValue = trimInvalidChar(info.editingText);
  242. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  243. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  244. return;
  245. }
  246. data.update[col.field] = newValue;
  247. } else {
  248. if (col.field !== 'name') {
  249. toastr.warning('请先输入名称');
  250. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  251. return;
  252. }
  253. data.add = {};
  254. data.add.order = seSafeObj.getMaxOrder() + 1;
  255. data.add.name = trimInvalidChar(info.editingText);
  256. }
  257. postData(window.location.pathname + '/update', data, function (result) {
  258. seSafeObj.loadUpdateData(result);
  259. SpreadJsObj.reLoadSheetData(info.sheet);
  260. refreshSum();
  261. }, function () {
  262. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  263. });
  264. },
  265. editStarting(e, info) {
  266. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  267. info.cancel = true;
  268. return;
  269. }
  270. const col = info.sheet.zh_setting.cols[info.col];
  271. const node = info.sheet.zh_data[info.row];
  272. if (!node) return;
  273. switch (col.field) {
  274. case 'unit':
  275. case 'unit_price':
  276. case 'quantity':
  277. info.cancel = readOnly || node.pre_used;
  278. break;
  279. }
  280. },
  281. clipboardPasting(e, info) {
  282. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  283. info.cancel = true;
  284. if (!setting || !sortData) return;
  285. const pasteData = info.pasteData.html
  286. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  287. : (info.pasteData.text === ''
  288. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  289. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  290. const hint = {
  291. name: {type: 'warning', msg: '名称不可为空,已过滤'},
  292. };
  293. const uDatas = [], iDatas = [], maxOrder = seSafeObj.getMaxOrder();
  294. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  295. const curRow = info.cellRange.row + iRow;
  296. const node = sortData[curRow];
  297. let bPaste = false;
  298. const data = {};
  299. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  300. const curCol = info.cellRange.col + iCol;
  301. const colSetting = setting.cols[curCol];
  302. const value = trimInvalidChar(pasteData[iRow][iCol]);
  303. if (colSetting.field === 'name' && (!value || value === '')) {
  304. toastMessageUniq(hint.name);
  305. break;
  306. }
  307. if (colSetting.type === 'Number') {
  308. const num = _.toNumber(value);
  309. if (num) {
  310. data[colSetting.field] = num;
  311. bPaste = true;
  312. }
  313. } else {
  314. data[colSetting.field] = value;
  315. bPaste = true;
  316. }
  317. }
  318. if (bPaste) {
  319. if (node) {
  320. data.id = node.id;
  321. uDatas.push(data);
  322. } else {
  323. data.order = maxOrder + iRow + 1;
  324. iDatas.push(data);
  325. }
  326. }
  327. }
  328. const updateData = {};
  329. if (uDatas.length > 0) updateData.update = uDatas;
  330. if (iDatas.length > 0) updateData.add = iDatas;
  331. if (uDatas.length > 0 || iDatas.length > 0) {
  332. postData(window.location.pathname + '/update', updateData, function (result) {
  333. seSafeObj.loadUpdateData(result);
  334. SpreadJsObj.reLoadSheetData(info.sheet);
  335. refreshSum();
  336. });
  337. } else {
  338. SpreadJsObj.reLoadSheetData(info.sheet);
  339. }
  340. },
  341. upMove: function () {
  342. const sels = safeSheet.getSelections(), sortData = safeSheet.zh_data;
  343. const node = sortData[sels[0].row];
  344. const preNode = sortData[sels[0].row - 1];
  345. const data = [
  346. {id: node.id, order: preNode.order},
  347. {id: preNode.id, order: node.order}
  348. ];
  349. postData(window.location.pathname + '/update', {update: data}, function (result) {
  350. seSafeObj.loadUpdateData(result);
  351. SpreadJsObj.reLoadRowsData(safeSheet, [sels[0].row, sels[0].row - 1]);
  352. safeSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  353. });
  354. },
  355. downMove: function () {
  356. const sels = safeSheet.getSelections(), sortData = safeSheet.zh_data;
  357. const node = sortData[sels[0].row];
  358. const nextNode = sortData[sels[0].row + 1];
  359. const data = [
  360. {id: node.id, order: nextNode.order},
  361. {id: nextNode.id, order: node.order}
  362. ];
  363. postData(window.location.pathname + '/update', {update: data}, function (result) {
  364. seSafeObj.loadUpdateData(result);
  365. SpreadJsObj.reLoadRowsData(safeSheet, [sels[0].row, sels[0].row + 1]);
  366. safeSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  367. });
  368. }
  369. };
  370. safeSheet.bind(spreadNS.Events.EditEnded, seSafeOprObj.editEnded);
  371. safeSheet.bind(spreadNS.Events.EditStarting, seSafeOprObj.editStarting);
  372. safeSheet.bind(spreadNS.Events.ClipboardPasting, seSafeOprObj.clipboardPasting);
  373. SpreadJsObj.addDeleteBind(safeSpread, seSafeOprObj.deletePress);
  374. $.contextMenu({
  375. selector: '#safe-prod-spread',
  376. build: function ($trigger, e) {
  377. const target = SpreadJsObj.safeRightClickSelection($trigger, e, safeSpread);
  378. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  379. },
  380. items: {
  381. del: {
  382. name: '删除',
  383. icon: 'fa-remove',
  384. callback: function (key, opt) {
  385. seSafeOprObj.delete(safeSheet);
  386. },
  387. disabled: function (key, opt) {
  388. const node = SpreadJsObj.getSelectObject(safeSheet);
  389. return !node;
  390. },
  391. visible: function (key, opt) {
  392. return !readOnly;
  393. }
  394. },
  395. sprDel: '------------',
  396. insert: {
  397. name: '插入',
  398. icon: 'fa-plus',
  399. callback: function (key, opt) {
  400. seSafeOprObj.insert(safeSheet);
  401. },
  402. disabled: function (key, opt) {
  403. const node = SpreadJsObj.getSelectObject(safeSheet);
  404. return !node;
  405. },
  406. visible: function (key, opt) {
  407. return !readOnly;
  408. }
  409. },
  410. upMove: {
  411. name: '上移',
  412. icon: 'fa-arrow-up',
  413. callback: function (key, opt) {
  414. seSafeOprObj.upMove();
  415. },
  416. disabled: function (key, opt) {
  417. const sels = safeSheet.getSelections();
  418. if (!sels || !sels[0] || sels[0].row === 0) return true;
  419. const row = sels[0].row;
  420. const node = seSafeObj.data[row];
  421. return node === undefined || node === null;
  422. },
  423. visible: function (key, opt) {
  424. return !readOnly;
  425. }
  426. },
  427. downMove: {
  428. name: '下移',
  429. icon: 'fa-arrow-down',
  430. callback: function (key, opt) {
  431. seSafeOprObj.downMove();
  432. },
  433. disabled: function (key, opt) {
  434. const sels = safeSheet.getSelections();
  435. if (!sels || !sels[0] || sels[0].row >= seSafeObj.data.length - 1) return true;
  436. const row = sels[0].row;
  437. const node = seSafeObj.data[row];
  438. return node === undefined || node === null;
  439. },
  440. visible: function (key, opt) {
  441. return !readOnly;
  442. }
  443. }
  444. },
  445. })
  446. }
  447. $('#exportExcel').click(function () {
  448. SpreadExcelObj.exportSimpleXlsxSheet(safeSpreadSetting, seSafeObj.data, $('.sidebar-title').attr('data-original-title') + "-安全生产.xlsx");
  449. });
  450. });