gljComponent.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * Created by Zhong on 2017/8/15.
  3. */
  4. let gljComponentOprObj = {
  5. workBook: null,
  6. setting: {
  7. owner: "gljComponent",
  8. header:[
  9. {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  10. {headerName:"名称",headerWidth:110,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
  11. {headerName:"计量单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
  12. {headerName:"单价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
  13. {headerName:"消耗量",headerWidth:80,dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", hAlign: "right", vAlign: "center"}
  14. ],
  15. view: {
  16. lockedCells:[0, 1, 2, 3]
  17. }
  18. },
  19. buildSheet: function(container) {
  20. let me = gljComponentOprObj;
  21. me.workBook = sheetOpr.buildSheet(container, me.setting, 30);
  22. me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  23. me.workBook.getSheet(0).setFormatter(-1, 0, "@", GC.Spread.Sheets.SheetArea.viewport);
  24. me.workBook.getSheet(0).options.isProtected = true;
  25. sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  26. me.onContextmenuOpr();//右键菜单
  27. me.gljComponentDelOpr();
  28. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  29. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  30. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  31. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  32. /*me.gljComponentDelOpr();
  33. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  34. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  35. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  36. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);*/
  37. },
  38. getRowData: function (sheet, row, setting) {
  39. let rst = {};
  40. for(let i = 0; i < setting.header.length; i++){
  41. rst[setting.header[i].dataCode] = sheet.getValue(row, i);
  42. }
  43. return rst;
  44. },
  45. getComponent: function (sheet, rowCount) {
  46. let component = [];
  47. for(let row = 0; row < rowCount; row++){
  48. let obj = {};
  49. obj.consumeAmt = sheet.getValue(row, 4);
  50. obj.ID = sheet.getTag(row, 0);
  51. component.push(obj);
  52. }
  53. return component;
  54. },
  55. onContextmenuOpr: function () {
  56. let me = gljComponentOprObj, that = repositoryGljObj, co = componentOprObj;
  57. $.contextMenu({
  58. selector: '#gljComponentSheet',
  59. build: function($triggerElement, e){
  60. //控制允许右键菜单在哪个位置出现
  61. let clientX = e.originalEvent.clientX,
  62. clientY = e.originalEvent.clientY;
  63. let sheet = me.workBook.getSheet(0);
  64. let offset = $("#gljComponentSheet").offset(),
  65. x = clientX - offset.left,
  66. y = clientY - offset.top;
  67. let target = sheet.hitTest(x, y);
  68. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  69. sheet.setActiveCell(target.row, target.col);
  70. //控制按钮是否可用
  71. let insertDis = false,
  72. delDis = false;
  73. if(me.isLocked){
  74. insertDis = true;
  75. delDis = true;
  76. }
  77. if(typeof that.currentComponent !== 'undefined' && target.row >= that.currentComponent.length){//右键定位在有组成物的行,删除键才显示可用
  78. delDis = true;
  79. }
  80. return {
  81. callback: function(){},
  82. items: {
  83. "insert": {name: "插入", disabled: insertDis, callback: function (key, opt) {
  84. //默认radio所有工料机
  85. co.initRadio();
  86. //默认点击树根节点
  87. if(co.rootNode){
  88. co.treeObj.selectNode(co.rootNode);
  89. componentTypeTreeOprObj.onClick(null, 'componentTree', co.rootNode);
  90. }
  91. //弹出窗口
  92. $('#componentBtn').click();
  93. }},
  94. "delete": {name: "删除", disabled: delDis, callback: function (key, opt) {
  95. //删除
  96. let deleteObj = that.currentComponent[target.row];
  97. let gljComponent = that.currentGlj.component;
  98. let updateArr = [];
  99. //更新当前工料机的组成物列表
  100. for(let i = 0, len = gljComponent.length; i < len; i++){
  101. if(gljComponent[i].ID === deleteObj.ID){
  102. gljComponent.splice(i, 1);
  103. break;
  104. }
  105. }
  106. //重新计算工料机
  107. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(gljComponent));
  108. if(gljBasePrc !== that.currentGlj.basePrice){
  109. that.currentGlj.basePrice = gljBasePrc;
  110. that.reshowGljBasePrc(that.currentGlj);
  111. //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  112. }
  113. updateArr.push(that.currentGlj);
  114. me.updateComponent(updateArr);
  115. }}
  116. }
  117. };
  118. }
  119. else{
  120. return false;
  121. }
  122. }
  123. });
  124. },
  125. gljComponentDelOpr: function () {
  126. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [], removeArr = [], isUpdate = false, updateBasePrc= [];
  127. me.workBook.commandManager().register('gljComponentDel', function () {
  128. let sels = me.workBook.getSheet(0).getSelections();
  129. if(sels.length > 0 && that.currentComponent.length > 0){
  130. let component = that.currentGlj.component;
  131. for(let i = 0; i < sels.length > 0; i++){
  132. if(sels[i].colCount === me.setting.header.length){//可删除
  133. for(let j = 0; j < sels[i].rowCount; j++){
  134. if(sels[i].row + j < that.currentComponent.length){
  135. removeArr.push(that.currentComponent[sels[i].row + j].ID);
  136. }
  137. }
  138. }
  139. else if(sels[i].col === 0){
  140. //编码不可为空
  141. alert("编码不可为空!");
  142. }
  143. else if(sels[i].col === 4){//消耗量修改为0
  144. if(sels[i].row === -1){//全修改
  145. for(let j = 0; j < that.currentComponent.length; j++){
  146. isUpdate = true;
  147. that.currentComponent[j].consumeAmt = 0;
  148. for(let k = 0; k < component.length; k++){
  149. if(component[k].ID === that.currentComponent[j].ID){
  150. component[k].consumeAmt = 0;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. else{//部分修改
  157. for(let j = 0; j < sels[i].rowCount; j++){
  158. if(sels[i].row + j < that.currentComponent.length){
  159. isUpdate = true;
  160. that.currentComponent[sels[i].row + j].consumeAmt = 0;
  161. for(let k = 0; k < component.length; k++){
  162. if(component[k].ID === that.currentComponent[sels[i].row + j].ID){
  163. component[k].consumeAmt = 0;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. if(removeArr.length > 0 || isUpdate){
  173. for(let i = 0; i < removeArr.length; i++){
  174. for(let j = 0; j < that.currentComponent.length; j++){
  175. if(that.currentComponent[j].ID === removeArr[i]){
  176. that.currentComponent.splice(j--, 1);
  177. }
  178. }
  179. for(let j = 0; j < component.length; j++){
  180. if(component[j].ID === removeArr[i]){
  181. component.splice(j--, 1);
  182. }
  183. }
  184. }
  185. //重新计算工料机
  186. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  187. if(gljBasePrc !== that.currentGlj.basePrice){
  188. that.currentGlj.basePrice = gljBasePrc;
  189. that.reshowGljBasePrc(that.currentGlj);
  190. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  191. }
  192. updateArr.push(that.currentGlj);
  193. me.updateComponent(updateArr);
  194. if(updateBasePrc.length > 0){
  195. that.updateRationBasePrcRq(updateBasePrc);
  196. }
  197. }
  198. }
  199. });
  200. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  201. //me.workBook.commandManager().setShortcutKey('gljComponentDel', GC.Spread.Commands.Key.del, false, false, false, false);
  202. },
  203. onCellEditStart: function(sender, args) {
  204. let me = gljComponentOprObj, that = repositoryGljObj;
  205. let rObj = me.getRowData(args.sheet, args.row, me.setting);
  206. me.currentEditingComponent = rObj;
  207. },
  208. onCellEditEnd: function (sender, args) {
  209. let me = gljComponentOprObj, that = repositoryGljObj, updateBasePrc = [];
  210. let gljList = that.gljList, updateArr = [], materialComponent = [202, 203, 204], machineComponent = [302, 303];
  211. // if(args.editingText !== me.currentEditingComponent.code){
  212. //编码
  213. /* if(args.col === 0 && args.editingText && args.editingText.trim().length > 0 &&args.editingText !== me.currentEditingComponent.code){
  214. let component = that.currentGlj.component, hasCode = false;
  215. for(let i = 0; i < gljList.length; i++){
  216. if(gljList[i].code === args.editingText){//有效的组成物
  217. hasCode = true;
  218. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljList[i].gljType === 201)
  219. || (that.currentGlj.gljType === 301 && machineComponent.indexOf(gljList[i].gljType) !== -1 )){//普通材料
  220. //是否与原有组成物不同
  221. let isExist = false;
  222. for(let j = 0; j < component.length; j++){
  223. if(component[j].ID === gljList[i].ID){
  224. isExist = true;
  225. break;
  226. }
  227. }
  228. if(!isExist){
  229. let rObj = {};
  230. rObj.ID = gljList[i].ID;
  231. //rObj.basePrice = gljList[i].basePrice;
  232. if(typeof that.currentComponent[args.row] !== 'undefined'){
  233. rObj.consumeAmt = that.currentComponent[args.row].consumeAmt;
  234. let index;
  235. for(let j = 0; j < component.length; j++){
  236. if(component[j].ID === that.currentComponent[args.row].ID){
  237. index = j;
  238. break;
  239. }
  240. }
  241. component.splice(index, 1);
  242. component.splice(index, 0, rObj);
  243. //计算工料机单价
  244. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
  245. if(gljBasePrc !== that.currentGlj.basePrice){
  246. that.currentGlj.basePrice = gljBasePrc;
  247. that.reshowGljBasePrc(that.currentGlj);
  248. //工料机单价改变,重算引用了该工料机的定额单价
  249. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  250. }
  251. updateArr.push(that.currentGlj);
  252. }
  253. else{
  254. rObj.consumeAmt = 0;
  255. component.push(rObj);
  256. //计算工料机单价
  257. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
  258. if(gljBasePrc !== that.currentGlj.basePrice){
  259. that.currentGlj.basePrice = gljBasePrc;
  260. that.reshowGljBasePrc(that.currentGlj);
  261. //工料机单价改变,重算引用了该工料机的定额单价
  262. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  263. }
  264. updateArr.push(that.currentGlj);
  265. }
  266. break;
  267. }
  268. else{
  269. //已存在
  270. alert("已存在!");
  271. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  272. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  273. }
  274. }
  275. else{
  276. if(materialComponent.indexOf(that.currentGlj.gljType) === 1){
  277. alert("该组成物只能是普通材料!");
  278. }
  279. else if(that.currentGlj.gljType === 301){
  280. alert("该组成物只能是机械组成物或机上人工!")
  281. }
  282. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  283. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  284. //无效
  285. }
  286. }
  287. }
  288. if(!hasCode){
  289. alert("不存在此工料机,请先添加!");
  290. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  291. me.currentEditingComponent[me.setting.header[args.col].dataCode] : '');
  292. //不存在
  293. }
  294. }*/
  295. if(args.col === 4 && me.currentEditingComponent.code && args.editingText && args.editingText.trim().length > 0){//消耗量
  296. let consumeAmt = parseFloat(args.editingText);
  297. if(!isNaN(consumeAmt) && consumeAmt !== me.currentEditingComponent.consumeAmt){
  298. let roundCons = me.round(consumeAmt, 3);
  299. let component = that.currentGlj.component;
  300. for(let i = 0; i < component.length; i++){
  301. if(component[i].ID === that.currentComponent[args.row].ID){
  302. component[i].consumeAmt = roundCons;
  303. }
  304. }
  305. that.currentComponent[args.row].consumeAmt = roundCons;
  306. //计算工料机单价
  307. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  308. if(gljBasePrc !== that.currentGlj.basePrice){
  309. that.currentGlj.basePrice = gljBasePrc;
  310. that.reshowGljBasePrc(that.currentGlj);
  311. //工料机单价改变,重算引用了该工料机的定额单价
  312. //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  313. }
  314. updateArr.push(that.currentGlj);
  315. }
  316. else{
  317. //只能输入数值
  318. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  319. me.currentEditingComponent[me.setting.header[args.col].dataCode]: 0);
  320. }
  321. }
  322. else{
  323. args.sheet.setValue(args.row, args.col, me.currentEditingComponent.consumeAmt);
  324. }
  325. if(updateArr.length > 0){
  326. me.updateComponent(updateArr);
  327. /*if(updateBasePrc.length > 0){
  328. that.updateRationBasePrcRq(updateBasePrc)
  329. }*/
  330. }
  331. },
  332. onClipboardPasting: function (sender, info) {
  333. let me = gljComponentOprObj;
  334. let maxCol = info.cellRange.col + info.cellRange.colCount - 1;
  335. //复制的列数超过正确的列数,不可复制
  336. if(maxCol >= me.setting.header.length){
  337. args.cancel = true;
  338. }
  339. },
  340. onClipboardPasted: function (sender, info) {
  341. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [] ,materialComponent = [202, 203, 204], machineComponent = [302, 303],
  342. component = that.currentGlj.component, newComponent = [], concatComponent = [], isChange = false, updateBasePrc = [];
  343. let items = sheetOpr.analyzePasteData(me.setting, info);
  344. let gljCache = that.gljList;
  345. //编码
  346. /* if(info.cellRange.col === 0){
  347. for(let i = 0; i < items.length; i++){
  348. for(let j = 0; j < gljCache.length; j++){
  349. if(items[i].code === gljCache[j].code){
  350. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljCache[j].gljType === 201)
  351. || (that.currentGlj.gljType === 301 && machineComponent.indexOf(gljCache[j].gljType) !== -1 )){
  352. //是否与原有组成物不同
  353. let isExist = false;
  354. for(let k = 0; k < component.length; k++){
  355. if(component[k].ID === gljCache[j].ID){
  356. isExist = true;
  357. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  358. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  359. break;
  360. }
  361. }
  362. if(!isExist){
  363. isChange = true;
  364. let obj = {};
  365. obj.ID = gljCache[j].ID;
  366. if(typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'){//更新
  367. obj.consumeAmt = that.currentComponent[info.cellRange.row + i].consumeAmt;
  368. let index;
  369. for(let k = 0; k < component.length; k++){
  370. if(that.currentComponent[info.cellRange.row + i].ID === component[k].ID){
  371. index = k;
  372. break;
  373. }
  374. }
  375. component.splice(index, 1);
  376. component.splice(index, 0, obj);
  377. }
  378. else{//新增
  379. obj.consumeAmt = 0;
  380. component.push(obj);
  381. }
  382. break;
  383. }
  384. }
  385. else{
  386. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  387. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  388. }
  389. }
  390. else{
  391. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  392. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  393. }
  394. }
  395. }
  396. if(isChange){
  397. //计算工料机单价
  398. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
  399. if(gljBasePrc !== that.currentGlj.basePrice){
  400. that.currentGlj.basePrice = gljBasePrc;
  401. that.reshowGljBasePrc(that.currentGlj);
  402. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  403. }
  404. updateArr.push(that.currentGlj);
  405. }
  406. }*/
  407. //消耗量
  408. if(info.cellRange.col === 4){
  409. let items = sheetOpr.analyzePasteData(me.setting, info);
  410. let row = info.cellRange.row;
  411. for(let i = 0; i < items.length; i++){
  412. if(row + i < that.currentComponent.length){
  413. let currentObj = that.currentComponent[row + i];
  414. if(items[i].consumeAmt.trim().length > 0 && items[i].consumeAmt !== currentObj.consumeAmt){
  415. let roundCons = me.round(items[i].consumeAmt, 3);
  416. isChange = true;
  417. currentObj.consumeAmt = roundCons;
  418. for(let j = 0; j < component.length; j++){
  419. if(component[j].ID === currentObj.ID){
  420. component[j].consumeAmt = currentObj.consumeAmt;
  421. break;
  422. }
  423. }
  424. }
  425. else{
  426. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, currentObj.consumeAmt);
  427. }
  428. }
  429. else{
  430. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, '');
  431. }
  432. }
  433. if(isChange){
  434. //计算工料机单价
  435. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  436. if(gljBasePrc !== that.currentGlj.basePrice){
  437. that.currentGlj.basePrice = gljBasePrc;
  438. that.reshowGljBasePrc(that.currentGlj);
  439. //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  440. }
  441. updateArr.push(that.currentGlj);
  442. }
  443. }
  444. if(updateArr.length > 0){
  445. me.updateComponent(updateArr);
  446. /* if(updateBasePrc.length > 0){
  447. that.updateRationBasePrcRq(updateBasePrc);
  448. }*/
  449. }
  450. },
  451. updateComponent: function (updateArr) {
  452. let me = gljComponentOprObj, that = repositoryGljObj;
  453. $.ajax({
  454. type: 'post',
  455. url: 'complementartGlj/api/updateComponent',
  456. data: {"userId": pageOprObj.userId, "updateArr": JSON.stringify(updateArr)},
  457. dataType: 'json',
  458. success: function (result) {
  459. if(!result.error){
  460. that.currentComponent = that.getCurrentComponent(result.data[0].component);
  461. sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  462. sheetOpr.showData(me.workBook.getSheet(0), me.setting, that.currentComponent);
  463. }
  464. else{
  465. sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  466. }
  467. $('#componentsCacnel').click();
  468. }
  469. })
  470. },
  471. round: function (v, e) {
  472. let t=1;
  473. for(;e>0;t*=10,e--);
  474. for(;e<0;t/=10,e++);
  475. return Math.round(v*t)/t;
  476. },
  477. reCalGljBasePrc: function (component) {
  478. let me = gljComponentOprObj, gljBasePrc = 0;
  479. for(let i = 0; i < component.length; i++){
  480. let roundBasePrc = me.round(component[i].basePrice, 2);
  481. gljBasePrc += me.round(roundBasePrc * component[i].consumeAmt, 2);
  482. }
  483. return gljBasePrc;
  484. }
  485. };