ration_glj.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var rationGLJOprObj = {
  5. sheet: null,
  6. currentRationItem: null,
  7. distTypeTree: null,
  8. activeCell: null,
  9. cache: {},
  10. setting: {
  11. header:[
  12. {headerName:"编码",headerWidth:120,dataCode:"code", dataType: "String", formatter: "@"},
  13. {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String"},
  14. {headerName:"单位",headerWidth:160,dataCode:"unit", dataType: "String"},
  15. {headerName:"基价单位",headerWidth:160, dataCode:"basePrice", dataType: "Number", formatter:"0.00", precision: 2},
  16. {headerName:"定额消耗",headerWidth:160, dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", precision: 3},
  17. {headerName:"类型",headerWidth:160,dataCode:"gljType", dataType: "String"}
  18. ],
  19. view:{
  20. comboBox:[],
  21. lockColumns:[1,2,3,5]
  22. }
  23. },
  24. getDistTypeTree: function (gljDistType) {
  25. let me = this;
  26. let distType;
  27. let distTypeTree = {
  28. prefix : 'gljDistType',
  29. distTypes: {},
  30. comboDatas: [],
  31. distTypesArr: []
  32. };
  33. gljDistType.forEach(function (typeData) {
  34. let typeObj = {
  35. data: typeData,
  36. children: [],
  37. parent: null
  38. }
  39. distTypeTree.distTypes[distTypeTree.prefix + typeData.ID] = typeObj;
  40. distTypeTree.distTypesArr.push(typeObj);
  41. });
  42. gljDistType.forEach(function (typeData) {
  43. distType = distTypeTree.distTypes[distTypeTree.prefix + typeData.ID];
  44. let parent = distTypeTree.distTypes[distTypeTree.prefix + typeData.ParentID];
  45. if(parent){
  46. distType.parent = parent;
  47. parent.children.push(distType);
  48. }
  49. });
  50. distTypeTree.distTypesArr.forEach(function (distTypeObj) {
  51. if(distTypeObj.children.length === 0 && distTypeObj.data.fullName !== '普通机械' &&distTypeObj.data.fullName !== '机械组成物'
  52. && distTypeObj.data.fullName !== '机上人工'){
  53. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  54. }
  55. if(distTypeObj.data.fullName === '机械'){
  56. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  57. }
  58. });
  59. return distTypeTree;
  60. },
  61. getGljDistType: function (callback) {
  62. let me = this;
  63. $.ajax({
  64. type: 'post',
  65. url: "api/getGljDistType",
  66. dataType: 'json',
  67. success: function (result) {
  68. if(!result.error && callback){
  69. me.distTypeTree = me.getDistTypeTree(result.data);
  70. callback();
  71. }
  72. }
  73. })
  74. },
  75. buildSheet: function(sheet) {
  76. var me = this;
  77. me.sheet = sheet;
  78. me.getGljDistType(function () {
  79. me.onContextmenuOpr();
  80. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  81. me.rationGljDelOpr();
  82. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  83. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  84. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  85. //me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  86. });
  87. },
  88. rationGljDelOpr: function () {
  89. let me = rationGLJOprObj, spreadBook = me.sheet.getParent();
  90. spreadBook.commandManager().register('rationGljDelete', function () {
  91. let sels = me.sheet.getSelections(), updateArr = [], removeArr = [], lockCols = me.setting.view.lockColumns;
  92. let cacheSection = me.cache["_GLJ_" + me.currentRationItem.ID], isUpdate = false;
  93. if(sels.length > 0){
  94. for(let sel = 0; sel < sels.length; sel++){
  95. if(sels[sel].colCount === me.setting.header.length){
  96. if(cacheSection){
  97. for(let i = 0; i < sels[sel].rowCount; i++){
  98. if(sels[sel].row + i < cacheSection.length){
  99. isUpdate = true;
  100. cacheSection.splice(sels[sel].row + i, 1);
  101. me.updateRationItem();
  102. }
  103. }
  104. }
  105. }
  106. else{
  107. if(sels[sel].col === 0){
  108. $('#alertText').text("编号不能为空,修改失败!");
  109. $('#alertModalBtn').click();
  110. me.sheet.options.isProtected = true;
  111. $('#alertModalCls').click(function () {
  112. me.sheet.options.isProtected = false;
  113. });
  114. $('#alertModalCof').click(function () {
  115. me.sheet.options.isProtected = false;
  116. });
  117. }
  118. else if(sels[sel].col !== 0 && sels[sel].col !== 5 && !(sels[sel].col === 1 && sels.col + sels[sel].colCount -1 === 3)){
  119. if(cacheSection){
  120. for(let i = sels[sel].row === -1 ? 1 : 0; i < sels[sel].rowCount; i++){
  121. if(sels[sel].row + i < cacheSection.length){
  122. for(let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++){
  123. if(lockCols.indexOf(col) === -1){
  124. isUpdate = true;
  125. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = 0;
  126. me.sheet.setValue(sels[sel].row + i, col, 0.00);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. if(isUpdate){
  137. me.updateRationItem();
  138. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  139. me.showGljItems(me.currentRationItem.ID);
  140. }
  141. /* if(updateArr.length > 0 || removeArr.length > 0){
  142. me.mixUpdateRequest(updateArr, [], removeArr);
  143. }*/
  144. });
  145. spreadBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  146. spreadBook.commandManager().setShortcutKey('rationGljDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  147. },
  148. onRangeChanged: function(sender, args) {
  149. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  150. var me = rationGLJOprObj, updateArr = [], removeArr = [];
  151. if (args.col == 0) {
  152. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  153. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  154. for (var i = args.rowCount - 1; i >= 0; i--) {
  155. if (args.row + i < cacheArr.length) {
  156. cacheArr.splice(args.row + i, 1);
  157. }
  158. }
  159. me.updateRationItem();
  160. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  161. me.showGljItems(me.currentRationItem.ID);
  162. }
  163. }
  164. }
  165. },
  166. onClipboardPasting: function(sender, args) {
  167. var me = rationGLJOprObj;
  168. /*if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.currentRationItem)) {
  169. args.cancel = true;
  170. }*/
  171. },
  172. onClipboardPasted: function(e, info) {
  173. var me = rationGLJOprObj, repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  174. if (repId) {
  175. let gljLibId = storageUtil.getSessionCache("gljLib", "repositoryID_" + repId);
  176. if(gljLibId){
  177. if (info.cellRange.col == 0) {
  178. var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
  179. var codes = [];
  180. for (var i = 0; i < tmpCodes.length; i++) {
  181. codes.push(tmpCodes[i].code);
  182. }
  183. me.addGljItems(codes, gljLibId, info.cellRange);
  184. } else {
  185. //修改用量
  186. if(me.cache["_GLJ_" + me.currentRationItem.ID] && info.cellRange.row < me.cache["_GLJ_" + me.currentRationItem.ID].length){
  187. let tempConsumes = sheetCommonObj.analyzePasteData(me.setting, info);
  188. let maxCount = info.cellRange.row + info.cellRange.rowCount -1 > me.cache["_GLJ_" + me.currentRationItem.ID].length -1 ?
  189. me.cache["_GLJ_" + me.currentRationItem.ID].length - info.cellRange.row : info.cellRange.rowCount;
  190. for(let i = 0; i < maxCount; i++){
  191. let roundCons = me.round(tempConsumes[i].consumeAmt, 3);
  192. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = roundCons;
  193. }
  194. me.updateRationItem();
  195. if(info.cellRange.row + info.cellRange.rowCount -1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length -1){
  196. me.sheet.suspendPaint();
  197. for(let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx++){
  198. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  199. }
  200. me.sheet.resumePaint();
  201. }
  202. }
  203. else if(info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length){
  204. me.sheet.suspendPaint();
  205. for(let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx ++){
  206. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  207. }
  208. me.sheet.resumePaint();
  209. }
  210. }
  211. }
  212. }
  213. },
  214. onCellEditEnd: function(sender, args){
  215. var me = rationGLJOprObj;
  216. if (args.col != 0) {
  217. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  218. if (args.row < cacheArr.length) {
  219. var editGlj = cacheArr[args.row];
  220. if (editGlj["consumeAmt"] != args.editingText) {
  221. let parseNum = parseFloat(args.editingText);
  222. if(isNaN(parseFloat(args.editingText))){
  223. $('#alertModalBtn').click();
  224. $('#alertText').text("定额消耗只能输入数值!");
  225. args.sheet.options.isProtected = true;
  226. $('#alertModalCls').click(function () {
  227. args.sheet.options.isProtected = false;
  228. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  229. });
  230. $('#alertModalCof').click(function () {
  231. args.sheet.options.isProtected = false;
  232. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  233. })
  234. }
  235. else{
  236. args.sheet.setValue(args.row, args.col, parseNum);
  237. let roundNum = me.round(parseNum, 3);
  238. editGlj["consumeAmt"] = roundNum;
  239. me.updateRationItem();
  240. }
  241. }
  242. }
  243. } else {
  244. //重新更新工料机
  245. /*if (args.editingText == null || args.editingText.trim() == "") {
  246. //delete
  247. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  248. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  249. if (args.row < cacheArr.length) {
  250. cacheArr.splice(args.row, 1);
  251. me.updateRationItem();
  252. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  253. me.showGljItems(me.currentRationItem.ID);
  254. }
  255. }
  256. } else*/
  257. if(args.editingText !== null && args.editingText.trim().length !== 0){
  258. let rationRepId = storageUtil.getSessionCache("RationGrp","repositoryID");
  259. let gljLibID = storageUtil.getSessionCache("gljLib", "repositoryID_" + rationRepId);
  260. if (gljLibID) {
  261. var codes = [];
  262. codes.push(args.editingText.trim());
  263. me.addGljItems(codes, gljLibID, args);
  264. }
  265. }
  266. }
  267. },
  268. onContextmenuOpr: function () {
  269. $.contextMenu({
  270. selector: '#rdSpread',
  271. callback: function(key, options) {
  272. var m = "clicked: " + key;
  273. window.console && console.log(m) || alert(m);
  274. },
  275. items: {
  276. "insert": {name: "插入", callback: function (key, opt) {
  277. }},
  278. "delete": {name: "删除"}
  279. }
  280. });
  281. },
  282. addGljItems: function(codes, repId, args) {
  283. var me = this;
  284. $.ajax({
  285. type:"POST",
  286. url:"api/getGljItemsByCodes",
  287. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  288. dataType:"json",
  289. cache:false,
  290. timeout:5000,
  291. success:function(result){
  292. //sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  293. if (result) {
  294. if(result.data.length > 0){
  295. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  296. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  297. for (var i = 0; i < result.data.length; i++) {
  298. dummyR.gljId = result.data[i].ID;
  299. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  300. }
  301. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  302. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  303. for (var i = 0; i < rstArr.length; i++) {
  304. var hasDup = false;
  305. for (var j = 0; j < cacheArr.length; j++) {
  306. if (cacheArr[j].gljId == rstArr[i].gljId) {
  307. hasDup = true;
  308. break;
  309. }
  310. }
  311. if (!hasDup) {
  312. newAddArr.push(rstArr[i]);
  313. }
  314. }
  315. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  316. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  317. var rst = 0;
  318. if (a.code > b.code) rst = 1
  319. else if (a.code < b.code) rst = -1;
  320. return rst;
  321. });
  322. }
  323. me.showGljItems(me.currentRationItem.ID);
  324. if (newAddArr.length > 0) {
  325. me.updateRationItem();
  326. }
  327. }
  328. else{
  329. $('#alertModalBtn').click();
  330. $('#alertText').text("工料机"+ codes + "不存在,请查找你所需要的工料机,或新增工料机");
  331. me.sheet.options.isProtected = true;
  332. $('#alertModalCls').click(function () {
  333. sheetCommonObj.lockCells(me.sheet, me.setting);
  334. me.showGljItems(me.currentRationItem.ID);
  335. me.sheet.setValue(args.row, args.col, '');
  336. });
  337. $('#alertModalCof').click(function () {
  338. sheetCommonObj.lockCells(me.sheet, me.setting);
  339. me.showGljItems(me.currentRationItem.ID);
  340. me.sheet.setValue(args.row, args.col, '');
  341. })
  342. }
  343. }
  344. sheetCommonObj.lockCells(me.sheet, me.setting);
  345. },
  346. error:function(err){
  347. alert(err);
  348. }
  349. })
  350. },
  351. round(v, e){
  352. var t=1;
  353. for(;e>0;t*=10,e--);
  354. for(;e<0;t/=10,e++);
  355. return Math.round(v*t)/t;
  356. },
  357. rationCal: function () {
  358. let me = rationGLJOprObj;
  359. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  360. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  361. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  362. cacheArr.forEach(function (gljData) {
  363. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  364. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  365. if(parent && parent.data.ID <= 3){
  366. price['gljType' + parent.data.ID].push(me.round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  367. }
  368. if(!parent && gljData.gljType <= 3){
  369. price['gljType' + gljData.gljType].push(me.round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  370. }
  371. }
  372. });
  373. if(price.gljType1.length > 0){
  374. let labourPrice = 0;
  375. price.gljType1.forEach(function (singlePrc) {
  376. labourPrice += singlePrc;
  377. });
  378. let roundPrice = me.round(labourPrice, 2);
  379. rst.labourPrice = roundPrice;
  380. rationBasePrc += roundPrice;
  381. }
  382. if(price.gljType2.length > 0){
  383. let materialPrice = 0;
  384. price.gljType2.forEach(function (singlePrc) {
  385. materialPrice += singlePrc;
  386. });
  387. let roundPrice = me.round(materialPrice, 2);
  388. rst.materialPrice = roundPrice;
  389. rationBasePrc += roundPrice;
  390. }
  391. if(price.gljType3.length > 0){
  392. let machinePrice = 0;
  393. price.gljType3.forEach(function (singlePrc) {
  394. machinePrice += singlePrc;
  395. });
  396. let roundPrice = me.round(machinePrice, 2);
  397. rst.machinePrice = roundPrice;
  398. rationBasePrc += roundPrice;
  399. }
  400. rst.rationBasePrc = rationBasePrc;
  401. }
  402. return rst;
  403. },
  404. updateRationItem: function() {
  405. var me = this, updateArr = [];
  406. if (me.currentRationItem) {
  407. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  408. //recalculate ration basePrice
  409. let price = me.rationCal();
  410. me.currentRationItem.labourPrice = price.labourPrice;
  411. me.currentRationItem.materialPrice = price.materialPrice;
  412. me.currentRationItem.machinePrice = price.machinePrice;
  413. me.currentRationItem.basePrice = price.rationBasePrc;
  414. updateArr.push(me.currentRationItem);
  415. rationOprObj.mixUpdateRequest(updateArr, [], []);
  416. }
  417. },
  418. buildRationItemGlj: function(){
  419. var me = this, rst = [];
  420. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  421. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  422. for (var i = 0; i < cacheArr.length; i++) {
  423. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  424. }
  425. }
  426. return rst;
  427. },
  428. createRationGljDisplayItem: function(rItem, repGlj) {
  429. var rst = {};
  430. rst.gljId = rItem.gljId;
  431. rst.consumeAmt = rItem.consumeAmt;
  432. rst.code = repGlj.code;
  433. rst.name = repGlj.name;
  434. rst.specs = repGlj.specs;
  435. rst.unit = repGlj.unit;
  436. rst.basePrice = repGlj.basePrice;
  437. rst.gljType = repGlj.gljType;
  438. return rst;
  439. },
  440. getGljItems: function(rationItem, callback) {
  441. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  442. me.currentRationItem = rationItem;
  443. if (me.cache["_GLJ_" + rationID]) {
  444. me.showGljItems(rationID);
  445. sheetCommonObj.lockCells(me.sheet, me.setting);
  446. } else {
  447. var gljIds = [];
  448. for (var i = 0; i < rationGljList.length; i++) {
  449. gljIds.push(rationGljList[i].gljId);
  450. }
  451. $.ajax({
  452. type:"POST",
  453. url:"api/getGljItemsByIds",
  454. data:{"gljIds": JSON.stringify(gljIds)},
  455. dataType:"json",
  456. cache:false,
  457. timeout:5000,
  458. success:function(result){
  459. console.log(result);
  460. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  461. if (result) {
  462. var cacheArr = [];
  463. for (var i = 0; i < result.data.length; i++) {
  464. for (var j = 0; j < rationGljList.length; j++) {
  465. if (rationGljList[j].gljId == result.data[i].ID) {
  466. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  467. break;
  468. }
  469. }
  470. }
  471. function compare(){
  472. return function (a, b) {
  473. let rst = 0;
  474. if (a.code > b.code) {
  475. rst = 1;
  476. }
  477. else if (a.code < b.code) {
  478. rst = -1;
  479. }
  480. return rst;
  481. }
  482. }
  483. cacheArr.sort(compare());
  484. me.cache["_GLJ_" + rationID] = cacheArr;
  485. me.showGljItems(rationID);
  486. }
  487. sheetCommonObj.lockCells(me.sheet, me.setting);
  488. callback();
  489. },
  490. error:function(err){
  491. alert(err);
  492. }
  493. })
  494. }
  495. },
  496. showGljItems: function(rationID) {
  497. var me = this;
  498. if (me.cache["_GLJ_" + rationID]) {
  499. sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  500. }
  501. }
  502. }