Bug 24857: Add ability to set item group when adding a new item
authorNick Clemens <nick@bywatersolutions.com>
Tue, 8 Mar 2022 12:03:38 +0000 (12:03 +0000)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 8 Jul 2022 19:00:07 +0000 (16:00 -0300)
During cataloging a user may wish to add an item to a group when
creating a new item

This patch also copies the group description to the enumchron field

To test:
1 - Browse to details page for a record
2 - Create or ensure the record has item group(s)
3 - Click New->New item
4 - Note the bottom of the page has a form to attach to existing group, or create new
5 - Note when a group is selected the enumchron field is populated
6 - Confirm item is saved to group when saved

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
cataloguing/additem.pl
koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js

index d1db1d1..7cf5c03 100755 (executable)
@@ -39,6 +39,7 @@ use C4::Barcodes::ValueBuilder;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Items;
 use Koha::ItemTypes;
+use Koha::Items;
 use Koha::Libraries;
 use Koha::Patrons;
 use Koha::SearchEngine::Indexer;
@@ -57,6 +58,34 @@ use List::MoreUtils qw( any uniq );
 
 our $dbh = C4::Context->dbh;
 
+sub add_item_to_item_group {
+    my ( $biblionumber, $itemnumber, $item_group, $item_group_description ) = @_;
+
+    return unless $item_group;
+
+    my $item_group_id;
+    if ( $item_group eq 'create' ) {
+        my $item_group = Koha::Biblio::ItemGroup->new(
+            {
+                biblionumber => $biblionumber,
+                description  => $item_group_description,
+            }
+        )->store();
+
+        $item_group_id = $item_group->id;
+    }
+    else {
+        $item_group_id = $item_group;
+    }
+
+    my $item_group_item = Koha::Biblio::ItemGroup::Item->new(
+        {
+            itemnumber => $itemnumber,
+            item_group_id  => $item_group_id,
+        }
+    )->store();
+}
+
 sub get_item_from_cookie {
     my ( $input ) = @_;
 
@@ -102,6 +131,8 @@ my $fa_barcode            = $input->param('barcode');
 my $fa_branch             = $input->param('branch');
 my $fa_stickyduedate      = $input->param('stickyduedate');
 my $fa_duedatespec        = $input->param('duedatespec');
+my $volume                = $input->param('volume');
+my $volume_description    = $input->param('volume_description');
 
 our $frameworkcode = &GetFrameworkCode($biblionumber);
 
@@ -206,6 +237,7 @@ if ($op eq "additem") {
         }
         else {
             $item->store->discard_changes;
+            add_item_to_item_group( $item->biblionumber, $item->biblioitemnumber, $volume, $volume_description );
 
             # This is a bit tricky : if there is a cookie for the last created item and
             # we just added an item, the cookie value is not correct yet (it will be updated
@@ -316,6 +348,7 @@ if ($op eq "additem") {
                         { skip_record_index => 1 } );
                     $current_item->discard_changes; # Cannot chain discard_changes
                     $current_item = $current_item->unblessed;
+                    add_item_to_item_group( $item->biblionumber, $item->biblioitemnumber, $volume, $volume_description );
 
 # We count the item only if it was really added
 # That way, all items are added, even if there was some already existing barcodes
@@ -606,10 +639,12 @@ if( my $default_location = C4::Context->preference('NewItemsDefaultLocation') )
     $location_field->{marc_value}->{value} ||= $default_location;
 }
 
+my @ig = Koha::Biblio::ItemGroups->search({ biblio_id => $biblionumber })->as_list();
 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
 $template->param(
     biblio       => $biblio,
     items        => \@items,
+    item_groups      => \@ig,
     item_header_loop => \@header_value_loop,
     subfields        => $subfields,
     itemnumber       => $itemnumber,
index aa02320..31eaf8e 100644 (file)
@@ -23,6 +23,9 @@
 [% INCLUDE 'calendar.inc' %]
 [% INCLUDE 'str/cataloging_additem.inc' %]
 [% Asset.js("js/cataloging_additem.js") | $raw %]
+    <script>
+      var has_item_groups = "[% item_groups.size | html %]";
+    </script>
 </head>
 
 <body id="cat_additem" class="cat">
         <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
     [% END %]
 
+[% IF item_groups.size && op != 'saveitem' && CAN_user_editcatalogue_manage_item_groups %]
+    <fieldset class="rows">
+        <legend><i class="fa fa-plus"></i> Add to item group</legend>
+        [% FOREACH ig IN item_groups %]
+            <input type="hidden" id="item-group-[% ig.id | html %]" value="[% ig.description | html %]" />
+        [% END %]
+        <p>
+            <label for="select_item_group">Options: </label>
+            <select name="item_group" id="item-group-add-or-create-form-select">
+                <optgroup label="Use existing item group">
+                    [% FOREACH ig IN item_groups %]
+                        <option value="[% ig.id | html %]">Use: [% ig.description | html %]</option>
+                    [% END %]
+                </optgroup>
+                <optgroup label="Other options">
+                    <option id="item-group-add-or-create-form-no-add" value="">Do not add to item group</option>
+                    <option value="create">Create new item group</option>
+                </optgroup>
+            </select>
+        </p>
+
+        <p id="item-group-add-or-create-form-description-block">
+            <label for="item_group_description" class="required">Name: </label>
+            <input name="item_group_description" id="item-group-add-or-create-form-description" type="text" size="30" class="required" />
+            <span class="required">Required</span>
+        </p>
+    </fieldset>
+[% END %]
+
 <fieldset class="action">    [% IF op != 'saveitem' %]
     <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
     <!-- Note : We use here a false submit button because we have several submit buttons and we don't want the user to believe they validated the adding of multiple items
         </div> <!-- /.col-sm-12 -->
     </div> <!-- /.row -->
 
+[% MACRO jsinclude BLOCK %]
+[% END %]
+
 [% INCLUDE 'intranet-bottom.inc' %]
index 0f9311b..840a9e4 100644 (file)
@@ -69,6 +69,33 @@ $(document).ready(function(){
         multiCopyControl.toggle();
     });
 
+    // Add new item to an item group
+    if ( has_item_groups ) {
+        $('#item-group-add-or-create-form-description-block').hide();
+        $('#item-group-add-or-create-form-no-add').attr('selected', 'selected' );
+
+        $('#item-group-add-or-create-form-select').on('change', function(){
+            if ( $(this).val() == 'create' ) {
+                $('#item-group-add-or-create-form-description')
+                    .addClass('required')
+                    .attr( 'required', 'required' );
+                $('#item-group-add-or-create-form-description-block').show();
+            } else {
+                $('#item-group-add-or-create-form-description')
+                    .removeClass('required')
+                    .removeAttr('required');
+                $('#item-group-add-or-create-form-description-block').hide();
+            }
+        });
+    }
+
+    $('#item-group-add-or-create-form-select').on('change', function() {
+        if ( ! $('input.items-enumchron').val() ) {
+            let item_group_selector = '#item-group-' + $(this).val();
+            let enumchron = $(item_group_selector).val();
+            $('input.items-enumchron').val( enumchron );
+        }
+    });
 });
 
 function Check(f) {