gljComponent.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. var 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){
  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 === 3 && 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. updateArr.push(that.currentGlj);
  173. }
  174. else{
  175. rObj.consumeAmt = 0;
  176. component.push(rObj);
  177. updateArr.push(that.currentGlj);
  178. }
  179. break;
  180. }
  181. else{
  182. //已存在
  183. alert("已存在!");
  184. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  185. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  186. }
  187. }
  188. else{
  189. if(materialComponent.indexOf(that.currentGlj.gljType) === 1){
  190. alert("该组成物只能是普通材料!");
  191. }
  192. else if(that.currentGlj.gljType === 3){
  193. alert("该组成物只能是机械组成物或机上人工!")
  194. }
  195. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  196. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  197. //无效
  198. }
  199. }
  200. }
  201. if(!hasCode){
  202. alert("不存在此工料机,请先添加!");
  203. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  204. me.currentEditingComponent[me.setting.header[args.col].dataCode] : '');
  205. //不存在
  206. }
  207. }
  208. else if(args.col === 4 && me.currentEditingComponent.code && args.editingText && args.editingText.trim().length > 0){//消耗量
  209. let consumeAmt = parseFloat(args.editingText);
  210. if(!isNaN(consumeAmt) && consumeAmt !== me.currentEditingComponent.consumeAmt){
  211. let component = that.currentGlj.component;
  212. for(let i = 0; i < component.length; i++){
  213. if(component[i].ID === that.currentComponent[args.row].ID){
  214. component[i].consumeAmt = consumeAmt
  215. }
  216. }
  217. that.currentComponent[args.row].consumeAmt = consumeAmt;
  218. //计算工料机单价
  219. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  220. if(gljBasePrc !== that.currentGlj.basePrice){
  221. that.currentGlj.basePrice = gljBasePrc;
  222. that.reshowGljBasePrc(that.currentGlj);
  223. //工料机单价改变,重算引用了该工料机的定额单价
  224. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  225. }
  226. updateArr.push(that.currentGlj);
  227. }
  228. else{
  229. //只能输入数值
  230. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  231. me.currentEditingComponent[me.setting.header[args.col].dataCode]: 0);
  232. }
  233. }
  234. else{
  235. args.sheet.setValue(args.row, args.col, '');
  236. }
  237. }
  238. if(updateArr.length > 0){
  239. me.updateComponent(updateArr);
  240. if(updateBasePrc.length > 0){
  241. that.updateRationBasePrcRq(updateBasePrc)
  242. }
  243. }
  244. },
  245. onClipboardPasting: function (sender, info) {
  246. },
  247. onClipboardPasted: function (sender, info) {
  248. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [] ,materialComponent = [202, 203, 204], machineComponent = [302, 303],
  249. component = that.currentGlj.component, newComponent = [], concatComponent = [], isChange = false, updateBasePrc = [];
  250. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  251. let gljCache = that.gljList;
  252. if(info.cellRange.col === 0){
  253. for(let i = 0; i < items.length; i++){
  254. for(let j = 0; j < gljCache.length; j++){
  255. if(items[i].code === gljCache[j].code){
  256. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljCache[j].gljType === 201)
  257. || (that.currentGlj.gljType === 3 && machineComponent.indexOf(gljCache[j].gljType) !== -1 )){
  258. //是否与原有组成物不同
  259. let isExist = false;
  260. for(let k = 0; k < component.length; k++){
  261. if(component[k].ID === gljCache[j].ID){
  262. isExist = true;
  263. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  264. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  265. break;
  266. }
  267. }
  268. if(!isExist){
  269. isChange = true;
  270. let obj = {};
  271. obj.ID = gljCache[j].ID;
  272. if(typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'){//更新
  273. obj.consumeAmt = that.currentComponent[info.cellRange.row + i].consumeAmt;
  274. let index;
  275. for(let k = 0; k < component.length; k++){
  276. if(that.currentComponent[info.cellRange.row + i].ID === component[k].ID){
  277. index = k;
  278. break;
  279. }
  280. }
  281. component.splice(index, 1);
  282. component.splice(index, 0, obj);
  283. }
  284. else{//新增
  285. obj.consumeAmt = 0;
  286. component.push(obj);
  287. }
  288. break;
  289. }
  290. }
  291. else{
  292. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  293. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  294. }
  295. }
  296. else{
  297. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  298. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  299. }
  300. }
  301. }
  302. if(isChange){
  303. //计算工料机单价
  304. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  305. if(gljBasePrc !== that.currentGlj.basePrice){
  306. that.currentGlj.basePrice = gljBasePrc;
  307. that.reshowGljBasePrc(that.currentGlj);
  308. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  309. }
  310. updateArr.push(that.currentGlj);
  311. }
  312. }
  313. else if(info.cellRange.col === 4){
  314. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  315. let row = info.cellRange.row;
  316. for(let i = 0; i < items.length; i++){
  317. if(row + i < that.currentComponent.length){
  318. let currentObj = that.currentComponent[row + i];
  319. if(items[i].consumeAmt.trim().length > 0 && items[i].consumeAmt !== currentObj.consumeAmt){
  320. isChange = true;
  321. currentObj.consumeAmt = items[i].consumeAmt;
  322. for(let j = 0; j < component.length; j++){
  323. if(component[j].ID === currentObj.ID){
  324. component[j].consumeAmt = currentObj.consumeAmt;
  325. break;
  326. }
  327. }
  328. }
  329. else{
  330. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, currentObj.consumeAmt);
  331. }
  332. }
  333. else{
  334. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, '');
  335. }
  336. }
  337. if(isChange){
  338. //计算工料机单价
  339. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  340. if(gljBasePrc !== that.currentGlj.basePrice){
  341. that.currentGlj.basePrice = gljBasePrc;
  342. that.reshowGljBasePrc(that.currentGlj);
  343. updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
  344. }
  345. updateArr.push(that.currentGlj);
  346. }
  347. }
  348. if(updateArr.length > 0){
  349. me.updateComponent(updateArr);
  350. if(updateBasePrc.length > 0){
  351. that.updateRationBasePrcRq(updateBasePrc);
  352. }
  353. }
  354. },
  355. updateComponent: function (updateArr) {
  356. let me = gljComponentOprObj, that = repositoryGljObj;
  357. $.ajax({
  358. type: 'post',
  359. url: 'api/updateComponent',
  360. data: {libId: pageOprObj.gljLibId, updateArr: updateArr, oprtor: userAccount},
  361. dataType: 'json',
  362. success: function (result) {
  363. console.log(result);
  364. if(result.data.length > 0){
  365. if(result.data[0]){
  366. that.currentComponent = that.getCurrentComponent(result.data[0].component);
  367. console.log(that.currentComponent);
  368. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  369. sheetCommonObj.showData(me.workBook.getSheet(0), me.setting, that.currentComponent);
  370. }
  371. }
  372. }
  373. })
  374. },
  375. round: function (v, e) {
  376. var t=1;
  377. for(;e>0;t*=10,e--);
  378. for(;e<0;t/=10,e++);
  379. return Math.round(v*t)/t;
  380. },
  381. reCalGljBasePrc: function (component) {
  382. let me = gljComponentOprObj, gljBasePrc = 0;
  383. for(let i = 0; i < component.length; i++){
  384. gljBasePrc += me.round(component[i].basePrice * component[i].consumeAmt, 2);
  385. }
  386. return gljBasePrc;
  387. }
  388. };