From 7d897282056382a3630275b26a931f3bffbfce68 Mon Sep 17 00:00:00 2001 From: Srdjan Jankovic Date: Wed, 14 Sep 2011 15:45:40 +1200 Subject: [PATCH] bug_3966: Update items on receive Changed AddItem() and ModItem() interface so now they return item record. AddItemFromMarc() looks for itemnumber, and calls ModItem() if one is found --- C4/Biblio.pm | 6 +- C4/ImportBatch.pm | 4 +- C4/Items.pm | 19 +++-- acqui/addorder.pl | 5 +- acqui/addorderiso2709.pl | 4 +- acqui/finishreceive.pl | 93 +++++++++++--------- acqui/orderreceive.pl | 63 ++++++------- cataloguing/additem.pl | 10 +- .../prog/en/modules/acqui/orderreceive.tt | 14 ++-- t/db_dependent/lib/KohaTest.pm | 16 ++-- t/db_dependent/lib/KohaTest/Biblio/ModBiblio.pm | 6 +- tools/batchMod.pl | 2 +- 12 files changed, 124 insertions(+), 118 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 96baaef..95759ec 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2343,7 +2343,7 @@ sub TransformMarcToKohaOneField { =head2 PrepareItemrecordDisplay - PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber,$frameworkcode); + PrepareItemrecordDisplay($bibnum,$itemumber,$defaultvalues,$frameworkcode); Returns a hash with all the fields for Display a given item data in a template @@ -2522,14 +2522,14 @@ sub PrepareItemrecordDisplay { $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms? my $index_subfield = int(rand(1000000)); $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield; - $subfield_data{marc_value} = qq[ ... $javascript]; } else { warn "Plugin Failed: $plugin"; - $subfield_data{marc_value} = qq(); # supply default input form + $subfield_data{marc_value} = qq(); # supply default input form } } elsif ( $tag eq '' ) { # it's an hidden field diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index b6db406..a121d08 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -547,10 +547,10 @@ sub BatchCommitItems { $updsth->execute(); $num_items_errored++; } else { - my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItemFromMarc($item_marc, $biblionumber); + my $item = AddItemFromMarc($item_marc, $biblionumber); my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?"); $updsth->bind_param(1, 'imported'); - $updsth->bind_param(2, $itemnumber); + $updsth->bind_param(2, $item->{itemnumber}); $updsth->bind_param(3, $row->{'import_items_id'}); $updsth->execute(); $updsth->finish(); diff --git a/C4/Items.pm b/C4/Items.pm index bc36dd1..e8e590e 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -190,11 +190,11 @@ sub CartToShelf { =head2 AddItemFromMarc - my ($biblionumber, $biblioitemnumber, $itemnumber) - = AddItemFromMarc($source_item_marc, $biblionumber); + my $item = AddItemFromMarc($source_item_marc, $biblionumber); Given a MARC::Record object containing an embedded item -record and a biblionumber, create a new item record. +record and a biblionumber, create a new item record or update +existing one. =cut @@ -210,13 +210,15 @@ sub AddItemFromMarc { $localitemmarc->append_fields($source_item_marc->field($itemtag)); my $item = &TransformMarcToKoha( $dbh, $localitemmarc, $frameworkcode ,'items'); my $unlinked_item_subfields = _get_unlinked_item_subfields($localitemmarc, $frameworkcode); + if (my $itemnumber = $item->{itemnumber}) { + return ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields); + } return AddItem($item, $biblionumber, $dbh, $frameworkcode, $unlinked_item_subfields); } =head2 AddItem - my ($biblionumber, $biblioitemnumber, $itemnumber) - = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $unlinked_item_subfields]); + my $item = AddItem($item, $biblionumber[, $dbh, $frameworkcode, $unlinked_item_subfields]); Given a hash containing item column names as keys, create a new Koha item record. @@ -268,7 +270,7 @@ sub AddItem { logaction("CATALOGUING", "ADD", $itemnumber, "item") if C4::Context->preference("CataloguingLog"); - return ($item->{biblionumber}, $item->{biblioitemnumber}, $itemnumber); + return $item; } =head2 AddItemBatchFromMarc @@ -445,13 +447,14 @@ sub ModItemFromMarc { $item->{$item_field} = $default_values_for_mod_from_marc{$item_field} unless (exists $item->{$item_field}); } my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode ); + $itemnumber ||= $item->{itemnumber}; return ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields); } =head2 ModItem - ModItem({ column => $newvalue }, $biblionumber, + my $item = ModItem({ column => $newvalue }, $biblionumber, $itemnumber[, $original_item_marc]); Change one or more columns in an item record and update @@ -512,6 +515,8 @@ sub ModItem { ModZebra( $biblionumber, "specialUpdate", "biblioserver", undef, undef ); logaction("CATALOGUING", "MODIFY", $itemnumber, Dumper($item)) if C4::Context->preference("CataloguingLog"); + + return $item; } =head2 ModItemTransfer diff --git a/acqui/addorder.pl b/acqui/addorder.pl index e321d2c..5352535 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -260,9 +260,8 @@ if ( $orderinfo->{quantity} ne '0' ) { $itemhash{$item}->{'indicator'}, 'ITEM'); my $record=MARC::Record::new_from_xml($xml, 'UTF-8'); - my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber}); - NewOrderItem($itemnumber, $$orderinfo{ordernumber}); - + my $item = AddItemFromMarc($record,$$orderinfo{biblionumber}); + NewOrderItem($item->{itemnumber}, $$orderinfo{ordernumber}); } } diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl index 0b9d860..ca94dbc 100755 --- a/acqui/addorderiso2709.pl +++ b/acqui/addorderiso2709.pl @@ -246,8 +246,8 @@ if ($op eq ""){ my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values, \@ind_tag, \@indicator ); my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' ); for (my $qtyloop=1;$qtyloop <=$quantity;$qtyloop++) { - my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber ); - NewOrderItem( $itemnumber, $ordernumber ); + my $item = AddItemFromMarc( $record, $biblionumber ); + NewOrderItem( $item->{itemnumber}, $ordernumber ); } } } diff --git a/acqui/finishreceive.pl b/acqui/finishreceive.pl index 71b13d6..7327d18 100755 --- a/acqui/finishreceive.pl +++ b/acqui/finishreceive.pl @@ -50,7 +50,7 @@ my $gst=$input->param('gst'); my $freight=$input->param('freight'); my $supplierid = $input->param('supplierid'); my $cnt=0; -my $error_url_str; +my $error_url_str = ''; my $ecost = $input->param('ecost'); my $note = $input->param("note"); @@ -73,45 +73,56 @@ if ( any { $order->{$_} ne $tplorder{$_} } qw(quantity quantityreceived notes rr } #need old recievedate if we update the order, parcel.pl only shows the right parcel this way FIXME -if ($quantityrec > $origquantityrec ) { - # now, add items if applicable - if (C4::Context->preference('AcqCreateItem') eq 'receiving') { - my @tags = $input->param('tag'); - my @subfields = $input->param('subfield'); - my @field_values = $input->param('field_value'); - my @serials = $input->param('serial'); - my @itemid = $input->param('itemid'); - my @ind_tag = $input->param('ind_tag'); - my @indicator = $input->param('indicator'); - #Rebuilding ALL the data for items into a hash - # parting them on $itemid. - my %itemhash; - my $countdistinct; - my $range=scalar(@itemid); - for (my $i=0; $i<$range; $i++){ - unless ($itemhash{$itemid[$i]}){ - $countdistinct++; - } - push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i]; - push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i]; - push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i]; - push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i]; - push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i]; - } - foreach my $item (keys %itemhash){ - my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'}, - $itemhash{$item}->{'subfields'}, - $itemhash{$item}->{'field_values'}, - $itemhash{$item}->{'ind_tag'}, - $itemhash{$item}->{'indicator'},'ITEM'); - my $record=MARC::Record::new_from_xml($xml, 'UTF-8'); - my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber); - } + +# now, add or update items if applicable +my @tags = $input->param('tag'); +my @subfields = $input->param('subfield'); +my @field_values = $input->param('field_value'); +my @serials = $input->param('serial'); +my @itemid = $input->param('itemid'); +my @ind_tag = $input->param('ind_tag'); +my @indicator = $input->param('indicator'); +#Rebuilding ALL the data for items into a hash +# parting them on $itemid. +my %itemhash; +my $countdistinct; +my $range=scalar(@itemid); +for (my $i=0; $i<$range; $i++){ + unless ($itemhash{$itemid[$i]}){ + $countdistinct++; + } + push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i]; + push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i]; + push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i]; + push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i]; + push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i]; +} +my $error; +my %itemnumbers = map { $_ => 1 } GetItemnumbersFromOrder($ordernumber); +foreach my $item (keys %itemhash){ + my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'}, + $itemhash{$item}->{'subfields'}, + $itemhash{$item}->{'field_values'}, + $itemhash{$item}->{'ind_tag'}, + $itemhash{$item}->{'indicator'},'ITEM'); + my $record=MARC::Record::new_from_xml($xml, 'UTF-8'); + my $item = AddItemFromMarc($record,$biblionumber); + my $itemnumber = $item ? $item->{itemnumber} : undef; + if ($itemnumber) { + NewOrderItem($itemnumber, $ordernumber) unless $itemnumbers{$itemnumber}; + } else { + $error++; + } +} + +if ($error) { + $error_url_str = "&error=Datbase+write+error"; +} +else { + if ($quantityrec != $origquantityrec ) { + # save the quantity received. + $datereceived = ModReceiveOrder($biblionumber,$ordernumber, $quantityrec ,$user,$unitprice,$invoiceno,$freight,$replacement,undef,$datereceived); } - - # save the quantity received. - if( $quantityrec > 0 ) { - $datereceived = ModReceiveOrder($biblionumber,$ordernumber, $quantityrec ,$user,$unitprice,$invoiceno,$freight,$replacement,undef,$datereceived); - } } - print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&datereceived=$datereceived$error_url_str"); + +print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&datereceived=$datereceived$error_url_str"); diff --git a/acqui/orderreceive.pl b/acqui/orderreceive.pl index 98ba544..ae57f8a 100755 --- a/acqui/orderreceive.pl +++ b/acqui/orderreceive.pl @@ -85,21 +85,13 @@ my $search = $input->param('receive'); my $invoice = $input->param('invoice'); my $freight = $input->param('freight'); my $datereceived = $input->param('datereceived'); - - $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new(); my $bookseller = GetBookSellerFromId($supplierid); my $input_gst = ($input->param('gst') eq '' ? undef : $input->param('gst')); my $gst= $input_gst // $bookseller->{gstrate} // C4::Context->preference("gist") // 0; my $results = SearchOrder($ordernumber,$search); - - my $count = scalar @$results; -my $order = GetOrder($ordernumber); - - -my $date = @$results[0]->{'entrydate'}; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { @@ -114,47 +106,50 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( # prepare the form for receiving if ( $count == 1 ) { - if (C4::Context->preference('AcqCreateItem') eq 'receiving') { + my $order = GetOrder($results->[0]->{ordernumber}); + my $biblionumber = $order->{biblionumber}; + + my @itemloop; + if ( my @itemnumbers = GetItemnumbersFromOrder($ordernumber) ) { + @itemloop = map PrepareItemrecordDisplay($biblionumber,$_), @itemnumbers; + } + elsif (C4::Context->preference('AcqCreateItem') eq 'receiving') { # prepare empty item form my $cell = PrepareItemrecordDisplay('','','','ACQ'); unless ($cell) { $cell = PrepareItemrecordDisplay('','','',''); $template->param('NoACQframework' => 1); } - my @itemloop; push @itemloop,$cell; - - $template->param(items => \@itemloop); } + + $template->param(items => \@itemloop) if @itemloop; - if ( @$results[0]->{'quantityreceived'} == 0 ) { - @$results[0]->{'quantityreceived'} = ''; - } - if ( @$results[0]->{'unitprice'} == 0 ) { - @$results[0]->{'unitprice'} = ''; + if ( $order->{'unitprice'} == 0 ) { + $order->{'unitprice'} = ''; } $template->param( count => 1, - biblionumber => @$results[0]->{'biblionumber'}, - ordernumber => @$results[0]->{'ordernumber'}, - biblioitemnumber => @$results[0]->{'biblioitemnumber'}, - supplierid => @$results[0]->{'booksellerid'}, + biblionumber => $biblionumber, + ordernumber => $order->{'ordernumber'}, + biblioitemnumber => $order->{'biblioitemnumber'}, + supplierid => $order->{'booksellerid'}, freight => $freight, gst => $gst, name => $bookseller->{'name'}, - date => format_date($date), - title => @$results[0]->{'title'}, - author => @$results[0]->{'author'}, - copyrightdate => @$results[0]->{'copyrightdate'}, - isbn => @$results[0]->{'isbn'}, - seriestitle => @$results[0]->{'seriestitle'}, - bookfund => @$results[0]->{'bookfundid'}, - quantity => @$results[0]->{'quantity'}, - quantityreceivedplus1 => @$results[0]->{'quantityreceived'} + 1, - quantityreceived => @$results[0]->{'quantityreceived'}, - rrp => @$results[0]->{'rrp'}, - ecost => @$results[0]->{'ecost'}, - unitprice => @$results[0]->{'unitprice'}, + date => format_date($order->{'entrydate'}), + title => $order->{'title'}, + author => $order->{'author'}, + copyrightdate => $order->{'copyrightdate'}, + isbn => $order->{'isbn'}, + seriestitle => $order->{'seriestitle'}, + bookfund => $order->{'bookfundid'}, + quantity => $order->{'quantity'}, + quantityreceived => scalar(@itemloop) || 1, + origquantityrec => $order->{'quantityreceived'}, + rrp => $order->{'rrp'}, + ecost => $order->{'ecost'}, + unitprice => $order->{'unitprice'}, invoice => $invoice, datereceived => $datereceived->output(), datereceived_iso => $datereceived->output('iso'), diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index 0db2d2d..dc0fc1d 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -340,8 +340,8 @@ if ($op eq "additem") { 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); + my $item = AddItemFromMarc($record,$biblionumber); + set_item_default_location($item->{biblioitemnumber}); } $nextop = "additem"; if ($exist_itemnumber) { @@ -407,8 +407,8 @@ if ($op eq "additem") { # Adding the item if (!$exist_itemnumber) { - my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber); - set_item_default_location($oldbibitemnum); + my $item = AddItemFromMarc($record,$biblionumber); + set_item_default_location($item->{biblioitemnumber}); # We count the item only if it was really added # That way, all items are added, even if there was some already existing barcodes @@ -498,7 +498,7 @@ if ($op eq "additem") { if ($exist_itemnumber && $exist_itemnumber != $itemnumber) { push @errors,"barcode_not_unique"; } else { - my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber); + ModItemFromMarc($itemtosave,$biblionumber,$itemnumber); $itemnumber=""; } $nextop="additem"; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt index 746155f..d42e898 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt @@ -42,6 +42,7 @@

No ACQ framework, using default. You should create a framework with code ACQ, the items framework would be used

[% END %] + [% itemid = 1 %] [% FOREACH item IN items %]
@@ -50,7 +51,7 @@ [% iteminformatio.marc_value %] - + @@ -72,8 +73,9 @@ - + + [% itemid = itemid + 1 %] [% END %] [% END %] @@ -101,14 +103,12 @@ [% IF ( quantityreceived ) %] [% IF ( edit ) %] - [% ELSE %] [% IF ( items ) %] - + [% ELSE %] - + [% END %] - [% END %] [% ELSE %] [% IF ( items ) %] @@ -116,8 +116,8 @@ [% ELSE %] [% END %] - [% END %] +
  • diff --git a/t/db_dependent/lib/KohaTest.pm b/t/db_dependent/lib/KohaTest.pm index 70c963d..77e91d1 100644 --- a/t/db_dependent/lib/KohaTest.pm +++ b/t/db_dependent/lib/KohaTest.pm @@ -572,16 +572,12 @@ sub add_biblios { ok( $biblionumber, "the biblionumber is $biblionumber" ); ok( $biblioitemnumber, "the biblioitemnumber is $biblioitemnumber" ); if ( $param{'add_items'} ) { - # my @iteminfo = AddItem( {}, $biblionumber ); - my @iteminfo = AddItemFromMarc( $marcrecord, $biblionumber ); - is( $iteminfo[0], $biblionumber, "biblionumber is $biblionumber" ); - is( $iteminfo[1], $biblioitemnumber, "biblioitemnumber is $biblioitemnumber" ); - ok( $iteminfo[2], "itemnumber is $iteminfo[2]" ); - push @{ $self->{'items'} }, - { biblionumber => $iteminfo[0], - biblioitemnumber => $iteminfo[1], - itemnumber => $iteminfo[2], - }; + # my $item = AddItem( {}, $biblionumber ); + my $item = AddItemFromMarc( $marcrecord, $biblionumber ); + is( $item->{biblionumber}, "biblionumber is $biblionumber" ); + is( $item->{biblioitemnumber}, "biblioitemnumber is $biblioitemnumber" ); + ok( $item->{itemnumber}, "itemnumber is $item->{itemnumber}" ); + push @{ $self->{'items'} }, $item; } push @{$self->{'biblios'}}, $biblionumber; } diff --git a/t/db_dependent/lib/KohaTest/Biblio/ModBiblio.pm b/t/db_dependent/lib/KohaTest/Biblio/ModBiblio.pm index 5b29ea8..8a83268 100644 --- a/t/db_dependent/lib/KohaTest/Biblio/ModBiblio.pm +++ b/t/db_dependent/lib/KohaTest/Biblio/ModBiblio.pm @@ -31,10 +31,10 @@ sub add_bib_to_modify : Test( startup => 3 ) { $self->{'bib_to_modify'} = $bibnum; # add an item - my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum); + my $item = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum); - cmp_ok($item_bibnum, '==', $bibnum, "new item is linked to correct biblionumber"); - cmp_ok($item_bibitemnum, '==', $bibitemnum, "new item is linked to correct biblioitemnumber"); + cmp_ok($item->{biblionumber}, '==', $bibnum, "new item is linked to correct biblionumber"); + cmp_ok($item->{biblioitemnumber}, '==', $bibitemnum, "new item is linked to correct biblioitemnumber"); $self->reindex_marc(); diff --git a/tools/batchMod.pl b/tools/batchMod.pl index 9d4431b..ebf9d6e 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -173,7 +173,7 @@ if ($op eq "action") { if ($values_to_modify || $values_to_blank) { my $localmarcitem = Item2Marc($itemdata); UpdateMarcWith( $marcitem, $localmarcitem ); - eval{ my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) }; + eval{ ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) }; } } $i++; -- 1.6.5