Merge branch 'bug_9076' into 3.12-master

This commit is contained in:
Jared Camins-Esakov 2013-01-07 22:02:20 -05:00
commit fc1385a282
4 changed files with 107 additions and 75 deletions

View file

@ -89,7 +89,7 @@ BEGIN {
GetBorrowerCategorycode
&GetBorrowercategoryList
&GetBorrowersWhoHaveNotBorrowedSince
&GetBorrowersToExpunge
&GetBorrowersWhoHaveNeverBorrowed
&GetBorrowersWithIssuesHistoryOlderThan
@ -1938,52 +1938,65 @@ WHERE roadtypeid=?|;
return ($roadtype);
}
=head2 GetBorrowersWhoHaveNotBorrowedSince
=head2 GetBorrowersToExpunge
&GetBorrowersWhoHaveNotBorrowedSince($date)
$borrowers = &GetBorrowersToExpunge(
not_borrowered_since => $not_borrowered_since,
expired_before => $expired_before,
category_code => $category_code,
branchcode => $branchcode
);
this function get all borrowers who haven't borrowed since the date given on input arg.
This function get all borrowers based on the given criteria.
=cut
sub GetBorrowersWhoHaveNotBorrowedSince {
my $filterdate = shift||POSIX::strftime("%Y-%m-%d",localtime());
my $filterexpiry = shift;
my $filterbranch = shift ||
sub GetBorrowersToExpunge {
my $params = shift;
my $filterdate = $params->{'not_borrowered_since'};
my $filterexpiry = $params->{'expired_before'};
my $filtercategory = $params->{'category_code'};
my $filterbranch = $params->{'branchcode'} ||
((C4::Context->preference('IndependantBranches')
&& C4::Context->userenv
&& C4::Context->userenv->{flags} % 2 !=1
&& C4::Context->userenv->{branch})
? C4::Context->userenv->{branch}
: "");
my $dbh = C4::Context->dbh;
my $query = "
SELECT borrowers.borrowernumber,
max(old_issues.timestamp) as latestissue,
max(issues.timestamp) as currentissue
MAX(old_issues.timestamp) AS latestissue,
MAX(issues.timestamp) AS currentissue
FROM borrowers
JOIN categories USING (categorycode)
LEFT JOIN old_issues USING (borrowernumber)
LEFT JOIN issues USING (borrowernumber)
WHERE category_type <> 'S'
AND borrowernumber NOT IN (SELECT guarantorid FROM borrowers WHERE guarantorid IS NOT NULL AND guarantorid <> 0)
AND borrowernumber NOT IN (SELECT guarantorid FROM borrowers WHERE guarantorid IS NOT NULL AND guarantorid <> 0)
";
my @query_params;
if ($filterbranch && $filterbranch ne ""){
$query.=" AND borrowers.branchcode= ?";
push @query_params,$filterbranch;
if ( $filterbranch && $filterbranch ne "" ) {
$query.= " AND borrowers.branchcode = ? ";
push( @query_params, $filterbranch );
}
if($filterexpiry){
if ( $filterexpiry ) {
$query .= " AND dateexpiry < ? ";
push @query_params,$filterdate;
push( @query_params, $filterexpiry );
}
$query.=" GROUP BY borrowers.borrowernumber";
if ($filterdate){
$query.=" HAVING (latestissue < ? OR latestissue IS NULL)
AND currentissue IS NULL";
if ( $filtercategory ) {
$query .= " AND categorycode = ? ";
push( @query_params, $filtercategory );
}
$query.=" GROUP BY borrowers.borrowernumber HAVING currentissue IS NULL ";
if ( $filterdate ) {
$query.=" AND ( latestissue < ? OR latestissue IS NULL ) ";
push @query_params,$filterdate;
}
warn $query if $debug;
my $sth = $dbh->prepare($query);
if (scalar(@query_params)>0){
$sth->execute(@query_params);

View file

@ -10,16 +10,14 @@
*/
function checkForm(form) {
if((form.checkbox[0].checked)){
if(!(form.date1.value)){
alert(_("please enter a date !"));
document.form.date1.focus();
if ( (!form.date1.value) && (!form.borrower_dateexpiry.value) && (!form.borrower_categorycode.value) ){
alert(_("Please enter at least one criterion for deletion!"));
return false;
}
}
if((form.checkbox[1].checked)){
if(!(form.date2.value)){
alert(_("please enter a date !"));
document.form.date2.focus();
return false;
}
}
@ -57,14 +55,29 @@
<form name="f1" onsubmit="return checkForm(this);" action="/cgi-bin/koha/tools/cleanborrowers.pl" method="post">
<fieldset>
<legend>What do you want to do ?</legend>
<p><input id="checkborrower" type="checkbox" name="checkbox" value="borrower" checked="checked" />
<label for="checkborrower">Delete borrower who has not borrowed since:</label>
<input size="10" id="date1" name="filterdate1" value="[% filterdate1 %]" type="text" class="datepicker" />
<h3><input id="checkborrower" type="checkbox" name="checkbox" value="borrower" /><label for="checkborrower"> Delete borrowers</label></h3>
<label for="date1">Who have not borrowed since:</label>
<input size="10" id="date1" name="filterdate1" type="text" class="datepicker" />
<span class="hint">[% INCLUDE 'date-format.inc' %]</span></p>
<p><input id="checkissue" type="checkbox" name="checkbox" value="issue" checked="checked" />
<label for="checkissue">Anonymize check-out history older than</label>
<input size="10" id="date2" name="filterdate2" value="[% filterdate2 %]" type="text" class="datepicker" />
<label for="borrower_dateexpiry">Whose expiration date is before:</label>
<input size="10" id=borrower_dateexpiry" name="borrower_dateexpiry" type="text" class="datepicker" />
<span class="hint">[% INCLUDE 'date-format.inc' %]</span></p>
<label for="borrower_categorycode">Whose patron category is:</label>
<select id="borrower_categorycode" name="borrower_categorycode">
<option value="" selected="selected">Any</option>
[% FOREACH bc IN borrower_categorycodes %]
[% UNLESS bc.categorycode == 'S' %]
<option value="[% bc.categorycode %]">[% bc.description %]</option>
[% END %]
[% END %]
</select>
<h3><input id="checkissue" type="checkbox" name="checkbox" value="issue" /><label for="checkissue"> Anonymize history</label></h3>
<label for="date2">Anonymize check-out history older than</label>
<input size="10" id="date2" name="filterdate2" type="text" class="datepicker" />
<span class="hint">[% INCLUDE 'date-format.inc' %]</span></p>
<!-- hidden here -->
@ -108,6 +121,8 @@
<input type="hidden" name="step3" value="1" />
<input type="hidden" name="filterdate1" value="[% filterdate1 %]" />
<input type="hidden" name="filterdate2" value="[% filterdate2 %]" />
<input type="hidden" name="borrower_dateexpiry" value="[% borrower_dateexpiry %]" />
<input type="hidden" name="borrower_categorycode" value="[% borrower_categorycode %]" />
</fieldset>
<fieldset class="action"><input type="submit" value="Finish" /> <a class="cancel" href="/cgi-bin/koha/tools/cleanborrowers.pl">Cancel</a></fieldset>
</form>

View file

@ -48,7 +48,7 @@ sub methods : Test( 1 ) {
GetPatronImage
PutPatronImage
RmPatronImage
GetBorrowersWhoHaveNotBorrowedSince
GetBorrowersToExpunge
GetBorrowersWhoHaveNeverBorrowed
GetBorrowersWithIssuesHistoryOlderThan
GetBorrowersNamesAndLatestIssue

View file

@ -17,7 +17,6 @@
#
# Written by Antoine Farnault antoine@koha-fr.org on Nov. 2006.
=head1 cleanborrowers.pl
This script allows to do 2 things.
@ -33,6 +32,7 @@ This script allows to do 2 things.
=cut
use strict;
#use warnings; FIXME - Bug 2505
use CGI;
use C4::Auth;
@ -40,7 +40,7 @@ use C4::Output;
use C4::Dates qw/format_date format_date_in_iso/;
use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
use C4::Circulation; # AnonymiseIssueHistory.
use C4::VirtualShelves (); #no import
use C4::VirtualShelves (); #no import
use Date::Calc qw/Today Add_Delta_YM/;
my $cgi = new CGI;
@ -51,13 +51,14 @@ my $cgi = new CGI;
# * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
my $params = $cgi->Vars;
my $filterdate1; # the date which filter on issue history.
my $filterdate2; # the date which filter on borrowers last issue.
my $filterdate1; # the date which filter on issue history.
my $filterdate2; # the date which filter on borrowers last issue.
my $borrower_dateexpiry;
my $borrower_categorycode;
# getting the template
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "tools/cleanborrowers.tmpl",
{ template_name => "tools/cleanborrowers.tmpl",
query => $cgi,
type => "intranet",
authnotrequired => 0,
@ -66,67 +67,74 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
);
if ( $params->{'step2'} ) {
$filterdate1 = format_date_in_iso($params->{'filterdate1'});
$filterdate2 = format_date_in_iso($params->{'filterdate2'});
$filterdate1 = format_date_in_iso( $params->{'filterdate1'} );
$filterdate2 = format_date_in_iso( $params->{'filterdate2'} );
$borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} );
$borrower_categorycode = $params->{'borrower_categorycode'};
my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
my $totalDel;
my $membersToDelete;
if ($checkboxes{borrower}) {
$membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
if ( $checkboxes{borrower} ) {
$membersToDelete =
GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
$totalDel = scalar @$membersToDelete;
}
my $totalAno;
my $membersToAnonymize;
if ($checkboxes{issue}) {
$membersToAnonymize =
GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
$totalAno = scalar @$membersToAnonymize;
if ( $checkboxes{issue} ) {
$membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
$totalAno = scalar @$membersToAnonymize;
}
$template->param(
step2 => 1,
totalToDelete => $totalDel,
totalToAnonymize => $totalAno,
memberstodelete_list => $membersToDelete,
memberstoanonymize_list => $membersToAnonymize,
filterdate1 => format_date($filterdate1),
filterdate2 => format_date($filterdate2),
step2 => 1,
totalToDelete => $totalDel,
totalToAnonymize => $totalAno,
memberstodelete_list => $membersToDelete,
memberstoanonymize_list => $membersToAnonymize,
filterdate1 => format_date($filterdate1),
filterdate2 => format_date($filterdate2),
borrower_dateexpiry => $borrower_dateexpiry,
borrower_categorycode => $borrower_categorycode,
);
### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
### Now, we are only using total, which is not enough imlo
#writing the template
### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
output_html_with_http_headers $cgi, $cookie, $template->output;
exit;
}
if ( $params->{'step3'} ) {
$filterdate1 = format_date_in_iso($params->{'filterdate1'});
$filterdate2 = format_date_in_iso($params->{'filterdate2'});
$filterdate1 = format_date_in_iso( $params->{'filterdate1'} );
$filterdate2 = format_date_in_iso( $params->{'filterdate2'} );
$borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} );
$borrower_categorycode = $params->{'borrower_categorycode'};
my $do_delete = $params->{'do_delete'};
my $do_anonym = $params->{'do_anonym'};
my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
# delete members
if ($do_delete) {
my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
my $membersToDelete =
GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
$totalDel = scalar(@$membersToDelete);
$radio = $params->{'radio'};
if ( $radio eq 'trash' ) {
my $i;
for ( $i = 0 ; $i < $totalDel ; $i++ ) {
MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} );
DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
}
}
else { # delete completly.
} else { # delete completly.
my $i;
for ( $i = 0 ; $i < $totalDel ; $i++ ) {
C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
DelMember($membersToDelete->[$i]->{'borrowernumber'});
C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} );
DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
}
}
$template->param(
@ -134,7 +142,7 @@ if ( $params->{'step3'} ) {
TotalDel => $totalDel
);
}
# Anonymising all members
if ($do_anonym) {
$totalAno = AnonymiseIssueHistory($filterdate2);
@ -143,7 +151,7 @@ if ( $params->{'step3'} ) {
do_anonym => '1',
);
}
$template->param(
step3 => '1',
trash => ( $radio eq "trash" ) ? (1) : (0),
@ -154,16 +162,12 @@ if ( $params->{'step3'} ) {
exit;
}
#default value set to the template are the 'CNIL' value.
my ( $year, $month, $day ) = &Today();
$filterdate1 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0)));
$filterdate2 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, 0, -3)));
$template->param(
step1 => '1',
filterdate1 => $filterdate1,
filterdate2 => $filterdate2,
step1 => '1',
filterdate1 => $filterdate1,
filterdate2 => $filterdate2,
DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
borrower_categorycodes => GetBorrowercategoryList(),
);
#writing the template