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';
17 * Dialog for marking items damaged and asessing related bills.
21 selector: 'eg-mark-damaged-dialog',
22 templateUrl: 'mark-damaged-dialog.component.html'
25 export class MarkDamagedDialogComponent
26 extends DialogComponent implements OnInit {
28 @Input() copyId: number;
30 bibSummary: BibRecordSummary;
31 billingTypes: ComboboxEntry[];
33 // Overide the API suggested charge amount
34 amountChangeRequested: boolean;
39 @ViewChild('successMsg') private successMsg: StringComponent;
40 @ViewChild('errorMsg') private errorMsg: StringComponent;
43 // Charge data returned from the server requesting additional charge info.
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 = [];
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.
66 open(args: NgbModalOptions): Observable<boolean> {
70 return throwError('copy ID required');
73 this.getBillingTypes()
74 .then(() => { this.getData(); })
75 .then(() => super.open(args));
78 // Fetch-cache billing types
79 getBillingTypes(): Promise<any> {
80 if (this.billingTypes.length > 1) {
81 return Promise.resolve();
83 return this.pcrud.search('cbt',
84 {owner: this.org.fullPath(this.auth.user().ws_ou(), 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()}));
93 getData(): Promise<any> {
94 return this.pcrud.retrieve('acp', this.copyId,
95 {flesh: 1, flesh_fields: {acp: ['call_number']}}).toPromise()
98 return this.bib.getBibSummary(
99 copy.call_number().record()).toPromise();
101 this.bibSummary = summary;
107 this.bibSummary = null;
108 this.chargeResponse = null;
109 this.newCharge = null;
111 this.amountChangeRequested = false;
114 bTypeChange(entry: ComboboxEntry) {
115 this.newBtype = entry.id;
118 markDamaged(args: any) {
119 this.chargeResponse = null;
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;
128 'open-ils.circ', 'open-ils.circ.mark_item_damaged',
129 this.auth.token(), this.copyId, args
132 console.debug('Mark damaged returned', result);
134 if (Number(result) === 1) {
135 this.successMsg.current().then(msg => this.toast.success(msg));
140 const evt = this.evt.parse(result);
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;
149 this.errorMsg.current().then(m => this.toast.danger(m));