]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-associate-material.component.ts
lp1849212: Course Admin Page and OPAC improvements
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-associate-material.component.ts
1 import {Component, Input, ViewChild, OnInit, TemplateRef} from '@angular/core';
2 import {Observable, Observer, of} from 'rxjs';
3 import {DialogComponent} from '@eg/share/dialog/dialog.component';
4 import {AuthService} from '@eg/core/auth.service';
5 import {NetService} from '@eg/core/net.service';
6 import {EventService} from '@eg/core/event.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {Pager} from '@eg/share/util/pager';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {GridDataSource} from '@eg/share/grid/grid';
12 import {GridComponent} from '@eg/share/grid/grid.component';
13 import {IdlObject, IdlService} from '@eg/core/idl.service';
14 import {StringComponent} from '@eg/share/string/string.component';
15 import {ToastService} from '@eg/share/toast/toast.service';
16 import {CourseService} from '@eg/staff/share/course.service';
17
18 @Component({
19     selector: 'eg-course-associate-material-dialog',
20     templateUrl: './course-associate-material.component.html'
21 })
22
23 export class CourseAssociateMaterialComponent extends DialogComponent {
24
25     @ViewChild('materialsGrid', {static: true}) materialsGrid: GridComponent;
26     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
27     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
28     @ViewChild('successString', { static: true }) successString: StringComponent;
29     @ViewChild('failedString', { static: true }) failedString: StringComponent;
30     @ViewChild('differentLibraryString', { static: true }) differentLibraryString: StringComponent;
31     @Input() table_name = "Course Materials";
32     @Input() barcodeInput: String;
33     @Input() relationshipInput: String;
34     @Input() tempCallNumber: String;
35     @Input() tempStatus: Number;
36     @Input() tempLocation: Number;
37     @Input() tempCircMod: String;
38     @Input() isModifyingStatus: Boolean;
39     @Input() isModifyingCircMod: Boolean;
40     @Input() isModifyingCallNumber: Boolean;
41     @Input() isModifyingLocation: Boolean;
42     currentCourse: IdlObject;
43     materials: any[];
44     gridDataSource: GridDataSource;
45
46     constructor(
47         private auth: AuthService,
48         private idl: IdlService,
49         private net: NetService,
50         private pcrud: PcrudService,
51         private org: OrgService,
52         private evt: EventService,
53         private modal: NgbModal,
54         private toast: ToastService,
55         private courseSvc: CourseService
56     ) {
57         super(modal);
58         this.gridDataSource = new GridDataSource();
59     }
60
61     ngOnInit() {
62         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
63             return this.fetchMaterials(pager);
64         }
65     }
66
67     deleteSelected(items) {
68         let item_ids = [];
69         items.forEach(item => {
70             this.gridDataSource.data.splice(this.gridDataSource.data.indexOf(item, 0), 1);
71             item_ids.push(item.id())
72         });
73         this.pcrud.search('acmcm', {course: this.currentCourse.id(), item: item_ids}).subscribe(material => {
74             material.isdeleted(true);
75             this.pcrud.autoApply(material).subscribe(
76                 val => {
77                     this.courseSvc.resetItemFields(material, this.currentCourse.owning_lib());
78                     console.debug('deleted: ' + val);
79                     this.deleteSuccessString.current().then(str => this.toast.success(str));
80                 },
81                 err => {
82                     this.deleteFailedString.current()
83                         .then(str => this.toast.danger(str));
84                 }
85             );
86         });
87     }
88
89     associateItem(barcode, relationship) {
90         if (barcode) {
91             let args = {
92                 barcode: barcode,
93                 relationship: relationship,
94                 isModifyingCallNumber: this.isModifyingCallNumber,
95                 isModifyingCircMod: this.isModifyingCircMod,
96                 isModifyingLocation: this.isModifyingLocation,
97                 isModifyingStatus: this.isModifyingStatus,
98                 tempCircMod: this.tempCircMod,
99                 tempLocation: this.tempLocation,
100                 tempStatus: this.tempStatus,
101                 currentCourse: this.currentCourse
102             }
103             this.barcodeInput = null;
104             
105             this.pcrud.search('acp', {barcode: args.barcode}, {
106                 flesh: 3, flesh_fields: {acp: ['call_number']}
107             }).subscribe(item => {
108                 let associatedMaterial = this.courseSvc.associateMaterials(item, args);
109                 associatedMaterial.material.then(res => {
110                     item = associatedMaterial.item;
111                     let new_cn = item.call_number().label();
112                     if (this.tempCallNumber) new_cn = this.tempCallNumber;
113                     this.courseSvc.updateItem(item,
114                         this.currentCourse.owning_lib(),
115                         new_cn, args.isModifyingCallNumber).then(resp => {
116                         this.fetchItem(item.id(), args.relationship);
117                         if (item.circ_lib() != this.currentCourse.owning_lib()) {
118                             this.differentLibraryString.current().then(str => this.toast.warning(str));
119                         } else {
120                             this.successString.current().then(str => this.toast.success(str));
121                         }
122                     });
123                 }, err => {
124                     this.failedString.current().then(str => this.toast.danger(str));
125                 });
126             });
127         }
128     }
129
130     fetchMaterials(pager: Pager): Observable<any> {
131         return new Observable<any>(observer => {
132             this.materials.forEach(material => {
133                 this.fetchItem(material.item, material.relationship);
134             });
135             observer.complete();
136         });
137     }
138
139     fetchItem(itemId, relationship): Promise<any> {
140         return new Promise((resolve, reject) => {
141             this.net.request(
142                 'open-ils.circ',
143                 'open-ils.circ.copy_details.retrieve',
144                 this.auth.token(), itemId
145             ).subscribe(res => {
146                 if (res) {
147                     let item = res.copy;
148                     item.call_number(res.volume);
149                     item.circ_lib(this.org.get(item.circ_lib()));
150                     item._title = res.mvr.title();
151                     item._relationship = relationship;
152                     this.gridDataSource.data.push(item);
153                 }
154             }, err => {
155                 reject(err);
156             }, () => resolve(this.gridDataSource.data));
157         });
158     }
159 }