gljComponent.js 22 KB

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