se_yjcl.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. insert: function(sheet) {
  203. if (!sheet.zh_setting || !sheet.zh_data) return;
  204. const node = SpreadJsObj.getSelectObject(sheet);
  205. postData(window.location.pathname + '/update', {insert: { select: node.id, count: 1 }}, function (result) {
  206. yjclObj.loadUpdateData(result);
  207. SpreadJsObj.reLoadSheetData(sheet);
  208. }, function () {
  209. SpreadJsObj.reLoadSheetData(sheet);
  210. });
  211. },
  212. editEnded: function (e, info) {
  213. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  214. const node = info.sheet.zh_data[info.row];
  215. const col = info.sheet.zh_setting.cols[info.col];
  216. const data = {};
  217. const maxOrder = yjclObj.getMaxOrder();
  218. if (node) {
  219. data.update = {};
  220. data.update.id = node.id;
  221. const oldValue = node ? node[col.field] : null;
  222. const newValue = col.wordWrap ? info.editingText : trimInvalidChar(info.editingText);
  223. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  224. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  225. return;
  226. }
  227. data.update[col.field] = newValue;
  228. } else {
  229. if (col.field !== 'name') {
  230. toastr.warning('新增材料,请先输入名称');
  231. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  232. return;
  233. }
  234. data.add = {};
  235. data.add.m_order = maxOrder + 1;
  236. data.add.name = col.wordWrap ? info.editingText : trimInvalidChar(info.editingText);
  237. }
  238. postData(window.location.pathname + '/update', data, function (result) {
  239. yjclObj.loadUpdateData(result);
  240. SpreadJsObj.reLoadSheetData(info.sheet);
  241. }, function () {
  242. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  243. });
  244. },
  245. editStarting(e, info) {
  246. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  247. info.cancel = true;
  248. return;
  249. }
  250. const col = info.sheet.zh_setting.cols[info.col];
  251. const node = info.sheet.zh_data[info.row];
  252. if (!node) return;
  253. switch (col.field) {
  254. case 'name':
  255. case 'spec':
  256. case 'unit':
  257. case 'tax':
  258. case 'arrive_qty':
  259. case 'arrive_tp':
  260. case 'arrive_time':
  261. case 'source':
  262. case 'bills_code':
  263. case 'location':
  264. case 'prepare_pos':
  265. info.cancel = readOnly || node.add_sid !== stageId;
  266. break;
  267. }
  268. },
  269. clipboardPasting(e, info) {
  270. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  271. info.cancel = true;
  272. if (!setting || !sortData) return;
  273. const pasteData = info.pasteData.html
  274. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  275. : (info.pasteData.text === ''
  276. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  277. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  278. const hint = {
  279. name: {type: 'warning', msg: '材料名称不可为空,已过滤'},
  280. tax: {type: 'warning', msg: '输入的 税率 非法,已过滤'},
  281. arrive: {type: 'warning', msg: '输入的 到场数量/金额 非法,已过滤'},
  282. qty: {type: 'warning', msg: '输入的 本期计量-数量 非法,已过滤'},
  283. };
  284. const uDatas = [], iDatas = [];
  285. const maxOrder = yjclObj.getMaxOrder();
  286. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  287. const curRow = info.cellRange.row + iRow;
  288. const node = sortData[curRow];
  289. let bPaste = false;
  290. const data = {};
  291. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  292. const curCol = info.cellRange.col + iCol;
  293. const colSetting = setting.cols[curCol];
  294. const value = colSetting.wordWrap ? pasteData[iRow][iCol] : trimInvalidChar(pasteData[iRow][iCol]);
  295. if (colSetting.field === 'name' && (!value || value === '')) {
  296. toastMessageUniq(hint.name);
  297. break;
  298. }
  299. if (colSetting.type === 'Number') {
  300. const num = _.toNumber(value);
  301. if (num) {
  302. data[colSetting.field] = num;
  303. bPaste = true;
  304. }
  305. } else {
  306. data[colSetting.field] = value;
  307. bPaste = true;
  308. }
  309. }
  310. if (bPaste) {
  311. if (node) {
  312. data.id = node.id;
  313. uDatas.push(data);
  314. } else {
  315. data.m_order = maxOrder + iDatas.length + 1;
  316. iDatas.push(data);
  317. }
  318. }
  319. }
  320. const updateData = {};
  321. if (uDatas.length > 0) updateData.update = uDatas;
  322. if (iDatas.length > 0) updateData.add = iDatas;
  323. if (uDatas.length > 0 || iDatas.length > 0) {
  324. postData(window.location.pathname + '/update', updateData, function (result) {
  325. yjclObj.loadUpdateData(result);
  326. SpreadJsObj.reLoadSheetData(info.sheet);
  327. });
  328. } else {
  329. SpreadJsObj.reLoadSheetData(info.sheet);
  330. }
  331. },
  332. upMove: function () {
  333. const sels = yjclSheet.getSelections(), sortData = yjclSheet.zh_data;
  334. const node = sortData[sels[0].row];
  335. const preNode = sortData[sels[0].row - 1];
  336. const data = [
  337. { id: node.id, m_order: preNode.m_order },
  338. { id: preNode.id, m_order: node.m_order }
  339. ];
  340. postData(window.location.pathname + '/update', {update: data}, function (result) {
  341. yjclObj.loadUpdateData(result);
  342. SpreadJsObj.reLoadRowsData(yjclSheet, [sels[0].row, sels[0].row - 1]);
  343. yjclSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  344. });
  345. },
  346. downMove: function () {
  347. const sels = yjclSheet.getSelections(), sortData = yjclSheet.zh_data;
  348. const node = sortData[sels[0].row];
  349. const nextNode = sortData[sels[0].row + 1];
  350. const data = [
  351. {id: node.id, m_order: nextNode.m_order},
  352. {id: nextNode.id, m_order: node.m_order}
  353. ];
  354. postData(window.location.pathname + '/update', {update: data}, function (result) {
  355. yjclObj.loadUpdateData(result);
  356. SpreadJsObj.reLoadRowsData(yjclSheet, [sels[0].row, sels[0].row + 1]);
  357. yjclSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  358. });
  359. }
  360. };
  361. yjclSheet.bind(spreadNS.Events.EditEnded, yjclOprObj.editEnded);
  362. yjclSheet.bind(spreadNS.Events.EditStarting, yjclOprObj.editStarting);
  363. yjclSheet.bind(spreadNS.Events.ClipboardPasting, yjclOprObj.clipboardPasting);
  364. SpreadJsObj.addDeleteBind(yjclSpread, yjclOprObj.deletePress);
  365. $.contextMenu({
  366. selector: '#yjcl-spread',
  367. build: function ($trigger, e) {
  368. const target = SpreadJsObj.safeRightClickSelection($trigger, e, yjclSpread);
  369. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  370. },
  371. items: {
  372. del: {
  373. name: '删除',
  374. icon: 'fa-remove',
  375. callback: function (key, opt) {
  376. yjclOprObj.delete(yjclSheet);
  377. },
  378. disabled: function (key, opt) {
  379. const sels = yjclSheet.getSelections();
  380. if (!sels || !sels[0]) return true;
  381. const row = sels[0].row;
  382. const node = yjclObj.data[row];
  383. return node === undefined || node === null;
  384. },
  385. visible: function (key, opt) {
  386. return !readOnly;
  387. }
  388. },
  389. sprDel: '------------',
  390. insert: {
  391. name: '插入',
  392. icon: 'fa-plus',
  393. callback: function (key, opt) {
  394. yjclOprObj.insert(yjclSheet);
  395. },
  396. disabled: function (key, opt) {
  397. const node = SpreadJsObj.getSelectObject(yjclSheet);
  398. return !node;
  399. },
  400. visible: function (key, opt) {
  401. return !readOnly;
  402. }
  403. },
  404. upMove: {
  405. name: '上移',
  406. icon: 'fa-arrow-up',
  407. callback: function (key, opt) {
  408. yjclOprObj.upMove();
  409. },
  410. disabled: function (key, opt) {
  411. const sels = yjclSheet.getSelections();
  412. if (!sels || !sels[0] || sels[0].row === 0) return true;
  413. const row = sels[0].row;
  414. const node = yjclObj.data[row];
  415. return node === undefined || node === null;
  416. },
  417. visible: function (key, opt) {
  418. return !readOnly;
  419. }
  420. },
  421. downMove: {
  422. name: '下移',
  423. icon: 'fa-arrow-down',
  424. callback: function (key, opt) {
  425. yjclOprObj.downMove();
  426. },
  427. disabled: function (key, opt) {
  428. const sels = yjclSheet.getSelections();
  429. if (!sels || !sels[0] || sels[0].row >= yjclObj.data.length - 1) return true;
  430. const row = sels[0].row;
  431. const node = yjclObj.data[row];
  432. return node === undefined || node === null;
  433. },
  434. visible: function (key, opt) {
  435. return !readOnly;
  436. }
  437. }
  438. },
  439. })
  440. }
  441. $('#exportExcel').click(function () {
  442. SpreadExcelObj.exportSimpleXlsxSheet(spreadSetting, yjclObj.data, $('.sidebar-title').attr('data-original-title') + "-永久材料.xlsx");
  443. });
  444. });