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';
8 import {ToastService} from '@eg/share/toast/toast.service';
11 ['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
14 selector: 'eg-admin-org-address',
15 templateUrl: './org-addr.component.html'
17 export class OrgAddressComponent {
19 orgUnit: IdlObject = null;
20 private tabName: string;
22 private _orgId: number;
24 get orgId(): number { return this._orgId; }
26 @Input() set orgId(newId: number) {
28 if (!this._orgId || this._orgId !== newId) {
37 @Output() addrChange: EventEmitter<IdlObject>;
40 private idl: IdlService,
41 private org: OrgService,
42 private pcrud: PcrudService,
43 private auth: AuthService,
44 private net: NetService,
45 private toast: ToastService
47 this.addrChange = new EventEmitter<IdlObject>();
48 this.tabName = 'billing_address';
52 if (!this.orgId) { return; }
54 return this.pcrud.retrieve('aou', this.orgId,
55 {flesh : 1, flesh_fields : {aou : ADDR_TYPES}},
59 ADDR_TYPES.forEach(aType => {
60 if (!this.addr(aType)) {
61 this.createAddress(aType);
67 tabChanged($event: NgbTabChangeEvent) {
68 this.tabName = $event.nextId;
71 addrTypes(): string[] { // for UI
75 // Template shorthand -- get a specific address by type.
76 addr(addrType: string) {
77 return this.orgUnit ? this.orgUnit[addrType]() : null;
80 createAddress(addrType: string) {
81 const addr = this.idl.create('aoa');
84 addr.org_unit(this.orgId);
85 this.orgUnit[addrType](addr);
88 cloneAddress(addrType: string) {
91 let fromAddr: IdlObject;
92 ADDR_TYPES.forEach(aType => {
93 if (aType !== addrType &&
94 this.addr(aType).id() === this.addr(addrType).id()) {
95 fromAddr = this.addr(aType);
99 const addr = this.idl.clone(fromAddr);
103 this.orgUnit[addrType](addr);
106 // True if the provided address is used for more than one addr type.
107 sharedAddress(addrId: number): boolean {
108 return ADDR_TYPES.filter(aType => {
110 !this.addr(aType).isnew() && this.addr(aType).id() === addrId
115 deleteAddress($event: any) {
116 const addr = $event.record;
117 const tmpOrg = this.updatableOrg();
119 // Set the FKey to NULL on the org unit for deleted addresses
120 ADDR_TYPES.forEach(aType => {
121 const a = this.addr(aType);
122 if (a && a.id() === addr.id()) {
124 this.createAddress(aType);
128 this.pcrud.update(tmpOrg).toPromise()
129 .then(_ => this.pcrud.remove(addr).toPromise())
130 .then(_ => this.addrChange.emit(addr));
133 // Addr saved by fm-editor.
134 // In the case of new address creation, point the org unit at
135 // the new address ID.
136 addrSaved(addr: number | IdlObject) {
138 if (typeof addr !== 'object') {
139 // pcrud returns a number on 'update' calls. No need to
140 // reload the data on a simple address change. it's changed
145 // update local copy with version that has an ID.
146 this.orgUnit[this.tabName](addr);
148 const org = this.updatableOrg();
149 org[this.tabName](addr.id());
151 // Creating a new address -- tell our org about it.
152 this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
155 // Create an unfleshed org unit object that's a clone of this.orgUnit
156 // to use when pushing updates to the server.
157 updatableOrg(): IdlObject {
158 const org = this.idl.clone(this.orgUnit);
160 ADDR_TYPES.forEach(aType => {
161 const addr = this.addr(aType);
162 if (addr) { org[aType](addr.id()); }
168 getCoordinates($event: any) {
169 const addr = $event.record;
172 'open-ils.actor.geo.retrieve_coordinates',
174 typeof addr.org_unit() === 'object' ? addr.org_unit().id() : addr.org_unit(),
175 addr.street1() + ' ' + addr.street2() + ', '
176 + addr.city() + ', ' + addr.state() + ' ' + addr.post_code()
177 + ' ' + addr.country()
180 console.log('geo',res);
181 if (typeof res.ilsevent == 'undefined') {
182 addr.latitude( res.latitude );
183 addr.longitude( res.longitude );
185 this.toast.danger(res.desc);
189 console.error('geo',err);