From ff4cc059efb173be1e4e9bdbbb90a1a4dcc99980 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 makes use of new module Koha::AdditionalField to provide additional fields to order lines. Once created, these fields can be filled during order line creation or modification. If additional field is linked to a MARC field, then value from biblio record is retrieved at order line creation. When saving order line (at creation or modification), values in additional fields are saved into biblio record. 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 a 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 b/ no AV category, a MARC field linked to AV category c/ a AV category, no MARC field d/ a AV category, a MARC field not linked to AV category e/ a AV category, a MARC field linked to another AV category In case of (e), the additional AV category should be used, not the AV category linked to 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) (check this is the right AV category for (e)) 9/ Fill these fields with some data and save order line 10/ check that data was correctly saved into biblio for fields (a), (b), (d) and (e) 11/ modify the same order line, check that values you've filled is correctly retrieved. 12/ modify all values, save, and check biblio once again 13/ create a new order line on the same biblio used for previous order line. check that values are correctly retrieved from biblio Signed-off-by: Harold Dramer --- Koha/Template/Plugin/AuthorisedValues.pm | 7 + acqui/add_order_fields.pl | 123 +++++++++++++++ acqui/addorder.pl | 29 ++++ acqui/neworderempty.pl | 47 ++++-- .../prog/en/includes/acquisitions-menu.inc | 1 + .../prog/en/modules/acqui/add_order_fields.tt | 165 ++++++++++++++++++++ .../prog/en/modules/acqui/neworderempty.tt | 30 ++++ 7 files changed, 388 insertions(+), 14 deletions(-) create mode 100755 acqui/add_order_fields.pl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/acqui/add_order_fields.tt diff --git a/Koha/Template/Plugin/AuthorisedValues.pm b/Koha/Template/Plugin/AuthorisedValues.pm index 36ddce1..ae66077 100644 --- a/Koha/Template/Plugin/AuthorisedValues.pm +++ b/Koha/Template/Plugin/AuthorisedValues.pm @@ -38,6 +38,11 @@ sub GetAuthValueDropbox { return C4::Koha::GetAuthvalueDropbox($category, $default); } +sub GetValues { + my ( $self, $category ) = @_; + return GetAuthorisedValues($category); +} + 1; =head1 NAME @@ -77,3 +82,5 @@ Kyle M Hall Jonathan Druart =cut + +1; diff --git a/acqui/add_order_fields.pl b/acqui/add_order_fields.pl new file mode 100755 index 0000000..ef77af6 --- /dev/null +++ b/acqui/add_order_fields.pl @@ -0,0 +1,123 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright 2014 BibLibre +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + +use Modern::Perl; +use CGI; +use C4::Auth; +use C4::Koha; +use C4::Output; +use Koha::AdditionalField; + +my $input = new CGI; + +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "acqui/add_order_fields.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { acquisition => '*' }, + debug => 1, + } +); + +my $op = $input->param('op') // 'list'; +my $field_id = $input->param('field_id'); +my @messages; + +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{}; + if ( $field_id and $name ) { + my $updated = 0; + eval { + my $af = Koha::AdditionalField->new({ + id => $field_id, + name => $name, + authorised_value_category => $authorised_value_category, + marcfield => $marcfield, + }); + $updated = $af->update; + }; + push @messages, { + code => 'update', + number => $updated, + }; + } elsif ( $name ) { + my $inserted = 0; + eval { + my $af = Koha::AdditionalField->new({ + tablename => 'aqorders', + name => $name, + authorised_value_category => $authorised_value_category, + marcfield => $marcfield, + }); + $inserted = $af->insert; + }; + push @messages, { + code => 'insert', + number => $inserted, + }; + } else { + push @messages, { + code => 'insert', + number => 0, + }; + } + $op = 'list'; +} + +if ( $op eq 'delete' ) { + my $deleted = 0; + eval { + my $af = Koha::AdditionalField->new( { id => $field_id } ); + $deleted = $af->delete; + $deleted = 0 if $deleted eq '0E0'; + }; + push @messages, { + code => 'delete', + number => $deleted, + }; + $op = 'list'; +} + +if ( $op eq 'add_form' ) { + my $categories = C4::Koha::GetAuthorisedValueCategories(); + my $field; + if ( $field_id ) { + $field = Koha::AdditionalField->new( { id => $field_id } )->fetch; + } + + $template->param( + field => $field, + categories => $categories, + ); +} + +if ( $op eq 'list' ) { + my $fields = Koha::AdditionalField->all( { tablename => 'aqorders' } ); + $template->param( fields => $fields ); +} + +$template->param( + op => $op, + messages => \@messages, +); + +output_html_with_http_headers $input, $cookie, $template->output; diff --git a/acqui/addorder.pl b/acqui/addorder.pl index f58e58d..5500a21 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -130,6 +130,8 @@ use C4::Budgets; use C4::Items; use C4::Output; +use Koha::AdditionalField; + ### "-------------------- addorder.pl ----------" # FIXME: This needs to do actual error checking and possibly return user to the same form, @@ -276,6 +278,33 @@ if ( $orderinfo->{quantity} ne '0' ) { $order->insert; } + # Retrieve and save additional fields values + my $record = GetMarcBiblio($orderinfo->{biblionumber}); + my $record_updated = 0; + my $additional_fields = Koha::AdditionalField->all({tablename => 'aqorders'}); + foreach my $af (@$additional_fields) { + my $id = $af->{id}; + my $value = $input->param("additional_field_$id"); + $af->{values}->{$orderinfo->{ordernumber}} = $value; + $af->insert_values; + + # Save value in biblio record, if marcfield is set + if ($af->{marcfield}) { + my ($fieldtag, $subfieldtag) = split /\$/, $af->{marcfield}; + my $field = $record->field($fieldtag); + if ($field) { + $field->update($subfieldtag => $value); + } else { + $field = new MARC::Field($fieldtag, '', '', $subfieldtag => $value); + $record->append_fields($field); + } + $record_updated = 1; + } + } + if ($record_updated) { + ModBiblio($record, $orderinfo->{biblionumber}); + } + # now, add items if applicable if (C4::Context->preference('AcqCreateItem') eq 'ordering') { diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl index 4a37815..3c7e609 100755 --- a/acqui/neworderempty.pl +++ b/acqui/neworderempty.pl @@ -66,29 +66,26 @@ the item's id in the breeding reservoir =cut -use warnings; -use strict; +use Modern::Perl; + use CGI qw ( -utf8 ); -use C4::Context; +use C4::Acquisition; use C4::Auth; +use C4::Biblio; # GetBiblioData GetMarcPrice +use C4::Branch; # GetBranches use C4::Budgets; - -use C4::Acquisition; +use C4::Context; use C4::Contract; -use C4::Suggestions; # GetSuggestion -use C4::Biblio; # GetBiblioData GetMarcPrice -use C4::Items; #PrepareItemRecord -use C4::Output; +use C4::ImportBatch qw/GetImportRecordMarc SetImportRecordStatus/; #needed for z3950 import: +use C4::Items; #PrepareItemRecord use C4::Koha; -use C4::Branch; # GetBranches use C4::Members; +use C4::Output; +use C4::Suggestions; # GetSuggestion use C4::Search qw/FindDuplicate/; - -#needed for z3950 import: -use C4::ImportBatch qw/GetImportRecordMarc SetImportRecordStatus/; - use Koha::Acquisition::Bookseller; +use Koha::AdditionalField; our $input = new CGI; my $booksellerid = $input->param('booksellerid'); # FIXME: else ERROR! @@ -329,6 +326,28 @@ if ( defined $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::AdditionalField->all({tablename => 'aqorders'}); +foreach my $af (@$additional_fields) { + if (!$af->{authorised_value_category} && $af->{marcfield}) { + # Try to retrieve authorised values category from MARC framework + my ($field, $subfield) = split /\$/, $af->{marcfield}; + my $category = C4::Koha::GetAuthValCodeFromField($field, $subfield); + if ($category) { + $af->{authorised_value_category} = $category; + } + } + if ($ordernumber) { + $af->fetch_values({record_id => $ordernumber}); + } elsif ($biblionumber && $af->{marcfield}) { + $record //= GetMarcBiblio($biblionumber); + my ($field, $subfield) = split /\$/, $af->{marcfield}; + $af->{values}->{'new'} = $record->subfield($field, $subfield); + } +} +$template->param(additional_fields => $additional_fields); + # fill template $template->param( close => $close, diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc index 439118d..364921b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/acquisitions-menu.inc @@ -9,4 +9,5 @@ [% IF ( CAN_user_parameters ) %]
  • Currencies
  • [% END %] +
  • Add order line fields
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/add_order_fields.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/add_order_fields.tt new file mode 100644 index 0000000..4fab644 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/add_order_fields.tt @@ -0,0 +1,165 @@ +[% INCLUDE 'doc-head-open.inc' %] +Koha › Acquisitions › Manage new fields for order lines + [% IF op == "list" %] › List of fields + [% ELSIF op == "add_form" %] + [% IF field %] › Modify field + [% ELSE %] › Add field + [% END %] + [% END %] + +[% INCLUDE 'doc-head-close.inc' %] + +[% INCLUDE "datatables.inc" %] + + + + + [% INCLUDE 'header.inc' %] + [% INCLUDE 'acquisitions-search.inc' %] + + + +
    +
    +
    +
    + [% IF op == 'list' %] + + [% END %] + + [% IF messages %] + [% FOR message IN messages %] + [% IF message.code == 'insert' %] + [% IF message.number > 0 %] +
    The field has been inserted
    + [% ELSE %] +
    The field has not been inserted (name still exist?)
    + [% END %] + [% ELSIF message.code == 'update' %] + [% IF message.number > 0 %] +
    The field has been updated
    + [% ELSE %] +
    The field has not been updated (name still exist?)
    + [% END %] + [% ELSIF message.code == 'delete' %] + [% IF message.number > 0 %] +
    The field has been deleted
    + [% ELSE %] +
    The field has not been deleted
    + [% END %] + [% END %] + [% END %] + [% END %] + + [% IF op == 'list' %] +

    Additional fields for order lines

    + [% IF fields %] + + + + + + + + + + + [% FOR field IN fields %] + + + + + + + [% END %] + +
    NameAuthorised value categoryMARC fieldActions
    [% field.name %][% field.authorised_value_category %][% field.marcfield %] + Edit + Delete +
    + [% ELSE %] + There is no field defined. + [% END %] + [% ELSIF op == 'add_form' %] + [% IF field %] +

    Modify field

    + [% ELSE %] +

    Add field

    + [% END %] +
    +
    +
      +
    1. + + +
    2. +
    3. + + +
    4. +
    5. + + + Use this syntax: XXX$y +
    6. +
    +
    +
    + [% IF field %] + + [% END %] + + + Cancel +
    +
    + [% END %] + +
    +
    + +
    +[% INCLUDE 'acquisitions-menu.inc' %] +
    +
    +[% INCLUDE 'intranet-bottom.inc' %] 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 7638f02..1c5db66 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt @@ -1,4 +1,5 @@ [% USE KohaDates %] +[% USE AuthorisedValues %] [% INCLUDE 'doc-head-open.inc' %] Koha › Acquisitions › Basket [% basketno %] › [% IF ( ordernumber ) %]Modify order details (line #[% ordernumber %])[% ELSE %]New order[% END %] [% INCLUDE 'doc-head-close.inc' %] @@ -625,6 +626,35 @@ $(document).ready(function() + [% FOREACH af IN additional_fields %] +
  • + + [% value = '' %] + [% IF ordernumber %] + [% value = af.values.$ordernumber %] + [% ELSE %] + [% value = af.values.new %] + [% END %] + [% authorised_values = [] %] + [% IF af.authorised_value_category %] + [% authorised_values = AuthorisedValues.GetValues(af.authorised_value_category) %] + [% END %] + [% IF authorised_values.size > 0 %] + + [% ELSE %] + + [% END %] +
  • + [% END %]
    -- 1.7.10.4