]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/org-addr.component.ts
lp1863252 fix Get Coordinates button in org admin
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / org-addr.component.ts
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';
9
10 const ADDR_TYPES =
11     ['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
12
13 @Component({
14     selector: 'eg-admin-org-address',
15     templateUrl: './org-addr.component.html'
16 })
17 export class OrgAddressComponent {
18
19     orgUnit: IdlObject = null;
20     private tabName: string;
21
22     private _orgId: number;
23
24     get orgId(): number { return this._orgId; }
25
26     @Input() set orgId(newId: number) {
27         if (newId) {
28             if (!this._orgId || this._orgId !== newId) {
29                 this._orgId = newId;
30                 this.init();
31             }
32         } else {
33             this._orgId = null;
34         }
35     }
36
37     @Output() addrChange: EventEmitter<IdlObject>;
38
39     constructor(
40         private idl: IdlService,
41         private org: OrgService,
42         private pcrud: PcrudService,
43         private auth: AuthService,
44         private net: NetService,
45         private toast: ToastService
46     ) {
47         this.addrChange = new EventEmitter<IdlObject>();
48         this.tabName = 'billing_address';
49     }
50
51     init() {
52         if (!this.orgId) { return; }
53
54         return this.pcrud.retrieve('aou', this.orgId,
55             {flesh : 1, flesh_fields : {aou : ADDR_TYPES}},
56             {authoritative: true}
57         ).subscribe(org => {
58             this.orgUnit = org;
59             ADDR_TYPES.forEach(aType => {
60                 if (!this.addr(aType)) {
61                     this.createAddress(aType);
62                 }
63             });
64         });
65     }
66
67     tabChanged($event: NgbTabChangeEvent) {
68         this.tabName = $event.nextId;
69     }
70
71     addrTypes(): string[] { // for UI
72         return ADDR_TYPES;
73     }
74
75     // Template shorthand -- get a specific address by type.
76     addr(addrType: string) {
77         return this.orgUnit ? this.orgUnit[addrType]() : null;
78     }
79
80     createAddress(addrType: string) {
81         const addr = this.idl.create('aoa');
82         addr.isnew(true);
83         addr.valid('t');
84         addr.org_unit(this.orgId);
85         this.orgUnit[addrType](addr);
86     }
87
88     cloneAddress(addrType: string) {
89
90         // Find the address
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);
96             }
97         });
98
99         const addr = this.idl.clone(fromAddr);
100         addr.id(null);
101         addr.isnew(true);
102         addr.valid('t');
103         this.orgUnit[addrType](addr);
104     }
105
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 => {
109             return (
110                 !this.addr(aType).isnew() && this.addr(aType).id() === addrId
111             );
112         }).length > 1;
113     }
114
115     deleteAddress($event: any) {
116         const addr = $event.record;
117         const tmpOrg = this.updatableOrg();
118
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()) {
123                 tmpOrg[aType](null);
124                 this.createAddress(aType);
125             }
126         });
127
128         this.pcrud.update(tmpOrg).toPromise()
129         .then(_ => this.pcrud.remove(addr).toPromise())
130         .then(_ => this.addrChange.emit(addr));
131     }
132
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) {
137
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
141             // in place.
142             return;
143         }
144
145         // update local copy with version that has an ID.
146         this.orgUnit[this.tabName](addr);
147
148         const org = this.updatableOrg();
149         org[this.tabName](addr.id());
150
151         // Creating a new address -- tell our org about it.
152         this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
153     }
154
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);
159
160         ADDR_TYPES.forEach(aType => {
161             const addr = this.addr(aType);
162             if (addr) { org[aType](addr.id()); }
163         });
164
165         return org;
166     }
167
168     getCoordinates($event: any) {
169         const addr = $event.record;
170         this.net.request(
171             'open-ils.actor',
172             'open-ils.actor.geo.retrieve_coordinates',
173             this.auth.token(),
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()
178         ).subscribe(
179             (res) => {
180                 console.log('geo',res);
181                 if (typeof res.ilsevent == 'undefined') {
182                     addr.latitude( res.latitude );
183                     addr.longitude( res.longitude );
184                 } else {
185                     this.toast.danger(res.desc);
186                 }
187             },
188             (err) => {
189                 console.error('geo',err);
190             }
191         );
192     }
193 }
194