X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=cataloguing%2Fadditem.pl;h=20007dae1340ab69892bfa3bc8827644e4d03123;hb=58e76f605c7d991ffd78c1b6c732bb568950e956;hp=3d32130e09208d4b2f1c8f216049e80c62bbaea1;hpb=38685bdf906dd14cb5eaa3bb1db65b004e01d556;p=koha.git diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index 3d32130e09..20007dae13 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -23,9 +23,12 @@ use strict; use C4::Auth; use C4::Output; use C4::Biblio; +use C4::Items; use C4::Context; use C4::Koha; # XXX subfield_is_koha_internal_p -use Date::Calc qw(Today); +use C4::Branch; # XXX subfield_is_koha_internal_p +use C4::ClassSource; +use C4::Dates; use MARC::File::XML; @@ -55,17 +58,37 @@ sub get_item_from_barcode { return($result); } +sub set_item_default_location { + my $itemnumber = shift; + if ( C4::Context->preference('NewItemsDefaultLocation') ) { + my $item = GetItem( $itemnumber ); + $item->{'permanent_location'} = $item->{'location'}; + $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation'); + ModItem( $item, undef, $itemnumber); + } +} + my $input = new CGI; my $dbh = C4::Context->dbh; -my $error = $input->param('error'); +my $error = $input->param('error'); my $biblionumber = $input->param('biblionumber'); -my $itemnumber = $input->param('itemnumber'); -warn Data::Dumper::Dumper($input->param()); -my $op = $input->param('op'); +my $itemnumber = $input->param('itemnumber'); +my $op = $input->param('op'); + +my ($template, $loggedinuser, $cookie) + = get_template_and_user({template_name => "cataloguing/additem.tmpl", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => {editcatalogue => 1}, + debug => 1, + }); -# find itemtype my $frameworkcode = &GetFrameworkCode($biblionumber); +my $today_iso = C4::Dates->today('iso'); +$template->param(today_iso => $today_iso); + my $tagslib = &GetMarcStructure(1,$frameworkcode); my $record = GetMarcBiblio($biblionumber); my $oldrecord = TransformMarcToKoha($dbh,$record); @@ -76,16 +99,25 @@ my @errors; # store errors found while checking data BEFORE saving item. if ($op eq "additem") { #------------------------------------------------------------------------------- # rebuild - my @tags = $input->param('tag'); + my @tags = $input->param('tag'); my @subfields = $input->param('subfield'); - my @values = $input->param('field_value'); + my @values = $input->param('field_value'); # build indicator hash. - my @ind_tag = $input->param('ind_tag'); + my @ind_tag = $input->param('ind_tag'); my @indicator = $input->param('indicator'); my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM'); - my $record=MARC::Record::new_from_xml($xml, 'UTF-8'); - # if autoBarcode is ON, calculate barcode... - if (C4::Context->preference('autoBarcode')) { + my $record = MARC::Record::new_from_xml($xml, 'UTF-8'); + + # type of add + my $add_submit = $input->param('add_submit'); + my $add_duplicate_submit = $input->param('add_duplicate_submit'); + my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit'); + my $number_of_copies = $input->param('number_of_copies'); + + # if autoBarcode is set to 'incremental', calculate barcode... + # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code + # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript + if (C4::Context->preference('autoBarcode') eq 'incremental') { my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode); unless ($record->field($tagfield)->subfield($tagsubfield)) { my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items"); @@ -99,59 +131,156 @@ if ($op eq "additem") { $record->insert_fields_ordered($fieldItem); } } -# check for item barcode # being unique + my $addedolditem = TransformMarcToKoha($dbh,$record); - my $exists = get_item_from_barcode($addedolditem->{'barcode'}); - push @errors,"barcode_not_unique" if($exists); - # if barcode exists, don't create, but report The problem. - my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exists); - if ($exists) { - $nextop = "additem"; + + # If we have to add or add & duplicate, we add the item + if ($add_submit || $add_duplicate_submit) { + # check for item barcode # being unique + my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'}); + push @errors,"barcode_not_unique" if($exist_itemnumber); + # if barcode exists, don't create, but report The problem. + unless ($exist_itemnumber) { + my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber); + set_item_default_location($oldbibitemnum); + } + $nextop = "additem"; + if ($exist_itemnumber) { + $itemrecord = $record; + } + } + + # If we have to add & duplicate + if ($add_duplicate_submit) { + + # We try to get the next barcode + use C4::Barcodes; + my $barcodeobj = C4::Barcodes->new; + my $barcodevalue = $barcodeobj->next_value($addedolditem->{'barcode'}) if $barcodeobj; + my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode); + if ($record->field($tagfield)->subfield($tagsubfield)) { + # If we got the next codebar value, we put it in the record + if ($barcodevalue) { + $record->field($tagfield)->update($tagsubfield => $barcodevalue); + # If not, we delete the recently inserted barcode from the record (so the user can input a barcode himself) + } else { + $record->field($tagfield)->update($tagsubfield => ''); + } + } $itemrecord = $record; - } else { - $nextop = "additem"; } + + # If we have to add multiple copies + if ($add_multiple_copies_submit) { + + use C4::Barcodes; + my $barcodeobj = C4::Barcodes->new; + my $oldbarcode = $addedolditem->{'barcode'}; + my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode); + + # If there is a barcode and we can't find him new values, we can't add multiple copies + my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj; + if ($oldbarcode && !$testbarcode) { + + push @errors, "no_next_barcode"; + $itemrecord = $record; + + } else { + # We add each item + + # For the first iteration + my $barcodevalue = $oldbarcode; + my $exist_itemnumber; + + + for (my $i = 0; $i < $number_of_copies;) { + + # If there is a barcode + if ($barcodevalue) { + + # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists) + $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber); + + # Putting it into the record + if ($barcodevalue) { + $record->field($tagfield)->update($tagsubfield => $barcodevalue); + } + + # Checking if the barcode already exists + $exist_itemnumber = get_item_from_barcode($barcodevalue); + } + + # Adding the item + if (!$exist_itemnumber) { + my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber); + set_item_default_location($oldbibitemnum); + + # We count the item only if it was really added + # That way, all items are added, even if there was some already existing barcodes + # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode + $i++; + } + + # Preparing the next iteration + $oldbarcode = $barcodevalue; + } + undef($itemrecord); + } + } + + #------------------------------------------------------------------------------- } elsif ($op eq "edititem") { #------------------------------------------------------------------------------- # retrieve item if exist => then, it's a modif - $itemrecord = GetMarcItem($biblionumber,$itemnumber); - $nextop="saveitem"; + $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber); + $nextop = "saveitem"; #------------------------------------------------------------------------------- } elsif ($op eq "delitem") { #------------------------------------------------------------------------------- # check that there is no issue on this item before deletion. - my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?"); - $sth->execute($itemnumber); - my $onloan=$sth->fetchrow; - push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well - if ($onloan){ - $nextop="additem"; - } else { - &DelItem($dbh,$biblionumber,$itemnumber); + $error = &DelItemCheck($dbh,$biblionumber,$itemnumber); + if($error == 1){ print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode"); - #$nextop="additem"; + }else{ + push @errors,$error; + $nextop="additem"; } #------------------------------------------------------------------------------- +} elsif ($op eq "delallitems") { +#------------------------------------------------------------------------------- + my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber); + foreach my $biblioitem (@biblioitems){ + my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber}); + + foreach my $item (@$items){ + &DelItem($dbh,$biblionumber,$item->{itemnumber}); + } + } +#------------------------------------------------------------------------------- } elsif ($op eq "saveitem") { #------------------------------------------------------------------------------- # rebuild - my @tags = $input->param('tag'); + my @tags = $input->param('tag'); my @subfields = $input->param('subfield'); - my @values = $input->param('field_value'); + my @values = $input->param('field_value'); # build indicator hash. - my @ind_tag = $input->param('ind_tag'); + my @ind_tag = $input->param('ind_tag'); my @indicator = $input->param('indicator'); - warn "tags :@tags" ; - warn "subfields :@subfields" ; - warn "values :@values" ; -# my $itemnumber = $input->param('itemnumber'); + # my $itemnumber = $input->param('itemnumber'); my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM'); - my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8'); -# MARC::Record builded => now, record in DB -# warn "R: ".$record->as_formatted; - my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0); - $itemnumber=""; + my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8'); + # MARC::Record builded => now, record in DB + # warn "R: ".$record->as_formatted; + # check that the barcode don't exist already + my $addedolditem = TransformMarcToKoha($dbh,$itemtosave); + my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'}); + if ($exist_itemnumber && $exist_itemnumber != $itemnumber) { + push @errors,"barcode_not_unique"; + } else { + my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber); + $itemnumber=""; + } $nextop="additem"; } @@ -159,14 +288,6 @@ if ($op eq "additem") { #------------------------------------------------------------------------------- # build screen with existing items. and "new" one #------------------------------------------------------------------------------- -my ($template, $loggedinuser, $cookie) - = get_template_and_user({template_name => "cataloguing/additem.tmpl", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => {editcatalogue => 1}, - debug => 1, - }); # now, build existiing item list my $temp = GetMarcBiblio( $biblionumber ); @@ -175,24 +296,27 @@ my @fields = $temp->fields(); my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code my @big_array; #---- finds where items.itemnumber is stored -my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode); -my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode); +my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode); +my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode); foreach my $field (@fields) { next if ($field->tag()<10); - my @subf=$field->subfields; + my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context my %this_row; # loop through each subfield for my $i (0..$#subf) { next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 - && ($field->tag() ne $itemtagfield - && $subf[$i][0] ne $itemtagsubfield)); + && ($field->tag() ne $itemtagfield + && $subf[$i][0] ne $itemtagsubfield)); $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10); + if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10) { + $this_row{$subf[$i][0]}=GetAuthorisedValueDesc( $field->tag(), + $subf[$i][0], $subf[$i][1], '', $tagslib) + || $subf[$i][1]; + } - $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10); - - if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) { + if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) { #verifying rights my $userenv = C4::Context->userenv(); unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){ @@ -205,12 +329,7 @@ foreach my $field (@fields) { push(@big_array, \%this_row); } } -#fill big_row with missing data -foreach my $subfield_code (keys(%witness)) { - for (my $i=0;$i<=$#big_array;$i++) { - $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code}); - } -} + my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode); @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array; @@ -218,17 +337,13 @@ my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbran # First, the existing items for display my @item_value_loop; my @header_value_loop; -for (my $i=0;$i<=$#big_array; $i++) { - my $items_data; - foreach my $subfield_code (sort keys(%witness)) { - $items_data .="