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';
18 * Dialog for marking items damaged and asessing related bills.
22 selector: 'eg-mark-damaged-dialog',
23 templateUrl: 'mark-damaged-dialog.component.html'
26 export class MarkDamagedDialogComponent
27 extends DialogComponent implements OnInit {
29 @Input() copyId: number;
31 bibSummary: BibRecordSummary;
32 billingTypes: ComboboxEntry[];
34 // Overide the API suggested charge amount
35 amountChangeRequested: boolean;
40 @ViewChild('successMsg') private successMsg: StringComponent;
41 @ViewChild('errorMsg') private errorMsg: StringComponent;
44 // Charge data returned from the server requesting additional charge info.
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 = [];
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.
67 open(args: NgbModalOptions): Observable<boolean> {
71 return throwError('copy ID required');
74 // Map data-loading promises to an observable
76 this.getBillingTypes().then(_ => this.getData()));
78 // Fire data loading observable and replace results with
79 // dialog opener observable.
80 return obs.pipe(switchMap(_ => super.open(args)));
83 // Fetch-cache billing types
84 getBillingTypes(): Promise<any> {
85 if (this.billingTypes.length > 1) {
86 return Promise.resolve();
88 return this.pcrud.search('cbt',
89 {owner: this.org.fullPath(this.auth.user().ws_ou(), 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()}));
98 getData(): Promise<any> {
99 return this.pcrud.retrieve('acp', this.copyId,
100 {flesh: 1, flesh_fields: {acp: ['call_number']}}).toPromise()
103 return this.bib.getBibSummary(
104 copy.call_number().record()).toPromise();
106 this.bibSummary = summary;
112 this.bibSummary = null;
113 this.chargeResponse = null;
114 this.newCharge = null;
116 this.amountChangeRequested = false;
119 bTypeChange(entry: ComboboxEntry) {
120 this.newBtype = entry.id;
123 markDamaged(args: any) {
124 this.chargeResponse = null;
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;
133 'open-ils.circ', 'open-ils.circ.mark_item_damaged',
134 this.auth.token(), this.copyId, args
137 console.debug('Mark damaged returned', result);
139 if (Number(result) === 1) {
140 this.successMsg.current().then(msg => this.toast.success(msg));
145 const evt = this.evt.parse(result);
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;
154 this.errorMsg.current().then(m => this.toast.danger(m));