ration_glj.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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: "/complementaryRation/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. gljSelOprObj.initView(0, true);
  131. }
  132. },
  133. "delete": {
  134. name: "删除人材机", disabled: delDis, icon: "fa-remove", callback: function (key, opt) {
  135. rationGlj.splice(target.row, 1);
  136. me.updateRationItem(function () {
  137. me.sheet.getParent().focus();
  138. });
  139. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  140. me.showGljItems(me.currentRationItem.ID);
  141. }
  142. },
  143. }
  144. };
  145. }
  146. //rationCoe表
  147. else if (me.sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined') {
  148. let currentCache = raCoe.curRation && raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  149. sheet.setActiveCell(target.row, target.col);
  150. //控制按钮是否可用
  151. let upDis = false,
  152. downDis = false,
  153. refDis = false;
  154. if (target.row >= currentCache.length) {
  155. upDis = true;
  156. downDis = true;
  157. refDis = true;
  158. }
  159. else {
  160. if (!raCoe.isDef(currentCache[target.row - 1])) {
  161. upDis = true;
  162. }
  163. if (!raCoe.isDef(currentCache[target.row + 1])) {
  164. downDis = true;
  165. }
  166. }
  167. return {
  168. callback: function () { },
  169. items: {
  170. "upMove": {
  171. name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  172. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], { row: target.row - 1, col: target.col });
  173. }
  174. },
  175. "downMove": {
  176. name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  177. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], { row: target.row + 1, col: target.col });
  178. }
  179. },
  180. "ref": {
  181. name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  182. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  183. for (let i = 0, len = updateArr.length; i < len; i++) {
  184. let ration = updateArr[i];
  185. let rationCoeList = updateArr[i].rationCoeList;
  186. let newNo = 1;
  187. for (let j = 0, jLen = rationCoeList.length; j < jLen; j++) {
  188. if (rationCoeList[j].no >= newNo) {
  189. newNo = rationCoeList[j].no + 1;
  190. }
  191. }
  192. let theCache = raCoe.cache["_Coe_" + ration.ID];
  193. if (theCache !== undefined && theCache !== null) {
  194. let newCoe = {};
  195. for (let attr in currentCache[target.row]) {
  196. newCoe[attr] = currentCache[target.row][attr];
  197. }
  198. newCoe.no = newNo;
  199. theCache.push(newCoe);
  200. }
  201. }
  202. });
  203. }
  204. }
  205. }
  206. };
  207. }
  208. else {
  209. return false;
  210. }
  211. }
  212. });
  213. },
  214. bindRationGljDelOpr: function () {
  215. let me = rationGLJOprObj, spreadBook = me.sheet.getParent();
  216. spreadBook.commandManager().register('rationGljDelete', function () {
  217. if (!me.currentRationItem) {
  218. return;
  219. }
  220. let sels = me.sheet.getSelections(), lockCols = me.setting.view.lockColumns;
  221. let cacheSection = me.cache["_GLJ_" + me.currentRationItem.ID], isUpdate = false;
  222. if (sels.length > 0) {
  223. for (let sel = 0; sel < sels.length; sel++) {
  224. if (sels[sel].colCount === me.setting.header.length) {
  225. if (cacheSection && sels[sel].row < cacheSection.length) {
  226. isUpdate = true;
  227. cacheSection.splice(sels[sel].row, sels[sel].rowCount);
  228. }
  229. }
  230. else {
  231. if (sels[sel].col !== 0 && sels[sel].col !== 5 && !(sels[sel].col === 1 && sels.col + sels[sel].colCount - 1 === 3)) {
  232. if (cacheSection) {
  233. for (let i = sels[sel].row === -1 ? 1 : 0; i < sels[sel].rowCount; i++) {
  234. if (sels[sel].row + i < cacheSection.length) {
  235. for (let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++) {
  236. if (lockCols.indexOf(col) === -1) {
  237. isUpdate = true;
  238. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = 0;
  239. me.sheet.setValue(sels[sel].row + i, col, 0.00);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. if (isUpdate) {
  250. me.updateRationItem(function () {
  251. me.sheet.getParent().focus(true);
  252. });
  253. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  254. me.showGljItems(me.currentRationItem.ID);
  255. }
  256. });
  257. spreadBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  258. spreadBook.commandManager().setShortcutKey('rationGljDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  259. },
  260. onClipboardPasting: function (sender, args) {
  261. var me = rationGLJOprObj;
  262. let rationSection = rationOprObj.getCache();
  263. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  264. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  265. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  266. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  267. }
  268. if (!(args.cellRange.col === 0 || args.cellRange.col === 5) || !(me.currentRationItem)) {
  269. args.cancel = true;
  270. }
  271. },
  272. onClipboardPasted: function (e, info) {
  273. var me = rationGLJOprObj, repId = pageOprObj.rationLibId;
  274. me.tempCacheArr = [];
  275. let gljLibId = pageOprObj.gljLibId;
  276. console.log(gljLibId);
  277. if (gljLibId) {
  278. if (info.cellRange.col == 0) {
  279. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  280. var tmpCodes = sheetCommonObj.analyzePasteData({ header: [{ dataCode: "code" }] }, info);
  281. var codes = [];
  282. for (var i = 0; i < tmpCodes.length; i++) {
  283. let rowIdx = info.cellRange.row + i;
  284. if (rowIdx < cacheArr.length) {//更新
  285. me.tempCacheArr.push({ org: cacheArr[rowIdx], newCode: tmpCodes[i].code });
  286. cacheArr.splice(rowIdx--, 1);
  287. }
  288. codes.push(tmpCodes[i].code);
  289. }
  290. me.addGljItems(codes, gljLibId, info.cellRange);
  291. } else {
  292. //修改用量
  293. if (me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  294. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  295. let maxCount = info.cellRange.row + info.cellRange.rowCount - 1 > me.cache["_GLJ_" + me.currentRationItem.ID].length - 1 ?
  296. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  297. for (let i = 0; i < maxCount; i++) {
  298. let roundCons = scMathUtil.roundTo(tempConsumes[i].consumeAmt, -3);
  299. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  300. }
  301. me.updateRationItem(function () {
  302. me.sheet.getParent().focus(true);
  303. });
  304. if (info.cellRange.row + info.cellRange.rowCount - 1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length - 1) {
  305. me.sheet.suspendPaint();
  306. for (let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  307. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  308. }
  309. me.sheet.resumePaint();
  310. }
  311. }
  312. else if (info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  313. me.sheet.suspendPaint();
  314. for (let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount - 1; rowIdx++) {
  315. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  316. }
  317. me.sheet.resumePaint();
  318. }
  319. }
  320. }
  321. },
  322. onEditStarting: function (sender, args) {
  323. let me = rationGLJOprObj;
  324. let rationSection = rationOprObj.getCache();
  325. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  326. me.currentRationItem = rationRow < rationSection.length ? rationSection[rationRow] : null;
  327. if (me.currentRationItem && typeof me.cache["_GLJ_" + me.currentRationItem.ID] === 'undefined') {
  328. me.cache["_GLJ_" + me.currentRationItem.ID] = [];
  329. }
  330. if (!me.currentRationItem) {
  331. args.cancel = true;
  332. }
  333. else {
  334. if (args.col !== 0 && args.col !== 5 || args.col === 5 && args.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length) {
  335. args.cancel = true;
  336. }
  337. }
  338. },
  339. onCellEditEnd: function (sender, args) {
  340. var me = rationGLJOprObj;
  341. if (me.currentRationItem) {
  342. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  343. me.tempCacheArr = [];
  344. if (args.col != 0) {
  345. if (args.row < cacheArr.length) {
  346. var editGlj = cacheArr[args.row];
  347. if (editGlj["consumeAmt"] != args.editingText) {
  348. let parseNum = parseFloat(args.editingText);
  349. if (isNaN(parseFloat(args.editingText))) {
  350. $('#alertModalBtn').click();
  351. $('#alertText').text("定额消耗只能输入数值!");
  352. $('#alertModalCls').click(function () {
  353. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  354. });
  355. $('#alertModalCof').click(function () {
  356. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  357. })
  358. }
  359. else {
  360. args.sheet.setValue(args.row, args.col, parseNum);
  361. let roundNum = scMathUtil.roundTo(parseNum, -3);
  362. editGlj["consumeAmt"] = roundNum;
  363. me.updateRationItem(function () {
  364. me.sheet.getParent().focus(true);
  365. });
  366. }
  367. }
  368. }
  369. } else {
  370. if (args.editingText && args.editingText.toString().trim().length !== 0) {
  371. let isExist = false;
  372. for (let i = 0, len = cacheArr.length; i < len; i++) {
  373. if (cacheArr[i].code === args.editingText && i !== args.row) {
  374. isExist = true;
  375. break;
  376. }
  377. }
  378. if (isExist) {
  379. alert("人材机已存在!");
  380. args.sheet.setValue(args.row, args.col, typeof cacheArr[args.row] !== 'undefined' ? cacheArr[args.row].code + '' : '');
  381. }
  382. else {
  383. if (args.row < cacheArr.length && args.editingText !== cacheArr[args.row].code) {//更新
  384. me.tempCacheArr.push({ org: cacheArr[args.row], newCode: args.editingText.toString().trim() });
  385. cacheArr.splice(args.row, 1);
  386. let gljLibID = pageOprObj.gljLibId;
  387. let codes = [];
  388. codes.push(args.editingText.toString().trim());
  389. me.addGljItems(codes, gljLibID, args);
  390. }
  391. else if (args.row >= cacheArr.length) {//新增
  392. let gljLibID = pageOprObj.gljLibId;
  393. if (gljLibID) {
  394. var codes = [];
  395. codes.push(args.editingText.toString().trim());
  396. me.addGljItems(codes, gljLibID, args);
  397. }
  398. }
  399. }
  400. }
  401. else {
  402. args.sheet.setValue(args.row, args.col, args.row < cacheArr.length ? cacheArr[args.row].code : '');
  403. }
  404. }
  405. }
  406. else {
  407. args.sheet.setValue(args.row, args.col, '');
  408. }
  409. },
  410. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的code不存在,恢复删除的被更新数据
  411. let rst = [];
  412. for (let i = 0, len = tempDelArr.length; i < len; i++) {
  413. let isExist = false;
  414. for (let j = 0, jLen = newArr.length; j < jLen; j++) {
  415. if (tempDelArr[i].newCode == newArr[j].code) {
  416. isExist = true;
  417. break;
  418. }
  419. }
  420. if (!isExist) {
  421. rst.push(tempDelArr[i].org);
  422. }
  423. }
  424. return rst;
  425. },
  426. addGljItems: function (codes, repId, args) {
  427. let me = this;
  428. CommonAjax.post('/complementaryRation/api/getGljItemsByCodes', { gljCodes: codes, rationRepId: repId }, function (rstData) {
  429. if (rstData.length > 0) {
  430. if (priceProperties && priceProperties.length > 0) {
  431. let priceField = priceProperties[0].price.dataCode;
  432. for (let glj of rstData) {
  433. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  434. }
  435. }
  436. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  437. let rstArr = [], dummyR = { gljId: 0, consumeAmt: 0 }, newAddArr = [];
  438. for (let i = 0; i < rstData.length; i++) {
  439. dummyR.gljId = rstData[i].ID;
  440. dummyR.type = rstData[i].type;
  441. rstArr.push(me.createRationGljDisplayItem(dummyR, rstData[i]));
  442. }
  443. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  444. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  445. for (var i = 0; i < rstArr.length; i++) {
  446. var hasDup = false;
  447. for (var j = 0; j < cacheArr.length; j++) {
  448. if (cacheArr[j].gljId == rstArr[i].gljId) {
  449. hasDup = true;
  450. break;
  451. }
  452. }
  453. if (!hasDup) {
  454. newAddArr.push(rstArr[i]);
  455. }
  456. }
  457. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  458. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, rstData);
  459. if (recoveryArr.length > 0) {
  460. me.cache["_GLJ_" + me.currentRationItem.ID] = me.cache["_GLJ_" + me.currentRationItem.ID].concat(recoveryArr);
  461. }
  462. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  463. let aV = a.gljType + a.code,
  464. bV = b.gljType + b.code;
  465. if (aV > bV) {
  466. return 1;
  467. } else if (aV < bV) {
  468. return -1
  469. }
  470. return 0;
  471. });
  472. }
  473. me.showGljItems(me.currentRationItem.ID);
  474. if (newAddArr.length > 0) {
  475. me.updateRationItem(function () {
  476. me.sheet.getParent().focus(true);
  477. });
  478. }
  479. }
  480. else {
  481. let cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID] ? me.cache["_GLJ_" + me.currentRationItem.ID] : [];
  482. let recoveryArr = me.getRecoveryArr(me.tempCacheArr, []);
  483. if (recoveryArr.length > 0) {
  484. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(recoveryArr);
  485. }
  486. //更新的工料机不存在
  487. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function (a, b) {
  488. let aV = a.gljType + a.code,
  489. bV = b.gljType + b.code;
  490. if (aV > bV) {
  491. return 1;
  492. } else if (aV < bV) {
  493. return -1
  494. }
  495. return 0;
  496. });
  497. $('#alertModalBtn').click();
  498. $('#alertText').text("人材机" + codes + "不存在,请查找你所需要的人材机,或新增人材机");
  499. $('#alertModalCls').click(function () {
  500. me.showGljItems(me.currentRationItem.ID);
  501. });
  502. $('#alertModalCof').click(function () {
  503. me.showGljItems(me.currentRationItem.ID);
  504. })
  505. }
  506. });
  507. },
  508. round(v, e) {
  509. var t = 1;
  510. for (; e > 0; t *= 10, e--);
  511. for (; e < 0; t /= 10, e++);
  512. return Math.round(v * t) / t;
  513. },
  514. rationCal: function () {
  515. let me = rationGLJOprObj;
  516. let price = { gljType1: [], gljType2: [], gljType3: [] }, rst = { labourPrice: 0, materialPrice: 0, machinePrice: 0 }, rationBasePrc = 0;
  517. if (me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]) {
  518. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  519. cacheArr.forEach(function (gljData) {
  520. if (gljData.gljType && gljData.basePrice && gljData.consumeAmt) {
  521. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  522. if (parent && parent.data.ID <= 3) {
  523. price['gljType' + parent.data.ID].push(scMathUtil.roundTo(gljData.basePrice * gljData.consumeAmt, -3));//取三位
  524. }
  525. if (!parent && gljData.gljType <= 3) {
  526. price['gljType' + gljData.gljType].push(scMathUtil.roundTo(gljData.basePrice * gljData.consumeAmt, -3));//取三位
  527. }
  528. }
  529. });
  530. if (price.gljType1.length > 0) {
  531. let labourPrice = 0;
  532. price.gljType1.forEach(function (singlePrc) {
  533. labourPrice = scMathUtil.roundTo(labourPrice + singlePrc, me.processDecimal);
  534. });
  535. let roundPrice = scMathUtil.roundTo(labourPrice, -2);
  536. rst.labourPrice = roundPrice;
  537. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  538. }
  539. if (price.gljType2.length > 0) {
  540. let materialPrice = 0;
  541. price.gljType2.forEach(function (singlePrc) {
  542. materialPrice = scMathUtil.roundTo(materialPrice + singlePrc, me.processDecimal);
  543. });
  544. let roundPrice = scMathUtil.roundTo(materialPrice, -2);
  545. rst.materialPrice = roundPrice;
  546. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  547. }
  548. if (price.gljType3.length > 0) {
  549. let machinePrice = 0;
  550. price.gljType3.forEach(function (singlePrc) {
  551. machinePrice = scMathUtil.roundTo(machinePrice + singlePrc, me.processDecimal);
  552. });
  553. let roundPrice = scMathUtil.roundTo(machinePrice, -2);
  554. rst.machinePrice = roundPrice;
  555. rationBasePrc = scMathUtil.roundTo(rationBasePrc + roundPrice, -2);
  556. }
  557. rst.rationBasePrc = rationBasePrc;
  558. }
  559. return rst;
  560. },
  561. updateRationItem: function (callback) {
  562. var me = this, updateArr = [];
  563. if (me.currentRationItem) {
  564. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  565. //recalculate ration basePrice
  566. let price = me.rationCal();
  567. me.currentRationItem.labourPrice = price.labourPrice;
  568. me.currentRationItem.materialPrice = price.materialPrice;
  569. me.currentRationItem.machinePrice = price.machinePrice;
  570. me.currentRationItem.basePrice = price.rationBasePrc;
  571. updateArr.push(me.currentRationItem);
  572. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  573. if (callback) callback();
  574. });
  575. }
  576. },
  577. buildRationItemGlj: function () {
  578. var me = this, rst = [];
  579. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  580. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  581. for (var i = 0; i < cacheArr.length; i++) {
  582. const newItem = { gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, type: cacheArr[i].type };
  583. if (cacheArr[i].userId && cacheArr[i].userId !== userID) {
  584. // 是分享人材机
  585. newItem.fromUser = cacheArr[i].userId;
  586. }
  587. rst.push(newItem);
  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. if (rItem.fromUser) {
  604. rst.userId = rItem.fromUser;
  605. }
  606. return rst;
  607. },
  608. getGljItems: function (rationItem, callback) {
  609. let me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList ? rationItem.rationGljList : [], rationType = rationItem.type;
  610. me.currentRationItem = rationItem;
  611. if (me.cache["_GLJ_" + rationID]) {
  612. me.showGljItems(rationID);
  613. } else {
  614. let gljIds = [];
  615. for (let i = 0; i < rationGljList.length; i++) {
  616. let idObj = Object.create(null);
  617. idObj.type = rationGljList[i].type;
  618. idObj.id = rationGljList[i].gljId;
  619. if (rationGljList[i].fromUser) {
  620. idObj.fromUser = rationGljList[i].fromUser;
  621. }
  622. gljIds.push(idObj);
  623. }
  624. CommonAjax.post('/complementaryRation/api/getGljItemsByIds', { ids: gljIds }, function (rstData) {
  625. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  626. if (priceProperties && priceProperties.length > 0) {
  627. let priceField = priceProperties[0].price.dataCode;
  628. for (let glj of rstData) {
  629. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  630. }
  631. }
  632. var cacheArr = [];
  633. for (let i = 0; i < rstData.length; i++) {
  634. for (let j = 0; j < rationGljList.length; j++) {
  635. if (rationGljList[j].gljId == rstData[i].ID) {
  636. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], rstData[i]));
  637. break;
  638. }
  639. }
  640. }
  641. function compare() {
  642. return function (a, b) {
  643. let aV = a.gljType + a.code,
  644. bV = b.gljType + b.code;
  645. if (aV > bV) {
  646. return 1;
  647. } else if (aV < bV) {
  648. return -1
  649. }
  650. return 0;
  651. }
  652. }
  653. cacheArr.sort(compare());
  654. me.cache["_GLJ_" + rationID] = cacheArr;
  655. me.showGljItems(rationID);
  656. if (callback) callback();
  657. });
  658. }
  659. },
  660. showGljItems: function (rationID) {
  661. var me = this;
  662. if (me.cache["_GLJ_" + rationID]) {
  663. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  664. me.cache["_GLJ_" + rationID].sort(function (a, b) {
  665. let aV = a.gljType + a.code,
  666. bV = b.gljType + b.code;
  667. if (aV > bV) {
  668. return 1;
  669. } else if (aV < bV) {
  670. return -1
  671. }
  672. return 0;
  673. });
  674. sheetsOprObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  675. }
  676. }
  677. }