gljComponent.js 26 KB

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