se_jgcl.js 21 KB

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