@@ -, +, @@ - MARC field mode = get: The field cannot be modified and its value is retrieved from the bibliographic record (current behaviour) - MARC field mode = set: The field can be modified and its value is saved to the bibliographic record (new behaviour) a/ no AV category, a MARC field not linked to AV category, MARC field mode = get b/ no AV category, a MARC field not linked to AV category, MARC field mode = set c/ no AV category, a MARC field linked to AV category, MARC field mode = get d/ no AV category, a MARC field linked to AV category, MARC field mode = set e/ an AV category, no MARC field (supplier, basket, ...) additional fields, with authorised values dropdrown list for fields (c), (d) and (e). Fields (a) and (c) should be disabled. (d), but not for (a) and (c) correctly retrieved and that values for (a) and (c) were correctly retrieved from the bibliographic record --- C4/Acquisition.pm | 21 +++++++++--- Koha/Acquisition/Order.pm | 2 +- Koha/AdditionalField.pm | 19 +++++++++++ Koha/Object/Mixin/AdditionalFields.pm | 32 +++++++++++++++++++ Koha/Schema/Result/Aqorder.pm | 17 ++++++++++ acqui/addorder.pl | 15 +++++++++ acqui/neworderempty.pl | 24 ++++++++++++++ admin/additional-fields.pl | 3 ++ .../data/mysql/atomicupdate/bug-11844.pl | 17 ++++++++++ installer/data/mysql/kohastructure.sql | 1 + .../en/includes/additional-fields-entry.inc | 28 ++++++++++------ .../prog/en/modules/acqui/neworderempty.tt | 4 +++ .../en/modules/admin/additional-fields.tt | 26 ++++++++++++++- serials/subscription-add.pl | 14 -------- 14 files changed, 192 insertions(+), 31 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug-11844.pl --- a/C4/Acquisition.pm +++ a/C4/Acquisition.pm @@ -32,6 +32,7 @@ use Koha::Acquisition::Baskets; use Koha::Acquisition::Booksellers; use Koha::Acquisition::Invoices; use Koha::Acquisition::Orders; +use Koha::AdditionalFieldValue; use Koha::Biblios; use Koha::Exceptions; use Koha::Items; @@ -1880,7 +1881,7 @@ sub TransferOrder { my $order = Koha::Acquisition::Orders->find( $ordernumber ) or return; return if $order->datereceived; - $order = $order->unblessed; + my $orderhash = $order->unblessed; my $basket = GetBasket($basketno); return unless $basket; @@ -1896,11 +1897,12 @@ sub TransferOrder { $sth = $dbh->prepare($query); $rv = $sth->execute('cancelled', $ordernumber); - delete $order->{'ordernumber'}; - delete $order->{parent_ordernumber}; - $order->{'basketno'} = $basketno; + delete $orderhash->{ordernumber}; + delete $orderhash->{parent_ordernumber}; + $orderhash->{basketno} = $basketno; - my $newordernumber = Koha::Acquisition::Order->new($order)->store->ordernumber; + my $neworder = Koha::Acquisition::Order->new($orderhash); + my $newordernumber = $neworder->store->ordernumber; $query = q{ UPDATE aqorders_items @@ -1917,6 +1919,15 @@ sub TransferOrder { $sth = $dbh->prepare($query); $sth->execute($ordernumber, $newordernumber); + # Copy additional fields values + foreach my $afv ($order->additional_field_values->as_list) { + Koha::AdditionalFieldValue->new({ + field_id => $afv->field_id, + record_id => $newordernumber, + value => $afv->value, + })->store; + } + return $newordernumber; } --- a/Koha/Acquisition/Order.pm +++ a/Koha/Acquisition/Order.pm @@ -36,7 +36,7 @@ use Koha::Number::Price; use Koha::Patrons; use Koha::Subscriptions; -use base qw(Koha::Object); +use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields); =head1 NAME --- a/Koha/AdditionalField.pm +++ a/Koha/AdditionalField.pm @@ -11,6 +11,25 @@ use Modern::Perl; use base qw(Koha::Object); use C4::Context; +use Koha::MarcSubfieldStructures; + +sub effective_authorised_value_category { + my ($self) = @_; + + my $category = $self->authorised_value_category; + unless ($category) { + if ($self->marcfield) { + my ($tag, $subfield) = split /\$/, $self->marcfield; + + my $mss = Koha::MarcSubfieldStructures->find('', $tag, $subfield); + if ($mss) { + $category = $mss->authorised_value; + } + } + } + + return $category; +} sub _type { 'AdditionalField' } --- a/Koha/Object/Mixin/AdditionalFields.pm +++ a/Koha/Object/Mixin/AdditionalFields.pm @@ -46,8 +46,36 @@ sub set_additional_fields { $self->additional_field_values->delete; + my $biblionumber; + my $record; + my $record_updated; + if ($self->_result->has_column('biblionumber')) { + $biblionumber = $self->biblionumber; + } + foreach my $additional_field (@$additional_fields) { + my $field = Koha::AdditionalFields->find($additional_field->{id}); my $value = $additional_field->{value}; + + if ($biblionumber and $field->marcfield) { + require Koha::Biblios; + $record //= Koha::Biblios->find($biblionumber)->metadata->record; + + my ($tag, $subfield) = split /\$/, $field->marcfield; + my $marc_field = $record->field($tag); + if ($field->marcfield_mode eq 'get') { + $value = $marc_field ? $marc_field->subfield($subfield) : ''; + } elsif ($field->marcfield_mode eq 'set') { + if ($marc_field) { + $marc_field->update($subfield => $value); + } else { + $marc_field = MARC::Field->new($tag, '', '', $subfield => $value); + $record->append_fields($marc_field); + } + $record_updated = 1; + } + } + if (defined $value) { my $field_value = Koha::AdditionalFieldValue->new({ field_id => $additional_field->{id}, @@ -56,6 +84,10 @@ sub set_additional_fields { })->store; } } + + if ($record_updated) { + C4::Biblio::ModBiblio($record, $biblionumber); + } } =head3 additional_field_values --- a/Koha/Schema/Result/Aqorder.pm +++ a/Koha/Schema/Result/Aqorder.pm @@ -892,6 +892,23 @@ __PACKAGE__->belongs_to( }, ); +__PACKAGE__->has_many( + "additional_field_values", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.ordernumber" }, + + # TODO Add column additional_field_values.tablename to avoid subquery ? + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename = "aqorders")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + sub koha_objects_class { 'Koha::Acquisition::Orders'; } --- a/acqui/addorder.pl +++ a/acqui/addorder.pl @@ -139,6 +139,8 @@ use Koha::Acquisition::Baskets; use C4::Barcodes; use Koha::DateUtils qw( dt_from_string ); +use Koha::AdditionalFields; + ### "-------------------- addorder.pl ----------" # FIXME: This needs to do actual error checking and possibly return user to the same form, @@ -368,6 +370,19 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) { my @order_users = split( /:/, $order_users_ids ); ModOrderUsers( $order->ordernumber, @order_users ); + # Retrieve and save additional fields values + my @additional_fields = Koha::AdditionalFields->search({ tablename => 'aqorders' })->as_list; + my @additional_field_values; + foreach my $af (@additional_fields) { + my $id = $af->id; + my $value = $input->param("additional_field_$id"); + push @additional_field_values, { + id => $id, + value => $value, + }; + } + $order->set_additional_fields(\@additional_field_values); + # now, add items if applicable if ($basket->effective_create_items eq 'ordering') { --- a/acqui/neworderempty.pl +++ a/acqui/neworderempty.pl @@ -100,6 +100,7 @@ use Koha::Patrons; use Koha::RecordProcessor; use Koha::Subscriptions; use Koha::UI::Form::Builder::Biblio; +use Koha::AdditionalFields; our $input = CGI->new; my $booksellerid = $input->param('booksellerid'); # FIXME: else ERROR! @@ -415,6 +416,29 @@ my $quantity = $input->param('rr_quantity_to_order') ? $data->{'quantity'}; $quantity //= 0; +# Get additional fields +my $record; +my @additional_fields = Koha::AdditionalFields->search({ tablename => 'aqorders' })->as_list; +my %additional_field_values; +if ($ordernumber) { + my $order = Koha::Acquisition::Orders->find($ordernumber); + foreach my $value ($order->additional_field_values->as_list) { + $additional_field_values{$value->field_id} = $value->value; + } +} elsif ( $biblionumber ) { + foreach my $af (@additional_fields) { + if ($af->marcfield) { + $record //= Koha::Biblios->find($biblionumber)->metadata->record; + my ($field, $subfield) = split /\$/, $af->marcfield; + $additional_field_values{$af->id} = $record->subfield($field, $subfield); + } + } +} +$template->param( + additional_fields => \@additional_fields, + additional_field_values => \%additional_field_values, +); + # fill template $template->param( existing => $biblionumber, --- a/admin/additional-fields.pl +++ a/admin/additional-fields.pl @@ -51,6 +51,7 @@ if ( $op eq 'add' ) { my $name = $input->param('name') // q{}; my $authorised_value_category = $input->param('authorised_value_category') // q{}; my $marcfield = $input->param('marcfield') // q{}; + my $marcfield_mode = $input->param('marcfield_mode') // 'get'; my $searchable = $input->param('searchable') ? 1 : 0; if ( $field_id and $name ) { my $updated = 0; @@ -60,6 +61,7 @@ if ( $op eq 'add' ) { name => $name, authorised_value_category => $authorised_value_category, marcfield => $marcfield, + marcfield_mode => $marcfield_mode, searchable => $searchable, }); $updated = $af->store ? 1 : 0; @@ -76,6 +78,7 @@ if ( $op eq 'add' ) { name => $name, authorised_value_category => $authorised_value_category, marcfield => $marcfield, + marcfield_mode => $marcfield_mode, searchable => $searchable, }); $inserted = $af->store ? 1 : 0; --- a/installer/data/mysql/atomicupdate/bug-11844.pl +++ a/installer/data/mysql/atomicupdate/bug-11844.pl @@ -0,0 +1,17 @@ +use Modern::Perl; + +return { + bug_number => '11844', + description => 'Add column additional_fields.marcfield_mode', + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + unless ( column_exists( 'additional_fields', 'marcfield_mode' ) ) { + $dbh->do(q{ + ALTER TABLE additional_fields + ADD COLUMN marcfield_mode ENUM('get', 'set') NOT NULL DEFAULT 'get' COMMENT 'mode of operation (get or set) for marcfield' AFTER marcfield + }); + } + }, +}; --- a/installer/data/mysql/kohastructure.sql +++ a/installer/data/mysql/kohastructure.sql @@ -244,6 +244,7 @@ CREATE TABLE `additional_fields` ( `name` varchar(255) NOT NULL DEFAULT '' COMMENT 'name of the field', `authorised_value_category` varchar(32) NOT NULL DEFAULT '' COMMENT 'is an authorised value category', `marcfield` varchar(16) NOT NULL DEFAULT '' COMMENT 'contains the marc field to copied into the record', + `marcfield_mode` ENUM('get', 'set') NOT NULL DEFAULT 'get' COMMENT 'mode of operation (get or set) for marcfield', `searchable` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'is the field searchable?', PRIMARY KEY (`id`), UNIQUE KEY `fields_uniq` (`tablename`(191),`name`(191)) --- a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc +++ a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc @@ -5,26 +5,34 @@
    [% END %] [% FOR field IN available %] + [% authorised_value_category = field.effective_authorised_value_category %]
  1. - [% IF field.authorised_value_category %] - + [% ELSE %] + (Authorised values for [% field.authorised_value_category | html %]) + (Authorised values for [% authorised_value_category | html %]) + [% ELSIF field.marcfield && field.marcfield_mode == 'get' %] + [% ELSE %] - [% IF field.marcfield %] - - This value will be filled with the [% field.marcfield | html %] subfield of the selected biblio. - [% ELSE %] - - [% END %] + + [% END %] + + [% IF field.marcfield && field.marcfield_mode == 'get' %] + This value will be filled with the [% field.marcfield | html %] subfield of the selected biblio. + [% ELSIF field.marcfield && field.marcfield_mode == 'set' %] + This value will be saved to the [% field.marcfield %] subfield of the selected biblio. [% END %]
  2. [% END %] --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt @@ -4,6 +4,7 @@ [% USE KohaDates %] [% USE Price %] [% USE ItemTypes %] +[% USE AuthorisedValues %] [% INCLUDE 'doc-head-open.inc' %] [% IF ( ordernumber ) %]Modify order details (line #[% ordernumber | html %])[% ELSE %]New order[% END %] › Basket [% basketno | html %] › Acquisitions › Koha [% INCLUDE 'doc-head-close.inc' %] @@ -697,6 +698,9 @@
+ + [% INCLUDE 'additional-fields-entry.inc' available = additional_fields values = additional_field_values %] +
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/additional-fields.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/additional-fields.tt @@ -11,7 +11,7 @@ [% INCLUDE 'doc-head-close.inc' %] -[% marcfield_tables = ['subscription'] %] +[% marcfield_tables = ['subscription', 'aqorders'] %] [% searchable_tables = ['subscription', 'aqbasket', 'aqinvoices'] %] [% show_marcfield = marcfield_tables.grep('^' _ tablename _ '$').size ? 1 : 0 %] [% show_searchable = searchable_tables.grep('^' _ tablename _ '$').size ? 1 : 0 %] @@ -105,6 +105,7 @@