Bug 28854: Update circulation functionality for bundles
[koha-ffzg.git] / circ / reserveratios.pl
index 47b0375..7311d1e 100755 (executable)
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
-use Date::Calc qw/Today Add_Delta_YM/;
 use POSIX qw( ceil );
 
 use C4::Context;
-use C4::Output;
-use C4::Auth;
-use C4::Debug;
+use C4::Output qw( output_html_with_http_headers );
+use C4::Auth qw( get_template_and_user );
 use C4::Acquisition qw/GetOrdersByBiblionumber/;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Acquisition::Baskets;
 
 my $input = CGI->new;
@@ -45,7 +43,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         query           => $input,
         type            => "intranet",
         flagsrequired   => { circulate => "circulate_remaining_permissions" },
-        debug           => 1,
     }
 );
 
@@ -85,7 +82,6 @@ if ($ratio <= 0) {
 
 my $dbh    = C4::Context->dbh;
 my $sqldatewhere = "";
-$debug and warn output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 }) . "\n" . output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
 my @query_params = ();
 
 $sqldatewhere .= " AND reservedate >= ?";
@@ -157,8 +153,8 @@ $sth->execute(@query_params);
 my @reservedata;
 while ( my $data = $sth->fetchrow_hashref ) {
     my $thisratio = $data->{reservecount} / $data->{itemcount};
-    my $ratiocalc = ceil($data->{reservecount}/$ratio - $data->{itemcount});
-    $ratiocalc >= 1 or next;  # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
+    my $copies_to_buy = ceil($data->{reservecount}/$ratio - $data->{itemcount});
+    $thisratio >= $ratio or next;  # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
     push(
         @reservedata,
         {
@@ -182,7 +178,7 @@ while ( my $data = $sth->fetchrow_hashref ) {
             itype              => [split('\|', $data->{l_itype})],
             reservecount       => $data->{reservecount},
             itemcount          => $data->{itemcount},
-            ratiocalc          => sprintf( "%.0d", $ratiocalc ),
+            copies_to_buy      => sprintf( "%d", $copies_to_buy ),
             thisratio => sprintf( "%.2f", $thisratio ),
             thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
             listcall           => [split('\|', $data->{listcall})]