Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.86 KB | None | 0 0
  1. import {Component, Inject, OnInit} from '@angular/core';
  2. import {MAT_DIALOG_DATA, MatAutocompleteSelectedEvent, MatDialog, MatDialogRef, MatSnackBar} from '@angular/material';
  3. import {EmployeeService} from '../../../core/services/employee.service';
  4. import {EmployeeOfficeModel} from '../../../shared/models/employee-office.model';
  5. import {TreeNode} from '../../../shared/models/tree-node-properties.model';
  6. import {EmployeeMasterModel} from '../../../shared/models/employee-master.model';
  7. import {OfficeUnit} from '../../../shared/models/office-unit.model';
  8. import {OfficeUnitPostModel} from '../../../shared/models/office-unit-post.model';
  9. import {OfficeUnitService} from '../../../../app/core/services/office-unit.service';
  10. import {Office} from '../../../shared/models/office.model';
  11. import {Observable} from 'rxjs';
  12. import {FormControl} from '@angular/forms';
  13. import {map, startWith} from 'rxjs/operators';
  14. import {OfficeService} from '../../../core/services/office.service';
  15. import {IHash} from '../../theme/typography.component';
  16. import {Tree} from '../../../shared/response-body/tree';
  17. import {SearchEmployeeModel} from '../../../shared/models/search-employee.model';
  18. import {EmployeeInfoModel} from '../../../shared/models/employee-info.model';
  19.  
  20. @Component({
  21. selector: 'app-create-assign-employee',
  22. templateUrl: './create-assign-employee.component.html',
  23. styleUrls: ['./create-assign-employee.component.scss']
  24. })
  25. export class CreateAssignEmployeeComponent implements OnInit {
  26. masterModel: EmployeeMasterModel = new EmployeeMasterModel();
  27. empOptions: EmployeeInfoModel [];
  28. singleEmpInfo: EmployeeInfoModel;
  29. empOffice: EmployeeOfficeModel = new EmployeeOfficeModel();
  30. empFilteredOptions: Observable<EmployeeInfoModel []>;
  31. myEmpControl = new FormControl();
  32. officeList: Array<Office>;
  33. myControl = new FormControl();
  34. options: Office[];
  35. filteredOptions: Observable<Office[]>;
  36. NameToOid: IHash;
  37. OidName: string;
  38. treeNode: TreeNode;
  39. office: Office;
  40. backEndTree: Tree;
  41. OfficeOid: string;
  42.  
  43. constructor(protected dialogRef: MatDialogRef<CreateAssignEmployeeComponent>,
  44. protected empService: EmployeeService,
  45. protected unitService: OfficeUnitService,
  46. protected officeService: OfficeService,
  47. protected snackbar: MatSnackBar,
  48. @Inject(MAT_DIALOG_DATA) public data: { dialogTitle: string, model: any, treeNode: TreeNode }) {
  49.  
  50. // Search Office
  51. this.options = [];
  52. this.NameToOid = {};
  53. this.officeService.getOffice(new Office()).subscribe(result => {
  54. this.officeList = new Array<Office>();
  55. console.log(result.body.data);
  56. for (const child of result.body.data) {
  57. this.officeList.push(child);
  58. this.options.push(child);
  59. this.NameToOid[child.nameEn] = child.oid;
  60. }
  61. });
  62.  
  63. // Fillup field for remove data
  64. if (this.data.dialogTitle === 'REMOVE_EMPLOYEE') {
  65. console.log('remove');
  66. console.log(this.data.model);
  67.  
  68. this.masterModel.nameBn = this.data.model[0].nameBn;
  69. this.masterModel.nameEn = this.data.model[0].nameEn;
  70. }
  71.  
  72.  
  73. // this.singleEmpInfo.officeNameEn = this.data.model.officeNameEn;
  74.  
  75. }
  76.  
  77. ngOnInit() {
  78. // start searching office
  79.  
  80. this.officeService.getOffice(new Office()).subscribe(result => {
  81.  
  82. this.officeList = new Array<Office>();
  83. for (const child of result.body.data) {
  84. this.officeList.push(child);
  85. }
  86. });
  87. this.filteredOptions = this.myControl.valueChanges.pipe(
  88. startWith(''),
  89. map(value => this._filter(value))
  90. );
  91.  
  92. // end searching office
  93.  
  94.  
  95. if (this.data.treeNode.type === 'OfficeUnitPost') {
  96. const OUorOUP: OfficeUnitPostModel = this.data.treeNode.objectDetails;
  97. this.data.model.officeUnitOid = OUorOUP.officeUnitOid;
  98. const offUnit: OfficeUnit = new OfficeUnit();
  99. offUnit.oid = OUorOUP.officeUnitOid;
  100. console.log(offUnit);
  101. this.unitService.getUnitByUnitOid(offUnit).subscribe(result => {
  102. const office: Office = new Office();
  103. console.log(result.body.data);
  104. office.oid = result.body.data[0].officeOid;
  105.  
  106. this.empService.getEmployeeOfficeInfoByOfficeOid(office).subscribe(results => {
  107. const empModel: EmployeeMasterModel = new EmployeeMasterModel();
  108. console.log('result body employee oid');
  109. console.log(results.body.data);
  110. empModel.oid = results.body.data[0].employeeOid;
  111. if (results.body.data[0].joiningDate !== null) {
  112. console.log(results.body.data[0].joiningDate);
  113. // this.data.model.joiningDate = results.body.data[0].joiningDate.toDateString();
  114. }
  115. });
  116. });
  117. }
  118. }
  119. private _filter(name: string): Office[] {
  120. const filterValue = name.toLowerCase();
  121.  
  122. return this.options.filter(option => option.nameEn.toLowerCase().indexOf(filterValue) === 0);
  123. }
  124.  
  125. save() {
  126. if (this.data.treeNode.type === 'OfficeUnitPost') {
  127. console.log('Save Emlployee');
  128. console.log(this.OfficeOid);
  129. const OUorOUP: OfficeUnitPostModel = this.data.treeNode.objectDetails;
  130. console.log('Ofifce Unit Post');
  131. console.log(OUorOUP);
  132.  
  133. this.data.model.officeOid = this.OfficeOid;
  134. this.data.model.officeUnitOid = OUorOUP.officeUnitOid;
  135. this.data.model.officeUnitPostOid = OUorOUP.oid;
  136. this.data.model.inchargeLabelBn = this.data.treeNode.nameBn;
  137. this.data.model.inchargeLabelEn = this.data.treeNode.nameEn;
  138. this.data.model.isOfficeAdmin = 'No';
  139. this.data.model.isOfficeHead = 'No';
  140. this.data.model.statusChangeDate = null;
  141. this.data.model.nodeLevel = null;
  142. this.data.model.nodeSequence = null;
  143. this.data.model.systemRoleId = null;
  144. this.data.model.status = null;
  145. console.log('data model');
  146. console.log(this.data.model);
  147. if (this.data.dialogTitle === 'ASSIGN_EMPLOYEE') {
  148. this.data.model.status = 'Active';
  149. this.data.model.lastOfficeDate = null;
  150.  
  151. this.empService.createEmployeeOfficeForAssigningPost(this.data.model).subscribe(result => {
  152. if ( result.header.responseCode.toString() === '200') {
  153. const sendEmployee = [result.body.data[0], 'NEW_EMPLOYEE'];
  154. this.dialogRef.close(sendEmployee);
  155. }
  156. });
  157. } else if (this.data.dialogTitle === 'REMOVE_EMPLOYEE') {
  158.  
  159. // this.data.model.joiningDate = null;
  160. // this.data.model.status = null;
  161. console.log(this.data.model);
  162. const empInfo: EmployeeInfoModel = this.data.model[0];
  163. this.empOffice.oid = empInfo.employeeOfficeOid;
  164. this.empOffice.employmentTypeOid = empInfo.employeeTypeOid;
  165. this.empOffice.officeOid = empInfo.officeOid;
  166. this.empOffice.officeUnitOid = empInfo.officeUnitOid;
  167. this.empOffice.officeUnitPostOid = empInfo.officeUnitPostOid;
  168. this.empOffice.employeeOid = empInfo.oid;
  169. this.empOffice.responsibilityType = 'Main';
  170. console.log(this.empOffice);
  171. this.empService.removeEmployee(this.empOffice).subscribe(result => {
  172. if ( result.header.responseCode.toString() === '200') {
  173. const sendEmployee = [result.body.data[0], 'REMOVE_EMPLOYEE'];
  174. this.dialogRef.close(sendEmployee);
  175. }
  176. });
  177. }
  178.  
  179.  
  180. }
  181.  
  182. }
  183. cancel() {
  184. this.dialogRef.close();
  185. }
  186.  
  187. loadEmployee(event: MatAutocompleteSelectedEvent) {
  188. console.log(event);
  189. console.log(this.empOptions);
  190. this.OidName = event.option.value;
  191. const Oid = this.NameToOid[this.OidName];
  192. this.data.model.employeeOid = Oid;
  193. // this.empOffice.employeeOid = Oid;
  194. this.masterModel.nameEn = this.OidName;
  195. this.singleEmpInfo = this.empOptions.find(x => x.oid === Oid);
  196. this.masterModel.nameBn = this.singleEmpInfo.nameBn;
  197. this.data.model.employmentTypeOid = this.singleEmpInfo.employeeTypeOid;
  198. this.data.model.isDefaultProfile = this.singleEmpInfo.isDefaultProfile;
  199. console.log(this.singleEmpInfo);
  200. }
  201.  
  202. loadEmplyeeList(officeOid: string): void {
  203. const sendOffice: SearchEmployeeModel = new SearchEmployeeModel();
  204. console.log(officeOid);
  205. sendOffice.listOfOfficeOid.push(officeOid);
  206. this.empOptions = [];
  207. this.NameToOid = {};
  208. this.empService.searchEmployee(sendOffice).subscribe( searchResult => {
  209. console.log('search employee result');
  210. for (const child of searchResult.body.data) {
  211. this.empOptions = searchResult.body.data;
  212. this.NameToOid[child.nameEn] = child.oid;
  213. }
  214. console.log(this.empOptions);
  215. this.empFilteredOptions = this.myEmpControl.valueChanges.pipe(
  216. startWith(''),
  217. map(value => this._empfilter(value))
  218. );
  219. });
  220.  
  221.  
  222.  
  223. }
  224. private _empfilter(name: string): EmployeeInfoModel[] {
  225. const filterValue = name.toLowerCase();
  226. console.log(name);
  227. return this.empOptions.filter(option => option.nameEn.toLowerCase().indexOf(filterValue) === 0);
  228. }
  229. loadOffice(event: MatAutocompleteSelectedEvent) {
  230.  
  231. this.OidName = event.option.value;
  232. console.log('load office hierarchy');
  233. console.log(this.OidName);
  234. const officeOid = this.NameToOid[this.OidName];
  235. console.log(officeOid);
  236. this.OfficeOid = officeOid;
  237. this.getOfficeByOfficeOid(officeOid);
  238. this.loadEmplyeeList(officeOid);
  239.  
  240. }
  241.  
  242. getOfficeByOfficeOid(officeOid: string): void {
  243.  
  244. const sendOffice: Office = new Office();
  245. sendOffice.oid = officeOid;
  246. this.officeService.getOfficeByOfficeOid(sendOffice).subscribe((officeByName) => {
  247. this.backEndTree = officeByName.body.data[0];
  248. this.treeNode = this.BackEndTreeToTreeNode(this.backEndTree);
  249.  
  250. });
  251.  
  252. }
  253.  
  254. BackEndTreeToTreeNode(backEndTree: Tree): TreeNode {
  255.  
  256. const treeNodeTemp: TreeNode = new TreeNode();
  257. if (backEndTree.type === null) {
  258. treeNodeTemp.type = 'OfficeOfOfficeHierarchy';
  259. let tempOffice = new Office();
  260. tempOffice = backEndTree.parentDTO;
  261. treeNodeTemp.objectDetails = tempOffice;
  262. treeNodeTemp.nameEn = tempOffice.nameEn;
  263. treeNodeTemp.nameBn = tempOffice.nameBn;
  264. treeNodeTemp.typeEn = tempOffice.officeLayerOid;
  265. treeNodeTemp.typeBn = tempOffice.officeLayerOid;
  266. treeNodeTemp.rootOid = tempOffice.ministryOid;
  267. treeNodeTemp.pid = tempOffice.parentOid;
  268. treeNodeTemp.id = tempOffice.oid;
  269. treeNodeTemp.displayOrder = tempOffice.sortOrder;
  270. if ( tempOffice.logoUrl != null ) {
  271. treeNodeTemp.logoUrl = tempOffice.logoUrl;
  272. } else {
  273. treeNodeTemp.logoUrl = '../../../../assets/img/office_icon.png';
  274. }
  275. treeNodeTemp.id = tempOffice.oid;
  276. if ( tempOffice.parentOid != null ) {
  277. treeNodeTemp.pid = tempOffice.parentOid;
  278. }
  279. if (this.backEndTree.childOffices !== undefined && this.backEndTree.childOffices.length > 0) {
  280. treeNodeTemp.children = new Array<TreeNode>();
  281. // tslint:disable-next-line:prefer-const
  282. for (let child of backEndTree.childOffices) {
  283.  
  284. const tempChild = this.BackEndTreeToTreeNode(child);
  285. treeNodeTemp.children.push(tempChild);
  286. }
  287. treeNodeTemp.children.sort((a, b) => (a.displayOrder < b.displayOrder) ? 1 : -1 );
  288. }
  289.  
  290. }
  291. return treeNodeTemp;
  292.  
  293.  
  294. }
  295.  
  296. deleteEmployee() {
  297.  
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement