Bug 27673: Fix t/Context.t
[koha-ffzg.git] / C4 / CourseReserves.pm
index 06101db..1c08ad4 100644 (file)
@@ -56,7 +56,7 @@ BEGIN {
     %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
 
     $DEBUG = 0;
-    @FIELDS = ( 'itype', 'ccode', 'holdingbranch', 'location' );
+    @FIELDS = ( 'itype', 'ccode', 'homebranch', 'holdingbranch', 'location' );
 }
 
 =head1 NAME
@@ -84,7 +84,7 @@ sub GetCourse {
     warn whoami() . "( $course_id )" if $DEBUG;
 
     my $course = Koha::Courses->find( $course_id );
-    return undef unless $course;
+    return unless $course;
     $course = $course->unblessed;
 
     my $dbh = C4::Context->dbh;
@@ -495,6 +495,7 @@ sub _AddCourseItem {
     my (%params) = @_;
     warn identify_myself(%params) if $DEBUG;
 
+    $params{homebranch} ||= undef; # Can't be empty string, FK constraint
     $params{holdingbranch} ||= undef; # Can't be empty string, FK constraint
 
     my %data = map { $_ => $params{$_} } @FIELDS;
@@ -524,6 +525,7 @@ sub _UpdateCourseItem {
     my $ci_id         = $params{'ci_id'};
     my $course_item   = $params{'course_item'};
 
+    $params{homebranch} ||= undef; # Can't be empty string, FK constraint
     $params{holdingbranch} ||= undef; # Can't be empty string, FK constraint
 
     return unless ( $ci_id || $course_item );
@@ -533,21 +535,40 @@ sub _UpdateCourseItem {
     my %data = map { $_ => $params{$_} } @FIELDS;
     my %enabled = map { $_ . "_enabled" => $params{ $_ . "_enabled" } } @FIELDS;
 
-    $course_item->update( { %data, %enabled } );
+    my $item = Koha::Items->find( $course_item->itemnumber );
+
+    # Handle updates to changed fields for a course item, both adding and removing
     if ( $course_item->is_enabled ) {
         my $item_fields = {};
-        $item_fields->{itype}         = $course_item->itype         if $course_item->itype_enabled;
-        $item_fields->{ccode}         = $course_item->ccode         if $course_item->ccode_enabled;
-        $item_fields->{location}      = $course_item->location      if $course_item->location_enabled;
-        $item_fields->{holdingbranch} = $course_item->holdingbranch if $course_item->holdingbranch_enabled;
 
-        Koha::Items->find( $course_item->itemnumber )
-                   ->set( $item_fields )
-                   ->store
-            if keys %$item_fields;
+        for my $field ( @FIELDS ) {
 
+            my $field_enabled = $field . '_enabled';
+            my $field_storage = $field . '_storage';
+
+            # Find newly enabled field and add item value to storage
+            if ( $params{$field_enabled} && !$course_item->$field_enabled ) {
+                $enabled{$field_storage} = $item->$field;
+                $item_fields->{$field}   = $params{$field};
+            }
+            # Find newly disabled field and copy the storage value to the item, unset storage value
+            elsif ( !$params{$field_enabled} && $course_item->$field_enabled ) {
+                $item_fields->{$field}   = $course_item->$field_storage;
+                $enabled{$field_storage} = undef;
+            }
+            # The field was already enabled, copy the incoming value to the item.
+            # The "original" ( when not on course reserve ) value is already in the storage field
+            elsif ( $course_item->$field_enabled) {
+                $item_fields->{$field} = $params{$field};
+            }
+        }
+
+        $item->set( $item_fields )->store
+            if keys %$item_fields;
     }
 
+    $course_item->update( { %data, %enabled } );
+
 }
 
 =head2 _RevertFields
@@ -572,6 +593,7 @@ sub _RevertFields {
     $item_fields->{itype}         = $course_item->itype_storage         if $course_item->itype_enabled;
     $item_fields->{ccode}         = $course_item->ccode_storage         if $course_item->ccode_enabled;
     $item_fields->{location}      = $course_item->location_storage      if $course_item->location_enabled;
+    $item_fields->{homebranch} = $course_item->homebranch_storage if $course_item->homebranch_enabled;
     $item_fields->{holdingbranch} = $course_item->holdingbranch_storage if $course_item->holdingbranch_enabled;
 
     Koha::Items->find( $course_item->itemnumber )
@@ -582,6 +604,7 @@ sub _RevertFields {
     $course_item->itype_storage(undef);
     $course_item->ccode_storage(undef);
     $course_item->location_storage(undef);
+    $course_item->homebranch_storage(undef);
     $course_item->holdingbranch_storage(undef);
     $course_item->store();
 }
@@ -603,6 +626,7 @@ sub _SwapAllFields {
         $course_item->itype_storage( $item->effective_itemtype )    if $course_item->itype_enabled;
         $course_item->ccode_storage( $item->ccode )                 if $course_item->ccode_enabled;
         $course_item->location_storage( $item->location )           if $course_item->location_enabled;
+        $course_item->homebranch_storage( $item->homebranch )       if $course_item->homebranch_enabled;
         $course_item->holdingbranch_storage( $item->holdingbranch ) if $course_item->holdingbranch_enabled;
         $course_item->store();
 
@@ -610,6 +634,7 @@ sub _SwapAllFields {
         $item_fields->{itype}         = $course_item->itype         if $course_item->itype_enabled;
         $item_fields->{ccode}         = $course_item->ccode         if $course_item->ccode_enabled;
         $item_fields->{location}      = $course_item->location      if $course_item->location_enabled;
+        $item_fields->{homebranch}    = $course_item->homebranch    if $course_item->homebranch_enabled;
         $item_fields->{holdingbranch} = $course_item->holdingbranch if $course_item->holdingbranch_enabled;
 
         Koha::Items->find( $course_item->itemnumber )
@@ -622,6 +647,7 @@ sub _SwapAllFields {
         $item_fields->{itype}         = $course_item->itype_storage         if $course_item->itype_enabled;
         $item_fields->{ccode}         = $course_item->ccode_storage         if $course_item->ccode_enabled;
         $item_fields->{location}      = $course_item->location_storage      if $course_item->location_enabled;
+        $item_fields->{homebranch}    = $course_item->homebranch_storage    if $course_item->homebranch_enabled;
         $item_fields->{holdingbranch} = $course_item->holdingbranch_storage if $course_item->holdingbranch_enabled;
 
         Koha::Items->find( $course_item->itemnumber )
@@ -632,6 +658,7 @@ sub _SwapAllFields {
         $course_item->itype_storage(undef);
         $course_item->ccode_storage(undef);
         $course_item->location_storage(undef);
+        $course_item->homebranch_storage(undef);
         $course_item->holdingbranch_storage(undef);
         $course_item->store();
     }
@@ -693,7 +720,10 @@ sub DelCourseItem {
 
     return unless ($ci_id);
 
-    _RevertFields( ci_id => $ci_id );
+    my $course_item = Koha::Course::Items->find( $ci_id );
+    return unless $course_item;
+
+    _RevertFields( ci_id => $ci_id ) if $course_item->enabled eq 'yes';
 
     my $query = "
         DELETE FROM course_items