se_safe_prod.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. editEnded: function (e, info) {
  219. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  220. const node = info.sheet.zh_data[info.row];
  221. const col = info.sheet.zh_setting.cols[info.col];
  222. const data = {};
  223. if (node) {
  224. data.update = {};
  225. data.update.id = node.id;
  226. const oldValue = node ? node[col.field] : null;
  227. const newValue = trimInvalidChar(info.editingText);
  228. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  229. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  230. return;
  231. }
  232. data.update[col.field] = newValue;
  233. } else {
  234. if (col.field !== 'name') {
  235. toastr.warning('请先输入名称');
  236. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  237. return;
  238. }
  239. data.add = {};
  240. data.add.order = info.row + 1;
  241. data.add.name = trimInvalidChar(info.editingText);
  242. }
  243. postData(window.location.pathname + '/update', data, function (result) {
  244. seSafeObj.loadUpdateData(result);
  245. SpreadJsObj.reLoadSheetData(info.sheet);
  246. refreshSum();
  247. }, function () {
  248. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  249. });
  250. },
  251. editStarting(e, info) {
  252. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  253. info.cancel = true;
  254. return;
  255. }
  256. const col = info.sheet.zh_setting.cols[info.col];
  257. const node = info.sheet.zh_data[info.row];
  258. if (!node) return;
  259. switch (col.field) {
  260. case 'unit':
  261. case 'unit_price':
  262. case 'quantity':
  263. info.cancel = readOnly || node.pre_used;
  264. break;
  265. }
  266. },
  267. clipboardPasting(e, info) {
  268. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  269. info.cancel = true;
  270. if (!setting || !sortData) return;
  271. const pasteData = info.pasteData.html
  272. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  273. : (info.pasteData.text === ''
  274. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  275. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  276. const hint = {
  277. name: {type: 'warning', msg: '名称不可为空,已过滤'},
  278. };
  279. const uDatas = [], iDatas = [];
  280. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  281. const curRow = info.cellRange.row + iRow;
  282. const node = sortData[curRow];
  283. let bPaste = false;
  284. const data = {};
  285. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  286. const curCol = info.cellRange.col + iCol;
  287. const colSetting = setting.cols[curCol];
  288. const value = trimInvalidChar(pasteData[iRow][iCol]);
  289. if (colSetting.field === 'name' && (!value || value === '')) {
  290. toastMessageUniq(hint.name);
  291. break;
  292. }
  293. if (colSetting.type === 'Number') {
  294. const num = _.toNumber(value);
  295. if (num) {
  296. data[colSetting.field] = num;
  297. bPaste = true;
  298. }
  299. } else {
  300. data[colSetting.field] = value;
  301. bPaste = true;
  302. }
  303. }
  304. if (bPaste) {
  305. if (node) {
  306. data.id = node.id;
  307. uDatas.push(data);
  308. } else {
  309. data.order = curRow + 1;
  310. iDatas.push(data);
  311. }
  312. }
  313. }
  314. const updateData = {};
  315. if (uDatas.length > 0) updateData.update = uDatas;
  316. if (iDatas.length > 0) updateData.add = iDatas;
  317. if (uDatas.length > 0 || iDatas.length > 0) {
  318. postData(window.location.pathname + '/update', updateData, function (result) {
  319. seSafeObj.loadUpdateData(result);
  320. SpreadJsObj.reLoadSheetData(info.sheet);
  321. refreshSum();
  322. });
  323. } else {
  324. SpreadJsObj.reLoadSheetData(info.sheet);
  325. }
  326. },
  327. upMove: function () {
  328. const sels = safeSheet.getSelections(), sortData = safeSheet.zh_data;
  329. const node = sortData[sels[0].row];
  330. const preNode = sortData[sels[0].row - 1];
  331. const data = [
  332. {id: node.id, order: preNode.order},
  333. {id: preNode.id, order: node.order}
  334. ];
  335. postData(window.location.pathname + '/update', {update: data}, function (result) {
  336. seSafeObj.loadUpdateData(result);
  337. SpreadJsObj.reLoadRowsData(safeSheet, [sels[0].row, sels[0].row - 1]);
  338. safeSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  339. });
  340. },
  341. downMove: function () {
  342. const sels = safeSheet.getSelections(), sortData = safeSheet.zh_data;
  343. const node = sortData[sels[0].row];
  344. const nextNode = sortData[sels[0].row + 1];
  345. const data = [
  346. {id: node.id, order: nextNode.order},
  347. {id: nextNode.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. };
  356. safeSheet.bind(spreadNS.Events.EditEnded, seSafeOprObj.editEnded);
  357. safeSheet.bind(spreadNS.Events.EditStarting, seSafeOprObj.editStarting);
  358. safeSheet.bind(spreadNS.Events.ClipboardPasting, seSafeOprObj.clipboardPasting);
  359. SpreadJsObj.addDeleteBind(safeSpread, seSafeOprObj.deletePress);
  360. $.contextMenu({
  361. selector: '#safe-prod-spread',
  362. build: function ($trigger, e) {
  363. const target = SpreadJsObj.safeRightClickSelection($trigger, e, safeSpread);
  364. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  365. },
  366. items: {
  367. del: {
  368. name: '删除',
  369. icon: 'fa-remove',
  370. callback: function (key, opt) {
  371. seSafeOprObj.delete(safeSheet);
  372. },
  373. disabled: function (key, opt) {
  374. const sels = safeSheet.getSelections();
  375. if (!sels || !sels[0]) return true;
  376. const row = sels[0].row;
  377. const node = seSafeObj.data[row];
  378. return node === undefined || node === null;
  379. },
  380. visible: function (key, opt) {
  381. return !readOnly;
  382. }
  383. },
  384. sprDel: '------------',
  385. upMove: {
  386. name: '上移',
  387. icon: 'fa-arrow-up',
  388. callback: function (key, opt) {
  389. seSafeOprObj.upMove();
  390. },
  391. disabled: function (key, opt) {
  392. const sels = safeSheet.getSelections();
  393. if (!sels || !sels[0] || sels[0].row === 0) return true;
  394. const row = sels[0].row;
  395. const node = seSafeObj.data[row];
  396. return node === undefined || node === null;
  397. },
  398. visible: function (key, opt) {
  399. return !readOnly;
  400. }
  401. },
  402. downMove: {
  403. name: '下移',
  404. icon: 'fa-arrow-down',
  405. callback: function (key, opt) {
  406. seSafeOprObj.downMove();
  407. },
  408. disabled: function (key, opt) {
  409. const sels = safeSheet.getSelections();
  410. if (!sels || !sels[0] || sels[0].row >= seSafeObj.data.length - 1) return true;
  411. const row = sels[0].row;
  412. const node = seSafeObj.data[row];
  413. return node === undefined || node === null;
  414. },
  415. visible: function (key, opt) {
  416. return !readOnly;
  417. }
  418. }
  419. },
  420. })
  421. }
  422. $('#exportExcel').click(function () {
  423. SpreadExcelObj.exportSimpleXlsxSheet(safeSpreadSetting, seSafeObj.data, $('.sidebar-title').attr('data-original-title') + "-安全生产.xlsx");
  424. });
  425. });