Bug 8438: Users can only claim for serials related to their branch.
[koha.git] / serials / serials-edit.pl
index fbf9ad5..8df489d 100755 (executable)
@@ -5,18 +5,18 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME
 
@@ -63,7 +63,8 @@ op can be :
 
 use strict;
 use warnings;
-use CGI;
+use CGI qw ( -utf8 );
+use Encode qw( decode is_utf8 );
 use C4::Auth;
 use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Biblio;
@@ -72,6 +73,7 @@ use C4::Koha;
 use C4::Output;
 use C4::Context;
 use C4::Serials;
+use C4::Search qw/enabled_staff_search_views/;
 use List::MoreUtils qw/uniq/;
 
 my $query           = CGI->new();
@@ -93,9 +95,10 @@ my @errseq;
 # If user comes from subscription details
 unless (@serialids) {
     my $serstatus = $query->param('serstatus');
+    my @statuses = split ',', $serstatus;
     if ($serstatus) {
         foreach my $subscriptionid (@subscriptionids) {
-            my @tmpser = GetSerials2( $subscriptionid, $serstatus );
+            my @tmpser = GetSerials2( $subscriptionid, \@statuses );
             push @serialids, map { $_->{serialid} } @tmpser;
         }
     }
@@ -111,11 +114,11 @@ unless ( @serialids ) {
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
-        template_name   => 'serials/serials-edit.tmpl',
+        template_name   => 'serials/serials-edit.tt',
         query           => $query,
         type            => 'intranet',
         authnotrequired => 0,
-        flagsrequired   => { serials => 1 },
+        flagsrequired   => { serials => 'receive_serials' },
         debug           => 1,
     }
 );
@@ -133,6 +136,7 @@ foreach my $serialid (@serialids) {
         && !$processedserialid{$serialid} )
     {
         my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
+
         for my $d ( qw( publisheddate planneddate )){
             if ( $serinfo->{$d} =~m/^00/ ) {
                 $serinfo->{$d} = q{};
@@ -150,6 +154,8 @@ foreach my $serialid (@serialids) {
             )
             || $serinfo->{'cannotedit'}
         );
+        $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
+        $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
         push @serialdatalist, $serinfo;
         $processedserialid{$serialid} = 1;
     }
@@ -176,6 +182,7 @@ foreach my $subscriptionid (@subscriptionids) {
             $cell->{serialsadditems} = 1;
         }
         $cell->{'subscriptionid'} = $subscriptionid;
+        $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
         $cell->{'itemid'}         = 'NNEW';
         $cell->{'serialid'}       = 'NEW';
         $cell->{'issuesatonce'}   = 1;
@@ -196,6 +203,11 @@ $template->param( subscriptions => \@subscriptionloop );
 
 if ( $op and $op eq 'serialchangestatus' ) {
 
+    # Convert serialseqs to UTF-8 to prevent encoding problems
+    foreach my $seq (@serialseqs) {
+        $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
+    }
+
     my $newserial;
     for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
         my ($plan_date, $pub_date);
@@ -310,8 +322,12 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     if ( C4::Context->preference('autoBarcode') eq
                         'incremental' )
                     {
-                        if ( !$bib_record->field($barcodetagfield)
-                            ->subfield($barcodetagsubfield) )
+                        if (
+                            !(
+                                   $bib_record->field($barcodetagfield)
+                                && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
+                            )
+                          )
                         {
                             my $sth_barcode = $dbh->prepare(
                                 'select max(abs(barcode)) from items');
@@ -387,24 +403,28 @@ my $default_bib_view = get_default_view();
 $template->param(
     serialsadditems => $serialdatalist[0]->{'serialsadditems'},
     callnumber      => $serialdatalist[0]->{'callnumber'},
+    internalnotes   => $serialdatalist[0]->{'internalnotes'},
     bibliotitle     => $biblio->{'title'},
     biblionumber    => $serialdatalist[0]->{'biblionumber'},
     serialslist     => \@serialdatalist,
     default_bib_view => $default_bib_view,
     location         => $locationlib,
+    (uc(C4::Context->preference("marcflavour"))) => 1
+
 );
 output_html_with_http_headers $query, $cookie, $template->output;
 
 sub get_default_view {
     my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
-    my $views = { C4::Search::enabled_staff_search_views };
-    if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
+    my %views       = C4::Search::enabled_staff_search_views();
+    if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
         return 'ISBDdetail';
-    } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
+    }
+    elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
         return 'MARCdetail';
-    } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
+    }
+    elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
         return 'labeledMARCdetail';
-    } else {
-        return 'detail';
     }
+    return 'detail';
 }