se_jgcl.js 18 KB

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