ration_glj.js 29 KB

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