Bug 14402: Add function purge_zero_balance_fees to C4/Accounts.pm
[koha.git] / C4 / Suggestions.pm
index cad25ef..30746b7 100644 (file)
@@ -25,13 +25,11 @@ use CGI qw ( -utf8 );
 
 use C4::Context;
 use C4::Output;
-use C4::Dates qw(format_date format_date_in_iso);
 use C4::Debug;
 use C4::Letters;
-use Koha::DateUtils qw( dt_from_string );
+use Koha::DateUtils;
 
 use List::MoreUtils qw(any);
-use C4::Dates qw(format_date_in_iso);
 use base qw(Exporter);
 
 our $VERSION = 3.07.00.049;
@@ -49,6 +47,7 @@ our @EXPORT  = qw(
   NewSuggestion
   SearchSuggestion
   DelSuggestionsOlderThan
+  GetUnprocessedSuggestions
 );
 
 =head1 NAME
@@ -101,6 +100,7 @@ sub SearchSuggestion {
             B1.branchname       AS branchnamesuggestedby,
             U1.surname          AS surnamesuggestedby,
             U1.firstname        AS firstnamesuggestedby,
+            U1.cardnumber       AS cardnumbersuggestedby,
             U1.email            AS emailsuggestedby,
             U1.borrowernumber   AS borrnumsuggestedby,
             U1.categorycode     AS categorycodesuggestedby,
@@ -174,16 +174,20 @@ sub SearchSuggestion {
     }
 
     # filter on date fields
-    my $today = C4::Dates->today('iso');
     foreach my $field (qw( suggesteddate manageddate accepteddate )) {
         my $from = $field . "_from";
         my $to   = $field . "_to";
+        my $from_dt;
+        $from_dt = eval { dt_from_string( $suggestion->{$from} ) } if ( $suggestion->{$from} );
+        my $from_sql = '0000-00-00';
+        $from_sql = output_pref({ dt => $from_dt, dateformat => 'iso', dateonly => 1 })
+            if ($from_dt);
+        $debug && warn "SQL for start date ($field): $from_sql";
         if ( $suggestion->{$from} || $suggestion->{$to} ) {
             push @query, qq{ AND suggestions.$field BETWEEN ? AND ? };
+            push @sql_params, $from_sql;
             push @sql_params,
-              format_date_in_iso( $suggestion->{$from} ) || '0000-00-00';
-            push @sql_params,
-              format_date_in_iso( $suggestion->{$to} ) || $today;
+              output_pref({ dt => dt_from_string( $suggestion->{$to} ), dateformat => 'iso', dateonly => 1 }) || output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
         }
     }
 
@@ -597,6 +601,23 @@ sub DelSuggestionsOlderThan {
     $sth->execute("-$days");
 }
 
+sub GetUnprocessedSuggestions {
+    my ( $number_of_days_since_the_last_modification ) = @_;
+
+    $number_of_days_since_the_last_modification ||= 0;
+
+    my $dbh = C4::Context->dbh;
+
+    my $s = $dbh->selectall_arrayref(q|
+        SELECT *
+        FROM suggestions
+        WHERE STATUS = 'ASKED'
+            AND budgetid IS NOT NULL
+            AND CAST(NOW() AS DATE) - INTERVAL ? DAY = CAST(suggesteddate AS DATE)
+    |, { Slice => {} }, $number_of_days_since_the_last_modification );
+    return $s;
+}
+
 1;
 __END__