From d86a171021752aa1b6466ebdf6d01dc404417543 Mon Sep 17 00:00:00 2001 From: Roch D'Amour Date: Thu, 15 Mar 2018 14:45:37 -0400 Subject: [PATCH] Bug 17674: Renamed GetSerialItemsInformations to get_serial_items_count and use new OOP logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Séverine QUEUNE Signed-off-by: Michal Denar Signed-off-by: Michal Denar --- Koha/Serials.pm | 28 +++++++++++++++------------- serials/serials-collection.pl | 2 +- t/db_dependent/Serials.t | 36 ++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Koha/Serials.pm b/Koha/Serials.pm index e5ba33c..f757940 100644 --- a/Koha/Serials.pm +++ b/Koha/Serials.pm @@ -24,6 +24,7 @@ use Carp; use Koha::Database; use Koha::Serial; +use Koha::Serial::Items; use base qw(Koha::Objects); @@ -33,7 +34,7 @@ BEGIN { require Exporter; @ISA = qw(Exporter); @EXPORT = qw( - &GetSerialItemsInformations + &get_serial_items_count ); } @@ -59,29 +60,30 @@ sub object_class { return 'Koha::Serial'; } -=head2 GetSerialItemsInformations +=head2 get_serial_items_count - @serialsitemsinformations = GetSerialItemsInformations (@serialid) + @serialsitemsinformations = get_serial_items_count (@serialid) return an array of specifique information of serials and serialitem a given array of serialid =cut -sub GetSerialItemsInformations{ - my ( @serialid ) = @_; +sub get_serial_items_count{ + my ( @self ) = @_; my @serialitemsinformation; - my $dbh = C4::Context->dbh; - foreach my $sid ( @serialid ) { - my $sth = $dbh->prepare("select count(i.itemnumber) as countitems,s.itemnumber as itemnumber from items i natural join serialitems s where s.serialid=?"); - $sth->execute( $sid ); + foreach my $sid ( @self ) { + my $serialitems = Koha::Serial::Items->search({ serialid => $sid}) ; - my $line = $sth->fetchrow_hashref; - if( $line->{'countitems'} ){ - push @serialitemsinformation, $line; + my $countitem = 0; + while ( my $s = $serialitems->next() ) { + $countitem++; } + + push @serialitemsinformation,{'countitems' => $countitem, 'serialid' => $sid } if $countitem; } - return @serialitemsinformation; + + return @serialitemsinformation; } =head1 AUTHOR diff --git a/serials/serials-collection.pl b/serials/serials-collection.pl index e0c61ac..076ca8e 100755 --- a/serials/serials-collection.pl +++ b/serials/serials-collection.pl @@ -63,7 +63,7 @@ my $subscriptions; my $delete; my $countitems=0; my @ids=split("!",$serialsid); -my @serialitemsinformation=GetSerialItemsInformations(@ids); +my @serialitemsinformation=get_serial_items_count(@ids); my $subscriptionid = $subscriptionid[0]; if($op eq 'gennext' && @subscriptionid){ my $subscriptionid = $subscriptionid[0]; diff --git a/t/db_dependent/Serials.t b/t/db_dependent/Serials.t index 6093724..6954cd8 100755 --- a/t/db_dependent/Serials.t +++ b/t/db_dependent/Serials.t @@ -348,9 +348,9 @@ subtest "Do not generate an expected if one already exists" => sub { is( @serialsByStatus, 1, "ModSerialStatus delete corectly serial expected and not create another if exists" ); }; -subtest "Test GetSerialItemsInformations " => sub { +subtest "Test get_serial_items_count " => sub { plan tests => 4; - is (Koha::Serials::GetSerialItemsInformations(),0,"test GetSerialItemsInformation with nothing parameters "); + is (Koha::Serials::get_serial_items_count(),0,"test get_serial_items_count with nothing parameters "); my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype }; my ($biblionumber1, $bibitemnumber) = AddBiblio(MARC::Record->new, ''); @@ -365,31 +365,31 @@ subtest "Test GetSerialItemsInformations " => sub { my $itemnumber3 = AddItem({ barcode => '0103', %item_infos }, $biblionumber1); my $itemnumber4 = AddItem({ barcode => '0104', %item_infos }, $biblionumber1); my @serialid = ($serials[0]->{serialid},$serials[1]->{serialid}); - is (Koha::Serials::GetSerialItemsInformations(@serialid),0,"test GetSerialItemsInformation with array of serialid and none have items"); + is (Koha::Serials::get_serial_items_count(@serialid),0,"test get_serial_items_count with array of serialid and none have items"); subtest "Test with 2 serials and each have one item" => sub { plan tests => 5; AddItem2Serial($serialid[0],$itemnumber1); - my @result = Koha::Serials::GetSerialItemsInformations(@serialid); - is (scalar @result, 1 , "GetSerialItemsInformation return right length of array using 1 serial with 1 item"); - is ($result[0]->{countitems}, 1 , "GetSerialItemsInformation return right number items of serial1"); + my @result = Koha::Serials::get_serial_items_count(@serialid); + is (scalar @result, 1 , "get_serial_items_count return right length of array using 1 serial with 1 item"); + is ($result[0]->{countitems}, 1 , "get_serial_items_count return right number items of serial1"); AddItem2Serial($serialid[1],$itemnumber3); - @result = Koha::Serials::GetSerialItemsInformations(@serialid); - is (scalar @result, 2 , "GetSerialItemsInformation return right length of array using 2 serials and each have 1 item"); - is ($result[0]->{countitems}, 1 , "GetSerialItemsInformation return right number items of serial1"); - is ($result[1]->{countitems}, 1 , "GetSerialItemsInformation return right number items of serial2"); + @result = Koha::Serials::get_serial_items_count(@serialid); + is (scalar @result, 2 , "get_serial_items_count return right length of array using 2 serials and each have 1 item"); + is ($result[0]->{countitems}, 1 , "get_serial_items_count return right number items of serial1"); + is ($result[1]->{countitems}, 1 , "get_serial_items_count return right number items of serial2"); }; subtest "Test with 2 serials and each have 2 items" => sub { plan tests => 6; AddItem2Serial($serialid[0],$itemnumber2); - my @result = Koha::Serials::GetSerialItemsInformations(@serialid); - is (scalar @result, 2 , "GetSerialItemsInformation return right length of array using 2 serials "); - is ($result[0]->{countitems}, 2 , "GetSerialItemsInformation return right number items of serial1"); - is ($result[1]->{countitems}, 1 , "GetSerialItemsInformation return right number items of serial2"); + my @result = Koha::Serials::get_serial_items_count(@serialid); + is (scalar @result, 2 , "get_serial_items_count return right length of array using 2 serials "); + is ($result[0]->{countitems}, 2 , "get_serial_items_count return right number items of serial1"); + is ($result[1]->{countitems}, 1 , "get_serial_items_count return right number items of serial2"); AddItem2Serial($serialid[1],$itemnumber4); - @result = Koha::Serials::GetSerialItemsInformations(@serialid); - is (scalar @result, 2 , "GetSerialItemsInformation return right length of array using 2 serials and each have 2 items "); - is ($result[0]->{countitems}, 2 , "GetSerialItemsInformation return right number items of serial1"); - is ($result[1]->{countitems}, 2 , "GetSerialItemsInformation return right number items of serial2"); + @result = Koha::Serials::get_serial_items_count(@serialid); + is (scalar @result, 2 , "get_serial_items_count return right length of array using 2 serials and each have 2 items "); + is ($result[0]->{countitems}, 2 , "get_serial_items_count return right number items of serial1"); + is ($result[1]->{countitems}, 2 , "get_serial_items_count return right number items of serial2"); }; }; -- 2.1.4