ration_glj.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. me.cache["_GLJ_" + me.currentRationItem.ID][info.cellRange.row + i].consumeAmt = tempConsumes[i].consumeAmt;
  192. }
  193. me.updateRationItem();
  194. if(info.cellRange.row + info.cellRange.rowCount -1 >= me.cache["_GLJ_" + me.currentRationItem.ID].length -1){
  195. me.sheet.suspendPaint();
  196. for(let rowIdx = me.cache["_GLJ_" + me.currentRationItem.ID].length; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx++){
  197. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  198. }
  199. me.sheet.resumePaint();
  200. }
  201. }
  202. else if(info.cellRange.row >= me.cache["_GLJ_" + me.currentRationItem.ID].length){
  203. me.sheet.suspendPaint();
  204. for(let rowIdx = info.cellRange.row; rowIdx <= info.cellRange.row + info.cellRange.rowCount -1; rowIdx ++){
  205. me.sheet.setValue(rowIdx, info.cellRange.col, '');
  206. }
  207. me.sheet.resumePaint();
  208. }
  209. }
  210. }
  211. }
  212. },
  213. onCellEditEnd: function(sender, args){
  214. var me = rationGLJOprObj;
  215. if (args.col != 0) {
  216. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  217. if (args.row < cacheArr.length) {
  218. var editGlj = cacheArr[args.row];
  219. if (editGlj["consumeAmt"] != args.editingText) {
  220. let parseNum = parseFloat(args.editingText);
  221. if(isNaN(parseFloat(args.editingText))){
  222. $('#alertModalBtn').click();
  223. $('#alertText').text("定额消耗只能输入数值!");
  224. args.sheet.options.isProtected = true;
  225. $('#alertModalCls').click(function () {
  226. args.sheet.options.isProtected = false;
  227. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  228. });
  229. $('#alertModalCof').click(function () {
  230. args.sheet.options.isProtected = false;
  231. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  232. })
  233. }
  234. else{
  235. args.sheet.setValue(args.row, args.col, parseNum);
  236. editGlj["consumeAmt"] = parseNum;
  237. me.updateRationItem();
  238. }
  239. }
  240. }
  241. } else {
  242. //重新更新工料机
  243. /*if (args.editingText == null || args.editingText.trim() == "") {
  244. //delete
  245. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  246. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  247. if (args.row < cacheArr.length) {
  248. cacheArr.splice(args.row, 1);
  249. me.updateRationItem();
  250. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  251. me.showGljItems(me.currentRationItem.ID);
  252. }
  253. }
  254. } else*/
  255. if(args.editingText !== null && args.editingText.trim().length !== 0){
  256. let rationRepId = storageUtil.getSessionCache("RationGrp","repositoryID");
  257. let gljLibID = storageUtil.getSessionCache("gljLib", "repositoryID_" + rationRepId);
  258. if (gljLibID) {
  259. var codes = [];
  260. codes.push(args.editingText.trim());
  261. me.addGljItems(codes, gljLibID, args);
  262. }
  263. }
  264. }
  265. },
  266. onContextmenuOpr: function () {
  267. $.contextMenu({
  268. selector: '#rdSpread',
  269. callback: function(key, options) {
  270. var m = "clicked: " + key;
  271. window.console && console.log(m) || alert(m);
  272. },
  273. items: {
  274. "insert": {name: "插入", callback: function (key, opt) {
  275. }},
  276. "delete": {name: "删除"}
  277. }
  278. });
  279. },
  280. addGljItems: function(codes, repId, args) {
  281. var me = this;
  282. $.ajax({
  283. type:"POST",
  284. url:"api/getGljItemsByCodes",
  285. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  286. dataType:"json",
  287. cache:false,
  288. timeout:5000,
  289. success:function(result){
  290. //sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  291. if (result) {
  292. if(result.data.length > 0){
  293. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  294. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  295. for (var i = 0; i < result.data.length; i++) {
  296. dummyR.gljId = result.data[i].ID;
  297. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  298. }
  299. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  300. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  301. for (var i = 0; i < rstArr.length; i++) {
  302. var hasDup = false;
  303. for (var j = 0; j < cacheArr.length; j++) {
  304. if (cacheArr[j].gljId == rstArr[i].gljId) {
  305. hasDup = true;
  306. break;
  307. }
  308. }
  309. if (!hasDup) {
  310. newAddArr.push(rstArr[i]);
  311. }
  312. }
  313. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  314. me.cache["_GLJ_" + me.currentRationItem.ID].sort(function(a, b) {
  315. var rst = 0;
  316. if (a.code > b.code) rst = 1
  317. else if (a.code < b.code) rst = -1;
  318. return rst;
  319. });
  320. }
  321. me.showGljItems(me.currentRationItem.ID);
  322. if (newAddArr.length > 0) {
  323. me.updateRationItem();
  324. }
  325. }
  326. else{
  327. $('#alertModalBtn').click();
  328. $('#alertText').text("工料机"+ codes + "不存在,请查找你所需要的工料机,或新增工料机");
  329. me.sheet.options.isProtected = true;
  330. $('#alertModalCls').click(function () {
  331. sheetCommonObj.lockCells(me.sheet, me.setting);
  332. me.showGljItems(me.currentRationItem.ID);
  333. me.sheet.setValue(args.row, args.col, '');
  334. });
  335. $('#alertModalCof').click(function () {
  336. sheetCommonObj.lockCells(me.sheet, me.setting);
  337. me.showGljItems(me.currentRationItem.ID);
  338. me.sheet.setValue(args.row, args.col, '');
  339. })
  340. }
  341. }
  342. sheetCommonObj.lockCells(me.sheet, me.setting);
  343. },
  344. error:function(err){
  345. alert(err);
  346. }
  347. })
  348. },
  349. rationCal: function () {
  350. let me = this;
  351. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  352. function round(v,e){
  353. var t=1;
  354. for(;e>0;t*=10,e--);
  355. for(;e<0;t/=10,e++);
  356. return Math.round(v*t)/t;
  357. }
  358. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  359. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  360. cacheArr.forEach(function (gljData) {
  361. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  362. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  363. if(parent && parent.data.ID <= 3){
  364. price['gljType' + parent.data.ID].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  365. }
  366. if(!parent && gljData.gljType <= 3){
  367. price['gljType' + gljData.gljType].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  368. }
  369. }
  370. });
  371. if(price.gljType1.length > 0){
  372. let labourPrice = 0;
  373. price.gljType1.forEach(function (singlePrc) {
  374. labourPrice += singlePrc;
  375. });
  376. let roundPrice = round(labourPrice, 2);
  377. rst.labourPrice = roundPrice;
  378. rationBasePrc += roundPrice;
  379. }
  380. if(price.gljType2.length > 0){
  381. let materialPrice = 0;
  382. price.gljType2.forEach(function (singlePrc) {
  383. materialPrice += singlePrc;
  384. });
  385. let roundPrice = round(materialPrice, 2);
  386. rst.materialPrice = roundPrice;
  387. rationBasePrc += roundPrice;
  388. }
  389. if(price.gljType3.length > 0){
  390. let machinePrice = 0;
  391. price.gljType3.forEach(function (singlePrc) {
  392. machinePrice += singlePrc;
  393. });
  394. let roundPrice = round(machinePrice, 2);
  395. rst.machinePrice = roundPrice;
  396. rationBasePrc += roundPrice;
  397. }
  398. rst.rationBasePrc = rationBasePrc;
  399. }
  400. return rst;
  401. },
  402. updateRationItem: function() {
  403. var me = this, updateArr = [];
  404. if (me.currentRationItem) {
  405. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  406. //recalculate ration basePrice
  407. let price = me.rationCal();
  408. me.currentRationItem.labourPrice = price.labourPrice;
  409. me.currentRationItem.materialPrice = price.materialPrice;
  410. me.currentRationItem.machinePrice = price.machinePrice;
  411. me.currentRationItem.basePrice = price.rationBasePrc;
  412. updateArr.push(me.currentRationItem);
  413. rationOprObj.mixUpdateRequest(updateArr, [], []);
  414. }
  415. },
  416. buildRationItemGlj: function(){
  417. var me = this, rst = [];
  418. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  419. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  420. for (var i = 0; i < cacheArr.length; i++) {
  421. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  422. }
  423. }
  424. return rst;
  425. },
  426. createRationGljDisplayItem: function(rItem, repGlj) {
  427. var rst = {};
  428. rst.gljId = rItem.gljId;
  429. rst.consumeAmt = rItem.consumeAmt;
  430. rst.code = repGlj.code;
  431. rst.name = repGlj.name;
  432. rst.specs = repGlj.specs;
  433. rst.unit = repGlj.unit;
  434. rst.basePrice = repGlj.basePrice;
  435. rst.gljType = repGlj.gljType;
  436. return rst;
  437. },
  438. getGljItems: function(rationItem, callback) {
  439. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  440. me.currentRationItem = rationItem;
  441. if (me.cache["_GLJ_" + rationID]) {
  442. me.showGljItems(rationID);
  443. sheetCommonObj.lockCells(me.sheet, me.setting);
  444. } else {
  445. var gljIds = [];
  446. for (var i = 0; i < rationGljList.length; i++) {
  447. gljIds.push(rationGljList[i].gljId);
  448. }
  449. $.ajax({
  450. type:"POST",
  451. url:"api/getGljItemsByIds",
  452. data:{"gljIds": JSON.stringify(gljIds)},
  453. dataType:"json",
  454. cache:false,
  455. timeout:5000,
  456. success:function(result){
  457. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  458. if (result) {
  459. var cacheArr = [];
  460. for (var i = 0; i < result.data.length; i++) {
  461. for (var j = 0; j < rationGljList.length; j++) {
  462. if (rationGljList[j].gljId == result.data[i].ID) {
  463. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  464. break;
  465. }
  466. }
  467. }
  468. function compare(){
  469. return function (a, b) {
  470. let rst = 0;
  471. if (a.code > b.code) {
  472. rst = 1;
  473. }
  474. else if (a.code < b.code) {
  475. rst = -1;
  476. }
  477. return rst;
  478. }
  479. }
  480. cacheArr.sort(compare());
  481. me.cache["_GLJ_" + rationID] = cacheArr;
  482. me.showGljItems(rationID);
  483. }
  484. sheetCommonObj.lockCells(me.sheet, me.setting);
  485. callback();
  486. },
  487. error:function(err){
  488. alert(err);
  489. }
  490. })
  491. }
  492. },
  493. showGljItems: function(rationID) {
  494. var me = this;
  495. if (me.cache["_GLJ_" + rationID]) {
  496. sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  497. }
  498. }
  499. }