Created
October 23, 2017 15:35
-
-
Save xmorave2/8c3ceaa55bd42f58c7db72c19c57d92b to your computer and use it in GitHub Desktop.
Cronjob pro výpočet upomínek alá Clavius
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) } | |
| }, { | |
| join => 'issues', | |
| 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-11-01"); # Datum převodu do Kohy | |
| 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; | |
| 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); | |
| # Na hulváta zadrátovaný termíny, částky a popisky, needs improvement of course | |
| 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"; | |
| } | |
| 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; | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pro verzi 17.11 bude potřeba upravit s ohledem na bug 14826 - account offsets