1 import {Component, Input, Output, EventEmitter} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {OrgService} from '@eg/core/org.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
10 ['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
13 selector: 'eg-admin-org-address',
14 templateUrl: './org-addr.component.html'
16 export class OrgAddressComponent {
18 orgUnit: IdlObject = null;
19 private tabName: string;
21 private _orgId: number;
23 get orgId(): number { return this._orgId; }
25 @Input() set orgId(newId: number) {
27 if (!this._orgId || this._orgId !== newId) {
36 @Output() addrChange: EventEmitter<IdlObject>;
39 private idl: IdlService,
40 private org: OrgService,
41 private pcrud: PcrudService,
42 private auth: AuthService,
43 private net: NetService
45 this.addrChange = new EventEmitter<IdlObject>();
46 this.tabName = 'billing_address';
50 if (!this.orgId) { return; }
52 return this.pcrud.retrieve('aou', this.orgId,
53 {flesh : 1, flesh_fields : {aou : ADDR_TYPES}},
57 ADDR_TYPES.forEach(aType => {
58 if (!this.addr(aType)) {
59 this.createAddress(aType);
65 tabChanged($event: NgbTabChangeEvent) {
66 this.tabName = $event.nextId;
69 addrTypes(): string[] { // for UI
73 // Template shorthand -- get a specific address by type.
74 addr(addrType: string) {
75 return this.orgUnit ? this.orgUnit[addrType]() : null;
78 createAddress(addrType: string) {
79 const addr = this.idl.create('aoa');
82 addr.org_unit(this.orgId);
83 this.orgUnit[addrType](addr);
86 cloneAddress(addrType: string) {
89 let fromAddr: IdlObject;
90 ADDR_TYPES.forEach(aType => {
91 if (aType !== addrType &&
92 this.addr(aType).id() === this.addr(addrType).id()) {
93 fromAddr = this.addr(aType);
97 const addr = this.idl.clone(fromAddr);
101 this.orgUnit[addrType](addr);
104 // True if the provided address is used for more than one addr type.
105 sharedAddress(addrId: number): boolean {
106 return ADDR_TYPES.filter(aType => {
108 !this.addr(aType).isnew() && this.addr(aType).id() === addrId
113 deleteAddress($event: any) {
114 const addr = $event.record;
115 const tmpOrg = this.updatableOrg();
117 // Set the FKey to NULL on the org unit for deleted addresses
118 ADDR_TYPES.forEach(aType => {
119 const a = this.addr(aType);
120 if (a && a.id() === addr.id()) {
122 this.createAddress(aType);
126 this.pcrud.update(tmpOrg).toPromise()
127 .then(_ => this.pcrud.remove(addr).toPromise())
128 .then(_ => this.addrChange.emit(addr));
131 // Addr saved by fm-editor.
132 // In the case of new address creation, point the org unit at
133 // the new address ID.
134 addrSaved(addr: number | IdlObject) {
136 if (typeof addr !== 'object') {
137 // pcrud returns a number on 'update' calls. No need to
138 // reload the data on a simple address change. it's changed
143 // update local copy with version that has an ID.
144 this.orgUnit[this.tabName](addr);
146 const org = this.updatableOrg();
147 org[this.tabName](addr.id());
149 // Creating a new address -- tell our org about it.
150 this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
153 // Create an unfleshed org unit object that's a clone of this.orgUnit
154 // to use when pushing updates to the server.
155 updatableOrg(): IdlObject {
156 const org = this.idl.clone(this.orgUnit);
158 ADDR_TYPES.forEach(aType => {
159 const addr = this.addr(aType);
160 if (addr) { org[aType](addr.id()); }
166 getCoordinates($event: any) {
167 const addr = $event.record;
171 'open-ils.actor.geo.retrieve_coordinates',
174 addr.street1() + ' ' + addr.street2() + ', '
175 + addr.city() + ', ' + addr.state() + ' ' + addr.post_code()
176 + ' ' + addr.country()
179 addr.latitude( res.latitude );
180 addr.longitude( res.longitude );