ration_glj.js 36 KB

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