ration_glj.js 32 KB

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