Bug 31158: (bug 23991 follow-up) Fix suggestion search by dates
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 1 Aug 2022 06:14:27 +0000 (08:14 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 16 Aug 2022 12:15:56 +0000 (09:15 -0300)
Search suggestions by date is broken, we don't remove the '_from' CGI
params for the DBIC query
DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: Unknown column 'suggesteddate_from' in 'where clause' at /kohadevbox/koha/Koha/Objects.pm line 394
 at /usr/share/perl5/DBIx/Class/Exception.pm line 77

Test plan:
Create some suggestions, search for them using date range (suggested
date, managed date and accepted date)

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
suggestion/suggestion.pl

index c383672..e0c571b 100755 (executable)
@@ -384,25 +384,20 @@ if ($op=~/else/) {
 
         # filter on date fields
         foreach my $field (qw( suggesteddate manageddate accepteddate )) {
-            my $from = $field . "_from";
-            my $to   = $field . "_to";
-            my $from_dt =
-              $suggestion_ref->{$from}
-              ? eval { dt_from_string( $suggestion_ref->{$from} ) }
-              : undef;
-            my $to_dt =
-              $suggestion_ref->{$to}
-              ? eval { dt_from_string( $suggestion_ref->{$to} ) }
-              : undef;
+            my $from    = delete $search_params->{"${field}_from"};
+            my $to      = delete $search_params->{"${field}_to"};
+
+            my $from_dt = $from && eval { dt_from_string($from) };
+            my $to_dt   = $to && eval { dt_from_string($to) };
 
             if ( $from_dt || $to_dt ) {
                 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
                 if ( $from_dt && $to_dt ) {
-                    $search_params->{$field} = { -between => [ $from_dt, $to_dt ] };
+                    $search_params->{$field} = { -between => [ $dtf->format_date($from_dt), $dtf->format_date($to_dt) ] };
                 } elsif ( $from_dt ) {
-                    $search_params->{$field} = { '>=' => $from_dt };
+                    $search_params->{$field} = { '>=' => $dtf->format_date($from_dt) };
                 } elsif ( $to_dt ) {
-                    $search_params->{$field} = { '<=' => $to_dt };
+                    $search_params->{$field} = { '<=' => $dtf->format_date($to_dt) };
                 }
             }
         }