import * as _ from "lodash"; import { ChapterFilter } from "../../services/ChapterFilter"; import { DocumentService } from '../../services/DocumentService'; import * as widgets from "oasi-widgets"; import { DocumentIsogd, DocumentTerritoryDocTerritory, DocumentSpecific, TEP, DocumentApproveDocument, DocumentTerritory } from "isogd-document-rest"; import { NsiRestService } from "oasi-nsi-rest"; class DocumentEditController { static $inject = ['nsiRestService', 'documentService']; private loadingStatus: widgets.LoadingStatus = widgets.LoadingStatus.LOADING; private document: DocumentIsogd; private documentRequisitesShow: boolean = true; private registrationShow: boolean = true; private territoryShow: boolean = true; private objectShow: boolean = true; private specificShow: boolean = true; private requestShow: boolean = true; private approveDocumentShow: boolean = true; private linkDocumentShow: boolean = true; private organizationsShow: boolean = true; private tepShow: boolean = true; private operatorShow: boolean = true; private chapters: any[]; private selectedChapter: any; private documentTypes: any[]; private selectedDocumentType: any = null; private selectedDocumentTypes: any[]; private documentOrgs: any[]; private selectedDocumentOrg: any; private selectedDocumentOrg1: any; private documentStatuses: any[]; private selectedDocumentStatus: any; private workSheetNumbers: any[]; private selectedWorkSheetNumbers: any[] = []; private areas: any[]; private selectedAreas: any[] = []; private districts: any[]; private districtsByArea: any[] = []; private selectedDistricts: any[] = []; private finance: any[] = []; private selectedFinance: any; private specifics: any[]; private approvedocTypes: any[]; private selectedApprovedocType: any; private orgRoles: any[]; private orgTypes: any[]; private teps: any[]; private fnos: any[]; private docTerritories: DocumentTerritoryDocTerritory[]; private selectedDocTerritory: DocumentTerritoryDocTerritory; private copy: boolean; private notFoundAreas: string[] = []; private notFoundDistricts: string[] = []; private notFoundFinance: string[] = []; private notFoundDocStatuses: string[] = []; private fieldsDocumentISOGD: any[]; private showRegistration: boolean; private showTerritory: boolean; private showObject: boolean; private showSpecific: boolean; private showRequest: boolean; private showApproveDocument: boolean; private showLinkDocument: boolean; private showOrganizations: boolean; private showTeps: boolean; constructor(private nsiRestService: NsiRestService, private documentService: DocumentService) { } $onChanges(changes: any) { if (changes.selectedDocumentType && changes.selectedDocumentType.currentValue) { this.showSections(); } } hideSection(name: string): boolean { let result = false; if(this.selectedDocumentType) { this.fieldsDocumentISOGD.forEach(f => { if (f.xPath === name && f.hideDoc && f.hideDoc.length && f.hideDoc.length > 0) { if (_.find(f.hideDoc, d => {return d === this.selectedDocumentType.docCode})) { result = true; } } }); } return result; } documentTypeChanged(type: any) { this.selectedDocumentType = type; this.showSections(); } showSections() { this.showRegistration = !(this.hideSection('registration') && this.isEmptyObject(this.document.registration)); this.showTerritory = !(this.hideSection('territory') && this.isEmptyObject(this.document.territory)); this.showObject = !(this.hideSection('object') && this.isEmptyObject(this.document.object)); this.showSpecific = !(this.hideSection('specific') && this.isEmptyObject(this.document.specific)); this.showRequest = !(this.hideSection('request') && this.isEmptyObject(this.document.request)); this.showApproveDocument = !(this.hideSection('approveDocument') && this.isEmptyObject(this.document.approveDocument)); this.showLinkDocument = !(this.hideSection('linkDocument') && this.isEmptyObject(this.document.linkDocument)); this.showOrganizations = !(this.hideSection('organizations') && this.isEmptyObject(this.document.organizations)); this.showTeps = !(this.hideSection('teps') && this.isEmptyObject(this.document.teps)); } $onInit() { this.nsiRestService.get("OldChapters").then(response => { this.chapters = ChapterFilter.transform(response); if (this.copy) { this.chapters = _.filter(this.chapters, (ch) => { return ch.code === "8400" || ch.code === "8599"; }); } this.chapters.forEach(ch => { if (ch.code === this.document.reestr) { this.selectedChapter = ch; } }); return this.nsiRestService.get("DocumentTypes"); }).then(response => { this.documentTypes = response; if (this.selectedChapter && this.selectedChapter.doc && this.selectedChapter.doc.length > 0) { this.selectedDocumentTypes = this.selectedChapter.doc; } else { this.selectedDocumentTypes = this.documentTypes; } this.selectedDocumentTypes.forEach(d => { if (d.docCode === this.document.documentType) { this.selectedDocumentType = d; } }); return this.nsiRestService.get("fieldsDocumentISOGD"); }).then(response => { this.fieldsDocumentISOGD = response; return this.nsiRestService.get("DocumentOrg"); }).then(response => { this.documentOrgs = response; if (this.document.documentRequisites && this.document.documentRequisites.documentOrganizationCode) { this.documentOrgs.forEach(d => { if (d.orgCode === this.document.documentRequisites.documentOrganizationCode) { this.selectedDocumentOrg = d; } }); } if (this.document.approveDocument && this.document.approveDocument.approveOrganizationCode) { this.documentOrgs.forEach(d => { if (d.orgCode === this.document.approveDocument.approveOrganizationCode) { this.selectedDocumentOrg1 = d; } }); } if (this.document.territory && this.document.territory.workSheetNumber && this.document.territory.workSheetNumber.length > 0) { this.selectedWorkSheetNumbers = this.document.territory.workSheetNumber } return this.nsiRestService.get("DocumentStatus"); }).then(response => { this.documentStatuses = response; if (this.document.documentRequisites) { if (this.document.documentRequisites.documentStatusCode) { this.documentStatuses.forEach(d => { if (d.statusCode === this.document.documentRequisites.documentStatusCode) { this.selectedDocumentStatus = d; } }); } else { if (this.document.documentRequisites.documentStatusValue) { let newValue: any; this.documentStatuses.forEach(d => { if (d.synonym && d.synonym.length > 0) { d.synonym.forEach(s => { if (s === this.document.documentRequisites.documentStatusValue) { newValue = d; } }); } }); if (newValue) { this.selectedDocumentStatus = newValue; this.document.documentRequisites.documentStatusCode = this.selectedDocumentStatus.statusCode; this.document.documentRequisites.documentStatusColor = this.selectedDocumentStatus.statusColor[0]; this.document.documentRequisites.documentStatusValue = this.selectedDocumentStatus.statusValue; } else { this.notFoundDocStatuses.push(this.document.documentRequisites.documentStatusValue); } } } } return this.nsiRestService.get("Prefect"); }).then(response => { this.areas = response; if (this.document.territory && this.document.territory.area && this.document.territory.area.length > 0) { this.document.territory.area.forEach(a => { if (a.areaCode) { this.areas.forEach(d => { if (d.code === a.areaCode) { this.selectedAreas.push(d); } }); } else { if (a.areaName) { let newValue: any; this.areas.forEach(d => { if (d.synonyms && d.synonyms.length > 0) { d.synonyms.forEach(s => { if (s === a.areaName) { newValue = d; } }); } }); if (newValue) { a.areaCode = newValue.code; a.areaName = newValue.name; this.selectedAreas.push(newValue); } else { this.notFoundAreas.push(a.areaName); } } } }); } return this.nsiRestService.get("District"); }).then(response => { this.districts = response; if (this.document.territory && this.document.territory.district && this.document.territory.district.length > 0) { this.document.territory.district.forEach(a => { if (a.districtCode) { this.districts.forEach(d => { if (d.code === a.districtCode) { this.selectedDistricts.push(d); } }); } else { if (a.districtName) { let newValue: any; this.districts.forEach(d => { if (d.synonyms && d.synonyms.length > 0) { d.synonyms.forEach(s => { if (s === a.districtName) { newValue = d; } }); } }); if (newValue) { a.districtCode = newValue.code; a.districtName = newValue.name; this.selectedDistricts.push(newValue); } else { this.notFoundDistricts.push(a.districtName); } } } }); } if (this.selectedAreas.length > 0) { let _districts = []; this.districts.forEach(d => { this.selectedAreas.forEach(a => { if (d.perfectId && d.perfectId.length > 0) { d.perfectId.forEach(p => { if (p.code === a.code) { _districts.push(d); } }); } }); }); this.districtsByArea = _districts; } else { this.districtsByArea = this.districts; } return this.nsiRestService.get("Finance"); }).then(response => { this.finance = response; if (this.document.object) { if (this.document.object.objectFinanceCode) { this.finance.forEach(f => { if (f.financeCode === this.document.object.objectFinanceCode) { this.selectedFinance = f; } }); } else { if (this.document.object.objectFinanceValue) { let newValue: any; this.finance.forEach(f => { if (f.synonyms && f.synonyms.length > 0) { f.synonyms.forEach(s => { if (s === this.document.object.objectFinanceValue) { newValue = f; } }); } }); if (newValue) { this.selectedFinance = newValue; this.document.object.objectFinanceCode = this.selectedFinance.financeCode; this.document.object.objectFinanceValue = this.selectedFinance.financeName; } else { this.notFoundFinance.push(this.document.object.objectFinanceValue); } } } } return this.nsiRestService.get("Specific"); }).then(response => { this.specifics = response; return this.nsiRestService.get("DocTerritory"); }).then((response: any[]) => { this.docTerritories = response; if (this.document.territory && this.document.territory.docTerritory) { this.docTerritories.forEach(d => { if (d.code === this.document.territory.docTerritory.code) { this.selectedDocTerritory = d; } }) } return this.nsiRestService.get("ApprovedocTypes"); }).then(response => { this.approvedocTypes = response; if (this.document.approveDocument && this.document.approveDocument.approveDocumentType) { this.approvedocTypes.forEach(d => { if (d.approvedocCode === this.document.approveDocument.approveDocumentType) { this.selectedApprovedocType = d; } }); } return this.nsiRestService.get("OrgRoles"); }).then(response => { this.orgRoles = response; return this.nsiRestService.get("OrgTypes"); }).then(response => { this.orgTypes = response; return this.nsiRestService.get("TEP"); }).then(response => { this.teps = _.concat([{ tepCode: "", tepName: "" }], response); return this.nsiRestService.get("FNO"); }).then(response => { this.fnos = response; if (!this.document.approveDocument) { this.document.approveDocument = new DocumentApproveDocument(); } if (!this.document.territory) { this.document.territory = new DocumentTerritory(); } if (this.document.territory) { if (!this.document.territory.cadastralNumber) { this.document.territory.cadastralNumber = []; } if (this.document.territory.cadastralNumber && this.document.territory.cadastralNumber.length === 0) { this.document.territory.cadastralNumber.push(""); } if (!this.document.territory.workSheetNumber) { this.document.territory.workSheetNumber = []; } if (this.document.territory.workSheetNumber && this.document.territory.workSheetNumber.length === 0) { this.document.territory.workSheetNumber.push(""); } } let newSpecific: DocumentSpecific = new DocumentSpecific(); newSpecific.specificCode = this.specifics[0].code; newSpecific.specificName = this.specifics[0].name; newSpecific.specificValue = ""; if (!this.document.specific) { this.document.specific = []; } this.document.specific.push(newSpecific); let newTep: TEP = new TEP(); newTep.tepName = ""; newTep.tepValue = ""; newTep.tepCode = ""; if (!this.document.teps) { this.document.teps = []; } this.document.teps.push(newTep); this.showSections(); this.loadingStatus = widgets.LoadingStatus.SUCCESS; }).catch(error => { console.log(error); this.loadingStatus = widgets.LoadingStatus.ERROR; }); } isEmptyObject(obj: any) { for (var i in obj) { if (obj.hasOwnProperty(i) && obj[i]) { return false; } } return true; } } export const DocumentEditComponent: angular.IComponentOptions = { controller: DocumentEditController, template: require('./documentEdit.html'), bindings: { document: "<", copy: "=" }, transclude: { actionsSlot: "?actions" }, };