Bug 20434: Update UNIMARC framework - auth (NTWORK)
[koha.git] / misc / cronjobs / reconcile_balances.pl
blob0dcae6cb13480043785369a190dac6e25695d913
1 #!/usr/bin/perl
3 # Copyright 2018 Theke Solutions
5 # This file is part of Koha.
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.
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.
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>.
20 =head1 NAME
22 reconcile_balances.pl - cron script to reconcile patron's balances
24 =head1 SYNOPSIS
26 ./reconcile_balances.pl
28 or, in crontab:
29 0 1 * * * reconcile_balances.pl
31 =head1 DESCRIPTION
33 This script loops through patrons with outstanding credits and proceeds
34 to reconcile their balances.
36 =head1 OPTIONS
38 =over 8
40 =item B<--help>
42 Prints a brief help message and exits.
44 =item B<--verbose>
46 Makes the process print information about the taken actions.
48 =back
50 =cut
52 use Modern::Perl;
54 use Getopt::Long;
55 use Pod::Usage;
56 use Try::Tiny;
58 BEGIN {
59 # find Koha's Perl modules
60 # test carefully before changing this
61 use FindBin;
62 eval { require "$FindBin::Bin/../kohalib.pl" };
65 use Koha::Script -cron;
66 use C4::Log;
68 use Koha::Account::Lines;
69 use Koha::Patrons;
71 my $help = 0;
72 my $verbose = 0;
74 GetOptions(
75 'help' => \$help,
76 'verbose' => \$verbose
77 ) or pod2usage(2);
79 pod2usage(1) if $help;
80 cronlogaction();
82 my @patron_ids = map { $_->borrowernumber } Koha::Account::Lines->search(
84 amountoutstanding => { '<' => 0 }
87 columns => [ qw/borrowernumber/ ],
88 distinct => 1,
92 my $patrons = Koha::Patrons->search({ borrowernumber => { -in => \@patron_ids } });
94 while (my $patron = $patrons->next) {
96 my $account = $patron->account;
97 my $total_outstanding_credit = $account->outstanding_credits->total_outstanding;
98 my $total_outstanding_debit = $account->outstanding_debits->total_outstanding;
100 if ( $total_outstanding_credit < 0
101 and $total_outstanding_debit > 0) {
103 try {
105 $account->reconcile_balance;
107 print $patron->id . ": credit: $total_outstanding_credit " .
108 "debit: $total_outstanding_debit " .
109 "=> outstanding " .
110 "credit: " . $account->outstanding_credits->total_outstanding .
111 " debit: " . $account->outstanding_debits->total_outstanding . "\n"
112 if $verbose;
114 catch {
115 print "Problem with patron " . $patron->borrowernumber . ": $_";
122 __END__