se_jgcl.js 18 KB

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