View | Details | Raw Unified | Return to bug 19336
Collapse All | Expand All

(-)a/misc/cronjobs/fines.pl (-50 / +51 lines)
Lines 34-40 use Koha::Script -cron; Link Here
34
use C4::Context;
34
use C4::Context;
35
use C4::Overdues qw( Getoverdues CalcFine UpdateFine );
35
use C4::Overdues qw( Getoverdues CalcFine UpdateFine );
36
use Getopt::Long qw( GetOptions );
36
use Getopt::Long qw( GetOptions );
37
use Carp qw( carp croak );
37
use Carp         qw( carp croak );
38
use File::Spec;
38
use File::Spec;
39
use Try::Tiny qw( catch try );
39
use Try::Tiny qw( catch try );
40
40
Lines 52-66 my $maxdays; Link Here
52
my $verify_issue;
52
my $verify_issue;
53
my $maxfinesdays;
53
my $maxfinesdays;
54
54
55
my $command_line_options = join(" ",@ARGV);
55
my $command_line_options = join( " ", @ARGV );
56
56
57
GetOptions(
57
GetOptions(
58
    'h|help'    => \$help,
58
    'h|help'         => \$help,
59
    'v|verbose' => \$verbose,
59
    'v|verbose'      => \$verbose,
60
    'l|log'     => \$log,
60
    'l|log'          => \$log,
61
    'o|out:s'   => \$output_dir,
61
    'o|out:s'        => \$output_dir,
62
    'm|maxdays:i' => \$maxdays,
62
    'm|maxdays:i'    => \$maxdays,
63
    'i|verifyissue' => \$verify_issue,
63
    'i|verifyissue'  => \$verify_issue,
64
    'maxfinesdays=i' => \$maxfinesdays
64
    'maxfinesdays=i' => \$maxfinesdays
65
);
65
);
66
my $usage = << 'ENDUSAGE';
66
my $usage = << 'ENDUSAGE';
Lines 87-121 if ($help) { Link Here
87
    exit;
87
    exit;
88
}
88
}
89
89
90
my $script_handler = Koha::Script->new({ script => $0 });
90
my $script_handler = Koha::Script->new( { script => $0 } );
91
91
92
try {
92
try {
93
    $script_handler->lock_exec;
93
    $script_handler->lock_exec;
94
}
94
} catch {
95
catch {
96
    my $message = "Skipping execution of $0 ($_)";
95
    my $message = "Skipping execution of $0 ($_)";
97
    print STDERR "$message\n"
96
    print STDERR "$message\n"
98
        if $verbose;
97
        if $verbose;
99
    cronlogaction({ info => $message });
98
    cronlogaction( { info => $message } );
100
    exit;
99
    exit;
101
};
100
};
102
101
103
cronlogaction({ info => $command_line_options });
102
cronlogaction( { info => $command_line_options } );
104
103
105
my @borrower_fields =
104
my @borrower_fields = qw(cardnumber categorycode surname firstname email phone address citystate);
106
  qw(cardnumber categorycode surname firstname email phone address citystate);
105
my @item_fields     = qw(itemnumber barcode date_due);
107
my @item_fields  = qw(itemnumber barcode date_due);
106
my @other_fields    = qw(days_overdue fine);
108
my @other_fields = qw(days_overdue fine);
107
my $libname         = C4::Context->preference('LibraryName');
109
my $libname      = C4::Context->preference('LibraryName');
108
my $control         = C4::Context->preference('CircControl');
110
my $control      = C4::Context->preference('CircControl');
109
my $branch_type     = C4::Context->preference('HomeOrHoldingBranch') || 'homebranch';
111
my $branch_type  = C4::Context->preference('HomeOrHoldingBranch') || 'homebranch';
110
my $mode            = C4::Context->preference('finesMode');
112
my $mode         = C4::Context->preference('finesMode');
111
my $delim           = "\t";    # ?  C4::Context->preference('CSVDelimiter') || "\t";
113
my $delim = "\t";    # ?  C4::Context->preference('CSVDelimiter') || "\t";
114
112
115
my %is_holiday;
113
my %is_holiday;
116
my $today = dt_from_string();
114
my $today = dt_from_string();
117
my $filename;
115
my $filename;
118
if ($log or $output_dir) {
116
if ( $log or $output_dir ) {
119
    $filename = get_filename($output_dir);
117
    $filename = get_filename($output_dir);
120
}
118
}
121
119
Lines 134-150 for my $overdue ( @{$overdues} ) { Link Here
134
    next if $overdue->{itemlost};
132
    next if $overdue->{itemlost};
135
133
136
    if ( !defined $overdue->{borrowernumber} ) {
134
    if ( !defined $overdue->{borrowernumber} ) {
137
        carp
135
        carp "ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
138
"ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
139
        next;
136
        next;
140
    }
137
    }
141
    my $patron = Koha::Patrons->find( $overdue->{borrowernumber} );
138
    my $patron = Koha::Patrons->find( $overdue->{borrowernumber} );
142
    my $branchcode =
139
    my $branchcode =
143
        ( $control eq 'ItemHomeLibrary' ) ? $overdue->{$branch_type}
140
          ( $control eq 'ItemHomeLibrary' ) ? $overdue->{$branch_type}
144
      : ( $control eq 'PatronLibrary' )   ? $patron->branchcode
141
        : ( $control eq 'PatronLibrary' )   ? $patron->branchcode
145
      :                                     $overdue->{branchcode};
142
        :                                     $overdue->{branchcode};
146
143
147
# In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
144
    # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
148
    if ( !exists $is_holiday{$branchcode} ) {
145
    if ( !exists $is_holiday{$branchcode} ) {
149
        $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
146
        $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
150
    }
147
    }
Lines 155-177 for my $overdue ( @{$overdues} ) { Link Here
155
    }
152
    }
