]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/lineitem/copy-attrs.component.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / lineitem / copy-attrs.component.ts
1 import {Component, OnInit, AfterViewInit, ViewChild, Input, Output, EventEmitter} 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 {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {LineitemService} from './lineitem.service';
8 import {ComboboxComponent, ComboboxEntry} from '@eg/share/combobox/combobox.component';
9 import {ItemLocationService} from '@eg/share/item-location-select/item-location-select.service';
10 import {ItemLocationSelectComponent} from '@eg/share/item-location-select/item-location-select.component';
11
12 @Component({
13   templateUrl: 'copy-attrs.component.html',
14   selector: 'eg-lineitem-copy-attrs'
15 })
16 export class LineitemCopyAttrsComponent implements OnInit {
17
18     @Input() lineitem: IdlObject;
19     fundEntries: ComboboxEntry[];
20     circModEntries: ComboboxEntry[];
21
22     private _copy: IdlObject;
23     @Input() set copy(c: IdlObject) { // acqlid
24         if (c === undefined) {
25             return;
26         } else if (c === null) {
27             this._copy = null;
28         } else {
29             // Enture cbox entries are populated before the copy is
30             // applied so the cbox has the minimal set of values it
31             // needs at copy render time.
32             this.setInitialOptions(c);
33             this._copy = c;
34         }
35     }
36
37     get copy(): IdlObject {
38         return this._copy;
39     }
40
41     // A row of batch edit inputs
42     @Input() batchMode = false;
43
44     // One of several rows embedded in the main LI list page.
45     // Always read-only.
46     @Input() embedded = false;
47
48     // Emits an 'acqlid' object;
49     @Output() batchApplyRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
50     @Output() deleteRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
51     @Output() receiveRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
52     @Output() unReceiveRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
53     @Output() cancelRequested: EventEmitter<IdlObject> = new EventEmitter<IdlObject>();
54
55     @ViewChild('locationSelector') locationSelector: ItemLocationSelectComponent;
56     @ViewChild('circModSelector') circModSelector: ComboboxComponent;
57     @ViewChild('fundSelector') fundSelector: ComboboxComponent;
58
59     constructor(
60         private idl: IdlService,
61         private net: NetService,
62         private auth: AuthService,
63         private loc: ItemLocationService,
64         private liService: LineitemService
65     ) {}
66
67     ngOnInit() {
68
69         if (this.batchMode) { // stub batch copy
70             this.copy = this.idl.create('acqlid');
71             this.copy.isnew(true);
72
73         } else {
74
75             // When a batch selector value changes, duplicate the selected
76             // value into our selector entries, so if/when the value is
77             // chosen we (and our pile of siblings) are not required to
78             // re-fetch them from the server.
79             this.liService.batchOptionWanted.subscribe(option => {
80                 const field = Object.keys(option)[0];
81                 if (field === 'location') {
82                     this.locationSelector.comboBox.addAsyncEntry(option[field]);
83                 } else if (field === 'circ_modifier') {
84                     this.circModSelector.addAsyncEntry(option[field]);
85                 } else if (field === 'fund') {
86                     this.fundSelector.addAsyncEntry(option[field]);
87                 }
88             });
89         }
90     }
91
92     valueChange(field: string, entry: ComboboxEntry) {
93
94         const announce: any = {};
95         this.copy.ischanged(true);
96
97         switch (field) {
98
99             case 'cn_label':
100             case 'barcode':
101             case 'collection_code':
102                 this.copy[field](entry);
103                 break;
104
105             case 'owning_lib':
106                 this.copy[field](entry ? entry.id() : null);
107                 break;
108
109             case 'location':
110                 this.copy[field](entry ? entry.id() : null);
111                 if (this.batchMode) {
112                     announce[field] = entry;
113                     this.liService.batchOptionWanted.emit(announce);
114                 }
115                 break;
116
117             case 'circ_modifier':
118             case 'fund':
119                 this.copy[field](entry ? entry.id : null);
120                 if (this.batchMode) {
121                     announce[field] = entry;
122                     this.liService.batchOptionWanted.emit(announce);
123                 }
124                 break;
125         }
126     }
127
128     // Tell our inputs about the values we know we need
129     // Values will be pre-cached in the liService
130     setInitialOptions(copy: IdlObject) {
131
132         if (copy.fund()) {
133             const fund = this.liService.fundCache[copy.fund()];
134             this.fundEntries = [{id: fund.id(), label: fund.code(), fm: fund}];
135         }
136
137         if (copy.circ_modifier()) {
138             const mod = this.liService.circModCache[copy.circ_modifier()];
139             this.circModEntries = [{id: mod.code(), label: mod.name(), fm: mod}];
140         }
141     }
142
143     fieldIsDisabled(field: string) {
144         if (this.batchMode) { return false; }
145
146         if (this.embedded || // inline expandy view
147             this.copy.isdeleted() ||
148             this.disposition() !== 'pre-order') {
149             return true;
150         }
151
152         return false;
153     }
154
155     disposition(): 'canceled' | 'delayed' | 'received' | 'on-order' | 'pre-order' {
156         if (!this.copy || !this.lineitem) {
157             return null;
158         } else if (this.copy.cancel_reason()) {
159             if (this.copy.cancel_reason().keep_debits() === 't') {
160                 return 'delayed';
161             } else {
162                 return 'canceled';
163             }
164         } else if (this.copy.recv_time()) {
165             return 'received';
166         } else if (this.lineitem.state() === 'on-order') {
167             return 'on-order';
168         } else { return 'pre-order'; }
169     }
170 }
171
172