|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2018 Theke Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
=head1 NAME |
| 21 |
|
| 22 |
reconcile_balances.pl - cron script to reconcile patron's balances |
| 23 |
|
| 24 |
=head1 SYNOPSIS |
| 25 |
|
| 26 |
./reconcile_balances.pl |
| 27 |
|
| 28 |
or, in crontab: |
| 29 |
0 1 * * * reconcile_balances.pl |
| 30 |
|
| 31 |
=head1 DESCRIPTION |
| 32 |
|
| 33 |
This script loops through patrons with outstanding credits and proceeds |
| 34 |
to reconcile their balances. |
| 35 |
|
| 36 |
=head1 OPTIONS |
| 37 |
|
| 38 |
=over 8 |
| 39 |
|
| 40 |
=item B<--help> |
| 41 |
|
| 42 |
Prints a brief help message and exits. |
| 43 |
|
| 44 |
=item B<--verbose> |
| 45 |
|
| 46 |
Makes the process print information about the taken actions. |
| 47 |
|
| 48 |
=back |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
use Modern::Perl; |
| 53 |
|
| 54 |
use Getopt::Long; |
| 55 |
use Pod::Usage; |
| 56 |
|
| 57 |
BEGIN { |
| 58 |
# find Koha's Perl modules |
| 59 |
# test carefully before changing this |
| 60 |
use FindBin; |
| 61 |
eval { require "$FindBin::Bin/../kohalib.pl" }; |
| 62 |
} |
| 63 |
|
| 64 |
use C4::Log; |
| 65 |
|
| 66 |
use Koha::Account::Lines; |
| 67 |
use Koha::Patrons; |
| 68 |
|
| 69 |
my $help = 0; |
| 70 |
my $verbose = 0; |
| 71 |
|
| 72 |
GetOptions( |
| 73 |
'help' => \$help, |
| 74 |
'verbose' => \$verbose |
| 75 |
) or pod2usage(2); |
| 76 |
|
| 77 |
pod2usage(1) if $help; |
| 78 |
|
| 79 |
cronlogaction(); |
| 80 |
|
| 81 |
my @patron_ids = map { $_->borrowernumber } Koha::Account::Lines->search( |
| 82 |
{ |
| 83 |
amountoutstanding => { '<' => 0 } |
| 84 |
}, |
| 85 |
{ |
| 86 |
columns => [ qw/borrowernumber/ ], |
| 87 |
distinct => 1, |
| 88 |
} |
| 89 |
); |
| 90 |
|
| 91 |
my $patrons = Koha::Patrons->search({ borrowernumber => { -in => \@patron_ids } }); |
| 92 |
|
| 93 |
while (my $patron = $patrons->next) { |
| 94 |
|
| 95 |
my $account = $patron->account; |
| 96 |
my $total_outstanding_credit = $account->outstanding_credits->total_outstanding; |
| 97 |
my $total_outstanding_debit = $account->outstanding_debits->total_outstanding; |
| 98 |
|
| 99 |
if ( $total_outstanding_credit < 0 |
| 100 |
and $total_outstanding_debit > 0) { |
| 101 |
|
| 102 |
$account->reconcile_balance; |
| 103 |
|
| 104 |
print $patron->id . ": credit: $total_outstanding_credit " . |
| 105 |
"debit: $total_outstanding_debit " . |
| 106 |
"=> outstanding " . |
| 107 |
"credit: " . $account->outstanding_credits->total_outstanding . |
| 108 |
" debit: " . $account->outstanding_debits->total_outstanding . "\n" |
| 109 |
if $verbose; |
| 110 |
|
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
1; |
| 115 |
|
| 116 |
__END__ |