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