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.

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.

Consequently, all code that creates, modifies, or deletes item records must use an appropriate function from C4::Items. If no existing function is suitable, it is better to add one to C4::Items than to use add one-off SQL statements to add or modify items.

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


$item = GetItem($itemnumber,$barcode,$serial);

Return item information, for a given itemnumber or barcode. The return value is a hashref mapping item column names to values. If $serial is true, include serial publication data.


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 biblioitems.marc and biblioitems.marcxml are 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 beccause 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.


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

Change one or more columns in an item record and update the MARC representation of the item.

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


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

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



Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking. $itemnum is the item number


DelItem($biblionumber, $itemnumber);

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.

Some of the functions in this group are candidates for refactoring -- for example, some of the code in GetItemsByBiblioitemnumber and GetItemsInfo has copy-and-paste work.


$itemstatushash = GetItemStatus($fwkcode);

Returns a list of valid values for the items.notforloan field.

NOTE: does not return an individual item's status.

Can be MARC dependant. fwkcode is optional. But basically could be can be loan or not Create a status selector with the following code


my $itemstatushash = getitemstatus; my @itemstatusloop; foreach my $thisstatus (keys %$itemstatushash) { my %row =(value => $thisstatus, statusname => $itemstatushash->{$thisstatus}->{'statusname'}, ); push @itemstatusloop, \%row; } $template->param(statusloop=>\@itemstatusloop);


<select name="statusloop"> <option value="">Default</option> <!-- TMPL_LOOP name="statusloop" --> <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option> <!-- /TMPL_LOOP --> </select>


$itemlochash = GetItemLocation($fwk);

Returns a list of valid values for the items.location field.

NOTE: does not return an individual item's location.

where fwk stands for an optional framework code. Create a location selector with the following code


my $itemlochash = getitemlocation; my @itemlocloop; foreach my $thisloc (keys %$itemlochash) { my $selected = 1 if $thisbranch eq $branch; my %row =(locval => $thisloc, selected => $selected, locname => $itemlochash->{$thisloc}, ); push @itemlocloop, \%row; } $template->param(itemlocationloop => \@itemlocloop);


<select name="location"> <option value="">Default</option> <!-- TMPL_LOOP name="itemlocationloop" --> <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option> <!-- /TMPL_LOOP --> </select>


$items = GetLostItems( $where, $orderby );

This function gets a list of lost items.


$where is a hashref. it containts a field of the items table as key and the value to match as value. For example:

{ barcode => 'abc123', homebranch => 'CPL', }

$orderby is a field of the items table by which the resultset should be orderd.


$items is a reference to an array full of hashrefs with columns from the "items" table as keys.

usage in the perl script:

my $where = { barcode => '0001548' }; my $items = GetLostItems( $where, "homebranch" ); $template->param( itemsloop => $items );


$itemlist = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype $datelastseen, $branch, $offset, $size);

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)


This function return count of item with $biblionumber





Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP Called by C4::XISBN


@results = GetItemsInfo($biblionumber, $type);

Returns information about books with the given biblionumber.

$type may be either intra or anything else. If it is not set to intra, then the search will exclude lost, very overdue, and withdrawn items.

GetItemsInfo returns a list of references-to-hash. Each element contains a number of keys. Most of them are table items 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 @itemnumbers_of = get_itemnumbers_of(@biblionumbers);

Given a list of biblionumbers, return the list of corresponding itemnumbers for each biblionumber.

Return a reference on a hash where keys are biblionumbers and values are references on array of itemnumbers.


$result = GetItemnumberFromBarcode($barcode);


  find the types and values for all authorised values assigned to this item.


  returns: a hashref malling the authorised value to the value set for this itemnumber

    $authorised_values = {
             'CCODE'      => undef,
             'DAMAGED'    => '0',
             'LOC'        => '3',
             'LOST'       => '0'
             'NOT_LOAN'   => '0',
             'RESTRICTED' => undef,
             'STACK'      => undef,
             'WITHDRAWN'  => '0',
             'branches'   => 'CPL',
             'cn_source'  => undef,
             'itemtypes'  => 'SER',

   Notes: see C4::Biblio::get_biblio_authorised_values for a similar method at the biblio level.


  find a list of icons that are appropriate for display based on the
  authorised values for a biblio.

  parameters: listref of authorised values, such as comes from
    get_item_ahtorised_values or
    from C4::Biblio::get_biblio_authorised_values

  returns: listref of hashrefs for each image. Each hashref looks like

      { imageurl => '/intranet-tmpl/prog/img/itemtypeimg/npl/WEB.gif',
        label    => '',
        category => '',
        value    => '', }

  Notes: Currently, I put on the full path to the images on the staff
  side. This should either be configurable or not done at all. Since I
  have to deal with 'intranet' or 'opac' in
  get_biblio_authorised_values, perhaps I should be passing it in.


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 wthdrawn 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.


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( $dbh, $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_item_field_to_biblio($item_marc, $biblionumber, $frameworkcode);

Adds the fields from a MARC record containing the representation of a Koha item record to the MARC biblio record. The input $item_marc record is expect to contain just one field, the embedded item information field.


&_replace_item_field_in_biblio($item_marc, $biblionumber, $itemnumber, $frameworkcode)

Given a MARC::Record $item_marc containing one tag with the MARC representation of the item, examine the biblio MARC for the corresponding tag for that item and replace it with the tag from $item_marc.


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'}):