ration_glj.js 29 KB

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