Bug 32799: Rename ILLSTATUS authorised value category to ILL_STATUS_ALIAS
[koha-ffzg.git] / circ / overdue.pl
index 0e558dc..e2bb848 100755 (executable)
 
 use Modern::Perl;
 use C4::Context;
-use C4::Output;
+use C4::Output qw( output_html_with_http_headers );
 use CGI qw(-oldstyle_urls -utf8);
-use C4::Auth;
-use C4::Debug;
+use C4::Auth qw( get_template_and_user );
 use Text::CSV_XS;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Patron::Attribute::Types;
 use DateTime;
 use DateTime::Format::MySQL;
 
-my $input = new CGI;
+my $input = CGI->new;
 my $showall         = $input->param('showall');
 my $bornamefilter   = $input->param('borname') || '';
 my $borcatfilter    = $input->param('borcat') || '';
@@ -61,6 +61,7 @@ my $filters = {
     holdingbranch => $holdingbranchfilter,
     dateduefrom   => $dateduefrom,
     datedueto     => $datedueto,
+    showall       => $showall,
 };
 
 my $isfiltered      = $op =~ /apply/i && $op =~ /filter/i;
@@ -71,13 +72,11 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         template_name   => "circ/overdue.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { circulate => "overdues_report" },
-        debug           => 1,
     }
 );
 
-our $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
+our $logged_in_user = Koha::Patrons->find( $loggedinuser );
 
 my $dbh = C4::Context->dbh;
 
@@ -114,20 +113,28 @@ for my $attrcode (grep { /^patron_attr_filter_/ } $input->multi_param) {
     if (my @attrvalues = grep { length($_) > 0 } $input->multi_param($attrcode)) {
         $attrcode =~ s/^patron_attr_filter_//;
         $cgi_attrcode_to_attrvalues{$attrcode} = \@attrvalues;
-        print STDERR ">>>param($attrcode)[@{[scalar @attrvalues]}] = '@attrvalues'\n" if $debug;
     }
 }
 my $have_pattr_filter_data = keys(%cgi_attrcode_to_attrvalues) > 0;
 
 my @patron_attr_filter_loop;   # array of [ domid cgivalue ismany isclone ordinal code description repeatable authorised_value_category ]
 
