se_jgcl.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. insertJgcl: function(sheet) {
  240. if (!sheet.zh_setting || !sheet.zh_data) return;
  241. const node = SpreadJsObj.getSelectObject(sheet);
  242. postData(window.location.pathname + '/update', {insert: { select: node.id, count: 1 }}, function (result) {
  243. jgclObj.loadUpdateData(result);
  244. SpreadJsObj.reLoadSheetData(jgclSheet);
  245. refreshSum();
  246. }, function () {
  247. SpreadJsObj.reLoadSheetData(jgclSheet);
  248. });
  249. },
  250. editEnded: function (e, info) {
  251. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  252. const node = info.sheet.zh_data[info.row];
  253. const col = info.sheet.zh_setting.cols[info.col];
  254. const data = {};
  255. if (node) {
  256. data.update = {};
  257. data.update.id = node.id;
  258. const oldValue = node ? node[col.field] : null;
  259. const newValue = trimInvalidChar(info.editingText);
  260. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  261. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  262. return;
  263. }
  264. data.update[col.field] = newValue;
  265. } else {
  266. if (col.field !== 'name') {
  267. toastr.warning('新增甲供材料,请先输入名称');
  268. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  269. return;
  270. }
  271. data.add = {};
  272. data.add.order = info.row + 1;
  273. data.add.name = trimInvalidChar(info.editingText);
  274. }
  275. postData(window.location.pathname + '/update', data, function (result) {
  276. jgclObj.loadUpdateData(result);
  277. SpreadJsObj.reLoadSheetData(info.sheet);
  278. refreshSum();
  279. }, function () {
  280. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  281. });
  282. },
  283. editStarting(e, info) {
  284. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  285. info.cancel = true;
  286. return;
  287. }
  288. const col = info.sheet.zh_setting.cols[info.col];
  289. const node = info.sheet.zh_data[info.row];
  290. if (!node) return;
  291. switch (col.field) {
  292. case 'name':
  293. case 'unit':
  294. case 'unit_price':
  295. info.cancel = readOnly;
  296. break;
  297. }
  298. },
  299. clipboardPasting(e, info) {
  300. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  301. info.cancel = true;
  302. if (!setting || !sortData) return;
  303. const pasteData = info.pasteData.html
  304. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  305. : (info.pasteData.text === ''
  306. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  307. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  308. const hint = {
  309. name: {type: 'warning', msg: '甲供材料名称不可为空,已过滤'},
  310. unit_price: {type: 'warning', msg: '输入的 单价 非法,已过滤'},
  311. arrive_qty: {type: 'warning', msg: '输入的 本期到场-数量 非法,已过滤'},
  312. reduce_qty: {type: 'warning', msg: '输入的 本期扣回-数量 非法,已过滤'},
  313. };
  314. const uDatas = [], iDatas = [];
  315. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  316. const curRow = info.cellRange.row + iRow;
  317. const node = sortData[curRow];
  318. let bPaste = false;
  319. const data = {};
  320. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  321. const curCol = info.cellRange.col + iCol;
  322. const colSetting = setting.cols[curCol];
  323. const value = trimInvalidChar(pasteData[iRow][iCol]);
  324. if (colSetting.field === 'name' && (!value || value === '')) {
  325. toastMessageUniq(hint.name);
  326. break;
  327. }
  328. if (colSetting.type === 'Number') {
  329. const num = _.toNumber(value);
  330. if (num) {
  331. data[colSetting.field] = num;
  332. bPaste = true;
  333. }
  334. } else {
  335. data[colSetting.field] = value;
  336. bPaste = true;
  337. }
  338. }
  339. if (bPaste) {
  340. if (node) {
  341. data.id = node.id;
  342. uDatas.push(data);
  343. } else {
  344. data.order = curRow + 1;
  345. iDatas.push(data);
  346. }
  347. }
  348. }
  349. const updateData = {};
  350. if (uDatas.length > 0) updateData.update = uDatas;
  351. if (iDatas.length > 0) updateData.add = iDatas;
  352. if (uDatas.length > 0 || iDatas.length > 0) {
  353. postData(window.location.pathname + '/update', updateData, function (result) {
  354. jgclObj.loadUpdateData(result);
  355. SpreadJsObj.reLoadSheetData(info.sheet);
  356. refreshSum();
  357. });
  358. } else {
  359. SpreadJsObj.reLoadSheetData(info.sheet);
  360. }
  361. },
  362. upMove: function () {
  363. const sels = jgclSheet.getSelections(), sortData = jgclSheet.zh_data;
  364. const node = sortData[sels[0].row];
  365. const preNode = sortData[sels[0].row - 1];
  366. const data = [
  367. {id: node.id, order: preNode.order},
  368. {id: preNode.id, order: node.order}
  369. ];
  370. postData(window.location.pathname + '/update', {update: data}, function (result) {
  371. jgclObj.loadUpdateData(result);
  372. SpreadJsObj.reLoadRowsData(jgclSheet, [sels[0].row, sels[0].row - 1]);
  373. jgclSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  374. });
  375. },
  376. downMove: function () {
  377. const sels = jgclSheet.getSelections(), sortData = jgclSheet.zh_data;
  378. const node = sortData[sels[0].row];
  379. const nextNode = sortData[sels[0].row + 1];
  380. const data = [
  381. {id: node.id, order: nextNode.order},
  382. {id: nextNode.id, order: node.order}
  383. ];
  384. postData(window.location.pathname + '/update', {update: data}, function (result) {
  385. jgclObj.loadUpdateData(result);
  386. SpreadJsObj.reLoadRowsData(jgclSheet, [sels[0].row, sels[0].row + 1]);
  387. jgclSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  388. });
  389. }
  390. };
  391. jgclSheet.bind(spreadNS.Events.EditEnded, jgclOprObj.editEnded);
  392. jgclSheet.bind(spreadNS.Events.EditStarting, jgclOprObj.editStarting);
  393. jgclSheet.bind(spreadNS.Events.ClipboardPasting, jgclOprObj.clipboardPasting);
  394. SpreadJsObj.addDeleteBind(jgclSpread, jgclOprObj.deletePress);
  395. $.contextMenu({
  396. selector: '#jgcl-spread',
  397. build: function ($trigger, e) {
  398. const target = SpreadJsObj.safeRightClickSelection($trigger, e, jgclSpread);
  399. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  400. },
  401. items: {
  402. del: {
  403. name: '删除',
  404. icon: 'fa-remove',
  405. callback: function (key, opt) {
  406. jgclOprObj.deleteJgcl(jgclSheet);
  407. },
  408. disabled: function (key, opt) {
  409. const sels = jgclSheet.getSelections();
  410. if (!sels || !sels[0]) return true;
  411. const row = sels[0].row;
  412. const node = jgclObj.data[row];
  413. return node === undefined || node === null;
  414. },
  415. visible: function (key, opt) {
  416. return !readOnly;
  417. }
  418. },
  419. sprDel: '------------',
  420. insert: {
  421. name: '插入',
  422. icon: 'fa-plus',
  423. callback: function (key, opt) {
  424. jgclOprObj.insertJgcl(jgclSheet);
  425. },
  426. disabled: function (key, opt) {
  427. const node = SpreadJsObj.getSelectObject(jgclSheet);
  428. return !node;
  429. },
  430. visible: function (key, opt) {
  431. return !readOnly;
  432. }
  433. },
  434. upMove: {
  435. name: '上移',
  436. icon: 'fa-arrow-up',
  437. callback: function (key, opt) {
  438. jgclOprObj.upMove();
  439. },
  440. disabled: function (key, opt) {
  441. const sels = jgclSheet.getSelections();
  442. if (!sels || !sels[0] || sels[0].row === 0) return true;
  443. const row = sels[0].row;
  444. const node = jgclObj.data[row];
  445. return node === undefined || node === null;
  446. },
  447. visible: function (key, opt) {
  448. return !readOnly;
  449. }
  450. },
  451. downMove: {
  452. name: '下移',
  453. icon: 'fa-arrow-down',
  454. callback: function (key, opt) {
  455. jgclOprObj.downMove();
  456. },
  457. disabled: function (key, opt) {
  458. const sels = jgclSheet.getSelections();
  459. if (!sels || !sels[0] || sels[0].row >= jgclObj.data.length - 1) return true;
  460. const row = sels[0].row;
  461. const node = jgclObj.data[row];
  462. return node === undefined || node === null;
  463. },
  464. visible: function (key, opt) {
  465. return !readOnly;
  466. }
  467. }
  468. },
  469. })
  470. }
  471. $('#exportExcel').click(function () {
  472. SpreadExcelObj.exportSimpleXlsxSheet(spreadSetting, jgclObj.data, $('.sidebar-title').attr('data-original-title') + "-甲供材料.xlsx");
  473. });
  474. });