C4::Items - item management functions


This module contains an API for manipulating item records in Koha, and is used by cataloguing, circulation, acquisitions, and serials management.

# FIXME This POD is not up-to-date A Koha item record is stored in two places: the items table and embedded in a MARC tag in the XML version of the associated bib record in biblioitems.marcxml. This is done to allow the item information to be readily indexed (e.g., by Zebra), but means that each item modification transaction must keep the items table and the MARC XML in sync at all times.

The items table will be considered authoritative. In other words, if there is ever a discrepancy between the items table and the MARC XML, the items table should be considered accurate.


Most of the functions in C4::Items were originally in the C4::Biblio module.


The following functions are meant for use by users of C4::Items



Set the current shelving location of the item record to its stored permanent shelving location. This is primarily used to indicate when an item whose current location is a special processing ('PROC') or shelving cart ('CART') location is back in the stacks.


  my ($biblionumber, $biblioitemnumber, $itemnumber) 
      = AddItemFromMarc($source_item_marc, $biblionumber[, $params]);

Given a MARC::Record object containing an embedded item record and a biblionumber, create a new item record.

The final optional parameter, $params, expected to contain 'skip_record_index' key, which relayed down to Koha::Item/store, there it prevents calling of index_records, which takes most of the time in batch adds/deletes: index_records to be called later in additem.pl after the whole loop.

$params: skip_record_index => 1|0


  ($itemnumber_ref, $error_ref) = AddItemBatchFromMarc($record, 
             $biblionumber, $biblioitemnumber, $frameworkcode);

Efficiently create item records from a MARC biblio record with embedded item fields. This routine is suitable for batch jobs.

This API assumes that the bib record has already been saved to the biblio and biblioitems tables. It does not expect that biblio_metadata.metadata is populated, but it will do so via a call to ModBibiloMarc.

The goal of this API is to have a similar effect to using AddBiblio and AddItems in succession, but without inefficient repeated parsing of the MARC XML bib record.

This function returns an arrayref of new itemsnumbers and an arrayref of item errors encountered during the processing. Each entry in the errors list is a hashref containing the following keys:


Sequence number of original item tag in the MARC record.


Item barcode, provide to assist in the construction of useful error messages.


Code representing the error condition. Can be 'duplicate_barcode', 'invalid_homebranch', or 'invalid_holdingbranch'.


Additional information appropriate to the error condition.


my $item = ModItemFromMarc($item_marc, $biblionumber, $itemnumber[, $params]);

The final optional parameter, $params, expected to contain 'skip_record_index' key, which relayed down to Koha::Item/store, there it prevents calling of index_records, which takes most of the time in batch adds/deletes: index_records better to be called later in additem.pl after the whole loop.

$params: skip_record_index => 1|0


  ModItemTransfer($itemnumber, $frombranch, $tobranch, $trigger, [$params]);

Marks an item as being transferred from one branch to another and records the trigger.

The last optional parameter allows for passing skip_record_index through to the items store call.


ModDateLastSeen( $itemnumber, $leave_item_lost, $params );

Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking. $itemnumber is the item number $leave_item_lost determines if a lost item will be found or remain lost

