Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / cataloguing / additem.pl
index ec27c1a..2d0ba7e 100755 (executable)
@@ -107,7 +107,7 @@ sub _increment_barcode {
 
 
 sub generate_subfield_form {
-        my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $biblionumber, $temp, $loop_data, $i, $restrictededition) = @_;
+        my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $biblionumber, $temp, $loop_data, $i, $restrictededition, $item) = @_;
   
         my $frameworkcode = &GetFrameworkCode($biblionumber);
 
@@ -132,12 +132,14 @@ sub generate_subfield_form {
         if ( ! defined( $value ) || $value eq '')  {
             $value = $subfieldlib->{defaultvalue};
             if ( $value ) {
-                # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
+                # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
                 my $today_dt = dt_from_string;
                 my $year = $today_dt->strftime('%Y');
+                my $shortyear = $today_dt->strftime('%y');
                 my $month = $today_dt->strftime('%m');
                 my $day = $today_dt->strftime('%d');
                 $value =~ s/<<YYYY>>/$year/g;
+                $value =~ s/<<YY>>/$shortyear/g;
                 $value =~ s/<<MM>>/$month/g;
                 $value =~ s/<<DD>>/$day/g;
                 # And <<USER>> with surname (?)
@@ -170,7 +172,21 @@ sub generate_subfield_form {
             my @authorised_values;
             my %authorised_lib;
             # builds list, depending on authorised value...
-            if ( $subfieldlib->{authorised_value} eq "branches" ) {
+            if ( $subfieldlib->{authorised_value} eq "LOST" ) {
+                my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue');
+                my $item_is_return_claim = $ClaimReturnedLostValue && $item && $item->itemlost && $ClaimReturnedLostValue eq $item->itemlost;
+                $subfield_data{IS_RETURN_CLAIM} = $item_is_return_claim;
+
+                $subfield_data{IS_LOST_AV} = 1;
+
+                push @authorised_values, qq{};
+                my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
+                for my $r ( @$av ) {
+                    push @authorised_values, $r->{authorised_value};
+                    $authorised_lib{$r->{authorised_value}} = $r->{lib};
+                }
+            }
+            elsif ( $subfieldlib->{authorised_value} eq "branches" ) {
                 foreach my $thisbranch (@$branches) {
                     push @authorised_values, $thisbranch->{branchcode};
                     $authorised_lib{$thisbranch->{branchcode}} = $thisbranch->{branchname};
@@ -231,6 +247,7 @@ sub generate_subfield_form {
                     id          => $subfield_data{id},
                     maxlength   => $subfield_data{maxlength},
                     value       => $value,
+                    ( ( grep { $_ eq $subfieldlib->{authorised_value}} ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $subfieldlib->{authorised_value}) ),
                 };
             }
             else {
@@ -240,6 +257,7 @@ sub generate_subfield_form {
                     values   => \@authorised_values,
                     labels   => \%authorised_lib,
                     default  => $value,
+                    ( ( grep { $_ eq $subfieldlib->{authorised_value}} ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $subfieldlib->{authorised_value}) ),
                 };
             }
         }
@@ -596,7 +614,8 @@ if ($op eq "additem") {
 
                # Adding the item
         if (!$exist_itemnumber) {
-            my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
+            my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) =
+                AddItemFromMarc( $record, $biblionumber, { skip_modzebra_update => 1 } );
             set_item_default_location($oldbibitemnum);
 
             # We count the item only if it was really added
@@ -610,6 +629,9 @@ if ($op eq "additem") {
                # Preparing the next iteration
                $oldbarcode = $barcodevalue;
            }
+
+        C4::Biblio::ModZebra( $biblionumber, "specialUpdate", "biblioserver" );
+
            undef($itemrecord);
        }
     }  
@@ -671,7 +693,7 @@ if ($op eq "additem") {
     # check that there is no issue on this item before deletion.
     my $item = Koha::Items->find($itemnumber);
     $error = $item->safe_delete;
-    if($error ne '1'){
+    if(ref($error) eq 'Koha::Item'){
         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
     }else{
         push @errors,$error;
@@ -682,10 +704,11 @@ if ($op eq "additem") {
 #-------------------------------------------------------------------------------
     my $items = Koha::Items->search({ biblionumber => $biblionumber });
     while ( my $item = $items->next ) {
-        $error = $item->safe_delete;
-        next if $error eq '1'; # Means ok
+        $error = $item->safe_delete({ skip_modzebra_update => 1 });
+        next if ref $error eq 'Koha::Item'; # Deleted item is returned if deletion successful
         push @errors,$error;
     }
+    C4::Biblio::ModZebra( $biblionumber, "specialUpdate", "biblioserver" );
     if ( @errors ) {
         $nextop="additem";
     } else {
@@ -728,8 +751,9 @@ if ($op eq "additem") {
         $itemnumber = q{};
         my $olditemlost = $item->itemlost;
         my $newitemlost = $newitem->{itemlost};
-        LostItem( $item->itemnumber, 'additem' )
-            if $newitemlost && $newitemlost ge '1' && !$olditemlost;
+        if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) {
+            LostItem( $item->itemnumber, 'additem' )
+        }
     }
     $nextop="additem";
 } elsif ($op eq "delinkitem"){
@@ -867,7 +891,7 @@ for my $row ( @big_array ) {
         {
             my ( undef, $subfield ) = GetMarcFromKohaField($kohafield);
             next unless $key eq $subfield;
-            $item_field->{field} = output_pref( { str => $row->{$key}, dateonly => 1 } );
+            $item_field->{datatype} = 'date';
         }
 
         push @item_fields, $item_field;
@@ -904,6 +928,8 @@ for my $library ( @$libraries ) {
     $library->{selected} = 1 if $library->{branchcode} eq $branch
 }
 
+my $item = Koha::Items->find($itemnumber);
+
 # We generate form, from actuel record
 @fields = ();
 if($itemrecord){
@@ -917,7 +943,7 @@ if($itemrecord){
 
             next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
 
-            my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
+            my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition, $item);
             push @fields, "$tag$subfieldtag";
             push (@loop_data, $subfield_data);
             $i++;
@@ -941,7 +967,7 @@ foreach my $tag ( keys %{$tagslib}){
         my @values = (undef);
         @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
         for my $value (@values){
-            my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
+            my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition, $item);
             push (@loop_data, $subfield_data);
             $i++;
         }
@@ -949,8 +975,6 @@ foreach my $tag ( keys %{$tagslib}){
 }
 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
 
-my $item = Koha::Items->find($itemnumber); # We certainly want to fetch it earlier
-
 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
 $template->param(
     biblionumber => $biblionumber,