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';
17 selector: 'eg-distribution-formula-edit-dialog',
18 templateUrl: './distribution-formula-edit-dialog.component.html'
21 export class DistributionFormulaEditDialogComponent
22 extends DialogComponent implements OnInit {
24 @Input() mode: string = 'create';
25 @Input() formulaId: number;
26 @Input() cloneSource: number;
28 @ViewChild('defaultCloneLabel', { static: true }) defaultCloneLabel: StringComponent;
30 deadEntries: IdlObject[];
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
48 this.onOpen$.subscribe(() => this._initRecord());
51 private _initRecord() {
53 this.deadEntries = [];
54 this.clonedLabel = '';
55 if (this.mode === 'update') {
56 this.pcrud.retrieve('acqdf', this.formulaId, {
58 flesh_fields: { acqdf: ['entries'] }
61 this._generateFormulaInputs();
63 } else if (this.mode === 'clone') {
64 this.pcrud.retrieve('acqdf', this.cloneSource, {
66 flesh_fields: { acqdf: ['entries'] }
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();
75 } else if (this.mode === 'create') {
76 this.formula = this.idl.create('acqdf');
77 this.formula.entries([]);
78 this._generateFormulaInputs();
82 _generateFormulaInputs() {
83 this.formula.entries().sort((a, b) => { return 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);
90 return this.org.root().id();
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
97 const entry = this.idl.create('acqdfe');
98 entry.id(-9999); // magic placeholder for new record
99 this.formula.entries().push(entry);
101 removeRow(idx: number) {
102 this.deadEntries.push(this.formula.entries().splice(idx, 1)[0]);
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;
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;
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());
123 this.formula.ischanged(true);
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
136 if (entry.owning_lib() == null &&
137 entry.fund() == null &&
138 entry.location() == null &&
139 entry.circ_modifier() == null &&
140 entry.collection_code() == null
142 // this is a pointless entry; ignore
147 if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); }
149 entry.position(idx); // re-writing all the positions
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); }
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);
169 if (entry.item_count() == null) {
170 // we got nothing; ignore
173 if (entry.owning_lib() == null &&
174 entry.fund() == null &&
175 entry.location() == null &&
176 entry.circ_modifier() == null &&
177 entry.collection_code() == null
179 // this is a pointless entry; ignore
184 if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); }
185 entry.position(idx); // re-writing all the positions
191 this.deadEntries.forEach((entry) => {
193 entry.isdeleted(true);
194 updates.unshift(entry); // deletions have to be processed first
197 this.pcrud.autoApply(updates).subscribe(
199 err => this.close(err),
200 () => this.close(true)
202 }, err => this.close(false));