The last optional parameter allows for passing skip_record_index through to the items store call.


    my $item_ref = TransformMarcToKoha($marc, 'items');
    # do stuff
    my %errors = CheckItemPreSave($item_ref);
    if (exists $errors{'duplicate_barcode'}) {
        print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
    } elsif (exists $errors{'invalid_homebranch'}) {
        print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
    } elsif (exists $errors{'invalid_holdingbranch'}) {
        print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
    } else {
        print "item is OK";

Given a hashref containing item fields, determine if it can be inserted or updated in the database. Specifically, checks for database integrity issues, and returns a hash containing any of the following keys, if applicable.


Barcode, if it duplicates one already found in the database.


Home branch, if not defined in branches table.


Holding branch, if not defined in branches table.

This function does NOT implement any policy-related checks, e.g., whether current operator is allowed to save an item that has a given branch code.


The following functions provide various ways of getting an item record, a set of item records, or lists of authorized values for certain item fields.


($itemlist, $iTotalRecords) = GetItemsForInventory( { minlocation => $minlocation, maxlocation => $maxlocation, location => $location, itemtype => $itemtype, ignoreissued => $ignoreissued, datelastseen => $datelastseen, branchcode => $branchcode, branch => $branch, offset => $offset, size => $size, statushash => $statushash, } );

Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.

The sub returns a reference to a list of hashes, each containing itemnumber, author, title, barcode, item callnumber, and date last seen. It is ordered by callnumber then title.

The required minlocation & maxlocation parameters are used to specify a range of item callnumbers the datelastseen can be used to specify that you want to see items not seen since a past date only. offset & size can be used to retrieve only a part of the whole listing (defaut behaviour) $statushash requires a hashref that has the authorized values fieldname (intems.notforloan, etc...) as keys, and an arrayref of statuscodes we are searching for as values.

$iTotalRecords is the number of rows that would have been returned without the $offset, $size limit clause


  @results = GetItemsInfo($biblionumber);

Returns information about items with the given biblionumber.

GetItemsInfo returns a list of references-to-hash. Each element contains a number of keys. Most of them are attributes from the biblio, biblioitems, items, and itemtypes tables in the Koha database. Other keys include:


The name (not the code) of the branch to which the book belongs.


This is simply items.datelastseen, except that while the date is stored in YYYY-MM-DD format in the database, here it is converted to DD/MM/YYYY format. A NULL date is returned as //.


This is the concatenation of biblioitems.classification, the book's Dewey code, and biblioitems.subclass.


I think this is the number of copies of the book available.


If this is set, it is set to One Order.


  my @itemlocinfo = GetItemsLocationInfo($biblionumber);

Returns the branch names, shelving location and itemcallnumber for each item attached to the biblio in question

GetItemsInfo returns a list of references-to-hash. Data returned:


Branch Name of the item's homebranch


Branch Name of the item's holdingbranch


Item's shelving location code


The intranet description for the Shelving Location as set in authorised_values 'LOC'


The OPAC description for the Shelving Location as set in authorised_values 'LOC'. Falls back to intranet description if no OPAC description is set.


Item's itemcallnumber


Item's call number normalized for sorting


    $hostiteminfo = GetHostItemsInfo($hostfield);
    Returns the iteminfo for items linked to records via a host field


  my @itemnumbers_of = get_hostitemnumbers_of($biblionumber);

Given a biblionumber, return the list of corresponding itemnumbers that are linked to it via host fields

Return a reference on a hash where key is a biblionumber and values are references on array of itemnumbers.


    my @itemnumbers_to_hide = GetHiddenItemnumbers({ items => \@items, borcat => $category });

Given a list of items it checks which should be hidden from the OPAC given the current configuration. Returns a list of itemnumbers corresponding to those that should be hidden. Optionally takes a borcat parameter for certain borrower types to be excluded


The following functions, while part of the public API, are not exported. This is generally because they are meant to be used by only one script for a specific purpose, and should not be used in any other context without careful thought.


  my $item_marc = GetMarcItem($biblionumber, $itemnumber);

Returns MARC::Record of the item passed in parameter. This function is meant for use only in cataloguing/additem.pl, where it is needed to support that script's MARC-like editor.


The following functions are not meant to be called directly, but are documented in order to explain the inner workings of C4::Items.


  MoveItemFromBiblio($itenumber, $frombiblio, $tobiblio);

Moves an item from a biblio to another

Returns undef if the move failed or the biblionumber of the destination record otherwise


  my $item_marc = _marc_from_item_hash($item, $frameworkcode[, $unlinked_item_subfields]);

Given an item hash representing a complete item record, create a MARC::Record object containing an embedded tag representing that item.

The third, optional parameter $unlinked_item_subfields is an arrayref of subfields (not mapped to items fields per the framework) to be added to the MARC representation of the item.


Add an error message hash generated by CheckItemPreSave to a list of errors.


  my $unlinked_item_subfields = _get_unlinked_item_subfields($original_item_marc, $frameworkcode);


  my $unlinked_subfields_xml = _get_unlinked_subfields_xml($unlinked_item_subfields);


  my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'}):


  $count= &GetAnalyticsCount($itemnumber)

counts Usage of itemnumber in Analytical bibliorecords.


    my ($items, $total) = SearchItems($filter, $params);

Perform a search among items

$filter is a reference to a hash which can be a filter, or a combination of filters.

A filter has the following keys:

A combination of filters hash the following keys:

$params is a reference to a hash that can contain the following parameters:



  ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);

Find the given $subfield in the given $tag in the given MARC::Record $record. If the subfield is found, returns the (indicators, value) pair; otherwise, (undef, undef) is returned.

PROPOSITION : Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities. I suggest we export it from this module.



Returns a hash with all the fields for Display a given item data in a template

The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided