ration_glj.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 distType;
  29. let distTypeTree = {
  30. prefix : 'gljDistType',
  31. distTypes: {},
  32. comboDatas: [],
  33. distTypesArr: []
  34. };
  35. gljDistType.forEach(function (typeData) {
  36. let typeObj = {
  37. data: typeData,
  38. children: [],
  39. parent: null
  40. }
  41. distTypeTree.distTypes[distTypeTree.prefix + typeData.ID] = typeObj;
  42. distTypeTree.distTypesArr.push(typeObj);
  43. });
  44. gljDistType.forEach(function (typeData) {
  45. distType = distTypeTree.distTypes[distTypeTree.prefix + typeData.ID];
  46. let parent = distTypeTree.distTypes[distTypeTree.prefix + typeData.ParentID];
  47. if(parent){
  48. distType.parent = parent;
  49. parent.children.push(distType);
  50. }
  51. });
  52. distTypeTree.distTypesArr.forEach(function (distTypeObj) {
  53. if(distTypeObj.children.length === 0 && distTypeObj.data.fullName !== '普通机械' &&distTypeObj.data.fullName !== '机械组成物'
  54. && distTypeObj.data.fullName !== '机上人工'){
  55. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  56. }
  57. if(distTypeObj.data.fullName === '机械'){
  58. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  59. }
  60. });
  61. return distTypeTree;
  62. },
  63. initGljDistType: function (gljDistTypeList) {
  64. this.distTypeTree = this.getDistTypeTree(gljDistTypeList);
  65. },
  66. buildSheet: function(sheet) {
  67. this.sheet = sheet;
  68. sheetCommonObj.initSheet(this.sheet, this.setting, 30);
  69. this.onContextmenuOpr();
  70. this.bindRationGljDelOpr();
  71. this.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, this.onClipboardPasting);
  72. this.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, this.onClipboardPasted);
  73. this.sheet.bind(GC.Spread.Sheets.Events.EditStarting, this.onEditStarting);
  74. this.sheet.bind(GC.Spread.Sheets.Events.EditEnded, this.onCellEditEnd);
  75. },
  76. bindRationGljDelOpr: function () {
  77. if (locked) {
  78. return;
  79. }
  80. let me = rationGLJOprObj, spreadBook = me.sheet.getParent();
  81. spreadBook.commandManager().register('rationGljDelete', function () {
  82. let sels = me.sheet.getSelections(), lockCols = me.setting.view.lockColumns;
  83. let cacheSection = me.cache["_GLJ_" + me.currentRationItem.ID], isUpdate = false;
  84. if(sels.length > 0){
  85. for(let sel = 0; sel < sels.length; sel++){
  86. if(sels[sel].colCount === me.setting.header.length){
  87. if(cacheSection && sels[sel].row < cacheSection.length){
  88. isUpdate = true;
  89. cacheSection.splice(sels[sel].row, sels[sel].rowCount);
  90. }
  91. }
  92. else{
  93. if(sels[sel].col !== 0 && sels[sel].col !== 5 && !(sels[sel].col === 1 && sels.col + sels[sel].colCount -1 === 3)){
  94. if(cacheSection){
  95. for(let i = sels[sel].row === -1 ? 1 : 0; i < sels[sel].rowCount; i++){
  96. if(sels[sel].row + i < cacheSection.length){
  97. for(let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++){
  98. if(lockCols.indexOf(col) === -1){
  99. isUpdate = true;
  100. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = 0;
  101. me.sheet.setValue(sels[sel].row + i, col, 0.00);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. if(isUpdate){
  112. me.updateRationItem(function () {
  113. me.sheet.getParent().focus(true);
  114. });
  115. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  116. me.showGljItems(me.currentRationItem.ID);
  117. }
  118. });
  119. spreadBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  120. spreadBook.commandManager().setShortcutKey('rationGljDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  121. },
  122. onClipboardPasting: function(sender, args) {
  123. var me = rationGLJOprObj;
  124. let rationSection = rationOprObj.getCache();
  125. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  126. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  127. if(me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined'){
  128. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  129. }
  130. if (!(args.cellRange.col === 0 || args.cellRange.col === 5) || !(me.currentRationItem)) {
  131. args.cancel = true;
  132. }
  133. },
  134. onClipboardPasted: function(e, info) {
  135. var me = rationGLJOprObj, repId = pageOprObj.rationLibId;
  136. me.tempCacheArr = [];
  137. if (repId) {
  138. let gljLibId = pageOprObj.gljLibId;
  139. if(gljLibId){
  140. if (info.cellRange.col == 0) {
  141. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  142. var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
  143. var codes = [];
  144. for (var i = 0; i < tmpCodes.length; i++) {
  145. let rowIdx = info.cellRange.row + i;
  146. if(rowIdx < cacheArr.length){//更新
  147. me.tempCacheArr.push({org: cacheArr[rowIdx], newCode: tmpCodes[i].code});
  148. cacheArr.splice(rowIdx--, 1);
  149. }
  150. codes.push(tmpCodes[i].code);
  151. }
  152. me.addGljItems(codes, gljLibId, info.cellRange);
  153. } else {
  154. //修改用量
  155. if(me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length){
  156. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  157. let maxCount = info.cellRange.row + info.cellRange.rowCount -1 > me.cache["_GLJ_" + me.currentRationItem.ID].length -1 ?
  158. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  159. for(let i = 0; i < maxCount; i++){
  160. let roundCons = scMathUtil.roundTo(tempConsumes[i].consumeAmt, -3);
  161. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  162. }
  163. me.updateRationItem(function () {
  164. me.sheet.getParent().focus(true);
  165. });
  166. if(info.cellRange.row + info.cellRange.rowCount -1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length -1){
  167. me.sheet.suspendPaint();
  168. for(let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx++){
  169. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  170. }
  171. me.sheet.resumePaint();
  172. }
  173. }
  174. else if(info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length){
  175. me.sheet.suspendPaint();
  176. for(let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx ++){
  177. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  178. }
  179. me.sheet.resumePaint();
  180. }
  181. }
  182. }
  183. }
  184. },
  185. onEditStarting: function (sender, args) {
  186. let me = rationGLJOprObj;
  187. let rationSection = rationOprObj.getCache();
  188. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  189. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  190. if(me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined'){
  191. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  192. }
  193. if(!me.currentRationItem){
  194. args.cancel = true;
  195. }
  196. else {
  197. if(args.col !== 0 && args.col !== 5 || args.col === 5 && args.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length){
  198. args.cancel = true;
  199. }
  200. }
  201. },
  202. onCellEditEnd: function(sender, args){
  203. var me = rationGLJOprObj;
  204. if(me.currentRationItem) {
  205. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  206. me.tempCacheArr = [];
  207. if (args.col != 0) {
  208. if (args.row < cacheArr.length) {
  209. var editGlj = cacheArr[args.row];
  210. if (editGlj["consumeAmt"] != args.editingText) {
  211. let parseNum = parseFloat(args.editingText);
  212. if (isNaN(parseFloat(args.editingText))) {
  213. $('#alertModalBtn').click();
  214. $('#alertText').text("定额消耗只能输入数值!");
  215. $('#alertModalCls').click(function () {
  216. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  217. });
  218. $('#alertModalCof').click(function () {
  219. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  220. })
  221. }
  222. else {
  223. args.sheet.setValue(args.row, args.col, parseNum);
  224. let roundNum = scMathUtil.roundTo(parseNum, -3);
  225. editGlj["consumeAmt"] = roundNum;
  226. me.updateRationItem(function () {
  227. me.sheet.getParent().focus(true);
  228. });
  229. }
  230. }
  231. }
  232. } else {
  233. if (args.editingText && args.editingText.toString().trim().length !== 0) {
  234. let isExist = false;
  235. for (let i = 0, len = cacheArr.length; i < len; i++) {
  236. if (cacheArr[i].code === args.editingText && i !== args.row) {
  237. isExist = true;
  238. break;
  239. }
  240. }
  241. if (isExist) {
  242. alert("该人材机已存在!");
  243. args.sheet.setValue(args.row, args.col, typeof cacheArr[args.row] !== 'undefined' ? cacheArr[args.row].code + '' : '');
  244. }
  245. else {
  246. if (args.row < cacheArr.length && args.editingText !== cacheArr[args.row].code) {//更新
  247. me.tempCacheArr.push({org: cacheArr[args.row], newCode: args.editingText.toString().trim()});
  248. cacheArr.splice(args.row, 1);
  249. let gljLibID = pageOprObj.gljLibId;
  250. let codes = [];
  251. codes.push(args.editingText.toString().trim());
  252. me.addGljItems(codes, gljLibID, args);
  253. }
  254. else if (args.row >= cacheArr.length) {//新增
  255. let gljLibID = pageOprObj.gljLibId;
  256. if (gljLibID) {
  257. var codes = [];
  258. codes.push(args.editingText.toString().trim());
  259. me.addGljItems(codes, gljLibID, args);
  260. }
  261. }
  262. }
  263. }
  264. else {
  265. args.sheet.setValue(args.row, args.col, args.row < cacheArr.length ? cacheArr[args.row].code : '');
  266. }
  267. }
  268. }
  269. else {
  270. args.sheet.setValue(args.row, args.col, '');
  271. }
  272. },
  273. onContextmenuOpr: function () {//右键菜单
  274. let me = this;
  275. let raCoe = rationCoeOprObj;
  276. $.contextMenu({
  277. selector: '#rdSpread',
  278. build: function($triggerElement, e){
  279. //控制允许右键菜单在哪个位置出现
  280. let target = SheetDataHelper.safeRightClickSelection($triggerElement, e, me.sheet.getParent());
  281. let sheet = me.sheet;
  282. if(me.sheet.getParent().getActiveSheetIndex() === 0 && target.hitTestType === 3){//在表格内&& typeof target.row !== 'undefined' && typeof target.col !== 'undefined'
  283. //rationGlj表
  284. if(typeof target.row !== 'undefined'){
  285. //控制按钮是否可用
  286. sheet.setActiveCell(target.row, target.col);
  287. }
  288. return {
  289. callback: function(){},
  290. items: {
  291. "add": {
  292. name: "添加人材机",
  293. disabled: function () {
  294. const inValidCell = !commonUtil.isDef(target.row) || !commonUtil.isDef(target.col);
  295. if (locked || inValidCell || !me.currentRationItem) {
  296. return true;
  297. }
  298. return false;
  299. },
  300. icon: "fa-plus",
  301. callback: function (key, opt) {
  302. //默认radio所有工料机
  303. gljSelOprObj.initRadio();
  304. gljSelOprObj.gljCurTypeId = null;
  305. //默认点击树根节点
  306. if(gljSelOprObj.rootNode){
  307. gljSelOprObj.treeObj.selectNode(gljSelOprObj.rootNode);
  308. gljSelTreeOprObj.setting.callback.onClick(null, 'componentTree', gljSelOprObj.rootNode);
  309. }
  310. //弹出窗口
  311. $('#selGlj').modal('show');
  312. }},
  313. "delete": {
  314. name: "删除人材机",
  315. disabled: function () {
  316. const inValidCell = !commonUtil.isDef(target.row) || !commonUtil.isDef(target.col);
  317. const rationGlj = me.cache['_GLJ_' + me.currentRationItem.ID];
  318. const inValidData = !rationGlj || target.row >= rationGlj.length;
  319. if (locked || inValidCell || !me.currentRationItem || inValidData) {
  320. return true;
  321. }
  322. return false;
  323. },
  324. icon: "fa-remove",
  325. callback: function (key, opt) {
  326. const rationGlj = me.cache['_GLJ_' + me.currentRationItem.ID];
  327. rationGlj.splice(target.row, 1);
  328. me.updateRationItem(function(){
  329. me.sheet.getParent().focus();
  330. });
  331. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  332. me.showGljItems(me.currentRationItem.ID);
  333. }},
  334. }
  335. };
  336. }
  337. //rationCoe表
  338. else if(me.sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){
  339. let currentCache = raCoe.curRation && raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  340. sheet.setActiveCell(target.row, target.col);
  341. return {
  342. callback: function(){},
  343. items: {
  344. "upMove": {
  345. name: "上移",
  346. disabled: function () {
  347. const inValidCell = !commonUtil.isDef(target.row) || !commonUtil.isDef(target.col);
  348. const inValidData = target.row >= currentCache.length || !raCoe.isDef(currentCache[target.row - 1]);
  349. return locked || inValidCell || inValidData
  350. },
  351. icon: "fa-arrow-up",
  352. callback: function (key, opt) {
  353. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], {row: target.row - 1, col: target.col});
  354. }
  355. },
  356. "downMove": {
  357. name: "下移",
  358. disabled: function () {
  359. const inValidCell = !commonUtil.isDef(target.row) || !commonUtil.isDef(target.col);
  360. const inValidData = target.row >= currentCache.length || !raCoe.isDef(currentCache[target.row + 1]);
  361. return locked || inValidCell || inValidData;
  362. },
  363. icon: "fa-arrow-down",
  364. callback: function (key, opt) {
  365. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], {row: target.row + 1, col: target.col});
  366. }
  367. },
  368. "ref": {
  369. name: "添加到本节其他定额",
  370. disabled: function () {
  371. const inValidCell = !commonUtil.isDef(target.row) || !commonUtil.isDef(target.col);
  372. const inValidData = target.row >= currentCache.length;
  373. return locked || inValidCell || inValidData;
  374. },
  375. icon: "fa-arrow-left",
  376. callback: function (key, opt) {
  377. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  378. for(let i = 0, len = updateArr.length; i < len; i++){
  379. let ration = updateArr[i];
  380. let rationCoeList = updateArr[i].rationCoeList;
  381. let newNo = 1;
  382. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  383. if(rationCoeList[j].no >= newNo){
  384. newNo = rationCoeList[j].no + 1;
  385. }
  386. }
  387. let theCache = raCoe.cache["_Coe_" + ration.ID];
  388. if(theCache !== undefined && theCache !== null){
  389. let newCoe = {};
  390. for(let attr in currentCache[target.row]){
  391. newCoe[attr] = currentCache[target.row][attr];
  392. }
  393. newCoe.no = newNo;
  394. theCache.push(newCoe);
  395. }
  396. }
  397. });
  398. }
  399. }
  400. }
  401. };
  402. }
  403. else{
  404. return false;
  405. }
  406. }
  407. });
  408. },
  409. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的code不存在,恢复删除的被更新数据
  410. let rst = [];
  411. for(let i = 0, len = tempDelArr.length; i < len; i++){
  412. let isExist = false;
  413. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  414. if(tempDelArr[i].newCode == newArr[j].code){
  415. isExist = true;
  416. break;
  417. }
  418. }
  419. if(!isExist){
  420. rst.push(tempDelArr[i].org);
  421. }
  422. }
  423. return rst;
  424. },
  425. addGljItems: function(codes, repId, args) {
  426. var me = this;
  427. $.ajax({
  428. type:"POST",
  429. url:"api/getGljItemsByCodes",
  430. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  431. dataType:"json",
  432. cache:false,
  433. timeout:5000,
  434. success:function(result){
  435. if (result) {
  436. if(result.data.length > 0){
  437. if(priceProperties && priceProperties.length > 0){
  438. let priceField = priceProperties[0].price.dataCode;
  439. for(let glj of result.data){
  440. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  441. }
  442. }
  443. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  444. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  445. for (var i = 0; i < result.data.length; i++) {
  446. dummyR.gljId = result.data[i].ID;
  447. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  448. }
  449. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  450. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  451. for (var i = 0; i < rstArr.length; i++) {
  452. var hasDup = false;
  453. for (var j = 0; j < cacheArr.length; j++) {
  454. if (cacheArr[j].gljId == rstArr[i].gljId) {
  455. hasDup = true;
  456. break;
  457. }
  458. }
  459. if (!hasDup) {
  460. newAddArr.push(rstArr[i]);
  461. }
  462. }
  463. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  464. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, result.data);
  465. if(recoveryArr.length > 0){
  466. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  467. }
  468. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  469. let rst = 0;
  470. let aV = a.gljType + a.code,
  471. bV = b.gljType + b.code;
  472. if (aV > bV) {
  473. rst = 1;
  474. } else if (aV < bV) {
  475. rst = -1;
  476. }
  477. return rst;
  478. });
  479. }
  480. me.showGljItems(me.currentRationItem.ID);
  481. if (newAddArr.length > 0) {
  482. me.updateRationItem(function () {
  483. me.sheet.getParent().focus(true);
  484. });
  485. }
  486. }
  487. else{
  488. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID]? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  489. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  490. if(recoveryArr.length > 0){
  491. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  492. }
  493. //更新的工料机不存在
  494. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  495. let rst = 0;
  496. let aV = a.gljType + a.code,
  497. bV = b.gljType + b.code;
  498. if (aV > bV) rst = 1
  499. else if (aV < bV) rst = -1;
  500. return rst;
  501. });
  502. $('#alertModalBtn').click();
  503. $('#alertText').text("人材机"+ codes + "不存在,请查找你所需要的人材机,或新增人材机");
  504. $('#alertModalCls').click(function () {
  505. me.showGljItems(me.currentRationItem.ID);
  506. });
  507. $('#alertModalCof').click(function () {
  508. me.showGljItems(me.currentRationItem.ID);
  509. })
  510. }
  511. }
  512. },
  513. error:function(err){
  514. alert(err);
  515. }
  516. })
  517. },
  518. round(v, e){
  519. var t=1;
  520. for(;e>0;t*=10,e--);
  521. for(;e<0;t/=10,e++);
  522. return Math.round(v*t)/t;
  523. },
  524. rationCal: function () {
  525. let me = rationGLJOprObj;
  526. let price = {gljType1: [], gljType2: [], gljType3: [], gljType6: [], gljType7: [], gljType8: []},
  527. rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0, managePrice: 0, profitPrice: 0, riskPrice: 0},
  528. rationBasePrc = 0;
  529. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  530. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  531. cacheArr.forEach(function (gljData) {
  532. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  533. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  534. if(parent && parent.data.ID <= 3){
  535. price['gljType' + parent.data.ID].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  536. }
  537. if(!parent && gljData.gljType <= 3){
  538. price['gljType' + gljData.gljType].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  539. }
  540. if([6, 7, 8].includes(gljData.gljType)){
  541. price['gljType' + gljData.gljType].push(scMathUtil.roundTo( gljData.basePrice * gljData.consumeAmt, -3));//取三位
  542. }
  543. }
  544. });
  545. if(price.gljType1.length > 0){
  546. let labourPrice = 0;
  547. price.gljType1.forEach(function (singlePrc) {
  548. labourPrice = scMathUtil.roundTo(labourPrice + singlePrc, me.processDecimal);
  549. });
  550. let roundPrice = scMathUtil.roundTo(labourPrice, -2);
  551. rst.labourPrice = roundPrice;
  552. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  553. }
  554. if(price.gljType2.length > 0){
  555. let materialPrice = 0;
  556. price.gljType2.forEach(function (singlePrc) {
  557. materialPrice = scMathUtil.roundTo(materialPrice + singlePrc, me.processDecimal);
  558. });
  559. let roundPrice = scMathUtil.roundTo(materialPrice, -2);
  560. rst.materialPrice = roundPrice;
  561. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  562. }
  563. if(price.gljType3.length > 0){
  564. let machinePrice = 0;
  565. price.gljType3.forEach(function (singlePrc) {
  566. machinePrice = scMathUtil.roundTo(machinePrice + singlePrc, me.processDecimal);
  567. });
  568. let roundPrice = scMathUtil.roundTo(machinePrice, -2);
  569. rst.machinePrice = roundPrice;
  570. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  571. }
  572. if(price.gljType6.length > 0){
  573. let managePrice = 0;
  574. price.gljType6.forEach(function (singlePrc) {
  575. managePrice = scMathUtil.roundTo(managePrice + singlePrc, me.processDecimal);
  576. });
  577. let roundPrice = scMathUtil.roundTo(managePrice, -2);
  578. rst.managePrice = roundPrice;
  579. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  580. }
  581. if(price.gljType7.length > 0){
  582. let profitPrice = 0;
  583. price.gljType7.forEach(function (singlePrc) {
  584. profitPrice = scMathUtil.roundTo(profitPrice + singlePrc, me.processDecimal);
  585. });
  586. let roundPrice = scMathUtil.roundTo(profitPrice, -2);
  587. rst.profitPrice = roundPrice;
  588. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  589. }
  590. if(price.gljType8.length > 0){
  591. let riskPrice = 0;
  592. price.gljType8.forEach(function (singlePrc) {
  593. riskPrice = scMathUtil.roundTo(riskPrice + singlePrc, me.processDecimal);
  594. });
  595. let roundPrice = scMathUtil.roundTo(riskPrice, -2);
  596. rst.riskPrice = roundPrice;
  597. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  598. }
  599. rst.rationBasePrc = rationBasePrc;
  600. }
  601. return rst;
  602. },
  603. updateRationItem: function(callback) {
  604. var me = this, updateArr = [];
  605. if (me.currentRationItem) {
  606. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  607. //recalculate ration basePrice
  608. let price = me.rationCal();
  609. me.currentRationItem.labourPrice = price.labourPrice;
  610. me.currentRationItem.materialPrice = price.materialPrice;
  611. me.currentRationItem.machinePrice = price.machinePrice;
  612. me.currentRationItem.basePrice = price.rationBasePrc;
  613. updateArr.push(me.currentRationItem);
  614. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  615. if(callback) callback();
  616. });
  617. }
  618. },
  619. buildRationItemGlj: function(){
  620. var me = this, rst = [];
  621. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  622. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  623. for (var i = 0; i < cacheArr.length; i++) {
  624. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  625. }
  626. }
  627. return rst;
  628. },
  629. createRationGljDisplayItem: function(rItem, repGlj) {
  630. var rst = {};
  631. rst.gljId = rItem.gljId;
  632. rst.consumeAmt = rItem.consumeAmt;
  633. rst.code = repGlj.code;
  634. rst.name = repGlj.name;
  635. rst.specs = repGlj.specs;
  636. rst.unit = repGlj.unit;
  637. rst.basePrice = repGlj.basePrice;
  638. rst.gljType = repGlj.gljType;
  639. return rst;
  640. },
  641. getGljItems: function(rationItem, callback) {
  642. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [];
  643. me.currentRationItem = rationItem;
  644. if (me.cache["_GLJ_" + rationID]) {
  645. me.showGljItems(rationID);
  646. } else {
  647. var gljIds = [];
  648. for (var i = 0; i < rationGljList.length; i++) {
  649. gljIds.push(rationGljList[i].gljId);
  650. }
  651. $.ajax({
  652. type:"POST",
  653. url:"api/getGljItemsByIds",
  654. data:{"gljIds": JSON.stringify(gljIds)},
  655. dataType:"json",
  656. cache:false,
  657. timeout:5000,
  658. success:function(result){
  659. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  660. if (result) {
  661. if(priceProperties && priceProperties.length > 0){
  662. let priceField = priceProperties[0].price.dataCode;
  663. for(let glj of result.data){
  664. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  665. }
  666. }
  667. var cacheArr = [];
  668. for (var i = 0; i < result.data.length; i++) {
  669. for (var j = 0; j < rationGljList.length; j++) {
  670. if (rationGljList[j].gljId == result.data[i].ID) {
  671. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  672. break;
  673. }
  674. }
  675. }
  676. function compare(){
  677. return function (a, b) {
  678. let rst = 0;
  679. let aV = a.gljType + a.code,
  680. bV = b.gljType + b.code;
  681. if (aV > bV) {
  682. rst = -1;
  683. } else if (aV < bV) {
  684. rst = 1;
  685. }
  686. return rst;
  687. }
  688. }
  689. cacheArr.sort(compare());
  690. me.cache["_GLJ_" + rationID] = cacheArr;
  691. me.showGljItems(rationID);
  692. }
  693. if(callback) callback();
  694. },
  695. error:function(err){
  696. alert(err);
  697. }
  698. })
  699. }
  700. },
  701. showGljItems: function(rationID) {
  702. var me = this;
  703. if (me.cache["_GLJ_" + rationID]) {
  704. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  705. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  706. let rst = 0;
  707. let aV = a.gljType + a.code,
  708. bV = b.gljType + b.code;
  709. if(aV > bV){
  710. rst = 1;
  711. }
  712. else if(aV < bV){
  713. rst = -1;
  714. }
  715. return rst;
  716. });
  717. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  718. }
  719. }
  720. }