gljComponent.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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, icon: "fa-sign-in", callback: function (key, opt) {
  84. //默认radio所有工料机
  85. co.initRadio();
  86. co.gljCurTypeId = null;
  87. //默认点击树根节点
  88. if(co.rootNode){
  89. co.treeObj.selectNode(co.rootNode);
  90. componentTypeTreeOprObj.onClick(null, 'componentTree', co.rootNode);
  91. }
  92. //弹出窗口
  93. $('#componentBtn').click();
  94. }},
  95. "delete": {name: "删除", disabled: delDis, icon: "fa-remove", callback: function (key, opt) {
  96. //删除
  97. let deleteObj = that.currentComponent[target.row];
  98. let gljComponent = that.currentGlj.component;
  99. let updateArr = [];
  100. //更新当前工料机的组成物列表
  101. for(let i = 0, len = gljComponent.length; i < len; i++){
  102. if(gljComponent[i].ID === deleteObj.ID){
  103. gljComponent.splice(i, 1);
  104. break;
  105. }
  106. }
  107. //重新计算工料机
  108. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(gljComponent));
  109. if(gljBasePrc !== that.currentGlj.basePrice){
  110. that.currentGlj.basePrice = gljBasePrc;
  111. that.reshowGljBasePrc(that.currentGlj);
  112. //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  113. }
  114. updateArr.push(that.currentGlj);
  115. me.updateComponent(updateArr);
  116. }}
  117. }
  118. };
  119. }
  120. else{
  121. return false;
  122. }
  123. }
  124. });
  125. },
  126. gljComponentDelOpr: function () {
  127. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [], removeArr = [], isUpdate = false, updateBasePrc= [];
  128. me.workBook.commandManager().register('gljComponentDel', function () {
  129. let sels = me.workBook.getSheet(0).getSelections();
  130. if(sels.length > 0 && that.currentComponent.length > 0){
  131. let component = that.currentGlj.component;
  132. for(let i = 0; i < sels.length > 0; i++){
  133. if(sels[i].colCount === me.setting.header.length){//可删除
  134. for(let j = 0; j < sels[i].rowCount; j++){
  135. if(sels[i].row + j < that.currentComponent.length){
  136. removeArr.push(that.currentComponent[sels[i].row + j].ID);
  137. }
  138. }
  139. }
  140. else if(sels[i].col === 0){
  141. //编码不可为空
  142. alert("编码不可为空!");
  143. }
  144. else if(sels[i].col === 4){//消耗量修改为0
  145. if(sels[i].row === -1){//全修改
  146. for(let j = 0; j < that.currentComponent.length; j++){
  147. isUpdate = true;
  148. that.currentComponent[j].consumeAmt = 0;
  149. for(let k = 0; k < component.length; k++){
  150. if(component[k].ID === that.currentComponent[j].ID){
  151. component[k].consumeAmt = 0;
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. else{//部分修改
  158. for(let j = 0; j < sels[i].rowCount; j++){
  159. if(sels[i].row + j < that.currentComponent.length){
  160. isUpdate = true;
  161. that.currentComponent[sels[i].row + j].consumeAmt = 0;
  162. for(let k = 0; k < component.length; k++){
  163. if(component[k].ID === that.currentComponent[sels[i].row + j].ID){
  164. component[k].consumeAmt = 0;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. if(removeArr.length > 0 || isUpdate){
  174. for(let i = 0; i < removeArr.length; i++){
  175. for(let j = 0; j < that.currentComponent.length; j++){
  176. if(that.currentComponent[j].ID === removeArr[i]){
  177. that.currentComponent.splice(j--, 1);
  178. }
  179. }
  180. for(let j = 0; j < component.length; j++){
  181. if(component[j].ID === removeArr[i]){
  182. component.splice(j--, 1);
  183. }
  184. }
  185. }
  186. //重新计算工料机
  187. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  188. if(gljBasePrc !== that.currentGlj.basePrice){
  189. that.currentGlj.basePrice = gljBasePrc;
  190. that.reshowGljBasePrc(that.currentGlj);
  191. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  192. }
  193. updateArr.push(that.currentGlj);
  194. me.updateComponent(updateArr);
  195. if(updateBasePrc.length > 0){
  196. that.updateRationBasePrcRq(updateBasePrc);
  197. }
  198. }
  199. }
  200. });
  201. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  202. //me.workBook.commandManager().setShortcutKey('gljComponentDel', GC.Spread.Commands.Key.del, false, false, false, false);
  203. },
  204. onCellEditStart: function(sender, args) {
  205. let me = gljComponentOprObj, that = repositoryGljObj;
  206. let rObj = me.getRowData(args.sheet, args.row, me.setting);
  207. me.currentEditingComponent = rObj;
  208. },
  209. onCellEditEnd: function (sender, args) {
  210. let me = gljComponentOprObj, that = repositoryGljObj, updateBasePrc = [];
  211. let gljList = that.gljList, updateArr = [], materialComponent = [202, 203, 204], machineComponent = [302, 303];
  212. // if(args.editingText !== me.currentEditingComponent.code){
  213. //编码
  214. /* if(args.col === 0 && args.editingText && args.editingText.trim().length > 0 &&args.editingText !== me.currentEditingComponent.code){
  215. let component = that.currentGlj.component, hasCode = false;
  216. for(let i = 0; i < gljList.length; i++){
  217. if(gljList[i].code === args.editingText){//有效的组成物
  218. hasCode = true;
  219. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljList[i].gljType === 201)
  220. || (that.currentGlj.gljType === 301 && machineComponent.indexOf(gljList[i].gljType) !== -1 )){//普通材料
  221. //是否与原有组成物不同
  222. let isExist = false;
  223. for(let j = 0; j < component.length; j++){
  224. if(component[j].ID === gljList[i].ID){
  225. isExist = true;
  226. break;
  227. }
  228. }
  229. if(!isExist){
  230. let rObj = {};
  231. rObj.ID = gljList[i].ID;
  232. //rObj.basePrice = gljList[i].basePrice;
  233. if(typeof that.currentComponent[args.row] !== 'undefined'){
  234. rObj.consumeAmt = that.currentComponent[args.row].consumeAmt;
  235. let index;
  236. for(let j = 0; j < component.length; j++){
  237. if(component[j].ID === that.currentComponent[args.row].ID){
  238. index = j;
  239. break;
  240. }
  241. }
  242. component.splice(index, 1);
  243. component.splice(index, 0, rObj);
  244. //计算工料机单价
  245. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
  246. if(gljBasePrc !== that.currentGlj.basePrice){
  247. that.currentGlj.basePrice = gljBasePrc;
  248. that.reshowGljBasePrc(that.currentGlj);
  249. //工料机单价改变,重算引用了该工料机的定额单价
  250. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  251. }
  252. updateArr.push(that.currentGlj);
  253. }
  254. else{
  255. rObj.consumeAmt = 0;
  256. component.push(rObj);
  257. //计算工料机单价
  258. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
  259. if(gljBasePrc !== that.currentGlj.basePrice){
  260. that.currentGlj.basePrice = gljBasePrc;
  261. that.reshowGljBasePrc(that.currentGlj);
  262. //工料机单价改变,重算引用了该工料机的定额单价
  263. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  264. }
  265. updateArr.push(that.currentGlj);
  266. }
  267. break;
  268. }
  269. else{
  270. //已存在
  271. alert("已存在!");
  272. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  273. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  274. }
  275. }
  276. else{
  277. if(materialComponent.indexOf(that.currentGlj.gljType) === 1){
  278. alert("该组成物只能是普通材料!");
  279. }
  280. else if(that.currentGlj.gljType === 301){
  281. alert("该组成物只能是机械组成物或机上人工!")
  282. }
  283. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  284. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  285. //无效
  286. }
  287. }
  288. }
  289. if(!hasCode){
  290. alert("不存在此工料机,请先添加!");
  291. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  292. me.currentEditingComponent[me.setting.header[args.col].dataCode] : '');
  293. //不存在
  294. }
  295. }*/
  296. if(args.col === 4 && me.currentEditingComponent.code && args.editingText && args.editingText.trim().length > 0){//消耗量
  297. let consumeAmt = parseFloat(args.editingText);
  298. if(!isNaN(consumeAmt) && consumeAmt !== me.currentEditingComponent.consumeAmt){
  299. let roundCons = me.round(consumeAmt, 3);
  300. let component = that.currentGlj.component;
  301. for(let i = 0; i < component.length; i++){
  302. if(component[i].ID === that.currentComponent[args.row].ID){
  303. component[i].consumeAmt = roundCons;
  304. }
  305. }
  306. that.currentComponent[args.row].consumeAmt = roundCons;
  307. //计算工料机单价
  308. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  309. if(gljBasePrc !== that.currentGlj.basePrice){
  310. that.currentGlj.basePrice = gljBasePrc;
  311. that.reshowGljBasePrc(that.currentGlj);
  312. //工料机单价改变,重算引用了该工料机的定额单价
  313. //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  314. }
  315. updateArr.push(that.currentGlj);
  316. }
  317. else{
  318. //只能输入数值
  319. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  320. me.currentEditingComponent[me.setting.header[args.col].dataCode]: 0);
  321. }
  322. }
  323. else{
  324. args.sheet.setValue(args.row, args.col, me.currentEditingComponent.consumeAmt);
  325. }
  326. if(updateArr.length > 0){
  327. me.updateComponent(updateArr);
  328. /*if(updateBasePrc.length > 0){
  329. that.updateRationBasePrcRq(updateBasePrc)
  330. }*/
  331. }
  332. },
  333. onClipboardPasting: function (sender, info) {
  334. let me = gljComponentOprObj;
  335. let maxCol = info.cellRange.col + info.cellRange.colCount - 1;
  336. //复制的列数超过正确的列数,不可复制
  337. if(maxCol >= me.setting.header.length){
  338. args.cancel = true;
  339. }
  340. },
  341. onClipboardPasted: function (sender, info) {
  342. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [] ,materialComponent = [202, 203, 204], machineComponent = [302, 303],
  343. component = that.currentGlj.component, newComponent = [], concatComponent = [], isChange = false, updateBasePrc = [];
  344. let items = sheetOpr.analyzePasteData(me.setting, info);
  345. let gljCache = that.gljList;
  346. //编码
  347. /* if(info.cellRange.col === 0){
  348. for(let i = 0; i < items.length; i++){
  349. for(let j = 0; j < gljCache.length; j++){
  350. if(items[i].code === gljCache[j].code){
  351. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljCache[j].gljType === 201)
  352. || (that.currentGlj.gljType === 301 && machineComponent.indexOf(gljCache[j].gljType) !== -1 )){
  353. //是否与原有组成物不同
  354. let isExist = false;
  355. for(let k = 0; k < component.length; k++){
  356. if(component[k].ID === gljCache[j].ID){
  357. isExist = true;
  358. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  359. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  360. break;
  361. }
  362. }
  363. if(!isExist){
  364. isChange = true;
  365. let obj = {};
  366. obj.ID = gljCache[j].ID;
  367. if(typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'){//更新
  368. obj.consumeAmt = that.currentComponent[info.cellRange.row + i].consumeAmt;
  369. let index;
  370. for(let k = 0; k < component.length; k++){
  371. if(that.currentComponent[info.cellRange.row + i].ID === component[k].ID){
  372. index = k;
  373. break;
  374. }
  375. }
  376. component.splice(index, 1);
  377. component.splice(index, 0, obj);
  378. }
  379. else{//新增
  380. obj.consumeAmt = 0;
  381. component.push(obj);
  382. }
  383. break;
  384. }
  385. }
  386. else{
  387. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  388. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  389. }
  390. }
  391. else{
  392. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  393. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  394. }
  395. }
  396. }
  397. if(isChange){
  398. //计算工料机单价
  399. let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
  400. if(gljBasePrc !== that.currentGlj.basePrice){
  401. that.currentGlj.basePrice = gljBasePrc;
  402. that.reshowGljBasePrc(that.currentGlj);
  403. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  404. }
  405. updateArr.push(that.currentGlj);
  406. }
  407. }*/
  408. //消耗量
  409. if(info.cellRange.col === 4){
  410. let items = sheetOpr.analyzePasteData(me.setting, info);
  411. let row = info.cellRange.row;
  412. for(let i = 0; i < items.length; i++){
  413. if(row + i < that.currentComponent.length){
  414. let currentObj = that.currentComponent[row + i];
  415. if(items[i].consumeAmt.trim().length > 0 && items[i].consumeAmt !== currentObj.consumeAmt){
  416. let roundCons = me.round(items[i].consumeAmt, 3);
  417. isChange = true;
  418. currentObj.consumeAmt = roundCons;
  419. for(let j = 0; j < component.length; j++){
  420. if(component[j].ID === currentObj.ID){
  421. component[j].consumeAmt = currentObj.consumeAmt;
  422. break;
  423. }
  424. }
  425. }
  426. else{
  427. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, currentObj.consumeAmt);
  428. }
  429. }
  430. else{
  431. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, '');
  432. }
  433. }
  434. if(isChange){
  435. //计算工料机单价
  436. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  437. if(gljBasePrc !== that.currentGlj.basePrice){
  438. that.currentGlj.basePrice = gljBasePrc;
  439. that.reshowGljBasePrc(that.currentGlj);
  440. //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  441. }
  442. updateArr.push(that.currentGlj);
  443. }
  444. }
  445. if(updateArr.length > 0){
  446. me.updateComponent(updateArr);
  447. /* if(updateBasePrc.length > 0){
  448. that.updateRationBasePrcRq(updateBasePrc);
  449. }*/
  450. }
  451. },
  452. updateComponent: function (updateArr) {
  453. let me = gljComponentOprObj, that = repositoryGljObj;
  454. $.ajax({
  455. type: 'post',
  456. url: 'complementartGlj/api/updateComponent',
  457. data: {"userId": pageOprObj.userId, "updateArr": JSON.stringify(updateArr)},
  458. dataType: 'json',
  459. success: function (result) {
  460. if(!result.error){
  461. that.currentComponent = that.getCurrentComponent(result.data[0].component);
  462. sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  463. sheetOpr.showData(me.workBook.getSheet(0), me.setting, that.currentComponent);
  464. }
  465. else{
  466. sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  467. }
  468. $('#componentsCacnel').click();
  469. }
  470. })
  471. },
  472. round: function (v, e) {
  473. let t=1;
  474. for(;e>0;t*=10,e--);
  475. for(;e<0;t/=10,e++);
  476. return Math.round(v*t)/t;
  477. },
  478. reCalGljBasePrc: function (component) {
  479. let me = gljComponentOprObj, gljBasePrc = 0;
  480. for(let i = 0; i < component.length; i++){
  481. let roundBasePrc = me.round(component[i].basePrice, 2);
  482. gljBasePrc += me.round(roundBasePrc * component[i].consumeAmt, 2);
  483. }
  484. return gljBasePrc;
  485. }
  486. };