ration_glj.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var rationGLJOprObj = {
  5. processDecimal: -6,
  6. sheet: null,
  7. currentRationItem: null,
  8. distTypeTree: null,
  9. activeCell: null,
  10. tempCacheArr: [],//被更新的工料机,若更新的工料机不存在,则恢复
  11. cache: {},
  12. setting: {
  13. header: [
  14. { headerName: "编码", headerWidth: 120, dataCode: "code", dataType: "String", formatter: "@" },
  15. { headerName: "名称", headerWidth: 400, dataCode: "name", dataType: "String" },
  16. { headerName: "规格型号", headerWidth: 120, dataCode: "specs", dataType: "String" },
  17. { headerName: "单位", headerWidth: 160, dataCode: "unit", dataType: "String", hAlign: "center", vAlign: "center" },
  18. { headerName: "定额价", headerWidth: 160, dataCode: "basePrice", dataType: "Number", formatter: "0.00", precision: 2 },
  19. { headerName: "定额消耗", headerWidth: 160, dataCode: "consumeAmt", dataType: "Number", formatter: "0.000", precision: 3 },
  20. { headerName: "类型", headerWidth: 160, dataCode: "gljType", dataType: "String", hAlign: "center", vAlign: "center" }
  21. ],
  22. view: {
  23. comboBox: [],
  24. lockColumns: [1, 2, 3, 4, 6]
  25. }
  26. },
  27. getDistTypeTree: function (gljDistType) {
  28. let distType;
  29. let distTypeTree = {
  30. prefix: 'gljDistType',
  31. distTypes: {},
  32. comboDatas: [],
  33. distTypesArr: []
  34. };
  35. gljDistType.forEach(function (typeData) {
  36. let typeObj = {
  37. data: typeData,
  38. children: [],
  39. parent: null
  40. }
  41. distTypeTree.distTypes[distTypeTree.prefix + typeData.ID] = typeObj;
  42. distTypeTree.distTypesArr.push(typeObj);
  43. });
  44. gljDistType.forEach(function (typeData) {
  45. distType = distTypeTree.distTypes[distTypeTree.prefix + typeData.ID];
  46. let parent = distTypeTree.distTypes[distTypeTree.prefix + typeData.ParentID];
  47. if (parent) {
  48. distType.parent = parent;
  49. parent.children.push(distType);
  50. }
  51. });
  52. distTypeTree.distTypesArr.forEach(function (distTypeObj) {
  53. if (distTypeObj.children.length === 0 && distTypeObj.data.fullName !== '普通机械' && distTypeObj.data.fullName !== '机械组成物'
  54. && distTypeObj.data.fullName !== '机上人工') {
  55. distTypeTree.comboDatas.push({ text: distTypeObj.data.fullName, value: distTypeObj.data.ID });
  56. }
  57. if (distTypeObj.data.fullName === '机械') {
  58. distTypeTree.comboDatas.push({ text: distTypeObj.data.fullName, value: distTypeObj.data.ID });
  59. }
  60. });
  61. return distTypeTree;
  62. },
  63. buildSheet: function (sheet) {
  64. this.sheet = sheet;
  65. this.distTypeTree = this.getDistTypeTree(this.distTypeTree);
  66. sheetCommonObj.initSheet(this.sheet, this.setting, 30);
  67. this.onContextmenuOpr();
  68. this.bindRationGljDelOpr();
  69. this.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, this.onClipboardPasting);
  70. this.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, this.onClipboardPasted);
  71. this.sheet.bind(GC.Spread.Sheets.Events.EditStarting, this.onEditStarting);
  72. this.sheet.bind(GC.Spread.Sheets.Events.EditEnded, this.onCellEditEnd);
  73. },
  74. onContextmenuOpr: function () {//右键菜单
  75. let me = this;
  76. let raCoe = rationCoeOprObj;
  77. $.contextMenu({
  78. selector: '#rdSpread',
  79. build: function ($triggerElement, e) {
  80. //控制允许右键菜单在哪个位置出现
  81. let target = SheetDataHelper.safeRightClickSelection($triggerElement, e, me.sheet.getParent());
  82. let sheet = me.sheet;
  83. let addDis = false, delDis = false;
  84. let rationGlj = [];
  85. if (me.sheet.getParent().getActiveSheetIndex() === 0 && target.hitTestType === 3) {//在表格内&& typeof target.row !== 'undefined' && typeof target.col !== 'undefined'
  86. //rationGlj表
  87. if (typeof target.row !== 'undefined') {
  88. //控制按钮是否可用
  89. sheet.setActiveCell(target.row, target.col);
  90. console.log(me.currentRationItem);
  91. if (me.currentRationItem) {
  92. rationGlj = me.cache['_GLJ_' + me.currentRationItem.ID];
  93. if (!rationGlj || target.row >= rationGlj.length) {//右键定位在有数据的行,删除键才显示可用
  94. delDis = true;
  95. }
  96. else {//有数据
  97. if (typeof target.col === 'undefined') {//定位不在表格内
  98. delDis = true;
  99. }
  100. }
  101. }
  102. else {
  103. addDis = true;
  104. delDis = true;
  105. }
  106. }
  107. else {
  108. addDis = true;
  109. delDis = true;
  110. }
  111. return {
  112. callback: function () { },
  113. items: {
  114. "add": {
  115. name: "添加工料机", disabled: addDis, icon: "fa-plus", callback: function (key, opt) {
  116. gljSelOprObj.initView(0, true);
  117. }
  118. },
  119. "delete": {
  120. name: "删除工料机", disabled: delDis, icon: "fa-remove", callback: function (key, opt) {
  121. rationGlj.splice(target.row, 1);
  122. me.updateRationItem(function () {
  123. me.sheet.getParent().focus();
  124. });
  125. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  126. me.showGljItems(me.currentRationItem.ID);
  127. }
  128. },
  129. }
  130. };
  131. }
  132. //rationCoe表
  133. else if (me.sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined') {
  134. let currentCache = raCoe.curRation && raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  135. sheet.setActiveCell(target.row, target.col);
  136. //控制按钮是否可用
  137. let upDis = false,
  138. downDis = false,
  139. refDis = false;
  140. if (target.row >= currentCache.length) {
  141. upDis = true;
  142. downDis = true;
  143. refDis = true;
  144. }
  145. else {
  146. if (!raCoe.isDef(currentCache[target.row - 1])) {
  147. upDis = true;
  148. }
  149. if (!raCoe.isDef(currentCache[target.row + 1])) {
  150. downDis = true;
  151. }
  152. }
  153. return {
  154. callback: function () { },
  155. items: {
  156. "upMove": {
  157. name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  158. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], { row: target.row - 1, col: target.col });
  159. }
  160. },
  161. "downMove": {
  162. name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  163. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], { row: target.row + 1, col: target.col });
  164. }
  165. },
  166. "ref": {
  167. name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  168. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  169. for (let i = 0, len = updateArr.length; i < len; i++) {
  170. let ration = updateArr[i];
  171. let rationCoeList = updateArr[i].rationCoeList;
  172. let newNo = 1;
  173. for (let j = 0, jLen = rationCoeList.length; j < jLen; j++) {
  174. if (rationCoeList[j].no >= newNo) {
  175. newNo = rationCoeList[j].no + 1;
  176. }
  177. }
  178. let theCache = raCoe.cache["_Coe_" + ration.ID];
  179. if (theCache !== undefined && theCache !== null) {
  180. let newCoe = {};
  181. for (let attr in currentCache[target.row]) {
  182. newCoe[attr] = currentCache[target.row][attr];
  183. }
  184. newCoe.no = newNo;
  185. theCache.push(newCoe);
  186. }
  187. }
  188. });
  189. }
  190. }
  191. }
  192. };
  193. }
  194. else {
  195. return false;
  196. }
  197. }
  198. });
  199. },
  200. bindRationGljDelOpr: function () {
  201. let me = rationGLJOprObj, spreadBook = me.sheet.getParent();
  202. spreadBook.commandManager().register('rationGljDelete', function () {
  203. if (!me.currentRationItem) {
  204. return;
  205. }
  206. let sels = me.sheet.getSelections(), lockCols = me.setting.view.lockColumns;
  207. let cacheSection = me.cache["_GLJ_" + me.currentRationItem.ID], isUpdate = false;
  208. if (sels.length > 0) {
  209. for (let sel = 0; sel < sels.length; sel++) {
  210. if (sels[sel].colCount === me.setting.header.length) {
  211. if (cacheSection && sels[sel].row < cacheSection.length) {
  212. isUpdate = true;
  213. cacheSection.splice(sels[sel].row, sels[sel].rowCount);
  214. }
  215. }
  216. else {
  217. if (sels[sel].col !== 0 && sels[sel].col !== 5 && !(sels[sel].col === 1 && sels.col + sels[sel].colCount - 1 === 3)) {
  218. if (cacheSection) {
  219. for (let i = sels[sel].row === -1 ? 1 : 0; i < sels[sel].rowCount; i++) {
  220. if (sels[sel].row + i < cacheSection.length) {
  221. for (let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++) {
  222. if (lockCols.indexOf(col) === -1) {
  223. isUpdate = true;
  224. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = 0;
  225. me.sheet.setValue(sels[sel].row + i, col, 0.00);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. if (isUpdate) {
  236. me.updateRationItem(function () {
  237. me.sheet.getParent().focus(true);
  238. });
  239. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  240. me.showGljItems(me.currentRationItem.ID);
  241. }
  242. });
  243. spreadBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  244. spreadBook.commandManager().setShortcutKey('rationGljDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  245. },
  246. onClipboardPasting: function (sender, args) {
  247. var me = rationGLJOprObj;
  248. let rationSection = rationOprObj.getCache();
  249. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  250. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  251. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  252. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  253. }
  254. if (!(args.cellRange.col === 0 || args.cellRange.col === 5) || !(me.currentRationItem)) {
  255. args.cancel = true;
  256. }
  257. },
  258. onClipboardPasted: function (e, info) {
  259. var me = rationGLJOprObj, repId = pageOprObj.rationLibId;
  260. me.tempCacheArr = [];
  261. let gljLibId = pageOprObj.gljLibId;
  262. console.log(gljLibId);
  263. if (gljLibId) {
  264. if (info.cellRange.col == 0) {
  265. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  266. var tmpCodes = sheetCommonObj.analyzePasteData({ header: [{ dataCode: "code" }] }, info);
  267. var codes = [];
  268. for (var i = 0; i < tmpCodes.length; i++) {
  269. let rowIdx = info.cellRange.row + i;
  270. if (rowIdx < cacheArr.length) {//更新
  271. me.tempCacheArr.push({ org: cacheArr[rowIdx], newCode: tmpCodes[i].code });
  272. cacheArr.splice(rowIdx--, 1);
  273. }
  274. codes.push(tmpCodes[i].code);
  275. }
  276. me.addGljItems(codes, gljLibId, info.cellRange);
  277. } else {
  278. //修改用量
  279. if (me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  280. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  281. let maxCount = info.cellRange.row + info.cellRange.rowCount - 1 > me.cache["_GLJ_" + me.currentRationItem.ID].length - 1 ?
  282. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  283. for (let i = 0; i < maxCount; i++) {
  284. let roundCons = scMathUtil.roundTo(tempConsumes[i].consumeAmt, -3);
  285. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  286. }
  287. me.updateRationItem(function () {
  288. me.sheet.getParent().focus(true);
  289. });
  290. if (info.cellRange.row + info.cellRange.rowCount - 1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length - 1) {
  291. me.sheet.suspendPaint();
  292. for (let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  293. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  294. }
  295. me.sheet.resumePaint();
  296. }
  297. }
  298. else if (info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  299. me.sheet.suspendPaint();
  300. for (let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  301. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  302. }
  303. me.sheet.resumePaint();
  304. }
  305. }
  306. }
  307. },
  308. onEditStarting: function (sender, args) {
  309. let me = rationGLJOprObj;
  310. let rationSection = rationOprObj.getCache();
  311. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  312. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  313. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  314. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  315. }
  316. if (!me.currentRationItem) {
  317. args.cancel = true;
  318. }
  319. else {
  320. if (args.col !== 0 && args.col !== 5 || args.col === 5 && args.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  321. args.cancel = true;
  322. }
  323. }
  324. },
  325. onCellEditEnd: function (sender, args) {
  326. var me = rationGLJOprObj;
  327. if (me.currentRationItem) {
  328. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  329. me.tempCacheArr = [];
  330. if (args.col != 0) {
  331. if (args.row < cacheArr.length) {
  332. var editGlj = cacheArr[args.row];
  333. if (editGlj["consumeAmt"] != args.editingText) {
  334. let parseNum = parseFloat(args.editingText);
  335. if (isNaN(parseFloat(args.editingText))) {
  336. $('#alertModalBtn').click();
  337. $('#alertText').text("定额消耗只能输入数值!");
  338. $('#alertModalCls').click(function () {
  339. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  340. });
  341. $('#alertModalCof').click(function () {
  342. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  343. })
  344. }
  345. else {
  346. args.sheet.setValue(args.row, args.col, parseNum);
  347. let roundNum = scMathUtil.roundTo(parseNum, -3);
  348. editGlj["consumeAmt"] = roundNum;
  349. me.updateRationItem(function () {
  350. me.sheet.getParent().focus(true);
  351. });
  352. }
  353. }
  354. }
  355. } else {
  356. if (args.editingText && args.editingText.toString().trim().length !== 0) {
  357. let isExist = false;
  358. for (let i = 0, len = cacheArr.length; i < len; i++) {
  359. if (cacheArr[i].code === args.editingText && i !== args.row) {
  360. isExist = true;
  361. break;
  362. }
  363. }
  364. if (isExist) {
  365. alert("人材机已存在!");
  366. args.sheet.setValue(args.row, args.col, typeof cacheArr[args.row] !== 'undefined' ? cacheArr[args.row].code + '' : '');
  367. }
  368. else {
  369. if (args.row < cacheArr.length && args.editingText !== cacheArr[args.row].code) {//更新
  370. me.tempCacheArr.push({ org: cacheArr[args.row], newCode: args.editingText.toString().trim() });
  371. cacheArr.splice(args.row, 1);
  372. let gljLibID = pageOprObj.gljLibId;
  373. let codes = [];
  374. codes.push(args.editingText.toString().trim());
  375. me.addGljItems(codes, gljLibID, args);
  376. }
  377. else if (args.row >= cacheArr.length) {//新增
  378. let gljLibID = pageOprObj.gljLibId;
  379. if (gljLibID) {
  380. var codes = [];
  381. codes.push(args.editingText.toString().trim());
  382. me.addGljItems(codes, gljLibID, args);
  383. }
  384. }
  385. }
  386. }
  387. else {
  388. args.sheet.setValue(args.row, args.col, args.row < cacheArr.length ? cacheArr[args.row].code : '');
  389. }
  390. }
  391. }
  392. else {
  393. args.sheet.setValue(args.row, args.col, '');
  394. }
  395. },
  396. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的code不存在,恢复删除的被更新数据
  397. let rst = [];
  398. for (let i = 0, len = tempDelArr.length; i < len; i++) {
  399. let isExist = false;
  400. for (let j = 0, jLen = newArr.length; j < jLen; j++) {
  401. if (tempDelArr[i].newCode == newArr[j].code) {
  402. isExist = true;
  403. break;
  404. }
  405. }
  406. if (!isExist) {
  407. rst.push(tempDelArr[i].org);
  408. }
  409. }
  410. return rst;
  411. },
  412. addGljItems: function (codes, repId, args) {
  413. let me = this;
  414. CommonAjax.post('/complementaryRation/api/getGljItemsByCodes', { gljCodes: codes, rationRepId: repId }, function (rstData) {
  415. if (rstData.length > 0) {
  416. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  417. let rstArr = [], dummyR = { gljId: 0, consumeAmt: 0 }, newAddArr = [];
  418. for (let i = 0; i < rstData.length; i++) {
  419. dummyR.gljId = rstData[i].ID;
  420. dummyR.type = rstData[i].type;
  421. rstArr.push(me.createRationGljDisplayItem(dummyR, rstData[i]));
  422. }
  423. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  424. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  425. for (var i = 0; i < rstArr.length; i++) {
  426. var hasDup = false;
  427. for (var j = 0; j < cacheArr.length; j++) {
  428. if (cacheArr[j].gljId == rstArr[i].gljId) {
  429. hasDup = true;
  430. break;
  431. }
  432. }
  433. if (!hasDup) {
  434. newAddArr.push(rstArr[i]);
  435. }
  436. }
  437. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  438. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, rstData);
  439. if (recoveryArr.length > 0) {
  440. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  441. }
  442. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  443. let aV = a.gljType + a.code,
  444. bV = b.gljType + b.code;
  445. if (aV > bV) {
  446. return 1;
  447. } else if (aV < bV) {
  448. return -1
  449. }
  450. return 0;
  451. });
  452. }
  453. me.showGljItems(me.currentRationItem.ID);
  454. if (newAddArr.length > 0) {
  455. me.updateRationItem(function () {
  456. me.sheet.getParent().focus(true);
  457. });
  458. }
  459. }
  460. else {
  461. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID] ? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  462. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  463. if (recoveryArr.length > 0) {
  464. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  465. }
  466. //更新的工料机不存在
  467. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  468. let aV = a.gljType + a.code,
  469. bV = b.gljType + b.code;
  470. if (aV > bV) {
  471. return 1;
  472. } else if (aV < bV) {
  473. return -1
  474. }
  475. return 0;
  476. });
  477. $('#alertModalBtn').click();
  478. $('#alertText').text("人材机" + codes + "不存在,请查找你所需要的人材机,或新增人材机");
  479. $('#alertModalCls').click(function () {
  480. me.showGljItems(me.currentRationItem.ID);
  481. });
  482. $('#alertModalCof').click(function () {
  483. me.showGljItems(me.currentRationItem.ID);
  484. })
  485. }
  486. });
  487. },
  488. round(v, e) {
  489. var t = 1;
  490. for (; e > 0; t *= 10, e--);
  491. for (; e < 0; t /= 10, e++);
  492. return Math.round(v * t) / t;
  493. },
  494. rationCal: function () {
  495. let me = rationGLJOprObj;
  496. let rationBasePrc = 0;
  497. if (me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]) {
  498. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  499. cacheArr.forEach(function (gljData) {
  500. if (gljData.gljType && gljData.basePrice && gljData.consumeAmt) {
  501. rationBasePrc += gljData.basePrice * gljData.consumeAmt;
  502. }
  503. });
  504. rationBasePrc = scMathUtil.roundTo(rationBasePrc, 0);
  505. }
  506. return rationBasePrc;
  507. },
  508. updateRationItem: function (callback) {
  509. var me = this, updateArr = [];
  510. if (me.currentRationItem) {
  511. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  512. //recalculate ration basePrice
  513. let price = me.rationCal();
  514. me.currentRationItem.basePrice = price;
  515. updateArr.push(me.currentRationItem);
  516. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  517. if (callback) callback();
  518. });
  519. }
  520. },
  521. buildRationItemGlj: function () {
  522. var me = this, rst = [];
  523. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  524. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  525. for (var i = 0; i < cacheArr.length; i++) {
  526. const newItem = { gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, type: cacheArr[i].type };
  527. if (cacheArr[i].userId && cacheArr[i].userId !== userID) {
  528. // 是分享人材机
  529. newItem.fromUser = cacheArr[i].userId;
  530. }
  531. rst.push(newItem);
  532. }
  533. }
  534. return rst;
  535. },
  536. createRationGljDisplayItem: function (rItem, repGlj) {
  537. var rst = {};
  538. rst.gljId = rItem.gljId;
  539. rst.type = rItem.type;
  540. rst.consumeAmt = rItem.consumeAmt;
  541. rst.code = repGlj.code;
  542. rst.name = repGlj.name;
  543. rst.specs = repGlj.specs;
  544. rst.unit = repGlj.unit;
  545. rst.basePrice = repGlj.basePrice;
  546. rst.gljType = repGlj.gljType;
  547. if (rItem.fromUser) {
  548. rst.userId = rItem.fromUser;
  549. }
  550. return rst;
  551. },
  552. getGljItems: function (rationItem, callback) {
  553. let me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [], rationType = rationItem.type;
  554. me.currentRationItem = rationItem;
  555. if (me.cache["_GLJ_" + rationID]) {
  556. me.showGljItems(rationID);
  557. } else {
  558. let gljIds = [];
  559. for (let i = 0; i < rationGljList.length; i++) {
  560. let idObj = Object.create(null);
  561. idObj.type = rationGljList[i].type;
  562. idObj.id = rationGljList[i].gljId;
  563. if (rationGljList[i].fromUser) {
  564. idObj.fromUser = rationGljList[i].fromUser;
  565. }
  566. gljIds.push(idObj);
  567. }
  568. CommonAjax.post('/complementaryRation/api/getGljItemsByIds', { ids: gljIds }, function (rstData) {
  569. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  570. var cacheArr = [];
  571. for (let i = 0; i < rstData.length; i++) {
  572. for (let j = 0; j < rationGljList.length; j++) {
  573. if (rationGljList[j].gljId == rstData[i].ID) {
  574. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], rstData[i]));
  575. break;
  576. }
  577. }
  578. }
  579. function compare() {
  580. return function (a, b) {
  581. let aV = a.gljType + a.code,
  582. bV = b.gljType + b.code;
  583. if (aV > bV) {
  584. return 1;
  585. } else if (aV < bV) {
  586. return -1
  587. }
  588. return 0;
  589. }
  590. }
  591. cacheArr.sort(compare());
  592. me.cache["_GLJ_" + rationID] = cacheArr;
  593. me.showGljItems(rationID);
  594. if (callback) callback();
  595. });
  596. }
  597. },
  598. showGljItems: function (rationID) {
  599. var me = this;
  600. if (me.cache["_GLJ_" + rationID]) {
  601. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  602. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  603. let aV = a.gljType + a.code,
  604. bV = b.gljType + b.code;
  605. if (aV > bV) {
  606. return 1;
  607. } else if (aV < bV) {
  608. return -1
  609. }
  610. return 0;
  611. });
  612. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  613. }
  614. }
  615. }