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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 our (@ISA, @EXPORT_OK);
40 get_hostitemnumbers_of
45 PrepareItemrecordDisplay
53 use C4::Biblio qw( GetMarcStructure TransformMarcToKoha );
54 use Koha::DateUtils qw( dt_from_string output_pref );
56 use C4::ClassSource qw( GetClassSort GetClassSources GetClassSource );
57 use C4::Log qw( logaction );
58 use List::MoreUtils qw( any );
59 use DateTime::Format::MySQL;
60 # debugging; so please don't remove this
62 use Koha::AuthorisedValues;
63 use Koha::DateUtils qw( dt_from_string output_pref );
67 use Koha::Biblioitems;
70 use Koha::SearchEngine;
71 use Koha::SearchEngine::Indexer;
72 use Koha::SearchEngine::Search;
77 C4::Items - item management functions
81 This module contains an API for manipulating item
82 records in Koha, and is used by cataloguing, circulation,
83 acquisitions, and serials management.
85 # FIXME This POD is not up-to-date
86 A Koha item record is stored in two places: the
87 items table and embedded in a MARC tag in the XML
88 version of the associated bib record in C<biblioitems.marcxml>.
89 This is done to allow the item information to be readily
90 indexed (e.g., by Zebra), but means that each item
91 modification transaction must keep the items table
92 and the MARC XML in sync at all times.
94 The items table will be considered authoritative. In other
95 words, if there is ever a discrepancy between the items
96 table and the MARC XML, the items table should be considered
99 =head1 HISTORICAL NOTE
101 Most of the functions in C<C4::Items> were originally in
102 the C<C4::Biblio> module.
104 =head1 CORE EXPORTED FUNCTIONS
106 The following functions are meant for use by users
113 CartToShelf($itemnumber);
115 Set the current shelving location of the item record
116 to its stored permanent shelving location. This is
117 primarily used to indicate when an item whose current
118 location is a special processing ('PROC') or shelving cart
119 ('CART') location is back in the stacks.
124 my ( $itemnumber ) = @_;
126 unless ( $itemnumber ) {
127 croak "FAILED CartToShelf() - no itemnumber supplied";
130 my $item = Koha::Items->find($itemnumber);
131 if ( $item->location eq 'CART' ) {
132 $item->location($item->permanent_location)->store;
136 =head2 AddItemFromMarc
138 my ($biblionumber, $biblioitemnumber, $itemnumber)
139 = AddItemFromMarc($source_item_marc, $biblionumber[, $params]);
141 Given a MARC::Record object containing an embedded item
142 record and a biblionumber, create a new item record.
144 The final optional parameter, C<$params>, may contain
145 'skip_record_index' key, which relayed down to Koha::Item/store,
146 there it prevents calling of index_records,
147 which takes most of the time in batch adds/deletes: index_records
148 to be called later in C<additem.pl> after the whole loop.
150 You may also optionally pass biblioitemnumber in the params hash to
151 boost performance of inserts by preventing a lookup in Koha::Item.
154 skip_record_index => 1|0
155 biblioitemnumber => $biblioitemnumber
159 sub AddItemFromMarc {
160 my $source_item_marc = shift;
161 my $biblionumber = shift;
162 my $params = @_ ? shift : {};
164 my $dbh = C4::Context->dbh;
166 # parse item hash from MARC
167 my $frameworkcode = C4::Biblio::GetFrameworkCode($biblionumber);
168 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
169 my $localitemmarc = MARC::Record->new;
170 $localitemmarc->append_fields( $source_item_marc->field($itemtag) );
172 my $item_values = C4::Biblio::TransformMarcToKoha({ record => $localitemmarc, limit_table => 'items' });
173 my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode );
174 $item_values->{more_subfields_xml} = _get_unlinked_subfields_xml($unlinked_item_subfields);
175 $item_values->{biblionumber} = $biblionumber;
176 $item_values->{biblioitemnumber} = $params->{biblioitemnumber};
177 $item_values->{cn_source} = delete $item_values->{'items.cn_source'}; # Because of C4::Biblio::_disambiguate
178 $item_values->{cn_sort} = delete $item_values->{'items.cn_sort'}; # Because of C4::Biblio::_disambiguate
179 my $item = Koha::Item->new( $item_values )->store({ skip_record_index => $params->{skip_record_index} });
180 return ( $item->biblionumber, $item->biblioitemnumber, $item->itemnumber );
183 =head2 AddItemBatchFromMarc
185 ($itemnumber_ref, $error_ref) = AddItemBatchFromMarc($record,
186 $biblionumber, $biblioitemnumber, $frameworkcode);
188 Efficiently create item records from a MARC biblio record with
189 embedded item fields. This routine is suitable for batch jobs.
191 This API assumes that the bib record has already been
192 saved to the C<biblio> and C<biblioitems> tables. It does
193 not expect that C<biblio_metadata.metadata> is populated, but it
194 will do so via a call to ModBibiloMarc.
196 The goal of this API is to have a similar effect to using AddBiblio
197 and AddItems in succession, but without inefficient repeated
198 parsing of the MARC XML bib record.
200 This function returns an arrayref of new itemsnumbers and an arrayref of item
201 errors encountered during the processing. Each entry in the errors
202 list is a hashref containing the following keys:
208 Sequence number of original item tag in the MARC record.
212 Item barcode, provide to assist in the construction of
213 useful error messages.
217 Code representing the error condition. Can be 'duplicate_barcode',
218 'invalid_homebranch', or 'invalid_holdingbranch'.
220 =item error_information
222 Additional information appropriate to the error condition.
228 sub AddItemBatchFromMarc {
229 my ($record, $biblionumber, $biblioitemnumber, $frameworkcode) = @_;
230 my @itemnumbers = ();
232 my $dbh = C4::Context->dbh;
234 # We modify the record, so lets work on a clone so we don't change the
236 $record = $record->clone();
237 # loop through the item tags and start creating items
238 my @bad_item_fields = ();
239 my ($itemtag, $itemsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
240 my $item_sequence_num = 0;
241 ITEMFIELD: foreach my $item_field ($record->field($itemtag)) {
242 $item_sequence_num++;
243 # we take the item field and stick it into a new
244 # MARC record -- this is required so far because (FIXME)
245 # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
246 # and there is no TransformMarcFieldToKoha
247 my $temp_item_marc = MARC::Record->new();
248 $temp_item_marc->append_fields($item_field);
250 # add biblionumber and biblioitemnumber
251 my $item = TransformMarcToKoha({ record => $temp_item_marc, limit_table => 'items' });
252 my $unlinked_item_subfields = _get_unlinked_item_subfields($temp_item_marc, $frameworkcode);
253 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
254 $item->{'biblionumber'} = $biblionumber;
255 $item->{'biblioitemnumber'} = $biblioitemnumber;
256 $item->{cn_source} = delete $item->{'items.cn_source'}; # Because of C4::Biblio::_disambiguate
257 $item->{cn_sort} = delete $item->{'items.cn_sort'}; # Because of C4::Biblio::_disambiguate
259 # check for duplicate barcode
260 my %item_errors = CheckItemPreSave($item);
262 push @errors, _repack_item_errors($item_sequence_num, $item, \%item_errors);
263 push @bad_item_fields, $item_field;
267 my $item_object = Koha::Item->new($item)->store;
268 push @itemnumbers, $item_object->itemnumber; # FIXME not checking error
270 logaction("CATALOGUING", "ADD", $item_object->itemnumber, "item") if C4::Context->preference("CataloguingLog");
272 my $new_item_marc = _marc_from_item_hash($item_object->unblessed, $frameworkcode, $unlinked_item_subfields);
273 $item_field->replace_with($new_item_marc->field($itemtag));
276 # remove any MARC item fields for rejected items
277 foreach my $item_field (@bad_item_fields) {
278 $record->delete_field($item_field);
281 return (\@itemnumbers, \@errors);
284 =head2 ModItemFromMarc
286 my $item = ModItemFromMarc($item_marc, $biblionumber, $itemnumber[, $params]);
288 The final optional parameter, C<$params>, expected to contain
289 'skip_record_index' key, which relayed down to Koha::Item/store,
290 there it prevents calling of index_records,
291 which takes most of the time in batch adds/deletes: index_records better
292 to be called later in C<additem.pl> after the whole loop.
295 skip_record_index => 1|0
299 sub ModItemFromMarc {
300 my ( $item_marc, $biblionumber, $itemnumber, $params ) = @_;
302 my $frameworkcode = C4::Biblio::GetFrameworkCode($biblionumber);
303 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
305 my $localitemmarc = MARC::Record->new;
306 $localitemmarc->append_fields( $item_marc->field($itemtag) );
307 my $item_object = Koha::Items->find($itemnumber);
308 my $item = TransformMarcToKoha({ record => $localitemmarc, limit_table => 'items' });
310 # When importing items we blank this column, we need to set it to the existing value
311 # to prevent it being blanked by set_or_blank
312 $item->{onloan} = $item_object->onloan if( $item_object->onloan && !defined $item->{onloan} );
314 my ( $perm_loc_tag, $perm_loc_subfield ) = C4::Biblio::GetMarcFromKohaField( "items.permanent_location" );
315 my $has_permanent_location = defined $perm_loc_tag && defined $item_marc->subfield( $perm_loc_tag, $perm_loc_subfield );
317 # Retrieving the values for the fields that are not linked
318 my @mapped_fields = Koha::MarcSubfieldStructures->search(
320 frameworkcode => $frameworkcode,
321 kohafield => { -like => "items.%" }
323 )->get_column('kohafield');
324 for my $c ( $item_object->_result->result_source->columns ) {
325 next if grep { "items.$c" eq $_ } @mapped_fields;
326 $item->{$c} = $item_object->$c;
329 $item->{cn_source} = delete $item->{'items.cn_source'}; # Because of C4::Biblio::_disambiguate
330 delete $item->{'items.cn_sort'}; # Because of C4::Biblio::_disambiguate
331 $item->{itemnumber} = $itemnumber;
332 $item->{biblionumber} = $biblionumber;
334 my $existing_cn_sort = $item_object->cn_sort; # set_or_blank will reset cn_sort to undef as we are not passing it
335 # We rely on Koha::Item->store to modify it if itemcallnumber or cn_source is modified
336 $item_object = $item_object->set_or_blank($item);
337 $item_object->cn_sort($existing_cn_sort); # Resetting to the existing value
339 $item_object->make_column_dirty('permanent_location') if $has_permanent_location;
341 my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode );
342 $item_object->more_subfields_xml(_get_unlinked_subfields_xml($unlinked_item_subfields));
343 $item_object->store({ skip_record_index => $params->{skip_record_index} });
345 return $item_object->unblessed;
348 =head2 ModItemTransfer
350 ModItemTransfer($itemnumber, $frombranch, $tobranch, $trigger, [$params]);
352 Marks an item as being transferred from one branch to another and records the trigger.
354 The last optional parameter allows for passing skip_record_index through to the items store call.
358 sub ModItemTransfer {
359 my ( $itemnumber, $frombranch, $tobranch, $trigger, $params ) = @_;
361 my $dbh = C4::Context->dbh;
362 my $item = Koha::Items->find( $itemnumber );
364 # NOTE: This retains the existing hard coded behaviour by ignoring transfer limits
365 # and always replacing any existing transfers. (In theory, calls to ModItemTransfer
366 # will have been preceded by a check of branch transfer limits)
367 my $to_library = Koha::Libraries->find($tobranch);
368 my $transfer = $item->request_transfer(
377 # Immediately set the item to in transit if it is checked in
378 if ( !$item->checkout ) {
379 $item->holdingbranch($frombranch)->store(
382 skip_record_index => $params->{skip_record_index}
391 =head2 ModDateLastSeen
393 ModDateLastSeen( $itemnumber, $leave_item_lost, $params );
395 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking.
396 C<$itemnumber> is the item number
397 C<$leave_item_lost> determines if a lost item will be found or remain lost
399 The last optional parameter allows for passing skip_record_index through to the items store call.
403 sub ModDateLastSeen {
404 my ( $itemnumber, $leave_item_lost, $params ) = @_;
406 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
408 my $item = Koha::Items->find($itemnumber);
409 $item->datelastseen($today);
410 $item->itemlost(0) unless $leave_item_lost;
411 $item->store({ log_action => 0, skip_record_index => $params->{skip_record_index}, skip_holds_queue => $params->{skip_holds_queue} });
414 =head2 CheckItemPreSave
416 my $item_ref = TransformMarcToKoha({ record => $marc, limit_table => 'items' });
418 my %errors = CheckItemPreSave($item_ref);
419 if (exists $errors{'duplicate_barcode'}) {
420 print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
421 } elsif (exists $errors{'invalid_homebranch'}) {
422 print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
423 } elsif (exists $errors{'invalid_holdingbranch'}) {
424 print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
429 Given a hashref containing item fields, determine if it can be
430 inserted or updated in the database. Specifically, checks for
431 database integrity issues, and returns a hash containing any
432 of the following keys, if applicable.
436 =item duplicate_barcode
438 Barcode, if it duplicates one already found in the database.
440 =item invalid_homebranch
442 Home branch, if not defined in branches table.
444 =item invalid_holdingbranch
446 Holding branch, if not defined in branches table.
450 This function does NOT implement any policy-related checks,
451 e.g., whether current operator is allowed to save an
452 item that has a given branch code.
456 sub CheckItemPreSave {
457 my $item_ref = shift;
461 # check for duplicate barcode
462 if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
463 my $existing_item= Koha::Items->find({barcode => $item_ref->{'barcode'}});
464 if ($existing_item) {
465 if (!exists $item_ref->{'itemnumber'} # new item
466 or $item_ref->{'itemnumber'} != $existing_item->itemnumber) { # existing item
467 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
472 # check for valid home branch
473 if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
474 my $home_library = Koha::Libraries->find( $item_ref->{homebranch} );
475 unless (defined $home_library) {
476 $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
480 # check for valid holding branch
481 if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
482 my $holding_library = Koha::Libraries->find( $item_ref->{holdingbranch} );
483 unless (defined $holding_library) {
484 $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
492 =head1 EXPORTED SPECIAL ACCESSOR FUNCTIONS
494 The following functions provide various ways of
495 getting an item record, a set of item records, or
496 lists of authorized values for certain item fields.
500 =head2 GetItemsForInventory
502 ($itemlist, $iTotalRecords) = GetItemsForInventory( {
503 minlocation => $minlocation,
504 maxlocation => $maxlocation,
505 location => $location,
506 ignoreissued => $ignoreissued,
507 datelastseen => $datelastseen,
508 branchcode => $branchcode,
512 statushash => $statushash,
513 itemtypes => \@itemsarray,
516 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
518 The sub returns a reference to a list of hashes, each containing
519 itemnumber, author, title, barcode, item callnumber, and date last
520 seen. It is ordered by callnumber then title.
522 The required minlocation & maxlocation parameters are used to specify a range of item callnumbers
523 the datelastseen can be used to specify that you want to see items not seen since a past date only.
524 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
525 $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.
527 $iTotalRecords is the number of rows that would have been returned without the $offset, $size limit clause
531 sub GetItemsForInventory {
532 my ( $parameters ) = @_;
533 my $minlocation = $parameters->{'minlocation'} // '';
534 my $maxlocation = $parameters->{'maxlocation'} // '';
535 my $class_source = $parameters->{'class_source'} // C4::Context->preference('DefaultClassificationSource');
536 my $location = $parameters->{'location'} // '';
537 my $itemtype = $parameters->{'itemtype'} // '';
538 my $ignoreissued = $parameters->{'ignoreissued'} // '';
539 my $datelastseen = $parameters->{'datelastseen'} // '';
540 my $branchcode = $parameters->{'branchcode'} // '';
541 my $branch = $parameters->{'branch'} // '';
542 my $offset = $parameters->{'offset'} // '';
543 my $size = $parameters->{'size'} // '';
544 my $statushash = $parameters->{'statushash'} // '';
545 my $ignore_waiting_holds = $parameters->{'ignore_waiting_holds'} // '';
546 my $itemtypes = $parameters->{'itemtypes'} || [];
547 my $ccode = $parameters->{'ccode'} // '';
549 my $dbh = C4::Context->dbh;
550 my ( @bind_params, @where_strings );
552 my $min_cnsort = GetClassSort($class_source,undef,$minlocation);
553 my $max_cnsort = GetClassSort($class_source,undef,$maxlocation);
555 my $select_columns = q{
556 SELECT DISTINCT(items.itemnumber), barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, withdrawn, stocknumber, items.cn_sort, ccode
559 my $select_count = q{SELECT COUNT(DISTINCT(items.itemnumber))};
562 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
563 LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
566 for my $authvfield (keys %$statushash){
567 if ( scalar @{$statushash->{$authvfield}} > 0 ){
568 my $joinedvals = join ',', @{$statushash->{$authvfield}};
569 push @where_strings, "$authvfield in (" . $joinedvals . ")";
575 push @where_strings, 'ccode = ?';
576 push @bind_params, $ccode;
580 push @where_strings, 'items.cn_sort >= ?';
581 push @bind_params, $min_cnsort;
585 push @where_strings, 'items.cn_sort <= ?';
586 push @bind_params, $max_cnsort;
590 $datelastseen = output_pref({ str => $datelastseen, dateformat => 'iso', dateonly => 1 });
591 push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
592 push @bind_params, $datelastseen;
596 push @where_strings, 'items.location = ?';
597 push @bind_params, $location;
601 if($branch eq "homebranch"){
602 push @where_strings, 'items.homebranch = ?';
604 push @where_strings, 'items.holdingbranch = ?';
606 push @bind_params, $branchcode;
610 push @where_strings, 'biblioitems.itemtype = ?';
611 push @bind_params, $itemtype;
613 if ( $ignoreissued) {
614 $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
615 push @where_strings, 'issues.date_due IS NULL';
618 if ( $ignore_waiting_holds ) {
619 $query .= "LEFT JOIN reserves ON items.itemnumber = reserves.itemnumber ";
620 push( @where_strings, q{(reserves.found != 'W' OR reserves.found IS NULL)} );
624 my $itemtypes_str = join ', ', @$itemtypes;
625 push @where_strings, "( biblioitems.itemtype IN (" . $itemtypes_str . ") OR items.itype IN (" . $itemtypes_str . ") )";
628 if ( @where_strings ) {
630 $query .= join ' AND ', @where_strings;
632 my $count_query = $select_count . $query;
633 $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
634 $query .= " LIMIT $offset, $size" if ($offset and $size);
635 $query = $select_columns . $query;
636 my $sth = $dbh->prepare($query);
637 $sth->execute( @bind_params );
640 my $tmpresults = $sth->fetchall_arrayref({});
641 $sth = $dbh->prepare( $count_query );
642 $sth->execute( @bind_params );
643 my ($iTotalRecords) = $sth->fetchrow_array();
645 my @avs = Koha::AuthorisedValues->search(
646 { 'marc_subfield_structures.kohafield' => { '>' => '' },
647 'me.authorised_value' => { '>' => '' },
649 { join => { category => 'marc_subfield_structures' },
650 distinct => ['marc_subfield_structures.kohafield, me.category, frameworkcode, me.authorised_value'],
651 '+select' => [ 'marc_subfield_structures.kohafield', 'marc_subfield_structures.frameworkcode', 'me.authorised_value', 'me.lib' ],
652 '+as' => [ 'kohafield', 'frameworkcode', 'authorised_value', 'lib' ],
656 my $avmapping = { map { $_->get_column('kohafield') . ',' . $_->get_column('frameworkcode') . ',' . $_->get_column('authorised_value') => $_->get_column('lib') } @avs };
658 foreach my $row (@$tmpresults) {
661 foreach (keys %$row) {
664 $avmapping->{ "items.$_," . $row->{'frameworkcode'} . "," . ( $row->{$_} // q{} ) }
667 $row->{$_} = $avmapping->{"items.$_,".$row->{'frameworkcode'}.",".$row->{$_}};
673 return (\@results, $iTotalRecords);
678 @results = GetItemsInfo($biblionumber);
680 Returns information about items with the given biblionumber.
682 C<GetItemsInfo> returns a list of references-to-hash. Each element
683 contains a number of keys. Most of them are attributes from the
684 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
685 Koha database. Other keys include:
689 =item C<$data-E<gt>{branchname}>
691 The name (not the code) of the branch to which the book belongs.
693 =item C<$data-E<gt>{datelastseen}>
695 This is simply C<items.datelastseen>, except that while the date is
696 stored in YYYY-MM-DD format in the database, here it is converted to
697 DD/MM/YYYY format. A NULL date is returned as C<//>.
699 =item C<$data-E<gt>{datedue}>
701 =item C<$data-E<gt>{class}>
703 This is the concatenation of C<biblioitems.classification>, the book's
704 Dewey code, and C<biblioitems.subclass>.
706 =item C<$data-E<gt>{ocount}>
708 I think this is the number of copies of the book available.
710 =item C<$data-E<gt>{order}>
712 If this is set, it is set to C<One Order>.
719 my ( $biblionumber ) = @_;
720 my $dbh = C4::Context->dbh;
721 require C4::Languages;
722 my $language = C4::Languages::getlanguage();
728 biblioitems.itemtype,
731 biblioitems.publicationyear,
732 biblioitems.publishercode,
733 biblioitems.volumedate,
734 biblioitems.volumedesc,
737 items.notforloan as itemnotforloan,
738 issues.borrowernumber,
739 issues.date_due as datedue,
740 issues.onsite_checkout,
741 borrowers.cardnumber,
744 borrowers.branchcode as bcode,
746 serial.publisheddate,
747 itemtypes.description,
748 COALESCE( localization.translation, itemtypes.description ) AS translated_description,
749 itemtypes.notforloan as notforloan_per_itemtype,
753 holding.opac_info as holding_branch_opac_info,
754 home.opac_info as home_branch_opac_info,
755 IF(tmp_holdsqueue.itemnumber,1,0) AS has_pending_hold
757 LEFT JOIN branches AS holding ON items.holdingbranch = holding.branchcode
758 LEFT JOIN branches AS home ON items.homebranch=home.branchcode
759 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
760 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
761 LEFT JOIN issues USING (itemnumber)
762 LEFT JOIN borrowers USING (borrowernumber)
763 LEFT JOIN serialitems USING (itemnumber)
764 LEFT JOIN serial USING (serialid)
765 LEFT JOIN itemtypes ON itemtypes.itemtype = "
766 . (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype');
768 LEFT JOIN tmp_holdsqueue USING (itemnumber)
769 LEFT JOIN localization ON itemtypes.itemtype = localization.code
770 AND localization.entity = 'itemtypes'
771 AND localization.lang = ?
774 $query .= " WHERE items.biblionumber = ? ORDER BY home.branchname, items.enumchron, LPAD( items.copynumber, 8, '0' ), items.dateaccessioned DESC" ;
775 my $sth = $dbh->prepare($query);
776 $sth->execute($language, $biblionumber);
781 my $userenv = C4::Context->userenv;
782 my $want_not_same_branch = C4::Context->preference("IndependentBranches") && !C4::Context->IsSuperLibrarian();
783 while ( my $data = $sth->fetchrow_hashref ) {
784 if ( $data->{borrowernumber} && $want_not_same_branch) {
785 $data->{'NOTSAMEBRANCH'} = $data->{'bcode'} ne $userenv->{branch};
788 $serial ||= $data->{'serial'};
791 # get notforloan complete status if applicable
792 $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.notforloan', authorised_value => $data->{itemnotforloan} });
793 $data->{notforloanvalue} = $descriptions->{lib} // '';
794 $data->{notforloanvalueopac} = $descriptions->{opac_description} // '';
796 # get restricted status and description if applicable
797 $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.restricted', authorised_value => $data->{restricted} });
798 $data->{restrictedvalue} = $descriptions->{lib} // '';
799 $data->{restrictedvalueopac} = $descriptions->{opac_description} // '';
801 # my stack procedures
802 $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.stack', authorised_value => $data->{stack} });
803 $data->{stack} = $descriptions->{lib} // '';
805 # Find the last 3 people who borrowed this item.
806 my $sth2 = $dbh->prepare("SELECT * FROM old_issues,borrowers
808 AND old_issues.borrowernumber = borrowers.borrowernumber
809 ORDER BY returndate DESC
811 $sth2->execute($data->{'itemnumber'});
813 while (my $data2 = $sth2->fetchrow_hashref()) {
814 $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
815 $data->{"card$ii"} = $data2->{'cardnumber'} if $data2->{'cardnumber'};
816 $data->{"borrower$ii"} = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
820 $results[$i] = $data;
825 ? sort { ($b->{'publisheddate'} || $b->{'enumchron'} || "") cmp ($a->{'publisheddate'} || $a->{'enumchron'} || "") } @results
829 =head2 GetItemsLocationInfo
831 my @itemlocinfo = GetItemsLocationInfo($biblionumber);
833 Returns the branch names, shelving location and itemcallnumber for each item attached to the biblio in question
835 C<GetItemsInfo> returns a list of references-to-hash. Data returned:
839 =item C<$data-E<gt>{homebranch}>
841 Branch Name of the item's homebranch
843 =item C<$data-E<gt>{holdingbranch}>
845 Branch Name of the item's holdingbranch
847 =item C<$data-E<gt>{location}>
849 Item's shelving location code
851 =item C<$data-E<gt>{location_intranet}>
853 The intranet description for the Shelving Location as set in authorised_values 'LOC'
855 =item C<$data-E<gt>{location_opac}>
857 The OPAC description for the Shelving Location as set in authorised_values 'LOC'. Falls back to intranet description if no OPAC
860 =item C<$data-E<gt>{itemcallnumber}>
862 Item's itemcallnumber
864 =item C<$data-E<gt>{cn_sort}>
866 Item's call number normalized for sorting
872 sub GetItemsLocationInfo {
873 my $biblionumber = shift;
876 my $dbh = C4::Context->dbh;
877 my $query = "SELECT a.branchname as homebranch, b.branchname as holdingbranch,
878 location, itemcallnumber, cn_sort
879 FROM items, branches as a, branches as b
880 WHERE homebranch = a.branchcode AND holdingbranch = b.branchcode
882 ORDER BY cn_sort ASC";
883 my $sth = $dbh->prepare($query);
884 $sth->execute($biblionumber);
886 while ( my $data = $sth->fetchrow_hashref ) {
887 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $data->{location} });
888 $av = $av->count ? $av->next : undef;
889 $data->{location_intranet} = $av ? $av->lib : '';
890 $data->{location_opac} = $av ? $av->opac_description : '';
891 push @results, $data;
896 =head2 GetHostItemsInfo
898 $hostiteminfo = GetHostItemsInfo($hostfield);
899 Returns the iteminfo for items linked to records via a host field
903 sub GetHostItemsInfo {
907 if( !C4::Context->preference('EasyAnalyticalRecords') ) {
908 return @returnitemsInfo;
912 if( C4::Context->preference('marcflavour') eq 'MARC21' ) {
913 @fields = $record->field('773');
914 } elsif( C4::Context->preference('marcflavour') eq 'UNIMARC') {
915 @fields = $record->field('461');
918 foreach my $hostfield ( @fields ) {
919 my $hostbiblionumber = $hostfield->subfield("0");
920 my $linkeditemnumber = $hostfield->subfield("9");
921 my @hostitemInfos = GetItemsInfo($hostbiblionumber);
922 foreach my $hostitemInfo (@hostitemInfos) {
923 if( $hostitemInfo->{itemnumber} eq $linkeditemnumber ) {
924 push @returnitemsInfo, $hostitemInfo;
929 return @returnitemsInfo;
932 =head2 get_hostitemnumbers_of
934 my @itemnumbers_of = get_hostitemnumbers_of($biblionumber);
936 Given a biblionumber, return the list of corresponding itemnumbers that are linked to it via host fields
938 Return a reference on a hash where key is a biblionumber and values are
939 references on array of itemnumbers.
944 sub get_hostitemnumbers_of {
945 my ($biblionumber) = @_;
947 if( !C4::Context->preference('EasyAnalyticalRecords') ) {
951 my $biblio = Koha::Biblios->find($biblionumber);
952 my $marcrecord = $biblio->metadata->record;
953 return unless $marcrecord;
955 my ( @returnhostitemnumbers, $tag, $biblio_s, $item_s );
957 my $marcflavor = C4::Context->preference('marcflavour');
958 if ( $marcflavor eq 'MARC21' ) {
963 elsif ( $marcflavor eq 'UNIMARC' ) {
969 foreach my $hostfield ( $marcrecord->field($tag) ) {
970 my $hostbiblionumber = $hostfield->subfield($biblio_s);
971 next unless $hostbiblionumber; # have tag, don't have $biblio_s subfield
972 my $linkeditemnumber = $hostfield->subfield($item_s);
973 if ( ! $linkeditemnumber ) {
974 warn "ERROR biblionumber $biblionumber has 773^0, but doesn't have 9";
977 my $is_from_biblio = Koha::Items->search({ itemnumber => $linkeditemnumber, biblionumber => $hostbiblionumber });
978 push @returnhostitemnumbers, $linkeditemnumber
982 return @returnhostitemnumbers;
985 =head1 LIMITED USE FUNCTIONS
987 The following functions, while part of the public API,
988 are not exported. This is generally because they are
989 meant to be used by only one script for a specific
990 purpose, and should not be used in any other context
991 without careful thought.
997 my $item_marc = GetMarcItem($biblionumber, $itemnumber);
999 Returns MARC::Record of the item passed in parameter.
1000 This function is meant for use only in C<cataloguing/additem.pl>,
1001 where it is needed to support that script's MARC-like
1007 my ( $biblionumber, $itemnumber ) = @_;
1009 # GetMarcItem has been revised so that it does the following:
1010 # 1. Gets the item information from the items table.
1011 # 2. Converts it to a MARC field for storage in the bib record.
1013 # The previous behavior was:
1014 # 1. Get the bib record.
1015 # 2. Return the MARC tag corresponding to the item record.
1017 # The difference is that one treats the items row as authoritative,
1018 # while the other treats the MARC representation as authoritative
1019 # under certain circumstances.
1021 my $item = Koha::Items->find($itemnumber) or return;
1023 # Tack on 'items.' prefix to column names so that C4::Biblio::TransformKohaToMarc will work.
1024 # Also, don't emit a subfield if the underlying field is blank.
1026 return Item2Marc($item->unblessed, $biblionumber);
1030 my ($itemrecord,$biblionumber)=@_;
1033 defined($itemrecord->{$_}) && $itemrecord->{$_} ne '' ? ("items.$_" => $itemrecord->{$_}) : ()
1034 } keys %{ $itemrecord }
1036 my $framework = C4::Biblio::GetFrameworkCode( $biblionumber );
1037 my $itemmarc = C4::Biblio::TransformKohaToMarc( $mungeditem, { framework => $framework } );
1038 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField(
1039 "items.itemnumber", $framework,
1042 my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1043 if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1044 foreach my $field ($itemmarc->field($itemtag)){
1045 $field->add_subfields(@$unlinked_item_subfields);
1051 =head1 PRIVATE FUNCTIONS AND VARIABLES
1053 The following functions are not meant to be called
1054 directly, but are documented in order to explain
1055 the inner workings of C<C4::Items>.
1059 =head2 _marc_from_item_hash
1061 my $item_marc = _marc_from_item_hash($item, $frameworkcode[, $unlinked_item_subfields]);
1063 Given an item hash representing a complete item record,
1064 create a C<MARC::Record> object containing an embedded
1065 tag representing that item.
1067 The third, optional parameter C<$unlinked_item_subfields> is
1068 an arrayref of subfields (not mapped to C<items> fields per the
1069 framework) to be added to the MARC representation
1074 sub _marc_from_item_hash {
1076 my $frameworkcode = shift;
1077 my $unlinked_item_subfields;
1079 $unlinked_item_subfields = shift;
1082 # Tack on 'items.' prefix to column names so lookup from MARC frameworks will work
1083 # Also, don't emit a subfield if the underlying field is blank.
1084 my $mungeditem = { map { (defined($item->{$_}) and $item->{$_} ne '') ?
1085 (/^items\./ ? ($_ => $item->{$_}) : ("items.$_" => $item->{$_}))
1086 : () } keys %{ $item } };
1088 my $item_marc = MARC::Record->new();
1089 foreach my $item_field ( keys %{$mungeditem} ) {
1090 my ( $tag, $subfield ) = C4::Biblio::GetMarcFromKohaField( $item_field );
1091 next unless defined $tag and defined $subfield; # skip if not mapped to MARC field
1092 my @values = split(/\s?\|\s?/, $mungeditem->{$item_field}, -1);
1093 foreach my $value (@values){
1094 if ( my $field = $item_marc->field($tag) ) {
1095 $field->add_subfields( $subfield => $value );
1097 my $add_subfields = [];
1098 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
1099 $add_subfields = $unlinked_item_subfields;
1101 $item_marc->add_fields( $tag, " ", " ", $subfield => $value, @$add_subfields );
1109 =head2 _repack_item_errors
1111 Add an error message hash generated by C<CheckItemPreSave>
1112 to a list of errors.
1116 sub _repack_item_errors {
1117 my $item_sequence_num = shift;
1118 my $item_ref = shift;
1119 my $error_ref = shift;
1121 my @repacked_errors = ();
1123 foreach my $error_code (sort keys %{ $error_ref }) {
1124 my $repacked_error = {};
1125 $repacked_error->{'item_sequence'} = $item_sequence_num;
1126 $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ? $item_ref->{'barcode'} : '';
1127 $repacked_error->{'error_code'} = $error_code;
1128 $repacked_error->{'error_information'} = $error_ref->{$error_code};
1129 push @repacked_errors, $repacked_error;
1132 return @repacked_errors;
1135 =head2 _get_unlinked_item_subfields
1137 my $unlinked_item_subfields = _get_unlinked_item_subfields($original_item_marc, $frameworkcode);
1141 sub _get_unlinked_item_subfields {
1142 my $original_item_marc = shift;
1143 my $frameworkcode = shift;
1145 my $marcstructure = C4::Biblio::GetMarcStructure(1, $frameworkcode, { unsafe => 1 });
1147 # assume that this record has only one field, and that that
1148 # field contains only the item information
1150 my @fields = $original_item_marc->fields();
1151 if ($#fields > -1) {
1152 my $field = $fields[0];
1153 my $tag = $field->tag();
1154 foreach my $subfield ($field->subfields()) {
1155 if (defined $subfield->[1] and
1156 $subfield->[1] ne '' and
1157 !$marcstructure->{$tag}->{$subfield->[0]}->{'kohafield'}) {
1158 push @$subfields, $subfield->[0] => $subfield->[1];
1165 =head2 _get_unlinked_subfields_xml
1167 my $unlinked_subfields_xml = _get_unlinked_subfields_xml($unlinked_item_subfields);
1171 sub _get_unlinked_subfields_xml {
1172 my $unlinked_item_subfields = shift;
1175 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
1176 my $marc = MARC::Record->new();
1177 # use of tag 999 is arbitrary, and doesn't need to match the item tag
1178 # used in the framework
1179 $marc->append_fields(MARC::Field->new('999', ' ', ' ', @$unlinked_item_subfields));
1180 $marc->encoding("UTF-8");
1181 $xml = $marc->as_xml("USMARC");
1187 =head2 _parse_unlinked_item_subfields_from_xml
1189 my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'}):
1193 sub _parse_unlinked_item_subfields_from_xml {
1195 require C4::Charset;
1196 return unless defined $xml and $xml ne "";
1197 my $marc = MARC::Record->new_from_xml(C4::Charset::StripNonXmlChars($xml),'UTF-8');
1198 my $unlinked_subfields = [];
1199 my @fields = $marc->fields();
1200 if ($#fields > -1) {
1201 foreach my $subfield ($fields[0]->subfields()) {
1202 push @$unlinked_subfields, $subfield->[0] => $subfield->[1];
1205 return $unlinked_subfields;
1208 =head2 GetAnalyticsCount
1210 $count= &GetAnalyticsCount($itemnumber)
1212 counts Usage of itemnumber in Analytical bibliorecords.
1216 sub GetAnalyticsCount {
1217 my ($itemnumber) = @_;
1219 if ( !C4::Context->preference('EasyAnalyticalRecords') ) {
1223 ### ZOOM search here
1225 $query= "hi=".$itemnumber;
1226 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
1227 my ($err,$res,$result) = $searcher->simple_search_compat($query,0,10);
1231 sub _SearchItems_build_where_fragment {
1234 my $dbh = C4::Context->dbh;
1237 if (exists($filter->{conjunction})) {
1238 my (@where_strs, @where_args);
1239 foreach my $f (@{ $filter->{filters} }) {
1240 my $fragment = _SearchItems_build_where_fragment($f);
1242 push @where_strs, $fragment->{str};
1243 push @where_args, @{ $fragment->{args} };
1248 $where_str = '(' . join (' ' . $filter->{conjunction} . ' ', @where_strs) . ')';
1251 args => \@where_args,
1255 my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns;
1256 push @columns, Koha::Database->new()->schema()->resultset('Biblio')->result_source->columns;
1257 push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
1258 push @columns, Koha::Database->new()->schema()->resultset('Issue')->result_source->columns;
1259 my @operators = qw(= != > < >= <= is like);
1260 push @operators, 'not like';
1261 my $field = $filter->{field} // q{};
1262 if ( (0 < grep { $_ eq $field } @columns) or (substr($field, 0, 5) eq 'marc:') ) {
1263 my $op = $filter->{operator};
1264 my $query = $filter->{query};
1265 my $ifnull = $filter->{ifnull};
1267 if (!$op or (0 == grep { $_ eq $op } @operators)) {
1268 $op = '='; # default operator
1272 if ($field =~ /^marc:(\d{3})(?:\$(\w))?$/) {
1274 my $marcsubfield = $2;
1275 my ($kohafield) = $dbh->selectrow_array(q|
1276 SELECT kohafield FROM marc_subfield_structure
1277 WHERE tagfield=? AND tagsubfield=? AND frameworkcode=''
1278 |, undef, $marcfield, $marcsubfield);
1281 $column = $kohafield;
1283 # MARC field is not linked to a DB field so we need to use
1284 # ExtractValue on marcxml from biblio_metadata or
1285 # items.more_subfields_xml, depending on the MARC field.
1288 my ($itemfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber');
1289 if ($marcfield eq $itemfield) {
1290 $sqlfield = 'more_subfields_xml';
1291 $xpath = '//record/datafield/subfield[@code="' . $marcsubfield . '"]';
1293 $sqlfield = 'metadata'; # From biblio_metadata
1294 if ($marcfield < 10) {
1295 $xpath = "//record/controlfield[\@tag=\"$marcfield\"]";
1297 $xpath = "//record/datafield[\@tag=\"$marcfield\"]/subfield[\@code=\"$marcsubfield\"]";
1300 $column = "ExtractValue($sqlfield, '$xpath')";
1306 if ( defined $ifnull ) {
1307 $column = "COALESCE($column, ?)";
1310 if (ref $query eq 'ARRAY') {
1313 } elsif ($op eq '!=') {
1317 str => "$column $op (" . join (',', ('?') x @$query) . ")",
1320 } elsif ( $op eq 'is' ) {
1322 str => "$column $op $query",
1327 str => "$column $op ?",
1332 if ( defined $ifnull ) {
1333 unshift @{ $where_fragment->{args} }, $ifnull;
1338 return $where_fragment;
1343 my ($items, $total) = SearchItems($filter, $params);
1345 Perform a search among items
1347 $filter is a reference to a hash which can be a filter, or a combination of filters.
1349 A filter has the following keys:
1353 =item * field: the name of a SQL column in table items
1355 =item * query: the value to search in this column
1357 =item * operator: comparison operator. Can be one of = != > < >= <= like 'not like' is
1361 A combination of filters hash the following keys:
1365 =item * conjunction: 'AND' or 'OR'
1367 =item * filters: array ref of filters
1371 $params is a reference to a hash that can contain the following parameters:
1375 =item * rows: Number of items to return. 0 returns everything (default: 0)
1377 =item * page: Page to return (return items from (page-1)*rows to (page*rows)-1)
1380 =item * sortby: A SQL column name in items table to sort on
1382 =item * sortorder: 'ASC' or 'DESC'
1389 my ($filter, $params) = @_;
1393 return unless ref $filter eq 'HASH';
1394 return unless ref $params eq 'HASH';
1396 # Default parameters
1397 $params->{rows} ||= 0;
1398 $params->{page} ||= 1;
1399 $params->{sortby} ||= 'itemnumber';
1400 $params->{sortorder} ||= 'ASC';
1402 my ($where_str, @where_args);
1403 my $where_fragment = _SearchItems_build_where_fragment($filter);
1404 if ($where_fragment) {
1405 $where_str = $where_fragment->{str};
1406 @where_args = @{ $where_fragment->{args} };
1409 my $dbh = C4::Context->dbh;
1411 SELECT SQL_CALC_FOUND_ROWS items.*
1413 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1414 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1415 LEFT JOIN biblio_metadata ON biblio_metadata.biblionumber = biblio.biblionumber
1416 LEFT JOIN issues ON issues.itemnumber = items.itemnumber
1419 if (defined $where_str and $where_str ne '') {
1420 $query .= qq{ AND $where_str };
1423 $query .= q{ AND biblio_metadata.format = 'marcxml' AND biblio_metadata.schema = ? };
1424 push @where_args, C4::Context->preference('marcflavour');
1426 my @columns = Koha::Database->new()->schema()->resultset('Item')->result_source->columns;
1427 push @columns, Koha::Database->new()->schema()->resultset('Biblio')->result_source->columns;
1428 push @columns, Koha::Database->new()->schema()->resultset('Biblioitem')->result_source->columns;
1429 push @columns, Koha::Database->new()->schema()->resultset('Issue')->result_source->columns;
1431 if ( $params->{sortby} eq 'availability' ) {
1432 my $sortorder = (uc($params->{sortorder}) eq 'ASC') ? 'ASC' : 'DESC';
1433 $query .= qq{ ORDER BY onloan $sortorder };
1435 my $sortby = (0 < grep {$params->{sortby} eq $_} @columns)
1436 ? $params->{sortby} : 'itemnumber';
1437 my $sortorder = (uc($params->{sortorder}) eq 'ASC') ? 'ASC' : 'DESC';
1438 $query .= qq{ ORDER BY $sortby $sortorder };
1441 my $rows = $params->{rows};
1444 my $offset = $rows * ($params->{page}-1);
1445 $query .= qq { LIMIT ?, ? };
1446 push @limit_args, $offset, $rows;
1449 my $sth = $dbh->prepare($query);
1450 my $rv = $sth->execute(@where_args, @limit_args);
1452 return unless ($rv);
1453 my ($total_rows) = $dbh->selectrow_array(q{ SELECT FOUND_ROWS() });
1455 return ($sth->fetchall_arrayref({}), $total_rows);
1459 =head1 OTHER FUNCTIONS
1463 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
1465 Find the given $subfield in the given $tag in the given
1466 MARC::Record $record. If the subfield is found, returns
1467 the (indicators, value) pair; otherwise, (undef, undef) is
1471 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
1472 I suggest we export it from this module.
1477 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
1480 if ( $tagfield < 10 ) {
1481 if ( $record->field($tagfield) ) {
1482 push @result, $record->field($tagfield)->data();
1487 foreach my $field ( $record->field($tagfield) ) {
1488 my @subfields = $field->subfields();
1489 foreach my $subfield (@subfields) {
1490 if ( @$subfield[0] eq $insubfield ) {
1491 push @result, @$subfield[1];
1492 $indicator = $field->indicator(1) . $field->indicator(2);
1497 return ( $indicator, @result );
1501 =head2 PrepareItemrecordDisplay
1503 PrepareItemrecordDisplay($bibnum,$itemumber,$defaultvalues,$frameworkcode);
1505 Returns a hash with all the fields for Display a given item data in a template
1507 $defaultvalues should either contain a hashref of values for the new item, or be undefined.
1509 The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
1513 sub PrepareItemrecordDisplay {
1515 my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode ) = @_;
1517 my $dbh = C4::Context->dbh;
1518 $frameworkcode = C4::Biblio::GetFrameworkCode($bibnum) if $bibnum;
1519 my ( $itemtagfield, $itemtagsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1521 # Note: $tagslib obtained from GetMarcStructure() in 'unsafe' mode is
1522 # a shared data structure. No plugin (including custom ones) should change
1523 # its contents. See also GetMarcStructure.
1524 my $tagslib = GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } );
1526 # Pick the default location from NewItemsDefaultLocation
1527 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
1528 $defaultvalues //= {};
1529 $defaultvalues->{location} //= C4::Context->preference('NewItemsDefaultLocation');
1532 # return nothing if we don't have found an existing framework.
1533 return q{} unless $tagslib;
1536 $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum );
1540 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
1542 SELECT authorised_value,lib FROM authorised_values
1545 LEFT JOIN authorised_values_branches ON ( id = av_id )
1550 $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL )} if $branch_limit;
1551 $query .= qq{ ORDER BY lib};
1552 my $authorised_values_sth = $dbh->prepare( $query );
1553 foreach my $tag ( sort keys %{$tagslib} ) {
1556 # loop through each subfield
1558 foreach my $subfield (
1559 sort { $a->{display_order} <=> $b->{display_order} || $a->{subfield} cmp $b->{subfield} }
1560 grep { ref($_) && %$_ } # Not a subfield (values for "important", "lib", "mandatory", etc.) or empty
1561 values %{ $tagslib->{$tag} } )
1563 next unless ( $subfield->{'tab'} );
1564 next if ( $subfield->{'tab'} ne "10" );
1566 $subfield_data{tag} = $tag;
1567 $subfield_data{subfield} = $subfield->{subfield};
1568 $subfield_data{countsubfield} = $cntsubf++;
1569 $subfield_data{kohafield} = $subfield->{kohafield};
1570 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield->{subfield}."_".int(rand(1000000));
1572 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
1573 $subfield_data{marc_lib} = $subfield->{lib};
1574 $subfield_data{mandatory} = $subfield->{mandatory};
1575 $subfield_data{repeatable} = $subfield->{repeatable};
1576 $subfield_data{hidden} = "display:none"
1577 if ( ( $subfield->{hidden} > 4 )
1578 || ( $subfield->{hidden} < -4 ) );
1579 my ( $x, $defaultvalue );
1581 ( $x, $defaultvalue ) = _find_value( $tag, $subfield->{subfield}, $itemrecord );
1583 $defaultvalue = $subfield->{defaultvalue} unless $defaultvalue;
1584 if ( !defined $defaultvalue ) {
1585 $defaultvalue = q||;
1587 $defaultvalue =~ s/"/"/g;
1588 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
1589 my $today_dt = dt_from_string;
1590 my $year = $today_dt->strftime('%Y');
1591 my $shortyear = $today_dt->strftime('%y');
1592 my $month = $today_dt->strftime('%m');
1593 my $day = $today_dt->strftime('%d');
1594 $defaultvalue =~ s/<<YYYY>>/$year/g;
1595 $defaultvalue =~ s/<<YY>>/$shortyear/g;
1596 $defaultvalue =~ s/<<MM>>/$month/g;
1597 $defaultvalue =~ s/<<DD>>/$day/g;
1599 # And <<USER>> with surname (?)
1601 ( C4::Context->userenv
1602 ? C4::Context->userenv->{'surname'}
1603 : "superlibrarian" );
1604 $defaultvalue =~ s/<<USER>>/$username/g;
1607 my $maxlength = $subfield->{maxlength};
1609 # search for itemcallnumber if applicable
1610 if ( $subfield->{kohafield} eq 'items.itemcallnumber'
1611 && C4::Context->preference('itemcallnumber') && $itemrecord) {
1612 foreach my $itemcn_pref (split(/,/,C4::Context->preference('itemcallnumber'))){
1613 my $CNtag = substr( $itemcn_pref, 0, 3 );
1614 next unless my $field = $itemrecord->field($CNtag);
1615 my $CNsubfields = substr( $itemcn_pref, 3 );
1616 $CNsubfields = undef if $CNsubfields eq '';
1617 $defaultvalue = $field->as_string( $CNsubfields, ' ');
1618 last if $defaultvalue;
1621 if ( $subfield->{kohafield} eq 'items.itemcallnumber'
1623 && $defaultvalues->{'callnumber'} ) {
1624 if( $itemrecord and $defaultvalues and not $itemrecord->subfield($tag,$subfield->{subfield}) ){
1625 # if the item record exists, only use default value if the item has no callnumber
1626 $defaultvalue = $defaultvalues->{callnumber};
1627 } elsif ( !$itemrecord and $defaultvalues ) {
1628 # if the item record *doesn't* exists, always use the default value
1629 $defaultvalue = $defaultvalues->{callnumber};
1632 if ( ( $subfield->{kohafield} eq 'items.holdingbranch' || $subfield->{kohafield} eq 'items.homebranch' )
1634 && $defaultvalues->{'branchcode'} ) {
1635 if ( $itemrecord and $defaultvalues and not $itemrecord->subfield($tag,$subfield) ) {
1636 $defaultvalue = $defaultvalues->{branchcode};
1639 if ( ( $subfield->{kohafield} eq 'items.location' )
1641 && $defaultvalues->{'location'} ) {
1643 if ( $itemrecord and $defaultvalues and not $itemrecord->subfield($tag,$subfield->{subfield}) ) {
1644 # if the item record exists, only use default value if the item has no locationr
1645 $defaultvalue = $defaultvalues->{location};
1646 } elsif ( !$itemrecord and $defaultvalues ) {
1647 # if the item record *doesn't* exists, always use the default value
1648 $defaultvalue = $defaultvalues->{location};
1651 if ( ( $subfield->{kohafield} eq 'items.ccode' )
1653 && $defaultvalues->{'ccode'} ) {
1655 if ( !$itemrecord and $defaultvalues ) {
1656 # if the item record *doesn't* exists, always use the default value
1657 $defaultvalue = $defaultvalues->{ccode};
1660 if ( $subfield->{authorised_value} ) {
1661 my @authorised_values;
1664 # builds list, depending on authorised value...
1666 if ( $subfield->{'authorised_value'} eq "branches" ) {
1667 if ( ( C4::Context->preference("IndependentBranches") )
1668 && !C4::Context->IsSuperLibrarian() ) {
1669 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname" );
1670 $sth->execute( C4::Context->userenv->{branch} );
1671 push @authorised_values, ""
1672 unless ( $subfield->{mandatory} );
1673 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
1674 push @authorised_values, $branchcode;
1675 $authorised_lib{$branchcode} = $branchname;
1678 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches ORDER BY branchname" );
1680 push @authorised_values, ""
1681 unless ( $subfield->{mandatory} );
1682 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
1683 push @authorised_values, $branchcode;
1684 $authorised_lib{$branchcode} = $branchname;
1688 $defaultvalue = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
1689 if ( $defaultvalues and $defaultvalues->{branchcode} ) {
1690 $defaultvalue = $defaultvalues->{branchcode};
1694 } elsif ( $subfield->{authorised_value} eq "itemtypes" ) {
1695 my $itemtypes = Koha::ItemTypes->search_with_localization;
1696 push @authorised_values, "";
1697 while ( my $itemtype = $itemtypes->next ) {
1698 push @authorised_values, $itemtype->itemtype;
1699 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
1701 if ($defaultvalues && $defaultvalues->{'itemtype'}) {
1702 $defaultvalue = $defaultvalues->{'itemtype'};
1706 } elsif ( $subfield->{authorised_value} eq "cn_source" ) {
1707 push @authorised_values, "";
1709 my $class_sources = GetClassSources();
1710 my $default_source = $defaultvalue || C4::Context->preference("DefaultClassificationSource");
1712 foreach my $class_source (sort keys %$class_sources) {
1713 next unless $class_sources->{$class_source}->{'used'} or
1714 ($class_source eq $default_source);
1715 push @authorised_values, $class_source;
1716 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
1719 $defaultvalue = $default_source;
1721 #---- "true" authorised value
1723 $authorised_values_sth->execute(
1724 $subfield->{authorised_value},
1725 $branch_limit ? $branch_limit : ()
1727 push @authorised_values, "";
1728 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
1729 push @authorised_values, $value;
1730 $authorised_lib{$value} = $lib;
1733 $subfield_data{marc_value} = {
1735 values => \@authorised_values,
1736 default => $defaultvalue // q{},
1737 labels => \%authorised_lib,
1739 } elsif ( $subfield->{value_builder} ) {
1741 require Koha::FrameworkPlugin;
1742 my $plugin = Koha::FrameworkPlugin->new({
1743 name => $subfield->{value_builder},
1746 my $pars = { dbh => $dbh, record => undef, tagslib =>$tagslib, id => $subfield_data{id} };
1747 $plugin->build( $pars );
1748 if ( $itemrecord and my $field = $itemrecord->field($tag) ) {
1749 $defaultvalue = $field->subfield($subfield->{subfield}) || q{};
1751 if( !$plugin->errstr ) {
1752 #TODO Move html to template; see report 12176/13397
1753 my $tab= $plugin->noclick? '-1': '';
1754 my $class= $plugin->noclick? ' disabled': '';
1755 my $title= $plugin->noclick? 'No popup': 'Tag editor';
1756 $subfield_data{marc_value} = qq[<input type="text" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$maxlength" value="$defaultvalue" /><a href="#" id="buttonDot_$subfield_data{id}" class="buttonDot $class" title="$title">...</a>\n].$plugin->javascript;
1758 warn $plugin->errstr;
1759 $subfield_data{marc_value} = qq(<input type="text" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$maxlength" value="$defaultvalue" />); # supply default input form
1762 elsif ( $tag eq '' ) { # it's an hidden field
1763 $subfield_data{marc_value} = qq(<input type="hidden" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$maxlength" value="$defaultvalue" />);
1765 elsif ( $subfield->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
1766 $subfield_data{marc_value} = qq(<input type="text" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$maxlength" value="$defaultvalue" />);
1768 elsif ( length($defaultvalue) > 100
1769 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
1770 300 <= $tag && $tag < 400 && $subfield->{subfield} eq 'a' )
1771 or (C4::Context->preference("marcflavour") eq "MARC21" and
1772 500 <= $tag && $tag < 600 )
1774 # oversize field (textarea)
1775 $subfield_data{marc_value} = qq(<textarea id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$maxlength">$defaultvalue</textarea>\n");
1777 $subfield_data{marc_value} = qq(<input type="text" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$maxlength" value="$defaultvalue" />);
1779 push( @loop_data, \%subfield_data );
1784 if ( $itemrecord && $itemrecord->field($itemtagfield) ) {
1785 $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield );
1788 'itemtagfield' => $itemtagfield,
1789 'itemtagsubfield' => $itemtagsubfield,
1790 'itemnumber' => $itemnumber,
1791 'iteminformation' => \@loop_data
1795 sub ToggleNewStatus {
1796 my ( $params ) = @_;
1797 my @rules = @{ $params->{rules} };
1798 my $report_only = $params->{report_only};
1800 my $dbh = C4::Context->dbh;
1802 my @item_columns = map { "items.$_" } Koha::Items->columns;
1803 my @biblioitem_columns = map { "biblioitems.$_" } Koha::Biblioitems->columns;
1805 for my $rule ( @rules ) {
1806 my $age = $rule->{age};
1807 # Default to using items.dateaccessioned if there's an old item modification rule
1808 # missing an agefield value
1809 my $agefield = $rule->{agefield} ? $rule->{agefield} : 'items.dateaccessioned';
1810 my $conditions = $rule->{conditions};
1811 my $substitutions = $rule->{substitutions};
1812 foreach ( @$substitutions ) {
1813 ( $_->{item_field} ) = ( $_->{field} =~ /items\.(.*)/ );
1820 LEFT JOIN biblioitems ON biblioitems.biblionumber = items.biblionumber
1823 for my $condition ( @$conditions ) {
1824 next unless $condition->{field};
1826 grep { $_ eq $condition->{field} } @item_columns
1827 or grep { $_ eq $condition->{field} } @biblioitem_columns
1829 if ( $condition->{value} =~ /\|/ ) {
1830 my @values = split /\|/, $condition->{value};
1831 $query .= qq| AND $condition->{field} IN (|
1832 . join( ',', ('?') x scalar @values )
1834 push @params, @values;
1836 $query .= qq| AND $condition->{field} = ?|;
1837 push @params, $condition->{value};
1841 if ( defined $age ) {
1842 $query .= qq| AND TO_DAYS(NOW()) - TO_DAYS($agefield) >= ? |;
1845 my $sth = $dbh->prepare($query);
1846 $sth->execute( @params );
1847 while ( my $values = $sth->fetchrow_hashref ) {
1848 my $biblionumber = $values->{biblionumber};
1849 my $itemnumber = $values->{itemnumber};
1850 my $item = Koha::Items->find($itemnumber);
1851 for my $substitution ( @$substitutions ) {
1852 my $field = $substitution->{item_field};
1853 my $value = $substitution->{value};
1854 next unless $substitution->{field};
1855 next if ( defined $values->{ $substitution->{item_field} } and $values->{ $substitution->{item_field} } eq $substitution->{value} );
1856 $item->$field($value);
1857 push @{ $report->{$itemnumber} }, $substitution;
1859 $item->store unless $report_only;