ration_glj.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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": {name: "添加工料机", disabled: addDis, icon: "fa-plus", callback: function (key, opt) {
  115. //默认radio所有工料机
  116. gljSelOprObj.initRadio();
  117. gljSelOprObj.gljCurTypeId = null;
  118. gljSelOprObj.initClassTree('std', gljSelOprObj.treeData.std);
  119. //弹出窗口
  120. $('#selGlj').modal('show');
  121. if (gljSelOprObj.workBook) {
  122. gljSelOprObj.workBook.refresh();
  123. }
  124. }},
  125. "delete": {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. //rationCoe表
  137. else if(me.sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){
  138. let currentCache = raCoe.curRation && raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  139. sheet.setActiveCell(target.row, target.col);
  140. //控制按钮是否可用
  141. let upDis = false,
  142. downDis = false,
  143. refDis = false;
  144. if(target.row >= currentCache.length){
  145. upDis = true;
  146. downDis = true;
  147. refDis = true;
  148. }
  149. else {
  150. if(!raCoe.isDef(currentCache[target.row - 1])){
  151. upDis = true;
  152. }
  153. if(!raCoe.isDef(currentCache[target.row + 1])){
  154. downDis = true;
  155. }
  156. }
  157. return {
  158. callback: function(){},
  159. items: {
  160. "upMove": {name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  161. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], {row: target.row - 1, col: target.col});
  162. }},
  163. "downMove": {name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  164. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], {row: target.row + 1, col: target.col});
  165. }},
  166. "ref": {name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  167. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  168. for(let i = 0, len = updateArr.length; i < len; i++){
  169. let ration = updateArr[i];
  170. let rationCoeList = updateArr[i].rationCoeList;
  171. let newNo = 1;
  172. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  173. if(rationCoeList[j].no >= newNo){
  174. newNo = rationCoeList[j].no + 1;
  175. }
  176. }
  177. let theCache = raCoe.cache["_Coe_" + ration.ID];
  178. if(theCache !== undefined && theCache !== null){
  179. let newCoe = {};
  180. for(let attr in currentCache[target.row]){
  181. newCoe[attr] = currentCache[target.row][attr];
  182. }
  183. newCoe.no = newNo;
  184. theCache.push(newCoe);
  185. }
  186. }
  187. });
  188. }}
  189. }
  190. };
  191. }
  192. else{
  193. return false;
  194. }
  195. }
  196. });
  197. },
  198. bindRationGljDelOpr: function () {
  199. let me = rationGLJOprObj, spreadBook = me.sheet.getParent();
  200. spreadBook.commandManager().register('rationGljDelete', function () {
  201. if(!me.currentRationItem){
  202. return;
  203. }
  204. let sels = me.sheet.getSelections(), lockCols = me.setting.view.lockColumns;
  205. let cacheSection = me.cache["_GLJ_" + me.currentRationItem.ID], isUpdate = false;
  206. if(sels.length > 0){
  207. for(let sel = 0; sel < sels.length; sel++){
  208. if(sels[sel].colCount === me.setting.header.length){
  209. if(cacheSection && sels[sel].row < cacheSection.length){
  210. isUpdate = true;
  211. cacheSection.splice(sels[sel].row, sels[sel].rowCount);
  212. }
  213. }
  214. else{
  215. if(sels[sel].col !== 0 && sels[sel].col !== 5 && !(sels[sel].col === 1 && sels.col + sels[sel].colCount -1 === 3)){
  216. if(cacheSection){
  217. for(let i = sels[sel].row === -1 ? 1 : 0; i < sels[sel].rowCount; i++){
  218. if(sels[sel].row + i < cacheSection.length){
  219. for(let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++){
  220. if(lockCols.indexOf(col) === -1){
  221. isUpdate = true;
  222. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = 0;
  223. me.sheet.setValue(sels[sel].row + i, col, 0.00);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. if(isUpdate){
  234. me.updateRationItem(function () {
  235. me.sheet.getParent().focus(true);
  236. });
  237. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  238. me.showGljItems(me.currentRationItem.ID);
  239. }
  240. });
  241. spreadBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  242. spreadBook.commandManager().setShortcutKey('rationGljDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  243. },
  244. onClipboardPasting: function(sender, args) {
  245. var me = rationGLJOprObj;
  246. let rationSection = rationOprObj.getCache();
  247. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  248. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  249. if(me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined'){
  250. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  251. }
  252. if (!(args.cellRange.col === 0 || args.cellRange.col === 5) || !(me.currentRationItem)) {
  253. args.cancel = true;
  254. }
  255. },
  256. onClipboardPasted: function(e, info) {
  257. var me = rationGLJOprObj, repId = pageOprObj.rationLibId;
  258. me.tempCacheArr = [];
  259. if (repId) {
  260. let gljLibId = pageOprObj.gljLibId;
  261. console.log(gljLibId);
  262. if(gljLibId){
  263. if (info.cellRange.col == 0) {
  264. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  265. var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
  266. var codes = [];
  267. for (var i = 0; i < tmpCodes.length; i++) {
  268. let rowIdx = info.cellRange.row + i;
  269. if(rowIdx < cacheArr.length){//更新
  270. me.tempCacheArr.push({org: cacheArr[rowIdx], newCode: tmpCodes[i].code});
  271. cacheArr.splice(rowIdx--, 1);
  272. }
  273. codes.push(tmpCodes[i].code);
  274. }
  275. me.addGljItems(codes, gljLibId, info.cellRange);
  276. } else {
  277. //修改用量
  278. if(me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length){
  279. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  280. let maxCount = info.cellRange.row + info.cellRange.rowCount -1 > me.cache["_GLJ_" + me.currentRationItem.ID].length -1 ?
  281. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  282. for(let i = 0; i < maxCount; i++){
  283. let roundCons = scMathUtil.roundTo(tempConsumes[i].consumeAmt, -3);
  284. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  285. }
  286. me.updateRationItem(function () {
  287. me.sheet.getParent().focus(true);
  288. });
  289. if(info.cellRange.row + info.cellRange.rowCount -1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length -1){
  290. me.sheet.suspendPaint();
  291. for(let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx++){
  292. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  293. }
  294. me.sheet.resumePaint();
  295. }
  296. }
  297. else if(info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length){
  298. me.sheet.suspendPaint();
  299. for(let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx ++){
  300. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  301. }
  302. me.sheet.resumePaint();
  303. }
  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('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. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, type: cacheArr[i].type});
  527. }
  528. }
  529. return rst;
  530. },
  531. createRationGljDisplayItem: function(rItem, repGlj) {
  532. var rst = {};
  533. rst.gljId = rItem.gljId;
  534. rst.type = rItem.type;
  535. rst.consumeAmt = rItem.consumeAmt;
  536. rst.code = repGlj.code;
  537. rst.name = repGlj.name;
  538. rst.specs = repGlj.specs;
  539. rst.unit = repGlj.unit;
  540. rst.basePrice = repGlj.basePrice;
  541. rst.gljType = repGlj.gljType;
  542. return rst;
  543. },
  544. getGljItems: function(rationItem, callback) {
  545. let me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [], rationType = rationItem.type;
  546. me.currentRationItem = rationItem;
  547. if (me.cache["_GLJ_" + rationID]) {
  548. me.showGljItems(rationID);
  549. } else {
  550. let gljIds = [];
  551. for (let i = 0; i < rationGljList.length; i++) {
  552. let idObj = Object.create(null);
  553. idObj.type = rationGljList[i].type;
  554. idObj.id = rationGljList[i].gljId;
  555. gljIds.push(idObj);
  556. }
  557. CommonAjax.post('api/getGljItemsByIds', {ids: gljIds}, function (rstData) {
  558. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  559. var cacheArr = [];
  560. for (let i = 0; i < rstData.length; i++) {
  561. for (let j = 0; j < rationGljList.length; j++) {
  562. if (rationGljList[j].gljId == rstData[i].ID) {
  563. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], rstData[i]));
  564. break;
  565. }
  566. }
  567. }
  568. function compare(){
  569. return function (a, b) {
  570. let aV = a.gljType + a.code,
  571. bV = b.gljType + b.code;
  572. if (aV > bV) {
  573. return 1;
  574. } else if (aV < bV) {
  575. return -1
  576. }
  577. return 0;
  578. }
  579. }
  580. cacheArr.sort(compare());
  581. me.cache["_GLJ_" + rationID] = cacheArr;
  582. me.showGljItems(rationID);
  583. if(callback) callback();
  584. });
  585. }
  586. },
  587. showGljItems: function(rationID) {
  588. var me = this;
  589. if (me.cache["_GLJ_" + rationID]) {
  590. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  591. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  592. let aV = a.gljType + a.code,
  593. bV = b.gljType + b.code;
  594. if (aV > bV) {
  595. return 1;
  596. } else if (aV < bV) {
  597. return -1
  598. }
  599. return 0;
  600. });
  601. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  602. }
  603. }
  604. }