From 7999ab565a0703165d554269600e20717355d8ea Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 5 Feb 2014 11:12:21 +0100 Subject: [PATCH] Bug 11844: Use additional fields for order lines This patch allows to create additional fields for order lines. Once created, these fields can be filled during order line creation or modification. If additional field is linked to a MARC field, there are two possible scenario: - 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) If additional field is linked to an authorised value category, then authorised values are used. If not directly linked to an authorised value category, but linked to a MARC field, a search for an AV category is made on MARC default framework. This patch doesn't display additional fields value anywhere (except in order line creation/modification). Future patches will do that. Test plan: 1/ Go to Acquisitions home 2/ In the left menu, click on "Add order line fields" 3/ Click on "New field" button 4/ Give the field a name (unique), no AV category and no MARC field. 5/ Save. 6/ Create 5 other fields: 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 7/ Create everything you need to be able to create order lines (supplier, basket, ...) 8/ Create an order line. At bottom of the page, you should see your additional fields, with authorised values dropdrown list for fields (c), (d) and (e). Fields (a) and (c) should be disabled. 9/ Fill these fields with some data and save order line 10/ check that data was correctly saved into biblio for fields (b) and (d), but not for (a) and (c) 11/ modify the same order line, check that values you've filled are correctly retrieved and that values for (a) and (c) were correctly retrieved from the bibliographic record 12/ modify all values, save, and check biblio once again Signed-off-by: Harold Dramer --- C4/Acquisition.pm | 21 +++++++--- Koha/Acquisition/Order.pm | 2 +- Koha/AdditionalField.pm | 19 +++++++++ Koha/Object/Mixin/AdditionalFields.pm | 32 ++++++++++++++ Koha/Schema/Result/Aqorder.pm | 16 +++++++ acqui/addorder.pl | 15 +++++++ acqui/neworderempty.pl | 24 +++++++++++ admin/additional-fields.pl | 3 ++ installer/data/mysql/atomicupdate/bug_11844.perl | 12 ++++++ .../prog/en/includes/additional-fields-entry.inc | 49 ++++++++++------------ .../en/includes/additional-fields-fieldset.inc | 6 +++ .../prog/en/modules/acqui/basketheader.tt | 4 +- .../prog/en/modules/acqui/histsearch.tt | 2 +- .../prog/en/modules/acqui/neworderempty.tt | 6 +++ .../prog/en/modules/admin/additional-fields.tt | 26 +++++++++++- .../prog/en/modules/serials/serials-search.tt | 2 +- .../prog/en/modules/serials/subscription-add.tt | 4 +- serials/subscription-add.pl | 14 ------- 18 files changed, 204 insertions(+), 53 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_11844.perl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-fieldset.inc diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 8bbd0ce310..d3d7651a4f 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -31,6 +31,7 @@ use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Acquisition::Baskets; use Koha::Acquisition::Booksellers; use Koha::Acquisition::Orders; +use Koha::AdditionalFieldValue; use Koha::Biblios; use Koha::Items; use Koha::Number::Price; @@ -1961,7 +1962,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; @@ -1977,11 +1978,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 @@ -1998,6 +2000,15 @@ sub TransferOrder { $sth = $dbh->prepare($query); $sth->execute($ordernumber, $newordernumber); + # Copy additional fields values + foreach my $afv ($order->additional_field_values) { + Koha::AdditionalFieldValue->new({ + field_id => $afv->field_id, + record_id => $newordernumber, + value => $afv->value, + })->store; + } + return $newordernumber; } diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm index 79fb056802..3f126fb7fe 100644 --- a/Koha/Acquisition/Order.pm +++ b/Koha/Acquisition/Order.pm @@ -23,7 +23,7 @@ use Koha::Acquisition::Baskets; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); -use base qw(Koha::Object); +use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields); =head1 NAME diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index 78de775d5e..1d32272281 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -5,6 +5,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' } diff --git a/Koha/Object/Mixin/AdditionalFields.pm b/Koha/Object/Mixin/AdditionalFields.pm index 090e2824c2..4b3ce6a1cb 100644 --- a/Koha/Object/Mixin/AdditionalFields.pm +++ b/Koha/Object/Mixin/AdditionalFields.pm @@ -45,18 +45,50 @@ sub set_additional_fields { my $rs = Koha::Database->new->schema->resultset('AdditionalFieldValue'); + my $biblionumber; + my $record; + my $record_updated; + if ($self->_result->has_column('biblionumber')) { + $biblionumber = $self->biblionumber; + } + foreach my $additional_field (@$additional_fields) { my $field_value = $rs->find_or_new({ field_id => $additional_field->{id}, record_id => $self->id, }); + my $field = $field_value->field; my $value = $additional_field->{value}; + + if ($biblionumber and $field->marcfield) { + require C4::Biblio; + $record //= C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber }); + + 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) { $field_value->set_columns({ value => $value })->update_or_insert; } elsif ($field_value->in_storage) { $field_value->delete; } } + + if ($record_updated) { + C4::Biblio::ModBiblio($record, $biblionumber); + } } sub additional_field_values { diff --git a/Koha/Schema/Result/Aqorder.pm b/Koha/Schema/Result/Aqorder.pm index bafbe91b7f..fa2e3a82e3 100644 --- a/Koha/Schema/Result/Aqorder.pm +++ b/Koha/Schema/Result/Aqorder.pm @@ -628,6 +628,22 @@ __PACKAGE__->many_to_many("borrowernumbers", "aqorder_users", "borrowernumber"); # Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-02-16 17:54:53 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:K0GnMGYtZUQ1WCesHKIxHw +__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 }, +); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/acqui/addorder.pl b/acqui/addorder.pl index 17b50e6185..e2687b9d26 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -132,6 +132,8 @@ use Koha::Acquisition::Currencies; use Koha::Acquisition::Orders; use C4::Barcodes; +use Koha::AdditionalFields; + ### "-------------------- addorder.pl ----------" # FIXME: This needs to do actual error checking and possibly return user to the same form, @@ -299,6 +301,19 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) { $order->store; } + # Retrieve and save additional fields values + my @additional_fields = Koha::AdditionalFields->search({ tablename => 'aqorders' }); + 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') { diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl index 6326eaf39b..9af9c90301 100755 --- a/acqui/neworderempty.pl +++ b/acqui/neworderempty.pl @@ -94,6 +94,7 @@ use Koha::MarcSubfieldStructures; use Koha::ItemTypes; use Koha::Patrons; use Koha::RecordProcessor; +use Koha::AdditionalFields; our $input = new CGI; my $booksellerid = $input->param('booksellerid'); # FIXME: else ERROR! @@ -388,6 +389,29 @@ if ( defined $from_subscriptionid ) { # Find the items.barcode subfield for barcode validations my (undef, $barcode_subfield) = GetMarcFromKohaField('items.barcode', ''); +# Get additional fields +my $record; +my @additional_fields = Koha::AdditionalFields->search({ tablename => 'aqorders' }); +my %additional_field_values; +if ($ordernumber) { + my $order = Koha::Acquisition::Orders->find($ordernumber); + foreach my $value ($order->additional_field_values) { + $additional_field_values{$value->field_id} = $value->value; + } +} elsif ( $biblionumber ) { + foreach my $af (@additional_fields) { + if ($af->marcfield) { + $record //= GetMarcBiblio({ biblionumber => $biblionumber }); + 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( close => $close, diff --git a/admin/additional-fields.pl b/admin/additional-fields.pl index 5bc722c09d..c573fc863a 100755 --- a/admin/additional-fields.pl +++ b/admin/additional-fields.pl @@ -46,6 +46,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; @@ -55,6 +56,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; @@ -71,6 +73,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; diff --git a/installer/data/mysql/atomicupdate/bug_11844.perl b/installer/data/mysql/atomicupdate/bug_11844.perl new file mode 100644 index 0000000000..dc92c1cb32 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_11844.perl @@ -0,0 +1,12 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + if( !column_exists( 'additional_fields', 'marcfield_mode' ) ) { + $dbh->do(q{ + ALTER TABLE additional_fields + ADD COLUMN marcfield_mode ENUM('get', 'set') NOT NULL DEFAULT 'get' AFTER marcfield + }); + } + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 11844 - Add column additional_fields.marcfield_mode)\n"; +} diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc index c8cbd83bcd..52646a245d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-entry.inc @@ -1,34 +1,27 @@ [% USE AuthorisedValues %] -[% IF wrap_fieldset != 0 %] -
- Additional fields -
    -[% END %] - [% FOR field IN available %] -
  1. - - [% IF field.authorised_value_category %] - (Authorised values for [% field.authorised_value_category %]) - [% ELSE %] - [% IF field.marcfield %] - - This value will be filled with the [% field.marcfield %] subfield of the selected biblio. +[% FOR field IN available %] + [% disabled = (field.marcfield && field.marcfield_mode == 'get') ? 'disabled' : '' %] + [% authorised_value_category = field.effective_authorised_value_category %] +
  2. + + [% IF authorised_value_category %] + + [% END %] [% END %] -
  3. + (Authorised values for [% authorised_value_category %]) + [% ELSE %] + + [% END %] + [% IF field.marcfield && field.marcfield_mode == 'get' %] + This value will be filled with the [% field.marcfield %] 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 %] -[% IF wrap_fieldset != 0 %] -
