From eca1a64b21ab5419d2454eec222c1998de59d1ac Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Tue, 12 Nov 2019 08:45:51 +0000 Subject: [PATCH] Bug 23971: Add Acq action logging This patch adds logging for the following Acq actions: - Basket creation - Basket editing - Basket approval (via EDI) - Basket closure --- C4/Acquisition.pm | 92 +++++++++++++++++++++- Koha/EDI.pm | 4 +- acqui/basket.pl | 11 ++- acqui/basketgroup.pl | 3 +- acqui/basketheader.pl | 3 +- .../intranet-tmpl/prog/en/modules/tools/viewlog.tt | 4 +- 6 files changed, 107 insertions(+), 10 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 75429e7e22..a19d939390 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -19,6 +19,7 @@ package C4::Acquisition; use Modern::Perl; +use JSON qw(to_json); use Carp; use C4::Context; use C4::Debug; @@ -26,6 +27,7 @@ use C4::Suggestions; use C4::Biblio; use C4::Contract; use C4::Debug; +use C4::Log qw(logaction); use C4::Templates qw(gettemplate); use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Acquisition::Baskets; @@ -203,6 +205,23 @@ sub NewBasket { $basketbooksellernote ||= q{}; ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote, $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items ); + + # Log the basket creation + # We need the creation timestamp, so need to get the newly created basket + my $new_basket = GetBasket($basket); + my $bookseller = Koha::Acquisition::Booksellers->find( + $new_basket->{booksellerid} + ); + my $patron = Koha::Patrons->find( $authorisedby ); + my $infos = { + basketno => $basket, + basketname => $new_basket->{basketname}, + creationdate => $new_basket->{creationdate}, + vendorname => $bookseller->name, + creatorname => $patron->firstname . ' ' . $patron->surname + }; + logaction('ACQUISITIONS', 'ADD', $basket, to_json($infos)); + return $basket; } @@ -217,7 +236,7 @@ close a basket (becomes unmodifiable, except for receives) =cut sub CloseBasket { - my ($basketno) = @_; + my ($basketno, $edi_approval) = @_; my $dbh = C4::Context->dbh; $dbh->do('UPDATE aqbasket SET closedate=now() WHERE basketno=?', {}, $basketno ); @@ -225,6 +244,30 @@ sub CloseBasket { q{UPDATE aqorders SET orderstatus = 'ordered' WHERE basketno = ? AND orderstatus NOT IN ( 'complete', 'cancelled')}, {}, $basketno ); + + # Log the closure + my $basket = GetBasket($basketno); + my $bookseller = Koha::Acquisition::Booksellers->find( + $basket->{booksellerid} + ); + my ($day, $mon, $year) = (localtime->mday, localtime->mon, localtime->year); + $mon = sprintf '%02d', $mon + 1; + $day = sprintf '%02d', $day; + $year += 1900; + my $action = $edi_approval ? 'APPROVE_BASKET' : 'CLOSE_BASKET'; + my $infos = { + basketno => $basketno, + basketname => $basket->{basketname}, + closeddate => "$year-$mon-$day", + vendorname => $bookseller->name, + }; + logaction( + 'ACQUISITIONS', + $action, + $basketno, + to_json($infos) + ); + return; } @@ -548,6 +591,32 @@ sub ModBasket { my $sth = $dbh->prepare($query); $sth->execute(@params); + # Log the basket update + my $edited_basket = GetBasket($basketinfo->{'basketno'}); + my $bookseller = Koha::Acquisition::Booksellers->find( + $edited_basket->{booksellerid} + ); + my ($day, $mon, $year) = (localtime->mday, localtime->mon, localtime->year); + $mon = sprintf '%02d', $mon + 1; + $day = sprintf '%02d', $day; + $year += 1900; + my $infos = { + basketno => $basketinfo->{'basketno'}, + basketname => $edited_basket->{basketname}, + updatedate => "$year-$mon-$day", + vendorname => $bookseller->name, + }; + if ($basketinfo->{borrowernumber}) { + my $patron = Koha::Patrons->find( $basketinfo->{borrowernumber} ); + $infos->{editorname} = $patron->firstname . ' ' . $patron->surname; + } + logaction( + 'ACQUISITIONS', + 'MODIFY', + $basketinfo->{'basketno'}, + to_json($infos) + ); + return; } @@ -587,7 +656,7 @@ case the AcqCreateItem syspref takes precedence). =cut sub ModBasketHeader { - my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items) = @_; + my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items, $borrowernumber) = @_; $is_standing ||= 0; my $query = qq{ @@ -605,6 +674,25 @@ sub ModBasketHeader { my $sth2 = $dbh->prepare($query2); $sth2->execute($contractnumber,$basketno); } + + # Log the basket update + my $bookseller = Koha::Acquisition::Booksellers->find($booksellerid); + my ($day, $mon, $year) = (localtime->mday, localtime->mon, localtime->year); + $mon = sprintf '%02d', $mon + 1; + $day = sprintf '%02d', $day; + $year += 1900; + my $infos = { + basketno => $basketno, + basketname => $basketname, + updatedate => "$year-$mon-$day", + vendorname => $bookseller->name, + }; + if ($borrowernumber) { + my $patron = Koha::Patrons->find( $borrowernumber ); + $infos->{editorname} = $patron->firstname . ' ' . $patron->surname; + } + logaction('ACQUISITIONS', 'MODIFY', $basketno, to_json($infos)); + return; } diff --git a/Koha/EDI.pm b/Koha/EDI.pm index 0ab4658689..c1735e98f4 100644 --- a/Koha/EDI.pm +++ b/Koha/EDI.pm @@ -574,7 +574,9 @@ sub process_quote { basketno => $b, } ); - CloseBasket($b); + # Close the basket, passing a flag indicating that this action + # originated from an approval + CloseBasket($b, 1); } } diff --git a/acqui/basket.pl b/acqui/basket.pl index e9bf7cd440..218be20438 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -210,7 +210,8 @@ if ( $op eq 'delete_confirm' ) { closed => 1, }); ModBasket( { basketno => $basketno, - basketgroupid => $basketgroupid } ); + basketgroupid => $basketgroupid, + borrowernumber => $loggedinuser } ); print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1'); } else { print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?booksellerid=' . $booksellerid); @@ -243,7 +244,8 @@ elsif ( $op eq 'ediorder' ) { $branch = undef if(defined $branch and $branch eq ''); ModBasket({ basketno => $basket->{basketno}, - branch => $branch + branch => $branch, + borrowernumber => $loggedinuser }); print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno"); exit; @@ -558,8 +560,9 @@ sub edi_close_and_order { ); ModBasket( { - basketno => $basketno, - basketgroupid => $basketgroupid + basketno => $basketno, + basketgroupid => $basketgroupid, + borrowernumber => $loggedinuser } ); print $query->redirect( diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl index cc3f9fcc48..cc54d2269d 100755 --- a/acqui/basketgroup.pl +++ b/acqui/basketgroup.pl @@ -308,7 +308,8 @@ if ( $op eq "add" ) { my $basketno=$input->param('basketno'); my $basketgroupid=$input->param('basketgroupid'); ModBasket( { basketno => $basketno, - basketgroupid => $basketgroupid } ); + basketgroupid => $basketgroupid, + borrowernumber => $loggedinuser } ); print $input->redirect("basket.pl?basketno=" . $basketno); } elsif ( $op eq 'closeandprint') { # diff --git a/acqui/basketheader.pl b/acqui/basketheader.pl index 0eb479ff20..ee4cf647b8 100755 --- a/acqui/basketheader.pl +++ b/acqui/basketheader.pl @@ -154,7 +154,8 @@ if ( $op eq 'add_form' ) { scalar $input->param('deliveryplace'), scalar $input->param('billingplace'), scalar $input->param('is_standing') ? 1 : undef, - scalar $input->param('create_items') + scalar $input->param('create_items'), + $loggedinuser ); } else { #New basket $basketno = NewBasket( diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/viewlog.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/viewlog.tt index 64afbe27ae..7deb046ffd 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/viewlog.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/viewlog.tt @@ -57,6 +57,8 @@ [% CASE 'ADDCIRCMESSAGE' %]Add circulation message [% CASE 'DELCIRCMESSAGE' %]Delete circulation message [% CASE 'STATUS_CHANGE' %]Change ILL request status +[% CASE 'CLOSE_BASKET' %]Close an acquisitions basket +[% CASE 'APPROVE_BASKET' %]Approve an acquisitions basket [% CASE 'Run' %]Run [% CASE %][% action | html %] [% END %] @@ -124,7 +126,7 @@ [% END %] - [% FOREACH actx IN [ 'ADD' 'DELETE' 'MODIFY' 'ISSUE' 'RETURN' 'RENEW' 'CREATE' 'CANCEL' 'SUSPEND' 'RESUME' 'ADDCIRCMESSAGE' 'DELCIRCMESSAGE' 'STATUS_CHANGE' 'CHANGE PASS' 'Run' ] %] + [% FOREACH actx IN [ 'ADD' 'DELETE' 'MODIFY' 'ISSUE' 'RETURN' 'RENEW' 'CREATE' 'CANCEL' 'SUSPEND' 'RESUME' 'ADDCIRCMESSAGE' 'DELCIRCMESSAGE' 'STATUS_CHANGE' 'CHANGE PASS' 'Run', 'CLOSE_BASKET', 'APPROVE_BASKET' ] %] [% IF actions.grep(actx).size %] [% ELSE %] -- 2.11.0