LP1865898 Scan Item as Missing Pieces Angular Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / item / missing-pieces.component.ts
1 import {Component, Input, AfterViewInit, ViewChild, Renderer2} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {PrintService} from '@eg/share/print/print.service';
8 import {HoldingsService} from '@eg/staff/share/holdings/holdings.service';
9 import {EventService} from '@eg/core/event.service';
10 import {PatronPenaltyDialogComponent} from '@eg/staff/share/patron/penalty-dialog.component';
11
12 @Component({
13   templateUrl: 'missing-pieces.component.html'
14 })
15 export class MarkItemMissingPiecesComponent implements AfterViewInit {
16
17     itemId: number;
18     itemBarcode: string;
19     item: IdlObject;
20     letter: string;
21     circNotFound = false;
22     processing = false;
23     noSuchItem = false;
24
25     @ViewChild('penaltyDialog', {static: false})
26     penaltyDialog: PatronPenaltyDialogComponent;
27
28     constructor(
29         private route: ActivatedRoute,
30         private renderer: Renderer2,
31         private net: NetService,
32         private printer: PrintService,
33         private pcrud: PcrudService,
34         private auth: AuthService,
35         private evt: EventService,
36         private holdings: HoldingsService
37     ) {
38         this.itemId = +this.route.snapshot.paramMap.get('id');
39     }
40
41     ngAfterViewInit() {
42         if (this.itemId) { this.getItemById(); }
43         this.renderer.selectRootElement('#item-barcode-input').focus();
44     }
45
46     getItemByBarcode(): Promise<any> {
47         this.itemId = null;
48         this.item = null;
49
50         if (!this.itemBarcode) { return Promise.resolve(); }
51
52         return this.holdings.getItemIdFromBarcode(this.itemBarcode)
53         .then(id => {
54             this.noSuchItem = (id === null);
55             this.itemId = id;
56             return this.getItemById();
57         });
58     }
59
60     selectInput() {
61         setTimeout(() =>
62             this.renderer.selectRootElement('#item-barcode-input').select());
63     }
64
65     getItemById(): Promise<any> {
66         this.circNotFound = false;
67
68         if (!this.itemId) {
69             this.selectInput();
70             return Promise.resolve();
71         }
72
73         const flesh = {
74             flesh: 3,
75             flesh_fields: {
76                 acp: ['call_number'],
77                 acn: ['record'],
78                 bre: ['flat_display_entries']
79             }
80         };
81
82         return this.pcrud.retrieve('acp', this.itemId, flesh)
83         .toPromise().then(item => {
84             this.item = item;
85             this.itemId = item.id();
86             this.itemBarcode = item.barcode();
87             this.selectInput();
88         });
89     }
90
91     display(field: string): string {
92         if (!this.item) { return ''; }
93
94         const entry = this.item.call_number().record()
95             .flat_display_entries()
96             .filter(fde => fde.name() === field)[0];
97
98         return entry ? entry.value() : '';
99     }
100
101     reset() {
102         this.item = null;
103         this.itemId = null;
104         this.itemBarcode = null;
105         this.circNotFound = false;
106     }
107
108     processItem() {
109         this.circNotFound = false;
110
111         if (!this.item) { return; }
112
113         this.processing = true;
114
115         this.net.request(
116             'open-ils.circ',
117             'open-ils.circ.mark_item_missing_pieces',
118             this.auth.token(), this.itemId
119         ).subscribe(resp => {
120             const evt = this.evt.parse(resp); // always returns event
121             this.processing = false;
122
123             if (evt.textcode === 'ACTION_CIRCULATION_NOT_FOUND') {
124                 this.circNotFound = true;
125                 return;
126             }
127
128             const payload = evt.payload;
129
130             if (payload.letter) {
131                 this.letter = payload.letter.template_output().data();
132             }
133
134             if (payload.slip) {
135                 this.printer.print({
136                     printContext: 'default',
137                     contentType: 'text/html',
138                     text: payload.slip.template_output().data()
139                 });
140             }
141
142             if (payload.circ) {
143                 this.penaltyDialog.patronId = payload.circ.usr();
144                 this.penaltyDialog.open().subscribe(
145                     penId => console.debug('Applied penalty ', penId));
146             }
147         });
148     }
149
150     printLetter() {
151         this.printer.print({
152             printContext: 'default',
153             contentType: 'text/plain',
154             text: this.letter
155         });
156     }
157
158     letterRowCount(): number {
159         return this.letter ? this.letter.split(/\n/).length + 2 : 20;
160     }
161 }
162
163
164