gljComponent.js 22 KB

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