gljComponent.js 27 KB

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