]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/mark-damaged-dialog.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / mark-damaged-dialog.component.ts
1 import {Component, 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 {
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     /**
61      * Fetch the item/record, then open the dialog.
62      * Dialog promise resolves with true/false indicating whether
63      * the mark-damanged action occured or was dismissed.
64      */
65     open(args: NgbModalOptions): Observable<boolean> {
66         this.reset();
67
68         if (!this.copyId) {
69             return throwError('copy ID required');
70         }
71
72         // Map data-loading promises to an observable
73         const obs = from(
74             this.getBillingTypes().then(_ => this.getData()));
75
76         // Fire data loading observable and replace results with
77         // dialog opener observable.
78         return obs.pipe(switchMap(_ => super.open(args)));
79     }
80
81     // Fetch-cache billing types
82     getBillingTypes(): Promise<any> {
83         if (this.billingTypes.length > 1) {
84             return Promise.resolve();
85         }
86         return this.pcrud.search('cbt',
87             {owner: this.org.fullPath(this.auth.user().ws_ou(), true)},
88             {}, {atomic: true}
89         ).toPromise().then(bts => {
90             this.billingTypes = bts
91                 .sort((a, b) => a.name() < b.name() ? -1 : 1)
92                 .map(bt => ({id: bt.id(), label: bt.name()}));
93         });
94     }
95
96     getData(): Promise<any> {
97         return this.pcrud.retrieve('acp', this.copyId,
98             {flesh: 1, flesh_fields: {acp: ['call_number']}}).toPromise()
99         .then(copy => {
100             this.copy = copy;
101             return this.bib.getBibSummary(
102                 copy.call_number().record()).toPromise();
103         }).then(summary => {
104                 this.bibSummary = summary;
105         });
106     }
107
108     reset() {
109         this.copy = null;
110         this.bibSummary = null;
111         this.chargeResponse = null;
112         this.newCharge = null;
113         this.newNote = null;
114         this.amountChangeRequested = false;
115     }
116
117     bTypeChange(entry: ComboboxEntry) {
118         this.newBtype = entry.id;
119     }
120
121     markDamaged(args: any) {
122         this.chargeResponse = null;
123
124         if (args && args.apply_fines === 'apply') {
125             args.override_amount = this.newCharge;
126             args.override_btype = this.newBtype;
127             args.override_note = this.newNote;
128         }
129
130         this.net.request(
131             'open-ils.circ', 'open-ils.circ.mark_item_damaged',
132             this.auth.token(), this.copyId, args
133         ).subscribe(
134             result => {
135                 console.debug('Mark damaged returned', result);
136
137                 if (Number(result) === 1) {
138                     this.successMsg.current().then(msg => this.toast.success(msg));
139                     this.close(true);
140                     return;
141                 }
142
143                 const evt = this.evt.parse(result);
144
145                 if (evt.textcode === 'DAMAGE_CHARGE') {
146                     // More info needed from staff on how to hangle charges.
147                     this.chargeResponse = evt.payload;
148                     this.newCharge = this.chargeResponse.charge;
149                 }
150             },
151             err => {
152                 this.errorMsg.current().then(m => this.toast.danger(m));
153                 console.error(err);
154             }
155         );
156     }
157 }
158