cf13857737793d7d6bf76d55ad6fbe3fb6e3be9c
[working/Evergreen.git] /
1 import {Component, Input, ViewChild, TemplateRef, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {NgForm} from '@angular/forms';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {EventService} from '@eg/core/event.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {NetService} from '@eg/core/net.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {Pager} from '@eg/share/util/pager';
11 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
14 import {PermService} from '@eg/core/perm.service';
15
16 @Component({
17   selector: 'eg-distribution-formula-edit-dialog',
18   templateUrl: './distribution-formula-edit-dialog.component.html'
19 })
20
21 export class DistributionFormulaEditDialogComponent
22   extends DialogComponent implements OnInit {
23
24     @Input() mode = 'create';
25     @Input() formulaId: number;
26     @Input() cloneSource: number;
27
28     @ViewChild('defaultCloneLabel', { static: true }) defaultCloneLabel: StringComponent;
29     formula: IdlObject;
30     deadEntries: IdlObject[];
31     clonedLabel = '';
32
33     constructor(
34         private idl: IdlService,
35         private evt: EventService,
36         private net: NetService,
37         private auth: AuthService,
38         private org: OrgService,
39         private pcrud: PcrudService,
40         private perm: PermService,
41         private toast: ToastService,
42         private modal: NgbModal
43     ) {
44         super(modal);
45     }
46
47     ngOnInit() {
48         this.onOpen$.subscribe(() => this._initRecord());
49     }
50
51     private _initRecord() {
52         this.formula = null;
53         this.deadEntries = [];
54         this.clonedLabel = '';
55         if (this.mode === 'update') {
56             this.pcrud.retrieve('acqdf', this.formulaId, {
57                 flesh: 1,
58                 flesh_fields: { acqdf: ['entries'] }
59             }).subscribe(res => {
60                 this.formula = res;
61                 this._generateFormulaInputs();
62             });
63         } else if (this.mode === 'clone') {
64             this.pcrud.retrieve('acqdf', this.cloneSource, {
65                 flesh: 1,
66                 flesh_fields: { acqdf: ['entries'] }
67             }).subscribe(res => {
68                 this.clonedLabel = res.name();
69                 this.formula = this.idl.clone(res);
70                 this.formula.id(null);
71                 this.defaultCloneLabel.current().then(str => this.formula.name(str));
72                 this.formula.entries().forEach((e) => e.formula(null));
73                 this._generateFormulaInputs();
74             });
75         } else if (this.mode === 'create') {
76             this.formula = this.idl.create('acqdf');
77             this.formula.entries([]);
78             this._generateFormulaInputs();
79         }
80     }
81
82     _generateFormulaInputs() {
83         this.formula.entries().sort((a, b) => a.position() < b.position() ? -1 : 1 );
84         const entry = this.idl.create('acqdfe');
85         entry.id(-9999); // magic placeholder for new record
86         this.formula.entries().push(entry);
87     }
88
89     org_root(): number {
90         return this.org.root().id();
91     }
92
93     addRow() {
94         if (this.formula.entries().slice(-1)[0].id() === -9999) {
95             this.formula.entries().slice(-1)[0].id(-1); // magic placheholder for new entry that we intend to keep
96         }
97         const entry = this.idl.create('acqdfe');
98         entry.id(-9999); // magic placeholder for new record
99         this.formula.entries().push(entry);
100     }
101     removeRow(idx: number) {
102         this.deadEntries.push(this.formula.entries().splice(idx, 1)[0]);
103     }
104     moveUp(idx: number) {
105         const temp = this.formula.entries()[idx - 1];
106         this.formula.entries()[idx - 1] = this.formula.entries()[idx];
107         this.formula.entries()[idx] = temp;
108     }
109     moveDown(idx: number) {
110         const temp = this.formula.entries()[idx + 1];
111         this.formula.entries()[idx + 1] = this.formula.entries()[idx];
112         this.formula.entries()[idx] = temp;
113     }
114
115
116     save() {
117         // grab a copy to preserve the list of entries
118         const formulaCopy = this.idl.clone(this.formula);
119         if (this.formula.id() === undefined || this.formula.id() === null) {
120             this.formula.isnew(true);
121             this.formula.owner(this.formula.owner().id());
122         } else {
123             this.formula.ischanged(true);
124         }
125         this.pcrud.autoApply([this.formula]).subscribe(res => {
126             const dfId = this.mode === 'update' ? res : res.id();
127             const updates: IdlObject[] = [];
128             if (this.mode === 'create' || this.mode === 'clone') {
129                 formulaCopy.entries().forEach((entry, idx) => {
130                     if (entry.id() === -1) { entry.id(null); }
131                     if (entry.id() === -9999) { entry.id(null); }
132                     if (entry.item_count() == null) {
133                         // we got nothing; ignore
134                         return;
135                     }
136                     if (entry.owning_lib() == null &&
137                         entry.fund() == null &&
138                         entry.location() == null &&
139                         entry.circ_modifier() == null &&
140                         entry.collection_code() == null
141                        ) {
142                         // this is a pointless entry; ignore
143                         return;
144                     }
145
146                     entry.formula(dfId);
147                     if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); }
148                     entry.id(null);
149                     entry.position(idx); // re-writing all the positions
150                     entry.isnew(true);
151                     updates.push(entry);
152                 });
153             } else {
154                 // updating an existing set
155                 formulaCopy.entries().forEach((entry, idx) => {
156                     if (entry.id() === -1) { entry.id(null); }
157                     if (entry.id() === -9999) { entry.id(null); }
158                     if (entry.id()) {
159                         entry.formula(dfId);
160                         entry.position(idx);
161                         if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); }
162                         const delEntry = this.idl.clone(entry);
163                         // have to delete and recreate because of the
164                         // check constraint on formula, position
165                         this.deadEntries.push(delEntry);
166                         entry.isnew(true);
167                         updates.push(entry);
168                     } else {
169                         if (entry.item_count() == null) {
170                             // we got nothing; ignore
171                             return;
172                         }
173                         if (entry.owning_lib() == null &&
174                             entry.fund() == null &&
175                             entry.location() == null &&
176                             entry.circ_modifier() == null &&
177                             entry.collection_code() == null
178                         ) {
179                             // this is a pointless entry; ignore
180                             return;
181                         }
182
183                         entry.formula(dfId);
184                         if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); }
185                         entry.position(idx); // re-writing all the positions
186                         entry.isnew(true);
187                         updates.push(entry);
188                     }
189                 });
190             }
191             this.deadEntries.forEach((entry) => {
192                 if (entry.id()) {
193                     entry.isdeleted(true);
194                     updates.unshift(entry); // deletions have to be processed first
195                 }
196             });
197             this.pcrud.autoApply(updates).subscribe(
198                 ret => {},
199                 err => this.close(err),
200                 () => this.close(true)
201             );
202         }, err => this.close(false));
203     }
204
205 }