From cef5604ae80f9f7ef24195c4ac28b622b450433f Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Fri, 26 Mar 2021 18:13:01 -0400 Subject: [PATCH] LP#1904244: Angular distribution formulas interface Signed-off-by: Galen Charlton Signed-off-by: Ruth Frasur Signed-off-by: Bill Erickson --- .../admin/acq/admin-acq-splash.component.html | 3 - ...distribution-formula-edit-dialog.component.html | 128 +++++++++++++ .../distribution-formula-edit-dialog.component.ts | 205 +++++++++++++++++++++ .../distribution-formulas.component.html | 97 ++++++++++ .../distribution-formulas.component.ts | 191 +++++++++++++++++++ .../distribution-formulas.module.ts | 27 +++ .../acq/distribution_formula/routing.module.ts | 15 ++ .../eg2/src/app/staff/admin/acq/routing.module.ts | 4 + 8 files changed, 667 insertions(+), 3 deletions(-) create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formula-edit-dialog.component.html create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formula-edit-dialog.component.ts create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.html create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.ts create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.module.ts create mode 100644 Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/routing.module.ts diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/admin-acq-splash.component.html b/Open-ILS/src/eg2/src/app/staff/admin/acq/admin-acq-splash.component.html index f5de0e2db3..202ff6578a 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/acq/admin-acq-splash.component.html +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/admin-acq-splash.component.html @@ -12,10 +12,7 @@ - + + + + + +{{clonedLabel}} (clone) + diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formula-edit-dialog.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formula-edit-dialog.component.ts new file mode 100644 index 0000000000..7c0335c8a9 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formula-edit-dialog.component.ts @@ -0,0 +1,205 @@ +import {Component, Input, ViewChild, TemplateRef, OnInit} from '@angular/core'; +import {DialogComponent} from '@eg/share/dialog/dialog.component'; +import {NgForm, NG_VALIDATORS} from '@angular/forms'; +import {IdlService, IdlObject} from '@eg/core/idl.service'; +import {EventService} from '@eg/core/event.service'; +import {OrgService} from '@eg/core/org.service'; +import {NetService} from '@eg/core/net.service'; +import {AuthService} from '@eg/core/auth.service'; +import {PcrudService} from '@eg/core/pcrud.service'; +import {Pager} from '@eg/share/util/pager'; +import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; +import {StringComponent} from '@eg/share/string/string.component'; +import {ToastService} from '@eg/share/toast/toast.service'; +import {PermService} from '@eg/core/perm.service'; + +@Component({ + selector: 'eg-distribution-formula-edit-dialog', + templateUrl: './distribution-formula-edit-dialog.component.html' +}) + +export class DistributionFormulaEditDialogComponent + extends DialogComponent implements OnInit { + + @Input() mode = 'create'; + @Input() formulaId: number; + @Input() cloneSource: number; + + @ViewChild('defaultCloneLabel', { static: true }) defaultCloneLabel: StringComponent; + formula: IdlObject; + deadEntries: IdlObject[]; + clonedLabel = ''; + + constructor( + private idl: IdlService, + private evt: EventService, + private net: NetService, + private auth: AuthService, + private org: OrgService, + private pcrud: PcrudService, + private perm: PermService, + private toast: ToastService, + private modal: NgbModal + ) { + super(modal); + } + + ngOnInit() { + this.onOpen$.subscribe(() => this._initRecord()); + } + + private _initRecord() { + this.formula = null; + this.deadEntries = []; + this.clonedLabel = ''; + if (this.mode === 'update') { + this.pcrud.retrieve('acqdf', this.formulaId, { + flesh: 1, + flesh_fields: { acqdf: ['entries'] } + }).subscribe(res => { + this.formula = res; + this._generateFormulaInputs(); + }); + } else if (this.mode === 'clone') { + this.pcrud.retrieve('acqdf', this.cloneSource, { + flesh: 1, + flesh_fields: { acqdf: ['entries'] } + }).subscribe(res => { + this.clonedLabel = res.name(); + this.formula = this.idl.clone(res); + this.formula.id(null); + this.defaultCloneLabel.current().then(str => this.formula.name(str)); + this.formula.entries().forEach((e) => e.formula(null)); + this._generateFormulaInputs(); + }); + } else if (this.mode === 'create') { + this.formula = this.idl.create('acqdf'); + this.formula.entries([]); + this._generateFormulaInputs(); + } + } + + _generateFormulaInputs() { + this.formula.entries().sort((a, b) => a.position() < b.position() ? -1 : 1 ); + const entry = this.idl.create('acqdfe'); + entry.id(-9999); // magic placeholder for new record + this.formula.entries().push(entry); + } + + org_root(): number { + return this.org.root().id(); + } + + addRow() { + if (this.formula.entries().slice(-1)[0].id() === -9999) { + this.formula.entries().slice(-1)[0].id(-1); // magic placheholder for new entry that we intend to keep + } + const entry = this.idl.create('acqdfe'); + entry.id(-9999); // magic placeholder for new record + this.formula.entries().push(entry); + } + removeRow(idx: number) { + this.deadEntries.push(this.formula.entries().splice(idx, 1)[0]); + } + moveUp(idx: number) { + const temp = this.formula.entries()[idx - 1]; + this.formula.entries()[idx - 1] = this.formula.entries()[idx]; + this.formula.entries()[idx] = temp; + } + moveDown(idx: number) { + const temp = this.formula.entries()[idx + 1]; + this.formula.entries()[idx + 1] = this.formula.entries()[idx]; + this.formula.entries()[idx] = temp; + } + + + save() { + // grab a copy to preserve the list of entries + const formulaCopy = this.idl.clone(this.formula); + if (this.formula.id() === undefined || this.formula.id() === null) { + this.formula.isnew(true); + this.formula.owner(this.formula.owner().id()); + } else { + this.formula.ischanged(true); + } + this.pcrud.autoApply([this.formula]).subscribe(res => { + const dfId = this.mode === 'update' ? res : res.id(); + const updates: IdlObject[] = []; + if (this.mode === 'create' || this.mode === 'clone') { + formulaCopy.entries().forEach((entry, idx) => { + if (entry.id() === -1) { entry.id(null); } + if (entry.id() === -9999) { entry.id(null); } + if (entry.item_count() == null) { + // we got nothing; ignore + return; + } + if (entry.owning_lib() == null && + entry.fund() == null && + entry.location() == null && + entry.circ_modifier() == null && + entry.collection_code() == null + ) { + // this is a pointless entry; ignore + return; + } + + entry.formula(dfId); + if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); } + entry.id(null); + entry.position(idx); // re-writing all the positions + entry.isnew(true); + updates.push(entry); + }); + } else { + // updating an existing set + formulaCopy.entries().forEach((entry, idx) => { + if (entry.id() === -1) { entry.id(null); } + if (entry.id() === -9999) { entry.id(null); } + if (entry.id()) { + entry.formula(dfId); + entry.position(idx); + if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); } + const delEntry = this.idl.clone(entry); + // have to delete and recreate because of the + // check constraint on formula, position + this.deadEntries.push(delEntry); + entry.isnew(true); + updates.push(entry); + } else { + if (entry.item_count() == null) { + // we got nothing; ignore + return; + } + if (entry.owning_lib() == null && + entry.fund() == null && + entry.location() == null && + entry.circ_modifier() == null && + entry.collection_code() == null + ) { + // this is a pointless entry; ignore + return; + } + + entry.formula(dfId); + if (entry.owning_lib()) { entry.owning_lib(entry.owning_lib().id()); } + entry.position(idx); // re-writing all the positions + entry.isnew(true); + updates.push(entry); + } + }); + } + this.deadEntries.forEach((entry) => { + if (entry.id()) { + entry.isdeleted(true); + updates.unshift(entry); // deletions have to be processed first + } + }); + this.pcrud.autoApply(updates).subscribe( + ret => {}, + err => this.close(err), + () => this.close(true) + ); + }, err => this.close(false)); + } + +} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.html b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.html new file mode 100644 index 0000000000..d9e102a0a1 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.html @@ -0,0 +1,97 @@ + + + + + +{{idlClassDef.label}} Update Succeeded + + +Update of {{idlClassDef.label}} failed + + +Delete of {{idlClassDef.label}} failed or was not allowed + + +{{idlClassDef.label}} Successfully Deleted + + +{{idlClassDef.label}} Successfully Created + + +Failed to create new {{idlClassDef.label}} + + + +
+
+ + + + +
+
+
+
+ + + + + + + + + + {{configLinkLabel(row, col)}} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.ts new file mode 100644 index 0000000000..4b259212e3 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.component.ts @@ -0,0 +1,191 @@ +import {Component, Input, ViewChild, OnInit} from '@angular/core'; +import {Location} from '@angular/common'; +import {FormatService} from '@eg/core/format.service'; +import {GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid'; +import {GridComponent} from '@eg/share/grid/grid.component'; +import {AdminPageComponent} from '@eg/staff/share/admin-page/admin-page.component'; +import {Pager} from '@eg/share/util/pager'; +import {ActivatedRoute} from '@angular/router'; +import {IdlService, IdlObject} from '@eg/core/idl.service'; +import {ToastService} from '@eg/share/toast/toast.service'; +import {PcrudService} from '@eg/core/pcrud.service'; +import {OrgService} from '@eg/core/org.service'; +import {PermService} from '@eg/core/perm.service'; +import {AuthService} from '@eg/core/auth.service'; +import {NetService} from '@eg/core/net.service'; +import {map, mergeMap} from 'rxjs/operators'; +import {StringComponent} from '@eg/share/string/string.component'; +import {DistributionFormulaEditDialogComponent} from './distribution-formula-edit-dialog.component'; +import {Observable, forkJoin, of} from 'rxjs'; +import {AlertDialogComponent} from '@eg/share/dialog/alert.component'; +import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component'; + +@Component({ + templateUrl: './distribution-formulas.component.html' +}) + +export class DistributionFormulasComponent extends AdminPageComponent implements OnInit { + idlClass = 'acqdf'; + classLabel: string; + + @ViewChild('grid', { static: true }) grid: GridComponent; + @ViewChild('distributionFormulaEditDialog', { static: false }) distributionFormulaEditDialog: DistributionFormulaEditDialogComponent; + @ViewChild('alertDialog', {static: false}) private alertDialog: AlertDialogComponent; + @ViewChild('confirmDel', { static: true }) confirmDel: ConfirmDialogComponent; + + notOneSelectedRow: (rows: IdlObject[]) => boolean; + cellTextGenerator: GridCellTextGenerator; + + constructor( + route: ActivatedRoute, + ngLocation: Location, + format: FormatService, + idl: IdlService, + org: OrgService, + auth: AuthService, + pcrud: PcrudService, + perm: PermService, + toast: ToastService, + private net: NetService + ) { + super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast); + this.dataSource = new GridDataSource(); + } + + ngOnInit() { + this.notOneSelectedRow = (rows: IdlObject[]) => (rows.length !== 1); + this.cellTextGenerator = { + name: row => row.name() + }; + this.fieldOrder = 'name,code,year,org,active,currency_type,balance_stop_percentage,balance_warning_percentage,propagate,rollover'; + this.defaultNewRecord = this.idl.create('acqdf'); + this.defaultNewRecord.owner(this.auth.user().ws_ou()); + + this.dataSource.getRows = (pager: Pager, sort: any[]) => { + const orderBy: any = {}; + if (sort.length) { + // Sort specified from grid + orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir; + } else if (this.sortField) { + // Default sort field + orderBy[this.idlClass] = this.sortField; + } + + const searchOps = { + offset: pager.offset, + limit: pager.limit, + order_by: orderBy, + flesh: 1, + flesh_fields: { + acqdf: ['entries'] + } + }; + const reqOps = { + fleshSelectors: true, + }; + + if (!this.contextOrg && !Object.keys(this.dataSource.filters).length) { + // No org filter -- fetch all rows + return this.pcrud.retrieveAll( + this.idlClass, searchOps, reqOps) + .pipe(mergeMap((row) => this.countItems(row))); + } + + const search: any = new Array(); + const orgFilter: any = {}; + + if (this.orgField && (this.searchOrgs || this.contextOrg)) { + orgFilter[this.orgField] = + this.searchOrgs.orgIds || [this.contextOrg.id()]; + search.push(orgFilter); + } + + Object.keys(this.dataSource.filters).forEach(key => { + Object.keys(this.dataSource.filters[key]).forEach(key2 => { + search.push(this.dataSource.filters[key][key2]); + }); + }); + + return this.pcrud.search( + this.idlClass, search, searchOps, reqOps) + .pipe(mergeMap((row) => this.countItems(row))); + }; + + super.ngOnInit(); + + this.classLabel = this.idlClassDef.label; + this.includeOrgDescendants = true; + } + + countItems(row: IdlObject): Observable { + row['item_count'] = 0; + row.entries().forEach((e) => row['item_count'] += e.item_count()); + return of(row); + } + + showEditDistributionFormulaDialog(successString: StringComponent, failString: StringComponent): Promise { + return new Promise((resolve, reject) => { + this.distributionFormulaEditDialog.open({size: 'xl', scrollable: true}).subscribe( + result => { + this.successString.current() + .then(str => this.toast.success(str)); + resolve(result); + }, + error => { + this.updateFailedString.current() + .then(str => this.toast.danger(str)); + reject(error); + }, + () => this.grid.reload() + ); + }); + } + + createNew() { + this.distributionFormulaEditDialog.mode = 'create'; + this.showEditDistributionFormulaDialog(this.createString, this.createErrString); + } + + editSelected(rows: IdlObject[]) { + if (rows.length <= 0) { return; } + this.distributionFormulaEditDialog.mode = 'update'; + this.distributionFormulaEditDialog.formulaId = rows[0].id(); + this.showEditDistributionFormulaDialog(this.successString, this.updateFailedString); + } + + cloneSelected(rows: IdlObject[]) { + if (rows.length <= 0) { return; } + this.distributionFormulaEditDialog.mode = 'clone'; + this.distributionFormulaEditDialog.cloneSource = rows[0].id(); + this.showEditDistributionFormulaDialog(this.createString, this.createErrString); + } + + deleteSelected(rows: IdlObject[]) { + if (rows.length > 0) { + const id = rows[0].id(); + let can = true; + forkJoin([ + this.pcrud.search('acqdfa', { formula: id }, { limit: 1 }, { atomic: true }), + ]).subscribe( + results => { + results.forEach((res) => { + if (res.length > 0) { + can = false; + } + }); + }, + err => {}, + () => { + if (can) { + this.confirmDel.open().subscribe(confirmed => { + if (!confirmed) { return; } + super.deleteSelected([ rows[0] ]); + }); + } else { + this.alertDialog.open(); + } + } + ); + } + } +} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.module.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.module.ts new file mode 100644 index 0000000000..313f4b79bc --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/distribution-formulas.module.ts @@ -0,0 +1,27 @@ +import {NgModule} from '@angular/core'; +import {StaffCommonModule} from '@eg/staff/common.module'; +import {AdminCommonModule} from '@eg/staff/admin/common.module'; +import {DistributionFormulasRoutingModule} from './routing.module'; +import {DistributionFormulasComponent} from './distribution-formulas.component'; +import {DistributionFormulaEditDialogComponent} from './distribution-formula-edit-dialog.component'; +import {ItemLocationSelectModule} from '@eg/share/item-location-select/item-location-select.module'; + +@NgModule({ + declarations: [ + DistributionFormulasComponent, + DistributionFormulaEditDialogComponent + ], + imports: [ + StaffCommonModule, + AdminCommonModule, + ItemLocationSelectModule, + DistributionFormulasRoutingModule + ], + exports: [ + ], + providers: [ + ] +}) + +export class DistributionFormulasModule { +} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/routing.module.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/routing.module.ts new file mode 100644 index 0000000000..e67b402deb --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/distribution_formula/routing.module.ts @@ -0,0 +1,15 @@ +import {NgModule} from '@angular/core'; +import {RouterModule, Routes} from '@angular/router'; +import {DistributionFormulasComponent} from './distribution-formulas.component'; + +const routes: Routes = [{ + path: '', + component: DistributionFormulasComponent +}]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule] +}) + +export class DistributionFormulasRoutingModule {} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/routing.module.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/routing.module.ts index a1a011e22e..0ae8e0a85e 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/acq/routing.module.ts +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/routing.module.ts @@ -32,6 +32,10 @@ const routes: Routes = [{ path: 'claim_type', redirectTo: 'claiming' // from legacy auto-generated admin page }, { + path: 'distribution_formula', + loadChildren: () => + import('./distribution_formula/distribution-formulas.module').then(m => m.DistributionFormulasModule) +}, { path: 'edi_attr_set', loadChildren: () => import('./edi_attr_set/edi-attr-sets.module').then(m => m.EdiAttrSetsModule) -- 2.11.0