ration_glj.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. debugger;
  260. var me = rationGLJOprObj, repId = pageOprObj.rationLibId;
  261. me.tempCacheArr = [];
  262. let gljLibId = pageOprObj.gljLibId;
  263. console.log(gljLibId);
  264. if (gljLibId) {
  265. if (info.cellRange.col == 0) {
  266. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  267. var tmpCodes = sheetCommonObj.analyzePasteData({ header: [{ dataCode: "code" }] }, info);
  268. var codes = [];
  269. for (var i = 0; i < tmpCodes.length; i++) {
  270. let rowIdx = info.cellRange.row + i;
  271. if (rowIdx < cacheArr.length) {//更新
  272. me.tempCacheArr.push({ org: cacheArr[rowIdx], newCode: tmpCodes[i].code });
  273. cacheArr.splice(rowIdx--, 1);
  274. }
  275. codes.push(tmpCodes[i].code);
  276. }
  277. me.addGljItems(codes, gljLibId, info.cellRange);
  278. } else {
  279. //修改用量
  280. if (me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  281. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  282. let maxCount = info.cellRange.row + info.cellRange.rowCount - 1 > me.cache["_GLJ_" + me.currentRationItem.ID].length - 1 ?
  283. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  284. for (let i = 0; i < maxCount; i++) {
  285. let roundCons = scMathUtil.roundTo(tempConsumes[i].consumeAmt, -3);
  286. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  287. }
  288. me.updateRationItem(function () {
  289. me.sheet.getParent().focus(true);
  290. });
  291. if (info.cellRange.row + info.cellRange.rowCount - 1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length - 1) {
  292. me.sheet.suspendPaint();
  293. for (let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  294. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  295. }
  296. me.sheet.resumePaint();
  297. }
  298. }
  299. else if (info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  300. me.sheet.suspendPaint();
  301. for (let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  302. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  303. }
  304. me.sheet.resumePaint();
  305. }
  306. }
  307. }
  308. },
  309. onEditStarting: function (sender, args) {
  310. let me = rationGLJOprObj;
  311. let rationSection = rationOprObj.getCache();
  312. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  313. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  314. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  315. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  316. }
  317. if (!me.currentRationItem) {
  318. args.cancel = true;
  319. }
  320. else {
  321. if (args.col !== 0 && args.col !== 5 || args.col === 5 && args.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  322. args.cancel = true;
  323. }
  324. }
  325. },
  326. onCellEditEnd: function (sender, args) {
  327. var me = rationGLJOprObj;
  328. if (me.currentRationItem) {
  329. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  330. me.tempCacheArr = [];
  331. if (args.col != 0) {
  332. if (args.row < cacheArr.length) {
  333. var editGlj = cacheArr[args.row];
  334. if (editGlj["consumeAmt"] != args.editingText) {
  335. let parseNum = parseFloat(args.editingText);
  336. if (isNaN(parseFloat(args.editingText))) {
  337. $('#alertModalBtn').click();
  338. $('#alertText').text("定额消耗只能输入数值!");
  339. $('#alertModalCls').click(function () {
  340. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  341. });
  342. $('#alertModalCof').click(function () {
  343. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  344. })
  345. }
  346. else {
  347. args.sheet.setValue(args.row, args.col, parseNum);
  348. let roundNum = scMathUtil.roundTo(parseNum, -3);
  349. editGlj["consumeAmt"] = roundNum;
  350. me.updateRationItem(function () {
  351. me.sheet.getParent().focus(true);
  352. });
  353. }
  354. }
  355. }
  356. } else {
  357. if (args.editingText && args.editingText.toString().trim().length !== 0) {
  358. let isExist = false;
  359. for (let i = 0, len = cacheArr.length; i < len; i++) {
  360. if (cacheArr[i].code === args.editingText && i !== args.row) {
  361. isExist = true;
  362. break;
  363. }
  364. }
  365. if (isExist) {
  366. alert("人材机已存在!");
  367. args.sheet.setValue(args.row, args.col, typeof cacheArr[args.row] !== 'undefined' ? cacheArr[args.row].code + '' : '');
  368. }
  369. else {
  370. if (args.row < cacheArr.length && args.editingText !== cacheArr[args.row].code) {//更新
  371. me.tempCacheArr.push({ org: cacheArr[args.row], newCode: args.editingText.toString().trim() });
  372. cacheArr.splice(args.row, 1);
  373. let gljLibID = pageOprObj.gljLibId;
  374. let codes = [];
  375. codes.push(args.editingText.toString().trim());
  376. me.addGljItems(codes, gljLibID, args);
  377. }
  378. else if (args.row >= cacheArr.length) {//新增
  379. let gljLibID = pageOprObj.gljLibId;
  380. if (gljLibID) {
  381. var codes = [];
  382. codes.push(args.editingText.toString().trim());
  383. me.addGljItems(codes, gljLibID, args);
  384. }
  385. }
  386. }
  387. }
  388. else {
  389. args.sheet.setValue(args.row, args.col, args.row < cacheArr.length ? cacheArr[args.row].code : '');
  390. }
  391. }
  392. }
  393. else {
  394. args.sheet.setValue(args.row, args.col, '');
  395. }
  396. },
  397. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的code不存在,恢复删除的被更新数据
  398. let rst = [];
  399. for (let i = 0, len = tempDelArr.length; i < len; i++) {
  400. let isExist = false;
  401. for (let j = 0, jLen = newArr.length; j < jLen; j++) {
  402. if (tempDelArr[i].newCode == newArr[j].code) {
  403. isExist = true;
  404. break;
  405. }
  406. }
  407. if (!isExist) {
  408. rst.push(tempDelArr[i].org);
  409. }
  410. }
  411. return rst;
  412. },
  413. addGljItems: function (codes, repId, args) {
  414. let me = this;
  415. CommonAjax.post('/complementaryRation/api/getGljItemsByCodes', { gljCodes: codes, rationRepId: repId }, function (rstData) {
  416. if (rstData.length > 0) {
  417. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  418. let rstArr = [], dummyR = { gljId: 0, consumeAmt: 0 }, newAddArr = [];
  419. for (let i = 0; i < rstData.length; i++) {
  420. dummyR.gljId = rstData[i].ID;
  421. dummyR.type = rstData[i].type;
  422. rstArr.push(me.createRationGljDisplayItem(dummyR, rstData[i]));
  423. }
  424. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  425. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  426. for (var i = 0; i < rstArr.length; i++) {
  427. var hasDup = false;
  428. for (var j = 0; j < cacheArr.length; j++) {
  429. if (cacheArr[j].gljId == rstArr[i].gljId) {
  430. hasDup = true;
  431. break;
  432. }
  433. }
  434. if (!hasDup) {
  435. newAddArr.push(rstArr[i]);
  436. }
  437. }
  438. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  439. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, rstData);
  440. if (recoveryArr.length > 0) {
  441. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  442. }
  443. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  444. let aV = a.gljType + a.code,
  445. bV = b.gljType + b.code;
  446. if (aV > bV) {
  447. return 1;
  448. } else if (aV < bV) {
  449. return -1
  450. }
  451. return 0;
  452. });
  453. }
  454. me.showGljItems(me.currentRationItem.ID);
  455. if (newAddArr.length > 0) {
  456. me.updateRationItem(function () {
  457. me.sheet.getParent().focus(true);
  458. });
  459. }
  460. }
  461. else {
  462. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID] ? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  463. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  464. if (recoveryArr.length > 0) {
  465. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  466. }
  467. //更新的工料机不存在
  468. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  469. let aV = a.gljType + a.code,
  470. bV = b.gljType + b.code;
  471. if (aV > bV) {
  472. return 1;
  473. } else if (aV < bV) {
  474. return -1
  475. }
  476. return 0;
  477. });
  478. $('#alertModalBtn').click();
  479. $('#alertText').text("人材机" + codes + "不存在,请查找你所需要的人材机,或新增人材机");
  480. $('#alertModalCls').click(function () {
  481. me.showGljItems(me.currentRationItem.ID);
  482. });
  483. $('#alertModalCof').click(function () {
  484. me.showGljItems(me.currentRationItem.ID);
  485. })
  486. }
  487. });
  488. },
  489. round(v, e) {
  490. var t = 1;
  491. for (; e > 0; t *= 10, e--);
  492. for (; e < 0; t /= 10, e++);
  493. return Math.round(v * t) / t;
  494. },
  495. rationCal: function () {
  496. let me = rationGLJOprObj;
  497. let rationBasePrc = 0;
  498. if (me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]) {
  499. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  500. cacheArr.forEach(function (gljData) {
  501. if (gljData.gljType && gljData.basePrice && gljData.consumeAmt) {
  502. rationBasePrc += gljData.basePrice * gljData.consumeAmt;
  503. }
  504. });
  505. rationBasePrc = scMathUtil.roundTo(rationBasePrc, 0);
  506. }
  507. return rationBasePrc;
  508. },
  509. updateRationItem: function (callback) {
  510. var me = this, updateArr = [];
  511. if (me.currentRationItem) {
  512. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  513. //recalculate ration basePrice
  514. let price = me.rationCal();
  515. me.currentRationItem.basePrice = price;
  516. updateArr.push(me.currentRationItem);
  517. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  518. if (callback) callback();
  519. });
  520. }
  521. },
  522. buildRationItemGlj: function () {
  523. var me = this, rst = [];
  524. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  525. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  526. for (var i = 0; i < cacheArr.length; i++) {
  527. rst.push({ gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, type: cacheArr[i].type });
  528. }
  529. }
  530. return rst;
  531. },
  532. createRationGljDisplayItem: function (rItem, repGlj) {
  533. var rst = {};
  534. rst.gljId = rItem.gljId;
  535. rst.type = rItem.type;
  536. rst.consumeAmt = rItem.consumeAmt;
  537. rst.code = repGlj.code;
  538. rst.name = repGlj.name;
  539. rst.specs = repGlj.specs;
  540. rst.unit = repGlj.unit;
  541. rst.basePrice = repGlj.basePrice;
  542. rst.gljType = repGlj.gljType;
  543. return rst;
  544. },
  545. getGljItems: function (rationItem, callback) {
  546. let me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [], rationType = rationItem.type;
  547. me.currentRationItem = rationItem;
  548. if (me.cache["_GLJ_" + rationID]) {
  549. me.showGljItems(rationID);
  550. } else {
  551. let gljIds = [];
  552. for (let i = 0; i < rationGljList.length; i++) {
  553. let idObj = Object.create(null);
  554. idObj.type = rationGljList[i].type;
  555. idObj.id = rationGljList[i].gljId;
  556. gljIds.push(idObj);
  557. }
  558. CommonAjax.post('/complementaryRation/api/getGljItemsByIds', { ids: gljIds }, function (rstData) {
  559. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  560. var cacheArr = [];
  561. for (let i = 0; i < rstData.length; i++) {
  562. for (let j = 0; j < rationGljList.length; j++) {
  563. if (rationGljList[j].gljId == rstData[i].ID) {
  564. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], rstData[i]));
  565. break;
  566. }
  567. }
  568. }
  569. function compare() {
  570. return function (a, b) {
  571. let aV = a.gljType + a.code,
  572. bV = b.gljType + b.code;
  573. if (aV > bV) {
  574. return 1;
  575. } else if (aV < bV) {
  576. return -1
  577. }
  578. return 0;
  579. }
  580. }
  581. cacheArr.sort(compare());
  582. me.cache["_GLJ_" + rationID] = cacheArr;
  583. me.showGljItems(rationID);
  584. if (callback) callback();
  585. });
  586. }
  587. },
  588. showGljItems: function (rationID) {
  589. var me = this;
  590. if (me.cache["_GLJ_" + rationID]) {
  591. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  592. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  593. let aV = a.gljType + a.code,
  594. bV = b.gljType + b.code;
  595. if (aV > bV) {
  596. return 1;
  597. } else if (aV < bV) {
  598. return -1
  599. }
  600. return 0;
  601. });
  602. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  603. }
  604. }
  605. }