3 # Copyright 2007 LibLime, Inc.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
25 use C4::Dates qw/format_date format_date_in_iso/;
33 use vars qw($VERSION @ISA @EXPORT);
39 @ISA = qw( Exporter );
61 GetItemsByBiblioitemnumber
64 GetItemnumberFromBarcode
70 C4::Items - item management functions
74 This module contains an API for manipulating item
75 records in Koha, and is used by cataloguing, circulation,
76 acquisitions, and serials management.
78 A Koha item record is stored in two places: the
79 items table and embedded in a MARC tag in the XML
80 version of the associated bib record in C<biblioitems.marcxml>.
81 This is done to allow the item information to be readily
82 indexed (e.g., by Zebra), but means that each item
83 modification transaction must keep the items table
84 and the MARC XML in sync at all times.
86 Consequently, all code that creates, modifies, or deletes
87 item records B<must> use an appropriate function from
88 C<C4::Items>. If no existing function is suitable, it is
89 better to add one to C<C4::Items> than to use add
90 one-off SQL statements to add or modify items.
92 The items table will be considered authoritative. In other
93 words, if there is ever a discrepancy between the items
94 table and the MARC XML, the items table should be considered
97 =head1 HISTORICAL NOTE
99 Most of the functions in C<C4::Items> were originally in
100 the C<C4::Biblio> module.
102 =head1 CORE EXPORTED FUNCTIONS
104 The following functions are meant for use by users
113 $item = GetItem($itemnumber,$barcode,$serial);
117 Return item information, for a given itemnumber or barcode.
118 The return value is a hashref mapping item column
119 names to values. If C<$serial> is true, include serial publication data.
124 my ($itemnumber,$barcode, $serial) = @_;
125 my $dbh = C4::Context->dbh;
128 my $sth = $dbh->prepare("
130 WHERE itemnumber = ?");
131 $sth->execute($itemnumber);
132 $data = $sth->fetchrow_hashref;
134 my $sth = $dbh->prepare("
138 $sth->execute($barcode);
139 $data = $sth->fetchrow_hashref;
142 my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=?");
143 $ssth->execute($data->{'itemnumber'}) ;
144 ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
145 warn $data->{'serialseq'} , $data->{'publisheddate'};
150 =head2 AddItemFromMarc
154 my ($biblionumber, $biblioitemnumber, $itemnumber)
155 = AddItemFromMarc($source_item_marc, $biblionumber);
159 Given a MARC::Record object containing an embedded item
160 record and a biblionumber, create a new item record.
164 sub AddItemFromMarc {
165 my ( $source_item_marc, $biblionumber ) = @_;
166 my $dbh = C4::Context->dbh;
168 # parse item hash from MARC
169 my $frameworkcode = GetFrameworkCode( $biblionumber );
170 my $item = &TransformMarcToKoha( $dbh, $source_item_marc, $frameworkcode );
171 my $unlinked_item_subfields = _get_unlinked_item_subfields($source_item_marc, $frameworkcode);
172 return AddItem($item, $biblionumber, $dbh, $frameworkcode, $unlinked_item_subfields);
179 my ($biblionumber, $biblioitemnumber, $itemnumber)
180 = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $unlinked_item_subfields]);
184 Given a hash containing item column names as keys,
185 create a new Koha item record.
187 The first two optional parameters (C<$dbh> and C<$frameworkcode>)
188 do not need to be supplied for general use; they exist
189 simply to allow them to be picked up from AddItemFromMarc.
191 The final optional parameter, C<$unlinked_item_subfields>, contains
192 an arrayref containing subfields present in the original MARC
193 representation of the item (e.g., from the item editor) that are
194 not mapped to C<items> columns directly but should instead
195 be stored in C<items.more_subfields_xml> and included in
196 the biblio items tag for display and indexing.
202 my $biblionumber = shift;
204 my $dbh = @_ ? shift : C4::Context->dbh;
205 my $frameworkcode = @_ ? shift : GetFrameworkCode( $biblionumber );
206 my $unlinked_item_subfields;
208 $unlinked_item_subfields = shift
211 # needs old biblionumber and biblioitemnumber
212 $item->{'biblionumber'} = $biblionumber;
213 my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
214 $sth->execute( $item->{'biblionumber'} );
215 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
217 _set_defaults_for_add($item);
218 _set_derived_columns_for_add($item);
219 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
220 # FIXME - checks here
221 my ( $itemnumber, $error ) = _koha_new_item( $item, $item->{barcode} );
222 $item->{'itemnumber'} = $itemnumber;
224 # create MARC tag representing item and add to bib
225 my $new_item_marc = _marc_from_item_hash($item, $frameworkcode, $unlinked_item_subfields);
226 _add_item_field_to_biblio($new_item_marc, $item->{'biblionumber'}, $frameworkcode );
228 logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
229 if C4::Context->preference("CataloguingLog");
231 return ($item->{biblionumber}, $item->{biblioitemnumber}, $itemnumber);
234 =head2 AddItemBatchFromMarc
238 ($itemnumber_ref, $error_ref) = AddItemBatchFromMarc($record, $biblionumber, $biblioitemnumber, $frameworkcode);
242 Efficiently create item records from a MARC biblio record with
243 embedded item fields. This routine is suitable for batch jobs.
245 This API assumes that the bib record has already been
246 saved to the C<biblio> and C<biblioitems> tables. It does
247 not expect that C<biblioitems.marc> and C<biblioitems.marcxml>
248 are populated, but it will do so via a call to ModBibiloMarc.
250 The goal of this API is to have a similar effect to using AddBiblio
251 and AddItems in succession, but without inefficient repeated
252 parsing of the MARC XML bib record.
254 This function returns an arrayref of new itemsnumbers and an arrayref of item
255 errors encountered during the processing. Each entry in the errors
256 list is a hashref containing the following keys:
262 Sequence number of original item tag in the MARC record.
266 Item barcode, provide to assist in the construction of
267 useful error messages.
269 =item error_condition
271 Code representing the error condition. Can be 'duplicate_barcode',
272 'invalid_homebranch', or 'invalid_holdingbranch'.
274 =item error_information
276 Additional information appropriate to the error condition.
282 sub AddItemBatchFromMarc {
283 my ($record, $biblionumber, $biblioitemnumber, $frameworkcode) = @_;
285 my @itemnumbers = ();
287 my $dbh = C4::Context->dbh;
289 # loop through the item tags and start creating items
290 my @bad_item_fields = ();
291 my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
292 my $item_sequence_num = 0;
293 ITEMFIELD: foreach my $item_field ($record->field($itemtag)) {
294 $item_sequence_num++;
295 # we take the item field and stick it into a new
296 # MARC record -- this is required so far because (FIXME)
297 # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
298 # and there is no TransformMarcFieldToKoha
299 my $temp_item_marc = MARC::Record->new();
300 $temp_item_marc->append_fields($item_field);
302 # add biblionumber and biblioitemnumber
303 my $item = TransformMarcToKoha( $dbh, $temp_item_marc, $frameworkcode, 'items' );
304 my $unlinked_item_subfields = _get_unlinked_item_subfields($temp_item_marc, $frameworkcode);
305 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
306 $item->{'biblionumber'} = $biblionumber;
307 $item->{'biblioitemnumber'} = $biblioitemnumber;
309 # check for duplicate barcode
310 my %item_errors = CheckItemPreSave($item);
312 push @errors, _repack_item_errors($item_sequence_num, $item, \%item_errors);
313 push @bad_item_fields, $item_field;
317 _set_defaults_for_add($item);
318 _set_derived_columns_for_add($item);
319 my ( $itemnumber, $error ) = _koha_new_item( $item, $item->{barcode} );
320 warn $error if $error;
321 push @itemnumbers, $itemnumber; # FIXME not checking error
322 $item->{'itemnumber'} = $itemnumber;
324 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
325 if C4::Context->preference("CataloguingLog");
327 my $new_item_marc = _marc_from_item_hash($item, $frameworkcode, $unlinked_item_subfields);
328 $item_field->replace_with($new_item_marc->field($itemtag));
331 # remove any MARC item fields for rejected items
332 foreach my $item_field (@bad_item_fields) {
333 $record->delete_field($item_field);
336 # update the MARC biblio
337 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
339 return (\@itemnumbers, \@errors);
342 =head2 ModItemFromMarc
346 ModItemFromMarc($item_marc, $biblionumber, $itemnumber);
350 This function updates an item record based on a supplied
351 C<MARC::Record> object containing an embedded item field.
352 This API is meant for the use of C<additem.pl>; for
353 other purposes, C<ModItem> should be used.
357 sub ModItemFromMarc {
358 my $item_marc = shift;
359 my $biblionumber = shift;
360 my $itemnumber = shift;
362 my $dbh = C4::Context->dbh;
363 my $frameworkcode = GetFrameworkCode( $biblionumber );
364 my $item = &TransformMarcToKoha( $dbh, $item_marc, $frameworkcode );
365 my $unlinked_item_subfields = _get_unlinked_item_subfields($item_marc, $frameworkcode);
367 return ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields);
374 ModItem({ column => $newvalue }, $biblionumber, $itemnumber[, $original_item_marc]);
378 Change one or more columns in an item record and update
379 the MARC representation of the item.
381 The first argument is a hashref mapping from item column
382 names to the new values. The second and third arguments
383 are the biblionumber and itemnumber, respectively.
385 The fourth, optional parameter, C<$unlinked_item_subfields>, contains
386 an arrayref containing subfields present in the original MARC
387 representation of the item (e.g., from the item editor) that are
388 not mapped to C<items> columns directly but should instead
389 be stored in C<items.more_subfields_xml> and included in
390 the biblio items tag for display and indexing.
392 If one of the changed columns is used to calculate
393 the derived value of a column such as C<items.cn_sort>,
394 this routine will perform the necessary calculation
401 my $biblionumber = shift;
402 my $itemnumber = shift;
404 # if $biblionumber is undefined, get it from the current item
405 unless (defined $biblionumber) {
406 $biblionumber = _get_single_item_column('biblionumber', $itemnumber);
409 my $dbh = @_ ? shift : C4::Context->dbh;
410 my $frameworkcode = @_ ? shift : GetFrameworkCode( $biblionumber );
412 my $unlinked_item_subfields;
414 $unlinked_item_subfields = shift;
415 $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
418 $item->{'itemnumber'} = $itemnumber or return undef;
419 _set_derived_columns_for_mod($item);
420 _do_column_fixes_for_mod($item);
423 # attempt to change itemnumber
424 # attempt to change biblionumber (if we want
425 # an API to relink an item to a different bib,
426 # it should be a separate function)
429 _koha_modify_item($item);
431 # update biblio MARC XML
432 my $whole_item = GetItem($itemnumber) or die "FAILED GetItem($itemnumber)";
434 unless (defined $unlinked_item_subfields) {
435 $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'});
437 my $new_item_marc = _marc_from_item_hash($whole_item, $frameworkcode, $unlinked_item_subfields)
438 or die "FAILED _marc_from_item_hash($whole_item, $frameworkcode)";
440 _replace_item_field_in_biblio($new_item_marc, $biblionumber, $itemnumber, $frameworkcode);
441 (C4::Context->userenv eq '0') and die "userenv is '0', not hashref"; # logaction line would crash anyway
442 ($new_item_marc eq '0') and die "$new_item_marc is '0', not hashref"; # logaction line would crash anyway
443 logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$itemnumber,$new_item_marc->as_formatted)
444 if C4::Context->preference("CataloguingLog");
447 =head2 ModItemTransfer
451 ModItemTransfer($itenumber, $frombranch, $tobranch);
455 Marks an item as being transferred from one branch
460 sub ModItemTransfer {
461 my ( $itemnumber, $frombranch, $tobranch ) = @_;
463 my $dbh = C4::Context->dbh;
465 #new entry in branchtransfers....
466 my $sth = $dbh->prepare(
467 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
468 VALUES (?, ?, NOW(), ?)");
469 $sth->execute($itemnumber, $frombranch, $tobranch);
471 ModItem({ holdingbranch => $tobranch }, undef, $itemnumber);
472 ModDateLastSeen($itemnumber);
476 =head2 ModDateLastSeen
480 ModDateLastSeen($itemnum);
484 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking.
485 C<$itemnum> is the item number
489 sub ModDateLastSeen {
490 my ($itemnumber) = @_;
492 my $today = C4::Dates->new();
493 ModItem({ itemlost => 0, datelastseen => $today->output("iso") }, undef, $itemnumber);
500 DelItem($biblionumber, $itemnumber);
504 Exported function (core API) for deleting an item record in Koha.
509 my ( $dbh, $biblionumber, $itemnumber ) = @_;
511 # FIXME check the item has no current issues
513 _koha_delete_item( $dbh, $itemnumber );
515 # get the MARC record
516 my $record = GetMarcBiblio($biblionumber);
517 my $frameworkcode = GetFrameworkCode($biblionumber);
520 my $copy2deleted = $dbh->prepare("UPDATE deleteditems SET marc=? WHERE itemnumber=?");
521 $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
523 #search item field code
524 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
525 my @fields = $record->field($itemtag);
527 # delete the item specified
528 foreach my $field (@fields) {
529 if ( $field->subfield($itemsubfield) eq $itemnumber ) {
530 $record->delete_field($field);
533 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
534 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$itemnumber,"item")
535 if C4::Context->preference("CataloguingLog");
538 =head2 CheckItemPreSave
542 my $item_ref = TransformMarcToKoha($marc, 'items');
544 my %errors = CheckItemPreSave($item_ref);
545 if (exists $errors{'duplicate_barcode'}) {
546 print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
547 } elsif (exists $errors{'invalid_homebranch'}) {
548 print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
549 } elsif (exists $errors{'invalid_holdingbranch'}) {
550 print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
557 Given a hashref containing item fields, determine if it can be
558 inserted or updated in the database. Specifically, checks for
559 database integrity issues, and returns a hash containing any
560 of the following keys, if applicable.
564 =item duplicate_barcode
566 Barcode, if it duplicates one already found in the database.
568 =item invalid_homebranch
570 Home branch, if not defined in branches table.
572 =item invalid_holdingbranch
574 Holding branch, if not defined in branches table.
578 This function does NOT implement any policy-related checks,
579 e.g., whether current operator is allowed to save an
580 item that has a given branch code.
584 sub CheckItemPreSave {
585 my $item_ref = shift;
589 # check for duplicate barcode
590 if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
591 my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'});
592 if ($existing_itemnumber) {
593 if (!exists $item_ref->{'itemnumber'} # new item
594 or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
595 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
600 # check for valid home branch
601 if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
602 my $branch_name = GetBranchName($item_ref->{'homebranch'});
603 unless (defined $branch_name) {
604 # relies on fact that branches.branchname is a non-NULL column,
605 # so GetBranchName returns undef only if branch does not exist
606 $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
610 # check for valid holding branch
611 if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
612 my $branch_name = GetBranchName($item_ref->{'holdingbranch'});
613 unless (defined $branch_name) {
614 # relies on fact that branches.branchname is a non-NULL column,
615 # so GetBranchName returns undef only if branch does not exist
616 $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
624 =head1 EXPORTED SPECIAL ACCESSOR FUNCTIONS
626 The following functions provide various ways of
627 getting an item record, a set of item records, or
628 lists of authorized values for certain item fields.
630 Some of the functions in this group are candidates
631 for refactoring -- for example, some of the code
632 in C<GetItemsByBiblioitemnumber> and C<GetItemsInfo>
633 has copy-and-paste work.
641 $itemstatushash = GetItemStatus($fwkcode);
645 Returns a list of valid values for the
646 C<items.notforloan> field.
648 NOTE: does B<not> return an individual item's
651 Can be MARC dependant.
653 But basically could be can be loan or not
654 Create a status selector with the following code
656 =head3 in PERL SCRIPT
660 my $itemstatushash = getitemstatus;
662 foreach my $thisstatus (keys %$itemstatushash) {
663 my %row =(value => $thisstatus,
664 statusname => $itemstatushash->{$thisstatus}->{'statusname'},
666 push @itemstatusloop, \%row;
668 $template->param(statusloop=>\@itemstatusloop);
676 <select name="statusloop">
677 <option value="">Default</option>
678 <!-- TMPL_LOOP name="statusloop" -->
679 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option>
689 # returns a reference to a hash of references to status...
692 my $dbh = C4::Context->dbh;
694 $fwk = '' unless ($fwk);
695 my ( $tag, $subfield ) =
696 GetMarcFromKohaField( "items.notforloan", $fwk );
697 if ( $tag and $subfield ) {
700 "SELECT authorised_value
701 FROM marc_subfield_structure
707 $sth->execute( $tag, $subfield, $fwk );
708 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
711 "SELECT authorised_value,lib
712 FROM authorised_values
717 $authvalsth->execute($authorisedvaluecat);
718 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
719 $itemstatus{$authorisedvalue} = $lib;
735 $itemstatus{"1"} = "Not For Loan";
739 =head2 GetItemLocation
743 $itemlochash = GetItemLocation($fwk);
747 Returns a list of valid values for the
748 C<items.location> field.
750 NOTE: does B<not> return an individual item's
753 where fwk stands for an optional framework code.
754 Create a location selector with the following code
756 =head3 in PERL SCRIPT
760 my $itemlochash = getitemlocation;
762 foreach my $thisloc (keys %$itemlochash) {
763 my $selected = 1 if $thisbranch eq $branch;
764 my %row =(locval => $thisloc,
765 selected => $selected,
766 locname => $itemlochash->{$thisloc},
768 push @itemlocloop, \%row;
770 $template->param(itemlocationloop => \@itemlocloop);
778 <select name="location">
779 <option value="">Default</option>
780 <!-- TMPL_LOOP name="itemlocationloop" -->
781 <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
789 sub GetItemLocation {
791 # returns a reference to a hash of references to location...
794 my $dbh = C4::Context->dbh;
796 $fwk = '' unless ($fwk);
797 my ( $tag, $subfield ) =
798 GetMarcFromKohaField( "items.location", $fwk );
799 if ( $tag and $subfield ) {
802 "SELECT authorised_value
803 FROM marc_subfield_structure
808 $sth->execute( $tag, $subfield, $fwk );
809 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
812 "SELECT authorised_value,lib
813 FROM authorised_values
817 $authvalsth->execute($authorisedvaluecat);
818 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
819 $itemlocation{$authorisedvalue} = $lib;
822 return \%itemlocation;
835 $itemlocation{"1"} = "Not For Loan";
836 return \%itemlocation;
843 $items = GetLostItems($where,$orderby);
847 This function get the items lost into C<$items>.
852 C<$where> is a hashref. it containts a field of the items table as key
853 and the value to match as value.
854 C<$orderby> is a field of the items table.
857 C<$items> is a reference to an array full of hasref which keys are items' table column.
859 =item usage in the perl script:
862 $where{barcode} = 0001548;
863 my $items = GetLostItems( \%where, "homebranch" );
864 $template->param(itemsloop => $items);
871 # Getting input args.
874 my $dbh = C4::Context->dbh;
879 WHERE itemlost IS NOT NULL
882 foreach my $key (keys %$where) {
883 $query .= " AND " . $key . " LIKE '%" . $where->{$key} . "%'";
885 $query .= " ORDER BY ".$orderby if defined $orderby;
887 my $sth = $dbh->prepare($query);
890 while ( my $row = $sth->fetchrow_hashref ){
896 =head2 GetItemsForInventory
900 $itemlist = GetItemsForInventory($minlocation,$maxlocation,$datelastseen,$offset,$size)
904 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
906 The sub returns a list of hashes, containing itemnumber, author, title, barcode & item callnumber.
907 It is ordered by callnumber,title.
909 The minlocation & maxlocation parameters are used to specify a range of item callnumbers
910 the datelastseen can be used to specify that you want to see items not seen since a past date only.
911 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
915 sub GetItemsForInventory {
916 my ( $minlocation, $maxlocation,$location, $datelastseen, $branch, $offset, $size ) = @_;
917 my $dbh = C4::Context->dbh;
920 $datelastseen=format_date_in_iso($datelastseen);
922 "SELECT itemnumber,barcode,itemcallnumber,title,author,biblio.biblionumber,datelastseen
924 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
925 WHERE itemcallnumber>= ?
926 AND itemcallnumber <=?
927 AND (datelastseen< ? OR datelastseen IS NULL)";
928 $query.= " AND items.location=".$dbh->quote($location) if $location;
929 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
930 $query .= " ORDER BY itemcallnumber,title";
931 $sth = $dbh->prepare($query);
932 $sth->execute( $minlocation, $maxlocation, $datelastseen );
936 SELECT itemnumber,barcode,itemcallnumber,biblio.biblionumber,title,author,datelastseen
938 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
939 WHERE itemcallnumber>= ?
940 AND itemcallnumber <=?";
941 $query.= " AND items.location=".$dbh->quote($location) if $location;
942 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
943 $query .= " ORDER BY itemcallnumber,title";
944 $sth = $dbh->prepare($query);
945 $sth->execute( $minlocation, $maxlocation );
948 while ( my $row = $sth->fetchrow_hashref ) {
949 $offset-- if ($offset);
950 $row->{datelastseen}=format_date($row->{datelastseen});
951 if ( ( !$offset ) && $size ) {
962 $count = &GetItemsCount( $biblionumber);
966 This function return count of item with $biblionumber
971 my ( $biblionumber ) = @_;
972 my $dbh = C4::Context->dbh;
973 my $query = "SELECT count(*)
975 WHERE biblionumber=?";
976 my $sth = $dbh->prepare($query);
977 $sth->execute($biblionumber);
978 my $count = $sth->fetchrow;
983 =head2 GetItemInfosOf
987 GetItemInfosOf(@itemnumbers);
994 my @itemnumbers = @_;
999 WHERE itemnumber IN (' . join( ',', @itemnumbers ) . ')
1001 return get_infos_of( $query, 'itemnumber' );
1004 =head2 GetItemsByBiblioitemnumber
1008 GetItemsByBiblioitemnumber($biblioitemnumber);
1012 Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
1013 Called by C<C4::XISBN>
1017 sub GetItemsByBiblioitemnumber {
1018 my ( $bibitem ) = @_;
1019 my $dbh = C4::Context->dbh;
1020 my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
1021 # Get all items attached to a biblioitem
1024 $sth->execute($bibitem) || die $sth->errstr;
1025 while ( my $data = $sth->fetchrow_hashref ) {
1026 # Foreach item, get circulation information
1027 my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
1028 WHERE itemnumber = ?
1029 AND returndate is NULL
1030 AND issues.borrowernumber = borrowers.borrowernumber"
1032 $sth2->execute( $data->{'itemnumber'} );
1033 if ( my $data2 = $sth2->fetchrow_hashref ) {
1034 # if item is out, set the due date and who it is out too
1035 $data->{'date_due'} = $data2->{'date_due'};
1036 $data->{'cardnumber'} = $data2->{'cardnumber'};
1037 $data->{'borrowernumber'} = $data2->{'borrowernumber'};
1040 # set date_due to blank, so in the template we check itemlost, and wthdrawn
1041 $data->{'date_due'} = '';
1044 # Find the last 3 people who borrowed this item.
1045 my $query2 = "SELECT * FROM issues, borrowers WHERE itemnumber = ?
1046 AND issues.borrowernumber = borrowers.borrowernumber
1047 AND returndate is not NULL
1048 ORDER BY returndate desc,timestamp desc LIMIT 3";
1049 $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
1050 $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
1052 while ( my $data2 = $sth2->fetchrow_hashref ) {
1053 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1054 $data->{"card$i2"} = $data2->{'cardnumber'};
1055 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1059 push(@results,$data);
1069 @results = GetItemsInfo($biblionumber, $type);
1073 Returns information about books with the given biblionumber.
1075 C<$type> may be either C<intra> or anything else. If it is not set to
1076 C<intra>, then the search will exclude lost, very overdue, and
1079 C<GetItemsInfo> returns a list of references-to-hash. Each element
1080 contains a number of keys. Most of them are table items from the
1081 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1082 Koha database. Other keys include:
1086 =item C<$data-E<gt>{branchname}>
1088 The name (not the code) of the branch to which the book belongs.
1090 =item C<$data-E<gt>{datelastseen}>
1092 This is simply C<items.datelastseen>, except that while the date is
1093 stored in YYYY-MM-DD format in the database, here it is converted to
1094 DD/MM/YYYY format. A NULL date is returned as C<//>.
1096 =item C<$data-E<gt>{datedue}>
1098 =item C<$data-E<gt>{class}>
1100 This is the concatenation of C<biblioitems.classification>, the book's
1101 Dewey code, and C<biblioitems.subclass>.
1103 =item C<$data-E<gt>{ocount}>
1105 I think this is the number of copies of the book available.
1107 =item C<$data-E<gt>{order}>
1109 If this is set, it is set to C<One Order>.
1116 my ( $biblionumber, $type ) = @_;
1117 my $dbh = C4::Context->dbh;
1118 my $query = "SELECT *,items.notforloan as itemnotforloan
1120 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1121 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
1122 $query .= (C4::Context->preference('item-level_itypes')) ?
1123 " LEFT JOIN itemtypes on items.itype = itemtypes.itemtype "
1124 : " LEFT JOIN itemtypes on biblioitems.itemtype = itemtypes.itemtype ";
1125 $query .= "WHERE items.biblionumber = ? ORDER BY items.dateaccessioned desc" ;
1126 my $sth = $dbh->prepare($query);
1127 $sth->execute($biblionumber);
1130 my ( $date_due, $count_reserves, $serial );
1132 my $isth = $dbh->prepare(
1133 "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname,borrowers.branchcode as bcode
1134 FROM issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
1135 WHERE itemnumber = ?
1136 AND returndate IS NULL"
1138 my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=? ");
1139 while ( my $data = $sth->fetchrow_hashref ) {
1141 $isth->execute( $data->{'itemnumber'} );
1142 if ( my $idata = $isth->fetchrow_hashref ) {
1143 $data->{borrowernumber} = $idata->{borrowernumber};
1144 $data->{cardnumber} = $idata->{cardnumber};
1145 $data->{surname} = $idata->{surname};
1146 $data->{firstname} = $idata->{firstname};
1147 $datedue = $idata->{'date_due'};
1148 if (C4::Context->preference("IndependantBranches")){
1149 my $userenv = C4::Context->userenv;
1150 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1151 $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch});
1155 if ( $data->{'serial'}) {
1156 $ssth->execute($data->{'itemnumber'}) ;
1157 ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
1160 if ( $datedue eq '' ) {
1161 my ( $restype, $reserves ) =
1162 C4::Reserves::CheckReserves( $data->{'itemnumber'} );
1164 $count_reserves = $restype;
1169 #get branch information.....
1170 my $bsth = $dbh->prepare(
1171 "SELECT * FROM branches WHERE branchcode = ?
1174 $bsth->execute( $data->{'holdingbranch'} );
1175 if ( my $bdata = $bsth->fetchrow_hashref ) {
1176 $data->{'branchname'} = $bdata->{'branchname'};
1178 $data->{'datedue'} = $datedue;
1179 $data->{'count_reserves'} = $count_reserves;
1181 # get notforloan complete status if applicable
1182 my $sthnflstatus = $dbh->prepare(
1183 'SELECT authorised_value
1184 FROM marc_subfield_structure
1185 WHERE kohafield="items.notforloan"
1189 $sthnflstatus->execute;
1190 my ($authorised_valuecode) = $sthnflstatus->fetchrow;
1191 if ($authorised_valuecode) {
1192 $sthnflstatus = $dbh->prepare(
1193 "SELECT lib FROM authorised_values
1195 AND authorised_value=?"
1197 $sthnflstatus->execute( $authorised_valuecode,
1198 $data->{itemnotforloan} );
1199 my ($lib) = $sthnflstatus->fetchrow;
1200 $data->{notforloanvalue} = $lib;
1203 # my stack procedures
1204 my $stackstatus = $dbh->prepare(
1205 'SELECT authorised_value
1206 FROM marc_subfield_structure
1207 WHERE kohafield="items.stack"
1210 $stackstatus->execute;
1212 ($authorised_valuecode) = $stackstatus->fetchrow;
1213 if ($authorised_valuecode) {
1214 $stackstatus = $dbh->prepare(
1216 FROM authorised_values
1218 AND authorised_value=?
1221 $stackstatus->execute( $authorised_valuecode, $data->{stack} );
1222 my ($lib) = $stackstatus->fetchrow;
1223 $data->{stack} = $lib;
1225 # Find the last 3 people who borrowed this item.
1226 my $sth2 = $dbh->prepare("SELECT * FROM issues,borrowers
1227 WHERE itemnumber = ?
1228 AND issues.borrowernumber = borrowers.borrowernumber
1229 AND returndate IS NOT NULL LIMIT 3");
1230 $sth2->execute($data->{'itemnumber'});
1232 while (my $data2 = $sth2->fetchrow_hashref()) {
1233 $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
1234 $data->{"card$ii"} = $data2->{'cardnumber'} if $data2->{'cardnumber'};
1235 $data->{"borrower$ii"} = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
1239 $results[$i] = $data;
1244 return( sort { $b->{'publisheddate'} cmp $a->{'publisheddate'} } @results );
1250 =head2 get_itemnumbers_of
1254 my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
1258 Given a list of biblionumbers, return the list of corresponding itemnumbers
1259 for each biblionumber.
1261 Return a reference on a hash where keys are biblionumbers and values are
1262 references on array of itemnumbers.
1266 sub get_itemnumbers_of {
1267 my @biblionumbers = @_;
1269 my $dbh = C4::Context->dbh;
1275 WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ')
1277 my $sth = $dbh->prepare($query);
1278 $sth->execute(@biblionumbers);
1282 while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
1283 push @{ $itemnumbers_of{$biblionumber} }, $itemnumber;
1286 return \%itemnumbers_of;
1289 =head2 GetItemnumberFromBarcode
1293 $result = GetItemnumberFromBarcode($barcode);
1299 sub GetItemnumberFromBarcode {
1301 my $dbh = C4::Context->dbh;
1304 $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
1305 $rq->execute($barcode);
1306 my ($result) = $rq->fetchrow;
1310 =head1 LIMITED USE FUNCTIONS
1312 The following functions, while part of the public API,
1313 are not exported. This is generally because they are
1314 meant to be used by only one script for a specific
1315 purpose, and should not be used in any other context
1316 without careful thought.
1324 my $item_marc = GetMarcItem($biblionumber, $itemnumber);
1328 Returns MARC::Record of the item passed in parameter.
1329 This function is meant for use only in C<cataloguing/additem.pl>,
1330 where it is needed to support that script's MARC-like
1336 my ( $biblionumber, $itemnumber ) = @_;
1338 # GetMarcItem has been revised so that it does the following:
1339 # 1. Gets the item information from the items table.
1340 # 2. Converts it to a MARC field for storage in the bib record.
1342 # The previous behavior was:
1343 # 1. Get the bib record.
1344 # 2. Return the MARC tag corresponding to the item record.
1346 # The difference is that one treats the items row as authoritative,
1347 # while the other treats the MARC representation as authoritative
1348 # under certain circumstances.
1350 my $itemrecord = GetItem($itemnumber);
1352 # Tack on 'items.' prefix to column names so that TransformKohaToMarc will work.
1353 # Also, don't emit a subfield if the underlying field is blank.
1354 my $mungeditem = { map { $itemrecord->{$_} ne '' ? ("items.$_" => $itemrecord->{$_}) : () } keys %{ $itemrecord } };
1355 my $itemmarc = TransformKohaToMarc($mungeditem);
1357 my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1358 if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1359 my @fields = $itemmarc->fields();
1360 if ($#fields > -1) {
1361 $fields[0]->add_subfields(@$unlinked_item_subfields);
1369 =head1 PRIVATE FUNCTIONS AND VARIABLES
1371 The following functions are not meant to be called
1372 directly, but are documented in order to explain
1373 the inner workings of C<C4::Items>.
1377 =head2 %derived_columns
1379 This hash keeps track of item columns that
1380 are strictly derived from other columns in
1381 the item record and are not meant to be set
1384 Each key in the hash should be the name of a
1385 column (as named by TransformMarcToKoha). Each
1386 value should be hashref whose keys are the
1387 columns on which the derived column depends. The
1388 hashref should also contain a 'BUILDER' key
1389 that is a reference to a sub that calculates
1394 my %derived_columns = (
1395 'items.cn_sort' => {
1396 'itemcallnumber' => 1,
1397 'items.cn_source' => 1,
1398 'BUILDER' => \&_calc_items_cn_sort,
1402 =head2 _set_derived_columns_for_add
1406 _set_derived_column_for_add($item);
1410 Given an item hash representing a new item to be added,
1411 calculate any derived columns. Currently the only
1412 such column is C<items.cn_sort>.
1416 sub _set_derived_columns_for_add {
1419 foreach my $column (keys %derived_columns) {
1420 my $builder = $derived_columns{$column}->{'BUILDER'};
1421 my $source_values = {};
1422 foreach my $source_column (keys %{ $derived_columns{$column} }) {
1423 next if $source_column eq 'BUILDER';
1424 $source_values->{$source_column} = $item->{$source_column};
1426 $builder->($item, $source_values);
1430 =head2 _set_derived_columns_for_mod
1434 _set_derived_column_for_mod($item);
1438 Given an item hash representing a new item to be modified.
1439 calculate any derived columns. Currently the only
1440 such column is C<items.cn_sort>.
1442 This routine differs from C<_set_derived_columns_for_add>
1443 in that it needs to handle partial item records. In other
1444 words, the caller of C<ModItem> may have supplied only one
1445 or two columns to be changed, so this function needs to
1446 determine whether any of the columns to be changed affect
1447 any of the derived columns. Also, if a derived column
1448 depends on more than one column, but the caller is not
1449 changing all of then, this routine retrieves the unchanged
1450 values from the database in order to ensure a correct
1455 sub _set_derived_columns_for_mod {
1458 foreach my $column (keys %derived_columns) {
1459 my $builder = $derived_columns{$column}->{'BUILDER'};
1460 my $source_values = {};
1461 my %missing_sources = ();
1462 my $must_recalc = 0;
1463 foreach my $source_column (keys %{ $derived_columns{$column} }) {
1464 next if $source_column eq 'BUILDER';
1465 if (exists $item->{$source_column}) {
1467 $source_values->{$source_column} = $item->{$source_column};
1469 $missing_sources{$source_column} = 1;
1473 foreach my $source_column (keys %missing_sources) {
1474 $source_values->{$source_column} = _get_single_item_column($source_column, $item->{'itemnumber'});
1476 $builder->($item, $source_values);
1481 =head2 _do_column_fixes_for_mod
1485 _do_column_fixes_for_mod($item);
1489 Given an item hashref containing one or more
1490 columns to modify, fix up certain values.
1491 Specifically, set to 0 any passed value
1492 of C<notforloan>, C<damaged>, C<itemlost>, or
1493 C<wthdrawn> that is either undefined or
1494 contains the empty string.
1498 sub _do_column_fixes_for_mod {
1501 if (exists $item->{'notforloan'} and
1502 (not defined $item->{'notforloan'} or $item->{'notforloan'} eq '')) {
1503 $item->{'notforloan'} = 0;
1505 if (exists $item->{'damaged'} and
1506 (not defined $item->{'damaged'} or $item->{'damaged'} eq '')) {
1507 $item->{'damaged'} = 0;
1509 if (exists $item->{'itemlost'} and
1510 (not defined $item->{'itemlost'} or $item->{'itemlost'} eq '')) {
1511 $item->{'itemlost'} = 0;
1513 if (exists $item->{'wthdrawn'} and
1514 (not defined $item->{'wthdrawn'} or $item->{'wthdrawn'} eq '')) {
1515 $item->{'wthdrawn'} = 0;
1519 =head2 _get_single_item_column
1523 _get_single_item_column($column, $itemnumber);
1527 Retrieves the value of a single column from an C<items>
1528 row specified by C<$itemnumber>.
1532 sub _get_single_item_column {
1534 my $itemnumber = shift;
1536 my $dbh = C4::Context->dbh;
1537 my $sth = $dbh->prepare("SELECT $column FROM items WHERE itemnumber = ?");
1538 $sth->execute($itemnumber);
1539 my ($value) = $sth->fetchrow();
1543 =head2 _calc_items_cn_sort
1547 _calc_items_cn_sort($item, $source_values);
1551 Helper routine to calculate C<items.cn_sort>.
1555 sub _calc_items_cn_sort {
1557 my $source_values = shift;
1559 $item->{'items.cn_sort'} = GetClassSort($source_values->{'items.cn_source'}, $source_values->{'itemcallnumber'}, "");
1562 =head2 _set_defaults_for_add
1566 _set_defaults_for_add($item_hash);
1570 Given an item hash representing an item to be added, set
1571 correct default values for columns whose default value
1572 is not handled by the DBMS. This includes the following
1579 C<items.dateaccessioned>
1601 sub _set_defaults_for_add {
1604 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1605 if (!(exists $item->{'dateaccessioned'}) ||
1606 ($item->{'dateaccessioned'} eq '')) {
1607 # FIXME add check for invalid date
1608 my $today = C4::Dates->new();
1609 $item->{'dateaccessioned'} = $today->output("iso"); #TODO: check time issues
1612 # various item status fields cannot be null
1613 $item->{'notforloan'} = 0 unless exists $item->{'notforloan'} and defined $item->{'notforloan'} and $item->{'notforloan'} ne '';
1614 $item->{'damaged'} = 0 unless exists $item->{'damaged'} and defined $item->{'damaged'} and $item->{'damaged'} ne '';
1615 $item->{'itemlost'} = 0 unless exists $item->{'itemlost'} and defined $item->{'itemlost'} and $item->{'itemlost'} ne '';
1616 $item->{'wthdrawn'} = 0 unless exists $item->{'wthdrawn'} and defined $item->{'wthdrawn'} and $item->{'wthdrawn'} ne '';
1619 =head2 _koha_new_item
1623 my ($itemnumber,$error) = _koha_new_item( $item, $barcode );
1627 Perform the actual insert into the C<items> table.
1631 sub _koha_new_item {
1632 my ( $item, $barcode ) = @_;
1633 my $dbh=C4::Context->dbh;
1636 "INSERT INTO items SET
1638 biblioitemnumber = ?,
1640 dateaccessioned = ?,
1644 replacementprice = ?,
1645 replacementpricedate = NOW(),
1646 datelastborrowed = ?,
1647 datelastseen = NOW(),
1669 more_subfields_xml = ?
1671 my $sth = $dbh->prepare($query);
1673 $item->{'biblionumber'},
1674 $item->{'biblioitemnumber'},
1676 $item->{'dateaccessioned'},
1677 $item->{'booksellerid'},
1678 $item->{'homebranch'},
1680 $item->{'replacementprice'},
1681 $item->{datelastborrowed},
1683 $item->{'notforloan'},
1685 $item->{'itemlost'},
1686 $item->{'wthdrawn'},
1687 $item->{'itemcallnumber'},
1688 $item->{'restricted'},
1689 $item->{'itemnotes'},
1690 $item->{'holdingbranch'},
1692 $item->{'location'},
1695 $item->{'renewals'},
1696 $item->{'reserves'},
1697 $item->{'items.cn_source'},
1698 $item->{'items.cn_sort'},
1701 $item->{'materials'},
1703 $item->{'more_subfields_xml'},
1705 my $itemnumber = $dbh->{'mysql_insertid'};
1706 if ( defined $sth->errstr ) {
1707 $error.="ERROR in _koha_new_item $query".$sth->errstr;
1710 return ( $itemnumber, $error );
1713 =head2 _koha_modify_item
1717 my ($itemnumber,$error) =_koha_modify_item( $item );
1721 Perform the actual update of the C<items> row. Note that this
1722 routine accepts a hashref specifying the columns to update.
1726 sub _koha_modify_item {
1728 my $dbh=C4::Context->dbh;
1731 my $query = "UPDATE items SET ";
1733 for my $key ( keys %$item ) {
1735 push @bind, $item->{$key};
1738 $query .= " WHERE itemnumber=?";
1739 push @bind, $item->{'itemnumber'};
1740 my $sth = C4::Context->dbh->prepare($query);
1741 $sth->execute(@bind);
1742 if ( C4::Context->dbh->errstr ) {
1743 $error.="ERROR in _koha_modify_item $query".$dbh->errstr;
1747 return ($item->{'itemnumber'},$error);
1750 =head2 _koha_delete_item
1754 _koha_delete_item( $dbh, $itemnum );
1758 Internal function to delete an item record from the koha tables
1762 sub _koha_delete_item {
1763 my ( $dbh, $itemnum ) = @_;
1765 # save the deleted item to deleteditems table
1766 my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
1767 $sth->execute($itemnum);
1768 my $data = $sth->fetchrow_hashref();
1770 my $query = "INSERT INTO deleteditems SET ";
1772 foreach my $key ( keys %$data ) {
1773 $query .= "$key = ?,";
1774 push( @bind, $data->{$key} );
1777 $sth = $dbh->prepare($query);
1778 $sth->execute(@bind);
1781 # delete from items table
1782 $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
1783 $sth->execute($itemnum);
1788 =head2 _marc_from_item_hash
1792 my $item_marc = _marc_from_item_hash($item, $frameworkcode[, $unlinked_item_subfields]);
1796 Given an item hash representing a complete item record,
1797 create a C<MARC::Record> object containing an embedded
1798 tag representing that item.
1800 The third, optional parameter C<$unlinked_item_subfields> is
1801 an arrayref of subfields (not mapped to C<items> fields per the
1802 framework) to be added to the MARC representation
1807 sub _marc_from_item_hash {
1809 my $frameworkcode = shift;
1810 my $unlinked_item_subfields;
1812 $unlinked_item_subfields = shift;
1815 # Tack on 'items.' prefix to column names so lookup from MARC frameworks will work
1816 # Also, don't emit a subfield if the underlying field is blank.
1817 my $mungeditem = { map { (defined($item->{$_}) and $item->{$_} ne '') ?
1818 (/^items\./ ? ($_ => $item->{$_}) : ("items.$_" => $item->{$_}))
1819 : () } keys %{ $item } };
1821 my $item_marc = MARC::Record->new();
1822 foreach my $item_field (keys %{ $mungeditem }) {
1823 my ($tag, $subfield) = GetMarcFromKohaField($item_field, $frameworkcode);
1824 next unless defined $tag and defined $subfield; # skip if not mapped to MARC field
1825 if (my $field = $item_marc->field($tag)) {
1826 $field->add_subfields($subfield => $mungeditem->{$item_field});
1828 my $add_subfields = [];
1829 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
1830 $add_subfields = $unlinked_item_subfields;
1832 $item_marc->add_fields( $tag, " ", " ", $subfield => $mungeditem->{$item_field}, @$add_subfields);
1839 =head2 _add_item_field_to_biblio
1843 _add_item_field_to_biblio($item_marc, $biblionumber, $frameworkcode);
1847 Adds the fields from a MARC record containing the
1848 representation of a Koha item record to the MARC
1849 biblio record. The input C<$item_marc> record
1850 is expect to contain just one field, the embedded
1851 item information field.
1855 sub _add_item_field_to_biblio {
1856 my ($item_marc, $biblionumber, $frameworkcode) = @_;
1858 my $biblio_marc = GetMarcBiblio($biblionumber);
1860 foreach my $field ($item_marc->fields()) {
1861 $biblio_marc->append_fields($field);
1864 ModBiblioMarc($biblio_marc, $biblionumber, $frameworkcode);
1867 =head2 _replace_item_field_in_biblio
1871 &_replace_item_field_in_biblio($item_marc, $biblionumber, $itemnumber, $frameworkcode)
1875 Given a MARC::Record C<$item_marc> containing one tag with the MARC
1876 representation of the item, examine the biblio MARC
1877 for the corresponding tag for that item and
1878 replace it with the tag from C<$item_marc>.
1882 sub _replace_item_field_in_biblio {
1883 my ($ItemRecord, $biblionumber, $itemnumber, $frameworkcode) = @_;
1884 my $dbh = C4::Context->dbh;
1886 # get complete MARC record & replace the item field by the new one
1887 my $completeRecord = GetMarcBiblio($biblionumber);
1888 my ($itemtag,$itemsubfield) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
1889 my $itemField = $ItemRecord->field($itemtag);
1890 my @items = $completeRecord->field($itemtag);
1893 if ($_->subfield($itemsubfield) eq $itemnumber) {
1894 $_->replace_with($itemField);
1900 # If we haven't found the matching field,
1901 # just add it. However, this means that
1902 # there is likely a bug.
1903 $completeRecord->append_fields($itemField);
1907 ModBiblioMarc($completeRecord, $biblionumber, $frameworkcode);
1910 =head2 _repack_item_errors
1912 Add an error message hash generated by C<CheckItemPreSave>
1913 to a list of errors.
1917 sub _repack_item_errors {
1918 my $item_sequence_num = shift;
1919 my $item_ref = shift;
1920 my $error_ref = shift;
1922 my @repacked_errors = ();
1924 foreach my $error_code (sort keys %{ $error_ref }) {
1925 my $repacked_error = {};
1926 $repacked_error->{'item_sequence'} = $item_sequence_num;
1927 $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ? $item_ref->{'barcode'} : '';
1928 $repacked_error->{'error_code'} = $error_code;
1929 $repacked_error->{'error_information'} = $error_ref->{$error_code};
1930 push @repacked_errors, $repacked_error;
1933 return @repacked_errors;
1936 =head2 _get_unlinked_item_subfields
1940 my $unlinked_item_subfields = _get_unlinked_item_subfields($original_item_marc, $frameworkcode);
1946 sub _get_unlinked_item_subfields {
1947 my $original_item_marc = shift;
1948 my $frameworkcode = shift;
1950 my $marcstructure = GetMarcStructure(1, $frameworkcode);
1952 # assume that this record has only one field, and that that
1953 # field contains only the item information
1955 my @fields = $original_item_marc->fields();
1956 if ($#fields > -1) {
1957 my $field = $fields[0];
1958 my $tag = $field->tag();
1959 foreach my $subfield ($field->subfields()) {
1960 if (defined $subfield->[1] and
1961 $subfield->[1] ne '' and
1962 !$marcstructure->{$tag}->{$subfield->[0]}->{'kohafield'}) {
1963 push @$subfields, $subfield->[0] => $subfield->[1];
1970 =head2 _get_unlinked_subfields_xml
1974 my $unlinked_subfields_xml = _get_unlinked_subfields_xml($unlinked_item_subfields);
1980 sub _get_unlinked_subfields_xml {
1981 my $unlinked_item_subfields = shift;
1984 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
1985 my $marc = MARC::Record->new();
1986 # use of tag 999 is arbitrary, and doesn't need to match the item tag
1987 # used in the framework
1988 $marc->append_fields(MARC::Field->new('999', ' ', ' ', @$unlinked_item_subfields));
1989 $xml = $marc->as_xml();
1995 =head2 _parse_unlinked_item_subfields_from_xml
1999 my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'}):
2005 sub _parse_unlinked_item_subfields_from_xml {
2008 return unless defined $xml and $xml ne "";
2009 my $marc = MARC::Record->new_from_xml(StripNonXmlChars($xml), 'UTF-8', C4::Context->preference("marcflavour"));
2010 my $unlinked_subfields = [];
2011 my @fields = $marc->fields();
2012 if ($#fields > -1) {
2013 foreach my $subfield ($fields[0]->subfields()) {
2014 push @$unlinked_subfields, $subfield->[0] => $subfield->[1];
2017 return $unlinked_subfields;