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';
16 * Dialog for marking items damaged and asessing related bills.
20 selector: 'eg-mark-damaged-dialog',
21 templateUrl: 'mark-damaged-dialog.component.html'
24 export class MarkDamagedDialogComponent
25 extends DialogComponent implements OnInit {
27 @Input() copyId: number;
29 bibSummary: BibRecordSummary;
30 billingTypes: ComboboxEntry[];
32 // Overide the API suggested charge amount
33 amountChangeRequested: boolean;
38 @ViewChild('successMsg') private successMsg: StringComponent;
39 @ViewChild('errorMsg') private errorMsg: StringComponent;
42 // Charge data returned from the server requesting additional charge info.
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 = [];
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.
65 async open(args: NgbModalOptions): Promise<boolean> {
69 return Promise.reject('copy ID required');
72 await this.getBillingTypes();
74 return super.open(args);
77 // Fetch-cache billing types
78 async getBillingTypes(): Promise<any> {
79 if (this.billingTypes.length > 1) {
80 return Promise.resolve();
82 return this.pcrud.search('cbt',
83 {owner: this.org.fullPath(this.auth.user().ws_ou(), 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()}));
92 async getData(): Promise<any> {
93 return this.pcrud.retrieve('acp', this.copyId,
94 {flesh: 1, flesh_fields: {acp: ['call_number']}}).toPromise()
97 return this.bib.getBibSummary(
98 copy.call_number().record()).toPromise();
100 this.bibSummary = summary;
106 this.bibSummary = null;
107 this.chargeResponse = null;
108 this.newCharge = null;
110 this.amountChangeRequested = false;
113 bTypeChange(entry: ComboboxEntry) {
114 this.newBtype = entry.id;
117 markDamaged(args: any) {
118 this.chargeResponse = null;
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;
127 'open-ils.circ', 'open-ils.circ.mark_item_damaged',
128 this.auth.token(), this.copyId, args
131 console.debug('Mark damaged returned', result);
133 if (Number(result) === 1) {
134 this.successMsg.current().then(msg => this.toast.success(msg));
139 const evt = this.evt.parse(result);
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;
148 this.errorMsg.current().then(m => this.toast.danger(m));