3 # Copyright 2007 LibLime, Inc.
4 # Parts Copyright Biblibre 2010
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
28 use C4
::Dates qw
/format_date format_date_in_iso/;
32 use List
::MoreUtils qw
/any/;
34 use DateTime
::Format
::MySQL
;
35 use Data
::Dumper
; # used as part of logging item record changes, not just for
36 # debugging; so please don't remove this
37 use Koha
::DateUtils qw
/dt_from_string/;
39 use vars
qw($VERSION @ISA @EXPORT);
42 $VERSION = 3.07.00.049;
45 @ISA = qw( Exporter );
68 GetItemsByBiblioitemnumber
72 GetItemnumbersForBiblio
74 get_hostitemnumbers_of
75 GetItemnumberFromBarcode
76 GetBarcodeFromItemnumber
90 PrepareItemrecordDisplay
97 C4::Items - item management functions
101 This module contains an API for manipulating item
102 records in Koha, and is used by cataloguing, circulation,
103 acquisitions, and serials management.
105 A Koha item record is stored in two places: the
106 items table and embedded in a MARC tag in the XML
107 version of the associated bib record in C<biblioitems.marcxml>.
108 This is done to allow the item information to be readily
109 indexed (e.g., by Zebra), but means that each item
110 modification transaction must keep the items table
111 and the MARC XML in sync at all times.
113 Consequently, all code that creates, modifies, or deletes
114 item records B<must> use an appropriate function from
115 C<C4::Items>. If no existing function is suitable, it is
116 better to add one to C<C4::Items> than to use add
117 one-off SQL statements to add or modify items.
119 The items table will be considered authoritative. In other
120 words, if there is ever a discrepancy between the items
121 table and the MARC XML, the items table should be considered
124 =head1 HISTORICAL NOTE
126 Most of the functions in C<C4::Items> were originally in
127 the C<C4::Biblio> module.
129 =head1 CORE EXPORTED FUNCTIONS
131 The following functions are meant for use by users
138 $item = GetItem($itemnumber,$barcode,$serial);
140 Return item information, for a given itemnumber or barcode.
141 The return value is a hashref mapping item column
142 names to values. If C<$serial> is true, include serial publication data.
147 my ($itemnumber,$barcode, $serial) = @_;
148 my $dbh = C4
::Context
->dbh;
152 my $sth = $dbh->prepare("
154 WHERE itemnumber = ?");
155 $sth->execute($itemnumber);
156 $data = $sth->fetchrow_hashref;
158 my $sth = $dbh->prepare("
162 $sth->execute($barcode);
163 $data = $sth->fetchrow_hashref;
166 return unless ( $data );
169 my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=?");
170 $ssth->execute($data->{'itemnumber'}) ;
171 ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
173 #if we don't have an items.itype, use biblioitems.itemtype.
174 if( ! $data->{'itype'} ) {
175 my $sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
176 $sth->execute($data->{'biblionumber'});
177 ($data->{'itype'}) = $sth->fetchrow_array;
184 CartToShelf($itemnumber);
186 Set the current shelving location of the item record
187 to its stored permanent shelving location. This is
188 primarily used to indicate when an item whose current
189 location is a special processing ('PROC') or shelving cart
190 ('CART') location is back in the stacks.
195 my ( $itemnumber ) = @_;
197 unless ( $itemnumber ) {
198 croak
"FAILED CartToShelf() - no itemnumber supplied";
201 my $item = GetItem
($itemnumber);
202 if ( $item->{location
} eq 'CART' ) {
203 $item->{location
} = $item->{permanent_location
};
204 ModItem
($item, undef, $itemnumber);
210 ShelfToCart($itemnumber);
212 Set the current shelving location of the item
213 to shelving cart ('CART').
218 my ( $itemnumber ) = @_;
220 unless ( $itemnumber ) {
221 croak
"FAILED ShelfToCart() - no itemnumber supplied";
224 my $item = GetItem
($itemnumber);
225 $item->{'location'} = 'CART';
226 ModItem
($item, undef, $itemnumber);
229 =head2 AddItemFromMarc
231 my ($biblionumber, $biblioitemnumber, $itemnumber)
232 = AddItemFromMarc($source_item_marc, $biblionumber);
234 Given a MARC::Record object containing an embedded item
235 record and a biblionumber, create a new item record.
239 sub AddItemFromMarc
{
240 my ( $source_item_marc, $biblionumber ) = @_;
241 my $dbh = C4
::Context
->dbh;
243 # parse item hash from MARC
244 my $frameworkcode = GetFrameworkCode
( $biblionumber );
245 my ($itemtag,$itemsubfield)=GetMarcFromKohaField
("items.itemnumber",$frameworkcode);
247 my $localitemmarc=MARC
::Record
->new;
248 $localitemmarc->append_fields($source_item_marc->field($itemtag));
249 my $item = &TransformMarcToKoha
( $dbh, $localitemmarc, $frameworkcode ,'items');
250 my $unlinked_item_subfields = _get_unlinked_item_subfields
($localitemmarc, $frameworkcode);
251 return AddItem
($item, $biblionumber, $dbh, $frameworkcode, $unlinked_item_subfields);
256 my ($biblionumber, $biblioitemnumber, $itemnumber)
257 = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $unlinked_item_subfields]);
259 Given a hash containing item column names as keys,
260 create a new Koha item record.
262 The first two optional parameters (C<$dbh> and C<$frameworkcode>)
263 do not need to be supplied for general use; they exist
264 simply to allow them to be picked up from AddItemFromMarc.
266 The final optional parameter, C<$unlinked_item_subfields>, contains
267 an arrayref containing subfields present in the original MARC
268 representation of the item (e.g., from the item editor) that are
269 not mapped to C<items> columns directly but should instead
270 be stored in C<items.more_subfields_xml> and included in
271 the biblio items tag for display and indexing.
277 my $biblionumber = shift;
279 my $dbh = @_ ?
shift : C4
::Context
->dbh;
280 my $frameworkcode = @_ ?
shift : GetFrameworkCode
( $biblionumber );
281 my $unlinked_item_subfields;
283 $unlinked_item_subfields = shift
286 # needs old biblionumber and biblioitemnumber
287 $item->{'biblionumber'} = $biblionumber;
288 my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
289 $sth->execute( $item->{'biblionumber'} );
290 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
292 _set_defaults_for_add
($item);
293 _set_derived_columns_for_add
($item);
294 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml
($unlinked_item_subfields);
295 # FIXME - checks here
296 unless ( $item->{itype
} ) { # default to biblioitem.itemtype if no itype
297 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
298 $itype_sth->execute( $item->{'biblionumber'} );
299 ( $item->{'itype'} ) = $itype_sth->fetchrow_array;
302 my ( $itemnumber, $error ) = _koha_new_item
( $item, $item->{barcode
} );
303 $item->{'itemnumber'} = $itemnumber;
305 ModZebra
( $item->{biblionumber
}, "specialUpdate", "biblioserver" );
307 logaction
("CATALOGUING", "ADD", $itemnumber, "item") if C4
::Context
->preference("CataloguingLog");
309 return ($item->{biblionumber
}, $item->{biblioitemnumber
}, $itemnumber);
312 =head2 AddItemBatchFromMarc
314 ($itemnumber_ref, $error_ref) = AddItemBatchFromMarc($record,
315 $biblionumber, $biblioitemnumber, $frameworkcode);
317 Efficiently create item records from a MARC biblio record with
318 embedded item fields. This routine is suitable for batch jobs.
320 This API assumes that the bib record has already been
321 saved to the C<biblio> and C<biblioitems> tables. It does
322 not expect that C<biblioitems.marc> and C<biblioitems.marcxml>
323 are populated, but it will do so via a call to ModBibiloMarc.
325 The goal of this API is to have a similar effect to using AddBiblio
326 and AddItems in succession, but without inefficient repeated
327 parsing of the MARC XML bib record.
329 This function returns an arrayref of new itemsnumbers and an arrayref of item
330 errors encountered during the processing. Each entry in the errors
331 list is a hashref containing the following keys:
337 Sequence number of original item tag in the MARC record.
341 Item barcode, provide to assist in the construction of
342 useful error messages.
346 Code representing the error condition. Can be 'duplicate_barcode',
347 'invalid_homebranch', or 'invalid_holdingbranch'.
349 =item error_information
351 Additional information appropriate to the error condition.
357 sub AddItemBatchFromMarc
{
358 my ($record, $biblionumber, $biblioitemnumber, $frameworkcode) = @_;
360 my @itemnumbers = ();
362 my $dbh = C4
::Context
->dbh;
364 # We modify the record, so lets work on a clone so we don't change the
366 $record = $record->clone();
367 # loop through the item tags and start creating items
368 my @bad_item_fields = ();
369 my ($itemtag, $itemsubfield) = &GetMarcFromKohaField
("items.itemnumber",'');
370 my $item_sequence_num = 0;
371 ITEMFIELD
: foreach my $item_field ($record->field($itemtag)) {
372 $item_sequence_num++;
373 # we take the item field and stick it into a new
374 # MARC record -- this is required so far because (FIXME)
375 # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
376 # and there is no TransformMarcFieldToKoha
377 my $temp_item_marc = MARC
::Record
->new();
378 $temp_item_marc->append_fields($item_field);
380 # add biblionumber and biblioitemnumber
381 my $item = TransformMarcToKoha
( $dbh, $temp_item_marc, $frameworkcode, 'items' );
382 my $unlinked_item_subfields = _get_unlinked_item_subfields
($temp_item_marc, $frameworkcode);
383 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml
($unlinked_item_subfields);
384 $item->{'biblionumber'} = $biblionumber;
385 $item->{'biblioitemnumber'} = $biblioitemnumber;
387 # check for duplicate barcode
388 my %item_errors = CheckItemPreSave
($item);
390 push @errors, _repack_item_errors
($item_sequence_num, $item, \
%item_errors);
391 push @bad_item_fields, $item_field;
395 _set_defaults_for_add
($item);
396 _set_derived_columns_for_add
($item);
397 my ( $itemnumber, $error ) = _koha_new_item
( $item, $item->{barcode
} );
398 warn $error if $error;
399 push @itemnumbers, $itemnumber; # FIXME not checking error
400 $item->{'itemnumber'} = $itemnumber;
402 logaction
("CATALOGUING", "ADD", $itemnumber, "item") if C4
::Context
->preference("CataloguingLog");
404 my $new_item_marc = _marc_from_item_hash
($item, $frameworkcode, $unlinked_item_subfields);
405 $item_field->replace_with($new_item_marc->field($itemtag));
408 # remove any MARC item fields for rejected items
409 foreach my $item_field (@bad_item_fields) {
410 $record->delete_field($item_field);
413 # update the MARC biblio
414 # $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
416 return (\
@itemnumbers, \
@errors);
419 =head2 ModItemFromMarc
421 ModItemFromMarc($item_marc, $biblionumber, $itemnumber);
423 This function updates an item record based on a supplied
424 C<MARC::Record> object containing an embedded item field.
425 This API is meant for the use of C<additem.pl>; for
426 other purposes, C<ModItem> should be used.
428 This function uses the hash %default_values_for_mod_from_marc,
429 which contains default values for item fields to
430 apply when modifying an item. This is needed beccause
431 if an item field's value is cleared, TransformMarcToKoha
432 does not include the column in the
433 hash that's passed to ModItem, which without
434 use of this hash makes it impossible to clear
435 an item field's value. See bug 2466.
437 Note that only columns that can be directly
438 changed from the cataloging and serials
439 item editors are included in this hash.
445 our %default_values_for_mod_from_marc;
447 sub _build_default_values_for_mod_marc
{
448 my ($frameworkcode) = @_;
449 return $default_values_for_mod_from_marc{$frameworkcode}
450 if exists $default_values_for_mod_from_marc{$frameworkcode};
451 my $marc_structure = C4
::Biblio
::GetMarcStructure
( 1, $frameworkcode );
452 my $default_values = {
454 booksellerid
=> undef,
456 'items.cn_source' => undef,
457 coded_location_qualifier
=> undef,
461 holdingbranch
=> undef,
463 itemcallnumber
=> undef,
468 permanent_location
=> undef,
471 # paidfor => undef, # commented, see bug 12817
473 replacementprice
=> undef,
474 replacementpricedate
=> undef,
477 stocknumber
=> undef,
481 while ( my ( $field, $default_value ) = each %$default_values ) {
482 $field =~ s
|[^\
.]*\
.?
(.*)|items
.$1|;
483 $default_values_for_mod_from_marc{$frameworkcode}{$field} =
485 if C4
::Koha
::IsKohaFieldLinked
(
486 { kohafield
=> $field, frameworkcode
=> $frameworkcode } );
488 return $default_values_for_mod_from_marc{$frameworkcode};
491 sub ModItemFromMarc
{
492 my $item_marc = shift;
493 my $biblionumber = shift;
494 my $itemnumber = shift;
496 my $dbh = C4
::Context
->dbh;
497 my $frameworkcode = GetFrameworkCode
($biblionumber);
498 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField
( "items.itemnumber", $frameworkcode );
500 my $localitemmarc = MARC
::Record
->new;
501 $localitemmarc->append_fields( $item_marc->field($itemtag) );
502 my $item = &TransformMarcToKoha
( $dbh, $localitemmarc, $frameworkcode, 'items' );
503 my $default_values = _build_default_values_for_mod_marc
();
504 foreach my $item_field ( keys %$default_values ) {
505 $item->{$item_field} = $default_values->{$item_field}
506 unless exists $item->{$item_field};
508 my $unlinked_item_subfields = _get_unlinked_item_subfields
( $localitemmarc, $frameworkcode );
510 ModItem
($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields);
516 ModItem({ column => $newvalue }, $biblionumber, $itemnumber);
518 Change one or more columns in an item record and update
519 the MARC representation of the item.
521 The first argument is a hashref mapping from item column
522 names to the new values. The second and third arguments
523 are the biblionumber and itemnumber, respectively.
525 The fourth, optional parameter, C<$unlinked_item_subfields>, contains
526 an arrayref containing subfields present in the original MARC
527 representation of the item (e.g., from the item editor) that are
528 not mapped to C<items> columns directly but should instead
529 be stored in C<items.more_subfields_xml> and included in
530 the biblio items tag for display and indexing.
532 If one of the changed columns is used to calculate
533 the derived value of a column such as C<items.cn_sort>,
534 this routine will perform the necessary calculation
541 my $biblionumber = shift;
542 my $itemnumber = shift;
544 # if $biblionumber is undefined, get it from the current item
545 unless (defined $biblionumber) {
546 $biblionumber = _get_single_item_column
('biblionumber', $itemnumber);
549 my $dbh = @_ ?
shift : C4
::Context
->dbh;
550 my $frameworkcode = @_ ?
shift : GetFrameworkCode
( $biblionumber );
552 my $unlinked_item_subfields;
554 $unlinked_item_subfields = shift;
555 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml
($unlinked_item_subfields);
558 $item->{'itemnumber'} = $itemnumber or return;
560 my @fields = qw( itemlost withdrawn );
562 # Only call GetItem if we need to set an "on" date field
563 if ( $item->{itemlost
} || $item->{withdrawn
} ) {
564 my $pre_mod_item = GetItem
( $item->{'itemnumber'} );
565 for my $field (@fields) {
566 if ( defined( $item->{$field} )
567 and not $pre_mod_item->{$field}
568 and $item->{$field} )
570 $item->{ $field . '_on' } =
571 DateTime
::Format
::MySQL
->format_datetime( dt_from_string
() );
576 # If the field is defined but empty, we are removing and,
577 # and thus need to clear out the 'on' field as well
578 for my $field (@fields) {
579 if ( defined( $item->{$field} ) && !$item->{$field} ) {
580 $item->{ $field . '_on' } = undef;
585 _set_derived_columns_for_mod
($item);
586 _do_column_fixes_for_mod
($item);
589 # attempt to change itemnumber
590 # attempt to change biblionumber (if we want
591 # an API to relink an item to a different bib,
592 # it should be a separate function)
595 _koha_modify_item
($item);
597 # request that bib be reindexed so that searching on current
598 # item status is possible
599 ModZebra
( $biblionumber, "specialUpdate", "biblioserver" );
601 logaction
("CATALOGUING", "MODIFY", $itemnumber, "item ".Dumper
($item)) if C4
::Context
->preference("CataloguingLog");
604 =head2 ModItemTransfer
606 ModItemTransfer($itenumber, $frombranch, $tobranch);
608 Marks an item as being transferred from one branch
613 sub ModItemTransfer
{
614 my ( $itemnumber, $frombranch, $tobranch ) = @_;
616 my $dbh = C4
::Context
->dbh;
618 # Remove the 'shelving cart' location status if it is being used.
619 CartToShelf
( $itemnumber ) if ( C4
::Context
->preference("ReturnToShelvingCart") );
621 #new entry in branchtransfers....
622 my $sth = $dbh->prepare(
623 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
624 VALUES (?, ?, NOW(), ?)");
625 $sth->execute($itemnumber, $frombranch, $tobranch);
627 ModItem
({ holdingbranch
=> $tobranch }, undef, $itemnumber);
628 ModDateLastSeen
($itemnumber);
632 =head2 ModDateLastSeen
634 ModDateLastSeen($itemnum);
636 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking.
637 C<$itemnum> is the item number
641 sub ModDateLastSeen
{
642 my ($itemnumber) = @_;
644 my $today = C4
::Dates
->new();
645 ModItem
({ itemlost
=> 0, datelastseen
=> $today->output("iso") }, undef, $itemnumber);
650 DelItem({ itemnumber => $itemnumber, [ biblionumber => $biblionumber ] } );
652 Exported function (core API) for deleting an item record in Koha.
659 my $itemnumber = $params->{itemnumber
};
660 my $biblionumber = $params->{biblionumber
};
662 unless ($biblionumber) {
663 $biblionumber = C4
::Biblio
::GetBiblionumberFromItemnumber
($itemnumber);
666 # If there is no biblionumber for the given itemnumber, there is nothing to delete
667 return 0 unless $biblionumber;
669 # FIXME check the item has no current issues
670 my $deleted = _koha_delete_item
( $itemnumber );
672 # get the MARC record
673 my $record = GetMarcBiblio
($biblionumber);
674 ModZebra
( $biblionumber, "specialUpdate", "biblioserver" );
676 #search item field code
677 logaction
("CATALOGUING", "DELETE", $itemnumber, "item") if C4
::Context
->preference("CataloguingLog");
681 =head2 CheckItemPreSave
683 my $item_ref = TransformMarcToKoha($marc, 'items');
685 my %errors = CheckItemPreSave($item_ref);
686 if (exists $errors{'duplicate_barcode'}) {
687 print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
688 } elsif (exists $errors{'invalid_homebranch'}) {
689 print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
690 } elsif (exists $errors{'invalid_holdingbranch'}) {
691 print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
696 Given a hashref containing item fields, determine if it can be
697 inserted or updated in the database. Specifically, checks for
698 database integrity issues, and returns a hash containing any
699 of the following keys, if applicable.
703 =item duplicate_barcode
705 Barcode, if it duplicates one already found in the database.
707 =item invalid_homebranch
709 Home branch, if not defined in branches table.
711 =item invalid_holdingbranch
713 Holding branch, if not defined in branches table.
717 This function does NOT implement any policy-related checks,
718 e.g., whether current operator is allowed to save an
719 item that has a given branch code.
723 sub CheckItemPreSave
{
724 my $item_ref = shift;
729 # check for duplicate barcode
730 if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
731 my $existing_itemnumber = GetItemnumberFromBarcode
($item_ref->{'barcode'});
732 if ($existing_itemnumber) {
733 if (!exists $item_ref->{'itemnumber'} # new item
734 or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
735 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
740 # check for valid home branch
741 if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
742 my $branch_name = C4
::Branch
::GetBranchName
($item_ref->{'homebranch'});
743 unless (defined $branch_name) {
744 # relies on fact that branches.branchname is a non-NULL column,
745 # so GetBranchName returns undef only if branch does not exist
746 $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
750 # check for valid holding branch
751 if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
752 my $branch_name = C4
::Branch
::GetBranchName
($item_ref->{'holdingbranch'});
753 unless (defined $branch_name) {
754 # relies on fact that branches.branchname is a non-NULL column,
755 # so GetBranchName returns undef only if branch does not exist
756 $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
764 =head1 EXPORTED SPECIAL ACCESSOR FUNCTIONS
766 The following functions provide various ways of
767 getting an item record, a set of item records, or
768 lists of authorized values for certain item fields.
770 Some of the functions in this group are candidates
771 for refactoring -- for example, some of the code
772 in C<GetItemsByBiblioitemnumber> and C<GetItemsInfo>
773 has copy-and-paste work.
779 $itemstatushash = GetItemStatus($fwkcode);
781 Returns a list of valid values for the
782 C<items.notforloan> field.
784 NOTE: does B<not> return an individual item's
787 Can be MARC dependant.
789 But basically could be can be loan or not
790 Create a status selector with the following code
792 =head3 in PERL SCRIPT
794 my $itemstatushash = getitemstatus;
796 foreach my $thisstatus (keys %$itemstatushash) {
797 my %row =(value => $thisstatus,
798 statusname => $itemstatushash->{$thisstatus}->{'statusname'},
800 push @itemstatusloop, \%row;
802 $template->param(statusloop=>\@itemstatusloop);
806 <select name="statusloop" id="statusloop">
807 <option value="">Default</option>
808 [% FOREACH statusloo IN statusloop %]
809 [% IF ( statusloo.selected ) %]
810 <option value="[% statusloo.value %]" selected="selected">[% statusloo.statusname %]</option>
812 <option value="[% statusloo.value %]">[% statusloo.statusname %]</option>
821 # returns a reference to a hash of references to status...
824 my $dbh = C4
::Context
->dbh;
826 $fwk = '' unless ($fwk);
827 my ( $tag, $subfield ) =
828 GetMarcFromKohaField
( "items.notforloan", $fwk );
829 if ( $tag and $subfield ) {
832 "SELECT authorised_value
833 FROM marc_subfield_structure
839 $sth->execute( $tag, $subfield, $fwk );
840 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
843 "SELECT authorised_value,lib
844 FROM authorised_values
849 $authvalsth->execute($authorisedvaluecat);
850 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
851 $itemstatus{$authorisedvalue} = $lib;
865 $itemstatus{"1"} = "Not For Loan";
869 =head2 GetItemLocation
871 $itemlochash = GetItemLocation($fwk);
873 Returns a list of valid values for the
874 C<items.location> field.
876 NOTE: does B<not> return an individual item's
879 where fwk stands for an optional framework code.
880 Create a location selector with the following code
882 =head3 in PERL SCRIPT
884 my $itemlochash = getitemlocation;
886 foreach my $thisloc (keys %$itemlochash) {
887 my $selected = 1 if $thisbranch eq $branch;
888 my %row =(locval => $thisloc,
889 selected => $selected,
890 locname => $itemlochash->{$thisloc},
892 push @itemlocloop, \%row;
894 $template->param(itemlocationloop => \@itemlocloop);
898 <select name="location">
899 <option value="">Default</option>
900 <!-- TMPL_LOOP name="itemlocationloop" -->
901 <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
907 sub GetItemLocation
{
909 # returns a reference to a hash of references to location...
912 my $dbh = C4
::Context
->dbh;
914 $fwk = '' unless ($fwk);
915 my ( $tag, $subfield ) =
916 GetMarcFromKohaField
( "items.location", $fwk );
917 if ( $tag and $subfield ) {
920 "SELECT authorised_value
921 FROM marc_subfield_structure
926 $sth->execute( $tag, $subfield, $fwk );
927 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
930 "SELECT authorised_value,lib
931 FROM authorised_values
935 $authvalsth->execute($authorisedvaluecat);
936 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
937 $itemlocation{$authorisedvalue} = $lib;
939 return \
%itemlocation;
951 $itemlocation{"1"} = "Not For Loan";
952 return \
%itemlocation;
957 $items = GetLostItems( $where, $orderby );
959 This function gets a list of lost items.
965 C<$where> is a hashref. it containts a field of the items table as key
966 and the value to match as value. For example:
968 { barcode => 'abc123',
969 homebranch => 'CPL', }
971 C<$orderby> is a field of the items table by which the resultset
976 C<$items> is a reference to an array full of hashrefs with columns
977 from the "items" table as keys.
979 =item usage in the perl script:
981 my $where = { barcode => '0001548' };
982 my $items = GetLostItems( $where, "homebranch" );
983 $template->param( itemsloop => $items );
990 # Getting input args.
993 my $dbh = C4
::Context
->dbh;
996 SELECT title, author, lib, itemlost, authorised_value, barcode, datelastseen, price, replacementprice, homebranch,
997 itype, itemtype, holdingbranch, location, itemnotes, items.biblionumber as biblionumber
999 LEFT JOIN biblio ON (items.biblionumber = biblio.biblionumber)
1000 LEFT JOIN biblioitems ON (items.biblionumber = biblioitems.biblionumber)
1001 LEFT JOIN authorised_values ON (items.itemlost = authorised_values.authorised_value)
1003 authorised_values.category = 'LOST'
1004 AND itemlost IS NOT NULL
1007 my @query_parameters;
1008 foreach my $key (keys %$where) {
1009 $query .= " AND $key LIKE ?";
1010 push @query_parameters, "%$where->{$key}%";
1012 my @ordervalues = qw
/title author homebranch itype barcode price replacementprice lib datelastseen location/;
1014 if ( defined $orderby && grep($orderby, @ordervalues)) {
1015 $query .= ' ORDER BY '.$orderby;
1018 my $sth = $dbh->prepare($query);
1019 $sth->execute( @query_parameters );
1021 while ( my $row = $sth->fetchrow_hashref ){
1027 =head2 GetItemsForInventory
1029 ($itemlist, $iTotalRecords) = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $offset, $size, $statushash);
1031 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
1033 The sub returns a reference to a list of hashes, each containing
1034 itemnumber, author, title, barcode, item callnumber, and date last
1035 seen. It is ordered by callnumber then title.
1037 The required minlocation & maxlocation parameters are used to specify a range of item callnumbers
1038 the datelastseen can be used to specify that you want to see items not seen since a past date only.
1039 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
1040 $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.
1042 $iTotalRecords is the number of rows that would have been returned without the $offset, $size limit clause
1046 sub GetItemsForInventory
{
1047 my ( $minlocation, $maxlocation,$location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $size, $statushash ) = @_;
1048 my $dbh = C4
::Context
->dbh;
1049 my ( @bind_params, @where_strings );
1051 my $select_columns = q{
1052 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, stocknumber
1054 my $select_count = q{SELECT COUNT(*)};
1057 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
1058 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
1061 for my $authvfield (keys %$statushash){
1062 if ( scalar @
{$statushash->{$authvfield}} > 0 ){
1063 my $joinedvals = join ',', @
{$statushash->{$authvfield}};
1064 push @where_strings, "$authvfield in (" . $joinedvals . ")";
1070 push @where_strings, 'itemcallnumber >= ?';
1071 push @bind_params, $minlocation;
1075 push @where_strings, 'itemcallnumber <= ?';
1076 push @bind_params, $maxlocation;
1079 if ($datelastseen) {
1080 $datelastseen = format_date_in_iso
($datelastseen);
1081 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
1082 push @bind_params, $datelastseen;
1086 push @where_strings, 'items.location = ?';
1087 push @bind_params, $location;
1090 if ( $branchcode ) {
1091 if($branch eq "homebranch"){
1092 push @where_strings, 'items.homebranch = ?';
1094 push @where_strings, 'items.holdingbranch = ?';
1096 push @bind_params, $branchcode;
1100 push @where_strings, 'biblioitems.itemtype = ?';
1101 push @bind_params, $itemtype;
1104 if ( $ignoreissued) {
1105 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
1106 push @where_strings, 'issues.date_due IS NULL';
1109 if ( @where_strings ) {
1111 $query .= join ' AND ', @where_strings;
1113 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
1114 my $count_query = $select_count . $query;
1115 $query .= " LIMIT $offset, $size" if ($offset and $size);
1116 $query = $select_columns . $query;
1117 my $sth = $dbh->prepare($query);
1118 $sth->execute( @bind_params );
1121 my $tmpresults = $sth->fetchall_arrayref({});
1122 $sth = $dbh->prepare( $count_query );
1123 $sth->execute( @bind_params );
1124 my ($iTotalRecords) = $sth->fetchrow_array();
1126 foreach my $row (@
$tmpresults) {
1129 foreach (keys %$row) {
1130 # If the koha field is mapped to a marc field
1131 my ($f, $sf) = GetMarcFromKohaField
("items.$_", $row->{'frameworkcode'});
1133 # We replace the code with it's description
1134 my $authvals = C4
::Koha
::GetKohaAuthorisedValuesFromField
($f, $sf, $row->{'frameworkcode'});
1135 $row->{$_} = $authvals->{$row->{$_}} if defined $authvals->{$row->{$_}};
1138 push @results, $row;
1141 return (\
@results, $iTotalRecords);
1144 =head2 GetItemsCount
1146 $count = &GetItemsCount( $biblionumber);
1148 This function return count of item with $biblionumber
1153 my ( $biblionumber ) = @_;
1154 my $dbh = C4
::Context
->dbh;
1155 my $query = "SELECT count(*)
1157 WHERE biblionumber=?";
1158 my $sth = $dbh->prepare($query);
1159 $sth->execute($biblionumber);
1160 my $count = $sth->fetchrow;
1164 =head2 GetItemInfosOf
1166 GetItemInfosOf(@itemnumbers);
1170 sub GetItemInfosOf
{
1171 my @itemnumbers = @_;
1173 my $itemnumber_values = @itemnumbers ?
join( ',', @itemnumbers ) : "''";
1178 WHERE itemnumber IN ($itemnumber_values)
1180 return get_infos_of
( $query, 'itemnumber' );
1183 =head2 GetItemsByBiblioitemnumber
1185 GetItemsByBiblioitemnumber($biblioitemnumber);
1187 Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
1188 Called by C<C4::XISBN>
1192 sub GetItemsByBiblioitemnumber
{
1193 my ( $bibitem ) = @_;
1194 my $dbh = C4
::Context
->dbh;
1195 my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
1196 # Get all items attached to a biblioitem
1199 $sth->execute($bibitem) || die $sth->errstr;
1200 while ( my $data = $sth->fetchrow_hashref ) {
1201 # Foreach item, get circulation information
1202 my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
1203 WHERE itemnumber = ?
1204 AND issues.borrowernumber = borrowers.borrowernumber"
1206 $sth2->execute( $data->{'itemnumber'} );
1207 if ( my $data2 = $sth2->fetchrow_hashref ) {
1208 # if item is out, set the due date and who it is out too
1209 $data->{'date_due'} = $data2->{'date_due'};
1210 $data->{'cardnumber'} = $data2->{'cardnumber'};
1211 $data->{'borrowernumber'} = $data2->{'borrowernumber'};
1214 # set date_due to blank, so in the template we check itemlost, and withdrawn
1215 $data->{'date_due'} = '';
1217 # Find the last 3 people who borrowed this item.
1218 my $query2 = "SELECT * FROM old_issues, borrowers WHERE itemnumber = ?
1219 AND old_issues.borrowernumber = borrowers.borrowernumber
1220 ORDER BY returndate desc,timestamp desc LIMIT 3";
1221 $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
1222 $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
1224 while ( my $data2 = $sth2->fetchrow_hashref ) {
1225 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1226 $data->{"card$i2"} = $data2->{'cardnumber'};
1227 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1230 push(@results,$data);
1237 @results = GetItemsInfo($biblionumber);
1239 Returns information about items with the given biblionumber.
1241 C<GetItemsInfo> returns a list of references-to-hash. Each element
1242 contains a number of keys. Most of them are attributes from the
1243 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1244 Koha database. Other keys include:
1248 =item C<$data-E<gt>{branchname}>
1250 The name (not the code) of the branch to which the book belongs.
1252 =item C<$data-E<gt>{datelastseen}>
1254 This is simply C<items.datelastseen>, except that while the date is
1255 stored in YYYY-MM-DD format in the database, here it is converted to
1256 DD/MM/YYYY format. A NULL date is returned as C<//>.
1258 =item C<$data-E<gt>{datedue}>
1260 =item C<$data-E<gt>{class}>
1262 This is the concatenation of C<biblioitems.classification>, the book's
1263 Dewey code, and C<biblioitems.subclass>.
1265 =item C<$data-E<gt>{ocount}>
1267 I think this is the number of copies of the book available.
1269 =item C<$data-E<gt>{order}>
1271 If this is set, it is set to C<One Order>.
1278 my ( $biblionumber ) = @_;
1279 my $dbh = C4
::Context
->dbh;
1280 # note biblioitems.* must be avoided to prevent large marc and marcxml fields from killing performance.
1286 biblioitems.itemtype,
1289 biblioitems.publicationyear,
1290 biblioitems.publishercode,
1291 biblioitems.volumedate,
1292 biblioitems.volumedesc,
1295 items.notforloan as itemnotforloan,
1296 itemtypes.description,
1297 itemtypes.notforloan as notforloan_per_itemtype,
1300 holding.opac_info as holding_branch_opac_info,
1301 home.opac_info as home_branch_opac_info
1303 $query .= ", issues.onsite_checkout"
1304 if C4
::Context
->preference("OnSiteCheckouts");
1307 LEFT JOIN branches AS holding ON items.holdingbranch = holding.branchcode
1308 LEFT JOIN branches AS home ON items.homebranch=home.branchcode
1309 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1310 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1311 LEFT JOIN itemtypes ON itemtypes.itemtype = "
1312 . (C4
::Context
->preference('item-level_itypes') ?
'items.itype' : 'biblioitems.itemtype');
1313 $query .= " LEFT JOIN issues ON issues.itemnumber = items.itemnumber"
1314 if C4
::Context
->preference("OnSiteCheckouts");
1315 $query .= " WHERE items.biblionumber = ? ORDER BY home.branchname, items.enumchron, LPAD( items.copynumber, 8, '0' ), items.dateaccessioned DESC" ;
1316 my $sth = $dbh->prepare($query);
1317 $sth->execute($biblionumber);
1322 my $isth = $dbh->prepare(
1323 "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname,borrowers.branchcode as bcode
1324 FROM issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
1325 WHERE itemnumber = ?"
1327 my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=? ");
1328 while ( my $data = $sth->fetchrow_hashref ) {
1330 $isth->execute( $data->{'itemnumber'} );
1331 if ( my $idata = $isth->fetchrow_hashref ) {
1332 $data->{borrowernumber
} = $idata->{borrowernumber
};
1333 $data->{cardnumber
} = $idata->{cardnumber
};
1334 $data->{surname
} = $idata->{surname
};
1335 $data->{firstname
} = $idata->{firstname
};
1336 $data->{lastreneweddate
} = $idata->{lastreneweddate
};
1337 $datedue = $idata->{'date_due'};
1338 if (C4
::Context
->preference("IndependentBranches")){
1339 my $userenv = C4
::Context
->userenv;
1340 unless ( C4
::Context
->IsSuperLibrarian() ) {
1341 $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch
});
1345 if ( $data->{'serial'}) {
1346 $ssth->execute($data->{'itemnumber'}) ;
1347 ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
1350 #get branch information.....
1351 my $bsth = $dbh->prepare(
1352 "SELECT * FROM branches WHERE branchcode = ?
1355 $bsth->execute( $data->{'holdingbranch'} );
1356 if ( my $bdata = $bsth->fetchrow_hashref ) {
1357 $data->{'branchname'} = $bdata->{'branchname'};
1359 $data->{'datedue'} = $datedue;
1361 # get notforloan complete status if applicable
1362 if ( my $code = C4
::Koha
::GetAuthValCode
( 'items.notforloan', $data->{frameworkcode
} ) ) {
1363 $data->{notforloanvalue
} = C4
::Koha
::GetKohaAuthorisedValueLib
( $code, $data->{itemnotforloan
} );
1364 $data->{notforloanvalueopac
} = C4
::Koha
::GetKohaAuthorisedValueLib
( $code, $data->{itemnotforloan
}, 1 );
1367 # get restricted status and description if applicable
1368 if ( my $code = C4
::Koha
::GetAuthValCode
( 'items.restricted', $data->{frameworkcode
} ) ) {
1369 $data->{restrictedopac
} = C4
::Koha
::GetKohaAuthorisedValueLib
( $code, $data->{restricted
}, 1 );
1370 $data->{restricted
} = C4
::Koha
::GetKohaAuthorisedValueLib
( $code, $data->{restricted
} );
1373 # my stack procedures
1374 if ( my $code = C4
::Koha
::GetAuthValCode
( 'items.stack', $data->{frameworkcode
} ) ) {
1375 $data->{stack
} = C4
::Koha
::GetKohaAuthorisedValueLib
( $code, $data->{stack
} );
1377 # Find the last 3 people who borrowed this item.
1378 my $sth2 = $dbh->prepare("SELECT * FROM old_issues,borrowers
1379 WHERE itemnumber = ?
1380 AND old_issues.borrowernumber = borrowers.borrowernumber
1381 ORDER BY returndate DESC
1383 $sth2->execute($data->{'itemnumber'});
1385 while (my $data2 = $sth2->fetchrow_hashref()) {
1386 $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
1387 $data->{"card$ii"} = $data2->{'cardnumber'} if $data2->{'cardnumber'};
1388 $data->{"borrower$ii"} = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
1392 $results[$i] = $data;
1396 return( sort { ($b->{'publisheddate'} || $b->{'enumchron'}) cmp ($a->{'publisheddate'} || $a->{'enumchron'}) } @results );
1402 =head2 GetItemsLocationInfo
1404 my @itemlocinfo = GetItemsLocationInfo($biblionumber);
1406 Returns the branch names, shelving location and itemcallnumber for each item attached to the biblio in question
1408 C<GetItemsInfo> returns a list of references-to-hash. Data returned:
1412 =item C<$data-E<gt>{homebranch}>
1414 Branch Name of the item's homebranch
1416 =item C<$data-E<gt>{holdingbranch}>
1418 Branch Name of the item's holdingbranch
1420 =item C<$data-E<gt>{location}>
1422 Item's shelving location code
1424 =item C<$data-E<gt>{location_intranet}>
1426 The intranet description for the Shelving Location as set in authorised_values 'LOC'
1428 =item C<$data-E<gt>{location_opac}>
1430 The OPAC description for the Shelving Location as set in authorised_values 'LOC'. Falls back to intranet description if no OPAC
1433 =item C<$data-E<gt>{itemcallnumber}>
1435 Item's itemcallnumber
1437 =item C<$data-E<gt>{cn_sort}>
1439 Item's call number normalized for sorting
1445 sub GetItemsLocationInfo
{
1446 my $biblionumber = shift;
1449 my $dbh = C4
::Context
->dbh;
1450 my $query = "SELECT a.branchname as homebranch, b.branchname as holdingbranch,
1451 location, itemcallnumber, cn_sort
1452 FROM items, branches as a, branches as b
1453 WHERE homebranch = a.branchcode AND holdingbranch = b.branchcode
1454 AND biblionumber = ?
1455 ORDER BY cn_sort ASC";
1456 my $sth = $dbh->prepare($query);
1457 $sth->execute($biblionumber);
1459 while ( my $data = $sth->fetchrow_hashref ) {
1460 $data->{location_intranet
} = GetKohaAuthorisedValueLib
('LOC', $data->{location
});
1461 $data->{location_opac
}= GetKohaAuthorisedValueLib
('LOC', $data->{location
}, 1);
1462 push @results, $data;
1467 =head2 GetHostItemsInfo
1469 $hostiteminfo = GetHostItemsInfo($hostfield);
1470 Returns the iteminfo for items linked to records via a host field
1474 sub GetHostItemsInfo
{
1476 my @returnitemsInfo;
1478 if (C4
::Context
->preference('marcflavour') eq 'MARC21' ||
1479 C4
::Context
->preference('marcflavour') eq 'NORMARC'){
1480 foreach my $hostfield ( $record->field('773') ) {
1481 my $hostbiblionumber = $hostfield->subfield("0");
1482 my $linkeditemnumber = $hostfield->subfield("9");
1483 my @hostitemInfos = GetItemsInfo
($hostbiblionumber);
1484 foreach my $hostitemInfo (@hostitemInfos){
1485 if ($hostitemInfo->{itemnumber
} eq $linkeditemnumber){
1486 push (@returnitemsInfo,$hostitemInfo);
1491 } elsif ( C4
::Context
->preference('marcflavour') eq 'UNIMARC'){
1492 foreach my $hostfield ( $record->field('461') ) {
1493 my $hostbiblionumber = $hostfield->subfield("0");
1494 my $linkeditemnumber = $hostfield->subfield("9");
1495 my @hostitemInfos = GetItemsInfo
($hostbiblionumber);
1496 foreach my $hostitemInfo (@hostitemInfos){
1497 if ($hostitemInfo->{itemnumber
} eq $linkeditemnumber){
1498 push (@returnitemsInfo,$hostitemInfo);
1504 return @returnitemsInfo;
1508 =head2 GetLastAcquisitions
1510 my $lastacq = GetLastAcquisitions({'branches' => ('branch1','branch2'),
1511 'itemtypes' => ('BK','BD')}, 10);
1515 sub GetLastAcquisitions
{
1516 my ($data,$max) = @_;
1518 my $itemtype = C4
::Context
->preference('item-level_itypes') ?
'itype' : 'itemtype';
1520 my $number_of_branches = @
{$data->{branches
}};
1521 my $number_of_itemtypes = @
{$data->{itemtypes
}};
1524 my @where = ('WHERE 1 ');
1525 $number_of_branches and push @where
1526 , 'AND holdingbranch IN ('
1527 , join(',', ('?') x
$number_of_branches )
1531 $number_of_itemtypes and push @where
1532 , "AND $itemtype IN ("
1533 , join(',', ('?') x
$number_of_itemtypes )
1537 my $query = "SELECT biblio.biblionumber as biblionumber, title, dateaccessioned
1538 FROM items RIGHT JOIN biblio ON (items.biblionumber=biblio.biblionumber)
1539 RIGHT JOIN biblioitems ON (items.biblioitemnumber=biblioitems.biblioitemnumber)
1541 GROUP BY biblio.biblionumber
1542 ORDER BY dateaccessioned DESC LIMIT $max";
1544 my $dbh = C4
::Context
->dbh;
1545 my $sth = $dbh->prepare($query);
1547 $sth->execute((@
{$data->{branches
}}, @
{$data->{itemtypes
}}));
1550 while( my $row = $sth->fetchrow_hashref){
1551 push @results, {date
=> $row->{dateaccessioned
}
1552 , biblionumber
=> $row->{biblionumber
}
1553 , title
=> $row->{title
}};
1559 =head2 GetItemnumbersForBiblio
1561 my $itemnumbers = GetItemnumbersForBiblio($biblionumber);
1563 Given a single biblionumber, return an arrayref of all the corresponding itemnumbers
1567 sub GetItemnumbersForBiblio
{
1568 my $biblionumber = shift;
1570 my $dbh = C4
::Context
->dbh;
1571 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
1572 $sth->execute($biblionumber);
1573 while (my $result = $sth->fetchrow_hashref) {
1574 push @items, $result->{'itemnumber'};
1579 =head2 get_itemnumbers_of
1581 my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
1583 Given a list of biblionumbers, return the list of corresponding itemnumbers
1584 for each biblionumber.
1586 Return a reference on a hash where keys are biblionumbers and values are
1587 references on array of itemnumbers.
1591 sub get_itemnumbers_of
{
1592 my @biblionumbers = @_;
1594 my $dbh = C4
::Context
->dbh;
1600 WHERE biblionumber IN (?' . ( ',?' x
scalar @biblionumbers - 1 ) . ')
1602 my $sth = $dbh->prepare($query);
1603 $sth->execute(@biblionumbers);
1607 while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
1608 push @
{ $itemnumbers_of{$biblionumber} }, $itemnumber;
1611 return \
%itemnumbers_of;
1614 =head2 get_hostitemnumbers_of
1616 my @itemnumbers_of = get_hostitemnumbers_of($biblionumber);
1618 Given a biblionumber, return the list of corresponding itemnumbers that are linked to it via host fields
1620 Return a reference on a hash where key is a biblionumber and values are
1621 references on array of itemnumbers.
1626 sub get_hostitemnumbers_of
{
1627 my ($biblionumber) = @_;
1628 my $marcrecord = GetMarcBiblio
($biblionumber);
1629 my (@returnhostitemnumbers,$tag, $biblio_s, $item_s);
1631 my $marcflavor = C4
::Context
->preference('marcflavour');
1632 if ($marcflavor eq 'MARC21' || $marcflavor eq 'NORMARC') {
1636 } elsif ($marcflavor eq 'UNIMARC') {
1642 foreach my $hostfield ( $marcrecord->field($tag) ) {
1643 my $hostbiblionumber = $hostfield->subfield($biblio_s);
1644 my $linkeditemnumber = $hostfield->subfield($item_s);
1646 if (my $itemnumbers = get_itemnumbers_of
($hostbiblionumber)->{$hostbiblionumber})
1648 @itemnumbers = @
$itemnumbers;
1650 foreach my $itemnumber (@itemnumbers){
1651 if ($itemnumber eq $linkeditemnumber){
1652 push (@returnhostitemnumbers,$itemnumber);
1657 return @returnhostitemnumbers;
1661 =head2 GetItemnumberFromBarcode
1663 $result = GetItemnumberFromBarcode($barcode);
1667 sub GetItemnumberFromBarcode
{
1669 my $dbh = C4
::Context
->dbh;
1672 $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
1673 $rq->execute($barcode);
1674 my ($result) = $rq->fetchrow;
1678 =head2 GetBarcodeFromItemnumber
1680 $result = GetBarcodeFromItemnumber($itemnumber);
1684 sub GetBarcodeFromItemnumber
{
1685 my ($itemnumber) = @_;
1686 my $dbh = C4
::Context
->dbh;
1689 $dbh->prepare("SELECT barcode FROM items WHERE items.itemnumber=?");
1690 $rq->execute($itemnumber);
1691 my ($result) = $rq->fetchrow;
1695 =head2 GetHiddenItemnumbers
1697 my @itemnumbers_to_hide = GetHiddenItemnumbers(@items);
1699 Given a list of items it checks which should be hidden from the OPAC given
1700 the current configuration. Returns a list of itemnumbers corresponding to
1701 those that should be hidden.
1705 sub GetHiddenItemnumbers
{
1709 my $yaml = C4
::Context
->preference('OpacHiddenItems');
1710 return () if (! $yaml =~ /\S/ );
1711 $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
1714 $hidingrules = YAML
::Load
($yaml);
1717 warn "Unable to parse OpacHiddenItems syspref : $@";
1720 my $dbh = C4
::Context
->dbh;
1723 foreach my $item (@items) {
1725 # We check each rule
1726 foreach my $field (keys %$hidingrules) {
1728 if (exists $item->{$field}) {
1729 $val = $item->{$field};
1732 my $query = "SELECT $field from items where itemnumber = ?";
1733 $val = $dbh->selectrow_array($query, undef, $item->{'itemnumber'});
1735 $val = '' unless defined $val;
1737 # If the results matches the values in the yaml file
1738 if (any
{ $val eq $_ } @
{$hidingrules->{$field}}) {
1740 # We add the itemnumber to the list
1741 push @resultitems, $item->{'itemnumber'};
1743 # If at least one rule matched for an item, no need to test the others
1748 return @resultitems;
1751 =head3 get_item_authorised_values
1753 find the types and values for all authorised values assigned to this item.
1755 parameters: itemnumber
1757 returns: a hashref malling the authorised value to the value set for this itemnumber
1759 $authorised_values = {
1765 'RESTRICTED' => undef,
1768 'branches' => 'CPL',
1769 'cn_source' => undef,
1770 'itemtypes' => 'SER',
1773 Notes: see C4::Biblio::get_biblio_authorised_values for a similar method at the biblio level.
1777 sub get_item_authorised_values
{
1778 my $itemnumber = shift;
1780 # assume that these entries in the authorised_value table are item level.
1781 my $query = q
(SELECT distinct authorised_value
, kohafield
1782 FROM marc_subfield_structure
1783 WHERE kohafield like
'item%'
1784 AND authorised_value
!= '' );
1786 my $itemlevel_authorised_values = C4
::Context
->dbh->selectall_hashref( $query, 'authorised_value' );
1787 my $iteminfo = GetItem
( $itemnumber );
1788 # warn( Data::Dumper->Dump( [ $itemlevel_authorised_values ], [ 'itemlevel_authorised_values' ] ) );
1790 foreach my $this_authorised_value ( keys %$itemlevel_authorised_values ) {
1791 my $field = $itemlevel_authorised_values->{ $this_authorised_value }->{'kohafield'};
1792 $field =~ s/^items\.//;
1793 if ( exists $iteminfo->{ $field } ) {
1794 $return->{ $this_authorised_value } = $iteminfo->{ $field };
1797 # warn( Data::Dumper->Dump( [ $return ], [ 'return' ] ) );
1801 =head3 get_authorised_value_images
1803 find a list of icons that are appropriate for display based on the
1804 authorised values for a biblio.
1806 parameters: listref of authorised values, such as comes from
1807 get_item_authorised_values or
1808 from C4::Biblio::get_biblio_authorised_values
1810 returns: listref of hashrefs for each image. Each hashref looks like this:
1812 { imageurl => '/intranet-tmpl/prog/img/itemtypeimg/npl/WEB.gif',
1817 Notes: Currently, I put on the full path to the images on the staff
1818 side. This should either be configurable or not done at all. Since I
1819 have to deal with 'intranet' or 'opac' in
1820 get_biblio_authorised_values, perhaps I should be passing it in.
1824 sub get_authorised_value_images
{
1825 my $authorised_values = shift;
1829 my $authorised_value_list = GetAuthorisedValues
();
1830 # warn ( Data::Dumper->Dump( [ $authorised_value_list ], [ 'authorised_value_list' ] ) );
1831 foreach my $this_authorised_value ( @
$authorised_value_list ) {
1832 if ( exists $authorised_values->{ $this_authorised_value->{'category'} }
1833 && $authorised_values->{ $this_authorised_value->{'category'} } eq $this_authorised_value->{'authorised_value'} ) {
1834 # warn ( Data::Dumper->Dump( [ $this_authorised_value ], [ 'this_authorised_value' ] ) );
1835 if ( defined $this_authorised_value->{'imageurl'} ) {
1836 push @imagelist, { imageurl
=> C4
::Koha
::getitemtypeimagelocation
( 'intranet', $this_authorised_value->{'imageurl'} ),
1837 label
=> $this_authorised_value->{'lib'},
1838 category
=> $this_authorised_value->{'category'},
1839 value
=> $this_authorised_value->{'authorised_value'}, };
1844 # warn ( Data::Dumper->Dump( [ \@imagelist ], [ 'imagelist' ] ) );
1849 =head1 LIMITED USE FUNCTIONS
1851 The following functions, while part of the public API,
1852 are not exported. This is generally because they are
1853 meant to be used by only one script for a specific
1854 purpose, and should not be used in any other context
1855 without careful thought.
1861 my $item_marc = GetMarcItem($biblionumber, $itemnumber);
1863 Returns MARC::Record of the item passed in parameter.
1864 This function is meant for use only in C<cataloguing/additem.pl>,
1865 where it is needed to support that script's MARC-like
1871 my ( $biblionumber, $itemnumber ) = @_;
1873 # GetMarcItem has been revised so that it does the following:
1874 # 1. Gets the item information from the items table.
1875 # 2. Converts it to a MARC field for storage in the bib record.
1877 # The previous behavior was:
1878 # 1. Get the bib record.
1879 # 2. Return the MARC tag corresponding to the item record.
1881 # The difference is that one treats the items row as authoritative,
1882 # while the other treats the MARC representation as authoritative
1883 # under certain circumstances.
1885 my $itemrecord = GetItem
($itemnumber);
1887 # Tack on 'items.' prefix to column names so that TransformKohaToMarc will work.
1888 # Also, don't emit a subfield if the underlying field is blank.
1891 return Item2Marc
($itemrecord,$biblionumber);
1895 my ($itemrecord,$biblionumber)=@_;
1898 defined($itemrecord->{$_}) && $itemrecord->{$_} ne '' ?
("items.$_" => $itemrecord->{$_}) : ()
1899 } keys %{ $itemrecord }
1901 my $itemmarc = TransformKohaToMarc
($mungeditem);
1902 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField
("items.itemnumber",GetFrameworkCode
($biblionumber)||'');
1904 my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml
($mungeditem->{'items.more_subfields_xml'});
1905 if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1906 foreach my $field ($itemmarc->field($itemtag)){
1907 $field->add_subfields(@
$unlinked_item_subfields);
1913 =head1 PRIVATE FUNCTIONS AND VARIABLES
1915 The following functions are not meant to be called
1916 directly, but are documented in order to explain
1917 the inner workings of C<C4::Items>.
1921 =head2 %derived_columns
1923 This hash keeps track of item columns that
1924 are strictly derived from other columns in
1925 the item record and are not meant to be set
1928 Each key in the hash should be the name of a
1929 column (as named by TransformMarcToKoha). Each
1930 value should be hashref whose keys are the
1931 columns on which the derived column depends. The
1932 hashref should also contain a 'BUILDER' key
1933 that is a reference to a sub that calculates
1938 my %derived_columns = (
1939 'items.cn_sort' => {
1940 'itemcallnumber' => 1,
1941 'items.cn_source' => 1,
1942 'BUILDER' => \
&_calc_items_cn_sort
,
1946 =head2 _set_derived_columns_for_add
1948 _set_derived_column_for_add($item);
1950 Given an item hash representing a new item to be added,
1951 calculate any derived columns. Currently the only
1952 such column is C<items.cn_sort>.
1956 sub _set_derived_columns_for_add
{
1959 foreach my $column (keys %derived_columns) {
1960 my $builder = $derived_columns{$column}->{'BUILDER'};
1961 my $source_values = {};
1962 foreach my $source_column (keys %{ $derived_columns{$column} }) {
1963 next if $source_column eq 'BUILDER';
1964 $source_values->{$source_column} = $item->{$source_column};
1966 $builder->($item, $source_values);
1970 =head2 _set_derived_columns_for_mod
1972 _set_derived_column_for_mod($item);
1974 Given an item hash representing a new item to be modified.
1975 calculate any derived columns. Currently the only
1976 such column is C<items.cn_sort>.
1978 This routine differs from C<_set_derived_columns_for_add>
1979 in that it needs to handle partial item records. In other
1980 words, the caller of C<ModItem> may have supplied only one
1981 or two columns to be changed, so this function needs to
1982 determine whether any of the columns to be changed affect
1983 any of the derived columns. Also, if a derived column
1984 depends on more than one column, but the caller is not
1985 changing all of then, this routine retrieves the unchanged
1986 values from the database in order to ensure a correct
1991 sub _set_derived_columns_for_mod
{
1994 foreach my $column (keys %derived_columns) {
1995 my $builder = $derived_columns{$column}->{'BUILDER'};
1996 my $source_values = {};
1997 my %missing_sources = ();
1998 my $must_recalc = 0;
1999 foreach my $source_column (keys %{ $derived_columns{$column} }) {
2000 next if $source_column eq 'BUILDER';
2001 if (exists $item->{$source_column}) {
2003 $source_values->{$source_column} = $item->{$source_column};
2005 $missing_sources{$source_column} = 1;
2009 foreach my $source_column (keys %missing_sources) {
2010 $source_values->{$source_column} = _get_single_item_column
($source_column, $item->{'itemnumber'});
2012 $builder->($item, $source_values);
2017 =head2 _do_column_fixes_for_mod
2019 _do_column_fixes_for_mod($item);
2021 Given an item hashref containing one or more
2022 columns to modify, fix up certain values.
2023 Specifically, set to 0 any passed value
2024 of C<notforloan>, C<damaged>, C<itemlost>, or
2025 C<withdrawn> that is either undefined or
2026 contains the empty string.
2030 sub _do_column_fixes_for_mod
{
2033 if (exists $item->{'notforloan'} and
2034 (not defined $item->{'notforloan'} or $item->{'notforloan'} eq '')) {
2035 $item->{'notforloan'} = 0;
2037 if (exists $item->{'damaged'} and
2038 (not defined $item->{'damaged'} or $item->{'damaged'} eq '')) {
2039 $item->{'damaged'} = 0;
2041 if (exists $item->{'itemlost'} and
2042 (not defined $item->{'itemlost'} or $item->{'itemlost'} eq '')) {
2043 $item->{'itemlost'} = 0;
2045 if (exists $item->{'withdrawn'} and
2046 (not defined $item->{'withdrawn'} or $item->{'withdrawn'} eq '')) {
2047 $item->{'withdrawn'} = 0;
2049 if (exists $item->{'location'} && !$item->{'permanent_location'}) {
2050 $item->{'permanent_location'} = $item->{'location'};
2052 if (exists $item->{'timestamp'}) {
2053 delete $item->{'timestamp'};
2057 =head2 _get_single_item_column
2059 _get_single_item_column($column, $itemnumber);
2061 Retrieves the value of a single column from an C<items>
2062 row specified by C<$itemnumber>.
2066 sub _get_single_item_column
{
2068 my $itemnumber = shift;
2070 my $dbh = C4
::Context
->dbh;
2071 my $sth = $dbh->prepare("SELECT $column FROM items WHERE itemnumber = ?");
2072 $sth->execute($itemnumber);
2073 my ($value) = $sth->fetchrow();
2077 =head2 _calc_items_cn_sort
2079 _calc_items_cn_sort($item, $source_values);
2081 Helper routine to calculate C<items.cn_sort>.
2085 sub _calc_items_cn_sort
{
2087 my $source_values = shift;
2089 $item->{'items.cn_sort'} = GetClassSort
($source_values->{'items.cn_source'}, $source_values->{'itemcallnumber'}, "");
2092 =head2 _set_defaults_for_add
2094 _set_defaults_for_add($item_hash);
2096 Given an item hash representing an item to be added, set
2097 correct default values for columns whose default value
2098 is not handled by the DBMS. This includes the following
2105 C<items.dateaccessioned>
2127 sub _set_defaults_for_add
{
2129 $item->{dateaccessioned
} ||= C4
::Dates
->new->output('iso');
2130 $item->{$_} ||= 0 for (qw( notforloan damaged itemlost withdrawn));
2133 =head2 _koha_new_item
2135 my ($itemnumber,$error) = _koha_new_item( $item, $barcode );
2137 Perform the actual insert into the C<items> table.
2141 sub _koha_new_item
{
2142 my ( $item, $barcode ) = @_;
2143 my $dbh=C4
::Context
->dbh;
2146 "INSERT INTO items SET
2148 biblioitemnumber = ?,
2150 dateaccessioned = ?,
2154 replacementprice = ?,
2155 replacementpricedate = ?,
2156 datelastborrowed = ?,
2164 coded_location_qualifier = ?,
2170 permanent_location = ?,
2182 more_subfields_xml = ?,
2186 my $sth = $dbh->prepare($query);
2187 my $today = C4
::Dates
->today('iso');
2189 $item->{'biblionumber'},
2190 $item->{'biblioitemnumber'},
2192 $item->{'dateaccessioned'},
2193 $item->{'booksellerid'},
2194 $item->{'homebranch'},
2196 $item->{'replacementprice'},
2197 $item->{'replacementpricedate'} || $today,
2198 $item->{datelastborrowed
},
2199 $item->{datelastseen
} || $today,
2201 $item->{'notforloan'},
2203 $item->{'itemlost'},
2204 $item->{'withdrawn'},
2205 $item->{'itemcallnumber'},
2206 $item->{'coded_location_qualifier'},
2207 $item->{'restricted'},
2208 $item->{'itemnotes'},
2209 $item->{'holdingbranch'},
2211 $item->{'location'},
2212 $item->{'permanent_location'},
2215 $item->{'renewals'},
2216 $item->{'reserves'},
2217 $item->{'items.cn_source'},
2218 $item->{'items.cn_sort'},
2221 $item->{'materials'},
2223 $item->{'enumchron'},
2224 $item->{'more_subfields_xml'},
2225 $item->{'copynumber'},
2226 $item->{'stocknumber'},
2230 if ( defined $sth->errstr ) {
2231 $error.="ERROR in _koha_new_item $query".$sth->errstr;
2234 $itemnumber = $dbh->{'mysql_insertid'};
2237 return ( $itemnumber, $error );
2240 =head2 MoveItemFromBiblio
2242 MoveItemFromBiblio($itenumber, $frombiblio, $tobiblio);
2244 Moves an item from a biblio to another
2246 Returns undef if the move failed or the biblionumber of the destination record otherwise
2250 sub MoveItemFromBiblio
{
2251 my ($itemnumber, $frombiblio, $tobiblio) = @_;
2252 my $dbh = C4
::Context
->dbh;
2253 my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = ?");
2254 $sth->execute( $tobiblio );
2255 my ( $tobiblioitem ) = $sth->fetchrow();
2256 $sth = $dbh->prepare("UPDATE items SET biblioitemnumber = ?, biblionumber = ? WHERE itemnumber = ? AND biblionumber = ?");
2257 my $return = $sth->execute($tobiblioitem, $tobiblio, $itemnumber, $frombiblio);
2259 ModZebra
( $tobiblio, "specialUpdate", "biblioserver" );
2260 ModZebra
( $frombiblio, "specialUpdate", "biblioserver" );
2261 # Checking if the item we want to move is in an order
2262 require C4
::Acquisition
;
2263 my $order = C4
::Acquisition
::GetOrderFromItemnumber
($itemnumber);
2265 # Replacing the biblionumber within the order if necessary
2266 $order->{'biblionumber'} = $tobiblio;
2267 C4
::Acquisition
::ModOrder
($order);
2276 DelItemCheck($dbh, $biblionumber, $itemnumber);
2278 Exported function (core API) for deleting an item record in Koha if there no current issue.
2283 my ( $dbh, $biblionumber, $itemnumber ) = @_;
2286 my $countanalytics=GetAnalyticsCount
($itemnumber);
2289 # check that there is no issue on this item before deletion.
2290 my $sth = $dbh->prepare(q{
2291 SELECT COUNT(*) FROM issues
2292 WHERE itemnumber = ?
2294 $sth->execute($itemnumber);
2295 my ($onloan) = $sth->fetchrow;
2297 my $item = GetItem
($itemnumber);
2300 $error = "book_on_loan"
2302 elsif ( !C4
::Context
->IsSuperLibrarian()
2303 and C4
::Context
->preference("IndependentBranches")
2304 and ( C4
::Context
->userenv->{branch
} ne $item->{'homebranch'} ) )
2306 $error = "not_same_branch";
2309 # check it doesnt have a waiting reserve
2310 $sth = $dbh->prepare(q{
2311 SELECT COUNT(*) FROM reserves
2312 WHERE (found = 'W' OR found = 'T')
2315 $sth->execute($itemnumber);
2316 my ($reserve) = $sth->fetchrow;
2318 $error = "book_reserved";
2319 } elsif ($countanalytics > 0){
2320 $error = "linked_analytics";
2324 biblionumber
=> $biblionumber,
2325 itemnumber
=> $itemnumber
2334 =head2 _koha_modify_item
2336 my ($itemnumber,$error) =_koha_modify_item( $item );
2338 Perform the actual update of the C<items> row. Note that this
2339 routine accepts a hashref specifying the columns to update.
2343 sub _koha_modify_item
{
2345 my $dbh=C4
::Context
->dbh;
2348 my $query = "UPDATE items SET ";
2350 for my $key ( keys %$item ) {
2351 next if ( $key eq 'itemnumber' );
2353 push @bind, $item->{$key};
2356 $query .= " WHERE itemnumber=?";
2357 push @bind, $item->{'itemnumber'};
2358 my $sth = $dbh->prepare($query);
2359 $sth->execute(@bind);
2361 $error.="ERROR in _koha_modify_item $query: ".$sth->errstr;
2364 return ($item->{'itemnumber'},$error);
2367 =head2 _koha_delete_item
2369 _koha_delete_item( $itemnum );
2371 Internal function to delete an item record from the koha tables
2375 sub _koha_delete_item
{
2376 my ( $itemnum ) = @_;
2378 my $dbh = C4
::Context
->dbh;
2379 # save the deleted item to deleteditems table
2380 my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
2381 $sth->execute($itemnum);
2382 my $data = $sth->fetchrow_hashref();
2384 # There is no item to delete
2385 return 0 unless $data;
2387 my $query = "INSERT INTO deleteditems SET ";
2389 foreach my $key ( keys %$data ) {
2390 next if ( $key eq 'timestamp' ); # timestamp will be set by db
2391 $query .= "$key = ?,";
2392 push( @bind, $data->{$key} );
2395 $sth = $dbh->prepare($query);
2396 $sth->execute(@bind);
2398 # delete from items table
2399 $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
2400 my $deleted = $sth->execute($itemnum);
2401 return ( $deleted == 1 ) ?
1 : 0;
2404 =head2 _marc_from_item_hash
2406 my $item_marc = _marc_from_item_hash($item, $frameworkcode[, $unlinked_item_subfields]);
2408 Given an item hash representing a complete item record,
2409 create a C<MARC::Record> object containing an embedded
2410 tag representing that item.
2412 The third, optional parameter C<$unlinked_item_subfields> is
2413 an arrayref of subfields (not mapped to C<items> fields per the
2414 framework) to be added to the MARC representation
2419 sub _marc_from_item_hash
{
2421 my $frameworkcode = shift;
2422 my $unlinked_item_subfields;
2424 $unlinked_item_subfields = shift;
2427 # Tack on 'items.' prefix to column names so lookup from MARC frameworks will work
2428 # Also, don't emit a subfield if the underlying field is blank.
2429 my $mungeditem = { map { (defined($item->{$_}) and $item->{$_} ne '') ?
2430 (/^items\./ ?
($_ => $item->{$_}) : ("items.$_" => $item->{$_}))
2431 : () } keys %{ $item } };
2433 my $item_marc = MARC
::Record
->new();
2434 foreach my $item_field ( keys %{$mungeditem} ) {
2435 my ( $tag, $subfield ) = GetMarcFromKohaField
( $item_field, $frameworkcode );
2436 next unless defined $tag and defined $subfield; # skip if not mapped to MARC field
2437 my @values = split(/\s?\|\s?/, $mungeditem->{$item_field}, -1);
2438 foreach my $value (@values){
2439 if ( my $field = $item_marc->field($tag) ) {
2440 $field->add_subfields( $subfield => $value );
2442 my $add_subfields = [];
2443 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
2444 $add_subfields = $unlinked_item_subfields;
2446 $item_marc->add_fields( $tag, " ", " ", $subfield => $value, @
$add_subfields );
2454 =head2 _repack_item_errors
2456 Add an error message hash generated by C<CheckItemPreSave>
2457 to a list of errors.
2461 sub _repack_item_errors
{
2462 my $item_sequence_num = shift;
2463 my $item_ref = shift;
2464 my $error_ref = shift;
2466 my @repacked_errors = ();
2468 foreach my $error_code (sort keys %{ $error_ref }) {
2469 my $repacked_error = {};
2470 $repacked_error->{'item_sequence'} = $item_sequence_num;
2471 $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ?
$item_ref->{'barcode'} : '';
2472 $repacked_error->{'error_code'} = $error_code;
2473 $repacked_error->{'error_information'} = $error_ref->{$error_code};
2474 push @repacked_errors, $repacked_error;
2477 return @repacked_errors;
2480 =head2 _get_unlinked_item_subfields
2482 my $unlinked_item_subfields = _get_unlinked_item_subfields($original_item_marc, $frameworkcode);
2486 sub _get_unlinked_item_subfields
{
2487 my $original_item_marc = shift;
2488 my $frameworkcode = shift;
2490 my $marcstructure = GetMarcStructure
(1, $frameworkcode);
2492 # assume that this record has only one field, and that that
2493 # field contains only the item information
2495 my @fields = $original_item_marc->fields();
2496 if ($#fields > -1) {
2497 my $field = $fields[0];
2498 my $tag = $field->tag();
2499 foreach my $subfield ($field->subfields()) {
2500 if (defined $subfield->[1] and
2501 $subfield->[1] ne '' and
2502 !$marcstructure->{$tag}->{$subfield->[0]}->{'kohafield'}) {
2503 push @
$subfields, $subfield->[0] => $subfield->[1];
2510 =head2 _get_unlinked_subfields_xml
2512 my $unlinked_subfields_xml = _get_unlinked_subfields_xml($unlinked_item_subfields);
2516 sub _get_unlinked_subfields_xml
{
2517 my $unlinked_item_subfields = shift;
2520 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
2521 my $marc = MARC
::Record
->new();
2522 # use of tag 999 is arbitrary, and doesn't need to match the item tag
2523 # used in the framework
2524 $marc->append_fields(MARC
::Field
->new('999', ' ', ' ', @
$unlinked_item_subfields));
2525 $marc->encoding("UTF-8");
2526 $xml = $marc->as_xml("USMARC");
2532 =head2 _parse_unlinked_item_subfields_from_xml
2534 my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'}):
2538 sub _parse_unlinked_item_subfields_from_xml
{
2540 require C4
::Charset
;
2541 return unless defined $xml and $xml ne "";
2542 my $marc = MARC
::Record
->new_from_xml(C4
::Charset
::StripNonXmlChars
($xml),'UTF-8');
2543 my $unlinked_subfields = [];
2544 my @fields = $marc->fields();
2545 if ($#fields > -1) {
2546 foreach my $subfield ($fields[0]->subfields()) {
2547 push @
$unlinked_subfields, $subfield->[0] => $subfield->[1];
2550 return $unlinked_subfields;
2553 =head2 GetAnalyticsCount
2555 $count= &GetAnalyticsCount($itemnumber)
2557 counts Usage of itemnumber in Analytical bibliorecords.
2561 sub GetAnalyticsCount
{
2562 my ($itemnumber) = @_;
2565 ### ZOOM search here
2567 $query= "hi=".$itemnumber;
2568 my ($err,$res,$result) = C4
::Search
::SimpleSearch
($query,0,10);
2574 $holds = &GetItemHolds($biblionumber, $itemnumber);
2576 This function return the count of holds with $biblionumber and $itemnumber
2581 my ($biblionumber, $itemnumber) = @_;
2583 my $dbh = C4
::Context
->dbh;
2584 my $query = "SELECT count(*)
2586 WHERE biblionumber=? AND itemnumber=?";
2587 my $sth = $dbh->prepare($query);
2588 $sth->execute($biblionumber, $itemnumber);
2589 $holds = $sth->fetchrow;
2593 # Return the list of the column names of items table
2594 sub _get_items_columns
{
2595 my $dbh = C4
::Context
->dbh;
2596 my $sth = $dbh->column_info(undef, undef, 'items', '%');
2598 my $results = $sth->fetchall_hashref('COLUMN_NAME');
2599 return keys %$results;
2604 my $items = SearchItems($field, $value);
2606 SearchItems will search for items on a specific given field.
2607 For instance you can search all items with a specific stocknumber like this:
2609 my $items = SearchItems('stocknumber', $stocknumber);
2614 my ($field, $value) = @_;
2616 my $dbh = C4
::Context
->dbh;
2617 my @columns = _get_items_columns
;
2619 if(0 < grep /^$field$/, @columns) {
2620 my $query = "SELECT $field FROM items WHERE $field = ?";
2621 my $sth = $dbh->prepare( $query );
2622 $sth->execute( $value );
2623 $results = $sth->fetchall_arrayref({});
2629 =head1 OTHER FUNCTIONS
2633 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2635 Find the given $subfield in the given $tag in the given
2636 MARC::Record $record. If the subfield is found, returns
2637 the (indicators, value) pair; otherwise, (undef, undef) is
2641 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2642 I suggest we export it from this module.
2647 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2650 if ( $tagfield < 10 ) {
2651 if ( $record->field($tagfield) ) {
2652 push @result, $record->field($tagfield)->data();
2657 foreach my $field ( $record->field($tagfield) ) {
2658 my @subfields = $field->subfields();
2659 foreach my $subfield (@subfields) {
2660 if ( @
$subfield[0] eq $insubfield ) {
2661 push @result, @
$subfield[1];
2662 $indicator = $field->indicator(1) . $field->indicator(2);
2667 return ( $indicator, @result );
2671 =head2 PrepareItemrecordDisplay
2673 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber,$frameworkcode);
2675 Returns a hash with all the fields for Display a given item data in a template
2677 The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
2681 sub PrepareItemrecordDisplay
{
2683 my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode ) = @_;
2685 my $dbh = C4
::Context
->dbh;
2686 $frameworkcode = &GetFrameworkCode
($bibnum) if $bibnum;
2687 my ( $itemtagfield, $itemtagsubfield ) = &GetMarcFromKohaField
( "items.itemnumber", $frameworkcode );
2688 my $tagslib = &GetMarcStructure
( 1, $frameworkcode );
2690 # return nothing if we don't have found an existing framework.
2691 return q{} unless $tagslib;
2694 $itemrecord = C4
::Items
::GetMarcItem
( $bibnum, $itemnum );
2698 my $branch_limit = C4
::Context
->userenv ? C4
::Context
->userenv->{"branch"} : "";
2700 SELECT authorised_value
,lib FROM authorised_values
2703 LEFT JOIN authorised_values_branches ON
( id
= av_id
)
2708 $query .= qq{ AND
( branchcode
= ? OR branchcode IS NULL
)} if $branch_limit;
2709 $query .= qq{ ORDER BY lib
};
2710 my $authorised_values_sth = $dbh->prepare( $query );
2711 foreach my $tag ( sort keys %{$tagslib} ) {
2712 my $previous_tag = '';
2715 # loop through each subfield
2717 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2718 next if ( subfield_is_koha_internal_p
($subfield) );
2719 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2721 $subfield_data{tag
} = $tag;
2722 $subfield_data{subfield
} = $subfield;
2723 $subfield_data{countsubfield
} = $cntsubf++;
2724 $subfield_data{kohafield
} = $tagslib->{$tag}->{$subfield}->{'kohafield'};
2725 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".int(rand(1000000));
2727 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2728 $subfield_data{marc_lib
} = $tagslib->{$tag}->{$subfield}->{lib
};
2729 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
2730 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
2731 $subfield_data{hidden
} = "display:none"
2732 if ( ( $tagslib->{$tag}->{$subfield}->{hidden
} > 4 )
2733 || ( $tagslib->{$tag}->{$subfield}->{hidden
} < -4 ) );
2734 my ( $x, $defaultvalue );
2736 ( $x, $defaultvalue ) = _find_value
( $tag, $subfield, $itemrecord );
2738 $defaultvalue = $tagslib->{$tag}->{$subfield}->{defaultvalue
} unless $defaultvalue;
2739 if ( !defined $defaultvalue ) {
2740 $defaultvalue = q
||;
2742 $defaultvalue =~ s/"/"/g;
2745 # search for itemcallnumber if applicable
2746 if ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber'
2747 && C4
::Context
->preference('itemcallnumber') ) {
2748 my $CNtag = substr( C4
::Context
->preference('itemcallnumber'), 0, 3 );
2749 my $CNsubfield = substr( C4
::Context
->preference('itemcallnumber'), 3, 1 );
2750 if ( $itemrecord and my $field = $itemrecord->field($CNtag) ) {
2751 $defaultvalue = $field->subfield($CNsubfield);
2754 if ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber'
2756 && $defaultvalues->{'callnumber'} ) {
2757 if( $itemrecord and $defaultvalues and not $itemrecord->field($subfield) ){
2758 # if the item record exists, only use default value if the item has no callnumber
2759 $defaultvalue = $defaultvalues->{callnumber
};
2760 } elsif ( !$itemrecord and $defaultvalues ) {
2761 # if the item record *doesn't* exists, always use the default value
2762 $defaultvalue = $defaultvalues->{callnumber
};
2765 if ( ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.holdingbranch' || $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.homebranch' )
2767 && $defaultvalues->{'branchcode'} ) {
2768 if ( $itemrecord and $defaultvalues and not $itemrecord->field($subfield) ) {
2769 $defaultvalue = $defaultvalues->{branchcode
};
2772 if ( ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.location' )
2774 && $defaultvalues->{'location'} ) {
2776 if ( $itemrecord and $defaultvalues and not $itemrecord->field($subfield) ) {
2777 # if the item record exists, only use default value if the item has no locationr
2778 $defaultvalue = $defaultvalues->{location
};
2779 } elsif ( !$itemrecord and $defaultvalues ) {
2780 # if the item record *doesn't* exists, always use the default value
2781 $defaultvalue = $defaultvalues->{location
};
2784 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
2785 my @authorised_values;
2788 # builds list, depending on authorised value...
2790 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
2791 if ( ( C4
::Context
->preference("IndependentBranches") )
2792 && !C4
::Context
->IsSuperLibrarian() ) {
2793 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname" );
2794 $sth->execute( C4
::Context
->userenv->{branch
} );
2795 push @authorised_values, ""
2796 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2797 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2798 push @authorised_values, $branchcode;
2799 $authorised_lib{$branchcode} = $branchname;
2802 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches ORDER BY branchname" );
2804 push @authorised_values, ""
2805 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2806 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2807 push @authorised_values, $branchcode;
2808 $authorised_lib{$branchcode} = $branchname;
2812 $defaultvalue = C4
::Context
->userenv ? C4
::Context
->userenv->{branch
} : undef;
2813 if ( $defaultvalues and $defaultvalues->{branchcode
} ) {
2814 $defaultvalue = $defaultvalues->{branchcode
};
2818 } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
2819 my $sth = $dbh->prepare( "SELECT itemtype,description FROM itemtypes ORDER BY description" );
2821 push @authorised_values, ""
2822 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2823 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
2824 push @authorised_values, $itemtype;
2825 $authorised_lib{$itemtype} = $description;
2828 } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
2829 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2831 my $class_sources = GetClassSources
();
2832 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
2834 foreach my $class_source (sort keys %$class_sources) {
2835 next unless $class_sources->{$class_source}->{'used'} or
2836 ($class_source eq $default_source);
2837 push @authorised_values, $class_source;
2838 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
2841 $defaultvalue = $default_source;
2843 #---- "true" authorised value
2845 $authorised_values_sth->execute(
2846 $tagslib->{$tag}->{$subfield}->{authorised_value
},
2847 $branch_limit ?
$branch_limit : ()
2849 push @authorised_values, ""
2850 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2851 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
2852 push @authorised_values, $value;
2853 $authorised_lib{$value} = $lib;
2856 $subfield_data{marc_value
} = {
2858 values => \
@authorised_values,
2859 default => "$defaultvalue",
2860 labels
=> \
%authorised_lib,
2862 } elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) {
2864 my $plugin = C4
::Context
->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
2866 my $extended_param = plugin_parameters
( $dbh, undef, $tagslib, $subfield_data{id
}, undef );
2867 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, undef, $tagslib, $subfield_data{id
}, undef );
2868 $subfield_data{random
} = int(rand(1000000)); # why do we need 2 different randoms?
2869 $subfield_data{marc_value
} = qq[<input type
="text" tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="50" maxlength
="255"
2870 onfocus
="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
2871 onblur
=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
2872 <a href
="#" class="buttonDot" onclick
="Clic$function_name('$subfield_data{id}'); return false;" title
="Tag Editor">...</a
>
2875 warn "Plugin Failed: $plugin";
2876 $subfield_data{marc_value
} = qq(<input type
="text" tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="50" maxlength
="255" />); # supply default input form
2879 elsif ( $tag eq '' ) { # it's an hidden field
2880 $subfield_data{marc_value
} = qq(<input type
="hidden" tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="50" maxlength
="255" value
="$defaultvalue" />);
2882 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
2883 $subfield_data{marc_value
} = qq(<input type
="text" tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="50" maxlength
="255" value
="$defaultvalue" />);
2885 elsif ( length($defaultvalue) > 100
2886 or (C4
::Context
->preference("marcflavour") eq "UNIMARC" and
2887 300 <= $tag && $tag < 400 && $subfield eq 'a' )
2888 or (C4
::Context
->preference("marcflavour") eq "MARC21" and
2889 500 <= $tag && $tag < 600 )
2891 # oversize field (textarea)
2892 $subfield_data{marc_value
} = qq(<textarea tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="50" maxlength
="255">$defaultvalue</textarea
>\n");
2894 $subfield_data{marc_value} = "<input type
=\"text
\" name
=\"field_value
\" value
=\"$defaultvalue\" size
=\"50\" maxlength
=\"255\" />";
2896 push( @loop_data, \%subfield_data );
2901 if ( $itemrecord && $itemrecord->field($itemtagfield) ) {
2902 $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield );
2905 'itemtagfield' => $itemtagfield,
2906 'itemtagsubfield' => $itemtagsubfield,
2907 'itemnumber' => $itemnumber,
2908 'iteminformation' => \@loop_data