ration_glj.js 22 KB

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