From 431f2d0fd99b59abec15bf4c22f4cfcbc71c7985 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 --- Koha/Template/Plugin/AuthorisedValues.pm | 5 + acqui/add_order_fields.pl | 123 +++++++++++++++ acqui/addorder.pl | 29 ++++ acqui/neworderempty.pl | 24 +++ .../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, 377 insertions(+) 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 0a19be2..da73c35 100644 --- a/Koha/Template/Plugin/AuthorisedValues.pm +++ b/Koha/Template/Plugin/AuthorisedValues.pm @@ -43,4 +43,9 @@ sub GetByCode { return encode( 'UTF-8', GetAuthorisedValueByCode( $category, $code, $opac ) ); } +sub GetValues { + my ( $self, $category ) = @_; + return GetAuthorisedValues($category); +} + 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 1b05f07..cf87d69 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, @@ -278,6 +280,33 @@ if ( $orderinfo->{quantity} ne '0' ) { @$orderinfo{qw(basketno ordernumber )} = NewOrder($orderinfo); } + # 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 9fdf032..7cfb5e9 100755 --- a/acqui/neworderempty.pl +++ b/acqui/neworderempty.pl @@ -91,6 +91,8 @@ use C4::Search qw/FindDuplicate/; #needed for z3950 import: use C4::ImportBatch qw/GetImportRecordMarc SetImportRecordStatus/; +use Koha::AdditionalField; + our $input = new CGI; my $booksellerid = $input->param('booksellerid'); # FIXME: else ERROR! my $budget_id = $input->param('budget_id') || 0; @@ -346,6 +348,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 596aa07..0bf8b03 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' %] @@ -574,6 +575,35 @@ $(document).ready(function() [% END %] + [% 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