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';
14 const BATCH_FIELDS = [
24 templateUrl: 'batch-copies.component.html',
25 selector: 'eg-lineitem-batch-copies',
26 styleUrls: ['batch-copies.component.css']
28 export class LineitemBatchCopiesComponent implements OnInit {
30 @Input() lineitem: IdlObject;
32 @ViewChild('confirmAlertsDialog') confirmAlertsDialog: ConfirmDialogComponent;
33 @ViewChild('cancelDialog') cancelDialog: CancelDialogComponent;
35 // Current alert that needs confirming
39 private evt: EventService,
40 private idl: IdlService,
41 private net: NetService,
42 private auth: AuthService,
43 private liService: LineitemService
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 => {
55 copy.ischanged(true); // isnew() takes precedence
60 deleteCopy(copy: IdlObject) {
62 // Brand new copies can be discarded
63 this.lineitem.lineitem_details(
64 this.lineitem.lineitem_details().filter(c => c.id() !== copy.id())
67 // Requires a Save Changes action.
73 this.liService.getFleshedLineitems([this.lineitem.id()], {toCache: true})
74 .subscribe(liStruct => this.lineitem = liStruct.lineitem);
77 handleActionResponse(resp: any) {
78 const evt = this.evt.parse(resp);
82 this.refreshLineitem();
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));
96 receiveCopy(copy: IdlObject) {
97 this.checkLiAlerts().then(ok => {
100 'open-ils.acq.lineitem_detail.receive',
101 this.auth.token(), copy.id()
102 ).subscribe(ok2 => this.handleActionResponse(ok2));
103 }, err => {}); // avoid console errors
106 unReceiveCopy(copy: IdlObject) {
109 'open-ils.acq.lineitem_detail.receive.rollback',
110 this.auth.token(), copy.id()
111 ).subscribe(ok => this.handleActionResponse(ok));
114 checkLiAlerts(): Promise<boolean> {
116 let promise = Promise.resolve(true);
118 const notes = this.lineitem.lineitem_notes().filter(note =>
119 note.alert_text() && !this.liService.alertAcks[note.id()]);
121 if (notes.length === 0) { return promise; }
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;