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 {NgbNav, NgbNavChangeEvent} 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;
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>();
51 if (!this.orgId) { return; }
53 this.tabName = 'billing_address';
55 return this.pcrud.retrieve('aou', this.orgId,
56 {flesh : 1, flesh_fields : {aou : ADDR_TYPES}},
60 ADDR_TYPES.forEach(aType => {
61 if (!this.addr(aType)) {
62 this.createAddress(aType);
68 addrTypes(): string[] { // for UI
72 // Template shorthand -- get a specific address by type.
73 addr(addrType: string) {
74 return this.orgUnit ? this.orgUnit[addrType]() : null;
77 createAddress(addrType: string) {
78 const addr = this.idl.create('aoa');
81 addr.org_unit(this.orgId);
82 this.orgUnit[addrType](addr);
85 cloneAddress(addrType: string) {
88 let fromAddr: IdlObject;
89 ADDR_TYPES.forEach(aType => {
90 if (aType !== addrType &&
91 this.addr(aType).id() === this.addr(addrType).id()) {
92 fromAddr = this.addr(aType);
96 const addr = this.idl.clone(fromAddr);
100 this.orgUnit[addrType](addr);
103 // True if the provided address is used for more than one addr type.
104 sharedAddress(addrId: number): boolean {
105 return ADDR_TYPES.filter(aType => {
107 !this.addr(aType).isnew() && this.addr(aType).id() === addrId
112 deleteAddress($event: any) {
113 const addr = $event.record;
114 const tmpOrg = this.updatableOrg();
116 // Set the FKey to NULL on the org unit for deleted addresses
117 ADDR_TYPES.forEach(aType => {
118 const a = this.addr(aType);
119 if (a && a.id() === addr.id()) {
121 this.createAddress(aType);
125 this.pcrud.update(tmpOrg).toPromise()
126 .then(_ => this.pcrud.remove(addr).toPromise())
127 .then(_ => this.addrChange.emit(addr));
130 // Addr saved by fm-editor.
131 // In the case of new address creation, point the org unit at
132 // the new address ID.
133 addrSaved(addr: number | IdlObject) {
135 if (typeof addr !== 'object') {
136 // pcrud returns a number on 'update' calls. No need to
137 // reload the data on a simple address change. it's changed
142 // update local copy with version that has an ID.
143 this.orgUnit[this.tabName](addr);
145 const org = this.updatableOrg();
146 org[this.tabName](addr.id());
148 // Creating a new address -- tell our org about it.
149 this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
152 // Create an unfleshed org unit object that's a clone of this.orgUnit
153 // to use when pushing updates to the server.
154 updatableOrg(): IdlObject {
155 const org = this.idl.clone(this.orgUnit);
157 ADDR_TYPES.forEach(aType => {
158 const addr = this.addr(aType);
159 if (addr) { org[aType](addr.id()); }
165 getCoordinates($event: any) {
166 const addr = $event.record;
169 'open-ils.actor.geo.retrieve_coordinates',
171 typeof addr.org_unit() === 'object' ? addr.org_unit().id() : addr.org_unit(),
172 addr.street1() + ' ' + addr.street2() + ', '
173 + addr.city() + ', ' + addr.state() + ' ' + addr.post_code()
174 + ' ' + addr.country()
177 console.log('geo', res);
178 if (typeof res.ilsevent === 'undefined') {
179 addr.latitude( res.latitude );
180 addr.longitude( res.longitude );
182 this.toast.danger(res.desc);
186 console.error('geo', err);