se_safe_prod.js 20 KB

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