se_yjcl.js 20 KB

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