se_jgcl.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 (setting) {
  58. this.data = [];
  59. }
  60. resortData() {
  61. this.data.sort(function (a, b) {
  62. return a.order - b.order;
  63. });
  64. }
  65. loadDatas(datas) {
  66. this.data = datas;
  67. this.resortData();
  68. }
  69. loadUpdateData(updateData) {
  70. if (updateData.add) {
  71. for (const a of updateData.add) {
  72. this.data.push(a);
  73. }
  74. }
  75. if (updateData.update) {
  76. for (const u of updateData.update) {
  77. const d = this.data.find(function (x) {
  78. return u.id === x.id;
  79. });
  80. if (d) {
  81. _.assign(d, u);
  82. } else {
  83. this.data.push(d);
  84. }
  85. }
  86. }
  87. if (updateData.del) {
  88. _.remove(this.data, function (d) {
  89. return updateData.del.indexOf(d.id) >= 0;
  90. });
  91. }
  92. this.resortData();
  93. }
  94. }
  95. const jgclObj = new Jgcl();
  96. postData(window.location.pathname + '/load', null, function (result) {
  97. jgclObj.loadDatas(result);
  98. SpreadJsObj.loadSheetData(jgclSheet, SpreadJsObj.DataType.Data, jgclObj.data);
  99. });
  100. if (!readOnly) {
  101. const jgclOprObj = {
  102. /**
  103. * 删除按钮响应事件
  104. * @param sheet
  105. */
  106. deletePress: function (sheet) {
  107. if (!sheet.zh_setting || readOnly) return;
  108. const sortData = sheet.zh_data;
  109. const datas = [];
  110. const sels = sheet.getSelections();
  111. if (!sels || !sels[0]) return;
  112. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  113. let bDel = false;
  114. const node = sortData[iRow];
  115. if (node) {
  116. const data = {id: node.id};
  117. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  118. const colSetting = sheet.zh_setting.cols[iCol];
  119. if (colSetting.field === 'name') {
  120. toastr.error('名称不能为空,如需删除甲供材料请使用右键删除');
  121. return;
  122. }
  123. const style = sheet.getStyle(iRow, iCol);
  124. if (!style.locked) {
  125. const colSetting = sheet.zh_setting.cols[iCol];
  126. data[colSetting.field] = null;
  127. bDel = true;
  128. }
  129. }
  130. if (bDel) {
  131. datas.push(data);
  132. }
  133. }
  134. }
  135. if (datas.length > 0) {
  136. postData(window.location.pathname + '/update', {updateType: 'update', updateData: datas}, function (result) {
  137. jgclObj.loadUpdateData(result);
  138. SpreadJsObj.reLoadSheetData(jgclSheet);
  139. }, function () {
  140. SpreadJsObj.reLoadSheetData(jgclSheet);
  141. });
  142. }
  143. },
  144. deleteJgcl: function (sheet) {
  145. if (!sheet.zh_setting || readOnly) return;
  146. const sortData = sheet.zh_data;
  147. const datas = [];
  148. const sels = sheet.getSelections();
  149. if (!sels || !sels[0]) return;
  150. const hint = {
  151. isOld: {type: 'warning', msg: '该甲供材料为往期数据,不可删除'},
  152. invalidDel: {type: 'warning', msg: '该甲供材料不是您新增的,只有原报和新增人可删除'},
  153. };
  154. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  155. const node = sortData[iRow];
  156. if (node.add_sid === stageId) {
  157. toastMessageUniq(hint.isOld);
  158. continue;
  159. } else {
  160. if (node.add_uid !== userID || stageUserId !== userID) {
  161. toastMessageUniq(hint.invalidDel);
  162. continue;
  163. }
  164. datas.push(node.id);
  165. }
  166. }
  167. if (datas.length > 0) {
  168. postData(window.location.pathname + '/update', {updateType: 'del', updateData: datas}, function (result) {
  169. jgclObj.loadUpdateData(result);
  170. SpreadJsObj.reLoadSheetData(jgclSheet);
  171. }, function () {
  172. SpreadJsObj.reLoadSheetData(jgclSheet);
  173. });
  174. }
  175. },
  176. editEnded: function (e, info) {
  177. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  178. const node = info.sheet.zh_data[info.row];
  179. const col = info.sheet.zh_setting.cols[info.col];
  180. const data = {};
  181. if (node) {
  182. data.update = {};
  183. data.update.id = node.id;
  184. const oldValue = node ? node[col.field] : null;
  185. const newValue = trimInvalidChar(info.editingText);
  186. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  187. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  188. return;
  189. }
  190. data.update[col.field] = newValue;
  191. } else {
  192. if (col.field !== 'name') {
  193. toastr.warning('新增甲供材料,请先输入名称');
  194. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  195. return;
  196. }
  197. data.add = {};
  198. data.add.order = info.row + 1;
  199. data.add.name = trimInvalidChar(info.editingText);
  200. }
  201. postData(window.location.pathname + '/update', data, function (result) {
  202. jgclObj.loadUpdateData(result);
  203. //SpreadJsObj.reLoadRowData(info.sheet, info.row);
  204. SpreadJsObj.reLoadSheetData(info.sheet);
  205. }, function () {
  206. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  207. });
  208. },
  209. editStarting(e, info) {
  210. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  211. info.cancel = true;
  212. return;
  213. }
  214. const col = info.sheet.zh_setting.cols[info.col];
  215. const node = info.sheet.zh_data[info.row];
  216. if (!node) return;
  217. switch (col.field) {
  218. case 'name':
  219. case 'unit':
  220. case 'unit_price':
  221. info.cancel = readOnly && node.add_sid !== stageId;
  222. break;
  223. }
  224. },
  225. clipboardPasting(e, info) {
  226. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  227. info.cancel = true;
  228. if (!setting || !sortData) return;
  229. const pasteData = info.pasteData.html
  230. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  231. : (info.pasteData.text === ''
  232. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  233. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  234. const hint = {
  235. name: {type: 'warning', msg: '甲供材料名称不可为空,已过滤'},
  236. unit_price: {type: 'warning', msg: '输入的 单价 非法,已过滤'},
  237. arrive_qty: {type: 'warning', msg: '输入的 本期到场-数量 非法,已过滤'},
  238. reduce_qty: {type: 'warning', msg: '输入的 本期扣回-数量 非法,已过滤'},
  239. };
  240. const uDatas = [], iDatas = [];
  241. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  242. const curRow = info.cellRange.row + iRow;
  243. const node = sortData[curRow];
  244. let bPaste = false;
  245. const data = {};
  246. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  247. const curCol = info.cellRange.col + iCol;
  248. const colSetting = setting.cols[curCol];
  249. const value = trimInvalidChar(pasteData[iRow][iCol]);
  250. if (col.field === 'name' && (!value || value === '')) {
  251. toastMessageUniq(hint.name);
  252. break;
  253. }
  254. if (colSetting.type === 'Number') {
  255. const num = _.toNumber(value);
  256. if (num) {
  257. data[colSetting.field] = num;
  258. bPaste = true;
  259. }
  260. } else {
  261. data[colSetting.field] = value;
  262. bPaste = true;
  263. }
  264. }
  265. if (bPaste) {
  266. if (node) {
  267. data.id = node.id;
  268. uDatas.push(data);
  269. } else {
  270. iDatas.push(data);
  271. }
  272. }
  273. }
  274. const updateData = {};
  275. if (uDatas.length > 0) updateData.update = uDatas;
  276. if (iDatas.length > 0) updateData.add = iDatas;
  277. if (uDatas.length > 0 || iDatas.length > 0) {
  278. postData(window.location.pathname + '/update', updateData, function (result) {
  279. jgclObj.loadUpdateData(result);
  280. SpreadJsObj.reLoadSheetData(info.sheet);
  281. });
  282. } else {
  283. SpreadJsObj.reLoadSheetData(info.sheet);
  284. }
  285. },
  286. upMove: function () {
  287. },
  288. downMove: function () {
  289. }
  290. };
  291. jgclSheet.bind(spreadNS.Events.EditEnded, jgclOprObj.editEnded);
  292. jgclSheet.bind(spreadNS.Events.EditStarting, jgclOprObj.editStarting);
  293. jgclSheet.bind(spreadNS.Events.ClipboardPasting, jgclOprObj.clipboardPasting);
  294. SpreadJsObj.addDeleteBind(jgclSpread, jgclOprObj.deletePress);
  295. $.contextMenu({
  296. selector: '#jgcl-spread',
  297. build: function ($trigger, e) {
  298. const target = SpreadJsObj.safeRightClickSelection($trigger, e, jgclSpread);
  299. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  300. },
  301. items: {
  302. del: {
  303. name: '删除',
  304. icon: 'fa-remove',
  305. callback: function (key, opt) {
  306. jgclOprObj.deleteJgcl(jgclSheet);
  307. },
  308. disabled: function (key, opt) {
  309. const sels = jgclSheet.getSelections();
  310. if (!sels || !sels[0]) return true;
  311. const row = sels[0].row;
  312. const node = jgclObj.data[row];
  313. return node === undefined || node === null;
  314. },
  315. visible: function (key, opt) {
  316. return !readOnly;
  317. }
  318. },
  319. upMove: {
  320. name: '上移',
  321. icon: 'fa-remove',
  322. callback: function (key, opt) {
  323. jgclOprObj.upMove();
  324. },
  325. disabled: function (key, opt) {
  326. const sels = jgclSheet.getSelections();
  327. if (!sels || !sels[0] || sels[0].row === 0) return true;
  328. const row = sels[0].row;
  329. const node = jgclObj.data[row];
  330. return node === undefined || node === null;
  331. },
  332. visible: function (key, opt) {
  333. return !readOnly;
  334. }
  335. },
  336. downMove: {
  337. name: '下移',
  338. icon: 'fa-remove',
  339. callback: function (key, opt) {
  340. jgclOprObj.downMove();
  341. },
  342. disabled: function (key, opt) {
  343. const sels = jgclSheet.getSelections();
  344. if (!sels || !sels[0] || sels[0].row >= jgclObj.data.length - 1) 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. },
  354. })
  355. }
  356. });