+=head2 DelItem
+
+=over 4
+
+DelItem($biblionumber, $itemnumber);
+
+=back
+
+Exported function (core API) for deleting an item record in Koha.
+
+=cut
+
+sub DelItem {
+ my ( $dbh, $biblionumber, $itemnumber ) = @_;
+
+ # FIXME check the item has no current issues
+
+ _koha_delete_item( $dbh, $itemnumber );
+
+ # get the MARC record
+ my $record = GetMarcBiblio($biblionumber);
+ my $frameworkcode = GetFrameworkCode($biblionumber);
+
+ # backup the record
+ my $copy2deleted = $dbh->prepare("UPDATE deleteditems SET marc=? WHERE itemnumber=?");
+ $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
+
+ #search item field code
+ my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
+ my @fields = $record->field($itemtag);
+
+ # delete the item specified
+ foreach my $field (@fields) {
+ if ( $field->subfield($itemsubfield) eq $itemnumber ) {
+ $record->delete_field($field);
+ }
+ }
+ &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
+ logaction("CATALOGUING", "DELETE", $itemnumber, "item") if C4::Context->preference("CataloguingLog");
+}
+
+=head2 DelItemCheck
+
+=over 4
+
+DelItemCheck($dbh, $biblionumber, $itemnumber);
+
+=back
+
+Exported function (core API) for deleting an item record in Koha if there no current issue.
+
+=cut
+
+sub DelItemCheck {
+ my ( $dbh, $biblionumber, $itemnumber ) = @_;
+ my $error;
+
+ # check that there is no issue on this item before deletion.
+ my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
+ $sth->execute($itemnumber);
+
+ my $onloan=$sth->fetchrow;
+
+ $sth->finish();
+ if ($onloan){
+ $error = "book_on_loan"
+ }else{
+ # check it doesnt have a waiting reserve
+ $sth=$dbh->prepare("SELECT * FROM reserves WHERE found = 'W' AND itemnumber = ?");
+ $sth->execute($itemnumber);
+ my $reserve=$sth->fetchrow;
+ $sth->finish();
+ if ($reserve){
+ $error = "book_reserved";
+ }else{
+ DelItem($dbh, $biblionumber, $itemnumber);
+ return 1;
+ }
+ }
+ return $error;
+}
+
+=head2 CheckItemPreSave
+
+=over 4
+
+ my $item_ref = TransformMarcToKoha($marc, 'items');
+ # do stuff
+ my %errors = CheckItemPreSave($item_ref);
+ if (exists $errors{'duplicate_barcode'}) {
+ print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
+ } elsif (exists $errors{'invalid_homebranch'}) {
+ print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
+ } elsif (exists $errors{'invalid_holdingbranch'}) {
+ print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
+ } else {
+ print "item is OK";
+ }
+
+=back
+
+Given a hashref containing item fields, determine if it can be
+inserted or updated in the database. Specifically, checks for
+database integrity issues, and returns a hash containing any
+of the following keys, if applicable.
+
+=over 2
+
+=item duplicate_barcode
+
+Barcode, if it duplicates one already found in the database.
+
+=item invalid_homebranch
+
+Home branch, if not defined in branches table.
+
+=item invalid_holdingbranch
+
+Holding branch, if not defined in branches table.
+
+=back
+
+This function does NOT implement any policy-related checks,
+e.g., whether current operator is allowed to save an
+item that has a given branch code.
+
+=cut
+
+sub CheckItemPreSave {
+ my $item_ref = shift;
+
+ my %errors = ();
+
+ # check for duplicate barcode
+ if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
+ my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'});
+ if ($existing_itemnumber) {
+ if (!exists $item_ref->{'itemnumber'} # new item
+ or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
+ $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
+ }
+ }
+ }
+
+ # check for valid home branch
+ if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
+ my $branch_name = GetBranchName($item_ref->{'homebranch'});
+ unless (defined $branch_name) {
+ # relies on fact that branches.branchname is a non-NULL column,
+ # so GetBranchName returns undef only if branch does not exist
+ $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
+ }
+ }
+
+ # check for valid holding branch
+ if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
+ my $branch_name = GetBranchName($item_ref->{'holdingbranch'});
+ unless (defined $branch_name) {
+ # relies on fact that branches.branchname is a non-NULL column,
+ # so GetBranchName returns undef only if branch does not exist
+ $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
+ }
+ }
+
+ return %errors;
+
+}
+
+=head1 EXPORTED SPECIAL ACCESSOR FUNCTIONS
+
+The following functions provide various ways of
+getting an item record, a set of item records, or
+lists of authorized values for certain item fields.
+
+Some of the functions in this group are candidates
+for refactoring -- for example, some of the code
+in C<GetItemsByBiblioitemnumber> and C<GetItemsInfo>
+has copy-and-paste work.
+
+=cut
+
+=head2 GetItemStatus
+
+=over 4
+
+$itemstatushash = GetItemStatus($fwkcode);
+
+=back
+
+Returns a list of valid values for the
+C<items.notforloan> field.
+
+NOTE: does B<not> return an individual item's
+status.
+
+Can be MARC dependant.
+fwkcode is optional.
+But basically could be can be loan or not
+Create a status selector with the following code
+
+=head3 in PERL SCRIPT
+
+=over 4
+
+my $itemstatushash = getitemstatus;
+my @itemstatusloop;
+foreach my $thisstatus (keys %$itemstatushash) {
+ my %row =(value => $thisstatus,
+ statusname => $itemstatushash->{$thisstatus}->{'statusname'},
+ );
+ push @itemstatusloop, \%row;
+}
+$template->param(statusloop=>\@itemstatusloop);
+
+=back
+
+=head3 in TEMPLATE
+
+=over 4
+
+<select name="statusloop">
+ <option value="">Default</option>
+<!-- TMPL_LOOP name="statusloop" -->
+ <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option>
+<!-- /TMPL_LOOP -->
+</select>
+
+=back
+
+=cut
+
+sub GetItemStatus {
+
+ # returns a reference to a hash of references to status...
+ my ($fwk) = @_;
+ my %itemstatus;
+ my $dbh = C4::Context->dbh;
+ my $sth;
+ $fwk = '' unless ($fwk);
+ my ( $tag, $subfield ) =
+ GetMarcFromKohaField( "items.notforloan", $fwk );
+ if ( $tag and $subfield ) {
+ my $sth =
+ $dbh->prepare(
+ "SELECT authorised_value
+ FROM marc_subfield_structure
+ WHERE tagfield=?
+ AND tagsubfield=?
+ AND frameworkcode=?
+ "
+ );
+ $sth->execute( $tag, $subfield, $fwk );
+ if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
+ my $authvalsth =
+ $dbh->prepare(
+ "SELECT authorised_value,lib
+ FROM authorised_values
+ WHERE category=?
+ ORDER BY lib
+ "
+ );
+ $authvalsth->execute($authorisedvaluecat);
+ while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
+ $itemstatus{$authorisedvalue} = $lib;
+ }
+ $authvalsth->finish;
+ return \%itemstatus;
+ exit 1;
+ }
+ else {
+
+ #No authvalue list
+ # build default
+ }
+ $sth->finish;
+ }
+
+ #No authvalue list
+ #build default
+ $itemstatus{"1"} = "Not For Loan";
+ return \%itemstatus;
+}
+
+=head2 GetItemLocation
+
+=over 4
+
+$itemlochash = GetItemLocation($fwk);
+
+=back
+
+Returns a list of valid values for the
+C<items.location> field.
+
+NOTE: does B<not> return an individual item's
+location.
+
+where fwk stands for an optional framework code.
+Create a location selector with the following code
+
+=head3 in PERL SCRIPT
+
+=over 4
+
+my $itemlochash = getitemlocation;
+my @itemlocloop;
+foreach my $thisloc (keys %$itemlochash) {
+ my $selected = 1 if $thisbranch eq $branch;
+ my %row =(locval => $thisloc,
+ selected => $selected,
+ locname => $itemlochash->{$thisloc},
+ );
+ push @itemlocloop, \%row;
+}
+$template->param(itemlocationloop => \@itemlocloop);
+
+=back
+
+=head3 in TEMPLATE
+
+=over 4
+
+<select name="location">
+ <option value="">Default</option>
+<!-- TMPL_LOOP name="itemlocationloop" -->
+ <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
+<!-- /TMPL_LOOP -->
+</select>
+
+=back
+
+=cut
+
+sub GetItemLocation {
+
+ # returns a reference to a hash of references to location...
+ my ($fwk) = @_;
+ my %itemlocation;
+ my $dbh = C4::Context->dbh;
+ my $sth;
+ $fwk = '' unless ($fwk);
+ my ( $tag, $subfield ) =
+ GetMarcFromKohaField( "items.location", $fwk );
+ if ( $tag and $subfield ) {
+ my $sth =
+ $dbh->prepare(
+ "SELECT authorised_value
+ FROM marc_subfield_structure
+ WHERE tagfield=?
+ AND tagsubfield=?
+ AND frameworkcode=?"
+ );
+ $sth->execute( $tag, $subfield, $fwk );
+ if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
+ my $authvalsth =
+ $dbh->prepare(
+ "SELECT authorised_value,lib
+ FROM authorised_values
+ WHERE category=?
+ ORDER BY lib"
+ );
+ $authvalsth->execute($authorisedvaluecat);
+ while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
+ $itemlocation{$authorisedvalue} = $lib;
+ }
+ $authvalsth->finish;
+ return \%itemlocation;
+ exit 1;
+ }
+ else {
+
+ #No authvalue list
+ # build default
+ }
+ $sth->finish;
+ }
+
+ #No authvalue list
+ #build default
+ $itemlocation{"1"} = "Not For Loan";
+ return \%itemlocation;
+}
+
+=head2 GetLostItems
+
+=over 4
+
+$items = GetLostItems( $where, $orderby );
+
+=back
+
+This function gets a list of lost items.
+
+=over 2
+
+=item input:
+
+C<$where> is a hashref. it containts a field of the items table as key
+and the value to match as value. For example:
+
+{ barcode => 'abc123',
+ homebranch => 'CPL', }
+
+C<$orderby> is a field of the items table by which the resultset
+should be orderd.
+
+=item return:
+
+C<$items> is a reference to an array full of hashrefs with columns
+from the "items" table as keys.
+
+=item usage in the perl script:
+
+my $where = { barcode => '0001548' };
+my $items = GetLostItems( $where, "homebranch" );
+$template->param( itemsloop => $items );
+
+=back
+
+=cut
+
+sub GetLostItems {
+ # Getting input args.
+ my $where = shift;
+ my $orderby = shift;
+ my $dbh = C4::Context->dbh;
+
+ my $query = "
+ SELECT *
+ FROM items
+ LEFT JOIN biblio ON (items.biblionumber = biblio.biblionumber)
+ LEFT JOIN biblioitems ON (items.biblionumber = biblioitems.biblionumber)
+ LEFT JOIN authorised_values ON (items.itemlost = authorised_values.authorised_value)
+ WHERE
+ authorised_values.category = 'LOST'
+ AND itemlost IS NOT NULL
+ AND itemlost <> 0
+ ";
+ my @query_parameters;
+ foreach my $key (keys %$where) {
+ $query .= " AND $key LIKE ?";
+ push @query_parameters, "%$where->{$key}%";
+ }
+ my @ordervalues = qw/title author homebranch itype barcode price replacementprice lib datelastseen location/;
+
+ if ( defined $orderby && grep($orderby, @ordervalues)) {
+ $query .= ' ORDER BY '.$orderby;
+ }
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute( @query_parameters );
+ my $items = [];
+ while ( my $row = $sth->fetchrow_hashref ){
+ push @$items, $row;
+ }
+ return $items;
+}
+
+=head2 GetItemsForInventory
+
+=over 4
+
+$itemlist = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype $datelastseen, $branch, $offset, $size);
+
+=back
+
+Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
+
+The sub returns a reference to a list of hashes, each containing
+itemnumber, author, title, barcode, item callnumber, and date last
+seen. It is ordered by callnumber then title.
+
+The required minlocation & maxlocation parameters are used to specify a range of item callnumbers
+the datelastseen can be used to specify that you want to see items not seen since a past date only.
+offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
+
+=cut
+
+sub GetItemsForInventory {
+ my ( $minlocation, $maxlocation,$location, $itemtype, $ignoreissued, $datelastseen, $branch, $offset, $size ) = @_;
+ my $dbh = C4::Context->dbh;
+ my ( @bind_params, @where_strings );
+
+ my $query = <<'END_SQL';
+SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, datelastseen
+FROM items
+ LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
+ LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
+END_SQL
+
+ if ($minlocation) {
+ push @where_strings, 'itemcallnumber >= ?';
+ push @bind_params, $minlocation;
+ }
+
+ if ($maxlocation) {
+ push @where_strings, 'itemcallnumber <= ?';
+ push @bind_params, $maxlocation;
+ }
+
+ if ($datelastseen) {
+ $datelastseen = format_date_in_iso($datelastseen);
+ push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
+ push @bind_params, $datelastseen;
+ }
+
+ if ( $location ) {
+ push @where_strings, 'items.location = ?';
+ push @bind_params, $location;
+ }
+
+ if ( $branch ) {
+ push @where_strings, 'items.homebranch = ?';
+ push @bind_params, $branch;
+ }
+
+ if ( $itemtype ) {
+ push @where_strings, 'biblioitems.itemtype = ?';
+ push @bind_params, $itemtype;
+ }
+ if ( $ignoreissued) {
+ $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
+ push @where_strings, 'issues.date_due IS NULL';
+ }
+
+ if ( $ignoreissued) {
+ $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
+ push @where_strings, 'issues.date_due IS NULL';
+ }
+
+ if ( @where_strings ) {
+ $query .= 'WHERE ';
+ $query .= join ' AND ', @where_strings;
+ }
+ $query .= ' ORDER BY itemcallnumber, title';
+ my $sth = $dbh->prepare($query);
+ $sth->execute( @bind_params );
+
+ my @results;
+ $size--;
+ while ( my $row = $sth->fetchrow_hashref ) {
+ $offset-- if ($offset);
+ $row->{datelastseen}=format_date($row->{datelastseen});
+ if ( ( !$offset ) && $size ) {
+ push @results, $row;
+ $size--;
+ }
+ }
+ return \@results;
+}
+
+=head2 GetItemsCount
+
+=over 4
+$count = &GetItemsCount( $biblionumber);
+
+=back
+
+This function return count of item with $biblionumber
+
+=cut
+
+sub GetItemsCount {
+ my ( $biblionumber ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = "SELECT count(*)
+ FROM items
+ WHERE biblionumber=?";
+ my $sth = $dbh->prepare($query);
+ $sth->execute($biblionumber);
+ my $count = $sth->fetchrow;
+ $sth->finish;
+ return ($count);
+}
+
+=head2 GetItemInfosOf
+
+=over 4
+
+GetItemInfosOf(@itemnumbers);
+
+=back
+
+=cut
+
+sub GetItemInfosOf {
+ my @itemnumbers = @_;
+
+ my $query = '
+ SELECT *
+ FROM items
+ WHERE itemnumber IN (' . join( ',', @itemnumbers ) . ')
+ ';
+ return get_infos_of( $query, 'itemnumber' );
+}
+
+=head2 GetItemsByBiblioitemnumber
+
+=over 4
+
+GetItemsByBiblioitemnumber($biblioitemnumber);
+
+=back
+
+Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
+Called by C<C4::XISBN>
+
+=cut
+
+sub GetItemsByBiblioitemnumber {
+ my ( $bibitem ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
+ # Get all items attached to a biblioitem
+ my $i = 0;
+ my @results;
+ $sth->execute($bibitem) || die $sth->errstr;
+ while ( my $data = $sth->fetchrow_hashref ) {
+ # Foreach item, get circulation information
+ my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
+ WHERE itemnumber = ?
+ AND issues.borrowernumber = borrowers.borrowernumber"
+ );
+ $sth2->execute( $data->{'itemnumber'} );
+ if ( my $data2 = $sth2->fetchrow_hashref ) {
+ # if item is out, set the due date and who it is out too
+ $data->{'date_due'} = $data2->{'date_due'};
+ $data->{'cardnumber'} = $data2->{'cardnumber'};
+ $data->{'borrowernumber'} = $data2->{'borrowernumber'};
+ }
+ else {
+ # set date_due to blank, so in the template we check itemlost, and wthdrawn
+ $data->{'date_due'} = '';
+ } # else
+ $sth2->finish;
+ # Find the last 3 people who borrowed this item.
+ my $query2 = "SELECT * FROM old_issues, borrowers WHERE itemnumber = ?
+ AND old_issues.borrowernumber = borrowers.borrowernumber
+ ORDER BY returndate desc,timestamp desc LIMIT 3";
+ $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
+ $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
+ my $i2 = 0;
+ while ( my $data2 = $sth2->fetchrow_hashref ) {
+ $data->{"timestamp$i2"} = $data2->{'timestamp'};
+ $data->{"card$i2"} = $data2->{'cardnumber'};
+ $data->{"borrower$i2"} = $data2->{'borrowernumber'};
+ $i2++;
+ }
+ $sth2->finish;
+ push(@results,$data);
+ }
+ $sth->finish;
+ return (\@results);
+}
+
+=head2 GetItemsInfo
+
+=over 4
+
+@results = GetItemsInfo($biblionumber, $type);
+
+=back
+
+Returns information about books with the given biblionumber.
+
+C<$type> may be either C<intra> or anything else. If it is not set to
+C<intra>, then the search will exclude lost, very overdue, and
+withdrawn items.
+
+C<GetItemsInfo> returns a list of references-to-hash. Each element
+contains a number of keys. Most of them are table items 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, $type ) = @_;
+ my $dbh = C4::Context->dbh;
+ # note biblioitems.* must be avoided to prevent large marc and marcxml fields from killing performance.
+ 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,
+ itemtypes.description
+ FROM items
+ LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
+ LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
+ LEFT JOIN itemtypes ON itemtypes.itemtype = "
+ . (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype');
+ $query .= " WHERE items.biblionumber = ? ORDER BY items.dateaccessioned desc" ;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($biblionumber);
+ my $i = 0;
+ my @results;
+ my $serial;
+
+ my $isth = $dbh->prepare(
+ "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname,borrowers.branchcode as bcode
+ FROM issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
+ WHERE itemnumber = ?"
+ );
+ my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=? ");
+ while ( my $data = $sth->fetchrow_hashref ) {
+ my $count_reserves;
+ my $datedue = '';
+ $isth->execute( $data->{'itemnumber'} );
+ if ( my $idata = $isth->fetchrow_hashref ) {
+ $data->{borrowernumber} = $idata->{borrowernumber};
+ $data->{cardnumber} = $idata->{cardnumber};
+ $data->{surname} = $idata->{surname};
+ $data->{firstname} = $idata->{firstname};
+ $datedue = $idata->{'date_due'};
+ if (C4::Context->preference("IndependantBranches")){
+ my $userenv = C4::Context->userenv;
+ if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
+ $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch});
+ }
+ }
+ }
+ if ( $data->{'serial'}) {
+ $ssth->execute($data->{'itemnumber'}) ;
+ ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
+ $serial = 1;
+ }
+ if ( $datedue eq '' ) {
+ my ( $restype, $reserves ) =
+ C4::Reserves::CheckReserves( $data->{'itemnumber'} );
+ if ($restype) {
+ $count_reserves = $restype;
+ }
+ }
+ $isth->finish;
+ $ssth->finish;
+ #get branch information.....
+ my $bsth = $dbh->prepare(
+ "SELECT * FROM branches WHERE branchcode = ?
+ "
+ );
+ $bsth->execute( $data->{'holdingbranch'} );
+ if ( my $bdata = $bsth->fetchrow_hashref ) {
+ $data->{'branchname'} = $bdata->{'branchname'};
+ }
+ $data->{'datedue'} = $datedue;
+ $data->{'count_reserves'} = $count_reserves;
+
+ # get notforloan complete status if applicable
+ my $sthnflstatus = $dbh->prepare(
+ 'SELECT authorised_value
+ FROM marc_subfield_structure
+ WHERE kohafield="items.notforloan"
+ '
+ );
+
+ $sthnflstatus->execute;
+ my ($authorised_valuecode) = $sthnflstatus->fetchrow;
+ if ($authorised_valuecode) {
+ $sthnflstatus = $dbh->prepare(
+ "SELECT lib FROM authorised_values
+ WHERE category=?
+ AND authorised_value=?"
+ );
+ $sthnflstatus->execute( $authorised_valuecode,
+ $data->{itemnotforloan} );
+ my ($lib) = $sthnflstatus->fetchrow;
+ $data->{notforloanvalue} = $lib;
+ }
+ $data->{itypenotforloan} = $data->{notforloan} if (C4::Context->preference('item-level_itypes'));
+
+ # my stack procedures
+ my $stackstatus = $dbh->prepare(
+ 'SELECT authorised_value
+ FROM marc_subfield_structure
+ WHERE kohafield="items.stack"
+ '
+ );
+ $stackstatus->execute;
+
+ ($authorised_valuecode) = $stackstatus->fetchrow;
+ if ($authorised_valuecode) {
+ $stackstatus = $dbh->prepare(
+ "SELECT lib
+ FROM authorised_values
+ WHERE category=?
+ AND authorised_value=?
+ "
+ );
+ $stackstatus->execute( $authorised_valuecode, $data->{stack} );
+ my ($lib) = $stackstatus->fetchrow;
+ $data->{stack} = $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++;
+ }
+ if($serial) {
+ return( sort { ($b->{'publisheddate'} || $b->{'enumchron'}) cmp ($a->{'publisheddate'} || $a->{'enumchron'}) } @results );
+ } else {
+ return (@results);
+ }
+}
+
+=head2 get_itemnumbers_of
+
+=over 4
+
+my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
+
+=back
+
+Given a list of biblionumbers, return the list of corresponding itemnumbers
+for each biblionumber.
+
+Return a reference on a hash where keys are biblionumbers and values are
+references on array of itemnumbers.
+
+=cut
+
+sub get_itemnumbers_of {
+ my @biblionumbers = @_;
+
+ my $dbh = C4::Context->dbh;
+
+ my $query = '
+ SELECT itemnumber,
+ biblionumber
+ FROM items
+ WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ')
+ ';
+ my $sth = $dbh->prepare($query);
+ $sth->execute(@biblionumbers);
+
+ my %itemnumbers_of;
+
+ while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
+ push @{ $itemnumbers_of{$biblionumber} }, $itemnumber;
+ }
+
+ return \%itemnumbers_of;
+}
+
+=head2 GetItemnumberFromBarcode
+
+=over 4
+
+$result = GetItemnumberFromBarcode($barcode);
+
+=back
+
+=cut
+
+sub GetItemnumberFromBarcode {
+ my ($barcode) = @_;
+ my $dbh = C4::Context->dbh;
+
+ my $rq =
+ $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
+ $rq->execute($barcode);
+ my ($result) = $rq->fetchrow;
+ return ($result);
+}
+
+=head3 get_item_authorised_values
+
+ find the types and values for all authorised values assigned to this item.
+
+ parameters:
+ itemnumber
+
+ returns: a hashref malling the authorised value to the value set for this itemnumber
+
+ $authorised_values = {
+ 'CCODE' => undef,
+ 'DAMAGED' => '0',
+ 'LOC' => '3',
+ 'LOST' => '0'
+ 'NOT_LOAN' => '0',
+ 'RESTRICTED' => undef,
+ 'STACK' => undef,
+ 'WITHDRAWN' => '0',
+ 'branches' => 'CPL',
+ 'cn_source' => undef,
+ 'itemtypes' => 'SER',
+ };
+
+ Notes: see C4::Biblio::get_biblio_authorised_values for a similar method at the biblio level.
+
+=cut
+
+sub get_item_authorised_values {
+ my $itemnumber = shift;
+
+ # assume that these entries in the authorised_value table are item level.
+ my $query = q(SELECT distinct authorised_value, kohafield
+ FROM marc_subfield_structure
+ WHERE kohafield like 'item%'
+ AND authorised_value != '' );
+
+ my $itemlevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
+ my $iteminfo = GetItem( $itemnumber );
+ # warn( Data::Dumper->Dump( [ $itemlevel_authorised_values ], [ 'itemlevel_authorised_values' ] ) );
+ my $return;
+ foreach my $this_authorised_value ( keys %$itemlevel_authorised_values ) {
+ my $field = $itemlevel_authorised_values->{ $this_authorised_value }->{'kohafield'};
+ $field =~ s/^items\.//;
+ if ( exists $iteminfo->{ $field } ) {
+ $return->{ $this_authorised_value } = $iteminfo->{ $field };
+ }
+ }
+ # warn( Data::Dumper->Dump( [ $return ], [ 'return' ] ) );
+ return $return;
+}
+
+=head3 get_authorised_value_images
+
+ find a list of icons that are appropriate for display based on the
+ authorised values for a biblio.
+
+ parameters: listref of authorised values, such as comes from
+ get_item_authorised_values or
+ from C4::Biblio::get_biblio_authorised_values
+
+ returns: listref of hashrefs for each image. Each hashref looks like
+ this:
+
+ { imageurl => '/intranet-tmpl/prog/img/itemtypeimg/npl/WEB.gif',
+ label => '',
+ category => '',
+ value => '', }
+
+ Notes: Currently, I put on the full path to the images on the staff
+ side. This should either be configurable or not done at all. Since I
+ have to deal with 'intranet' or 'opac' in
+ get_biblio_authorised_values, perhaps I should be passing it in.
+
+=cut
+
+sub get_authorised_value_images {
+ my $authorised_values = shift;
+
+ my @imagelist;
+
+ my $authorised_value_list = GetAuthorisedValues();
+ # warn ( Data::Dumper->Dump( [ $authorised_value_list ], [ 'authorised_value_list' ] ) );
+ foreach my $this_authorised_value ( @$authorised_value_list ) {
+ if ( exists $authorised_values->{ $this_authorised_value->{'category'} }
+ && $authorised_values->{ $this_authorised_value->{'category'} } eq $this_authorised_value->{'authorised_value'} ) {
+ # warn ( Data::Dumper->Dump( [ $this_authorised_value ], [ 'this_authorised_value' ] ) );
+ if ( defined $this_authorised_value->{'imageurl'} ) {
+ push @imagelist, { imageurl => C4::Koha::getitemtypeimagelocation( 'intranet', $this_authorised_value->{'imageurl'} ),
+ label => $this_authorised_value->{'lib'},
+ category => $this_authorised_value->{'category'},
+ value => $this_authorised_value->{'authorised_value'}, };
+ }
+ }
+ }
+
+ # warn ( Data::Dumper->Dump( [ \@imagelist ], [ 'imagelist' ] ) );
+ return \@imagelist;
+
+}
+