Bug 25946: borrowerRelationship can be empty
[koha-ffzg.git] / C4 / CourseReserves.pm
index 3b9bbd4..1c08ad4 100644 (file)
@@ -22,6 +22,11 @@ use List::MoreUtils qw(any);
 use C4::Context;
 use C4::Circulation qw(GetOpenIssue);
 
+use Koha::Courses;
+use Koha::Course::Instructors;
+use Koha::Course::Items;
+use Koha::Course::Reserves;
+
 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG @FIELDS);
 
 BEGIN {
@@ -51,7 +56,7 @@ BEGIN {
     %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
 
     $DEBUG = 0;
-    @FIELDS = ( 'itype', 'ccode', 'holdingbranch', 'location' );
+    @FIELDS = ( 'itype', 'ccode', 'homebranch', 'holdingbranch', 'location' );
 }
 
 =head1 NAME
@@ -78,19 +83,17 @@ sub GetCourse {
     my ($course_id) = @_;
     warn whoami() . "( $course_id )" if $DEBUG;
 
-    my $query = "SELECT * FROM courses WHERE course_id = ?";
-    my $dbh   = C4::Context->dbh;
-    my $sth   = $dbh->prepare($query);
-    $sth->execute($course_id);
-
-    my $course = $sth->fetchrow_hashref();
+    my $course = Koha::Courses->find( $course_id );
+    return unless $course;
+    $course = $course->unblessed;
 
-    $query = "
+    my $dbh = C4::Context->dbh;
+    my $query = "
         SELECT b.* FROM course_instructors ci
         LEFT JOIN borrowers b ON ( ci.borrowernumber = b.borrowernumber )
         WHERE course_id =  ?
     ";
-    $sth = $dbh->prepare($query);
+    my $sth = $dbh->prepare($query);
     $sth->execute($course_id);
     $course->{'instructors'} = $sth->fetchall_arrayref( {} );
 
@@ -304,7 +307,7 @@ sub EnableOrDisableCourseItem {
     ## or disable and already disabled item,
     ## as that would cause the fields to swap
     if ( $course_item->{'enabled'} ne $enabled ) {
-        _SwapAllFields($ci_id);
+        _SwapAllFields($ci_id, $enabled );
 
         my $query = "
             UPDATE course_items
@@ -492,23 +495,21 @@ sub _AddCourseItem {
     my (%params) = @_;
     warn identify_myself(%params) if $DEBUG;
 
-    my ( @fields, @values );
-
-    push( @fields, 'itemnumber = ?' );
-    push( @values, $params{'itemnumber'} );
+    $params{homebranch} ||= undef; # Can't be empty string, FK constraint
+    $params{holdingbranch} ||= undef; # Can't be empty string, FK constraint
 
-    foreach (@FIELDS) {
-        push( @fields, "$_ = ?" );
-        push( @values, $params{$_} || undef );
-    }
-
-    my $query = "INSERT INTO course_items SET " . join( ',', @fields );
-    my $dbh = C4::Context->dbh;
-    $dbh->do( $query, undef, @values );
+    my %data = map { $_ => $params{$_} } @FIELDS;
+    my %enabled = map { $_ . "_enabled" => $params{ $_ . "_enabled" } } @FIELDS;
 
-    my $ci_id = $dbh->last_insert_id( undef, undef, 'course_items', 'ci_id' );
+    my $ci = Koha::Course::Item->new(
+        {
+            itemnumber => $params{itemnumber},
+            %data,
+            %enabled,
+        }
+    )->store();
 
-    return $ci_id;
+    return $ci->id;
 }
 
 =head2 _UpdateCourseItem
@@ -524,52 +525,49 @@ sub _UpdateCourseItem {
     my $ci_id         = $params{'ci_id'};
     my $course_item   = $params{'course_item'};
 
-    return unless ( $ci_id || $course_item );
-
-    $course_item = GetCourseItem( ci_id => $ci_id )
-      unless ($course_item);
-    $ci_id = $course_item->{'ci_id'} unless ($ci_id);
-
-    my %mod_params =
-      map {
-        defined $params{$_} && $params{$_} ne ''
-          ? ( $_ => $params{$_} )
-          : ()
-      } @FIELDS;
-
-    Koha::Items->find( $course_item->{itemnumber} )
-               ->set( \%mod_params )
-               ->store;
-}
+    $params{homebranch} ||= undef; # Can't be empty string, FK constraint
+    $params{holdingbranch} ||= undef; # Can't be empty string, FK constraint
 
-=head2 _ModStoredFields
+    return unless ( $ci_id || $course_item );
 
-    _ModStoredFields( %params );
+    $course_item = Koha::Course::Items->find( $ci_id || $course_item->{ci_id} );
 
-    Updates the values for the 'original' fields in course_items
-    for a given ci_id
+    my %data = map { $_ => $params{$_} } @FIELDS;
+    my %enabled = map { $_ . "_enabled" => $params{ $_ . "_enabled" } } @FIELDS;
 
-=cut
+    my $item = Koha::Items->find( $course_item->itemnumber );
 
-sub _ModStoredFields {
-    my (%params) = @_;
-    warn identify_myself(%params) if $DEBUG;
+    # Handle updates to changed fields for a course item, both adding and removing
+    if ( $course_item->is_enabled ) {
+        my $item_fields = {};
 
-    return unless ( $params{'ci_id'} );
+        for my $field ( @FIELDS ) {
 
-    my ( @fields_to_update, @values_to_update );
+            my $field_enabled = $field . '_enabled';
+            my $field_storage = $field . '_storage';
 
-    foreach (@FIELDS) {
-        if ( defined($params{$_}) ) {
-            push( @fields_to_update, $_ );
-            push( @values_to_update, $params{$_} );
+            # 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};
+            }
         }
-    }
 
-    my $query = "UPDATE course_items SET " . join( ',', map { "$_=?" } @fields_to_update ) . " WHERE ci_id = ?";
+        $item->set( $item_fields )->store
+            if keys %$item_fields;
+    }
 
-    C4::Context->dbh->do( $query, undef, @values_to_update, $params{'ci_id'} )
-      if (@values_to_update);
+    $course_item->update( { %data, %enabled } );
 
 }
 
@@ -577,6 +575,8 @@ sub _ModStoredFields {
 
     _RevertFields( ci_id => $ci_id, fields => \@fields_to_revert );
 
+    Copies fields from course item storage back to the actual item
+
 =cut
 
 sub _RevertFields {
@@ -585,15 +585,28 @@ sub _RevertFields {
 
     my $ci_id = $params{'ci_id'};
 
-    return unless ($ci_id);
+    return unless $ci_id;
 
-    my $course_item = GetCourseItem( ci_id => $params{'ci_id'} );
-    my $course_item_object;
-    foreach my $field ( @FIELDS ) {
-        next unless defined $course_item->{$field};
-        $course_item->$field($course_item->{$field});
-    }
-    $course_item_object->store;
+    my $course_item = Koha::Course::Items->find( $ci_id );
+
+    my $item_fields = {};
+    $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 )
+               ->set( $item_fields )
+               ->store
+        if keys %$item_fields;
+
+    $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();
 }
 
 =head2 _SwapAllFields
@@ -603,21 +616,52 @@ sub _RevertFields {
 =cut
 
 sub _SwapAllFields {
-    my ($ci_id) = @_;
+    my ( $ci_id, $enabled ) = @_;
     warn "C4::CourseReserves::_SwapFields( $ci_id )" if $DEBUG;
 
-    my $course_item = GetCourseItem( ci_id => $ci_id );
-    my $item = Koha::Items->find($course_item->{'itemnumber'});
-
-    my %item_fields;
-    foreach (@FIELDS) {
-        if ( defined( $course_item->{$_} ) ) {
-            $item_fields{$_}        = $item->$_ || q{};
-            $item->$_($course_item->{$_});
-        }
+    my $course_item = Koha::Course::Items->find( $ci_id );
+    my $item = Koha::Items->find( $course_item->itemnumber );
+
+    if ( $enabled eq 'yes' ) { # Copy item fields to course item storage, course item fields to item
+        $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();
+
+        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->{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 )
+                   ->set( $item_fields )
+                   ->store
+            if keys %$item_fields;
+
+    } else { # Copy course item storage to item
+        my $item_fields = {};
+        $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 )
+                   ->set( $item_fields )
+                   ->store
+            if keys %$item_fields;
+
+        $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();
     }
-    $item->store;
-    _ModStoredFields( %item_fields, ci_id => $ci_id );
 }
 
 =head2 GetCourseItems {
@@ -676,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