]> 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 Move Course Reserves to Local Admin
[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 './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             this.pcrud.search('acp', {barcode: barcode},
92               {flesh: 3, flesh_fields: {acp: ['call_number']}}).subscribe(item => {
93                 let material = this.idl.create('acmcm');
94                 material.item(item.id());
95                 material.course(this.currentCourse.id());
96                 if (relationship) material.relationship(relationship);
97                 if (this.isModifyingStatus && this.tempStatus) {
98                     material.original_status(item.status());
99                     item.status(this.tempStatus);
100                 }
101                 if (this.isModifyingLocation && this.tempLocation) {
102                     material.original_location(item.location());
103                     item.location(this.tempLocation);
104                 }
105                 if (this.isModifyingCircMod) {
106                     material.original_circ_modifier(item.circ_modifier());
107                     item.circ_modifier(this.tempCircMod);
108                     if (!this.tempCircMod) item.circ_modifier(null);
109                 }
110                 if (this.isModifyingCallNumber) {
111                     material.original_callnumber(item.call_number());
112                 }
113                 this.pcrud.create(material).subscribe(
114                 val => {
115                    console.debug('created: ' + val);
116                    let new_cn = item.call_number().label();
117                    if (this.tempCallNumber) new_cn = this.tempCallNumber;
118                     this.courseSvc.updateItem(item, this.currentCourse.owning_lib(), new_cn, this.isModifyingCallNumber).then(res => {
119                         this.fetchItem(item.id(), relationship);                        
120                         if (item.circ_lib() != this.currentCourse.owning_lib()) {
121                             this.differentLibraryString.current().then(str => this.toast.warning(str));
122                         } else {
123                             this.successString.current().then(str => this.toast.success(str));
124                         }
125                     });
126
127                     // Cleaning up inputs
128                     this.barcodeInput = "";
129                     this.relationshipInput = "";
130                     this.tempStatus = null;
131                     this.tempCircMod = null;
132                     this.tempCallNumber = null;
133                     this.tempLocation = null;
134                     this.isModifyingCallNumber = false;
135                     this.isModifyingCircMod = false;
136                     this.isModifyingLocation = false;
137                     this.isModifyingStatus = false;
138                 }, err => {
139                     this.failedString.current().then(str => this.toast.danger(str));
140                 });
141             });
142         }
143     }
144
145     fetchMaterials(pager: Pager): Observable<any> {
146         return new Observable<any>(observer => {
147             this.materials.forEach(material => {
148                 this.fetchItem(material.item, material.relationship);
149             });
150             observer.complete();
151         });
152     }
153
154     fetchItem(itemId, relationship): Promise<any> {
155         return new Promise((resolve, reject) => {
156             this.net.request(
157                 'open-ils.circ',
158                 'open-ils.circ.copy_details.retrieve',
159                 this.auth.token(), itemId
160             ).subscribe(res => {
161                 if (res) {
162                     let item = res.copy;
163                     item.call_number(res.volume);
164                     item.circ_lib(this.org.get(item.circ_lib()));
165                     item._title = res.mvr.title();
166                     item._relationship = relationship;
167                     this.gridDataSource.data.push(item);
168                 }
169             }, err => {
170                 reject(err);
171             }, () => resolve(this.gridDataSource.data));
172         });
173     }
174 }