-=head2 GetItemsInfo
-
- @results = GetItemsInfo($biblionumber);
-
-Returns information about items with the given biblionumber.
-
-C<GetItemsInfo> returns a list of references-to-hash. Each element
-contains a number of keys. Most of them are attributes from the
-C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
-Koha database. Other keys include:
-
-=over 2
-
-=item C<$data-E<gt>{branchname}>
-
-The name (not the code) of the branch to which the book belongs.
-
-=item C<$data-E<gt>{datelastseen}>
-
-This is simply C<items.datelastseen>, except that while the date is
-stored in YYYY-MM-DD format in the database, here it is converted to
-DD/MM/YYYY format. A NULL date is returned as C<//>.
-
-=item C<$data-E<gt>{datedue}>
-
-=item C<$data-E<gt>{class}>
-
-This is the concatenation of C<biblioitems.classification>, the book's
-Dewey code, and C<biblioitems.subclass>.
-
-=item C<$data-E<gt>{ocount}>
-
-I think this is the number of copies of the book available.
-
-=item C<$data-E<gt>{order}>
-
-If this is set, it is set to C<One Order>.
-
-=back
-
-=cut
-
-sub GetItemsInfo {
- my ( $biblionumber ) = @_;
- my $dbh = C4::Context->dbh;
- require C4::Languages;
- my $language = C4::Languages::getlanguage();
- my $query = "
- SELECT items.*,
- biblio.*,
- biblioitems.volume,
- biblioitems.number,
- biblioitems.itemtype,
- biblioitems.isbn,
- biblioitems.issn,
- biblioitems.publicationyear,
- biblioitems.publishercode,
- biblioitems.volumedate,
- biblioitems.volumedesc,
- biblioitems.lccn,
- biblioitems.url,
- items.notforloan as itemnotforloan,
- issues.borrowernumber,
- issues.date_due as datedue,
- issues.onsite_checkout,
- borrowers.cardnumber,
- borrowers.surname,
- borrowers.firstname,
- borrowers.branchcode as bcode,
- serial.serialseq,
- serial.publisheddate,
- itemtypes.description,
- COALESCE( localization.translation, itemtypes.description ) AS translated_description,
- itemtypes.notforloan as notforloan_per_itemtype,
- holding.branchurl,
- holding.branchcode,
- holding.branchname,
- holding.opac_info as holding_branch_opac_info,
- home.opac_info as home_branch_opac_info,
- IF(tmp_holdsqueue.itemnumber,1,0) AS has_pending_hold
- FROM items
- LEFT JOIN branches AS holding ON items.holdingbranch = holding.branchcode
- LEFT JOIN branches AS home ON items.homebranch=home.branchcode
- LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
- LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
- LEFT JOIN issues USING (itemnumber)
- LEFT JOIN borrowers USING (borrowernumber)
- LEFT JOIN serialitems USING (itemnumber)
- LEFT JOIN serial USING (serialid)
- LEFT JOIN itemtypes ON itemtypes.itemtype = "
- . (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype');
- $query .= q|
- LEFT JOIN tmp_holdsqueue USING (itemnumber)
- LEFT JOIN localization ON itemtypes.itemtype = localization.code
- AND localization.entity = 'itemtypes'
- AND localization.lang = ?
- |;
-
- $query .= " WHERE items.biblionumber = ? ORDER BY home.branchname, items.enumchron, LPAD( items.copynumber, 8, '0' ), items.dateaccessioned DESC" ;
- my $sth = $dbh->prepare($query);
- $sth->execute($language, $biblionumber);
- my $i = 0;
- my @results;
- my $serial;
-
- my $userenv = C4::Context->userenv;
- my $want_not_same_branch = C4::Context->preference("IndependentBranches") && !C4::Context->IsSuperLibrarian();
- while ( my $data = $sth->fetchrow_hashref ) {
- if ( $data->{borrowernumber} && $want_not_same_branch) {
- $data->{'NOTSAMEBRANCH'} = $data->{'bcode'} ne $userenv->{branch};
- }
-
- $serial ||= $data->{'serial'};
-
- my $descriptions;
- # get notforloan complete status if applicable
- $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.notforloan', authorised_value => $data->{itemnotforloan} });
- $data->{notforloanvalue} = $descriptions->{lib} // '';
- $data->{notforloanvalueopac} = $descriptions->{opac_description} // '';
-
- # get restricted status and description if applicable
- $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.restricted', authorised_value => $data->{restricted} });
- $data->{restrictedvalue} = $descriptions->{lib} // '';
- $data->{restrictedvalueopac} = $descriptions->{opac_description} // '';
-
- # my stack procedures
- $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.stack', authorised_value => $data->{stack} });
- $data->{stack} = $descriptions->{lib} // '';
-
- # Find the last 3 people who borrowed this item.
- my $sth2 = $dbh->prepare("SELECT * FROM old_issues,borrowers
- WHERE itemnumber = ?
- AND old_issues.borrowernumber = borrowers.borrowernumber
- ORDER BY returndate DESC
- LIMIT 3");
- $sth2->execute($data->{'itemnumber'});
- my $ii = 0;
- while (my $data2 = $sth2->fetchrow_hashref()) {
- $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
- $data->{"card$ii"} = $data2->{'cardnumber'} if $data2->{'cardnumber'};
- $data->{"borrower$ii"} = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
- $ii++;
- }
-
- $results[$i] = $data;
- $i++;
- }
-
- return $serial
- ? sort { ($b->{'publisheddate'} || $b->{'enumchron'}) cmp ($a->{'publisheddate'} || $a->{'enumchron'}) } @results
- : @results;
-}
-
-=head2 GetItemsLocationInfo
-
- my @itemlocinfo = GetItemsLocationInfo($biblionumber);
-
-Returns the branch names, shelving location and itemcallnumber for each item attached to the biblio in question
-
-C<GetItemsInfo> returns a list of references-to-hash. Data returned:
-
-=over 2
-
-=item C<$data-E<gt>{homebranch}>
-
-Branch Name of the item's homebranch
-
-=item C<$data-E<gt>{holdingbranch}>
-
-Branch Name of the item's holdingbranch
-
-=item C<$data-E<gt>{location}>
-
-Item's shelving location code
-
-=item C<$data-E<gt>{location_intranet}>
-
-The intranet description for the Shelving Location as set in authorised_values 'LOC'
-
-=item C<$data-E<gt>{location_opac}>
-
-The OPAC description for the Shelving Location as set in authorised_values 'LOC'. Falls back to intranet description if no OPAC
-description is set.
-
-=item C<$data-E<gt>{itemcallnumber}>
-
-Item's itemcallnumber
-
-=item C<$data-E<gt>{cn_sort}>
-
-Item's call number normalized for sorting
-
-=back
-
-=cut
-
-sub GetItemsLocationInfo {
- my $biblionumber = shift;
- my @results;
-
- my $dbh = C4::Context->dbh;
- my $query = "SELECT a.branchname as homebranch, b.branchname as holdingbranch,
- location, itemcallnumber, cn_sort
- FROM items, branches as a, branches as b
- WHERE homebranch = a.branchcode AND holdingbranch = b.branchcode
- AND biblionumber = ?
- ORDER BY cn_sort ASC";
- my $sth = $dbh->prepare($query);
- $sth->execute($biblionumber);
-
- while ( my $data = $sth->fetchrow_hashref ) {
- my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $data->{location} });
- $av = $av->count ? $av->next : undef;
- $data->{location_intranet} = $av ? $av->lib : '';
- $data->{location_opac} = $av ? $av->opac_description : '';
- push @results, $data;
- }
- return @results;
-}
-
-=head2 GetHostItemsInfo
-
- $hostiteminfo = GetHostItemsInfo($hostfield);
- Returns the iteminfo for items linked to records via a host field
-
-=cut
-
-sub GetHostItemsInfo {
- my ($record) = @_;
- my @returnitemsInfo;
-
- if( !C4::Context->preference('EasyAnalyticalRecords') ) {
- return @returnitemsInfo;
- }
-
- my @fields;
- if( C4::Context->preference('marcflavour') eq 'MARC21' ||
- C4::Context->preference('marcflavour') eq 'NORMARC') {
- @fields = $record->field('773');
- } elsif( C4::Context->preference('marcflavour') eq 'UNIMARC') {
- @fields = $record->field('461');
- }
-
- foreach my $hostfield ( @fields ) {
- my $hostbiblionumber = $hostfield->subfield("0");
- my $linkeditemnumber = $hostfield->subfield("9");
- my @hostitemInfos = GetItemsInfo($hostbiblionumber);
- foreach my $hostitemInfo (@hostitemInfos) {
- if( $hostitemInfo->{itemnumber} eq $linkeditemnumber ) {
- push @returnitemsInfo, $hostitemInfo;
- last;
- }
- }
- }
- return @returnitemsInfo;
-}
-