gljComponent.js 27 KB

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