gljComponent.js 24 KB

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