]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/lineitem/batch-copies.component.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / lineitem / batch-copies.component.ts
1 import {Component, OnInit, Input, Output, EventEmitter, ViewChild} from '@angular/core';
2 import {tap} from 'rxjs/operators';
3 import {Pager} from '@eg/share/util/pager';
4 import {IdlObject, IdlService} from '@eg/core/idl.service';
5 import {EventService} from '@eg/core/event.service';
6 import {NetService} from '@eg/core/net.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {LineitemService} from './lineitem.service';
9 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
10 import {LineitemCopyAttrsComponent} from './copy-attrs.component';
11 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
12 import {CancelDialogComponent} from './cancel-dialog.component';
13
14 const BATCH_FIELDS = [
15     'owning_lib',
16     'location',
17     'collection_code',
18     'fund',
19     'circ_modifier',
20     'cn_label'
21 ];
22
23 @Component({
24   templateUrl: 'batch-copies.component.html',
25   selector: 'eg-lineitem-batch-copies',
26   styleUrls: ['batch-copies.component.css']
27 })
28 export class LineitemBatchCopiesComponent implements OnInit {
29
30     @Input() lineitem: IdlObject;
31
32     @ViewChild('confirmAlertsDialog') confirmAlertsDialog: ConfirmDialogComponent;
33     @ViewChild('cancelDialog') cancelDialog: CancelDialogComponent;
34
35     // Current alert that needs confirming
36     alertText: IdlObject;
37
38     constructor(
39         private evt: EventService,
40         private idl: IdlService,
41         private net: NetService,
42         private auth: AuthService,
43         private liService: LineitemService
44     ) {}
45
46     ngOnInit() {}
47
48     // Propagate values from the batch edit bar into the indivudual LID's
49     batchApplyAttrs(copyTemplate: IdlObject) {
50         BATCH_FIELDS.forEach(field => {
51             const val = copyTemplate[field]();
52             if (val === undefined) { return; }
53             this.lineitem.lineitem_details().forEach(copy => {
54                 copy[field](val);
55                 copy.ischanged(true); // isnew() takes precedence
56             });
57         });
58     }
59
60     deleteCopy(copy: IdlObject) {
61         if (copy.isnew()) {
62             // Brand new copies can be discarded
63             this.lineitem.lineitem_details(
64                 this.lineitem.lineitem_details().filter(c => c.id() !== copy.id())
65             );
66         } else {
67             // Requires a Save Changes action.
68             copy.isdeleted(true);
69         }
70     }
71
72     refreshLineitem() {
73         this.liService.getFleshedLineitems([this.lineitem.id()], {toCache: true})
74         .subscribe(liStruct => this.lineitem = liStruct.lineitem);
75     }
76
77     handleActionResponse(resp: any) {
78         const evt = this.evt.parse(resp);
79         if (evt) {
80           alert(evt);
81         } else if (resp) {
82             this.refreshLineitem();
83         }
84     }
85
86     cancelCopy(copy: IdlObject) {
87         this.cancelDialog.open().subscribe(reason => {
88             if (!reason) { return; }
89             this.net.request('open-ils.acq',
90                 'open-ils.acq.lineitem_detail.cancel',
91                 this.auth.token(), copy.id(), reason
92             ).subscribe(ok => this.handleActionResponse(ok));
93         });
94     }
95
96     receiveCopy(copy: IdlObject) {
97         this.checkLiAlerts().then(ok => {
98             this.net.request(
99                 'open-ils.acq',
100                 'open-ils.acq.lineitem_detail.receive',
101                 this.auth.token(), copy.id()
102             ).subscribe(ok2 => this.handleActionResponse(ok2));
103         }, err => {}); // avoid console errors
104     }
105
106     unReceiveCopy(copy: IdlObject) {
107         this.net.request(
108             'open-ils.acq',
109             'open-ils.acq.lineitem_detail.receive.rollback',
110             this.auth.token(), copy.id()
111         ).subscribe(ok => this.handleActionResponse(ok));
112     }
113
114     checkLiAlerts(): Promise<boolean> {
115
116         let promise = Promise.resolve(true);
117
118         const notes = this.lineitem.lineitem_notes().filter(note =>
119             note.alert_text() && !this.liService.alertAcks[note.id()]);
120
121         if (notes.length === 0) { return promise; }
122
123         notes.forEach(n => {
124             promise = promise.then(_ => {
125                 this.alertText = n.alert_text();
126                 return this.confirmAlertsDialog.open().toPromise().then(ok => {
127                     if (!ok) { return Promise.reject(); }
128                     this.liService.alertAcks[n.id()] = true;
129                     return true;
130                 });
131             });
132         });
133
134         return promise;
135     }
136 }
137
138