se_yjcl.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. getMaxOrder() {
  118. return this.data.length > 0 ? this.data[this.data.length - 1].m_order : 0;
  119. }
  120. }
  121. const yjclObj = new Yjcl();
  122. postData(window.location.pathname + '/load', null, function (result) {
  123. yjclObj.loadDatas(result);
  124. SpreadJsObj.loadSheetData(yjclSheet, SpreadJsObj.DataType.Data, yjclObj.data);
  125. });
  126. if (!readOnly) {
  127. const yjclOprObj = {
  128. /**
  129. * 删除按钮响应事件
  130. * @param sheet
  131. */
  132. deletePress: function (sheet) {
  133. if (!sheet.zh_setting || readOnly) return;
  134. const sortData = sheet.zh_data;
  135. const datas = [];
  136. const sels = sheet.getSelections();
  137. if (!sels || !sels[0]) return;
  138. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  139. let bDel = false;
  140. const node = sortData[iRow];
  141. if (node) {
  142. const data = {id: node.id};
  143. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  144. const colSetting = sheet.zh_setting.cols[iCol];
  145. if (colSetting.field === 'name') {
  146. toastr.error('名称不能为空,如需删除材料请使用右键删除');
  147. return;
  148. }
  149. const style = sheet.getStyle(iRow, iCol);
  150. if (!style.locked) {
  151. const colSetting = sheet.zh_setting.cols[iCol];
  152. data[colSetting.field] = colSetting.type === 'Number' ? 0 : '';
  153. bDel = true;
  154. }
  155. }
  156. if (bDel) {
  157. datas.push(data);
  158. }
  159. }
  160. }
  161. if (datas.length > 0) {
  162. postData(window.location.pathname + '/update', {update: datas}, function (result) {
  163. yjclObj.loadUpdateData(result);
  164. SpreadJsObj.reLoadSheetData(yjclSheet);
  165. }, function () {
  166. SpreadJsObj.reLoadSheetData(yjclSheet);
  167. });
  168. }
  169. },
  170. delete: function (sheet) {
  171. if (!sheet.zh_setting || readOnly) return;
  172. const sortData = sheet.zh_data;
  173. const datas = [];
  174. const sels = sheet.getSelections();
  175. if (!sels || !sels[0]) return;
  176. const hint = {
  177. isOld: {type: 'warning', msg: '该材料已计量,不可删除'},
  178. invalidDel: {type: 'warning', msg: '该材料不是您新增的,只有原报和新增人可删除'},
  179. };
  180. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  181. const node = sortData[iRow];
  182. if (node.pre_used || !checkZero(node.end_qty) || !checkZero(node.end_qty)) {
  183. toastMessageUniq(hint.isOld);
  184. continue;
  185. } else {
  186. if (node.add_uid !== userID && stageUserId !== userID) {
  187. toastMessageUniq(hint.invalidDel);
  188. continue;
  189. }
  190. datas.push(node.id);
  191. }
  192. }
  193. if (datas.length > 0) {
  194. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  195. yjclObj.loadUpdateData(result);
  196. SpreadJsObj.reLoadSheetData(yjclSheet);
  197. }, function () {
  198. SpreadJsObj.reLoadSheetData(yjclSheet);
  199. });
  200. }
  201. },
  202. editEnded: function (e, info) {
  203. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  204. const node = info.sheet.zh_data[info.row];
  205. const col = info.sheet.zh_setting.cols[info.col];
  206. const data = {};
  207. const maxOrder = yjclObj.getMaxOrder();
  208. if (node) {
  209. data.update = {};
  210. data.update.id = node.id;
  211. const oldValue = node ? node[col.field] : null;
  212. const newValue = col.wordWrap ? info.editingText : trimInvalidChar(info.editingText);
  213. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  214. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  215. return;
  216. }
  217. data.update[col.field] = newValue;
  218. } else {
  219. if (col.field !== 'name') {
  220. toastr.warning('新增材料,请先输入名称');
  221. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  222. return;
  223. }
  224. data.add = {};
  225. data.add.m_order = maxOrder + 1;
  226. data.add.name = col.wordWrap ? info.editingText : trimInvalidChar(info.editingText);
  227. }
  228. postData(window.location.pathname + '/update', data, function (result) {
  229. yjclObj.loadUpdateData(result);
  230. SpreadJsObj.reLoadSheetData(info.sheet);
  231. }, function () {
  232. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  233. });
  234. },
  235. editStarting(e, info) {
  236. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  237. info.cancel = true;
  238. return;
  239. }
  240. const col = info.sheet.zh_setting.cols[info.col];
  241. const node = info.sheet.zh_data[info.row];
  242. if (!node) return;
  243. switch (col.field) {
  244. case 'name':
  245. case 'spec':
  246. case 'unit':
  247. case 'tax':
  248. case 'arrive_qty':
  249. case 'arrive_tp':
  250. case 'arrive_time':
  251. case 'source':
  252. case 'bills_code':
  253. case 'location':
  254. case 'prepare_pos':
  255. info.cancel = readOnly || node.add_sid !== stageId;
  256. break;
  257. }
  258. },
  259. clipboardPasting(e, info) {
  260. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  261. info.cancel = true;
  262. if (!setting || !sortData) return;
  263. const pasteData = info.pasteData.html
  264. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  265. : (info.pasteData.text === ''
  266. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  267. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  268. const hint = {
  269. name: {type: 'warning', msg: '材料名称不可为空,已过滤'},
  270. tax: {type: 'warning', msg: '输入的 税率 非法,已过滤'},
  271. arrive: {type: 'warning', msg: '输入的 到场数量/金额 非法,已过滤'},
  272. qty: {type: 'warning', msg: '输入的 本期计量-数量 非法,已过滤'},
  273. };
  274. const uDatas = [], iDatas = [];
  275. const maxOrder = yjclObj.getMaxOrder();
  276. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  277. const curRow = info.cellRange.row + iRow;
  278. const node = sortData[curRow];
  279. let bPaste = false;
  280. const data = {};
  281. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  282. const curCol = info.cellRange.col + iCol;
  283. const colSetting = setting.cols[curCol];
  284. const value = colSetting.wordWrap ? pasteData[iRow][iCol] : trimInvalidChar(pasteData[iRow][iCol]);
  285. if (colSetting.field === 'name' && (!value || value === '')) {
  286. toastMessageUniq(hint.name);
  287. break;
  288. }
  289. if (colSetting.type === 'Number') {
  290. const num = _.toNumber(value);
  291. if (num) {
  292. data[colSetting.field] = num;
  293. bPaste = true;
  294. }
  295. } else {
  296. data[colSetting.field] = value;
  297. bPaste = true;
  298. }
  299. }
  300. if (bPaste) {
  301. if (node) {
  302. data.id = node.id;
  303. uDatas.push(data);
  304. } else {
  305. data.m_order = maxOrder + iDatas.length + 1;
  306. iDatas.push(data);
  307. }
  308. }
  309. }
  310. const updateData = {};
  311. if (uDatas.length > 0) updateData.update = uDatas;
  312. if (iDatas.length > 0) updateData.add = iDatas;
  313. if (uDatas.length > 0 || iDatas.length > 0) {
  314. postData(window.location.pathname + '/update', updateData, function (result) {
  315. yjclObj.loadUpdateData(result);
  316. SpreadJsObj.reLoadSheetData(info.sheet);
  317. });
  318. } else {
  319. SpreadJsObj.reLoadSheetData(info.sheet);
  320. }
  321. },
  322. upMove: function () {
  323. const sels = yjclSheet.getSelections(), sortData = yjclSheet.zh_data;
  324. const node = sortData[sels[0].row];
  325. const preNode = sortData[sels[0].row - 1];
  326. const data = [
  327. { id: node.id, m_order: preNode.m_order },
  328. { id: preNode.id, m_order: node.m_order }
  329. ];
  330. postData(window.location.pathname + '/update', {update: data}, function (result) {
  331. yjclObj.loadUpdateData(result);
  332. SpreadJsObj.reLoadRowsData(yjclSheet, [sels[0].row, sels[0].row - 1]);
  333. yjclSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  334. });
  335. },
  336. downMove: function () {
  337. const sels = yjclSheet.getSelections(), sortData = yjclSheet.zh_data;
  338. const node = sortData[sels[0].row];
  339. const nextNode = sortData[sels[0].row + 1];
  340. const data = [
  341. {id: node.id, m_order: nextNode.m_order},
  342. {id: nextNode.id, m_order: node.m_order}
  343. ];
  344. postData(window.location.pathname + '/update', {update: data}, function (result) {
  345. yjclObj.loadUpdateData(result);
  346. SpreadJsObj.reLoadRowsData(yjclSheet, [sels[0].row, sels[0].row + 1]);
  347. yjclSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  348. });
  349. }
  350. };
  351. yjclSheet.bind(spreadNS.Events.EditEnded, yjclOprObj.editEnded);
  352. yjclSheet.bind(spreadNS.Events.EditStarting, yjclOprObj.editStarting);
  353. yjclSheet.bind(spreadNS.Events.ClipboardPasting, yjclOprObj.clipboardPasting);
  354. SpreadJsObj.addDeleteBind(yjclSpread, yjclOprObj.deletePress);
  355. $.contextMenu({
  356. selector: '#yjcl-spread',
  357. build: function ($trigger, e) {
  358. const target = SpreadJsObj.safeRightClickSelection($trigger, e, yjclSpread);
  359. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  360. },
  361. items: {
  362. del: {
  363. name: '删除',
  364. icon: 'fa-remove',
  365. callback: function (key, opt) {
  366. yjclOprObj.delete(yjclSheet);
  367. },
  368. disabled: function (key, opt) {
  369. const sels = yjclSheet.getSelections();
  370. if (!sels || !sels[0]) return true;
  371. const row = sels[0].row;
  372. const node = yjclObj.data[row];
  373. return node === undefined || node === null;
  374. },
  375. visible: function (key, opt) {
  376. return !readOnly;
  377. }
  378. },
  379. sprDel: '------------',
  380. upMove: {
  381. name: '上移',
  382. icon: 'fa-arrow-up',
  383. callback: function (key, opt) {
  384. yjclOprObj.upMove();
  385. },
  386. disabled: function (key, opt) {
  387. const sels = yjclSheet.getSelections();
  388. if (!sels || !sels[0] || sels[0].row === 0) return true;
  389. const row = sels[0].row;
  390. const node = yjclObj.data[row];
  391. return node === undefined || node === null;
  392. },
  393. visible: function (key, opt) {
  394. return !readOnly;
  395. }
  396. },
  397. downMove: {
  398. name: '下移',
  399. icon: 'fa-arrow-down',
  400. callback: function (key, opt) {
  401. yjclOprObj.downMove();
  402. },
  403. disabled: function (key, opt) {
  404. const sels = yjclSheet.getSelections();
  405. if (!sels || !sels[0] || sels[0].row >= yjclObj.data.length - 1) return true;
  406. const row = sels[0].row;
  407. const node = yjclObj.data[row];
  408. return node === undefined || node === null;
  409. },
  410. visible: function (key, opt) {
  411. return !readOnly;
  412. }
  413. }
  414. },
  415. })
  416. }
  417. $('#exportExcel').click(function () {
  418. SpreadExcelObj.exportSimpleXlsxSheet(spreadSetting, yjclObj.data, $('.sidebar-title').attr('data-original-title') + "-永久材料.xlsx");
  419. });
  420. });