ration_glj.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. let raCoe = rationCoeOprObj;
  292. $.contextMenu({
  293. selector: '#rdSpread',
  294. build: function($triggerElement, e){
  295. //控制允许右键菜单在哪个位置出现
  296. let target = SheetDataHelper.safeRightClickSelection($triggerElement, e, me.sheet.getParent());
  297. let sheet = me.sheet;
  298. let addDis = false, delDis = false;
  299. let rationGlj = [];
  300. if(me.sheet.getParent().getActiveSheetIndex() === 0 && target.hitTestType === 3){//在表格内&& typeof target.row !== 'undefined' && typeof target.col !== 'undefined'
  301. //rationGlj表
  302. if(typeof target.row !== 'undefined'){
  303. //控制按钮是否可用
  304. sheet.setActiveCell(target.row, target.col);
  305. if(me.currentRationItem){
  306. rationGlj = me.cache['_GLJ_' + me.currentRationItem.ID];
  307. if(!rationGlj ||target.row >= rationGlj.length){//右键定位在有数据的行,删除键才显示可用
  308. delDis = true;
  309. }
  310. else{//有数据
  311. if(typeof target.col === 'undefined'){//定位不在表格内
  312. delDis = true;
  313. }
  314. }
  315. }
  316. else{
  317. addDis = true;
  318. delDis = true;
  319. }
  320. }
  321. else{
  322. addDis = true;
  323. delDis = true;
  324. }
  325. return {
  326. callback: function(){},
  327. items: {
  328. "add": {name: "添加人材机", disabled: addDis, icon: "fa-plus", callback: function (key, opt) {
  329. //默认radio所有工料机
  330. gljSelOprObj.initRadio();
  331. gljSelOprObj.gljCurTypeId = null;
  332. //默认点击树根节点
  333. if(gljSelOprObj.rootNode){
  334. gljSelOprObj.treeObj.selectNode(gljSelOprObj.rootNode);
  335. gljSelTreeOprObj.setting.callback.onClick(null, 'componentTree', gljSelOprObj.rootNode);
  336. }
  337. //弹出窗口
  338. $('#selGlj').modal('show');
  339. }},
  340. "delete": {name: "删除人材机", disabled: delDis, icon: "fa-remove", callback: function (key, opt) {
  341. rationGlj.splice(target.row, 1);
  342. me.updateRationItem(function(){
  343. me.sheet.getParent().focus();
  344. });
  345. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  346. me.showGljItems(me.currentRationItem.ID);
  347. }},
  348. }
  349. };
  350. }
  351. //rationCoe表
  352. else if(me.sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){
  353. let currentCache = raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  354. sheet.setActiveCell(target.row, target.col);
  355. //控制按钮是否可用
  356. let upDis = false,
  357. downDis = false,
  358. refDis = false;
  359. if(target.row >= currentCache.length){
  360. upDis = true;
  361. downDis = true;
  362. refDis = true;
  363. }
  364. else {
  365. if(!raCoe.isDef(currentCache[target.row - 1])){
  366. upDis = true;
  367. }
  368. if(!raCoe.isDef(currentCache[target.row + 1])){
  369. downDis = true;
  370. }
  371. }
  372. return {
  373. callback: function(){},
  374. items: {
  375. "upMove": {name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  376. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], {row: target.row - 1, col: target.col});
  377. }},
  378. "downMove": {name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  379. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], {row: target.row + 1, col: target.col});
  380. }},
  381. "ref": {name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  382. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  383. for(let i = 0, len = updateArr.length; i < len; i++){
  384. let ration = updateArr[i];
  385. let rationCoeList = updateArr[i].rationCoeList;
  386. let newNo = 1;
  387. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  388. if(rationCoeList[j].no >= newNo){
  389. newNo = rationCoeList[j].no + 1;
  390. }
  391. }
  392. let theCache = raCoe.cache["_Coe_" + ration.ID];
  393. if(theCache !== undefined && theCache !== null){
  394. let newCoe = {};
  395. for(let attr in currentCache[target.row]){
  396. newCoe[attr] = currentCache[target.row][attr];
  397. }
  398. newCoe.no = newNo;
  399. theCache.push(newCoe);
  400. }
  401. }
  402. });
  403. }}
  404. }
  405. };
  406. }
  407. else{
  408. return false;
  409. }
  410. }
  411. });
  412. },
  413. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的code不存在,恢复删除的被更新数据
  414. let rst = [];
  415. for(let i = 0, len = tempDelArr.length; i < len; i++){
  416. let isExist = false;
  417. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  418. if(tempDelArr[i].newCode == newArr[j].code){
  419. isExist = true;
  420. break;
  421. }
  422. }
  423. if(!isExist){
  424. rst.push(tempDelArr[i].org);
  425. }
  426. }
  427. return rst;
  428. },
  429. addGljItems: function(codes, repId, args) {
  430. var me = this;
  431. $.ajax({
  432. type:"POST",
  433. url:"api/getGljItemsByCodes",
  434. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  435. dataType:"json",
  436. cache:false,
  437. timeout:5000,
  438. success:function(result){
  439. if (result) {
  440. if(result.data.length > 0){
  441. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  442. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  443. for (var i = 0; i < result.data.length; i++) {
  444. dummyR.gljId = result.data[i].ID;
  445. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  446. }
  447. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  448. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  449. for (var i = 0; i < rstArr.length; i++) {
  450. var hasDup = false;
  451. for (var j = 0; j < cacheArr.length; j++) {
  452. if (cacheArr[j].gljId == rstArr[i].gljId) {
  453. hasDup = true;
  454. break;
  455. }
  456. }
  457. if (!hasDup) {
  458. newAddArr.push(rstArr[i]);
  459. }
  460. }
  461. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  462. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, result.data);
  463. if(recoveryArr.length > 0){
  464. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  465. }
  466. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  467. var rst = 0;
  468. if (a.code > b.code) rst = 1
  469. else if (a.code < b.code) rst = -1;
  470. return rst;
  471. });
  472. }
  473. me.showGljItems(me.currentRationItem.ID);
  474. if (newAddArr.length > 0) {
  475. me.updateRationItem(function () {
  476. me.sheet.getParent().focus(true);
  477. });
  478. }
  479. }
  480. else{
  481. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID]? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  482. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  483. if(recoveryArr.length > 0){
  484. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  485. }
  486. //更新的工料机不存在
  487. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  488. var rst = 0;
  489. if (a.code > b.code) rst = 1
  490. else if (a.code < b.code) rst = -1;
  491. return rst;
  492. });
  493. $('#alertModalBtn').click();
  494. $('#alertText').text("人材机"+ codes + "不存在,请查找你所需要的人材机,或新增人材机");
  495. $('#alertModalCls').click(function () {
  496. me.showGljItems(me.currentRationItem.ID);
  497. });
  498. $('#alertModalCof').click(function () {
  499. me.showGljItems(me.currentRationItem.ID);
  500. })
  501. }
  502. }
  503. },
  504. error:function(err){
  505. alert(err);
  506. }
  507. })
  508. },
  509. round(v, e){
  510. var t=1;
  511. for(;e>0;t*=10,e--);
  512. for(;e<0;t/=10,e++);
  513. return Math.round(v*t)/t;
  514. },
  515. rationCal: function () {
  516. let me = rationGLJOprObj;
  517. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  518. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  519. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  520. cacheArr.forEach(function (gljData) {
  521. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  522. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  523. if(parent && parent.data.ID <= 3){
  524. price['gljType' + parent.data.ID].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  525. }
  526. if(!parent && gljData.gljType <= 3){
  527. price['gljType' + gljData.gljType].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  528. }
  529. }
  530. });
  531. if(price.gljType1.length > 0){
  532. let labourPrice = 0;
  533. price.gljType1.forEach(function (singlePrc) {
  534. labourPrice = scMathUtil.roundTo(labourPrice + singlePrc, me.processDecimal);
  535. });
  536. let roundPrice = scMathUtil.roundTo(labourPrice, -2);
  537. rst.labourPrice = roundPrice;
  538. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  539. }
  540. if(price.gljType2.length > 0){
  541. let materialPrice = 0;
  542. price.gljType2.forEach(function (singlePrc) {
  543. materialPrice = scMathUtil.roundTo(materialPrice + singlePrc, me.processDecimal);
  544. });
  545. let roundPrice = scMathUtil.roundTo(materialPrice, -2);
  546. rst.materialPrice = roundPrice;
  547. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  548. }
  549. if(price.gljType3.length > 0){
  550. let machinePrice = 0;
  551. price.gljType3.forEach(function (singlePrc) {
  552. machinePrice = scMathUtil.roundTo(machinePrice + singlePrc, me.processDecimal);
  553. });
  554. let roundPrice = scMathUtil.roundTo(machinePrice, -2);
  555. rst.machinePrice = roundPrice;
  556. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  557. }
  558. rst.rationBasePrc = rationBasePrc;
  559. }
  560. return rst;
  561. },
  562. updateRationItem: function(callback) {
  563. var me = this, updateArr = [];
  564. if (me.currentRationItem) {
  565. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  566. //recalculate ration basePrice
  567. let price = me.rationCal();
  568. me.currentRationItem.labourPrice = price.labourPrice;
  569. me.currentRationItem.materialPrice = price.materialPrice;
  570. me.currentRationItem.machinePrice = price.machinePrice;
  571. me.currentRationItem.basePrice = price.rationBasePrc;
  572. updateArr.push(me.currentRationItem);
  573. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  574. if(callback) callback();
  575. });
  576. }
  577. },
  578. buildRationItemGlj: function(){
  579. var me = this, rst = [];
  580. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  581. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  582. for (var i = 0; i < cacheArr.length; i++) {
  583. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  584. }
  585. }
  586. return rst;
  587. },
  588. createRationGljDisplayItem: function(rItem, repGlj) {
  589. var rst = {};
  590. rst.gljId = rItem.gljId;
  591. rst.consumeAmt = rItem.consumeAmt;
  592. rst.code = repGlj.code;
  593. rst.name = repGlj.name;
  594. rst.specs = repGlj.specs;
  595. rst.unit = repGlj.unit;
  596. rst.basePrice = repGlj.basePrice;
  597. rst.gljType = repGlj.gljType;
  598. return rst;
  599. },
  600. getGljItems: function(rationItem, callback) {
  601. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  602. me.currentRationItem = rationItem;
  603. if (me.cache["_GLJ_" + rationID]) {
  604. me.showGljItems(rationID);
  605. } else {
  606. var gljIds = [];
  607. for (var i = 0; i < rationGljList.length; i++) {
  608. gljIds.push(rationGljList[i].gljId);
  609. }
  610. $.ajax({
  611. type:"POST",
  612. url:"api/getGljItemsByIds",
  613. data:{"gljIds": JSON.stringify(gljIds)},
  614. dataType:"json",
  615. cache:false,
  616. timeout:5000,
  617. success:function(result){
  618. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  619. if (result) {
  620. var cacheArr = [];
  621. for (var i = 0; i < result.data.length; i++) {
  622. for (var j = 0; j < rationGljList.length; j++) {
  623. if (rationGljList[j].gljId == result.data[i].ID) {
  624. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  625. break;
  626. }
  627. }
  628. }
  629. function compare(){
  630. return function (a, b) {
  631. let rst = 0;
  632. if (a.code > b.code) {
  633. rst = 1;
  634. }
  635. else if (a.code < b.code) {
  636. rst = -1;
  637. }
  638. return rst;
  639. }
  640. }
  641. cacheArr.sort(compare());
  642. me.cache["_GLJ_" + rationID] = cacheArr;
  643. me.showGljItems(rationID);
  644. }
  645. if(callback) callback();
  646. },
  647. error:function(err){
  648. alert(err);
  649. }
  650. })
  651. }
  652. },
  653. showGljItems: function(rationID) {
  654. var me = this;
  655. if (me.cache["_GLJ_" + rationID]) {
  656. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  657. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  658. let rst = 0;
  659. if(a.code > b.code){
  660. rst = 1;
  661. }
  662. else if(a.code < b.code){
  663. rst = -1;
  664. }
  665. return rst;
  666. });
  667. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  668. }
  669. }
  670. }