ration_glj.js 33 KB

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