From 3863359f66f55c3340f9e5ba5dda524bd20a172e Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Thu, 13 Mar 2014 13:24:28 +1300 Subject: [PATCH] Working AcceptItem now --- lib/NCIP/Handler/AcceptItem.pm | 60 +++++++++-- lib/NCIP/ILS/Koha.pm | 118 ++++++++++++++++++++-- templates/includes/LookupUserResponse.inc | 8 +- templates/problem.tt | 2 + 4 files changed, 173 insertions(+), 15 deletions(-) diff --git a/lib/NCIP/Handler/AcceptItem.pm b/lib/NCIP/Handler/AcceptItem.pm index e1ea4eb..b739fb5 100644 --- a/lib/NCIP/Handler/AcceptItem.pm +++ b/lib/NCIP/Handler/AcceptItem.pm @@ -23,20 +23,64 @@ sub handle { my $self = shift; my $xmldoc = shift; if ($xmldoc) { - my $root = $xmldoc->documentElement(); - my $xpc = $self->xpc(); - my $itemid = - $xpc->findnodes( '//ns:ItemId/ItemIdentifierValue', $root ); + my $root = $xmldoc->documentElement(); + my $xpc = $self->xpc(); + my $itemid = $xpc->find( '//ns:ItemIdentifierValue', $root ); my ($action) = $xpc->findnodes( '//ns:RequestedActionType', $root ); my ($request) = $xpc->findnodes( '//ns:RequestId', $root ); my $requestagency = $xpc->find( 'ns:AgencyId', $request ); - my $requestid = $xpc->find( '//ns:RequestIdentifierValue', $request ); + my $requestid = $xpc->find( '//ns:RequestIdentifierValue', $request ); + my $borrowerid = $xpc->find( '//ns:UserIdentifierValue', $root ); + + if ($action) { + $action = $action->textContent(); + } + + my $iteminfo = $xpc->find( '//ns:ItemOptionalFields', $root ); + my $itemdata = {}; + + if ( $iteminfo->[0] ) { + +# populate a hashref with bibliographic data, we need this to create an item +# (this could be moved up to Handler.pm eventually as CreateItem will need this also) + my $bibliographic = + $xpc->find( '//ns:BibliographicDescription', $iteminfo->[0] ); + my $title = $xpc->find( '//ns:Title', $bibliographic->[0] ); + if ( $title->[0] ) { + $itemdata->{title} = $title->[0]->textContent(); + } + my $author = $xpc->find( '//ns:Author', $bibliographic->[0] ); + if ( $author->[0] ) { + $itemdata->{author} = $author->[0]->textContent(); + } + my $date = + $xpc->find( '//ns:PublicationDate', $bibliographic->[0] ); + if ( $date->[0] ) { + $itemdata->{publicationdate} = $date->[0]->textContent(); + } + my $publisher = $xpc->find( '//ns:Publisher', $bibliographic->[0] ); + if ( $publisher->[0] ) { + $itemdata->{publisher} = $publisher->[0]->textContent(); + } + my $medium = $xpc->find( '//ns:Mediumtype', $bibliographic->[0] ); + if ( $medium->[0] ) { + $itemdata->{mediumtype} = $medium->[0]->textContent(); + } + } # accept the item - my $accepted = $self->ils->acceptitem($itemid); + my $create = 0; + my ( $from, $to ) = $self->get_agencies($xmldoc); + +# Autographics workflow is for an accept item to create the item then do what is in $action + if ( $from->[0]->textContent() =~ /CPomAG/ ) { + $create = 1; + } + my $accepted = + $self->ils->acceptitem( $itemid, $borrowerid, $action, $create, + $itemdata ); my $output; my $vars; - my ( $from, $to ) = $self->get_agencies($xmldoc); # we switch these for the templates # because we are responding, to becomes from, from becomes to @@ -55,7 +99,7 @@ sub handle { my $elements = $self->get_user_elements($xmldoc); $vars->{'requestagency'} = $requestagency; $vars->{'requestid'} = $requestid; - + $vars->{'newbarcode'} = $accepted->{'newbarcode'} || $itemid; $vars->{'elements'} = $elements; $vars->{'accept'} = $accepted; $output = $self->render_output( 'response.tt', $vars ); diff --git a/lib/NCIP/ILS/Koha.pm b/lib/NCIP/ILS/Koha.pm index b448963..236437b 100644 --- a/lib/NCIP/ILS/Koha.pm +++ b/lib/NCIP/ILS/Koha.pm @@ -19,11 +19,17 @@ package NCIP::ILS::Koha; use Modern::Perl; use Object::Tiny qw{ name }; +use MARC::Record; +use MARC::Field; + use C4::Members qw{ GetMemberDetails }; use C4::Circulation qw { AddReturn CanBookBeIssued AddIssue }; use C4::Context; use C4::Items qw { GetItem }; use C4::Reserves qw {CanBookBeReserved AddReserve GetReservesFromItemnumber}; +use C4::Biblio qw {AddBiblio GetMarcFromKohaField}; +use C4::Barcodes::ValueBuilder; +use C4::Items qw{AddItem}; sub itemdata { my $self = shift; @@ -148,17 +154,116 @@ sub request { } sub acceptitem { - my $self = shift; - my $barcode = shift; + my $self = shift || die "Not called as a method, we must bail out"; + my $barcode = shift || die "No barcode passed can not continue"; + my $user = shift; + my $action = shift; + my $create = shift; + my $iteminfo = shift; my $result; + $self->userenv(); # set userenvironment + my ( $biblionumber, $biblioitemnumber ); + if ($create) { + my $record; + my $frameworkcode = 'FA'; # we should get this from config + warn "Create yo"; + warn $iteminfo->{title}; + + # we must make the item first + # Autographics workflow is to make the item each time + if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) { + + # TODO + } + elsif ( C4::Context->preference('marcflavour') eq 'NORMARC' ) { + + #TODO + } + else { + # MARC21 + # create a marc record + $record = MARC::Record->new(); + $record->leader(' nac 22 1u 4500'); + $record->insert_fields_ordered( + MARC::Field->new( '100', '1', '0', 'a' => $iteminfo->{author} ), + MARC::Field->new( '245', '1', '0', 'a' => $iteminfo->{title} ), + MARC::Field->new( + '260', '1', '0', + 'b' => $iteminfo->{publisher}, + 'c' => $iteminfo->{publicationdate} + ), + MARC::Field->new( + '942', '1', '0', 'c' => $iteminfo->{mediumtype} + ) + ); + + } + + ( $biblionumber, $biblioitemnumber ) = + AddBiblio( $record, $frameworkcode ); + my $itemnumber; + + my %args; + ( $args{tag}, $args{subfield} ) = + GetMarcFromKohaField( "items.barcode", '' ); + my ( $nextnum, $scr ) = + C4::Barcodes::ValueBuilder::incremental::get_barcode( \%args ); + my $item = { 'barcode' => $nextnum }; + ( $biblionumber, $biblioitemnumber, $itemnumber ) = + AddItem( $item, $biblionumber ); + $barcode = $nextnum; + } + # find hold and get branch for that, check in there my $itemdata = GetItem( undef, $barcode ); + warn $itemdata->{'itemnumber'}; + my ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $wait ) = GetReservesFromItemnumber( $itemdata->{'itemnumber'} ); - unless ($reserve_id) { - $result = { success => 0, messages => { NO_HOLD => 1 } }; - return $result; + warn "barcode $barcode"; + # now we have to check the requested action + if ( $action =~ /^Hold For Pickup And Notify/ ) { + unless ($reserve_id) { + $branchcode = 'AS'; # set this properly + # no reserve, place one + if ($user) { + my $borrower = GetMemberDetails( undef, $user ); + if ($borrower) { + AddReserve( + $branchcode, + $borrower->{'borrowernumber'}, + $biblionumber, + 'a', + [$biblioitemnumber], + 1, + undef, + undef, + 'Placed By ILL', + '', + $itemdata->{'itemnumber'}, + undef + ); + } + + else { + $result = + { success => 0, messages => { NO_BORROWER => 1 } }; + return $result; + } + } + else { + $result = + { success => 0, messages => { NO_HOLD_BORROWER => 1 } }; + return $result; + } + } + } + else { + unless ($reserve_id) { + $result = { success => 0, messages => { NO_HOLD => 1 } }; + return $result; + } } my ( $success, $messages, $issue, $borrower ) = AddReturn( $barcode, $branchcode, undef, undef ); @@ -171,7 +276,8 @@ sub acceptitem { success => $success, messages => $messages, iteminformation => $issue, - borrower => $borrower + borrower => $borrower, + newbarcode => $barcode }; return $result; diff --git a/templates/includes/LookupUserResponse.inc b/templates/includes/LookupUserResponse.inc index 1e54eff..fbcad05 100644 --- a/templates/includes/LookupUserResponse.inc +++ b/templates/includes/LookupUserResponse.inc @@ -1,4 +1,7 @@ -[% user.userdata.cardnumber %] + + +[% user.userdata.cardnumber %] + [%# LoanedItemsCount LoanedItem @@ -8,6 +11,8 @@ RequestedItem [% FOREACH element IN elements %] [% IF element.textContent == 'User Address Information' %] + + Primary Address [% user.userdata.streetnumber %] [% user.userdata.address %] @@ -28,6 +33,7 @@ RequestedItem [% END %] + [% END %] [% IF element.textContent == 'Name Information' %] diff --git a/templates/problem.tt b/templates/problem.tt index c8d153b..0c8af19 100644 --- a/templates/problem.tt +++ b/templates/problem.tt @@ -17,6 +17,8 @@ This item is already on loan to this borrower [% CASE 'NO_HOLD' %] There is no hold on this item + [% CASE 'NO_HOLD_BORROWER' %] + We can not place a hold, we have no borrower information [% END %] -- 2.43.2