ration_glj.js 32 KB

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