From c5299d094d211d8349416befee9290d10912bab1 Mon Sep 17 00:00:00 2001
From: Jesse Weaver <jweaver@bywatersolutions.com>
Date: Thu, 21 Jul 2016 14:48:57 -0600
Subject: [PATCH] Bug 11844: (followup) use new unified additional field screen

---
 acqui/add_order_fields.pl                          | 123 ---------------
 acqui/neworderempty.pl                             |  35 +++--
 .../prog/en/modules/acqui/add_order_fields.tt      | 165 ---------------------
 .../prog/en/modules/acqui/neworderempty.tt         |  31 +---
 .../prog/en/modules/admin/additional-fields.tt     |   1 +
 5 files changed, 23 insertions(+), 332 deletions(-)
 delete mode 100755 acqui/add_order_fields.pl
 delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/acqui/add_order_fields.tt

diff --git a/acqui/add_order_fields.pl b/acqui/add_order_fields.pl
deleted file mode 100755
index ef77af6..0000000
--- a/acqui/add_order_fields.pl
+++ /dev/null
@@ -1,123 +0,0 @@
-#!/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 <http://www.gnu.org/licenses>.
-
-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/neworderempty.pl b/acqui/neworderempty.pl
index e4aa0b4..9c21d49 100755
--- a/acqui/neworderempty.pl
+++ b/acqui/neworderempty.pl
@@ -301,25 +301,28 @@ 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;
+my $available_additional_fields = Koha::AdditionalField->all( { tablename => 'aqorders' } );
+my $additional_field_values = {};
+
+if ( $ordernumber ) {
+    $additional_field_values = Koha::AdditionalField->fetch_all_values( {
+        tablename => 'aqorders',
+        record_id => $ordernumber,
+    } )->{$ordernumber};
+} elsif ( $biblionumber ) {
+    foreach my $af (@$additional_fields) {
+        if ($af->{marcfield}) {
+            $record //= GetMarcBiblio($biblionumber);
+            my ($field, $subfield) = split /\$/, $af->{marcfield};
+            $additional_field_values->{ $af->{name} } = $record->subfield($field, $subfield);
         }
     }
-    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);
+
+$template->param(
+    available_additional_fields => $available_additional_fields,
+    additional_field_values => $additional_field_values
+);
 
 # fill template
 $template->param(
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
deleted file mode 100644
index 4fab644..0000000
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/add_order_fields.tt
+++ /dev/null
@@ -1,165 +0,0 @@
-[% INCLUDE 'doc-head-open.inc' %]
-<title>Koha &rsaquo; Acquisitions &rsaquo; Manage new fields for order lines
-  [% IF op == "list" %] &rsaquo; List of fields
-  [% ELSIF op == "add_form" %]
-    [% IF field %] &rsaquo; Modify field
-    [% ELSE %] &rsaquo; Add field
-    [% END %]
-  [% END %]
-</title>
-[% INCLUDE 'doc-head-close.inc' %]
-<link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
-[% INCLUDE "datatables.inc" %]
-<script type="text/javascript">
-//<![CDATA[
-  $(document).ready(function(){
-
-    $("#fieldst").dataTable($.extend(true, {}, dataTablesDefaults, {
-        'bAutoWidth': false,
-        'sDom': 't<"bottom pager"ilpf>',
-        'sPaginationType': 'four_button',
-        'aLengthMenu': [[10, 20, 50, 100, -1], [10, 20, 50, 100, "All"]],
-        'iDisplayLength': 20,
-        'aaSorting': [[ 0, "asc" ]],
-    }));
-
-    $(".confirmdelete").click(function(){
-      return confirm(_("Are you sure you want to delete this field?"));
-    });
-
-    $("#add_field").on('submit', function(){
-        if ( $("#marcfield").val().length > 0
-            && $("select[name='authorised_value_category']" ).val().length > 0 ) {
-            alert("You cannot select an authorised value category and a marcfield");
-            return false;
-        }
-        return true;
-    });
-  });
-//]]>
-</script>
-</head>
-
-<body id="acq_add_fields" class="acq">
-  [% INCLUDE 'header.inc' %]
-  [% INCLUDE 'acquisitions-search.inc' %]
-
-  <div id="breadcrumbs">
-    <a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/acqui/acqui-home.pl">Acquisitions</a> &rsaquo; Manage new fields for order lines
-  </div>
-
-  <div id="doc3" class="yui-t2">
-  <div id="bd">
-  <div id="yui-main">
-  <div class="yui-b">
-  [% IF op == 'list' %]
-    <div id="toolbar" class="btn-toolbar">
-      <a class="btn btn-small" id="newfields" href="/cgi-bin/koha/acqui/add_order_fields.pl?op=add_form"><i class="icon-plus"></i> New field</a>
-    </div>
-  [% END %]
-
-  [% IF messages %]
-    [% FOR message IN messages %]
-      [% IF message.code == 'insert' %]
-        [% IF message.number > 0 %]
-          <div class="dialog message">The field has been inserted</div>
-        [% ELSE %]
-          <div class="dialog alert">The field has not been inserted (name still exist?)</div>
-        [% END %]
-      [% ELSIF message.code == 'update' %]
-        [% IF message.number > 0 %]
-          <div class="dialog message">The field has been updated</div>
-        [% ELSE %]
-          <div class="dialog alert">The field has not been updated (name still exist?)</div>
-        [% END %]
-      [% ELSIF message.code == 'delete' %]
-        [% IF message.number > 0 %]
-          <div class="dialog message">The field has been deleted</div>
-        [% ELSE %]
-          <div class="dialog alert">The field has not been deleted</div>
-        [% END %]
-      [% END %]
-    [% END %]
-  [% END %]
-
-  [% IF op == 'list' %]
-    <h3>Additional fields for order lines</h3>
-    [% IF fields %]
-      <table id="fieldst">
-        <thead>
-          <tr>
-            <th>Name</th>
-            <th>Authorised value category</th>
-            <th>MARC field</th>
-            <th>Actions</th>
-          </tr>
-        </thead>
-        <tbody>
-          [% FOR field IN fields %]
-            <tr>
-              <td>[% field.name %]</td>
-              <td>[% field.authorised_value_category %]</td>
-              <td>[% field.marcfield %]</td>
-              <td>
-                <a href="/cgi-bin/koha/acqui/add_order_fields.pl?op=add_form&amp;field_id=[% field.id %]" title="Edit this field">Edit</a>
-                <a class="confirmdelete" href="/cgi-bin/koha/acqui/add_order_fields.pl?op=delete&amp;field_id=[% field.id %]" title="Delete this field">Delete</a>
-              </td>
-            </tr>
-          [% END %]
-        </tbody>
-      </table>
-    [% ELSE %]
-      There is no field defined.
-    [% END %]
-  [% ELSIF op == 'add_form' %]
-    [% IF field %]
-      <h3>Modify field</h3>
-    [% ELSE %]
-      <h3>Add field</h3>
-    [% END %]
-    <form action="/cgi-bin/koha/acqui/add_order_fields.pl" name="add_form" id="add_field" method="post">
-      <fieldset class="rows">
-        <ol>
-          <li>
-            <label for="name" class="required">Name: </label>
-            <input type="text" name="name" id="name" value="[% field.name | html %]" />
-          </li>
-          <li>
-            <label for="av">Authorised value category: </label>
-            <select name="authorised_value_category">
-              <option value="">None</option>
-              [% FOR category IN categories %]
-                [% IF field.authorised_value_category == category %]
-                  <option value="[% category %]" selected="selected">[% category %]</option>
-                [% ELSE %]
-                  <option value="[% category %]">[% category %]</option>
-                [% END %]
-              [% END %]
-            </select>
-          </li>
-          <li>
-            <label for="marcfield">MARC field: </label>
-            <input type="text" name="marcfield" id="marcfield" value="[% field.marcfield| html %]" />
-            <span class="hint">Use this syntax: XXX$y</span>
-          </li>
-        </ol>
-      </fieldset>
-      <fieldset class="action">
-        [% IF field %]
-          <input type="hidden" name="field_id" value="[% field.id %]" />
-        [% END %]
-        <input type="hidden" name="op" value="add" />
-        <input type="submit" value="Save" />
-        <a href="/cgi-bin/koha/acqui/add_order_fields.pl" class="cancel">Cancel</a>
-      </fieldset>
-    </form>
-  [% END %]
-
-  </div>
-  </div>
-
-<div class="yui-b">
-[% INCLUDE 'acquisitions-menu.inc' %]
-</div>
-</div>
-[% 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 b8d1c12..d2c759d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
@@ -631,34 +631,9 @@ $(document).ready(function()
                 <label for="sort2">Statistic 2: </label>
                 <input id="sort2" type="text" id="sort2" size="20" name="sort2" value="[% sort2 %]" />
             </li>
-            [% FOREACH af IN additional_fields %]
-              <li>
-                <label for="additional_field_[% af.id %]">[% af.name %]</label>
-                [% 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 %]
-                  <select name="additional_field_[% af.id %]" id="additional_field_[% af.id %]">
-                    <option value=""></option>
-                    [% FOREACH av IN authorised_values %]
-                      [% IF value == av.authorised_value %]
-                        <option value="[% av.authorised_value %]" selected="selected">[% av.lib %]</option>
-                      [% ELSE %]
-                        <option value="[% av.authorised_value %]">[% av.lib %]</option>
-                      [% END %]
-                    [% END %]
-                  </select>
-                [% ELSE %]
-                  <input type="text" name="additional_field_[% af.id %]" id="additional_field_[% af.id %]" value="[% value %]" />
-                [% END %]
-              </li>
+
+            [% IF available_additional_fields %]
+                [% INCLUDE 'additional-fields-entry.inc' available=available_additional_fields values=additional_field_values wrap_fieldset=0 %]
             [% END %]
         </ol>
     </fieldset>
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 5166e4e..735fca5 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
@@ -97,6 +97,7 @@
         [% END %]
         <ul>
             [% WRAPPER table_option value="aqbasket" %]Order baskets[% END %]
+            [% WRAPPER table_option value="aqorders" %]Order lines[% END %]
             [% WRAPPER table_option value="subscription" %]Subscriptions[% END %]
         </ul>
     [% ELSIF op == 'list' %]
-- 
2.8.1