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/;
32 use vars qw($VERSION @ISA @EXPORT);
38 @ISA = qw( Exporter );
60 GetItemsByBiblioitemnumber
63 GetItemnumberFromBarcode
69 C4::Items - item management functions
73 This module contains an API for manipulating item
74 records in Koha, and is used by cataloguing, circulation,
75 acquisitions, and serials management.
77 A Koha item record is stored in two places: the
78 items table and embedded in a MARC tag in the XML
79 version of the associated bib record in C<biblioitems.marcxml>.
80 This is done to allow the item information to be readily
81 indexed (e.g., by Zebra), but means that each item
82 modification transaction must keep the items table
83 and the MARC XML in sync at all times.
85 Consequently, all code that creates, modifies, or deletes
86 item records B<must> use an appropriate function from
87 C<C4::Items>. If no existing function is suitable, it is
88 better to add one to C<C4::Items> than to use add
89 one-off SQL statements to add or modify items.
91 The items table will be considered authoritative. In other
92 words, if there is ever a discrepancy between the items
93 table and the MARC XML, the items table should be considered
96 =head1 HISTORICAL NOTE
98 Most of the functions in C<C4::Items> were originally in
99 the C<C4::Biblio> module.
101 =head1 CORE EXPORTED FUNCTIONS
103 The following functions are meant for use by users
112 $item = GetItem($itemnumber,$barcode,$serial);
116 Return item information, for a given itemnumber or barcode.
117 The return value is a hashref mapping item column
118 names to values. If C<$serial> is true, include serial publication data.
123 my ($itemnumber,$barcode, $serial) = @_;
124 my $dbh = C4::Context->dbh;
127 my $sth = $dbh->prepare("
129 WHERE itemnumber = ?");
130 $sth->execute($itemnumber);
131 $data = $sth->fetchrow_hashref;
133 my $sth = $dbh->prepare("
137 $sth->execute($barcode);
138 $data = $sth->fetchrow_hashref;
141 my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=?");
142 $ssth->execute($data->{'itemnumber'}) ;
143 ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
144 warn $data->{'serialseq'} , $data->{'publisheddate'};
149 =head2 AddItemFromMarc
153 my ($biblionumber, $biblioitemnumber, $itemnumber)
154 = AddItemFromMarc($source_item_marc, $biblionumber);
158 Given a MARC::Record object containing an embedded item
159 record and a biblionumber, create a new item record.
163 sub AddItemFromMarc {
164 my ( $source_item_marc, $biblionumber ) = @_;
165 my $dbh = C4::Context->dbh;
167 # parse item hash from MARC
168 my $frameworkcode = GetFrameworkCode( $biblionumber );
169 my $item = &TransformMarcToKoha( $dbh, $source_item_marc, $frameworkcode );
170 return AddItem($item, $biblionumber, $dbh, $frameworkcode,$source_item_marc);
177 my ($biblionumber, $biblioitemnumber, $itemnumber)
178 = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $original_item_marc]);
182 Given a hash containing item column names as keys,
183 create a new Koha item record.
185 The first two optional parameters (C<$dbh> and C<$frameworkcode>)
186 do not need to be supplied for general use; they exist
187 simply to allow them to be picked up from AddItemFromMarc.
189 The final optional parameter, C<$original_item_marc>, contains
190 a C<MARC::Record> object containing the original MARC item tag
191 that the item record came from, either from the item editor or
192 the source MARC record during a bulk import. This is used
193 to supply item-level subfields that are not directly mapped to
194 a column in the C<items> table but should remain available for
195 display and indexing.
201 my $biblionumber = shift;
203 my $dbh = @_ ? shift : C4::Context->dbh;
204 my $frameworkcode = @_ ? shift : GetFrameworkCode( $biblionumber );
205 my $original_item_marc;
207 $original_item_marc = shift
210 # needs old biblionumber and biblioitemnumber
211 $item->{'biblionumber'} = $biblionumber;
212 my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
213 $sth->execute( $item->{'biblionumber'} );
214 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
216 _set_defaults_for_add($item);
217 _set_derived_columns_for_add($item);
218 # FIXME - checks here
219 my ( $itemnumber, $error ) = _koha_new_item( $item, $item->{barcode} );
220 $item->{'itemnumber'} = $itemnumber;
222 # create MARC tag representing item and add to bib
223 my $new_item_marc = _marc_from_item_hash($item, $frameworkcode);
224 if ($original_item_marc) {
225 _add_unlinked_marc_fields($new_item_marc,$original_item_marc,$frameworkcode);
227 _add_item_field_to_biblio($new_item_marc, $item->{'biblionumber'}, $frameworkcode );
229 logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
230 if C4::Context->preference("CataloguingLog");
232 return ($item->{biblionumber}, $item->{biblioitemnumber}, $itemnumber);
235 =head2 AddItemBatchFromMarc
239 ($itemnumber_ref, $error_ref) = AddItemBatchFromMarc($record, $biblionumber, $biblioitemnumber, $frameworkcode);
243 Efficiently create item records from a MARC biblio record with
244 embedded item fields. This routine is suitable for batch jobs.
246 This API assumes that the bib record has already been
247 saved to the C<biblio> and C<biblioitems> tables. It does
248 not expect that C<biblioitems.marc> and C<biblioitems.marcxml>
249 are populated, but it will do so via a call to ModBibiloMarc.
251 The goal of this API is to have a similar effect to using AddBiblio
252 and AddItems in succession, but without inefficient repeated
253 parsing of the MARC XML bib record.
255 This function returns an arrayref of new itemsnumbers and an arrayref of item
256 errors encountered during the processing. Each entry in the errors
257 list is a hashref containing the following keys:
263 Sequence number of original item tag in the MARC record.
267 Item barcode, provide to assist in the construction of
268 useful error messages.
270 =item error_condition
272 Code representing the error condition. Can be 'duplicate_barcode',
273 'invalid_homebranch', or 'invalid_holdingbranch'.
275 =item error_information
277 Additional information appropriate to the error condition.
283 sub AddItemBatchFromMarc {
284 my ($record, $biblionumber, $biblioitemnumber, $frameworkcode) = @_;
286 my @itemnumbers = ();
288 my $dbh = C4::Context->dbh;
290 # loop through the item tags and start creating items
291 my @bad_item_fields = ();
292 my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
293 my $item_sequence_num = 0;
294 ITEMFIELD: foreach my $item_field ($record->field($itemtag)) {
295 $item_sequence_num++;
296 # we take the item field and stick it into a new
297 # MARC record -- this is required so far because (FIXME)
298 # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
299 # and there is no TransformMarcFieldToKoha
300 my $temp_item_marc = MARC::Record->new();
301 $temp_item_marc->append_fields($item_field);
303 # add biblionumber and biblioitemnumber
304 my $item = TransformMarcToKoha( $dbh, $temp_item_marc, $frameworkcode, 'items' );
305 $item->{'biblionumber'} = $biblionumber;
306 $item->{'biblioitemnumber'} = $biblioitemnumber;
308 # check for duplicate barcode
309 my %item_errors = CheckItemPreSave($item);
311 push @errors, _repack_item_errors($item_sequence_num, $item, \%item_errors);
312 push @bad_item_fields, $item_field;
316 _set_defaults_for_add($item);
317 _set_derived_columns_for_add($item);
318 my ( $itemnumber, $error ) = _koha_new_item( $item, $item->{barcode} );
319 warn $error if $error;
320 push @itemnumbers, $itemnumber; # FIXME not checking error
321 $item->{'itemnumber'} = $itemnumber;
323 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
324 if C4::Context->preference("CataloguingLog");
326 my $new_item_marc = _marc_from_item_hash($item, $frameworkcode);
327 # FIXME - add _add_unlinked_marc_fields here?
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 );
366 return ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $item_marc);
373 ModItem({ column => $newvalue }, $biblionumber, $itemnumber[, $original_item_marc]);
377 Change one or more columns in an item record and update
378 the MARC representation of the item.
380 The first argument is a hashref mapping from item column
381 names to the new values. The second and third arguments
382 are the biblionumber and itemnumber, respectively.
384 The fourth, optional parameter, C<$original_item_marc>, contains
385 a C<MARC::Record> object containing the original MARC item tag
386 that the item record came from if the item is being modified
387 via the the cataloging item editor. This is used
388 to supply item-level subfields that are not directly mapped to
389 a column in the C<items> table but should remain available for
390 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 $original_item_marc;
414 $original_item_marc = shift
417 $item->{'itemnumber'} = $itemnumber or return undef;
418 _set_derived_columns_for_mod($item);
419 _do_column_fixes_for_mod($item);
422 # attempt to change itemnumber
423 # attempt to change biblionumber (if we want
424 # an API to relink an item to a different bib,
425 # it should be a separate function)
428 _koha_modify_item($item);
430 # update biblio MARC XML
431 my $whole_item = GetItem($itemnumber) or die "FAILED GetItem($itemnumber)";
432 my $new_item_marc = _marc_from_item_hash($whole_item, $frameworkcode)
433 or die "FAILED _marc_from_item_hash($whole_item, $frameworkcode)";
435 # FIXME - handle case where item is being modified but must carry
436 # additional unlnked subfields
437 if ($original_item_marc) {
438 _add_unlinked_marc_fields($new_item_marc,$original_item_marc,$frameworkcode);
441 _replace_item_field_in_biblio($new_item_marc, $biblionumber, $itemnumber, $frameworkcode);
442 (C4::Context->userenv eq '0') and die "userenv is '0', not hashref"; # logaction line would crash anyway
443 ($new_item_marc eq '0') and die "$new_item_marc is '0', not hashref"; # logaction line would crash anyway
444 logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$itemnumber,$new_item_marc->as_formatted)
445 if C4::Context->preference("CataloguingLog");
448 =head2 ModItemTransfer
452 ModItemTransfer($itenumber, $frombranch, $tobranch);
456 Marks an item as being transferred from one branch
461 sub ModItemTransfer {
462 my ( $itemnumber, $frombranch, $tobranch ) = @_;
464 my $dbh = C4::Context->dbh;
466 #new entry in branchtransfers....
467 my $sth = $dbh->prepare(
468 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
469 VALUES (?, ?, NOW(), ?)");
470 $sth->execute($itemnumber, $frombranch, $tobranch);
472 ModItem({ holdingbranch => $tobranch }, undef, $itemnumber);
473 ModDateLastSeen($itemnumber);
477 =head2 ModDateLastSeen
481 ModDateLastSeen($itemnum);
485 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking.
486 C<$itemnum> is the item number
490 sub ModDateLastSeen {
491 my ($itemnumber) = @_;
493 my $today = C4::Dates->new();
494 ModItem({ itemlost => 0, datelastseen => $today->output("iso") }, undef, $itemnumber);
501 DelItem($biblionumber, $itemnumber);
505 Exported function (core API) for deleting an item record in Koha.
510 my ( $dbh, $biblionumber, $itemnumber ) = @_;
512 # FIXME check the item has no current issues
514 _koha_delete_item( $dbh, $itemnumber );
516 # get the MARC record
517 my $record = GetMarcBiblio($biblionumber);
518 my $frameworkcode = GetFrameworkCode($biblionumber);
521 my $copy2deleted = $dbh->prepare("UPDATE deleteditems SET marc=? WHERE itemnumber=?");
522 $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
524 #search item field code
525 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
526 my @fields = $record->field($itemtag);
528 # delete the item specified
529 foreach my $field (@fields) {
530 if ( $field->subfield($itemsubfield) eq $itemnumber ) {
531 $record->delete_field($field);
534 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
535 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$itemnumber,"item")
536 if C4::Context->preference("CataloguingLog");
539 =head2 CheckItemPreSave
543 my $item_ref = TransformMarcToKoha($marc, 'items');
545 my %errors = CheckItemPreSave($item_ref);
546 if (exists $errors{'duplicate_barcode'}) {
547 print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
548 } elsif (exists $errors{'invalid_homebranch'}) {
549 print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
550 } elsif (exists $errors{'invalid_holdingbranch'}) {
551 print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
558 Given a hashref containing item fields, determine if it can be
559 inserted or updated in the database. Specifically, checks for
560 database integrity issues, and returns a hash containing any
561 of the following keys, if applicable.
565 =item duplicate_barcode
567 Barcode, if it duplicates one already found in the database.
569 =item invalid_homebranch
571 Home branch, if not defined in branches table.
573 =item invalid_holdingbranch
575 Holding branch, if not defined in branches table.
579 This function does NOT implement any policy-related checks,
580 e.g., whether current operator is allowed to save an
581 item that has a given branch code.
585 sub CheckItemPreSave {
586 my $item_ref = shift;
590 # check for duplicate barcode
591 if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
592 my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'});
593 if ($existing_itemnumber) {
594 if (!exists $item_ref->{'itemnumber'} # new item
595 or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
596 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
601 # check for valid home branch
602 if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
603 my $branch_name = GetBranchName($item_ref->{'homebranch'});
604 unless (defined $branch_name) {
605 # relies on fact that branches.branchname is a non-NULL column,
606 # so GetBranchName returns undef only if branch does not exist
607 $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
611 # check for valid holding branch
612 if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
613 my $branch_name = GetBranchName($item_ref->{'holdingbranch'});
614 unless (defined $branch_name) {
615 # relies on fact that branches.branchname is a non-NULL column,
616 # so GetBranchName returns undef only if branch does not exist
617 $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
625 =head1 EXPORTED SPECIAL ACCESSOR FUNCTIONS
627 The following functions provide various ways of
628 getting an item record, a set of item records, or
629 lists of authorized values for certain item fields.
631 Some of the functions in this group are candidates
632 for refactoring -- for example, some of the code
633 in C<GetItemsByBiblioitemnumber> and C<GetItemsInfo>
634 has copy-and-paste work.
642 $itemstatushash = GetItemStatus($fwkcode);
646 Returns a list of valid values for the
647 C<items.notforloan> field.
649 NOTE: does B<not> return an individual item's
652 Can be MARC dependant.
654 But basically could be can be loan or not
655 Create a status selector with the following code
657 =head3 in PERL SCRIPT
661 my $itemstatushash = getitemstatus;
663 foreach my $thisstatus (keys %$itemstatushash) {
664 my %row =(value => $thisstatus,
665 statusname => $itemstatushash->{$thisstatus}->{'statusname'},
667 push @itemstatusloop, \%row;
669 $template->param(statusloop=>\@itemstatusloop);
677 <select name="statusloop">
678 <option value="">Default</option>
679 <!-- TMPL_LOOP name="statusloop" -->
680 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option>
690 # returns a reference to a hash of references to status...
693 my $dbh = C4::Context->dbh;
695 $fwk = '' unless ($fwk);
696 my ( $tag, $subfield ) =
697 GetMarcFromKohaField( "items.notforloan", $fwk );
698 if ( $tag and $subfield ) {
701 "SELECT authorised_value
702 FROM marc_subfield_structure
708 $sth->execute( $tag, $subfield, $fwk );
709 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
712 "SELECT authorised_value,lib
713 FROM authorised_values
718 $authvalsth->execute($authorisedvaluecat);
719 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
720 $itemstatus{$authorisedvalue} = $lib;
736 $itemstatus{"1"} = "Not For Loan";
740 =head2 GetItemLocation
744 $itemlochash = GetItemLocation($fwk);
748 Returns a list of valid values for the
749 C<items.location> field.
751 NOTE: does B<not> return an individual item's
754 where fwk stands for an optional framework code.
755 Create a location selector with the following code
757 =head3 in PERL SCRIPT
761 my $itemlochash = getitemlocation;
763 foreach my $thisloc (keys %$itemlochash) {
764 my $selected = 1 if $thisbranch eq $branch;
765 my %row =(locval => $thisloc,
766 selected => $selected,
767 locname => $itemlochash->{$thisloc},
769 push @itemlocloop, \%row;
771 $template->param(itemlocationloop => \@itemlocloop);
779 <select name="location">
780 <option value="">Default</option>
781 <!-- TMPL_LOOP name="itemlocationloop" -->
782 <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
790 sub GetItemLocation {
792 # returns a reference to a hash of references to location...
795 my $dbh = C4::Context->dbh;
797 $fwk = '' unless ($fwk);
798 my ( $tag, $subfield ) =
799 GetMarcFromKohaField( "items.location", $fwk );
800 if ( $tag and $subfield ) {
803 "SELECT authorised_value
804 FROM marc_subfield_structure
809 $sth->execute( $tag, $subfield, $fwk );
810 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
813 "SELECT authorised_value,lib
814 FROM authorised_values
818 $authvalsth->execute($authorisedvaluecat);
819 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
820 $itemlocation{$authorisedvalue} = $lib;
823 return \%itemlocation;
836 $itemlocation{"1"} = "Not For Loan";
837 return \%itemlocation;
844 $items = GetLostItems($where,$orderby);
848 This function get the items lost into C<$items>.
853 C<$where> is a hashref. it containts a field of the items table as key
854 and the value to match as value.
855 C<$orderby> is a field of the items table.
858 C<$items> is a reference to an array full of hasref which keys are items' table column.
860 =item usage in the perl script:
863 $where{barcode} = 0001548;
864 my $items = GetLostItems( \%where, "homebranch" );
865 $template->param(itemsloop => $items);
872 # Getting input args.
875 my $dbh = C4::Context->dbh;
880 WHERE itemlost IS NOT NULL
883 foreach my $key (keys %$where) {
884 $query .= " AND " . $key . " LIKE '%" . $where->{$key} . "%'";
886 $query .= " ORDER BY ".$orderby if defined $orderby;
888 my $sth = $dbh->prepare($query);
891 while ( my $row = $sth->fetchrow_hashref ){
897 =head2 GetItemsForInventory
901 $itemlist = GetItemsForInventory($minlocation,$maxlocation,$datelastseen,$offset,$size)
905 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
907 The sub returns a list of hashes, containing itemnumber, author, title, barcode & item callnumber.
908 It is ordered by callnumber,title.
910 The minlocation & maxlocation parameters are used to specify a range of item callnumbers
911 the datelastseen can be used to specify that you want to see items not seen since a past date only.
912 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
916 sub GetItemsForInventory {
917 my ( $minlocation, $maxlocation,$location, $datelastseen, $branch, $offset, $size ) = @_;
918 my $dbh = C4::Context->dbh;
921 $datelastseen=format_date_in_iso($datelastseen);
923 "SELECT itemnumber,barcode,itemcallnumber,title,author,biblio.biblionumber,datelastseen
925 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
926 WHERE itemcallnumber>= ?
927 AND itemcallnumber <=?
928 AND (datelastseen< ? OR datelastseen IS NULL)";
929 $query.= " AND items.location=".$dbh->quote($location) if $location;
930 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
931 $query .= " ORDER BY itemcallnumber,title";
932 $sth = $dbh->prepare($query);
933 $sth->execute( $minlocation, $maxlocation, $datelastseen );
937 SELECT itemnumber,barcode,itemcallnumber,biblio.biblionumber,title,author,datelastseen
939 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
940 WHERE itemcallnumber>= ?
941 AND itemcallnumber <=?";
942 $query.= " AND items.location=".$dbh->quote($location) if $location;
943 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
944 $query .= " ORDER BY itemcallnumber,title";
945 $sth = $dbh->prepare($query);
946 $sth->execute( $minlocation, $maxlocation );
949 while ( my $row = $sth->fetchrow_hashref ) {
950 $offset-- if ($offset);
951 $row->{datelastseen}=format_date($row->{datelastseen});
952 if ( ( !$offset ) && $size ) {
963 $count = &GetItemsCount( $biblionumber);
967 This function return count of item with $biblionumber
972 my ( $biblionumber ) = @_;
973 my $dbh = C4::Context->dbh;
974 my $query = "SELECT count(*)
976 WHERE biblionumber=?";
977 my $sth = $dbh->prepare($query);
978 $sth->execute($biblionumber);
979 my $count = $sth->fetchrow;
984 =head2 GetItemInfosOf
988 GetItemInfosOf(@itemnumbers);
995 my @itemnumbers = @_;
1000 WHERE itemnumber IN (' . join( ',', @itemnumbers ) . ')
1002 return get_infos_of( $query, 'itemnumber' );
1005 =head2 GetItemsByBiblioitemnumber
1009 GetItemsByBiblioitemnumber($biblioitemnumber);
1013 Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
1014 Called by C<C4::XISBN>
1018 sub GetItemsByBiblioitemnumber {
1019 my ( $bibitem ) = @_;
1020 my $dbh = C4::Context->dbh;
1021 my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
1022 # Get all items attached to a biblioitem
1025 $sth->execute($bibitem) || die $sth->errstr;
1026 while ( my $data = $sth->fetchrow_hashref ) {
1027 # Foreach item, get circulation information
1028 my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
1029 WHERE itemnumber = ?
1030 AND returndate is NULL
1031 AND issues.borrowernumber = borrowers.borrowernumber"
1033 $sth2->execute( $data->{'itemnumber'} );
1034 if ( my $data2 = $sth2->fetchrow_hashref ) {
1035 # if item is out, set the due date and who it is out too
1036 $data->{'date_due'} = $data2->{'date_due'};
1037 $data->{'cardnumber'} = $data2->{'cardnumber'};
1038 $data->{'borrowernumber'} = $data2->{'borrowernumber'};
1041 # set date_due to blank, so in the template we check itemlost, and wthdrawn
1042 $data->{'date_due'} = '';
1045 # Find the last 3 people who borrowed this item.
1046 my $query2 = "SELECT * FROM issues, borrowers WHERE itemnumber = ?
1047 AND issues.borrowernumber = borrowers.borrowernumber
1048 AND returndate is not NULL
1049 ORDER BY returndate desc,timestamp desc LIMIT 3";
1050 $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
1051 $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
1053 while ( my $data2 = $sth2->fetchrow_hashref ) {
1054 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1055 $data->{"card$i2"} = $data2->{'cardnumber'};
1056 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1060 push(@results,$data);
1070 @results = GetItemsInfo($biblionumber, $type);
1074 Returns information about books with the given biblionumber.
1076 C<$type> may be either C<intra> or anything else. If it is not set to
1077 C<intra>, then the search will exclude lost, very overdue, and
1080 C<GetItemsInfo> returns a list of references-to-hash. Each element
1081 contains a number of keys. Most of them are table items from the
1082 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1083 Koha database. Other keys include:
1087 =item C<$data-E<gt>{branchname}>
1089 The name (not the code) of the branch to which the book belongs.
1091 =item C<$data-E<gt>{datelastseen}>
1093 This is simply C<items.datelastseen>, except that while the date is
1094 stored in YYYY-MM-DD format in the database, here it is converted to
1095 DD/MM/YYYY format. A NULL date is returned as C<//>.
1097 =item C<$data-E<gt>{datedue}>
1099 =item C<$data-E<gt>{class}>
1101 This is the concatenation of C<biblioitems.classification>, the book's
1102 Dewey code, and C<biblioitems.subclass>.
1104 =item C<$data-E<gt>{ocount}>
1106 I think this is the number of copies of the book available.
1108 =item C<$data-E<gt>{order}>
1110 If this is set, it is set to C<One Order>.
1117 my ( $biblionumber, $type ) = @_;
1118 my $dbh = C4::Context->dbh;
1119 my $query = "SELECT *,items.notforloan as itemnotforloan
1121 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1122 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
1123 $query .= (C4::Context->preference('item-level_itypes')) ?
1124 " LEFT JOIN itemtypes on items.itype = itemtypes.itemtype "
1125 : " LEFT JOIN itemtypes on biblioitems.itemtype = itemtypes.itemtype ";
1126 $query .= "WHERE items.biblionumber = ? ORDER BY items.dateaccessioned desc" ;
1127 my $sth = $dbh->prepare($query);
1128 $sth->execute($biblionumber);
1131 my ( $date_due, $count_reserves, $serial );
1133 my $isth = $dbh->prepare(
1134 "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname,borrowers.branchcode as bcode
1135 FROM issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
1136 WHERE itemnumber = ?
1137 AND returndate IS NULL"
1139 my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=? ");
1140 while ( my $data = $sth->fetchrow_hashref ) {
1142 $isth->execute( $data->{'itemnumber'} );
1143 if ( my $idata = $isth->fetchrow_hashref ) {
1144 $data->{borrowernumber} = $idata->{borrowernumber};
1145 $data->{cardnumber} = $idata->{cardnumber};
1146 $data->{surname} = $idata->{surname};
1147 $data->{firstname} = $idata->{firstname};
1148 $datedue = $idata->{'date_due'};
1149 if (C4::Context->preference("IndependantBranches")){
1150 my $userenv = C4::Context->userenv;
1151 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1152 $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch});
1156 if ( $data->{'serial'}) {
1157 $ssth->execute($data->{'itemnumber'}) ;
1158 ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
1161 if ( $datedue eq '' ) {
1162 my ( $restype, $reserves ) =
1163 C4::Reserves::CheckReserves( $data->{'itemnumber'} );
1165 $count_reserves = $restype;
1170 #get branch information.....
1171 my $bsth = $dbh->prepare(
1172 "SELECT * FROM branches WHERE branchcode = ?
1175 $bsth->execute( $data->{'holdingbranch'} );
1176 if ( my $bdata = $bsth->fetchrow_hashref ) {
1177 $data->{'branchname'} = $bdata->{'branchname'};
1179 $data->{'datedue'} = $datedue;
1180 $data->{'count_reserves'} = $count_reserves;
1182 # get notforloan complete status if applicable
1183 my $sthnflstatus = $dbh->prepare(
1184 'SELECT authorised_value
1185 FROM marc_subfield_structure
1186 WHERE kohafield="items.notforloan"
1190 $sthnflstatus->execute;
1191 my ($authorised_valuecode) = $sthnflstatus->fetchrow;
1192 if ($authorised_valuecode) {
1193 $sthnflstatus = $dbh->prepare(
1194 "SELECT lib FROM authorised_values
1196 AND authorised_value=?"
1198 $sthnflstatus->execute( $authorised_valuecode,
1199 $data->{itemnotforloan} );
1200 my ($lib) = $sthnflstatus->fetchrow;
1201 $data->{notforloanvalue} = $lib;
1204 # my stack procedures
1205 my $stackstatus = $dbh->prepare(
1206 'SELECT authorised_value
1207 FROM marc_subfield_structure
1208 WHERE kohafield="items.stack"
1211 $stackstatus->execute;
1213 ($authorised_valuecode) = $stackstatus->fetchrow;
1214 if ($authorised_valuecode) {
1215 $stackstatus = $dbh->prepare(
1217 FROM authorised_values
1219 AND authorised_value=?
1222 $stackstatus->execute( $authorised_valuecode, $data->{stack} );
1223 my ($lib) = $stackstatus->fetchrow;
1224 $data->{stack} = $lib;
1226 # Find the last 3 people who borrowed this item.
1227 my $sth2 = $dbh->prepare("SELECT * FROM issues,borrowers
1228 WHERE itemnumber = ?
1229 AND issues.borrowernumber = borrowers.borrowernumber
1230 AND returndate IS NOT NULL LIMIT 3");
1231 $sth2->execute($data->{'itemnumber'});
1233 while (my $data2 = $sth2->fetchrow_hashref()) {
1234 $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
1235 $data->{"card$ii"} = $data2->{'cardnumber'} if $data2->{'cardnumber'};
1236 $data->{"borrower$ii"} = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
1240 $results[$i] = $data;
1245 return( sort { $b->{'publisheddate'} cmp $a->{'publisheddate'} } @results );
1251 =head2 get_itemnumbers_of
1255 my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
1259 Given a list of biblionumbers, return the list of corresponding itemnumbers
1260 for each biblionumber.
1262 Return a reference on a hash where keys are biblionumbers and values are
1263 references on array of itemnumbers.
1267 sub get_itemnumbers_of {
1268 my @biblionumbers = @_;
1270 my $dbh = C4::Context->dbh;
1276 WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ')
1278 my $sth = $dbh->prepare($query);
1279 $sth->execute(@biblionumbers);
1283 while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
1284 push @{ $itemnumbers_of{$biblionumber} }, $itemnumber;
1287 return \%itemnumbers_of;
1290 =head2 GetItemnumberFromBarcode
1294 $result = GetItemnumberFromBarcode($barcode);
1300 sub GetItemnumberFromBarcode {
1302 my $dbh = C4::Context->dbh;
1305 $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
1306 $rq->execute($barcode);
1307 my ($result) = $rq->fetchrow;
1311 =head1 LIMITED USE FUNCTIONS
1313 The following functions, while part of the public API,
1314 are not exported. This is generally because they are
1315 meant to be used by only one script for a specific
1316 purpose, and should not be used in any other context
1317 without careful thought.
1325 my $item_marc = GetMarcItem($biblionumber, $itemnumber);
1329 Returns MARC::Record of the item passed in parameter.
1330 This function is meant for use only in C<cataloguing/additem.pl>,
1331 where it is needed to support that script's MARC-like
1337 my ( $biblionumber, $itemnumber ) = @_;
1339 # GetMarcItem has been revised so that it does the following:
1340 # 1. Gets the item information from the items table.
1341 # 2. Converts it to a MARC field for storage in the bib record.
1343 # The previous behavior was:
1344 # 1. Get the bib record.
1345 # 2. Return the MARC tag corresponding to the item record.
1347 # The difference is that one treats the items row as authoritative,
1348 # while the other treats the MARC representation as authoritative
1349 # under certain circumstances.
1351 my $itemrecord = GetItem($itemnumber);
1353 # Tack on 'items.' prefix to column names so that TransformKohaToMarc will work.
1354 # Also, don't emit a subfield if the underlying field is blank.
1355 my $mungeditem = { map { $itemrecord->{$_} ne '' ? ("items.$_" => $itemrecord->{$_}) : () } keys %{ $itemrecord } };
1356 my $itemmarc = TransformKohaToMarc($mungeditem);
1358 my $marc = GetMarcBiblio($biblionumber);
1359 my $frameworkcode=GetFrameworkCode($biblionumber);
1360 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
1361 foreach my $field ($marc->field($itemtag)){
1362 if ($field->subfield($itemsubfield)==$itemnumber){
1363 my $tmpmarc=MARC::Record->new();
1364 $tmpmarc->append_fields($field);
1365 _add_unlinked_marc_fields($itemmarc,$tmpmarc,$frameworkcode);
1372 =head1 PRIVATE FUNCTIONS AND VARIABLES
1374 The following functions are not meant to be called
1375 directly, but are documented in order to explain
1376 the inner workings of C<C4::Items>.
1380 =head2 %derived_columns
1382 This hash keeps track of item columns that
1383 are strictly derived from other columns in
1384 the item record and are not meant to be set
1387 Each key in the hash should be the name of a
1388 column (as named by TransformMarcToKoha). Each
1389 value should be hashref whose keys are the
1390 columns on which the derived column depends. The
1391 hashref should also contain a 'BUILDER' key
1392 that is a reference to a sub that calculates
1397 my %derived_columns = (
1398 'items.cn_sort' => {
1399 'itemcallnumber' => 1,
1400 'items.cn_source' => 1,
1401 'BUILDER' => \&_calc_items_cn_sort,
1405 =head2 _set_derived_columns_for_add
1409 _set_derived_column_for_add($item);
1413 Given an item hash representing a new item to be added,
1414 calculate any derived columns. Currently the only
1415 such column is C<items.cn_sort>.
1419 sub _set_derived_columns_for_add {
1422 foreach my $column (keys %derived_columns) {
1423 my $builder = $derived_columns{$column}->{'BUILDER'};
1424 my $source_values = {};
1425 foreach my $source_column (keys %{ $derived_columns{$column} }) {
1426 next if $source_column eq 'BUILDER';
1427 $source_values->{$source_column} = $item->{$source_column};
1429 $builder->($item, $source_values);
1433 =head2 _set_derived_columns_for_mod
1437 _set_derived_column_for_mod($item);
1441 Given an item hash representing a new item to be modified.
1442 calculate any derived columns. Currently the only
1443 such column is C<items.cn_sort>.
1445 This routine differs from C<_set_derived_columns_for_add>
1446 in that it needs to handle partial item records. In other
1447 words, the caller of C<ModItem> may have supplied only one
1448 or two columns to be changed, so this function needs to
1449 determine whether any of the columns to be changed affect
1450 any of the derived columns. Also, if a derived column
1451 depends on more than one column, but the caller is not
1452 changing all of then, this routine retrieves the unchanged
1453 values from the database in order to ensure a correct
1458 sub _set_derived_columns_for_mod {
1461 foreach my $column (keys %derived_columns) {
1462 my $builder = $derived_columns{$column}->{'BUILDER'};
1463 my $source_values = {};
1464 my %missing_sources = ();
1465 my $must_recalc = 0;
1466 foreach my $source_column (keys %{ $derived_columns{$column} }) {
1467 next if $source_column eq 'BUILDER';
1468 if (exists $item->{$source_column}) {
1470 $source_values->{$source_column} = $item->{$source_column};
1472 $missing_sources{$source_column} = 1;
1476 foreach my $source_column (keys %missing_sources) {
1477 $source_values->{$source_column} = _get_single_item_column($source_column, $item->{'itemnumber'});
1479 $builder->($item, $source_values);
1484 =head2 _do_column_fixes_for_mod
1488 _do_column_fixes_for_mod($item);
1492 Given an item hashref containing one or more
1493 columns to modify, fix up certain values.
1494 Specifically, set to 0 any passed value
1495 of C<notforloan>, C<damaged>, C<itemlost>, or
1496 C<wthdrawn> that is either undefined or
1497 contains the empty string.
1501 sub _do_column_fixes_for_mod {
1504 if (exists $item->{'notforloan'} and
1505 (not defined $item->{'notforloan'} or $item->{'notforloan'} eq '')) {
1506 $item->{'notforloan'} = 0;
1508 if (exists $item->{'damaged'} and
1509 (not defined $item->{'damaged'} or $item->{'damaged'} eq '')) {
1510 $item->{'damaged'} = 0;
1512 if (exists $item->{'itemlost'} and
1513 (not defined $item->{'itemlost'} or $item->{'itemlost'} eq '')) {
1514 $item->{'itemlost'} = 0;
1516 if (exists $item->{'wthdrawn'} and
1517 (not defined $item->{'wthdrawn'} or $item->{'wthdrawn'} eq '')) {
1518 $item->{'wthdrawn'} = 0;
1522 =head2 _get_single_item_column
1526 _get_single_item_column($column, $itemnumber);
1530 Retrieves the value of a single column from an C<items>
1531 row specified by C<$itemnumber>.
1535 sub _get_single_item_column {
1537 my $itemnumber = shift;
1539 my $dbh = C4::Context->dbh;
1540 my $sth = $dbh->prepare("SELECT $column FROM items WHERE itemnumber = ?");
1541 $sth->execute($itemnumber);
1542 my ($value) = $sth->fetchrow();
1546 =head2 _calc_items_cn_sort
1550 _calc_items_cn_sort($item, $source_values);
1554 Helper routine to calculate C<items.cn_sort>.
1558 sub _calc_items_cn_sort {
1560 my $source_values = shift;
1562 $item->{'items.cn_sort'} = GetClassSort($source_values->{'items.cn_source'}, $source_values->{'itemcallnumber'}, "");
1565 =head2 _set_defaults_for_add
1569 _set_defaults_for_add($item_hash);
1573 Given an item hash representing an item to be added, set
1574 correct default values for columns whose default value
1575 is not handled by the DBMS. This includes the following
1582 C<items.dateaccessioned>
1604 sub _set_defaults_for_add {
1607 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1608 if (!(exists $item->{'dateaccessioned'}) ||
1609 ($item->{'dateaccessioned'} eq '')) {
1610 # FIXME add check for invalid date
1611 my $today = C4::Dates->new();
1612 $item->{'dateaccessioned'} = $today->output("iso"); #TODO: check time issues
1615 # various item status fields cannot be null
1616 $item->{'notforloan'} = 0 unless exists $item->{'notforloan'} and defined $item->{'notforloan'} and $item->{'notforloan'} ne '';
1617 $item->{'damaged'} = 0 unless exists $item->{'damaged'} and defined $item->{'damaged'} and $item->{'damaged'} ne '';
1618 $item->{'itemlost'} = 0 unless exists $item->{'itemlost'} and defined $item->{'itemlost'} and $item->{'itemlost'} ne '';
1619 $item->{'wthdrawn'} = 0 unless exists $item->{'wthdrawn'} and defined $item->{'wthdrawn'} and $item->{'wthdrawn'} ne '';
1622 =head2 _koha_new_item
1626 my ($itemnumber,$error) = _koha_new_item( $item, $barcode );
1630 Perform the actual insert into the C<items> table.
1634 sub _koha_new_item {
1635 my ( $item, $barcode ) = @_;
1636 my $dbh=C4::Context->dbh;
1639 "INSERT INTO items SET
1641 biblioitemnumber = ?,
1643 dateaccessioned = ?,
1647 replacementprice = ?,
1648 replacementpricedate = NOW(),
1649 datelastborrowed = ?,
1650 datelastseen = NOW(),
1673 my $sth = $dbh->prepare($query);
1675 $item->{'biblionumber'},
1676 $item->{'biblioitemnumber'},
1678 $item->{'dateaccessioned'},
1679 $item->{'booksellerid'},
1680 $item->{'homebranch'},
1682 $item->{'replacementprice'},
1683 $item->{datelastborrowed},
1685 $item->{'notforloan'},
1687 $item->{'itemlost'},
1688 $item->{'wthdrawn'},
1689 $item->{'itemcallnumber'},
1690 $item->{'restricted'},
1691 $item->{'itemnotes'},
1692 $item->{'holdingbranch'},
1694 $item->{'location'},
1697 $item->{'renewals'},
1698 $item->{'reserves'},
1699 $item->{'items.cn_source'},
1700 $item->{'items.cn_sort'},
1703 $item->{'materials'},
1706 my $itemnumber = $dbh->{'mysql_insertid'};
1707 if ( defined $sth->errstr ) {
1708 $error.="ERROR in _koha_new_item $query".$sth->errstr;
1711 return ( $itemnumber, $error );
1714 =head2 _koha_modify_item
1718 my ($itemnumber,$error) =_koha_modify_item( $item );
1722 Perform the actual update of the C<items> row. Note that this
1723 routine accepts a hashref specifying the columns to update.
1727 sub _koha_modify_item {
1729 my $dbh=C4::Context->dbh;
1732 my $query = "UPDATE items SET ";
1734 for my $key ( keys %$item ) {
1736 push @bind, $item->{$key};
1739 $query .= " WHERE itemnumber=?";
1740 push @bind, $item->{'itemnumber'};
1741 my $sth = C4::Context->dbh->prepare($query);
1742 $sth->execute(@bind);
1743 if ( C4::Context->dbh->errstr ) {
1744 $error.="ERROR in _koha_modify_item $query".$dbh->errstr;
1748 return ($item->{'itemnumber'},$error);
1751 =head2 _koha_delete_item
1755 _koha_delete_item( $dbh, $itemnum );
1759 Internal function to delete an item record from the koha tables
1763 sub _koha_delete_item {
1764 my ( $dbh, $itemnum ) = @_;
1766 # save the deleted item to deleteditems table
1767 my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
1768 $sth->execute($itemnum);
1769 my $data = $sth->fetchrow_hashref();
1771 my $query = "INSERT INTO deleteditems SET ";
1773 foreach my $key ( keys %$data ) {
1774 $query .= "$key = ?,";
1775 push( @bind, $data->{$key} );
1778 $sth = $dbh->prepare($query);
1779 $sth->execute(@bind);
1782 # delete from items table
1783 $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
1784 $sth->execute($itemnum);
1789 =head2 _marc_from_item_hash
1793 my $item_marc = _marc_from_item_hash($item, $frameworkcode);
1797 Given an item hash representing a complete item record,
1798 create a C<MARC::Record> object containing an embedded
1799 tag representing that item.
1803 sub _marc_from_item_hash {
1805 my $frameworkcode = shift;
1807 # Tack on 'items.' prefix to column names so lookup from MARC frameworks will work
1808 # Also, don't emit a subfield if the underlying field is blank.
1809 my $mungeditem = { map { (defined($item->{$_}) and $item->{$_} ne '') ?
1810 (/^items\./ ? ($_ => $item->{$_}) : ("items.$_" => $item->{$_}))
1811 : () } keys %{ $item } };
1813 my $item_marc = MARC::Record->new();
1814 foreach my $item_field (keys %{ $mungeditem }) {
1815 my ($tag, $subfield) = GetMarcFromKohaField($item_field, $frameworkcode);
1816 next unless defined $tag and defined $subfield; # skip if not mapped to MARC field
1817 if (my $field = $item_marc->field($tag)) {
1818 $field->add_subfields($subfield => $mungeditem->{$item_field});
1820 $item_marc->add_fields( $tag, " ", " ", $subfield => $mungeditem->{$item_field});
1827 =head2 _add_item_field_to_biblio
1831 _add_item_field_to_biblio($item_marc, $biblionumber, $frameworkcode);
1835 Adds the fields from a MARC record containing the
1836 representation of a Koha item record to the MARC
1837 biblio record. The input C<$item_marc> record
1838 is expect to contain just one field, the embedded
1839 item information field.
1843 sub _add_item_field_to_biblio {
1844 my ($item_marc, $biblionumber, $frameworkcode) = @_;
1846 my $biblio_marc = GetMarcBiblio($biblionumber);
1848 foreach my $field ($item_marc->fields()) {
1849 $biblio_marc->append_fields($field);
1852 ModBiblioMarc($biblio_marc, $biblionumber, $frameworkcode);
1855 =head2 _replace_item_field_in_biblio
1859 &_replace_item_field_in_biblio($item_marc, $biblionumber, $itemnumber, $frameworkcode)
1863 Given a MARC::Record C<$item_marc> containing one tag with the MARC
1864 representation of the item, examine the biblio MARC
1865 for the corresponding tag for that item and
1866 replace it with the tag from C<$item_marc>.
1870 sub _replace_item_field_in_biblio {
1871 my ($ItemRecord, $biblionumber, $itemnumber, $frameworkcode) = @_;
1872 my $dbh = C4::Context->dbh;
1874 # get complete MARC record & replace the item field by the new one
1875 my $completeRecord = GetMarcBiblio($biblionumber);
1876 my ($itemtag,$itemsubfield) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
1877 my $itemField = $ItemRecord->field($itemtag);
1878 my @items = $completeRecord->field($itemtag);
1881 if ($_->subfield($itemsubfield) eq $itemnumber) {
1882 $_->replace_with($itemField);
1888 # If we haven't found the matching field,
1889 # just add it. However, this means that
1890 # there is likely a bug.
1891 $completeRecord->append_fields($itemField);
1895 ModBiblioMarc($completeRecord, $biblionumber, $frameworkcode);
1898 =head2 _repack_item_errors
1900 Add an error message hash generated by C<CheckItemPreSave>
1901 to a list of errors.
1905 sub _repack_item_errors {
1906 my $item_sequence_num = shift;
1907 my $item_ref = shift;
1908 my $error_ref = shift;
1910 my @repacked_errors = ();
1912 foreach my $error_code (sort keys %{ $error_ref }) {
1913 my $repacked_error = {};
1914 $repacked_error->{'item_sequence'} = $item_sequence_num;
1915 $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ? $item_ref->{'barcode'} : '';
1916 $repacked_error->{'error_code'} = $error_code;
1917 $repacked_error->{'error_information'} = $error_ref->{$error_code};
1918 push @repacked_errors, $repacked_error;
1921 return @repacked_errors;
1924 =head2 _add_unlinked_marc_fields
1926 Adds marc fields to new_item_marc
1927 from $original_item_marc fields which
1928 1) are not linked to items table
1929 2) but still are used in framework
1930 3) and are provided a value
1933 sub _add_unlinked_marc_fields{
1934 my $new_item_marc = shift;
1935 my $original_item_marc = shift;
1936 my $frameworkcode = shift;
1937 my $tmp_item_marc =$new_item_marc->clone;
1938 my $marcstructure = GetMarcStructure(1,$frameworkcode);
1939 foreach my $field ($original_item_marc->fields()){
1940 my $tag = $field->tag();
1941 if ($new_item_marc->fields($tag)){
1942 # It is assumed that item marc records only have ***one*** tag and that this tag is mandatory.
1943 # So new_item_marc MUST have $field->tag
1944 foreach my $subfield ($field->subfields()) {
1945 if (!$marcstructure->{$tag}->{$subfield->[0]}->{'kohafield'} && !$tmp_item_marc->subfield($tag,$subfield->[0])) {
1946 #$new_item_marc->field($tag)->add_subfields($subfield->[0]=>$subfield->[1]);
1948 my $ret= eval {$new_item_marc->field($tag)->add_subfields($subfield->[0] => $subfield->[1])};
1950 warn $subfield->[0]."=>".$subfield->[1]."\n".$new_item_marc->as_formatted."\n".$original_item_marc;
1956 return $new_item_marc;