ration_glj.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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.curRation && 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. if(priceProperties && priceProperties.length > 0){
  443. let priceField = priceProperties[0].price.dataCode;
  444. for(let glj of result.data){
  445. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  446. }
  447. }
  448. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  449. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  450. for (var i = 0; i < result.data.length; i++) {
  451. dummyR.gljId = result.data[i].ID;
  452. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  453. }
  454. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  455. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  456. for (var i = 0; i < rstArr.length; i++) {
  457. var hasDup = false;
  458. for (var j = 0; j < cacheArr.length; j++) {
  459. if (cacheArr[j].gljId == rstArr[i].gljId) {
  460. hasDup = true;
  461. break;
  462. }
  463. }
  464. if (!hasDup) {
  465. newAddArr.push(rstArr[i]);
  466. }
  467. }
  468. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  469. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, result.data);
  470. if(recoveryArr.length > 0){
  471. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  472. }
  473. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  474. let rst = 0;
  475. let aV = a.gljType + a.code,
  476. bV = b.gljType + b.code;
  477. if (aV > bV) {
  478. rst = 1;
  479. } else if (aV < bV) {
  480. rst = -1;
  481. }
  482. return rst;
  483. });
  484. }
  485. me.showGljItems(me.currentRationItem.ID);
  486. if (newAddArr.length > 0) {
  487. me.updateRationItem(function () {
  488. me.sheet.getParent().focus(true);
  489. });
  490. }
  491. }
  492. else{
  493. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID]? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  494. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  495. if(recoveryArr.length > 0){
  496. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  497. }
  498. //更新的工料机不存在
  499. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  500. let rst = 0;
  501. let aV = a.gljType + a.code,
  502. bV = b.gljType + b.code;
  503. if (aV > bV) rst = 1
  504. else if (aV < bV) rst = -1;
  505. return rst;
  506. });
  507. $('#alertModalBtn').click();
  508. $('#alertText').text("人材机"+ codes + "不存在,请查找你所需要的人材机,或新增人材机");
  509. $('#alertModalCls').click(function () {
  510. me.showGljItems(me.currentRationItem.ID);
  511. });
  512. $('#alertModalCof').click(function () {
  513. me.showGljItems(me.currentRationItem.ID);
  514. })
  515. }
  516. }
  517. },
  518. error:function(err){
  519. alert(err);
  520. }
  521. })
  522. },
  523. round(v, e){
  524. var t=1;
  525. for(;e>0;t*=10,e--);
  526. for(;e<0;t/=10,e++);
  527. return Math.round(v*t)/t;
  528. },
  529. rationCal: function () {
  530. let me = rationGLJOprObj;
  531. let price = {gljType1: [], gljType2: [], gljType3: [], gljType6: [], gljType7: [], gljType8: []},
  532. rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0, managePrice: 0, profitPrice: 0, riskPrice: 0},
  533. rationBasePrc = 0;
  534. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  535. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  536. cacheArr.forEach(function (gljData) {
  537. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  538. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  539. if(parent && parent.data.ID <= 3){
  540. price['gljType' + parent.data.ID].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  541. }
  542. if(!parent && gljData.gljType <= 3){
  543. price['gljType' + gljData.gljType].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  544. }
  545. if([6, 7, 8].includes(gljData.gljType)){
  546. price['gljType' + gljData.gljType].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  547. }
  548. }
  549. });
  550. if(price.gljType1.length > 0){
  551. let labourPrice = 0;
  552. price.gljType1.forEach(function (singlePrc) {
  553. labourPrice = scMathUtil.roundTo(labourPrice + singlePrc, me.processDecimal);
  554. });
  555. let roundPrice = scMathUtil.roundTo(labourPrice, -2);
  556. rst.labourPrice = roundPrice;
  557. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  558. }
  559. if(price.gljType2.length > 0){
  560. let materialPrice = 0;
  561. price.gljType2.forEach(function (singlePrc) {
  562. materialPrice = scMathUtil.roundTo(materialPrice + singlePrc, me.processDecimal);
  563. });
  564. let roundPrice = scMathUtil.roundTo(materialPrice, -2);
  565. rst.materialPrice = roundPrice;
  566. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  567. }
  568. if(price.gljType3.length > 0){
  569. let machinePrice = 0;
  570. price.gljType3.forEach(function (singlePrc) {
  571. machinePrice = scMathUtil.roundTo(machinePrice + singlePrc, me.processDecimal);
  572. });
  573. let roundPrice = scMathUtil.roundTo(machinePrice, -2);
  574. rst.machinePrice = roundPrice;
  575. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  576. }
  577. if(price.gljType6.length > 0){
  578. let managePrice = 0;
  579. price.gljType6.forEach(function (singlePrc) {
  580. managePrice = scMathUtil.roundTo(managePrice + singlePrc, me.processDecimal);
  581. });
  582. let roundPrice = scMathUtil.roundTo(managePrice, -2);
  583. rst.managePrice = roundPrice;
  584. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  585. }
  586. if(price.gljType7.length > 0){
  587. let profitPrice = 0;
  588. price.gljType7.forEach(function (singlePrc) {
  589. profitPrice = scMathUtil.roundTo(profitPrice + singlePrc, me.processDecimal);
  590. });
  591. let roundPrice = scMathUtil.roundTo(profitPrice, -2);
  592. rst.profitPrice = roundPrice;
  593. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  594. }
  595. if(price.gljType8.length > 0){
  596. let riskPrice = 0;
  597. price.gljType8.forEach(function (singlePrc) {
  598. riskPrice = scMathUtil.roundTo(riskPrice + singlePrc, me.processDecimal);
  599. });
  600. let roundPrice = scMathUtil.roundTo(riskPrice, -2);
  601. rst.riskPrice = roundPrice;
  602. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  603. }
  604. rst.rationBasePrc = rationBasePrc;
  605. }
  606. return rst;
  607. },
  608. updateRationItem: function(callback) {
  609. var me = this, updateArr = [];
  610. if (me.currentRationItem) {
  611. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  612. //recalculate ration basePrice
  613. let price = me.rationCal();
  614. me.currentRationItem.labourPrice = price.labourPrice;
  615. me.currentRationItem.materialPrice = price.materialPrice;
  616. me.currentRationItem.machinePrice = price.machinePrice;
  617. me.currentRationItem.basePrice = price.rationBasePrc;
  618. updateArr.push(me.currentRationItem);
  619. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  620. if(callback) callback();
  621. });
  622. }
  623. },
  624. buildRationItemGlj: function(){
  625. var me = this, rst = [];
  626. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  627. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  628. for (var i = 0; i < cacheArr.length; i++) {
  629. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  630. }
  631. }
  632. return rst;
  633. },
  634. createRationGljDisplayItem: function(rItem, repGlj) {
  635. var rst = {};
  636. rst.gljId = rItem.gljId;
  637. rst.consumeAmt = rItem.consumeAmt;
  638. rst.code = repGlj.code;
  639. rst.name = repGlj.name;
  640. rst.specs = repGlj.specs;
  641. rst.unit = repGlj.unit;
  642. rst.basePrice = repGlj.basePrice;
  643. rst.gljType = repGlj.gljType;
  644. return rst;
  645. },
  646. getGljItems: function(rationItem, callback) {
  647. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [];
  648. me.currentRationItem = rationItem;
  649. if (me.cache["_GLJ_" + rationID]) {
  650. me.showGljItems(rationID);
  651. } else {
  652. var gljIds = [];
  653. for (var i = 0; i < rationGljList.length; i++) {
  654. gljIds.push(rationGljList[i].gljId);
  655. }
  656. $.ajax({
  657. type:"POST",
  658. url:"api/getGljItemsByIds",
  659. data:{"gljIds": JSON.stringify(gljIds)},
  660. dataType:"json",
  661. cache:false,
  662. timeout:5000,
  663. success:function(result){
  664. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  665. if (result) {
  666. if(priceProperties && priceProperties.length > 0){
  667. let priceField = priceProperties[0].price.dataCode;
  668. for(let glj of result.data){
  669. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  670. }
  671. }
  672. var cacheArr = [];
  673. for (var i = 0; i < result.data.length; i++) {
  674. for (var j = 0; j < rationGljList.length; j++) {
  675. if (rationGljList[j].gljId == result.data[i].ID) {
  676. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  677. break;
  678. }
  679. }
  680. }
  681. function compare(){
  682. return function (a, b) {
  683. let rst = 0;
  684. let aV = a.gljType + a.code,
  685. bV = b.gljType + b.code;
  686. if (aV > bV) {
  687. rst = -1;
  688. } else if (aV < bV) {
  689. rst = 1;
  690. }
  691. return rst;
  692. }
  693. }
  694. cacheArr.sort(compare());
  695. me.cache["_GLJ_" + rationID] = cacheArr;
  696. me.showGljItems(rationID);
  697. }
  698. if(callback) callback();
  699. },
  700. error:function(err){
  701. alert(err);
  702. }
  703. })
  704. }
  705. },
  706. showGljItems: function(rationID) {
  707. var me = this;
  708. if (me.cache["_GLJ_" + rationID]) {
  709. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  710. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  711. let rst = 0;
  712. let aV = a.gljType + a.code,
  713. bV = b.gljType + b.code;
  714. if(aV > bV){
  715. rst = 1;
  716. }
  717. else if(aV < bV){
  718. rst = -1;
  719. }
  720. return rst;
  721. });
  722. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  723. }
  724. }
  725. }