From f97a046a0ceadb4dcbff5cccce1be246047f0575 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 16 Jul 2015 07:58:26 -0400 Subject: [PATCH] Bug 8483 - Borrower reading history should include deleted items When showing a borrower's reading history, it'd be good to be able to include the deleted items also, as we have that information available. Test Plan: 1) Apply this patch 2) prove t/db_dependent/Members/GetAllIssues.t 3) Verify old checkouts for deleted items show in reading history --- C4/Members.pm | 54 +++++++++++++++++++------------ Koha/Borrower.pm | 55 ++++++++++++++++++++++++++++++++ Koha/Object.pm | 12 +++++++ Koha/Objects.pm | 14 ++++++++ installer/data/mysql/kohastructure.sql | 2 +- 5 files changed, 115 insertions(+), 22 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 95fb4e7..081a1d3 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -42,6 +42,8 @@ use Text::Unaccent qw( unac_string ); use Koha::AuthUtils qw(hash_password); use Koha::Database; use Module::Load; +use Koha::Borrowers; + if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) { load Koha::NorwegianPatronDB, qw( NLUpdateHashedPIN NLEncryptPIN NLSync ); } @@ -1116,31 +1118,41 @@ sub GetAllIssues { my ( $borrowernumber, $order, $limit ) = @_; return unless $borrowernumber; + $order = 'date_due desc' unless $order; + my ( $column, $sort ) = split( / /, $order ); + $column ||= 'date_due'; + $sort ||= 'desc'; - my $dbh = C4::Context->dbh; - my $query = -'SELECT *, issues.timestamp as issuestimestamp, issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp - FROM issues - LEFT JOIN items on items.itemnumber=issues.itemnumber - LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber - LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber - WHERE borrowernumber=? - UNION ALL - SELECT *, old_issues.timestamp as issuestimestamp, old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp - FROM old_issues - LEFT JOIN items on items.itemnumber=old_issues.itemnumber - LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber - LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber - WHERE borrowernumber=? AND old_issues.itemnumber IS NOT NULL - order by ' . $order; - if ($limit) { - $query .= " limit $limit"; + my $borrower = Koha::Borrowers->find( $borrowernumber ); + my $checkouts = $borrower->all_checkouts( { order => { column => $column, sort => $sort } , limit => $limit } ); + + my @results; + foreach my $checkout (@$checkouts) { + my $item = $checkout->item( { deleted => 1 } ); + next unless $item; + + my $biblio = $item->biblio( { deleted => 1 } ); + next unless $biblio; + + my $biblioitem = $item->biblioitem( { deleted => 1 } ); + next unless $biblioitem; + + my $checkout_hashref = $checkout->unblessed(); + my $item_hashref = $item->unblessed(); + my $biblio_hashref = $biblio->unblessed(); + my $biblioitem_hashref = $biblioitem->unblessed(); + + my $hashref = { %$checkout_hashref, %$item_hashref, %$biblio_hashref, %$biblioitem_hashref }; + $hashref->{issuestimestamp} = $checkout->timestamp(); + $hashref->{renewals} = $checkout->renewals(); + $hashref->{totalrenewals} = $item->renewals(); + $hashref->{itemstimestamp} = $item->timestamp(); + + push( @results, $hashref ); } - my $sth = $dbh->prepare($query); - $sth->execute( $borrowernumber, $borrowernumber ); - return $sth->fetchall_arrayref( {} ); + return \@results; } diff --git a/Koha/Borrower.pm b/Koha/Borrower.pm index 93426bf..a210dc1 100644 --- a/Koha/Borrower.pm +++ b/Koha/Borrower.pm @@ -23,6 +23,9 @@ use Carp; use Koha::Database; +use Koha::Checkouts; +use Koha::Old::Checkouts; + use base qw(Koha::Object); =head1 NAME @@ -35,6 +38,58 @@ Koha::Borrower - Koha Borrower Object class =cut +=head3 all_checkouts + +=cut + +sub all_checkouts { + my ( $self, $params ) = @_; + + my $limit = $params->{limit}; + + my $order_by_column = $params->{order} ? $params->{order}->{column} : 'date_due'; + my $order_by_sort = $params->{order} ? $params->{order}->{sort} : 'desc'; + + my @results; + my $results_found = 0; + + my $issues = Koha::Checkouts->search( + { + borrowernumber => $self->borrowernumber(), + }, + { + order_by => { "-$order_by_sort" => $order_by_column } + } + ); + + while ( my $i = $issues->next() ) { + push( @results, $i ); + $results_found++; + last if ( $limit && $results_found >= $limit ); + } + + if ( !$limit || $results_found < $limit ) { + + $issues = Koha::Old::Checkouts->search( + { + borrowernumber => $self->borrowernumber(), + }, + { + order_by => { "-$order_by_sort" => $order_by_column } + } + ); + + while ( my $i = $issues->next() ) { + push( @results, $i ); + $results_found++; + last if ( $limit && $results_found >= $limit ); + } + + } + + return wantarray ? @results : \@results; +} + =head3 type =cut diff --git a/Koha/Object.pm b/Koha/Object.pm index a77eb5c..504872c 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -266,6 +266,18 @@ sub AUTOLOAD { return; } +=head3 $object->unblessed(); + +Returns an unblessed representation of object. + +=cut + +sub unblessed { + my ($self) = @_; + + return { $self->_result->get_columns }; +} + =head3 type This method must be defined in the child class. The value is the name of the DBIC resultset. diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 8ef6390..f6add21 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -195,6 +195,20 @@ sub _wrap { return @objects; } +=head3 Koha::Objects->unblessed + +Returns an unblessed representation of objects. + +=cut + +sub unblessed { + my ($self) = @_; + + return [ map { $_->unblessed } $self->as_list ]; +} + + + =head3 Koha::Objects->_resultset Returns the internal resultset or creates it if undefined diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index e919349..9c8cc5d 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1162,7 +1162,7 @@ CREATE TABLE `issues` ( -- information related to check outs or issues KEY `branchcode_idx` (`branchcode`), KEY `bordate` (`borrowernumber`,`timestamp`), CONSTRAINT `issues_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE RESTRICT ON UPDATE CASCADE, - CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE RESTRICT ON UPDATE CASCADE + CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -- 1.7.2.5