ration_glj.js 28 KB

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