ration_glj.js 25 KB

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