-my $sth = $dbh->prepare('SELECT code,description,repeatable,authorised_value_category
-    FROM borrower_attribute_types
-    WHERE staff_searchable <> 0
-    ORDER BY description');
-$sth->execute();
+my $patron_attrs = Koha::Patron::Attribute::Types->search_with_library_limits(
+    {
+        staff_searchable => 1,
+    },
+    {},
+    C4::Context->userenv->{'branch'}
+);
+
 my $ordinal = 0;
-while (my $row = $sth->fetchrow_hashref) {
+while (my $attr = $patron_attrs->next ) {
+    my $row = {
+        code => $attr->code,
+        description => $attr->description,
+        repeatable => $attr->repeatable,
+        authorised_value_category => $attr->authorised_value_category,
+    };
     $row->{ordinal} = $ordinal;
     my $code = $row->{code};
     my $cgivalues = $cgi_attrcode_to_attrvalues{$code} || [ '' ];
@@ -150,17 +157,19 @@ if (@patron_attr_filter_loop) {
     #              too resource intensive, MySQL can be used to do the filtering, i.e. rewire the
     #              SQL below to select only those attribute values that match the filters.
 
-    my $sql = q(SELECT borrowernumber AS bn, b.code, attribute AS val, category AS avcategory, lib AS avdescription
+    my $sql = q{
+        SELECT b.borrowernumber AS bn, b.code AS attrcode, b.attribute AS attrval, a.lib AS avdescription
         FROM borrower_attributes b
         JOIN borrower_attribute_types bt ON (b.code = bt.code)
-        LEFT JOIN authorised_values a ON (a.category = bt.authorised_value_category AND a.authorised_value = b.attribute));
+        LEFT JOIN authorised_values a ON (a.category = bt.authorised_value_category AND a.authorised_value = b.attribute)
+    };
     my $sth = $dbh->prepare($sql);
     $sth->execute();
     while (my $row = $sth->fetchrow_hashref) {
         my $pattrs = $borrowernumber_to_attributes{$row->{bn}} ||= { };
-        push @{ $pattrs->{$row->{code}} }, [
-            $row->{val},
-            defined $row->{avdescription} ? $row->{avdescription} : $row->{val},
+        push @{ $pattrs->{$row->{attrcode}} }, [
+            $row->{attrval},
+            defined $row->{avdescription} ? $row->{avdescription} : $row->{attrval},
         ];
     }
 
@@ -171,8 +180,7 @@ if (@patron_attr_filter_loop) {
             # discard patrons that do not match (case insensitive) at least one of each attribute filter value
             my $discard = 1;
             for my $attrval (map { lc $_ } @{ $cgi_attrcode_to_attrvalues{$code} }) {
-                ## if (grep { $attrval eq lc($_->[0]) } @{ $pattrs->{$code} })
-                if (grep { $attrval eq lc($_->[1]) } @{ $pattrs->{$code} }) {
+                if (grep { $attrval eq lc($_->[0]) } @{ $pattrs->{$code} }) {
                     $discard = 0;
                     last;
                 }
@@ -182,12 +190,6 @@ if (@patron_attr_filter_loop) {
                 last;
             }
         }
-        if ($debug) {
-            my $showkeep = $keep ? 'keep' : 'do NOT keep';
-            print STDERR ">>> patron $bn: $showkeep attributes: ";
-            for (sort keys %$pattrs) { my @a=map { "$_->[0]/$_->[1]  " } @{$pattrs->{$_}}; print STDERR "attrcode $_ = [@a] " }
-            print STDERR "\n";
-        }
         delete $borrowernumber_to_attributes{$bn} if !$keep;
     }
 }
@@ -214,7 +216,7 @@ if ($noreport) {
     #  FIX 2: ensure there are indexes for columns participating in the WHERE clauses, where feasible/reasonable
 
 
-    my $today_dt = DateTime->now(time_zone => C4::Context->tz);
+    my $today_dt = dt_from_string();
     $today_dt->truncate(to => 'minute');
     my $todaysdate = $today_dt->strftime('%Y-%m-%d %H:%M');
 
@@ -242,22 +244,29 @@ if ($noreport) {
         items.barcode,
         items.homebranch,
         items.holdingbranch,
+        items.location,
         biblio.title,
+        biblio.subtitle,
+        biblio.part_number,
+        biblio.part_name,
         biblio.author,
         biblio.biblionumber,
         items.itemcallnumber,
         items.replacementprice,
         items.enumchron,
         items.itemnotes_nonpublic,
-        items.itype
+        items.itype,
+        return_claims.created_on AS return_claim_created_on,
+        return_claims.id AS return_claim_id
       FROM issues
     LEFT JOIN borrowers   ON (issues.borrowernumber=borrowers.borrowernumber )
     LEFT JOIN items       ON (issues.itemnumber=items.itemnumber)
     LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
     LEFT JOIN biblio      ON (biblio.biblionumber=items.biblionumber )
+    LEFT JOIN return_claims ON (return_claims.borrowernumber=borrowers.borrowernumber AND return_claims.itemnumber=items.itemnumber)
     WHERE 1=1 "; # placeholder, since it is possible that none of the additional
                  # conditions will be selected by user
-    $strsth.=" AND date_due               < '" . $todaysdate     . "' " unless ($showall);
+    $strsth.=" AND date_due               < '" . $todaysdate     . "' " unless ($showall or $datedueto);
     $strsth.=" AND (borrowers.firstname like '".$bornamefilter."%' or borrowers.surname like '".$bornamefilter."%' or borrowers.cardnumber like '".$bornamefilter."%')" if($bornamefilter) ;
     $strsth.=" AND borrowers.categorycode = '" . $borcatfilter   . "' " if $borcatfilter;
     if( $itemtypefilter ){
@@ -309,7 +318,7 @@ if ($noreport) {
         }
 
         push @overduedata, {
-            patron                 => scalar Koha::Patrons->find( $data->{borrowernumber} ),
+            patron                 => Koha::Patrons->find( $data->{borrowernumber} ),
             duedate                => $data->{date_due},
             borrowernumber         => $data->{borrowernumber},
             cardnumber             => $data->{cardnumber},
@@ -328,15 +337,21 @@ if ($noreport) {
             branchcode             => $data->{branchcode},
             barcode                => $data->{barcode},
             itemnum                => $data->{itemnumber},
-            issuedate              => output_pref({ dt => dt_from_string( $data->{issuedate} ), dateonly => 1 }),
+            issuedate              => $data->{issuedate},
             biblionumber           => $data->{biblionumber},
             title                  => $data->{title},
+            subtitle               => $data->{subtitle},
+            part_number            => $data->{part_number},
+            part_name              => $data->{part_name},
             author                 => $data->{author},
             homebranchcode         => $data->{homebranch},
             holdingbranchcode      => $data->{holdingbranch},
+            location               => $data->{location},
             itemcallnumber         => $data->{itemcallnumber},
             replacementprice       => $data->{replacementprice},
             itemnotes_nonpublic    => $data->{itemnotes_nonpublic},
+            return_claim_created_on => $data->{return_claim_created_on},
+            return_claim_id        => $data->{return_claim_id},
             enumchron              => $data->{enumchron},
             itemtype               => $data->{itype},
             patron_attr_value_loop => \@patron_attr_value_loop,
@@ -359,7 +374,7 @@ if ($noreport) {
     $new_cgi->delete('op');
 
     $template->param(
-        todaysdate              => output_pref($today_dt),
+        todaysdate              => $today_dt,
         overdueloop             => \@overduedata,
         nnoverdue               => scalar(@overduedata),
         noverdue_is_plural      => scalar(@overduedata) != 1,