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