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);

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


  my ($biblionumber, $biblioitemnumber, $itemnumber) 
      = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $unlinked_item_subfields]);

Given a hash containing item column names as keys, create a new Koha item record.

The first two optional parameters ($dbh and $frameworkcode) do not need to be supplied for general use; they exist simply to allow them to be picked up from AddItemFromMarc.

The final optional parameter, $unlinked_item_subfields, contains an arrayref containing subfields present in the original MARC representation of the item (e.g., from the item editor) that are not mapped to items columns directly but should instead be stored in items.more_subfields_xml and included in the biblio items tag for display and indexing.


  ($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.


  ModItemFromMarc($item_marc, $biblionumber, $itemnumber);

This function updates an item record based on a supplied MARC::Record object containing an embedded item field. This API is meant for the use of additem.pl; for other purposes, ModItem should be used.

This function uses the hash %default_values_for_mod_from_marc, which contains default values for item fields to apply when modifying an item. This is needed because if an item field's value is cleared, TransformMarcToKoha does not include the column in the hash that's passed to ModItem, which without use of this hash makes it impossible to clear an item field's value. See bug 2466.

Note that only columns that can be directly changed from the cataloging and serials item editors are included in this hash.

Returns item record


ModItem( { column => $newvalue }, $biblionumber, $itemnumber, { [ unlinked_item_subfields => $unlinked_item_subfields, ] [ log_action => 1, ] } );

Change one or more columns in an item record.

The first argument is a hashref mapping from item column names to the new values. The second and third arguments are the biblionumber and itemnumber, respectively. The fourth, optional parameter (additional_params) may contain the keys unlinked_item_subfields and log_action.

$unlinked_item_subfields contains an arrayref containing subfields present in the original MARC representation of the item (e.g., from the item editor) that are not mapped to items columns directly but should instead be stored in items.more_subfields_xml and included in the biblio items tag for display and indexing.

If one of the changed columns is used to calculate the derived value of a column such as items.cn_sort, this routine will perform the necessary calculation and set the value.

If log_action is set to false, the action will not be logged. If log_action is true or undefined, the action will be logged.


  ModItemTransfer($itenumber, $frombranch, $tobranch);

Marks an item as being transferred from one branch to another.


ModDateLastSeen( $itemnumber, $leave_item_lost );

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


  DelItem({ itemnumber => $itemnumber, [ biblionumber => $biblionumber ] } );

Exported function (core API) for deleting an item record in Koha.


    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.


This hash keeps track of item columns that are strictly derived from other columns in the item record and are not meant to be set independently.

Each key in the hash should be the name of a column (as named by TransformMarcToKoha). Each value should be hashref whose keys are the columns on which the derived column depends. The hashref should also contain a 'BUILDER' key that is a reference to a sub that calculates the derived value.



Given an item hash representing a new item to be added, calculate any derived columns. Currently the only such column is items.cn_sort.



Given an item hash representing a new item to be modified. calculate any derived columns. Currently the only such column is items.cn_sort.

This routine differs from _set_derived_columns_for_add in that it needs to handle partial item records. In other words, the caller of ModItem may have supplied only one or two columns to be changed, so this function needs to determine whether any of the columns to be changed affect any of the derived columns. Also, if a derived column depends on more than one column, but the caller is not changing all of then, this routine retrieves the unchanged values from the database in order to ensure a correct calculation.



Given an item hashref containing one or more columns to modify, fix up certain values. Specifically, set to 0 any passed value of notforloan, damaged, itemlost, or withdrawn that is either undefined or contains the empty string.


  _get_single_item_column($column, $itemnumber);

Retrieves the value of a single column from an items row specified by $itemnumber.


  _calc_items_cn_sort($item, $source_values);

Helper routine to calculate items.cn_sort.



Given an item hash representing an item to be added, set correct default values for columns whose default value is not handled by the DBMS. This includes the following columns:


  my ($itemnumber,$error) = _koha_new_item( $item, $barcode );

Perform the actual insert into the items table.


  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


   ItemSafeToDelete( $biblionumber, $itemnumber);

Exported function (core API) for checking whether an item record is safe to delete.

returns 1 if the item is safe to delete,

"book_on_loan" if the item is checked out,

"not_same_branch" if the item is blocked by independent branches,

"book_reserved" if the there are holds aganst the item, or

"linked_analytics" if the item has linked analytic records.


   DelItemCheck( $biblionumber, $itemnumber);

Exported function (core API) for deleting an item record in Koha if there no current issue.

DelItemCheck wraps ItemSafeToDelete around DelItem.


  my ($itemnumber,$error) =_koha_modify_item( $item );

Perform the actual update of the items row. Note that this routine accepts a hashref specifying the columns to update.


  _koha_delete_item( $itemnum );

Internal function to delete an item record from the koha tables


  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 = SearchItemsByField($field, $value);

SearchItemsByField will search for items on a specific given field. For instance you can search all items with a specific stocknumber like this:

    my $items = SearchItemsByField('stocknumber', $stocknumber);


    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


Helper method that takes care of calling all plugin hooks