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