Bug 32594: (QA follow-up) Fix POD
[koha-ffzg.git] / serials / serials-edit.pl
index 928ff47..b80fa46 100755 (executable)
@@ -63,21 +63,20 @@ op can be :
 
 use Modern::Perl;
 use CGI qw ( -utf8 );
-use Encode qw( decode is_utf8 );
-use C4::Auth;
-use C4::Biblio;
-use C4::Items;
-use C4::Koha;
-use C4::Output;
+use Encode;
+use C4::Auth qw( get_template_and_user haspermission );
+use C4::Biblio qw( GetMarcFromKohaField TransformHtmlToXml );
+use C4::Items qw( AddItemFromMarc ModItemFromMarc PrepareItemrecordDisplay );
+use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
-use C4::Serials;
-use C4::Search qw/enabled_staff_search_views/;
+use C4::Serials qw( GetSerials GetSerials2 GetSerialInformation HasSubscriptionExpired GetSubscription abouttoexpire NewIssue ModSerialStatus GetPreviousSerialid AddItem2Serial );
+use C4::Search qw( enabled_staff_search_views );
 
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string );
 use Koha::Items;
 use Koha::Serial::Items;
 
-use List::MoreUtils qw/uniq/;
+use List::MoreUtils qw( uniq );
 
 my $query           = CGI->new();
 my $dbh             = C4::Context->dbh;
@@ -114,6 +113,7 @@ unless ( @serialids ) {
     $string =~ s/,$//;
 
     print $query->redirect($string);
+    exit;
 }
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
@@ -121,16 +121,14 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         template_name   => 'serials/serials-edit.tt',
         query           => $query,
         type            => 'intranet',
-        authnotrequired => 0,
         flagsrequired   => { serials => 'receive_serials' },
-        debug           => 1,
     }
 );
 
 my @serialdatalist;
 my %processedserialid;
 
-my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
+my $today = dt_from_string;
 
 foreach my $serialid (@serialids) {
 
@@ -142,14 +140,6 @@ foreach my $serialid (@serialids) {
     {
         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{};
-            }
-            else {
-                $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
-            }
-        }
         $serinfo->{arriveddate} = $today;
 
         $serinfo->{'editdisable'} = (
@@ -218,10 +208,10 @@ if ( $op and $op eq 'serialchangestatus' ) {
         my ($plan_date, $pub_date);
 
         if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
-            $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
+            $plan_date = $planneddates[$i];
         }
         if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
-            $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
+            $pub_date = $publisheddates[$i];
         }
 
         if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
@@ -237,7 +227,8 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     $plan_date,
                     $pub_date,
                     $publisheddatetexts[$i],
-                    $notes[$i]
+                    $notes[$i],
+                    $serialdatalist[0]->{'routingnotes'}
                 );
             }
         }
@@ -266,7 +257,13 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     my $subscriptioninfos = GetSubscription($subscriptionids[$i]);
 
                     # Changing the status to "available" and the itemtype according to the previousitemtype db field
-                    ModItem({notforloan => 0, itype => $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
+                    my $item = Koha::Items->find($itemnumber);
+                    $item->set(
+                        {
+                            notforloan => 0,
+                            itype => $subscriptioninfos->{'previousitemtype'}
+                        }
+                    )->store;
                 }
             }
         }
@@ -280,13 +277,12 @@ if ( $op and $op eq 'serialchangestatus' ) {
         my @serials      = $query->multi_param('serial');
         my @bibnums      = $query->multi_param('bibnum');
         my @itemid       = $query->multi_param('itemid');
-        my @ind_tag      = $query->multi_param('ind_tag');
-        my @indicator    = $query->multi_param('indicator');
+        my @num_copies   = $query->multi_param('number_of_copies');
 
         #Rebuilding ALL the data for items into a hash
         # parting them on $itemid.
         my %itemhash;
-        my $countdistinct;
+        my $countdistinct = 0;
         my $range = scalar(@itemid);
         for ( my $i = 0 ; $i < $range ; $i++ ) {
             unless ( $itemhash{ $itemid[$i] } ) {
@@ -300,14 +296,13 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
                 }
                 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
+                $itemhash{ $itemid[$i] }->{'num_copies'} = $num_copies[$countdistinct];
                 $countdistinct++;
             }
             push @{ $itemhash{ $itemid[$i] }->{'tags'} },      $tags[$i];
             push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
             push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
               $field_values[$i];
-            push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} },   $ind_tag[$i];
-            push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
         }
         foreach my $item ( keys %itemhash ) {
 
@@ -327,24 +322,22 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     $itemhash{$item}->{'tags'},
                     $itemhash{$item}->{'subfields'},
                     $itemhash{$item}->{'field_values'},
-                    $itemhash{$item}->{'indicator'},
-                    $itemhash{$item}->{'ind_tag'}
+                    undef,
+                    undef
                 );
 
