ration_glj.js 32 KB

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