ration_glj.js 38 KB

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