-
+ [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-fieldset.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-fieldset.inc new file mode 100644 index 0000000000..1e205418ca --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/additional-fields-fieldset.inc @@ -0,0 +1,6 @@ +
+ Additional fields +
    + [% content %] +
+
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketheader.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketheader.tt index 43f6a33119..3114821ce1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketheader.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketheader.tt @@ -128,7 +128,9 @@ [% IF available_additional_fields %] - [% INCLUDE 'additional-fields-entry.inc' available=available_additional_fields values=additional_field_values %] + [% WRAPPER 'additional-fields-fieldset.inc' %] + [% INCLUDE 'additional-fields-entry.inc' available=available_additional_fields values=additional_field_values %] + [% END %] [% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt index 1d89ad2820..6c59b44335 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/histsearch.tt @@ -44,7 +44,7 @@ - [% INCLUDE 'additional-fields-entry.inc' available=available_additional_fields values=additional_field_filters wrap_fieldset=0 %] + [% INCLUDE 'additional-fields-entry.inc' available=available_additional_fields values=additional_field_filters %]
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt index 1a10af9f2c..adfa6fd18b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt @@ -1,6 +1,7 @@ [% USE Asset %] [% USE Koha %] [% USE KohaDates %] +[% USE AuthorisedValues %] [% SET footerjs = 1 %] [% INCLUDE 'doc-head-open.inc' %] Koha › Acquisitions › Basket [% basketno %] › [% IF ( ordernumber ) %]Modify order details (line #[% ordernumber %])[% ELSE %]New order[% END %] @@ -474,6 +475,11 @@
  • + + [% WRAPPER 'additional-fields-fieldset.inc' %] + [% INCLUDE 'additional-fields-entry.inc' available = additional_fields values = additional_field_values %] + [% END %] +
    diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/additional-fields.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/additional-fields.tt index bdd6984be5..1883c4c7da 100755 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/additional-fields.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/additional-fields.tt @@ -12,7 +12,7 @@ [% Asset.css('css/datatables.css') %] -[% marcfield_tables = ['subscription'] %] +[% marcfield_tables = ['subscription', 'aqorders'] %] [% show_marcfield = marcfield_tables.grep('^' _ tablename _ '$').size ? 1 : 0 %] @@ -70,6 +70,7 @@ [% END %]
      [% WRAPPER table_option value="aqbasket" %]Order baskets[% END %] + [% WRAPPER table_option value="aqorders" %]Order lines[% END %] [% WRAPPER table_option value="subscription" %]Subscriptions[% END %]
    [% ELSIF op == 'list' %] @@ -82,6 +83,7 @@ Authorised value category [% IF show_marcfield %] MARC field + MARC field mode [% END %] Searchable Actions @@ -94,6 +96,12 @@ [% field.authorised_value_category %] [% IF show_marcfield %] [% field.marcfield %] + + [% SWITCH field.marcfield_mode %] + [% CASE 'get' %]Get value from MARC record + [% CASE 'set' %]Save value to MARC record + [% END %] + [% END %] [% IF field.searchable %]Yes[% ELSE %]No[% END %] @@ -139,6 +147,22 @@ +
  • + + +
  • [% END %]
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt index 8a37128168..7cff2813b4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt @@ -256,7 +256,7 @@
  • - [% INCLUDE 'additional-fields-entry.inc' available=additional_fields_for_subscription values=additional_field_filters wrap_fieldset=0 %] + [% INCLUDE 'additional-fields-entry.inc' available=additional_fields_for_subscription values=additional_field_filters %]
    diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt index 39c1a0b05f..b9b1d34408 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt @@ -438,7 +438,9 @@ fieldset.rows li.radio { width: 100%; } /* override staff-global.css */ [% IF additional_fields_for_subscription %]
    - [% INCLUDE 'additional-fields-entry.inc' available=additional_fields_for_subscription values=additional_fields %] + [% WRAPPER 'additional-fields-fieldset.inc' %] + [% INCLUDE 'additional-fields-entry.inc' available=additional_fields_for_subscription values=additional_fields %] + [% END %]
    [% END %] diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index 1c10528777..628304b231 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -340,15 +340,8 @@ sub redirect_add_subscription { ); my @additional_fields; - my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 }); for my $field (Koha::AdditionalFields->search({ tablename => 'subscription' })) { my $value = $query->param('additional_field_' . $field->id); - if ($field->marcfield) { - my ($field, $subfield) = split /\$/, $field->marcfield; - if ( $record and $field and $subfield ) { - $value = $record->subfield( $field, $subfield ); - } - } push @additional_fields, { id => $field->id, value => $value, @@ -438,15 +431,8 @@ sub redirect_mod_subscription { ); my @additional_fields; - my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 }); for my $field (Koha::AdditionalFields->search({ tablename => 'subscription' })) { my $value = $query->param('additional_field_' . $field->id); - if ($field->marcfield) { - my ($field, $subfield) = split /\$/, $field->marcfield; - if ( $record and $field and $subfield ) { - $value = $record->subfield( $field, $subfield ); - } - } push @additional_fields, { id => $field->id, value => $value, -- 2.14.2