156
    ++$counted;
153
    ++$counted;
157
154
158
    my ( $amount, $unitcounttotal, $unitcount);
155
    my ( $amount, $unitcounttotal, $unitcount );
159
    if ( defined($maxfinesdays) && $maxfinesdays <= $datedue->delta_days( $today )->in_units( 'days' ) ) {
156
    if ( defined($maxfinesdays) && $maxfinesdays <= $datedue->delta_days($today)->in_units('days') ) {
160
        my $itype = $overdue->{itemtype} || $overdue->{itype};
157
        my $itype = $overdue->{itemtype} || $overdue->{itype};
161
        my $data = Koha::CirculationRules->get_effective_rule( { rule_name => 'overduefinescap', categorycode => $patron->categorycode, itemtype => $itype, branchcode => $branchcode } );
158
        my $data  = Koha::CirculationRules->get_effective_rule(
162
        if ( defined($data->rule_value) ) {
159
            {
163
            $amount = $data->rule_value;
160
                rule_name  => 'overduefinescap', categorycode => $patron->categorycode, itemtype => $itype,
161
                branchcode => $branchcode
162
            }
163
        );
164
        if ( defined( $data->rule_value ) ) {
165
            $amount    = $data->rule_value;
164
            $unitcount = 'n/a';
166
            $unitcount = 'n/a';
165
        }
167
        } else {
166
        else {
168
            print
167
            print "No overduefinescap defined for {branchcode = $branchcode, itemtype = $itype and categorycode = $patron->categorycode}\n";
169
                "No overduefinescap defined for {branchcode = $branchcode, itemtype = $itype and categorycode = $patron->categorycode}\n";
168
        }
170
        }
169
    }
171
    }
170
    if (!$amount) {
172
    if ( !$amount ) {
171
        ( $amount, $unitcounttotal, $unitcount ) = CalcFine( $overdue, $patron->categorycode, $branchcode, $datedue, $today );
173
        ( $amount, $unitcounttotal, $unitcount ) =
174
            CalcFine( $overdue, $patron->categorycode, $branchcode, $datedue, $today );
172
    }
175
    }
173
176
174
175
    # Don't update the fine if today is a holiday.
177
    # Don't update the fine if today is a holiday.
176
    # This ensures that dropbox mode will remove the correct amount of fine.
178
    # This ensures that dropbox mode will remove the correct amount of fine.
177
    if (
179
    if (
Lines 179-190 for my $overdue ( @{$overdues} ) { Link Here
179
        && ( !$is_holiday{$branchcode}
181
        && ( !$is_holiday{$branchcode}
180
            || C4::Context->preference('ChargeFinesOnClosedDays') )
182
            || C4::Context->preference('ChargeFinesOnClosedDays') )
181
        && ( $amount && $amount > 0 )
183
        && ( $amount && $amount > 0 )
182
      )
184
        )
183
    {
185
    {
184
        if ( $verify_issue ) {
186
        if ($verify_issue) {
187
185
            # if the issue changed before the script got to it, then pass on it.
188
            # if the issue changed before the script got to it, then pass on it.
186
            my $issue = Koha::Checkouts->find({ issue_id => $overdue->{issue_id} });
189
            my $issue = Koha::Checkouts->find( { issue_id => $overdue->{issue_id} } );
187
            if ( ! $issue or $issue->date_due ne $overdue->{date_due} ) {
190
            if ( !$issue or $issue->date_due ne $overdue->{date_due} ) {
188
                $counted--;
191
                $counted--;
189
                next;
192
                next;
190
            }
193
            }
Lines 204-217 for my $overdue ( @{$overdues} ) { Link Here
204
    if ($filename) {
207
    if ($filename) {
205
        my @cells;
208
        my @cells;
206
        push @cells,
209
        push @cells,
207
          map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
210
            map { defined $borrower->{$_} ? $borrower->{$_} : q{} } @borrower_fields;
208
          @borrower_fields;
209
        push @cells, map { $overdue->{$_} } @item_fields;
211
        push @cells, map { $overdue->{$_} } @item_fields;
210
        push @cells, $unitcounttotal, $amount;
212
        push @cells, $unitcounttotal, $amount;
211
        say {$fh} join $delim, @cells;
213
        say {$fh} join $delim, @cells;
212
    }
214
    }
213
}
215
}
214
if ($filename){
216
if ($filename) {
215
    close $fh;
217
    close $fh;
216
}
218
}
217
219
Lines 232-238 Number of Overdue Items: Link Here
232
EOM
234
EOM
233
}
235
}
234
236
235
cronlogaction({ action => 'End', info => "COMPLETED" });
237
cronlogaction( { action => 'End', info => "COMPLETED" } );
236
238
237
sub set_holiday {
239
sub set_holiday {
238
    my ( $branch, $dt ) = @_;
240
    my ( $branch, $dt ) = @_;
Lines 253-259 sub get_filename { Link Here
253
    $name =~ s/\W//;
255
    $name =~ s/\W//;
254
    $name .= join q{}, q{_}, $today->ymd(), '.log';
256
    $name .= join q{}, q{_}, $today->ymd(), '.log';
255
    $name = File::Spec->catfile( $directory, $name );
257
    $name = File::Spec->catfile( $directory, $name );
256
    if ($verbose && $log) {
258
    if ( $verbose && $log ) {
257
        say "writing to $name";
259
        say "writing to $name";
258
    }
260
    }
259
    return $name;
261
    return $name;
260
- 

Return to bug 19336