Merge branch 'new/bug_5277' into kcmaster
[koha.git] / C4 / Items.pm
1 package C4::Items;
2
3 # Copyright 2007 LibLime, Inc.
4 # Parts Copyright Biblibre 2010
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23
24 use Carp;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Biblio;
28 use C4::Dates qw/format_date format_date_in_iso/;
29 use MARC::Record;
30 use C4::ClassSource;
31 use C4::Log;
32 use C4::Branch;
33 require C4::Reserves;
34 use C4::Charset;
35 use C4::Acquisition;
36
37 use vars qw($VERSION @ISA @EXPORT);
38
39 BEGIN {
40     $VERSION = 3.01;
41
42         require Exporter;
43     @ISA = qw( Exporter );
44
45     # function exports
46     @EXPORT = qw(
47         GetItem
48         AddItemFromMarc
49         AddItem
50         AddItemBatchFromMarc
51         ModItemFromMarc
52                 Item2Marc
53         ModItem
54         ModDateLastSeen
55         ModItemTransfer
56         DelItem
57     
58         CheckItemPreSave
59     
60         GetItemStatus
61         GetItemLocation
62         GetLostItems
63         GetItemsForInventory
64         GetItemsCount
65         GetItemInfosOf
66         GetItemsByBiblioitemnumber
67         GetItemsInfo
68         get_itemnumbers_of
69         GetItemnumberFromBarcode
70         GetBarcodeFromItemnumber
71
72                 DelItemCheck
73                 MoveItemFromBiblio 
74                 GetLatestAcquisitions
75         CartToShelf
76     );
77 }
78
79 =head1 NAME
80
81 C4::Items - item management functions
82
83 =head1 DESCRIPTION
84
85 This module contains an API for manipulating item 
86 records in Koha, and is used by cataloguing, circulation,
87 acquisitions, and serials management.
88
89 A Koha item record is stored in two places: the
90 items table and embedded in a MARC tag in the XML
91 version of the associated bib record in C<biblioitems.marcxml>.
92 This is done to allow the item information to be readily
93 indexed (e.g., by Zebra), but means that each item
94 modification transaction must keep the items table
95 and the MARC XML in sync at all times.
96
97 Consequently, all code that creates, modifies, or deletes
98 item records B<must> use an appropriate function from 
99 C<C4::Items>.  If no existing function is suitable, it is
100 better to add one to C<C4::Items> than to use add
101 one-off SQL statements to add or modify items.
102
103 The items table will be considered authoritative.  In other
104 words, if there is ever a discrepancy between the items
105 table and the MARC XML, the items table should be considered
106 accurate.
107
108 =head1 HISTORICAL NOTE
109
110 Most of the functions in C<C4::Items> were originally in
111 the C<C4::Biblio> module.
112
113 =head1 CORE EXPORTED FUNCTIONS
114
115 The following functions are meant for use by users
116 of C<C4::Items>
117
118 =cut
119
120 =head2 GetItem
121
122   $item = GetItem($itemnumber,$barcode,$serial);
123
124 Return item information, for a given itemnumber or barcode.
125 The return value is a hashref mapping item column
126 names to values.  If C<$serial> is true, include serial publication data.
127
128 =cut
129
130 sub GetItem {
131     my ($itemnumber,$barcode, $serial) = @_;
132     my $dbh = C4::Context->dbh;
133         my $data;
134     if ($itemnumber) {
135         my $sth = $dbh->prepare("
136             SELECT * FROM items 
137             WHERE itemnumber = ?");
138         $sth->execute($itemnumber);
139         $data = $sth->fetchrow_hashref;
140     } else {
141         my $sth = $dbh->prepare("
142             SELECT * FROM items 
143             WHERE barcode = ?"
144             );
145         $sth->execute($barcode);                
146         $data = $sth->fetchrow_hashref;
147     }
148     if ( $serial) {      
149     my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=?");
150         $ssth->execute($data->{'itemnumber'}) ;
151         ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
152     }
153         #if we don't have an items.itype, use biblioitems.itemtype.
154         if( ! $data->{'itype'} ) {
155                 my $sth = $dbh->prepare("SELECT itemtype FROM biblioitems  WHERE biblionumber = ?");
156                 $sth->execute($data->{'biblionumber'});
157                 ($data->{'itype'}) = $sth->fetchrow_array;
158         }
159     return $data;
160 }    # sub GetItem
161
162 =head2 CartToShelf
163
164   CartToShelf($itemnumber);
165
166 Set the current shelving location of the item record
167 to its stored permanent shelving location.  This is
168 primarily used to indicate when an item whose current
169 location is a special processing ('PROC') or shelving cart
170 ('CART') location is back in the stacks.
171
172 =cut
173
174 sub CartToShelf {
175     my ( $itemnumber ) = @_;
176
177     unless ( $itemnumber ) {
178         croak "FAILED CartToShelf() - no itemnumber supplied";
179     }
180
181     my $item = GetItem($itemnumber);
182     $item->{location} = $item->{permanent_location};
183     ModItem($item, undef, $itemnumber);
184 }
185
186 =head2 AddItemFromMarc
187
188   my ($biblionumber, $biblioitemnumber, $itemnumber) 
189       = AddItemFromMarc($source_item_marc, $biblionumber);
190
191 Given a MARC::Record object containing an embedded item
192 record and a biblionumber, create a new item record.
193
194 =cut
195
196 sub AddItemFromMarc {
197     my ( $source_item_marc, $biblionumber ) = @_;
198     my $dbh = C4::Context->dbh;
199
200     # parse item hash from MARC
201     my $frameworkcode = GetFrameworkCode( $biblionumber );
202         my ($itemtag,$itemsubfield)=GetMarcFromKohaField("items.itemnumber",$frameworkcode);
203         
204         my $localitemmarc=MARC::Record->new;
205         $localitemmarc->append_fields($source_item_marc->field($itemtag));
206     my $item = &TransformMarcToKoha( $dbh, $localitemmarc, $frameworkcode ,'items');
207     my $unlinked_item_subfields = _get_unlinked_item_subfields($localitemmarc, $frameworkcode);
208     return AddItem($item, $biblionumber, $dbh, $frameworkcode, $unlinked_item_subfields);
209 }
210
211 =head2 AddItem
212
213   my ($biblionumber, $biblioitemnumber, $itemnumber) 
214       = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $unlinked_item_subfields]);
215
216 Given a hash containing item column names as keys,
217 create a new Koha item record.
218
219 The first two optional parameters (C<$dbh> and C<$frameworkcode>)
220 do not need to be supplied for general use; they exist
221 simply to allow them to be picked up from AddItemFromMarc.
222
223 The final optional parameter, C<$unlinked_item_subfields>, contains
224 an arrayref containing subfields present in the original MARC
225 representation of the item (e.g., from the item editor) that are
226 not mapped to C<items> columns directly but should instead
227 be stored in C<items.more_subfields_xml> and included in 
228 the biblio items tag for display and indexing.
229
230 =cut
231
232 sub AddItem {
233     my $item = shift;
234     my $biblionumber = shift;
235
236     my $dbh           = @_ ? shift : C4::Context->dbh;
237     my $frameworkcode = @_ ? shift : GetFrameworkCode( $biblionumber );
238     my $unlinked_item_subfields;  
239     if (@_) {
240         $unlinked_item_subfields = shift
241     };
242
243     # needs old biblionumber and biblioitemnumber
244     $item->{'biblionumber'} = $biblionumber;
245     my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
246     $sth->execute( $item->{'biblionumber'} );
247     ($item->{'biblioitemnumber'}) = $sth->fetchrow;
248
249     _set_defaults_for_add($item);
250     _set_derived_columns_for_add($item);
251     $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
252     # FIXME - checks here
253     unless ( $item->{itype} ) {  # default to biblioitem.itemtype if no itype
254         my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
255         $itype_sth->execute( $item->{'biblionumber'} );
256         ( $item->{'itype'} ) = $itype_sth->fetchrow_array;
257     }
258
259         my ( $itemnumber, $error ) = _koha_new_item( $item, $item->{barcode} );
260     $item->{'itemnumber'} = $itemnumber;
261
262     # create MARC tag representing item and add to bib
263     my $new_item_marc = _marc_from_item_hash($item, $frameworkcode, $unlinked_item_subfields);
264     _add_item_field_to_biblio($new_item_marc, $item->{'biblionumber'}, $frameworkcode );
265    
266     logaction("CATALOGUING", "ADD", $itemnumber, "item") if C4::Context->preference("CataloguingLog");
267     
268     return ($item->{biblionumber}, $item->{biblioitemnumber}, $itemnumber);
269 }
270
271 =head2 AddItemBatchFromMarc
272
273   ($itemnumber_ref, $error_ref) = AddItemBatchFromMarc($record, 
274              $biblionumber, $biblioitemnumber, $frameworkcode);
275
276 Efficiently create item records from a MARC biblio record with
277 embedded item fields.  This routine is suitable for batch jobs.
278
279 This API assumes that the bib record has already been
280 saved to the C<biblio> and C<biblioitems> tables.  It does
281 not expect that C<biblioitems.marc> and C<biblioitems.marcxml>
282 are populated, but it will do so via a call to ModBibiloMarc.
283
284 The goal of this API is to have a similar effect to using AddBiblio
285 and AddItems in succession, but without inefficient repeated
286 parsing of the MARC XML bib record.
287
288 This function returns an arrayref of new itemsnumbers and an arrayref of item
289 errors encountered during the processing.  Each entry in the errors
290 list is a hashref containing the following keys:
291
292 =over
293
294 =item item_sequence
295
296 Sequence number of original item tag in the MARC record.
297
298 =item item_barcode
299
300 Item barcode, provide to assist in the construction of
301 useful error messages.
302
303 =item error_condition
304
305 Code representing the error condition.  Can be 'duplicate_barcode',
306 'invalid_homebranch', or 'invalid_holdingbranch'.
307
308 =item error_information
309
310 Additional information appropriate to the error condition.
311
312 =back
313
314 =cut
315
316 sub AddItemBatchFromMarc {
317     my ($record, $biblionumber, $biblioitemnumber, $frameworkcode) = @_;
318     my $error;
319     my @itemnumbers = ();
320     my @errors = ();
321     my $dbh = C4::Context->dbh;
322
323     # loop through the item tags and start creating items
324     my @bad_item_fields = ();
325     my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
326     my $item_sequence_num = 0;
327     ITEMFIELD: foreach my $item_field ($record->field($itemtag)) {
328         $item_sequence_num++;
329         # we take the item field and stick it into a new
330         # MARC record -- this is required so far because (FIXME)
331         # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
332         # and there is no TransformMarcFieldToKoha
333         my $temp_item_marc = MARC::Record->new();
334         $temp_item_marc->append_fields($item_field);
335     
336         # add biblionumber and biblioitemnumber
337         my $item = TransformMarcToKoha( $dbh, $temp_item_marc, $frameworkcode, 'items' );
338         my $unlinked_item_subfields = _get_unlinked_item_subfields($temp_item_marc, $frameworkcode);
339         $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
340         $item->{'biblionumber'} = $biblionumber;
341         $item->{'biblioitemnumber'} = $biblioitemnumber;
342
343         # check for duplicate barcode
344         my %item_errors = CheckItemPreSave($item);
345         if (%item_errors) {
346             push @errors, _repack_item_errors($item_sequence_num, $item, \%item_errors);
347             push @bad_item_fields, $item_field;
348             next ITEMFIELD;
349         }
350
351         _set_defaults_for_add($item);
352         _set_derived_columns_for_add($item);
353         my ( $itemnumber, $error ) = _koha_new_item( $item, $item->{barcode} );
354         warn $error if $error;
355         push @itemnumbers, $itemnumber; # FIXME not checking error
356         $item->{'itemnumber'} = $itemnumber;
357
358         logaction("CATALOGUING", "ADD", $itemnumber, "item") if C4::Context->preference("CataloguingLog"); 
359
360         my $new_item_marc = _marc_from_item_hash($item, $frameworkcode, $unlinked_item_subfields);
361         $item_field->replace_with($new_item_marc->field($itemtag));
362     }
363
364     # remove any MARC item fields for rejected items
365     foreach my $item_field (@bad_item_fields) {
366         $record->delete_field($item_field);
367     }
368
369     # update the MARC biblio
370     $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
371
372     return (\@itemnumbers, \@errors);
373 }
374
375 =head2 ModItemFromMarc
376
377   ModItemFromMarc($item_marc, $biblionumber, $itemnumber);
378
379 This function updates an item record based on a supplied
380 C<MARC::Record> object containing an embedded item field.
381 This API is meant for the use of C<additem.pl>; for 
382 other purposes, C<ModItem> should be used.
383
384 This function uses the hash %default_values_for_mod_from_marc,
385 which contains default values for item fields to
386 apply when modifying an item.  This is needed beccause
387 if an item field's value is cleared, TransformMarcToKoha
388 does not include the column in the
389 hash that's passed to ModItem, which without
390 use of this hash makes it impossible to clear
391 an item field's value.  See bug 2466.
392
393 Note that only columns that can be directly
394 changed from the cataloging and serials
395 item editors are included in this hash.
396
397 =cut
398
399 my %default_values_for_mod_from_marc = (
400     barcode              => undef, 
401     booksellerid         => undef, 
402     ccode                => undef, 
403     'items.cn_source'    => undef, 
404     copynumber           => undef, 
405     damaged              => 0,
406 #    dateaccessioned      => undef,
407     enumchron            => undef, 
408     holdingbranch        => undef, 
409     homebranch           => undef, 
410     itemcallnumber       => undef, 
411     itemlost             => 0,
412     itemnotes            => undef, 
413     itype                => undef, 
414     location             => undef, 
415     materials            => undef, 
416     notforloan           => 0,
417     paidfor              => undef, 
418     price                => undef, 
419     replacementprice     => undef, 
420     replacementpricedate => undef, 
421     restricted           => undef, 
422     stack                => undef, 
423     stocknumber          => undef, 
424     uri                  => undef, 
425     wthdrawn             => 0,
426 );
427
428 sub ModItemFromMarc {
429     my $item_marc = shift;
430     my $biblionumber = shift;
431     my $itemnumber = shift;
432
433     my $dbh           = C4::Context->dbh;
434     my $frameworkcode = GetFrameworkCode($biblionumber);
435     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
436
437     my $localitemmarc = MARC::Record->new;
438     $localitemmarc->append_fields( $item_marc->field($itemtag) );
439     my $item = &TransformMarcToKoha( $dbh, $localitemmarc, $frameworkcode, 'items' );
440     foreach my $item_field ( keys %default_values_for_mod_from_marc ) {
441         $item->{$item_field} = $default_values_for_mod_from_marc{$item_field} unless (exists $item->{$item_field});
442     }
443     my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode );
444
445     my $dbh = C4::Context->dbh;
446     my $frameworkcode = GetFrameworkCode( $biblionumber );
447         my ($itemtag,$itemsubfield)=GetMarcFromKohaField("items.itemnumber",$frameworkcode);
448         
449         my $localitemmarc=MARC::Record->new;
450         $localitemmarc->append_fields($item_marc->field($itemtag));
451     my $item = &TransformMarcToKoha( $dbh, $localitemmarc, $frameworkcode, 'items');
452     foreach my $item_field (keys %default_values_for_mod_from_marc) {
453         $item->{$item_field} = $default_values_for_mod_from_marc{$item_field} unless exists $item->{$item_field};
454     }
455     my $unlinked_item_subfields = _get_unlinked_item_subfields($localitemmarc, $frameworkcode);
456    
457     return ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields); 
458 }
459
460 =head2 ModItem
461
462   ModItem({ column => $newvalue }, $biblionumber, 
463                   $itemnumber[, $original_item_marc]);
464
465 Change one or more columns in an item record and update
466 the MARC representation of the item.
467
468 The first argument is a hashref mapping from item column
469 names to the new values.  The second and third arguments
470 are the biblionumber and itemnumber, respectively.
471
472 The fourth, optional parameter, C<$unlinked_item_subfields>, contains
473 an arrayref containing subfields present in the original MARC
474 representation of the item (e.g., from the item editor) that are
475 not mapped to C<items> columns directly but should instead
476 be stored in C<items.more_subfields_xml> and included in 
477 the biblio items tag for display and indexing.
478
479 If one of the changed columns is used to calculate
480 the derived value of a column such as C<items.cn_sort>, 
481 this routine will perform the necessary calculation
482 and set the value.
483
484 =cut
485
486 sub ModItem {
487     my $item = shift;
488     my $biblionumber = shift;
489     my $itemnumber = shift;
490
491     # if $biblionumber is undefined, get it from the current item
492     unless (defined $biblionumber) {
493         $biblionumber = _get_single_item_column('biblionumber', $itemnumber);
494     }
495
496     my $dbh           = @_ ? shift : C4::Context->dbh;
497     my $frameworkcode = @_ ? shift : GetFrameworkCode( $biblionumber );
498     
499     my $unlinked_item_subfields;  
500     if (@_) {
501         $unlinked_item_subfields = shift;
502         $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
503     };
504
505     $item->{'itemnumber'} = $itemnumber or return undef;
506     _set_derived_columns_for_mod($item);
507     _do_column_fixes_for_mod($item);
508     # FIXME add checks
509     # duplicate barcode
510     # attempt to change itemnumber
511     # attempt to change biblionumber (if we want
512     # an API to relink an item to a different bib,
513     # it should be a separate function)
514
515     # update items table
516     _koha_modify_item($item);
517
518     # update biblio MARC XML
519     my $whole_item = GetItem($itemnumber) or die "FAILED GetItem($itemnumber)";
520
521     unless (defined $unlinked_item_subfields) {
522         $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'});
523     }
524     my $new_item_marc = _marc_from_item_hash($whole_item, $frameworkcode, $unlinked_item_subfields) 
525         or die "FAILED _marc_from_item_hash($whole_item, $frameworkcode)";
526     
527     _replace_item_field_in_biblio($new_item_marc, $biblionumber, $itemnumber, $frameworkcode);
528         ($new_item_marc       eq '0') and die "$new_item_marc is '0', not hashref";  # logaction line would crash anyway
529     logaction("CATALOGUING", "MODIFY", $itemnumber, $new_item_marc->as_formatted) if C4::Context->preference("CataloguingLog");
530 }
531
532 =head2 ModItemTransfer
533
534   ModItemTransfer($itenumber, $frombranch, $tobranch);
535
536 Marks an item as being transferred from one branch
537 to another.
538
539 =cut
540
541 sub ModItemTransfer {
542     my ( $itemnumber, $frombranch, $tobranch ) = @_;
543
544     my $dbh = C4::Context->dbh;
545
546     #new entry in branchtransfers....
547     my $sth = $dbh->prepare(
548         "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
549         VALUES (?, ?, NOW(), ?)");
550     $sth->execute($itemnumber, $frombranch, $tobranch);
551
552     ModItem({ holdingbranch => $tobranch }, undef, $itemnumber);
553     ModDateLastSeen($itemnumber);
554     return;
555 }
556
557 =head2 ModDateLastSeen
558
559   ModDateLastSeen($itemnum);
560
561 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking.
562 C<$itemnum> is the item number
563
564 =cut
565
566 sub ModDateLastSeen {
567     my ($itemnumber) = @_;
568     
569     my $today = C4::Dates->new();    
570     ModItem({ itemlost => 0, datelastseen => $today->output("iso") }, undef, $itemnumber);
571 }
572
573 =head2 DelItem
574
575   DelItem($dbh, $biblionumber, $itemnumber);
576
577 Exported function (core API) for deleting an item record in Koha.
578
579 =cut
580
581 sub DelItem {
582     my ( $dbh, $biblionumber, $itemnumber ) = @_;
583     
584     # FIXME check the item has no current issues
585     
586     _koha_delete_item( $dbh, $itemnumber );
587
588     # get the MARC record
589     my $record = GetMarcBiblio($biblionumber);
590     my $frameworkcode = GetFrameworkCode($biblionumber);
591
592     # backup the record
593     my $copy2deleted = $dbh->prepare("UPDATE deleteditems SET marc=? WHERE itemnumber=?");
594     $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
595
596     #search item field code
597     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
598     my @fields = $record->field($itemtag);
599
600     # delete the item specified
601     foreach my $field (@fields) {
602         if ( $field->subfield($itemsubfield) eq $itemnumber ) {
603             $record->delete_field($field);
604         }
605     }
606     &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
607     logaction("CATALOGUING", "DELETE", $itemnumber, "item") if C4::Context->preference("CataloguingLog");
608 }
609
610 =head2 CheckItemPreSave
611
612     my $item_ref = TransformMarcToKoha($marc, 'items');
613     # do stuff
614     my %errors = CheckItemPreSave($item_ref);
615     if (exists $errors{'duplicate_barcode'}) {
616         print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
617     } elsif (exists $errors{'invalid_homebranch'}) {
618         print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
619     } elsif (exists $errors{'invalid_holdingbranch'}) {
620         print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
621     } else {
622         print "item is OK";
623     }
624
625 Given a hashref containing item fields, determine if it can be
626 inserted or updated in the database.  Specifically, checks for
627 database integrity issues, and returns a hash containing any
628 of the following keys, if applicable.
629
630 =over 2
631
632 =item duplicate_barcode
633
634 Barcode, if it duplicates one already found in the database.
635
636 =item invalid_homebranch
637
638 Home branch, if not defined in branches table.
639
640 =item invalid_holdingbranch
641
642 Holding branch, if not defined in branches table.
643
644 =back
645
646 This function does NOT implement any policy-related checks,
647 e.g., whether current operator is allowed to save an
648 item that has a given branch code.
649
650 =cut
651
652 sub CheckItemPreSave {
653     my $item_ref = shift;
654
655     my %errors = ();
656
657     # check for duplicate barcode
658     if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
659         my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'});
660         if ($existing_itemnumber) {
661             if (!exists $item_ref->{'itemnumber'}                       # new item
662                 or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
663                 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
664             }
665         }
666     }
667
668     # check for valid home branch
669     if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
670         my $branch_name = GetBranchName($item_ref->{'homebranch'});
671         unless (defined $branch_name) {
672             # relies on fact that branches.branchname is a non-NULL column,
673             # so GetBranchName returns undef only if branch does not exist
674             $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
675         }
676     }
677
678     # check for valid holding branch
679     if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
680         my $branch_name = GetBranchName($item_ref->{'holdingbranch'});
681         unless (defined $branch_name) {
682             # relies on fact that branches.branchname is a non-NULL column,
683             # so GetBranchName returns undef only if branch does not exist
684             $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
685         }
686     }
687
688     return %errors;
689
690 }
691
692 =head1 EXPORTED SPECIAL ACCESSOR FUNCTIONS
693
694 The following functions provide various ways of 
695 getting an item record, a set of item records, or
696 lists of authorized values for certain item fields.
697
698 Some of the functions in this group are candidates
699 for refactoring -- for example, some of the code
700 in C<GetItemsByBiblioitemnumber> and C<GetItemsInfo>
701 has copy-and-paste work.
702
703 =cut
704
705 =head2 GetItemStatus
706
707   $itemstatushash = GetItemStatus($fwkcode);
708
709 Returns a list of valid values for the
710 C<items.notforloan> field.
711
712 NOTE: does B<not> return an individual item's
713 status.
714
715 Can be MARC dependant.
716 fwkcode is optional.
717 But basically could be can be loan or not
718 Create a status selector with the following code
719
720 =head3 in PERL SCRIPT
721
722  my $itemstatushash = getitemstatus;
723  my @itemstatusloop;
724  foreach my $thisstatus (keys %$itemstatushash) {
725      my %row =(value => $thisstatus,
726                  statusname => $itemstatushash->{$thisstatus}->{'statusname'},
727              );
728      push @itemstatusloop, \%row;
729  }
730  $template->param(statusloop=>\@itemstatusloop);
731
732 =head3 in TEMPLATE
733
734  <select name="statusloop">
735      <option value="">Default</option>
736  <!-- TMPL_LOOP name="statusloop" -->
737      <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option>
738  <!-- /TMPL_LOOP -->
739  </select>
740
741 =cut
742
743 sub GetItemStatus {
744
745     # returns a reference to a hash of references to status...
746     my ($fwk) = @_;
747     my %itemstatus;
748     my $dbh = C4::Context->dbh;
749     my $sth;
750     $fwk = '' unless ($fwk);
751     my ( $tag, $subfield ) =
752       GetMarcFromKohaField( "items.notforloan", $fwk );
753     if ( $tag and $subfield ) {
754         my $sth =
755           $dbh->prepare(
756             "SELECT authorised_value
757             FROM marc_subfield_structure
758             WHERE tagfield=?
759                 AND tagsubfield=?
760                 AND frameworkcode=?
761             "
762           );
763         $sth->execute( $tag, $subfield, $fwk );
764         if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
765             my $authvalsth =
766               $dbh->prepare(
767                 "SELECT authorised_value,lib
768                 FROM authorised_values 
769                 WHERE category=? 
770                 ORDER BY lib
771                 "
772               );
773             $authvalsth->execute($authorisedvaluecat);
774             while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
775                 $itemstatus{$authorisedvalue} = $lib;
776             }
777             return \%itemstatus;
778             exit 1;
779         }
780         else {
781
782             #No authvalue list
783             # build default
784         }
785     }
786
787     #No authvalue list
788     #build default
789     $itemstatus{"1"} = "Not For Loan";
790     return \%itemstatus;
791 }
792
793 =head2 GetItemLocation
794
795   $itemlochash = GetItemLocation($fwk);
796
797 Returns a list of valid values for the
798 C<items.location> field.
799
800 NOTE: does B<not> return an individual item's
801 location.
802
803 where fwk stands for an optional framework code.
804 Create a location selector with the following code
805
806 =head3 in PERL SCRIPT
807
808   my $itemlochash = getitemlocation;
809   my @itemlocloop;
810   foreach my $thisloc (keys %$itemlochash) {
811       my $selected = 1 if $thisbranch eq $branch;
812       my %row =(locval => $thisloc,
813                   selected => $selected,
814                   locname => $itemlochash->{$thisloc},
815                );
816       push @itemlocloop, \%row;
817   }
818   $template->param(itemlocationloop => \@itemlocloop);
819
820 =head3 in TEMPLATE
821
822   <select name="location">
823       <option value="">Default</option>
824   <!-- TMPL_LOOP name="itemlocationloop" -->
825       <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
826   <!-- /TMPL_LOOP -->
827   </select>
828
829 =cut
830
831 sub GetItemLocation {
832
833     # returns a reference to a hash of references to location...
834     my ($fwk) = @_;
835     my %itemlocation;
836     my $dbh = C4::Context->dbh;
837     my $sth;
838     $fwk = '' unless ($fwk);
839     my ( $tag, $subfield ) =
840       GetMarcFromKohaField( "items.location", $fwk );
841     if ( $tag and $subfield ) {
842         my $sth =
843           $dbh->prepare(
844             "SELECT authorised_value
845             FROM marc_subfield_structure 
846             WHERE tagfield=? 
847                 AND tagsubfield=? 
848                 AND frameworkcode=?"
849           );
850         $sth->execute( $tag, $subfield, $fwk );
851         if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
852             my $authvalsth =
853               $dbh->prepare(
854                 "SELECT authorised_value,lib
855                 FROM authorised_values
856                 WHERE category=?
857                 ORDER BY lib"
858               );
859             $authvalsth->execute($authorisedvaluecat);
860             while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
861                 $itemlocation{$authorisedvalue} = $lib;
862             }
863             return \%itemlocation;
864             exit 1;
865         }
866         else {
867
868             #No authvalue list
869             # build default
870         }
871     }
872
873     #No authvalue list
874     #build default
875     $itemlocation{"1"} = "Not For Loan";
876     return \%itemlocation;
877 }
878
879 =head2 GetLostItems
880
881   $items = GetLostItems( $where, $orderby );
882
883 This function gets a list of lost items.
884
885 =over 2
886
887 =item input:
888
889 C<$where> is a hashref. it containts a field of the items table as key
890 and the value to match as value. For example:
891
892 { barcode    => 'abc123',
893   homebranch => 'CPL',    }
894
895 C<$orderby> is a field of the items table by which the resultset
896 should be orderd.
897
898 =item return:
899
900 C<$items> is a reference to an array full of hashrefs with columns
901 from the "items" table as keys.
902
903 =item usage in the perl script:
904
905   my $where = { barcode => '0001548' };
906   my $items = GetLostItems( $where, "homebranch" );
907   $template->param( itemsloop => $items );
908
909 =back
910
911 =cut
912
913 sub GetLostItems {
914     # Getting input args.
915     my $where   = shift;
916     my $orderby = shift;
917     my $dbh     = C4::Context->dbh;
918
919     my $query   = "
920         SELECT *
921         FROM   items
922             LEFT JOIN biblio ON (items.biblionumber = biblio.biblionumber)
923             LEFT JOIN biblioitems ON (items.biblionumber = biblioitems.biblionumber)
924             LEFT JOIN authorised_values ON (items.itemlost = authorised_values.authorised_value)
925         WHERE
926                 authorised_values.category = 'LOST'
927                 AND itemlost IS NOT NULL
928                 AND itemlost <> 0
929     ";
930     my @query_parameters;
931     foreach my $key (keys %$where) {
932         $query .= " AND $key LIKE ?";
933         push @query_parameters, "%$where->{$key}%";
934     }
935     my @ordervalues = qw/title author homebranch itype barcode price replacementprice lib datelastseen location/;
936     
937     if ( defined $orderby && grep($orderby, @ordervalues)) {
938         $query .= ' ORDER BY '.$orderby;
939     }
940
941     my $sth = $dbh->prepare($query);
942     $sth->execute( @query_parameters );
943     my $items = [];
944     while ( my $row = $sth->fetchrow_hashref ){
945         push @$items, $row;
946     }
947     return $items;
948 }
949
950 =head2 GetItemsForInventory
951
952   $itemlist = GetItemsForInventory($minlocation, $maxlocation, 
953                  $location, $itemtype $datelastseen, $branch, 
954                  $offset, $size, $statushash);
955
956 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
957
958 The sub returns a reference to a list of hashes, each containing
959 itemnumber, author, title, barcode, item callnumber, and date last
960 seen. It is ordered by callnumber then title.
961
962 The required minlocation & maxlocation parameters are used to specify a range of item callnumbers
963 the datelastseen can be used to specify that you want to see items not seen since a past date only.
964 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
965 $statushash requires a hashref that has the authorized values fieldname (intems.notforloan, etc...) as keys, and an arrayref of statuscodes we are searching for as values.
966
967 =cut
968
969 sub GetItemsForInventory {
970     my ( $minlocation, $maxlocation,$location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $size, $statushash ) = @_;
971     my $dbh = C4::Context->dbh;
972     my ( @bind_params, @where_strings );
973
974     my $query = <<'END_SQL';
975 SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, datelastseen
976 FROM items
977   LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
978   LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
979 END_SQL
980     if ($statushash){
981         for my $authvfield (keys %$statushash){
982             if ( scalar @{$statushash->{$authvfield}} > 0 ){
983                 my $joinedvals = join ',', @{$statushash->{$authvfield}};
984                 push @where_strings, "$authvfield in (" . $joinedvals . ")";
985             }
986         }
987     }
988
989     if ($minlocation) {
990         push @where_strings, 'itemcallnumber >= ?';
991         push @bind_params, $minlocation;
992     }
993
994     if ($maxlocation) {
995         push @where_strings, 'itemcallnumber <= ?';
996         push @bind_params, $maxlocation;
997     }
998
999     if ($datelastseen) {
1000         $datelastseen = format_date_in_iso($datelastseen);  
1001         push @where_strings, '(datelastseen < ? OR datelastseen IS NULL)';
1002         push @bind_params, $datelastseen;
1003     }
1004
1005     if ( $location ) {
1006         push @where_strings, 'items.location = ?';
1007         push @bind_params, $location;
1008     }
1009
1010     if ( $branchcode ) {
1011         if($branch eq "homebranch"){
1012         push @where_strings, 'items.homebranch = ?';
1013         }else{
1014             push @where_strings, 'items.holdingbranch = ?';
1015         }
1016         push @bind_params, $branchcode;
1017     }
1018     
1019     if ( $itemtype ) {
1020         push @where_strings, 'biblioitems.itemtype = ?';
1021         push @bind_params, $itemtype;
1022     }
1023
1024     if ( $ignoreissued) {
1025         $query .= "LEFT JOIN issues ON items.itemnumber = issues.itemnumber ";
1026         push @where_strings, 'issues.date_due IS NULL';
1027     }
1028
1029     if ( @where_strings ) {
1030         $query .= 'WHERE ';
1031         $query .= join ' AND ', @where_strings;
1032     }
1033     $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
1034     my $sth = $dbh->prepare($query);
1035     $sth->execute( @bind_params );
1036
1037     my @results;
1038     $size--;
1039     while ( my $row = $sth->fetchrow_hashref ) {
1040         $offset-- if ($offset);
1041         $row->{datelastseen}=format_date($row->{datelastseen});
1042         if ( ( !$offset ) && $size ) {
1043             push @results, $row;
1044             $size--;
1045         }
1046     }
1047     return \@results;
1048 }
1049
1050 =head2 GetItemsCount
1051
1052   $count = &GetItemsCount( $biblionumber);
1053
1054 This function return count of item with $biblionumber
1055
1056 =cut
1057
1058 sub GetItemsCount {
1059     my ( $biblionumber ) = @_;
1060     my $dbh = C4::Context->dbh;
1061     my $query = "SELECT count(*)
1062           FROM  items 
1063           WHERE biblionumber=?";
1064     my $sth = $dbh->prepare($query);
1065     $sth->execute($biblionumber);
1066     my $count = $sth->fetchrow;  
1067     return ($count);
1068 }
1069
1070 =head2 GetItemInfosOf
1071
1072   GetItemInfosOf(@itemnumbers);
1073
1074 =cut
1075
1076 sub GetItemInfosOf {
1077     my @itemnumbers = @_;
1078
1079     my $query = '
1080         SELECT *
1081         FROM items
1082         WHERE itemnumber IN (' . join( ',', @itemnumbers ) . ')
1083     ';
1084     return get_infos_of( $query, 'itemnumber' );
1085 }
1086
1087 =head2 GetItemsByBiblioitemnumber
1088
1089   GetItemsByBiblioitemnumber($biblioitemnumber);
1090
1091 Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
1092 Called by C<C4::XISBN>
1093
1094 =cut
1095
1096 sub GetItemsByBiblioitemnumber {
1097     my ( $bibitem ) = @_;
1098     my $dbh = C4::Context->dbh;
1099     my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
1100     # Get all items attached to a biblioitem
1101     my $i = 0;
1102     my @results; 
1103     $sth->execute($bibitem) || die $sth->errstr;
1104     while ( my $data = $sth->fetchrow_hashref ) {  
1105         # Foreach item, get circulation information
1106         my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
1107                                    WHERE itemnumber = ?
1108                                    AND issues.borrowernumber = borrowers.borrowernumber"
1109         );
1110         $sth2->execute( $data->{'itemnumber'} );
1111         if ( my $data2 = $sth2->fetchrow_hashref ) {
1112             # if item is out, set the due date and who it is out too
1113             $data->{'date_due'}   = $data2->{'date_due'};
1114             $data->{'cardnumber'} = $data2->{'cardnumber'};
1115             $data->{'borrowernumber'}   = $data2->{'borrowernumber'};
1116         }
1117         else {
1118             # set date_due to blank, so in the template we check itemlost, and wthdrawn 
1119             $data->{'date_due'} = '';                                                                                                         
1120         }    # else         
1121         # Find the last 3 people who borrowed this item.                  
1122         my $query2 = "SELECT * FROM old_issues, borrowers WHERE itemnumber = ?
1123                       AND old_issues.borrowernumber = borrowers.borrowernumber
1124                       ORDER BY returndate desc,timestamp desc LIMIT 3";
1125         $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
1126         $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
1127         my $i2 = 0;
1128         while ( my $data2 = $sth2->fetchrow_hashref ) {
1129             $data->{"timestamp$i2"} = $data2->{'timestamp'};
1130             $data->{"card$i2"}      = $data2->{'cardnumber'};
1131             $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
1132             $i2++;
1133         }
1134         push(@results,$data);
1135     } 
1136     return (\@results); 
1137 }
1138
1139 =head2 GetItemsInfo
1140
1141   @results = GetItemsInfo($biblionumber, $type);
1142
1143 Returns information about books with the given biblionumber.
1144
1145 C<$type> may be either C<intra> or anything else. If it is not set to
1146 C<intra>, then the search will exclude lost, very overdue, and
1147 withdrawn items.
1148
1149 C<GetItemsInfo> returns a list of references-to-hash. Each element
1150 contains a number of keys. Most of them are table items from the
1151 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
1152 Koha database. Other keys include:
1153
1154 =over 2
1155
1156 =item C<$data-E<gt>{branchname}>
1157
1158 The name (not the code) of the branch to which the book belongs.
1159
1160 =item C<$data-E<gt>{datelastseen}>
1161
1162 This is simply C<items.datelastseen>, except that while the date is
1163 stored in YYYY-MM-DD format in the database, here it is converted to
1164 DD/MM/YYYY format. A NULL date is returned as C<//>.
1165
1166 =item C<$data-E<gt>{datedue}>
1167
1168 =item C<$data-E<gt>{class}>
1169
1170 This is the concatenation of C<biblioitems.classification>, the book's
1171 Dewey code, and C<biblioitems.subclass>.
1172
1173 =item C<$data-E<gt>{ocount}>
1174
1175 I think this is the number of copies of the book available.
1176
1177 =item C<$data-E<gt>{order}>
1178
1179 If this is set, it is set to C<One Order>.
1180
1181 =back
1182
1183 =cut
1184
1185 sub GetItemsInfo {
1186     my ( $biblionumber, $type ) = @_;
1187     my $dbh   = C4::Context->dbh;
1188     # note biblioitems.* must be avoided to prevent large marc and marcxml fields from killing performance.
1189     my $query = "
1190     SELECT items.*,
1191            biblio.*,
1192            biblioitems.volume,
1193            biblioitems.number,
1194            biblioitems.itemtype,
1195            biblioitems.isbn,
1196            biblioitems.issn,
1197            biblioitems.publicationyear,
1198            biblioitems.publishercode,
1199            biblioitems.volumedate,
1200            biblioitems.volumedesc,
1201            biblioitems.lccn,
1202            biblioitems.url,
1203            items.notforloan as itemnotforloan,
1204            itemtypes.description,
1205            itemtypes.notforloan as notforloan_per_itemtype,
1206            branchurl
1207      FROM items
1208      LEFT JOIN branches ON items.homebranch = branches.branchcode
1209      LEFT JOIN biblio      ON      biblio.biblionumber     = items.biblionumber
1210      LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1211      LEFT JOIN itemtypes   ON   itemtypes.itemtype         = "
1212      . (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype');
1213     $query .= " WHERE items.biblionumber = ? ORDER BY branches.branchname,items.dateaccessioned desc" ;
1214     my $sth = $dbh->prepare($query);
1215     $sth->execute($biblionumber);
1216     my $i = 0;
1217     my @results;
1218     my $serial;
1219
1220     my $isth    = $dbh->prepare(
1221         "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname,borrowers.branchcode as bcode
1222         FROM   issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
1223         WHERE  itemnumber = ?"
1224        );
1225         my $ssth = $dbh->prepare("SELECT serialseq,publisheddate from serialitems left join serial on serialitems.serialid=serial.serialid where serialitems.itemnumber=? "); 
1226         while ( my $data = $sth->fetchrow_hashref ) {
1227         my $datedue = '';
1228         my $count_reserves;
1229         $isth->execute( $data->{'itemnumber'} );
1230         if ( my $idata = $isth->fetchrow_hashref ) {
1231             $data->{borrowernumber} = $idata->{borrowernumber};
1232             $data->{cardnumber}     = $idata->{cardnumber};
1233             $data->{surname}     = $idata->{surname};
1234             $data->{firstname}     = $idata->{firstname};
1235             $datedue                = $idata->{'date_due'};
1236         if (C4::Context->preference("IndependantBranches")){
1237         my $userenv = C4::Context->userenv;
1238         if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) { 
1239             $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch});
1240         }
1241         }
1242         }
1243                 if ( $data->{'serial'}) {       
1244                         $ssth->execute($data->{'itemnumber'}) ;
1245                         ($data->{'serialseq'} , $data->{'publisheddate'}) = $ssth->fetchrow_array();
1246                         $serial = 1;
1247         }
1248                 if ( $datedue eq '' ) {
1249             my ( $restype, $reserves ) =
1250               C4::Reserves::CheckReserves( $data->{'itemnumber'} );
1251 # Previous conditional check with if ($restype) is not needed because a true
1252 # result for one item will result in subsequent items defaulting to this true
1253 # value.
1254             $count_reserves = $restype;
1255         }
1256         #get branch information.....
1257         my $bsth = $dbh->prepare(
1258             "SELECT * FROM branches WHERE branchcode = ?
1259         "
1260         );
1261         $bsth->execute( $data->{'holdingbranch'} );
1262         if ( my $bdata = $bsth->fetchrow_hashref ) {
1263             $data->{'branchname'} = $bdata->{'branchname'};
1264         }
1265         $data->{'datedue'}        = $datedue;
1266         $data->{'count_reserves'} = $count_reserves;
1267
1268         # get notforloan complete status if applicable
1269         my $sthnflstatus = $dbh->prepare(
1270             'SELECT authorised_value
1271             FROM   marc_subfield_structure
1272             WHERE  kohafield="items.notforloan"
1273         '
1274         );
1275
1276         $sthnflstatus->execute;
1277         my ($authorised_valuecode) = $sthnflstatus->fetchrow;
1278         if ($authorised_valuecode) {
1279             $sthnflstatus = $dbh->prepare(
1280                 "SELECT lib FROM authorised_values
1281                  WHERE  category=?
1282                  AND authorised_value=?"
1283             );
1284             $sthnflstatus->execute( $authorised_valuecode,
1285                 $data->{itemnotforloan} );
1286             my ($lib) = $sthnflstatus->fetchrow;
1287             $data->{notforloanvalue} = $lib;
1288         }
1289
1290         # get restricted status and description if applicable
1291         my $restrictedstatus = $dbh->prepare(
1292             'SELECT authorised_value
1293             FROM   marc_subfield_structure
1294             WHERE  kohafield="items.restricted"
1295         '
1296         );
1297
1298         $restrictedstatus->execute;
1299         ($authorised_valuecode) = $restrictedstatus->fetchrow;
1300         if ($authorised_valuecode) {
1301             $restrictedstatus = $dbh->prepare(
1302                 "SELECT lib,lib_opac FROM authorised_values
1303                  WHERE  category=?
1304                  AND authorised_value=?"
1305             );
1306             $restrictedstatus->execute( $authorised_valuecode,
1307                 $data->{restricted} );
1308
1309             if ( my $rstdata = $restrictedstatus->fetchrow_hashref ) {
1310                 $data->{restricted} = $rstdata->{'lib'};
1311                 $data->{restrictedopac} = $rstdata->{'lib_opac'};
1312             }
1313         }
1314
1315         # my stack procedures
1316         my $stackstatus = $dbh->prepare(
1317             'SELECT authorised_value
1318              FROM   marc_subfield_structure
1319              WHERE  kohafield="items.stack"
1320         '
1321         );
1322         $stackstatus->execute;
1323
1324         ($authorised_valuecode) = $stackstatus->fetchrow;
1325         if ($authorised_valuecode) {
1326             $stackstatus = $dbh->prepare(
1327                 "SELECT lib
1328                  FROM   authorised_values
1329                  WHERE  category=?
1330                  AND    authorised_value=?
1331             "
1332             );
1333             $stackstatus->execute( $authorised_valuecode, $data->{stack} );
1334             my ($lib) = $stackstatus->fetchrow;
1335             $data->{stack} = $lib;
1336         }
1337         # Find the last 3 people who borrowed this item.
1338         my $sth2 = $dbh->prepare("SELECT * FROM old_issues,borrowers
1339                                     WHERE itemnumber = ?
1340                                     AND old_issues.borrowernumber = borrowers.borrowernumber
1341                                     ORDER BY returndate DESC
1342                                     LIMIT 3");
1343         $sth2->execute($data->{'itemnumber'});
1344         my $ii = 0;
1345         while (my $data2 = $sth2->fetchrow_hashref()) {
1346             $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
1347             $data->{"card$ii"}      = $data2->{'cardnumber'} if $data2->{'cardnumber'};
1348             $data->{"borrower$ii"}  = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
1349             $ii++;
1350         }
1351
1352         $results[$i] = $data;
1353         $i++;
1354     }
1355         if($serial) {
1356                 return( sort { ($b->{'publisheddate'} || $b->{'enumchron'}) cmp ($a->{'publisheddate'} || $a->{'enumchron'}) } @results );
1357         } else {
1358         return (@results);
1359         }
1360 }
1361
1362 =head2 GetLastAcquisitions
1363
1364   my $lastacq = GetLastAcquisitions({'branches' => ('branch1','branch2'), 
1365                                     'itemtypes' => ('BK','BD')}, 10);
1366
1367 =cut
1368
1369 sub  GetLastAcquisitions {
1370         my ($data,$max) = @_;
1371
1372         my $itemtype = C4::Context->preference('item-level_itypes') ? 'itype' : 'itemtype';
1373         
1374         my $number_of_branches = @{$data->{branches}};
1375         my $number_of_itemtypes   = @{$data->{itemtypes}};
1376         
1377         
1378         my @where = ('WHERE 1 '); 
1379         $number_of_branches and push @where
1380            , 'AND holdingbranch IN (' 
1381            , join(',', ('?') x $number_of_branches )
1382            , ')'
1383          ;
1384         
1385         $number_of_itemtypes and push @where
1386            , "AND $itemtype IN (" 
1387            , join(',', ('?') x $number_of_itemtypes )
1388            , ')'
1389          ;
1390
1391         my $query = "SELECT biblio.biblionumber as biblionumber, title, dateaccessioned
1392                                  FROM items RIGHT JOIN biblio ON (items.biblionumber=biblio.biblionumber) 
1393                                     RIGHT JOIN biblioitems ON (items.biblioitemnumber=biblioitems.biblioitemnumber)
1394                                     @where
1395                                     GROUP BY biblio.biblionumber 
1396                                     ORDER BY dateaccessioned DESC LIMIT $max";
1397
1398         my $dbh = C4::Context->dbh;
1399         my $sth = $dbh->prepare($query);
1400     
1401     $sth->execute((@{$data->{branches}}, @{$data->{itemtypes}}));
1402         
1403         my @results;
1404         while( my $row = $sth->fetchrow_hashref){
1405                 push @results, {date => $row->{dateaccessioned} 
1406                                                 , biblionumber => $row->{biblionumber}
1407                                                 , title => $row->{title}};
1408         }
1409         
1410         return @results;
1411 }
1412
1413 =head2 get_itemnumbers_of
1414
1415   my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
1416
1417 Given a list of biblionumbers, return the list of corresponding itemnumbers
1418 for each biblionumber.
1419
1420 Return a reference on a hash where keys are biblionumbers and values are
1421 references on array of itemnumbers.
1422
1423 =cut
1424
1425 sub get_itemnumbers_of {
1426     my @biblionumbers = @_;
1427
1428     my $dbh = C4::Context->dbh;
1429
1430     my $query = '
1431         SELECT itemnumber,
1432             biblionumber
1433         FROM items
1434         WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ')
1435     ';
1436     my $sth = $dbh->prepare($query);
1437     $sth->execute(@biblionumbers);
1438
1439     my %itemnumbers_of;
1440
1441     while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
1442         push @{ $itemnumbers_of{$biblionumber} }, $itemnumber;
1443     }
1444
1445     return \%itemnumbers_of;
1446 }
1447
1448 =head2 GetItemnumberFromBarcode
1449
1450   $result = GetItemnumberFromBarcode($barcode);
1451
1452 =cut
1453
1454 sub GetItemnumberFromBarcode {
1455     my ($barcode) = @_;
1456     my $dbh = C4::Context->dbh;
1457
1458     my $rq =
1459       $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
1460     $rq->execute($barcode);
1461     my ($result) = $rq->fetchrow;
1462     return ($result);
1463 }
1464
1465 =head2 GetBarcodeFromItemnumber
1466
1467   $result = GetBarcodeFromItemnumber($itemnumber);
1468
1469 =cut
1470
1471 sub GetBarcodeFromItemnumber {
1472     my ($itemnumber) = @_;
1473     my $dbh = C4::Context->dbh;
1474
1475     my $rq =
1476       $dbh->prepare("SELECT barcode FROM items WHERE items.itemnumber=?");
1477     $rq->execute($itemnumber);
1478     my ($result) = $rq->fetchrow;
1479     return ($result);
1480 }
1481
1482 =head3 get_item_authorised_values
1483
1484 find the types and values for all authorised values assigned to this item.
1485
1486 parameters: itemnumber
1487
1488 returns: a hashref malling the authorised value to the value set for this itemnumber
1489
1490     $authorised_values = {
1491              'CCODE'      => undef,
1492              'DAMAGED'    => '0',
1493              'LOC'        => '3',
1494              'LOST'       => '0'
1495              'NOT_LOAN'   => '0',
1496              'RESTRICTED' => undef,
1497              'STACK'      => undef,
1498              'WITHDRAWN'  => '0',
1499              'branches'   => 'CPL',
1500              'cn_source'  => undef,
1501              'itemtypes'  => 'SER',
1502            };
1503
1504 Notes: see C4::Biblio::get_biblio_authorised_values for a similar method at the biblio level.
1505
1506 =cut
1507
1508 sub get_item_authorised_values {
1509     my $itemnumber = shift;
1510
1511     # assume that these entries in the authorised_value table are item level.
1512     my $query = q(SELECT distinct authorised_value, kohafield
1513                     FROM marc_subfield_structure
1514                     WHERE kohafield like 'item%'
1515                       AND authorised_value != '' );
1516
1517     my $itemlevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
1518     my $iteminfo = GetItem( $itemnumber );
1519     # warn( Data::Dumper->Dump( [ $itemlevel_authorised_values ], [ 'itemlevel_authorised_values' ] ) );
1520     my $return;
1521     foreach my $this_authorised_value ( keys %$itemlevel_authorised_values ) {
1522         my $field = $itemlevel_authorised_values->{ $this_authorised_value }->{'kohafield'};
1523         $field =~ s/^items\.//;
1524         if ( exists $iteminfo->{ $field } ) {
1525             $return->{ $this_authorised_value } = $iteminfo->{ $field };
1526         }
1527     }
1528     # warn( Data::Dumper->Dump( [ $return ], [ 'return' ] ) );
1529     return $return;
1530 }
1531
1532 =head3 get_authorised_value_images
1533
1534 find a list of icons that are appropriate for display based on the
1535 authorised values for a biblio.
1536
1537 parameters: listref of authorised values, such as comes from
1538 get_item_authorised_values or
1539 from C4::Biblio::get_biblio_authorised_values
1540
1541 returns: listref of hashrefs for each image. Each hashref looks like this:
1542
1543       { imageurl => '/intranet-tmpl/prog/img/itemtypeimg/npl/WEB.gif',
1544         label    => '',
1545         category => '',
1546         value    => '', }
1547
1548 Notes: Currently, I put on the full path to the images on the staff
1549 side. This should either be configurable or not done at all. Since I
1550 have to deal with 'intranet' or 'opac' in
1551 get_biblio_authorised_values, perhaps I should be passing it in.
1552
1553 =cut
1554
1555 sub get_authorised_value_images {
1556     my $authorised_values = shift;
1557
1558     my @imagelist;
1559
1560     my $authorised_value_list = GetAuthorisedValues();
1561     # warn ( Data::Dumper->Dump( [ $authorised_value_list ], [ 'authorised_value_list' ] ) );
1562     foreach my $this_authorised_value ( @$authorised_value_list ) {
1563         if ( exists $authorised_values->{ $this_authorised_value->{'category'} }
1564              && $authorised_values->{ $this_authorised_value->{'category'} } eq $this_authorised_value->{'authorised_value'} ) {
1565             # warn ( Data::Dumper->Dump( [ $this_authorised_value ], [ 'this_authorised_value' ] ) );
1566             if ( defined $this_authorised_value->{'imageurl'} ) {
1567                 push @imagelist, { imageurl => C4::Koha::getitemtypeimagelocation( 'intranet', $this_authorised_value->{'imageurl'} ),
1568                                    label    => $this_authorised_value->{'lib'},
1569                                    category => $this_authorised_value->{'category'},
1570                                    value    => $this_authorised_value->{'authorised_value'}, };
1571             }
1572         }
1573     }
1574
1575     # warn ( Data::Dumper->Dump( [ \@imagelist ], [ 'imagelist' ] ) );
1576     return \@imagelist;
1577
1578 }
1579
1580 =head1 LIMITED USE FUNCTIONS
1581
1582 The following functions, while part of the public API,
1583 are not exported.  This is generally because they are
1584 meant to be used by only one script for a specific
1585 purpose, and should not be used in any other context
1586 without careful thought.
1587
1588 =cut
1589
1590 =head2 GetMarcItem
1591
1592   my $item_marc = GetMarcItem($biblionumber, $itemnumber);
1593
1594 Returns MARC::Record of the item passed in parameter.
1595 This function is meant for use only in C<cataloguing/additem.pl>,
1596 where it is needed to support that script's MARC-like
1597 editor.
1598
1599 =cut
1600
1601 sub GetMarcItem {
1602     my ( $biblionumber, $itemnumber ) = @_;
1603
1604     # GetMarcItem has been revised so that it does the following:
1605     #  1. Gets the item information from the items table.
1606     #  2. Converts it to a MARC field for storage in the bib record.
1607     #
1608     # The previous behavior was:
1609     #  1. Get the bib record.
1610     #  2. Return the MARC tag corresponding to the item record.
1611     #
1612     # The difference is that one treats the items row as authoritative,
1613     # while the other treats the MARC representation as authoritative
1614     # under certain circumstances.
1615
1616     my $itemrecord = GetItem($itemnumber);
1617
1618     # Tack on 'items.' prefix to column names so that TransformKohaToMarc will work.
1619     # Also, don't emit a subfield if the underlying field is blank.
1620
1621     
1622     return Item2Marc($itemrecord,$biblionumber);
1623
1624 }
1625 sub Item2Marc {
1626         my ($itemrecord,$biblionumber)=@_;
1627     my $mungeditem = { 
1628         map {  
1629             defined($itemrecord->{$_}) && $itemrecord->{$_} ne '' ? ("items.$_" => $itemrecord->{$_}) : ()  
1630         } keys %{ $itemrecord } 
1631     };
1632     my $itemmarc = TransformKohaToMarc($mungeditem);
1633     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",GetFrameworkCode($biblionumber)||'');
1634
1635     my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($mungeditem->{'items.more_subfields_xml'});
1636     if (defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1) {
1637                 foreach my $field ($itemmarc->field($itemtag)){
1638             $field->add_subfields(@$unlinked_item_subfields);
1639         }
1640     }
1641         return $itemmarc;
1642 }
1643
1644 =head1 PRIVATE FUNCTIONS AND VARIABLES
1645
1646 The following functions are not meant to be called
1647 directly, but are documented in order to explain
1648 the inner workings of C<C4::Items>.
1649
1650 =cut
1651
1652 =head2 %derived_columns
1653
1654 This hash keeps track of item columns that
1655 are strictly derived from other columns in
1656 the item record and are not meant to be set
1657 independently.
1658
1659 Each key in the hash should be the name of a
1660 column (as named by TransformMarcToKoha).  Each
1661 value should be hashref whose keys are the
1662 columns on which the derived column depends.  The
1663 hashref should also contain a 'BUILDER' key
1664 that is a reference to a sub that calculates
1665 the derived value.
1666
1667 =cut
1668
1669 my %derived_columns = (
1670     'items.cn_sort' => {
1671         'itemcallnumber' => 1,
1672         'items.cn_source' => 1,
1673         'BUILDER' => \&_calc_items_cn_sort,
1674     }
1675 );
1676
1677 =head2 _set_derived_columns_for_add 
1678
1679   _set_derived_column_for_add($item);
1680
1681 Given an item hash representing a new item to be added,
1682 calculate any derived columns.  Currently the only
1683 such column is C<items.cn_sort>.
1684
1685 =cut
1686
1687 sub _set_derived_columns_for_add {
1688     my $item = shift;
1689
1690     foreach my $column (keys %derived_columns) {
1691         my $builder = $derived_columns{$column}->{'BUILDER'};
1692         my $source_values = {};
1693         foreach my $source_column (keys %{ $derived_columns{$column} }) {
1694             next if $source_column eq 'BUILDER';
1695             $source_values->{$source_column} = $item->{$source_column};
1696         }
1697         $builder->($item, $source_values);
1698     }
1699 }
1700
1701 =head2 _set_derived_columns_for_mod 
1702
1703   _set_derived_column_for_mod($item);
1704
1705 Given an item hash representing a new item to be modified.
1706 calculate any derived columns.  Currently the only
1707 such column is C<items.cn_sort>.
1708
1709 This routine differs from C<_set_derived_columns_for_add>
1710 in that it needs to handle partial item records.  In other
1711 words, the caller of C<ModItem> may have supplied only one
1712 or two columns to be changed, so this function needs to
1713 determine whether any of the columns to be changed affect
1714 any of the derived columns.  Also, if a derived column
1715 depends on more than one column, but the caller is not
1716 changing all of then, this routine retrieves the unchanged
1717 values from the database in order to ensure a correct
1718 calculation.
1719
1720 =cut
1721
1722 sub _set_derived_columns_for_mod {
1723     my $item = shift;
1724
1725     foreach my $column (keys %derived_columns) {
1726         my $builder = $derived_columns{$column}->{'BUILDER'};
1727         my $source_values = {};
1728         my %missing_sources = ();
1729         my $must_recalc = 0;
1730         foreach my $source_column (keys %{ $derived_columns{$column} }) {
1731             next if $source_column eq 'BUILDER';
1732             if (exists $item->{$source_column}) {
1733                 $must_recalc = 1;
1734                 $source_values->{$source_column} = $item->{$source_column};
1735             } else {
1736                 $missing_sources{$source_column} = 1;
1737             }
1738         }
1739         if ($must_recalc) {
1740             foreach my $source_column (keys %missing_sources) {
1741                 $source_values->{$source_column} = _get_single_item_column($source_column, $item->{'itemnumber'});
1742             }
1743             $builder->($item, $source_values);
1744         }
1745     }
1746 }
1747
1748 =head2 _do_column_fixes_for_mod
1749
1750   _do_column_fixes_for_mod($item);
1751
1752 Given an item hashref containing one or more
1753 columns to modify, fix up certain values.
1754 Specifically, set to 0 any passed value
1755 of C<notforloan>, C<damaged>, C<itemlost>, or
1756 C<wthdrawn> that is either undefined or
1757 contains the empty string.
1758
1759 =cut
1760
1761 sub _do_column_fixes_for_mod {
1762     my $item = shift;
1763
1764     if (exists $item->{'notforloan'} and
1765         (not defined $item->{'notforloan'} or $item->{'notforloan'} eq '')) {
1766         $item->{'notforloan'} = 0;
1767     }
1768     if (exists $item->{'damaged'} and
1769         (not defined $item->{'damaged'} or $item->{'damaged'} eq '')) {
1770         $item->{'damaged'} = 0;
1771     }
1772     if (exists $item->{'itemlost'} and
1773         (not defined $item->{'itemlost'} or $item->{'itemlost'} eq '')) {
1774         $item->{'itemlost'} = 0;
1775     }
1776     if (exists $item->{'wthdrawn'} and
1777         (not defined $item->{'wthdrawn'} or $item->{'wthdrawn'} eq '')) {
1778         $item->{'wthdrawn'} = 0;
1779     }
1780     if (exists $item->{'location'} && !exists $item->{'permanent_location'}) {
1781         $item->{'permanent_location'} = $item->{'location'};
1782     }
1783     if (exists $item->{'timestamp'}) {
1784         delete $item->{'timestamp'};
1785     }
1786 }
1787
1788 =head2 _get_single_item_column
1789
1790   _get_single_item_column($column, $itemnumber);
1791
1792 Retrieves the value of a single column from an C<items>
1793 row specified by C<$itemnumber>.
1794
1795 =cut
1796
1797 sub _get_single_item_column {
1798     my $column = shift;
1799     my $itemnumber = shift;
1800     
1801     my $dbh = C4::Context->dbh;
1802     my $sth = $dbh->prepare("SELECT $column FROM items WHERE itemnumber = ?");
1803     $sth->execute($itemnumber);
1804     my ($value) = $sth->fetchrow();
1805     return $value; 
1806 }
1807
1808 =head2 _calc_items_cn_sort
1809
1810   _calc_items_cn_sort($item, $source_values);
1811
1812 Helper routine to calculate C<items.cn_sort>.
1813
1814 =cut
1815
1816 sub _calc_items_cn_sort {
1817     my $item = shift;
1818     my $source_values = shift;
1819
1820     $item->{'items.cn_sort'} = GetClassSort($source_values->{'items.cn_source'}, $source_values->{'itemcallnumber'}, "");
1821 }
1822
1823 =head2 _set_defaults_for_add 
1824
1825   _set_defaults_for_add($item_hash);
1826
1827 Given an item hash representing an item to be added, set
1828 correct default values for columns whose default value
1829 is not handled by the DBMS.  This includes the following
1830 columns:
1831
1832 =over 2
1833
1834 =item * 
1835
1836 C<items.dateaccessioned>
1837
1838 =item *
1839
1840 C<items.notforloan>
1841
1842 =item *
1843
1844 C<items.damaged>
1845
1846 =item *
1847
1848 C<items.itemlost>
1849
1850 =item *
1851
1852 C<items.wthdrawn>
1853
1854 =back
1855
1856 =cut
1857
1858 sub _set_defaults_for_add {
1859     my $item = shift;
1860     $item->{dateaccessioned} ||= C4::Dates->new->output('iso');
1861     $item->{$_} ||= 0 for (qw( notforloan damaged itemlost wthdrawn));
1862 }
1863
1864 =head2 _koha_new_item
1865
1866   my ($itemnumber,$error) = _koha_new_item( $item, $barcode );
1867
1868 Perform the actual insert into the C<items> table.
1869
1870 =cut
1871
1872 sub _koha_new_item {
1873     my ( $item, $barcode ) = @_;
1874     my $dbh=C4::Context->dbh;  
1875     my $error;
1876     my $query =
1877            "INSERT INTO items SET
1878             biblionumber        = ?,
1879             biblioitemnumber    = ?,
1880             barcode             = ?,
1881             dateaccessioned     = ?,
1882             booksellerid        = ?,
1883             homebranch          = ?,
1884             price               = ?,
1885             replacementprice    = ?,
1886             replacementpricedate = NOW(),
1887             datelastborrowed    = ?,
1888             datelastseen        = NOW(),
1889             stack               = ?,
1890             notforloan          = ?,
1891             damaged             = ?,
1892             itemlost            = ?,
1893             wthdrawn            = ?,
1894             itemcallnumber      = ?,
1895             restricted          = ?,
1896             itemnotes           = ?,
1897             holdingbranch       = ?,
1898             paidfor             = ?,
1899             location            = ?,
1900             onloan              = ?,
1901             issues              = ?,
1902             renewals            = ?,
1903             reserves            = ?,
1904             cn_source           = ?,
1905             cn_sort             = ?,
1906             ccode               = ?,
1907             itype               = ?,
1908             materials           = ?,
1909             uri = ?,
1910             enumchron           = ?,
1911             more_subfields_xml  = ?,
1912             copynumber          = ?
1913           ";
1914     my $sth = $dbh->prepare($query);
1915    $sth->execute(
1916             $item->{'biblionumber'},
1917             $item->{'biblioitemnumber'},
1918             $barcode,
1919             $item->{'dateaccessioned'},
1920             $item->{'booksellerid'},
1921             $item->{'homebranch'},
1922             $item->{'price'},
1923             $item->{'replacementprice'},
1924             $item->{datelastborrowed},
1925             $item->{stack},
1926             $item->{'notforloan'},
1927             $item->{'damaged'},
1928             $item->{'itemlost'},
1929             $item->{'wthdrawn'},
1930             $item->{'itemcallnumber'},
1931             $item->{'restricted'},
1932             $item->{'itemnotes'},
1933             $item->{'holdingbranch'},
1934             $item->{'paidfor'},
1935             $item->{'location'},
1936             $item->{'onloan'},
1937             $item->{'issues'},
1938             $item->{'renewals'},
1939             $item->{'reserves'},
1940             $item->{'items.cn_source'},
1941             $item->{'items.cn_sort'},
1942             $item->{'ccode'},
1943             $item->{'itype'},
1944             $item->{'materials'},
1945             $item->{'uri'},
1946             $item->{'enumchron'},
1947             $item->{'more_subfields_xml'},
1948             $item->{'copynumber'},
1949     );
1950     my $itemnumber = $dbh->{'mysql_insertid'};
1951     if ( defined $sth->errstr ) {
1952         $error.="ERROR in _koha_new_item $query".$sth->errstr;
1953     }
1954     return ( $itemnumber, $error );
1955 }
1956
1957 =head2 MoveItemFromBiblio
1958
1959   MoveItemFromBiblio($itenumber, $frombiblio, $tobiblio);
1960
1961 Moves an item from a biblio to another
1962
1963 Returns undef if the move failed or the biblionumber of the destination record otherwise
1964
1965 =cut
1966
1967 sub MoveItemFromBiblio {
1968     my ($itemnumber, $frombiblio, $tobiblio) = @_;
1969     my $dbh = C4::Context->dbh;
1970     my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = ?");
1971     $sth->execute( $tobiblio );
1972     my ( $tobiblioitem ) = $sth->fetchrow();
1973     $sth = $dbh->prepare("UPDATE items SET biblioitemnumber = ?, biblionumber = ? WHERE itemnumber = ? AND biblionumber = ?");
1974     my $return = $sth->execute($tobiblioitem, $tobiblio, $itemnumber, $frombiblio);
1975     if ($return == 1) {
1976
1977         # Getting framework
1978         my $frameworkcode = GetFrameworkCode($frombiblio);
1979
1980         # Getting marc field for itemnumber
1981         my ($itemtag, $itemsubfield) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
1982
1983         # Getting the record we want to move the item from
1984         my $record = GetMarcBiblio($frombiblio);
1985
1986         # The item we want to move
1987         my $item;
1988
1989         # For each item
1990         foreach my $fielditem ($record->field($itemtag)){
1991                 # If it is the item we want to move
1992                 if ($fielditem->subfield($itemsubfield) == $itemnumber) {
1993                     # We save it
1994                     $item = $fielditem;
1995                     # Then delete it from the record
1996                     $record->delete_field($fielditem) 
1997                 }
1998         }
1999
2000         # If we found an item (should always true, except in case of database-marcxml inconsistency)
2001         if ($item) {
2002
2003             # Checking if the item we want to move is in an order 
2004             my $order = GetOrderFromItemnumber($itemnumber);
2005             if ($order) {
2006                 # Replacing the biblionumber within the order if necessary
2007                 $order->{'biblionumber'} = $tobiblio;
2008                 ModOrder($order);
2009             }
2010
2011             # Saving the modification
2012             ModBiblioMarc($record, $frombiblio, $frameworkcode);
2013
2014             # Getting the record we want to move the item to
2015             $record = GetMarcBiblio($tobiblio);
2016
2017             # Inserting the previously saved item
2018             $record->insert_fields_ordered($item);      
2019
2020             # Saving the modification
2021             ModBiblioMarc($record, $tobiblio, $frameworkcode);
2022
2023         } else {
2024             return undef;
2025         }
2026     } else {
2027         return undef;
2028     }
2029 }
2030
2031 =head2 DelItemCheck
2032
2033    DelItemCheck($dbh, $biblionumber, $itemnumber);
2034
2035 Exported function (core API) for deleting an item record in Koha if there no current issue.
2036
2037 =cut
2038
2039 sub DelItemCheck {
2040     my ( $dbh, $biblionumber, $itemnumber ) = @_;
2041     my $error;
2042
2043     # check that there is no issue on this item before deletion.
2044     my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
2045     $sth->execute($itemnumber);
2046
2047     my $item = GetItem($itemnumber);
2048     my $onloan = $sth->fetchrow;
2049     if ($onloan) {
2050         $error = "book_on_loan";
2051     }
2052     elsif (C4::Context->preference("IndependantBranches") and (C4::Context->userenv->{branch} ne $item->{C4::Context->preference("HomeOrHoldingBranch")||'homebranch'})){
2053         $error = "not_same_branch";
2054     } 
2055     else {
2056         if ($onloan){ 
2057             $error = "book_on_loan" 
2058         }
2059         else {
2060             # check it doesnt have a waiting reserve
2061             $sth=$dbh->prepare("SELECT * FROM reserves WHERE (found = 'W' or found = 'T') AND itemnumber = ?");
2062             $sth->execute($itemnumber);
2063             my $reserve=$sth->fetchrow;
2064             if ($reserve) {
2065                 $error = "book_reserved";
2066             } 
2067             else {
2068                 DelItem($dbh, $biblionumber, $itemnumber);
2069                 return 1;
2070             }
2071         }
2072     }
2073     return $error;
2074 }
2075
2076 =head2 _koha_modify_item
2077
2078   my ($itemnumber,$error) =_koha_modify_item( $item );
2079
2080 Perform the actual update of the C<items> row.  Note that this
2081 routine accepts a hashref specifying the columns to update.
2082
2083 =cut
2084
2085 sub _koha_modify_item {
2086     my ( $item ) = @_;
2087     my $dbh=C4::Context->dbh;  
2088     my $error;
2089
2090     my $query = "UPDATE items SET ";
2091     my @bind;
2092     for my $key ( keys %$item ) {
2093         $query.="$key=?,";
2094         push @bind, $item->{$key};
2095     }
2096     $query =~ s/,$//;
2097     $query .= " WHERE itemnumber=?";
2098     push @bind, $item->{'itemnumber'};
2099     my $sth = C4::Context->dbh->prepare($query);
2100     $sth->execute(@bind);
2101     if ( C4::Context->dbh->errstr ) {
2102         $error.="ERROR in _koha_modify_item $query".$dbh->errstr;
2103         warn $error;
2104     }
2105     return ($item->{'itemnumber'},$error);
2106 }
2107
2108 =head2 _koha_delete_item
2109
2110   _koha_delete_item( $dbh, $itemnum );
2111
2112 Internal function to delete an item record from the koha tables
2113
2114 =cut
2115
2116 sub _koha_delete_item {
2117     my ( $dbh, $itemnum ) = @_;
2118
2119     # save the deleted item to deleteditems table
2120     my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
2121     $sth->execute($itemnum);
2122     my $data = $sth->fetchrow_hashref();
2123     my $query = "INSERT INTO deleteditems SET ";
2124     my @bind  = ();
2125     foreach my $key ( keys %$data ) {
2126         $query .= "$key = ?,";
2127         push( @bind, $data->{$key} );
2128     }
2129     $query =~ s/\,$//;
2130     $sth = $dbh->prepare($query);
2131     $sth->execute(@bind);
2132
2133     # delete from items table
2134     $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
2135     $sth->execute($itemnum);
2136     return undef;
2137 }
2138
2139 =head2 _marc_from_item_hash
2140
2141   my $item_marc = _marc_from_item_hash($item, $frameworkcode[, $unlinked_item_subfields]);
2142
2143 Given an item hash representing a complete item record,
2144 create a C<MARC::Record> object containing an embedded
2145 tag representing that item.
2146
2147 The third, optional parameter C<$unlinked_item_subfields> is
2148 an arrayref of subfields (not mapped to C<items> fields per the
2149 framework) to be added to the MARC representation
2150 of the item.
2151
2152 =cut
2153
2154 sub _marc_from_item_hash {
2155     my $item = shift;
2156     my $frameworkcode = shift;
2157     my $unlinked_item_subfields;
2158     if (@_) {
2159         $unlinked_item_subfields = shift;
2160     }
2161    
2162     # Tack on 'items.' prefix to column names so lookup from MARC frameworks will work
2163     # Also, don't emit a subfield if the underlying field is blank.
2164     my $mungeditem = { map {  (defined($item->{$_}) and $item->{$_} ne '') ? 
2165                                 (/^items\./ ? ($_ => $item->{$_}) : ("items.$_" => $item->{$_})) 
2166                                 : ()  } keys %{ $item } }; 
2167
2168     my $item_marc = MARC::Record->new();
2169     foreach my $item_field ( keys %{$mungeditem} ) {
2170         my ( $tag, $subfield ) = GetMarcFromKohaField( $item_field, $frameworkcode );
2171         next unless defined $tag and defined $subfield;    # skip if not mapped to MARC field
2172         my @values = split(/\s?\|\s?/, $mungeditem->{$item_field}, -1);
2173         foreach my $value (@values){
2174             if ( my $field = $item_marc->field($tag) ) {
2175                     $field->add_subfields( $subfield => $value );
2176             } else {
2177                 my $add_subfields = [];
2178                 if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
2179                     $add_subfields = $unlinked_item_subfields;
2180             }
2181             $item_marc->add_fields( $tag, " ", " ", $subfield => $value, @$add_subfields );
2182             }
2183         }
2184     }
2185
2186     return $item_marc;
2187 }
2188
2189 =head2 _add_item_field_to_biblio
2190
2191   _add_item_field_to_biblio($item_marc, $biblionumber, $frameworkcode);
2192
2193 Adds the fields from a MARC record containing the
2194 representation of a Koha item record to the MARC
2195 biblio record.  The input C<$item_marc> record
2196 is expect to contain just one field, the embedded
2197 item information field.
2198
2199 =cut
2200
2201 sub _add_item_field_to_biblio {
2202     my ($item_marc, $biblionumber, $frameworkcode) = @_;
2203
2204     my $biblio_marc = GetMarcBiblio($biblionumber);
2205     foreach my $field ($item_marc->fields()) {
2206         $biblio_marc->append_fields($field);
2207     }
2208
2209     ModBiblioMarc($biblio_marc, $biblionumber, $frameworkcode);
2210 }
2211
2212 =head2 _replace_item_field_in_biblio
2213
2214   &_replace_item_field_in_biblio($item_marc, $biblionumber, $itemnumber, $frameworkcode)
2215
2216 Given a MARC::Record C<$item_marc> containing one tag with the MARC 
2217 representation of the item, examine the biblio MARC
2218 for the corresponding tag for that item and 
2219 replace it with the tag from C<$item_marc>.
2220
2221 =cut
2222
2223 sub _replace_item_field_in_biblio {
2224     my ($ItemRecord, $biblionumber, $itemnumber, $frameworkcode) = @_;
2225     my $dbh = C4::Context->dbh;
2226     
2227     # get complete MARC record & replace the item field by the new one
2228     my $completeRecord = GetMarcBiblio($biblionumber);
2229     my ($itemtag,$itemsubfield) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
2230     my $itemField = $ItemRecord->field($itemtag);
2231     my @items = $completeRecord->field($itemtag);
2232     my $found = 0;
2233     foreach (@items) {
2234         if ($_->subfield($itemsubfield) eq $itemnumber) {
2235             $_->replace_with($itemField);
2236             $found = 1;
2237         }
2238     }
2239   
2240     unless ($found) { 
2241         # If we haven't found the matching field,
2242         # just add it.  However, this means that
2243         # there is likely a bug.
2244         $completeRecord->append_fields($itemField);
2245     }
2246
2247     # save the record
2248     ModBiblioMarc($completeRecord, $biblionumber, $frameworkcode);
2249 }
2250
2251 =head2 _repack_item_errors
2252
2253 Add an error message hash generated by C<CheckItemPreSave>
2254 to a list of errors.
2255
2256 =cut
2257
2258 sub _repack_item_errors {
2259     my $item_sequence_num = shift;
2260     my $item_ref = shift;
2261     my $error_ref = shift;
2262
2263     my @repacked_errors = ();
2264
2265     foreach my $error_code (sort keys %{ $error_ref }) {
2266         my $repacked_error = {};
2267         $repacked_error->{'item_sequence'} = $item_sequence_num;
2268         $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ? $item_ref->{'barcode'} : '';
2269         $repacked_error->{'error_code'} = $error_code;
2270         $repacked_error->{'error_information'} = $error_ref->{$error_code};
2271         push @repacked_errors, $repacked_error;
2272     } 
2273
2274     return @repacked_errors;
2275 }
2276
2277 =head2 _get_unlinked_item_subfields
2278
2279   my $unlinked_item_subfields = _get_unlinked_item_subfields($original_item_marc, $frameworkcode);
2280
2281 =cut
2282
2283 sub _get_unlinked_item_subfields {
2284     my $original_item_marc = shift;
2285     my $frameworkcode = shift;
2286
2287     my $marcstructure = GetMarcStructure(1, $frameworkcode);
2288
2289     # assume that this record has only one field, and that that
2290     # field contains only the item information
2291     my $subfields = [];
2292     my @fields = $original_item_marc->fields();
2293     if ($#fields > -1) {
2294         my $field = $fields[0];
2295             my $tag = $field->tag();
2296         foreach my $subfield ($field->subfields()) {
2297             if (defined $subfield->[1] and
2298                 $subfield->[1] ne '' and
2299                 !$marcstructure->{$tag}->{$subfield->[0]}->{'kohafield'}) {
2300                 push @$subfields, $subfield->[0] => $subfield->[1];
2301             }
2302         }
2303     }
2304     return $subfields;
2305 }
2306
2307 =head2 _get_unlinked_subfields_xml
2308
2309   my $unlinked_subfields_xml = _get_unlinked_subfields_xml($unlinked_item_subfields);
2310
2311 =cut
2312
2313 sub _get_unlinked_subfields_xml {
2314     my $unlinked_item_subfields = shift;
2315
2316     my $xml;
2317     if (defined $unlinked_item_subfields and ref($unlinked_item_subfields) eq 'ARRAY' and $#$unlinked_item_subfields > -1) {
2318         my $marc = MARC::Record->new();
2319         # use of tag 999 is arbitrary, and doesn't need to match the item tag
2320         # used in the framework
2321         $marc->append_fields(MARC::Field->new('999', ' ', ' ', @$unlinked_item_subfields));
2322         $marc->encoding("UTF-8");    
2323         $xml = $marc->as_xml("USMARC");
2324     }
2325
2326     return $xml;
2327 }
2328
2329 =head2 _parse_unlinked_item_subfields_from_xml
2330
2331   my $unlinked_item_subfields = _parse_unlinked_item_subfields_from_xml($whole_item->{'more_subfields_xml'}):
2332
2333 =cut
2334
2335 sub  _parse_unlinked_item_subfields_from_xml {
2336     my $xml = shift;
2337
2338     return unless defined $xml and $xml ne "";
2339     my $marc = MARC::Record->new_from_xml(StripNonXmlChars($xml),'UTF-8');
2340     my $unlinked_subfields = [];
2341     my @fields = $marc->fields();
2342     if ($#fields > -1) {
2343         foreach my $subfield ($fields[0]->subfields()) {
2344             push @$unlinked_subfields, $subfield->[0] => $subfield->[1];
2345         }
2346     }
2347     return $unlinked_subfields;
2348 }
2349
2350 1;