-                #           warn $xml;
+                # warn $xml;
                 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
                 if ( $item =~ /^N/ ) {
 
-                    #New Item
+                $itemhash{$item}->{'num_copies'} //= 1;
+
+                for (my $copy = 0; $copy < $itemhash{$item}->{'num_copies'};){
+
+                # New Item
 
                   # if autoBarcode is set to 'incremental', calculate barcode...
-                    my ( $barcodetagfield, $barcodetagsubfield ) =
-                      GetMarcFromKohaField(
-                        'items.barcode',
-                        GetFrameworkCode(
-                            $serialdatalist[0]->{'biblionumber'}
-                        )
-                      );
+                    my ( $barcodetagfield, $barcodetagsubfield ) = GetMarcFromKohaField( 'items.barcode' );
                     if ( C4::Context->preference('autoBarcode') eq
                         'incremental' )
                     {
@@ -360,21 +353,24 @@ if ( $op and $op eq 'serialchangestatus' ) {
                             $sth_barcode->execute;
                             my ($newbarcode) = $sth_barcode->fetchrow;
 
-# OK, we have the new barcode, add the entry in MARC record # FIXME -> should be  using barcode plugin here.
-                            $bib_record->field($barcodetagfield)
-                              ->update( $barcodetagsubfield => ++$newbarcode );
+                            # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be  using barcode plugin here.
+                            $bib_record->field($barcodetagfield)->update( $barcodetagsubfield => ++$newbarcode );
                         }
                     }
 
                     # check for item barcode # being unique
                     my $exists;
-                    if (
-                        $bib_record->subfield(
-                            $barcodetagfield, $barcodetagsubfield
-                        )
-                      )
-                    {
+                    if ( $bib_record->subfield( $barcodetagfield, $barcodetagsubfield ) ) {
                         my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
+
+                        if ($copy > 0){
+                            use C4::Barcodes;
+                            my $barcodeobj = C4::Barcodes->new;
+                            my $newbarcode = $barcodeobj->next_value($barcode);
+                            $barcode = $newbarcode;
+                            $bib_record->field($barcodetagfield)->update($barcodetagsubfield => $barcode);
+                        }
+
                         $exists = Koha::Items->find({barcode => $barcode});
                     }
 
@@ -391,7 +387,11 @@ if ( $op and $op eq 'serialchangestatus' ) {
                         AddItem2Serial( $itemhash{$item}->{serial},
                             $itemnumber );
                     }
+                    $copy++;
                 }
+
+                } # if ( $item =~ /^N/ ) {
+
                 else {
 
                     #modify item
@@ -417,7 +417,6 @@ if ( $op and $op eq 'serialchangestatus' ) {
     }
 }
 my $location = $serialdatalist[0]->{'location'};
-my $default_bib_view = get_default_view();
 
 $template->param(
     subscriptionid  => $serialdatalist[0]->{subscriptionid},
@@ -427,24 +426,8 @@ $template->param(
     bibliotitle     => $biblio->title,
     biblionumber    => $serialdatalist[0]->{'biblionumber'},
     serialslist     => \@serialdatalist,
-    default_bib_view => $default_bib_view,
     location         => $location,
     (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} ) {
-        return 'ISBDdetail';
-    }
-    elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
-        return 'MARCdetail';
-    }
-    elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
-        return 'labeledMARCdetail';
-    }
-    return 'detail';
-}