Bug 17600: Standardize our EXPORT_OK
[srvgit] / serials / serials-edit.pl
index e5f69e8..76a6bf0 100755 (executable)
@@ -63,19 +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 Koha::DateUtils;
+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 qw( dt_from_string output_pref );
+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;
@@ -112,6 +113,7 @@ unless ( @serialids ) {
     $string =~ s/,$//;
 
     print $query->redirect($string);
+    exit;
 }
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
@@ -119,9 +121,7 @@ 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,
     }
 );
 
@@ -235,7 +235,8 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     $plan_date,
                     $pub_date,
                     $publisheddatetexts[$i],
-                    $notes[$i]
+                    $notes[$i],
+                    $serialdatalist[0]->{'routingnotes'}
                 );
             }
         }
@@ -264,7 +265,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,11 +287,12 @@ if ( $op and $op eq 'serialchangestatus' ) {
         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] } ) {
@@ -298,6 +306,7 @@ 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];
@@ -329,20 +338,18 @@ if ( $op and $op eq 'serialchangestatus' ) {
                     $itemhash{$item}->{'ind_tag'}
                 );
 
-                #           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' )
                     {
@@ -358,25 +365,25 @@ 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
-                        )
-                      )
-                    {
-                        $exists = GetItemnumberFromBarcode(
-                            $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});
                     }
 
                     #           push @errors,"barcode_not_unique" if($exists);
@@ -392,7 +399,11 @@ if ( $op and $op eq 'serialchangestatus' ) {
                         AddItem2Serial( $itemhash{$item}->{serial},
                             $itemnumber );
                     }
+                    $copy++;
                 }
+
+                } # if ( $item =~ /^N/ ) {
+
                 else {
 
                     #modify item