From: Galen Charlton Date: Fri, 26 Mar 2021 22:21:30 +0000 (-0400) Subject: LP#1904244: Angular currency and exchange rates interface X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=0e3e64e693f3e4b18cbc4509b96459dd770ac7ea;p=Evergreen.git LP#1904244: Angular currency and exchange rates interface Signed-off-by: Galen Charlton Signed-off-by: Ruth Frasur Signed-off-by: Bill Erickson --- 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 202ff6578a..27a168854c 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 @@ -9,8 +9,8 @@ routerLink="/staff/admin/acq/cancel_reason"> - + - + + + + +{{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/currency/currencies.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/currencies.component.ts new file mode 100644 index 0000000000..a9fc0e9bb6 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/currencies.component.ts @@ -0,0 +1,155 @@ +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 {StringComponent} from '@eg/share/string/string.component'; +import {ExchangeRatesDialogComponent} from './exchange-rates-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: './currencies.component.html' +}) + +export class CurrenciesComponent extends AdminPageComponent implements OnInit { + idlClass = 'acqct'; + classLabel: string; + + @ViewChild('grid', { static: true }) grid: GridComponent; + @ViewChild('exchangeRatesDialog', { static: false }) exchangeRatesDialog: ExchangeRatesDialogComponent; + @ViewChild('alertDialog', {static: false}) private alertDialog: AlertDialogComponent; + @ViewChild('confirmDel', { static: true }) confirmDel: ConfirmDialogComponent; + + cellTextGenerator: GridCellTextGenerator; + notOneSelectedRow: (rows: IdlObject[]) => boolean; + + 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 = { + exchange_rates: row => '' + }; + this.fieldOrder = 'code,name'; + this.defaultNewRecord = this.idl.create('acqct'); + + 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 + }; + 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); + } + + 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); + }; + + super.ngOnInit(); + + this.classLabel = this.idlClassDef.label; + this.includeOrgDescendants = true; + } + + openExchangeRatesDialog(code: string) { + this.exchangeRatesDialog.currencyCode = code; + this.exchangeRatesDialog.open({size: 'lg'}); + } + + deleteIfPossible(rows: IdlObject[]) { + if (rows.length > 0) { + const code = rows[0].code(); + let can = true; + forkJoin([ + this.pcrud.search('acqexr', { from_currency: code }, { limit: 1 }, { atomic: true }), + this.pcrud.search('acqexr', { to_currency: code }, { limit: 1 }, { atomic: true }), + this.pcrud.search('acqf', { currency_type: code }, { limit: 1 }, { atomic: true }), + this.pcrud.search('acqpro', { currency_type: code }, { limit: 1 }, { atomic: true }), + this.pcrud.search('acqfdeb', { origin_currency_type: code }, { limit: 1 }, { atomic: true }), + this.pcrud.search('acqfs', { currency_type: code }, { 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(); + } + } + ); + } + } + + calculateReadonlyFields(mode: string) { + return mode === 'update' ? 'code' : ''; + } + +} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/currencies.module.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/currencies.module.ts new file mode 100644 index 0000000000..9b4f2582e2 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/currencies.module.ts @@ -0,0 +1,25 @@ +import {NgModule} from '@angular/core'; +import {StaffCommonModule} from '@eg/staff/common.module'; +import {AdminCommonModule} from '@eg/staff/admin/common.module'; +import {CurrenciesRoutingModule} from './routing.module'; +import {CurrenciesComponent} from './currencies.component'; +import {ExchangeRatesDialogComponent} from './exchange-rates-dialog.component'; + +@NgModule({ + declarations: [ + CurrenciesComponent, + ExchangeRatesDialogComponent + ], + imports: [ + StaffCommonModule, + AdminCommonModule, + CurrenciesRoutingModule + ], + exports: [ + ], + providers: [ + ] +}) + +export class CurrenciesModule { +} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/exchange-rates-dialog.component.html b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/exchange-rates-dialog.component.html new file mode 100644 index 0000000000..1459072eb6 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/exchange-rates-dialog.component.html @@ -0,0 +1,49 @@ + + + + + + + + diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/exchange-rates-dialog.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/exchange-rates-dialog.component.ts new file mode 100644 index 0000000000..045a757405 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/exchange-rates-dialog.component.ts @@ -0,0 +1,141 @@ +import {Component, Input, ViewChild, TemplateRef, OnInit} from '@angular/core'; +import {DialogComponent} from '@eg/share/dialog/dialog.component'; +import {NgForm} from '@angular/forms'; +import {IdlService, IdlObject} from '@eg/core/idl.service'; +import {EventService} from '@eg/core/event.service'; +import {NetService} from '@eg/core/net.service'; +import {AuthService} from '@eg/core/auth.service'; +import {PcrudService} from '@eg/core/pcrud.service'; +import {GridDataSource} from '@eg/share/grid/grid'; +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-exchange-rates-dialog', + templateUrl: './exchange-rates-dialog.component.html' +}) + +export class ExchangeRatesDialogComponent + extends DialogComponent implements OnInit { + + @Input() currencyCode: string; + currency: IdlObject; + otherCurrencies: IdlObject[]; + existingRatios: {[toCurrency: string]: IdlObject} = {}; + existingInverseRatios: {[fromCurrency: string]: IdlObject} = {}; + ratios: IdlObject[]; + idlDef: any; + fieldOrder: any; + canUpdate = false; + doneLoading = false; + + @ViewChild('successString', { static: true }) successString: StringComponent; + @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent; + + constructor( + private idl: IdlService, + private evt: EventService, + private net: NetService, + private auth: AuthService, + private pcrud: PcrudService, + private perm: PermService, + private toast: ToastService, + private modal: NgbModal + ) { + super(modal); + } + + ngOnInit() { + this.currency = null; + this.onOpen$.subscribe(() => this._initRecord()); + this.idlDef = this.idl.classes['acqct']; + this.perm.hasWorkPermAt(['ADMIN_CURRENCY_TYPE'], true).then((perm) => { + if (perm['ADMIN_CURRENCY_TYPE'].length > 0) { + this.canUpdate = true; + } + }); + } + + private _initRecord() { + this.doneLoading = false; + this.ratios = []; + this.otherCurrencies = []; + this.existingRatios = {}; + this.existingInverseRatios = {}; + this.pcrud.retrieve('acqct', this.currencyCode, {} + ).subscribe(res => this.currency = res); + this.pcrud.search('acqexr', { from_currency: this.currencyCode }, { + flesh: 1, + flesh_fields: {'acqexr': ['to_currency']}, + }, {}).subscribe( + exr => this.existingRatios[exr.to_currency().code()] = exr, + err => {}, + () => this.pcrud.search('acqexr', { to_currency: this.currencyCode }, { + flesh: 1, + flesh_fields: {'acqexr': ['from_currency']}, + }, {}).subscribe( + exr => this.existingInverseRatios[exr.from_currency().code()] = exr, + err => {}, + () => this.pcrud.search('acqct', { code: { '!=': this.currencyCode } }, + { order_by: 'code ASC' }, { atomic: true }) + .subscribe( + currs => this.otherCurrencies = currs, + err => {}, + () => { this._mergeCurrenciesAndRates(); this.doneLoading = true; } + ) + ) + ); + } + + private _mergeCurrenciesAndRates() { + this.ratios = []; + this.otherCurrencies.forEach(curr => { + if (curr.code() in this.existingRatios) { + this.ratios.push(this.existingRatios[curr.code()]); + } else if (curr.code() in this.existingInverseRatios) { + const ratio = this.idl.clone(this.existingInverseRatios[curr.code()]); + // mark it as an inverse ratio that should not be directly edited + ratio.id(-1); + const toCur = ratio.to_currency(); + ratio.to_currency(ratio.from_currency()); + ratio.from_currency(toCur); + ratio.ratio(1.0 / ratio.ratio()); + this.ratios.push(ratio); + } else { + const ratio = this.idl.create('acqexr'); + ratio.from_currency(this.currencyCode); + ratio.to_currency(curr); + this.ratios.push(ratio); + } + }); + this.ratios.sort((a, b) => { + return a.to_currency().code() < b.to_currency().code() ? -1 : 1; + }); + } + + save() { + const updateBatch: IdlObject[] = []; + this.ratios.forEach(ratio => { + if (ratio.id() === -1) { + // ignore inverse entries + } else if (ratio.id() === undefined && ratio.ratio() !== undefined && ratio.ratio() !== null) { + // completely new entry + ratio.isnew(true); + updateBatch.push(ratio); + } else if (ratio.id() !== undefined && ratio.ratio() !== undefined && ratio.ratio() !== null) { + // entry that might have been updated + ratio.ischanged(true); + updateBatch.push(ratio); + } else if (ratio.id() !== undefined && (ratio.ratio() === undefined || ratio.ratio() === null)) { + // existing entry to delete + ratio.isdeleted(true); + updateBatch.push(ratio); + } + }); + this.pcrud.autoApply(updateBatch).toPromise().then(res => this.close(res)); + } + +} diff --git a/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/routing.module.ts b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/routing.module.ts new file mode 100644 index 0000000000..b906726fb7 --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/admin/acq/currency/routing.module.ts @@ -0,0 +1,15 @@ +import {NgModule} from '@angular/core'; +import {RouterModule, Routes} from '@angular/router'; +import {CurrenciesComponent} from './currencies.component'; + +const routes: Routes = [{ + path: '', + component: CurrenciesComponent +}]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule] +}) + +export class CurrenciesRoutingModule {} 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 0ae8e0a85e..254f650d65 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,16 @@ const routes: Routes = [{ path: 'claim_type', redirectTo: 'claiming' // from legacy auto-generated admin page }, { + path: 'currency', + loadChildren: () => + import('./currency/currencies.module').then(m => m.CurrenciesModule) +}, { + path: 'currency_type', + redirectTo: 'currency' // from auto-generated admin page +}, { + path: 'exchange_rate', + redirectTo: 'currency' // from auto-generated admin page +}, { path: 'distribution_formula', loadChildren: () => import('./distribution_formula/distribution-formulas.module').then(m => m.DistributionFormulasModule)