Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / cataloguing / additem.pl
index 0fa8437..2d0ba7e 100755 (executable)
@@ -76,19 +76,14 @@ sub set_item_default_location {
     my $itemnumber = shift;
     my $item       = Koha::Items->find($itemnumber);
     if ( C4::Context->preference('NewItemsDefaultLocation') ) {
-        ModItem(
-            {
-                permanent_location => $item->location,
-                location => C4::Context->preference('NewItemsDefaultLocation')
-            },
-            undef,
-            $itemnumber
-        );
+        $item->permanent_location($item->location);
+        $item->location(C4::Context->preference('NewItemsDefaultLocation'));
     }
     else {
-        ModItem( { permanent_location => $item->location }, undef, $itemnumber )
-          unless defined $item->permanent_location;
+        # It seems that we are dealing with that in too many places
+        $item->permanent_location($item->location) unless defined $item->permanent_location;
     }
+    $item->store;
 }
 
 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
@@ -112,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);
 
@@ -130,18 +125,21 @@ sub generate_subfield_form {
         $subfield_data{subfield}   = $subfieldtag;
         $subfield_data{marc_lib}   ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
         $subfield_data{mandatory}  = $subfieldlib->{mandatory};
+        $subfield_data{important}  = $subfieldlib->{important};
         $subfield_data{repeatable} = $subfieldlib->{repeatable};
         $subfield_data{maxlength}  = $subfieldlib->{maxlength};
         
         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 (?)
@@ -174,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};
@@ -235,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 {
@@ -244,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}) ),
                 };
             }
         }
@@ -600,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
@@ -614,6 +629,9 @@ if ($op eq "additem") {
                # Preparing the next iteration
                $oldbarcode = $barcodevalue;
            }
+
+        C4::Biblio::ModZebra( $biblionumber, "specialUpdate", "biblioserver" );
+
            undef($itemrecord);
        }
     }  
@@ -673,8 +691,9 @@ if ($op eq "additem") {
 } elsif ($op eq "delitem") {
 #-------------------------------------------------------------------------------
     # check that there is no issue on this item before deletion.
-    $error = &DelItemCheck( $biblionumber,$itemnumber);
-    if($error == 1){
+    my $item = Koha::Items->find($itemnumber);
+    $error = $item->safe_delete;
+    if(ref($error) eq 'Koha::Item'){
         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
     }else{
         push @errors,$error;
@@ -683,12 +702,13 @@ if ($op eq "additem") {
 #-------------------------------------------------------------------------------
 } elsif ($op eq "delallitems") {
 #-------------------------------------------------------------------------------
-    my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber })->get_column('itemnumber');
-    foreach my $itemnumber ( @itemnumbers ) {
-        $error = C4::Items::DelItemCheck( $biblionumber, $itemnumber );
-        next if $error == 1; # Means ok
+    my $items = Koha::Items->search({ biblionumber => $biblionumber });
+    while ( my $item = $items->next ) {
+        $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 {
@@ -731,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"){
@@ -799,7 +820,6 @@ if ( C4::Context->preference('EasyAnalyticalRecords') ) {
     }
 }
 
-
 foreach my $field (@fields) {
     next if ( $field->tag() < 10 );
 
@@ -856,7 +876,26 @@ my @item_value_loop;
 my @header_value_loop;
 for my $row ( @big_array ) {
     my %row_data;
-    my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
+    my @item_fields;
+    foreach my $key (sort keys %witness){
+        my $item_field;
+        if ( $row->{$key} ){
+            $item_field->{field} = $row->{$key};
+        } else {
+            $item_field->{field} = '';
+        }
+
+        for my $kohafield (
+            qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate )
+          )
+        {
+            my ( undef, $subfield ) = GetMarcFromKohaField($kohafield);
+            next unless $key eq $subfield;
+            $item_field->{datatype} = 'date';
+        }
+
+        push @item_fields, $item_field;
+    }
     $row_data{item_value} = [ @item_fields ];
     $row_data{itemnumber} = $row->{itemnumber};
     #reporting this_row values
@@ -889,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){
@@ -902,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++;
@@ -926,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++;
         }
@@ -934,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,