1 import {Component, ViewChild} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {NetService} from '@eg/core/net.service';
4 import {AuthService} from '@eg/core/auth.service';
5 import {EventService} from '@eg/core/event.service';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
8 import {AlertDialogComponent} from '@eg/share/dialog/alert.component';
9 import {StringComponent} from '@eg/share/string/string.component';
11 /* Transfer items to a call number. */
14 selector: 'eg-transfer-items',
15 templateUrl: 'transfer-items.component.html'
18 export class TransferItemsComponent {
20 @ViewChild('successMsg', {static: false})
21 private successMsg: StringComponent;
23 @ViewChild('errorMsg', {static: false})
24 private errorMsg: StringComponent;
26 @ViewChild('noTargetMsg', {static: false})
27 private noTargetMsg: StringComponent;
29 @ViewChild('confirmDialog', {static: false})
30 private confirmDialog: ConfirmDialogComponent;
32 @ViewChild('alertDialog', {static: false})
33 private alertDialog: AlertDialogComponent;
38 private toast: ToastService,
39 private net: NetService,
40 private auth: AuthService,
41 private evt: EventService) {}
43 // Transfers a set of items/copies (by ID) to the selected call
45 // Resolves with true if transfer completed, false otherwise.
46 transferItems(itemIds: number[],
47 cnId: number, override?: boolean): Promise<boolean> {
51 let method = 'open-ils.cat.transfer_copies_to_volume';
52 if (override) { method += '.override'; }
54 return this.net.request('open-ils.cat',
55 method, this.auth.token(), cnId, itemIds)
56 .toPromise().then(resp => {
58 const evt = this.evt.parse(resp);
63 // Override failed, no looping please.
64 this.toast.warning(this.errorMsg.text);
68 this.eventDesc = evt.desc;
70 return this.confirmDialog.open().toPromise().then(ok =>
71 ok ? this.transferItems(itemIds, cnId, true) : false);
75 this.toast.success(this.successMsg.text);
81 // Transfers a set of items/copies (by object with fleshed call numbers)
82 // to the selected record and org unit ID, creating new call numbers
84 // Resolves with true if transfer completed, false otherwise.
85 autoTransferItems(items: IdlObject[], // acp with fleshed call_number's
86 recId: number, orgId: number): Promise<Boolean> {
90 const cnTransfers: any = {};
91 const itemTransfers: any = {};
93 items.forEach(item => {
94 const cn = item.call_number();
96 if (cn.owning_lib() !== orgId || cn.record() !== recId) {
100 if (cnTransfers[cn.id()]) {
101 itemTransfers[cn.id()].push(item.id());
104 cnTransfers[cn.id()] = cn;
105 itemTransfers[cn.id()] = [item.id()];
110 return this.transferCallNumbers(cnTransfers, itemTransfers);
113 transferCallNumbers(cnTransfers, itemTransfers): Promise<boolean> {
115 const cnId = Object.keys(cnTransfers)[0];
116 const cn = cnTransfers[cnId];
117 delete cnTransfers[cnId];
119 return this.net.request('open-ils.cat',
120 'open-ils.cat.call_number.find_or_create',
123 cn.record(), // may be new
124 cn.owning_lib(), // may be new
125 (typeof cn.prefix() === 'object' ? cn.prefix().id() : cn.prefix()),
126 (typeof cn.suffix() === 'object' ? cn.suffix().id() : cn.suffix()),
129 ).toPromise().then(resp => {
131 const evt = this.evt.parse(resp);
134 // Problem. Stop processing.
135 this.toast.warning(this.errorMsg.text);
136 this.eventDesc = evt.desc;
137 return this.alertDialog.open().toPromise().then(_ => false);
140 return this.transferItems(itemTransfers[cn.id()], resp.acn_id)
143 if (ok && Object.keys(cnTransfers).length > 0) {
144 // More call numbers to transfer.
145 return this.transferCallNumbers(cnTransfers, itemTransfers);