From b9096fe7dfbdf5a483608e5d1169184a6afbeb70 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 3 Aug 2022 11:28:12 +0200 Subject: [PATCH] Bug 32030: Filter by expired agreements This patch adds a checkbox and flatpickr input to filter agreements by expiration date. Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/Agreements.pm | 41 +++++++++++++- Koha/REST/V1/ERM/Agreements.pm | 5 ++ api/v1/swagger/paths/erm_agreements.yaml | 5 ++ .../js/vue/components/ERM/AgreementsList.vue | 55 ++++++++++++++++++- .../ERM/EHoldingsEBSCOPackagesList.vue | 18 ++---- .../ERM/EHoldingsEBSCOTitlesList.vue | 18 ++---- .../prog/js/vue/components/ERM/ERMMain.vue | 2 +- .../prog/js/vue/composables/datatables.js | 11 ++++ 8 files changed, 122 insertions(+), 33 deletions(-) diff --git a/Koha/ERM/Agreements.pm b/Koha/ERM/Agreements.pm index 9773db4180..6001c4727c 100644 --- a/Koha/ERM/Agreements.pm +++ b/Koha/ERM/Agreements.pm @@ -16,11 +16,13 @@ package Koha::ERM::Agreements; # along with Koha; if not, see . use Modern::Perl; - +use DateTime; use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); use Koha::ERM::Agreement; +use Koha::ERM::Agreement::Periods; use base qw(Koha::Objects); @@ -34,6 +36,43 @@ Koha::ERM::Agreements- Koha ErmAgreement Object set class =cut +=head3 filter_by_expired + +=cut + +sub filter_by_expired { + my ($self, $max_expiration_date) = @_; + + $max_expiration_date = + $max_expiration_date + ? dt_from_string( $max_expiration_date, 'iso' ) + : dt_from_string; + + my $periods = Koha::ERM::Agreement::Periods->search( + { agreement_id => [ $self->get_column('agreement_id') ] }, + { + select => [ + 'agreement_id', + \do {"CASE WHEN MAX(me.ended_on IS NULL) = 0 THEN max(me.ended_on) END"} + ], + as => [ 'agreement_id', 'max_ended_on' ], + group_by => ['agreement_id'], + } + ); + + my @expired_agreement_ids; + while ( my $p = $periods->next ) { + # FIXME Can this be moved in the HAVING clause of the previous query? + my $max_ended_on = $p->get_column('max_ended_on'); + next unless $max_ended_on; + my $max_ended_on_dt = dt_from_string($max_ended_on); + next if DateTime->compare( $max_ended_on_dt, $max_expiration_date ) == 1; + push @expired_agreement_ids, $p->agreement_id; + } + + return $self->search( { agreement_id => \@expired_agreement_ids } ); +} + =head3 type =cut diff --git a/Koha/REST/V1/ERM/Agreements.pm b/Koha/REST/V1/ERM/Agreements.pm index 53fda67988..c93af41201 100644 --- a/Koha/REST/V1/ERM/Agreements.pm +++ b/Koha/REST/V1/ERM/Agreements.pm @@ -35,8 +35,13 @@ use Try::Tiny qw( catch try ); sub list { my $c = shift->openapi->valid_input or return; + my $max_expiration_date = delete $c->validation->output->{max_expiration_date}; + return try { my $agreements_set = Koha::ERM::Agreements->new; + if ( $max_expiration_date ) { + $agreements_set = $agreements_set->filter_by_expired( $max_expiration_date ); + } my $agreements = $c->objects->search( $agreements_set ); return $c->render( status => 200, openapi => $agreements ); } diff --git a/api/v1/swagger/paths/erm_agreements.yaml b/api/v1/swagger/paths/erm_agreements.yaml index 6c311d53a5..2f3e7e3f1c 100644 --- a/api/v1/swagger/paths/erm_agreements.yaml +++ b/api/v1/swagger/paths/erm_agreements.yaml @@ -54,6 +54,11 @@ name: license_info required: false type: string + - description: filter by expired agreements + in: query + name: max_expiration_date + type: string + format: date - $ref: "../swagger.yaml#/parameters/match" - $ref: "../swagger.yaml#/parameters/order_by" - $ref: "../swagger.yaml#/parameters/page" diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue index 5ad63035ba..fc1f021189 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue @@ -2,6 +2,28 @@
{{ $t("Loading") }}
+
+ + + {{ $t("on") }} + + + +
{{ $t("There are no agreements defined") }} @@ -10,13 +32,14 @@