Skip to content

Instantly share code, notes, and snippets.

@xmorave2
Created April 27, 2018 06:31
Show Gist options
  • Select an option

  • Save xmorave2/9a19526d216c61378d357daec8146153 to your computer and use it in GitHub Desktop.

Select an option

Save xmorave2/9a19526d216c61378d357daec8146153 to your computer and use it in GitHub Desktop.
Upomínky
koha-foreach --enabled /usr/share/koha/bin/cronjobs/fines.pl
koha-shell chocen -c '/usr/share/koha/bin/cronjobs/finesChocen.pl -c'
#!/usr/bin/perl
use Modern::Perl;
use DateTime;
use Getopt::Long;
use Koha::Database;
use Koha::DateUtils;
use Koha::Account::Lines;
use Koha::Checkouts;
use Koha::Patrons;
my $verbose;
my $confirm;
GetOptions(
'v|verbose' => \$verbose,
'c|confirm' => \$confirm,
);
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
my $patrons = Koha::Patrons->search(
{
'issues.date_due' => { '<' => $dtf->format_datetime(dt_from_string) },
'categorycode.overduenoticerequired' => 1 #Only for categories with overdues
}, {
join => [ 'issues', 'categorycode' ],
group_by => 'me.borrowernumber'
}
);
if ($verbose) {
print $patrons->count;
print "\n";
}
my $patron;
my $today = dt_from_string;
my $changed_system = dt_from_string("2017-09-26"); # date of switch to Koha
while ($patron = $patrons->next) {
print "Patron: " . $patron->surname . " " . $patron->firstname . ", Cardnumber: " . $patron->cardnumber . "\n" if $verbose;
my $overdues = $patron->get_overdues;
$overdues = Koha::Checkouts->search(
{
borrowernumber => $patron->borrowernumber,
date_due => { '<' => $dtf->format_datetime(dt_from_string) },
}
);
my $min_date_due = $today;
my $overdue;
while ($overdue = $overdues->next) {
my $date_due = dt_from_string($overdue->date_due);
if (DateTime->compare($min_date_due, $date_due) == 1) {
$min_date_due = $date_due;
}
}
print "First date due: " . $dtf->format_datetime($min_date_due) . "\n" if $verbose;
my $accountlines = Koha::Account::Lines->search(
{
borrowernumber => $patron->borrowernumber,
amountoutstanding => { '>' => '0' },
accounttype => { in => [ 'F', 'FU', 'O' ] },
date => { '>' => $dtf->format_date($min_date_due) },
}
);
my $needed_fine_amount = 120; #FIXME 120 does not looks like great default, but will be never used, so till now it is ok.
my $description;
my $delta_days = $min_date_due->delta_days($today)->in_units('days');
print "Due days:" . $delta_days . "\n" if $verbose;
my $old_amount = (DateTime->compare($changed_system, $min_date_due) == 1);
if ($delta_days >= 43 ) {
$needed_fine_amount = 120;
$description = "4. upominka - doporuceny dopis"
} elsif ($delta_days >= 29 ) {
$needed_fine_amount = 60;
$description = "3. upominka";
} elsif ($delta_days >= 15 ) {
$needed_fine_amount = 40;
$needed_fine_amount = 20 if $old_amount;
$description = "2. upominka";
} elsif ($delta_days >= 1 ) {
$needed_fine_amount = 20;
$needed_fine_amount = 15 if $old_amount;
$description = "1. upominka";
} else {
$needed_fine_amount = 0;
next;
}
print "Found accountlines: " . $accountlines->count . "\n" if $verbose;
if ($accountlines->count) {
my $accountline = $accountlines->next;
print "Amount is " . $accountline->amount . ", " if $verbose;
my $delta_amount = $needed_fine_amount - $accountline->amount;
if ($delta_amount) {
print "Modification needed, raising by " . $delta_amount . " to " . $needed_fine_amount . "\n" if $verbose;
print "Updating description to: " . $description . "\n";
$accountline->lastincrement($delta_amount);
$accountline->amountoutstanding(
$accountline->amountoutstanding + $delta_amount
);
$accountline->amount($needed_fine_amount);
$accountline->description($description);
$accountline->store if $confirm;
print "Fine updated" . "\n" if ($verbose && $confirm);
} else {
## we have the right amount, no modification needed
print "No modification needed\n" if $verbose;
}
} else {
print "Creating new fine: " . $needed_fine_amount . ", " . $description . "\n";
my $accountline = Koha::Account::Line->new(
{
borrowernumber => $patron->borrowernumber,
date => $dtf->format_date($today),
amount => $needed_fine_amount,
description => $description,
accounttype => 'F',
amountoutstanding => $needed_fine_amount,
}
);
$accountline->store if $confirm;
print "Fine created" . "\n" if ($verbose && $confirm);
}
print "\n" if $verbose;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment