@@ -, +, @@ --- C4/Overdues.pm | 99 +++++++------ Koha/Schema/Result/Issuingrule.pm | 12 ++- admin/smart-rules.pl | 40 ++++-- installer/data/mysql/kohastructure.sql | 1 + installer/data/mysql/updatedatabase.pl | 8 + .../prog/en/modules/admin/smart-rules.tt | 8 + t/db_dependent/Circulation_Issuingrule.t | 153 ++++++++++---------- t/db_dependent/Fines.t | 56 +++++++ 8 files changed, 244 insertions(+), 133 deletions(-) create mode 100644 t/db_dependent/Fines.t --- a/C4/Overdues.pm +++ a/C4/Overdues.pm @@ -24,6 +24,8 @@ use strict; use Date::Calc qw/Today Date_to_Days/; use Date::Manip qw/UnixDate/; use List::MoreUtils qw( uniq ); +use POSIX qw( floor ceil ); +use Readonly; use C4::Circulation; use C4::Context; @@ -33,46 +35,51 @@ use C4::Debug; use vars qw($VERSION @ISA @EXPORT); +Readonly::Scalar our $INTERVAL_START => 1; +Readonly::Scalar our $INTERVAL_END => 0; + BEGIN { - # set the version for version checking + # set the version for version checking $VERSION = 3.07.00.049; - require Exporter; - @ISA = qw(Exporter); - # subs to rename (and maybe merge some...) - push @EXPORT, qw( - &CalcFine - &Getoverdues - &checkoverdues - &NumberNotifyId - &AmountNotify - &UpdateFine - &GetFine - - &CheckItemNotify - &GetOverduesForBranch - &RemoveNotifyLine - &AddNotifyLine - &GetOverdueMessageTransportTypes - ); - # subs to remove - push @EXPORT, qw( - &BorType - ); - - # check that an equivalent don't exist already before moving - - # subs to move to Circulation.pm - push @EXPORT, qw( - &GetIssuesIteminfo - ); - - # &GetIssuingRules - delete. - # use C4::Circulation::GetIssuingRule instead. - - # subs to move to Biblio.pm - push @EXPORT, qw( - &GetItems - ); + require Exporter; + @ISA = qw(Exporter); + + # subs to rename (and maybe merge some...) + push @EXPORT, qw( + &CalcFine + &Getoverdues + &checkoverdues + &NumberNotifyId + &AmountNotify + &UpdateFine + &GetFine + + &CheckItemNotify + &GetOverduesForBranch + &RemoveNotifyLine + &AddNotifyLine + &GetOverdueMessageTransportTypes + ); + + # subs to remove + push @EXPORT, qw( + &BorType + ); + + # check that an equivalent don't exist already before moving + + # subs to move to Circulation.pm + push @EXPORT, qw( + &GetIssuesIteminfo + ); + + # &GetIssuingRules - delete. + # use C4::Circulation::GetIssuingRule instead. + + # subs to move to Biblio.pm + push @EXPORT, qw( + &GetItems + ); } =head1 NAME @@ -251,15 +258,13 @@ sub CalcFine { my $chargeable_units = _get_chargeable_units($fine_unit, $start_date, $end_date, $branchcode); my $units_minus_grace = $chargeable_units - $data->{firstremind}; my $amount = 0; - if ($data->{'chargeperiod'} && ($units_minus_grace > 0) ) { - if ( C4::Context->preference('FinesIncludeGracePeriod') ) { - $amount = int($chargeable_units / $data->{'chargeperiod'}) * $data->{'fine'};# TODO fine calc should be in cents - } else { - $amount = int($units_minus_grace / $data->{'chargeperiod'}) * $data->{'fine'}; - } - } else { - # a zero (or null) chargeperiod or negative units_minus_grace value means no charge. - } + if ( $data->{'chargeperiod'} && ( $units_minus_grace > 0 ) ) { + my $units = C4::Context->preference('FinesIncludeGracePeriod') ? $chargeable_units : $units_minus_grace; + my $charge_periods = $units / $data->{'chargeperiod'}; + $charge_periods = $data->{'chargeperiod_charge_at'} == $INTERVAL_START ? ceil($charge_periods) : floor($charge_periods); + $amount = $charge_periods * $data->{'fine'}; + } # else { # a zero (or null) chargeperiod or negative units_minus_grace value means no charge. } + $amount = $data->{overduefinescap} if $data->{overduefinescap} && $amount > $data->{overduefinescap}; $debug and warn sprintf("CalcFine returning (%s, %s, %s, %s)", $amount, $data->{'chargename'}, $units_minus_grace, $chargeable_units); return ($amount, $data->{'chargename'}, $units_minus_grace, $chargeable_units); --- a/Koha/Schema/Result/Issuingrule.pm +++ a/Koha/Schema/Result/Issuingrule.pm @@ -80,6 +80,12 @@ __PACKAGE__->table("issuingrules"); data_type: 'integer' is_nullable: 1 +=head2 chargeperiod_charge_at + + data_type: 'tinyint' + default_value: 0 + is_nullable: 0 + =head2 accountsent data_type: 'integer' @@ -184,6 +190,8 @@ __PACKAGE__->add_columns( { data_type => "integer", is_nullable => 1 }, "chargeperiod", { data_type => "integer", is_nullable => 1 }, + "chargeperiod_charge_at", + { data_type => "tinyint", default_value => 0, is_nullable => 0 }, "accountsent", { data_type => "integer", is_nullable => 1 }, "chargename", @@ -236,8 +244,8 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("branchcode", "categorycode", "itemtype"); -# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-09-17 21:06:58 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vmlv13CeQ99RO0VsviRABg +# Created by DBIx::Class::Schema::Loader v0.07040 @ 2015-01-16 07:20:41 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:6AKNiL/+m3EyTjvAAOz9Cw # You can replace this text with custom content, and it will be preserved on regeneration --- a/admin/smart-rules.pl +++ a/admin/smart-rules.pl @@ -27,6 +27,7 @@ use C4::Koha; use C4::Debug; use C4::Branch; # GetBranches use C4::Dates qw/format_date format_date_in_iso/; +use Koha::Database; my $input = CGI->new; my $dbh = C4::Context->dbh; @@ -100,10 +101,6 @@ elsif ($op eq 'delete-branch-item') { } # save the values entered elsif ($op eq 'add') { - my $sth_search = $dbh->prepare('SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?'); - my $sth_insert = $dbh->prepare('INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, renewalperiod, norenewalbefore, auto_renew, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, maxsuspensiondays, firstremind, chargeperiod,rentaldiscount, overduefinescap) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); - my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, maxsuspensiondays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, renewalperiod=?, norenewalbefore=?, auto_renew=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=?, overduefinescap=? WHERE branchcode=? AND categorycode=? AND itemtype=?"); - my $br = $branch; # branch my $bor = $input->param('categorycode'); # borrower category my $cat = $input->param('itemtype'); # item type @@ -113,6 +110,7 @@ elsif ($op eq 'add') { $maxsuspensiondays = undef if $maxsuspensiondays eq q||; my $firstremind = $input->param('firstremind'); my $chargeperiod = $input->param('chargeperiod'); + my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at'); my $maxissueqty = $input->param('maxissueqty'); my $renewalsallowed = $input->param('renewalsallowed'); my $renewalperiod = $input->param('renewalperiod'); @@ -131,13 +129,33 @@ elsif ($op eq 'add') { my $overduefinescap = $input->param('overduefinescap') || undef; $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty"; - $sth_search->execute($br,$bor,$cat); - my $res = $sth_search->fetchrow_hashref(); - if ($res->{total}) { - $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat); - } else { - $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $auto_renew, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap); - } + my $rs = Koha::Database->new()->schema()->resultset('Issuingrule'); + + $rs->update_or_create( + { + branchcode => $br, + categorycode => $bor, + itemtype => $cat, + fine => $fine, + finedays => $finedays, + maxsuspensiondays => $maxsuspensiondays, + firstremind => $firstremind, + chargeperiod => $chargeperiod, + chargeperiod_charge_at => $chargeperiod_charge_at, + maxissueqty => $maxissueqty, + renewalsallowed => $renewalsallowed, + renewalperiod => $renewalperiod, + norenewalbefore => $norenewalbefore, + auto_renew => $auto_renew, + reservesallowed => $reservesallowed, + issuelength => $issuelength, + lengthunit => $lengthunit, + hardduedate => $hardduedate, + hardduedatecompare => $hardduedatecompare, + rentaldiscount => $rentaldiscount, + overduefinescap => $overduefinescap, + } + ); } elsif ($op eq "set-branch-defaults") { my $categorycode = $input->param('categorycode'); --- a/installer/data/mysql/kohastructure.sql +++ a/installer/data/mysql/kohastructure.sql @@ -1178,6 +1178,7 @@ CREATE TABLE `issuingrules` ( -- circulation and fine rules `maxsuspensiondays` int(11) default NULL, -- max suspension days `firstremind` int(11) default NULL, -- fine grace period `chargeperiod` int(11) default NULL, -- how often the fine amount is charged + `chargeperiod_charge_at` tinyint(1) NOT NULL DEFAULT '0', -- Should fine be given at the start ( 1 ) or the end ( 0 ) of the period `accountsent` int(11) default NULL, -- not used? always NULL `chargename` varchar(100) default NULL, -- not used? always NULL `maxissueqty` int(4) default NULL, -- total number of checkouts allowed --- a/installer/data/mysql/updatedatabase.pl +++ a/installer/data/mysql/updatedatabase.pl @@ -9669,6 +9669,14 @@ if ( CheckVersion($DBversion) ) { SetVersion ($DBversion); } +$DBversion = "3.19.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do("ALTER TABLE issuingrules ADD chargeperiod_charge_at BOOLEAN NOT NULL DEFAULT '0' AFTER chargeperiod"); + + print "Upgrade to $DBversion done (Bug 11944 - Convert DB tables to utf8_unicode_ci)\n"; + SetVersion ($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt @@ -147,6 +147,7 @@ for="tobranch">Clone these rules to: Clone these rules to: