]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/mark-damaged-dialog.component.ts
Merge branch 'master' of git.evergreen-ils.org:Evergreen
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / mark-damaged-dialog.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {Observable, throwError, from} from 'rxjs';
3 import {switchMap} from 'rxjs/operators';
4 import {NetService} from '@eg/core/net.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {EventService} from '@eg/core/event.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {DialogComponent} from '@eg/share/dialog/dialog.component';
13 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
14 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
15 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
16
17 /**
18  * Dialog for marking items damaged and asessing related bills.
19  */
20
21 @Component({
22   selector: 'eg-mark-damaged-dialog',
23   templateUrl: 'mark-damaged-dialog.component.html'
24 })
25
26 export class MarkDamagedDialogComponent
27     extends DialogComponent implements OnInit {
28
29     @Input() copyId: number;
30     copy: IdlObject;
31     bibSummary: BibRecordSummary;
32     billingTypes: ComboboxEntry[];
33
34     // Overide the API suggested charge amount
35     amountChangeRequested: boolean;
36     newCharge: number;
37     newNote: string;
38     newBtype: number;
39
40     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
41     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
42
43
44     // Charge data returned from the server requesting additional charge info.
45     chargeResponse: any;
46
47     constructor(
48         private modal: NgbModal, // required for passing to parent
49         private toast: ToastService,
50         private net: NetService,
51         private evt: EventService,
52         private pcrud: PcrudService,
53         private org: OrgService,
54         private bib: BibRecordService,
55         private auth: AuthService) {
56         super(modal); // required for subclassing
57         this.billingTypes = [];
58     }
59
60     ngOnInit() {}
61
62     /**
63      * Fetch the item/record, then open the dialog.
64      * Dialog promise resolves with true/false indicating whether
65      * the mark-damanged action occured or was dismissed.
66      */
67     open(args: NgbModalOptions): Observable<boolean> {
68         this.reset();
69
70         if (!this.copyId) {
71             return throwError('copy ID required');
72         }
73
74         // Map data-loading promises to an observable
75         const obs = from(
76             this.getBillingTypes().then(_ => this.getData()));
77
78         // Fire data loading observable and replace results with
79         // dialog opener observable.
80         return obs.pipe(switchMap(_ => super.open(args)));
81     }
82
83     // Fetch-cache billing types
84     getBillingTypes(): Promise<any> {
85         if (this.billingTypes.length > 1) {
86             return Promise.resolve();
87         }
88         return this.pcrud.search('cbt',
89             {owner: this.org.fullPath(this.auth.user().ws_ou(), true)},
90             {}, {atomic: true}
91         ).toPromise().then(bts => {
92             this.billingTypes = bts
93                 .sort((a, b) => a.name() < b.name() ? -1 : 1)
94                 .map(bt => ({id: bt.id(), label: bt.name()}));
95         });
96     }
97
98     getData(): Promise<any> {
99         return this.pcrud.retrieve('acp', this.copyId,
100             {flesh: 1, flesh_fields: {acp: ['call_number']}}).toPromise()
101         .then(copy => {
102             this.copy = copy;
103             return this.bib.getBibSummary(
104                 copy.call_number().record()).toPromise();
105         }).then(summary => {
106                 this.bibSummary = summary;
107         });
108     }
109
110     reset() {
111         this.copy = null;
112         this.bibSummary = null;
113         this.chargeResponse = null;
114         this.newCharge = null;
115         this.newNote = null;
116         this.amountChangeRequested = false;
117     }
118
119     bTypeChange(entry: ComboboxEntry) {
120         this.newBtype = entry.id;
121     }
122
123     markDamaged(args: any) {
124         this.chargeResponse = null;
125
126         if (args && args.apply_fines === 'apply') {
127             args.override_amount = this.newCharge;
128             args.override_btype = this.newBtype;
129             args.override_note = this.newNote;
130         }
131
132         this.net.request(
133             'open-ils.circ', 'open-ils.circ.mark_item_damaged',
134             this.auth.token(), this.copyId, args
135         ).subscribe(
136             result => {
137                 console.debug('Mark damaged returned', result);
138
139                 if (Number(result) === 1) {
140                     this.successMsg.current().then(msg => this.toast.success(msg));
141                     this.close(true);
142                     return;
143                 }
144
145                 const evt = this.evt.parse(result);
146
147                 if (evt.textcode === 'DAMAGE_CHARGE') {
148                     // More info needed from staff on how to hangle charges.
149                     this.chargeResponse = evt.payload;
150                     this.newCharge = this.chargeResponse.charge;
151                 }
152             },
153             err => {
154                 this.errorMsg.current().then(m => this.toast.danger(m));
155                 console.error(err);
156             }
157         );
158     }
159 }
160