se_jgcl.js 22 KB

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