ration_glj.js 32 KB

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