ration_glj.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. //默认radio所有工料机
  117. gljSelOprObj.initRadio();
  118. gljSelOprObj.gljCurTypeId = null;
  119. gljSelOprObj.initClassTree('std', gljSelOprObj.treeData.std);
  120. //弹出窗口
  121. $('#selGlj').modal('show');
  122. }
  123. },
  124. "delete": {
  125. name: "删除工料机", disabled: delDis, icon: "fa-remove", callback: function (key, opt) {
  126. rationGlj.splice(target.row, 1);
  127. me.updateRationItem(function () {
  128. me.sheet.getParent().focus();
  129. });
  130. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  131. me.showGljItems(me.currentRationItem.ID);
  132. }
  133. },
  134. }
  135. };
  136. }
  137. //rationCoe表
  138. else if (me.sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined') {
  139. let currentCache = raCoe.curRation && raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  140. sheet.setActiveCell(target.row, target.col);
  141. //控制按钮是否可用
  142. let upDis = false,
  143. downDis = false,
  144. refDis = false;
  145. if (target.row >= currentCache.length) {
  146. upDis = true;
  147. downDis = true;
  148. refDis = true;
  149. }
  150. else {
  151. if (!raCoe.isDef(currentCache[target.row - 1])) {
  152. upDis = true;
  153. }
  154. if (!raCoe.isDef(currentCache[target.row + 1])) {
  155. downDis = true;
  156. }
  157. }
  158. return {
  159. callback: function () { },
  160. items: {
  161. "upMove": {
  162. name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  163. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], { row: target.row - 1, col: target.col });
  164. }
  165. },
  166. "downMove": {
  167. name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  168. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], { row: target.row + 1, col: target.col });
  169. }
  170. },
  171. "ref": {
  172. name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  173. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  174. for (let i = 0, len = updateArr.length; i < len; i++) {
  175. let ration = updateArr[i];
  176. let rationCoeList = updateArr[i].rationCoeList;
  177. let newNo = 1;
  178. for (let j = 0, jLen = rationCoeList.length; j < jLen; j++) {
  179. if (rationCoeList[j].no >= newNo) {
  180. newNo = rationCoeList[j].no + 1;
  181. }
  182. }
  183. let theCache = raCoe.cache["_Coe_" + ration.ID];
  184. if (theCache !== undefined && theCache !== null) {
  185. let newCoe = {};
  186. for (let attr in currentCache[target.row]) {
  187. newCoe[attr] = currentCache[target.row][attr];
  188. }
  189. newCoe.no = newNo;
  190. theCache.push(newCoe);
  191. }
  192. }
  193. });
  194. }
  195. }
  196. }
  197. };
  198. }
  199. else {
  200. return false;
  201. }
  202. }
  203. });
  204. },
  205. bindRationGljDelOpr: function () {
  206. let me = rationGLJOprObj, spreadBook = me.sheet.getParent();
  207. spreadBook.commandManager().register('rationGljDelete', function () {
  208. if (!me.currentRationItem) {
  209. return;
  210. }
  211. let sels = me.sheet.getSelections(), lockCols = me.setting.view.lockColumns;
  212. let cacheSection = me.cache["_GLJ_" + me.currentRationItem.ID], isUpdate = false;
  213. if (sels.length > 0) {
  214. for (let sel = 0; sel < sels.length; sel++) {
  215. if (sels[sel].colCount === me.setting.header.length) {
  216. if (cacheSection && sels[sel].row < cacheSection.length) {
  217. isUpdate = true;
  218. cacheSection.splice(sels[sel].row, sels[sel].rowCount);
  219. }
  220. }
  221. else {
  222. if (sels[sel].col !== 0 && sels[sel].col !== 5 && !(sels[sel].col === 1 && sels.col + sels[sel].colCount - 1 === 3)) {
  223. if (cacheSection) {
  224. for (let i = sels[sel].row === -1 ? 1 : 0; i < sels[sel].rowCount; i++) {
  225. if (sels[sel].row + i < cacheSection.length) {
  226. for (let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++) {
  227. if (lockCols.indexOf(col) === -1) {
  228. isUpdate = true;
  229. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = 0;
  230. me.sheet.setValue(sels[sel].row + i, col, 0.00);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. if (isUpdate) {
  241. me.updateRationItem(function () {
  242. me.sheet.getParent().focus(true);
  243. });
  244. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  245. me.showGljItems(me.currentRationItem.ID);
  246. }
  247. });
  248. spreadBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  249. spreadBook.commandManager().setShortcutKey('rationGljDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  250. },
  251. onClipboardPasting: function (sender, args) {
  252. var me = rationGLJOprObj;
  253. let rationSection = rationOprObj.getCache();
  254. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  255. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  256. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  257. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  258. }
  259. if (!(args.cellRange.col === 0 || args.cellRange.col === 5) || !(me.currentRationItem)) {
  260. args.cancel = true;
  261. }
  262. },
  263. onClipboardPasted: function (e, info) {
  264. debugger;
  265. var me = rationGLJOprObj, repId = pageOprObj.rationLibId;
  266. me.tempCacheArr = [];
  267. let gljLibId = pageOprObj.gljLibId;
  268. console.log(gljLibId);
  269. if (gljLibId) {
  270. if (info.cellRange.col == 0) {
  271. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  272. var tmpCodes = sheetCommonObj.analyzePasteData({ header: [{ dataCode: "code" }] }, info);
  273. var codes = [];
  274. for (var i = 0; i < tmpCodes.length; i++) {
  275. let rowIdx = info.cellRange.row + i;
  276. if (rowIdx < cacheArr.length) {//更新
  277. me.tempCacheArr.push({ org: cacheArr[rowIdx], newCode: tmpCodes[i].code });
  278. cacheArr.splice(rowIdx--, 1);
  279. }
  280. codes.push(tmpCodes[i].code);
  281. }
  282. me.addGljItems(codes, gljLibId, info.cellRange);
  283. } else {
  284. //修改用量
  285. if (me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  286. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  287. let maxCount = info.cellRange.row + info.cellRange.rowCount - 1 > me.cache["_GLJ_" + me.currentRationItem.ID].length - 1 ?
  288. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  289. for (let i = 0; i < maxCount; i++) {
  290. let roundCons = scMathUtil.roundTo(tempConsumes[i].consumeAmt, -3);
  291. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  292. }
  293. me.updateRationItem(function () {
  294. me.sheet.getParent().focus(true);
  295. });
  296. if (info.cellRange.row + info.cellRange.rowCount - 1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length - 1) {
  297. me.sheet.suspendPaint();
  298. for (let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  299. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  300. }
  301. me.sheet.resumePaint();
  302. }
  303. }
  304. else if (info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  305. me.sheet.suspendPaint();
  306. for (let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  307. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  308. }
  309. me.sheet.resumePaint();
  310. }
  311. }
  312. }
  313. },
  314. onEditStarting: function (sender, args) {
  315. let me = rationGLJOprObj;
  316. let rationSection = rationOprObj.getCache();
  317. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  318. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  319. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  320. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  321. }
  322. if (!me.currentRationItem) {
  323. args.cancel = true;
  324. }
  325. else {
  326. if (args.col !== 0 && args.col !== 5 || args.col === 5 && args.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  327. args.cancel = true;
  328. }
  329. }
  330. },
  331. onCellEditEnd: function (sender, args) {
  332. var me = rationGLJOprObj;
  333. if (me.currentRationItem) {
  334. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  335. me.tempCacheArr = [];
  336. if (args.col != 0) {
  337. if (args.row < cacheArr.length) {
  338. var editGlj = cacheArr[args.row];
  339. if (editGlj["consumeAmt"] != args.editingText) {
  340. let parseNum = parseFloat(args.editingText);
  341. if (isNaN(parseFloat(args.editingText))) {
  342. $('#alertModalBtn').click();
  343. $('#alertText').text("定额消耗只能输入数值!");
  344. $('#alertModalCls').click(function () {
  345. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  346. });
  347. $('#alertModalCof').click(function () {
  348. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  349. })
  350. }
  351. else {
  352. args.sheet.setValue(args.row, args.col, parseNum);
  353. let roundNum = scMathUtil.roundTo(parseNum, -3);
  354. editGlj["consumeAmt"] = roundNum;
  355. me.updateRationItem(function () {
  356. me.sheet.getParent().focus(true);
  357. });
  358. }
  359. }
  360. }
  361. } else {
  362. if (args.editingText && args.editingText.toString().trim().length !== 0) {
  363. let isExist = false;
  364. for (let i = 0, len = cacheArr.length; i < len; i++) {
  365. if (cacheArr[i].code === args.editingText && i !== args.row) {
  366. isExist = true;
  367. break;
  368. }
  369. }
  370. if (isExist) {
  371. alert("人材机已存在!");
  372. args.sheet.setValue(args.row, args.col, typeof cacheArr[args.row] !== 'undefined' ? cacheArr[args.row].code + '' : '');
  373. }
  374. else {
  375. if (args.row < cacheArr.length && args.editingText !== cacheArr[args.row].code) {//更新
  376. me.tempCacheArr.push({ org: cacheArr[args.row], newCode: args.editingText.toString().trim() });
  377. cacheArr.splice(args.row, 1);
  378. let gljLibID = pageOprObj.gljLibId;
  379. let codes = [];
  380. codes.push(args.editingText.toString().trim());
  381. me.addGljItems(codes, gljLibID, args);
  382. }
  383. else if (args.row >= cacheArr.length) {//新增
  384. let gljLibID = pageOprObj.gljLibId;
  385. if (gljLibID) {
  386. var codes = [];
  387. codes.push(args.editingText.toString().trim());
  388. me.addGljItems(codes, gljLibID, args);
  389. }
  390. }
  391. }
  392. }
  393. else {
  394. args.sheet.setValue(args.row, args.col, args.row < cacheArr.length ? cacheArr[args.row].code : '');
  395. }
  396. }
  397. }
  398. else {
  399. args.sheet.setValue(args.row, args.col, '');
  400. }
  401. },
  402. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的code不存在,恢复删除的被更新数据
  403. let rst = [];
  404. for (let i = 0, len = tempDelArr.length; i < len; i++) {
  405. let isExist = false;
  406. for (let j = 0, jLen = newArr.length; j < jLen; j++) {
  407. if (tempDelArr[i].newCode == newArr[j].code) {
  408. isExist = true;
  409. break;
  410. }
  411. }
  412. if (!isExist) {
  413. rst.push(tempDelArr[i].org);
  414. }
  415. }
  416. return rst;
  417. },
  418. addGljItems: function (codes, repId, args) {
  419. let me = this;
  420. CommonAjax.post('api/getGljItemsByCodes', { gljCodes: codes, rationRepId: repId }, function (rstData) {
  421. if (rstData.length > 0) {
  422. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  423. let rstArr = [], dummyR = { gljId: 0, consumeAmt: 0 }, newAddArr = [];
  424. for (let i = 0; i < rstData.length; i++) {
  425. dummyR.gljId = rstData[i].ID;
  426. dummyR.type = rstData[i].type;
  427. rstArr.push(me.createRationGljDisplayItem(dummyR, rstData[i]));
  428. }
  429. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  430. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  431. for (var i = 0; i < rstArr.length; i++) {
  432. var hasDup = false;
  433. for (var j = 0; j < cacheArr.length; j++) {
  434. if (cacheArr[j].gljId == rstArr[i].gljId) {
  435. hasDup = true;
  436. break;
  437. }
  438. }
  439. if (!hasDup) {
  440. newAddArr.push(rstArr[i]);
  441. }
  442. }
  443. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  444. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, rstData);
  445. if (recoveryArr.length > 0) {
  446. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  447. }
  448. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  449. let aV = a.gljType + a.code,
  450. bV = b.gljType + b.code;
  451. if (aV > bV) {
  452. return 1;
  453. } else if (aV < bV) {
  454. return -1
  455. }
  456. return 0;
  457. });
  458. }
  459. me.showGljItems(me.currentRationItem.ID);
  460. if (newAddArr.length > 0) {
  461. me.updateRationItem(function () {
  462. me.sheet.getParent().focus(true);
  463. });
  464. }
  465. }
  466. else {
  467. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID] ? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  468. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  469. if (recoveryArr.length > 0) {
  470. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  471. }
  472. //更新的工料机不存在
  473. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  474. let aV = a.gljType + a.code,
  475. bV = b.gljType + b.code;
  476. if (aV > bV) {
  477. return 1;
  478. } else if (aV < bV) {
  479. return -1
  480. }
  481. return 0;
  482. });
  483. $('#alertModalBtn').click();
  484. $('#alertText').text("人材机" + codes + "不存在,请查找你所需要的人材机,或新增人材机");
  485. $('#alertModalCls').click(function () {
  486. me.showGljItems(me.currentRationItem.ID);
  487. });
  488. $('#alertModalCof').click(function () {
  489. me.showGljItems(me.currentRationItem.ID);
  490. })
  491. }
  492. });
  493. },
  494. round(v, e) {
  495. var t = 1;
  496. for (; e > 0; t *= 10, e--);
  497. for (; e < 0; t /= 10, e++);
  498. return Math.round(v * t) / t;
  499. },
  500. rationCal: function () {
  501. let me = rationGLJOprObj;
  502. let rationBasePrc = 0;
  503. if (me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]) {
  504. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  505. cacheArr.forEach(function (gljData) {
  506. if (gljData.gljType && gljData.basePrice && gljData.consumeAmt) {
  507. rationBasePrc += gljData.basePrice * gljData.consumeAmt;
  508. }
  509. });
  510. rationBasePrc = scMathUtil.roundTo(rationBasePrc, 0);
  511. }
  512. return rationBasePrc;
  513. },
  514. updateRationItem: function (callback) {
  515. var me = this, updateArr = [];
  516. if (me.currentRationItem) {
  517. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  518. //recalculate ration basePrice
  519. let price = me.rationCal();
  520. me.currentRationItem.basePrice = price;
  521. updateArr.push(me.currentRationItem);
  522. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  523. if (callback) callback();
  524. });
  525. }
  526. },
  527. buildRationItemGlj: function () {
  528. var me = this, rst = [];
  529. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  530. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  531. for (var i = 0; i < cacheArr.length; i++) {
  532. rst.push({ gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, type: cacheArr[i].type });
  533. }
  534. }
  535. return rst;
  536. },
  537. createRationGljDisplayItem: function (rItem, repGlj) {
  538. var rst = {};
  539. rst.gljId = rItem.gljId;
  540. rst.type = rItem.type;
  541. rst.consumeAmt = rItem.consumeAmt;
  542. rst.code = repGlj.code;
  543. rst.name = repGlj.name;
  544. rst.specs = repGlj.specs;
  545. rst.unit = repGlj.unit;
  546. rst.basePrice = repGlj.basePrice;
  547. rst.gljType = repGlj.gljType;
  548. return rst;
  549. },
  550. getGljItems: function (rationItem, callback) {
  551. let me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [], rationType = rationItem.type;
  552. me.currentRationItem = rationItem;
  553. if (me.cache["_GLJ_" + rationID]) {
  554. me.showGljItems(rationID);
  555. } else {
  556. let gljIds = [];
  557. for (let i = 0; i < rationGljList.length; i++) {
  558. let idObj = Object.create(null);
  559. idObj.type = rationGljList[i].type;
  560. idObj.id = rationGljList[i].gljId;
  561. gljIds.push(idObj);
  562. }
  563. CommonAjax.post('api/getGljItemsByIds', { ids: gljIds }, function (rstData) {
  564. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  565. var cacheArr = [];
  566. for (let i = 0; i < rstData.length; i++) {
  567. for (let j = 0; j < rationGljList.length; j++) {
  568. if (rationGljList[j].gljId == rstData[i].ID) {
  569. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], rstData[i]));
  570. break;
  571. }
  572. }
  573. }
  574. function compare() {
  575. return function (a, b) {
  576. let aV = a.gljType + a.code,
  577. bV = b.gljType + b.code;
  578. if (aV > bV) {
  579. return 1;
  580. } else if (aV < bV) {
  581. return -1
  582. }
  583. return 0;
  584. }
  585. }
  586. cacheArr.sort(compare());
  587. me.cache["_GLJ_" + rationID] = cacheArr;
  588. me.showGljItems(rationID);
  589. if (callback) callback();
  590. });
  591. }
  592. },
  593. showGljItems: function (rationID) {
  594. var me = this;
  595. if (me.cache["_GLJ_" + rationID]) {
  596. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  597. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  598. let aV = a.gljType + a.code,
  599. bV = b.gljType + b.code;
  600. if (aV > bV) {
  601. return 1;
  602. } else if (aV < bV) {
  603. return -1
  604. }
  605. return 0;
  606. });
  607. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  608. }
  609. }
  610. }