%EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
$DEBUG = 0;
- @FIELDS = ( 'itype', 'ccode', 'holdingbranch', 'location' );
+ @FIELDS = ( 'itype', 'ccode', 'homebranch', 'holdingbranch', 'location' );
}
=head1 NAME
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;
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;
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 );
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
$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 )
$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();
}
$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();
$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 )
$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 )
$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();
}
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