]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/circ/patron/patron.service.ts
LP1904036 Angular Patron UI initial structures
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / circ / patron / patron.service.ts
1 import {Injectable} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {NetService} from '@eg/core/net.service';
4 import {AuthService} from '@eg/core/auth.service';
5 import {PatronService} from '@eg/staff/share/patron/patron.service';
6
7 const PATRON_FLESH_FIELDS = [
8     'card',
9     'cards',
10     'settings',
11     'standing_penalties',
12     'addresses',
13     'billing_address',
14     'mailing_address',
15     'stat_cat_entries',
16     'waiver_entries',
17     'usr_activity',
18     'notes',
19     'profile',
20     'net_access_level',
21     'ident_type',
22     'ident_type2',
23     'groups'
24 ];
25
26 interface PatronStats {
27     fines: {balance_owed: number};
28     checkouts: {
29         overdue: number,
30         claims_returned: number,
31         lost: number,
32         out: number,
33         total_out: number,
34         long_overdue: number
35     };
36 }
37
38 @Injectable()
39 export class PatronManagerService {
40
41     patron: IdlObject;
42     patronStats: PatronStats;
43
44     // Value for YAOUS circ.do_not_tally_claims_returned
45     noTallyClaimsReturned = false;
46
47     // Value for YAOUS circ.tally_lost
48     tallyLost = false;
49
50     loaded = false;
51
52     constructor(
53         private net: NetService,
54         private auth: AuthService,
55         public patronService: PatronService
56     ) {}
57
58     loadPatron(id: number): Promise<any> {
59         this.loaded = false;
60         this.patron = null;
61
62         return this.net.request(
63             'open-ils.actor',
64             'open-ils.actor.user.fleshed.retrieve',
65             this.auth.token(), id, PATRON_FLESH_FIELDS).toPromise()
66         .then(patron => this.patron = patron)
67         .then(_ => this.getPatronStats(id))
68         .then(_ => this.loaded = true);
69     }
70
71    getPatronStats(id: number): Promise<any> {
72
73         return this.net.request(
74             'open-ils.actor',
75             'open-ils.actor.user.opac.vital_stats.authoritative',
76             this.auth.token(), id).toPromise()
77
78         .then((stats: PatronStats) => {
79
80             // force numeric values
81             stats.fines.balance_owed = Number(stats.fines.balance_owed);
82
83             Object.keys(stats.checkouts).forEach(key =>
84                 stats.checkouts[key] = Number(stats.checkouts[key]));
85
86             stats.checkouts.total_out = stats.checkouts.out +
87                 stats.checkouts.overdue + stats.checkouts.long_overdue
88
89             if (!this.noTallyClaimsReturned) {
90                 stats.checkouts.total_out += stats.checkouts.claims_returned;
91             }
92
93             if (this.tallyLost) {
94                 stats.checkouts.total_out += stats.checkouts.lost
95             }
96
97             return this.patronStats = stats;
98         });
99     }
100 }
101
102