se_jgcl.js 21 KB

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