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

(-)a/C4/Circulation.pm (-25 / +43 lines)
Lines 981-1009 sub CanBookBeIssued { Link Here
981
    }
981
    }
982
982
983
    ## CHECK AGE RESTRICTION
983
    ## CHECK AGE RESTRICTION
984
    # get $marker from preferences. Could be something like "FSK|PEGI|Alter|Age:"
984
    my $agerestriction  = $biblioitem->{'agerestriction'};
985
    my $markers         = C4::Context->preference('AgeRestrictionMarker');
985
    my ($restriction_age, $daysToAgeRestriction) = GetAgeRestriction( $agerestriction, $borrower );
986
    my $bibvalues       = $biblioitem->{'agerestriction'};
986
    if ( $daysToAgeRestriction ) {
987
    my $restriction_age = GetAgeRestriction( $bibvalues );
987
        if ( C4::Context->preference('AgeRestrictionOverride') ) {
988
988
            $needsconfirmation{AGE_RESTRICTION} = "$agerestriction";
989
    if ( $restriction_age > 0 ) {
989
        }
990
        if ( $borrower->{'dateofbirth'} ) {
990
        else {
991
            my @alloweddate = split /-/, $borrower->{'dateofbirth'};
991
            $issuingimpossible{AGE_RESTRICTION} = "$agerestriction";
992
            $alloweddate[0] += $restriction_age;
993
994
            #Prevent runime eror on leap year (invalid date)
995
            if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) {
996
                $alloweddate[2] = 28;
997
            }
998
999
            if ( Date_to_Days(Today) < Date_to_Days(@alloweddate) - 1 ) {
1000
                if ( C4::Context->preference('AgeRestrictionOverride') ) {
1001
                    $needsconfirmation{AGE_RESTRICTION} = "$bibvalues";
1002
                }
1003
                else {
1004
                    $issuingimpossible{AGE_RESTRICTION} = "$bibvalues";
1005
                }
1006
            }
1007
        }
992
        }
1008
    }
993
    }
1009
994
Lines 3737-3744 sub IsItemIssued { Link Here
3737
    return $sth->fetchrow;
3722
    return $sth->fetchrow;
3738
}
3723
}
3739
3724
3725
=head2 GetAgeRestriction
3726
3727
  my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions, $borrower);
3728
  my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions);
3729
  
3730
  if($daysToAgeRestriction <= 0) { #Borrower is allowed to access this material, as he is older or as old as the agerestriction }
3731
  if($daysToAgeRestriction > 0) { #Borrower is this many days from meeting the agerestriction }
3732
3733
@PARAM1 the koha.biblioitems.agerestriction value, like K18, PEGI 13, ...
3734
@PARAM2 a borrower-object with koha.borrowers.dateofbirth. (OPTIONAL)
3735
@RETURNS The age restriction age in years and the days to fulfill the age restriction for the given borrower.
3736
         Negative days mean the borrower has gone past the age restriction age.
3737
3738
=cut
3739
3740
sub GetAgeRestriction {
3740
sub GetAgeRestriction {
3741
    my ($record_restrictions) = @_;
3741
    my ($record_restrictions, $borrower) = @_;
3742
    my $markers = C4::Context->preference('AgeRestrictionMarker');
3742
    my $markers = C4::Context->preference('AgeRestrictionMarker');
3743
3743
3744
    # Split $record_restrictions to something like FSK 16 or PEGI 6
3744
    # Split $record_restrictions to something like FSK 16 or PEGI 6
Lines 3772-3778 sub GetAgeRestriction { Link Here
3772
        last if ( $restriction_year > 0 );
3772
        last if ( $restriction_year > 0 );
3773
    }
3773
    }
3774
3774
3775
    return $restriction_year;
3775
    #Check if the borrower is age restricted for this material and for how long.
3776
    if ($restriction_year && $borrower) {
3777
        if ( $borrower->{'dateofbirth'} ) {
3778
            my @alloweddate = split /-/, $borrower->{'dateofbirth'};
3779
            $alloweddate[0] += $restriction_year;
3780
3781
            #Prevent runime eror on leap year (invalid date)
3782
            if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) {
3783
                $alloweddate[2] = 28;
3784
            }
3785
3786
            #Get how many days the borrower has to reach the age restriction
3787
            my $daysToAgeRestriction = Date_to_Days(@alloweddate) - Date_to_Days(Today);
3788
            #Negative days means the borrower went past the age restriction age
3789
            return ($restriction_year, $daysToAgeRestriction);
3790
        }
3791
    }
3792
3793
    return ($restriction_year);
3776
}
3794
}
3777
3795
3778
1;
3796
1;
(-)a/t/Circulation/AgeRestrictionMarkers.t (-2 / +21 lines)
Lines 1-5 Link Here
1
#!/usr/bin/perl
2
1
use Modern::Perl;
3
use Modern::Perl;
2
use Test::More tests => 5;
4
use DateTime;
5
use Test::More tests => 10;
3
6
4
use t::lib::Mocks;
7
use t::lib::Mocks;
5
8
Lines 13-15 is ( C4::Circulation::GetAgeRestriction('PEGI16'), '16', 'PEGI16 returns 16' ); Link Here
13
is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' );
16
is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' );
14
is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' );
17
is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' );
15
18
16
- 
19
20
##Testing age restriction for a borrower.
21
my $now = DateTime->now();
22
my $duration_15years = DateTime::Duration->new(years => 15);
23
my $past15yearsAgo = DateTime->now()->subtract_duration($duration_15years);
24
my $borrower = {dateofbirth => $past15yearsAgo->ymd()};
25
26
my ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('FSK 16', $borrower);
27
is ( ($daysToAgeRestriction > 0), 1, 'FSK 16 blocked for a 15 year old' );
28
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI 15', $borrower);
29
is ( ($daysToAgeRestriction <= 0), 1, 'PEGI 15 allowed for a 15 year old' );
30
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI14', $borrower);
31
is ( ($daysToAgeRestriction <= 0), 1, 'PEGI14 allowed for a 15 year old' );
32
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('Age 10', $borrower);
33
is ( ($daysToAgeRestriction <= 0), 1, 'Age 10 allowed for a 15 year old' );
34
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('K18', $borrower);
35
is ( ($daysToAgeRestriction > 0), 1, 'K18 blocked for a 15 year old' );

Return to bug 13106