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

(-)a/C4/Circulation.pm (-25 / +40 lines)
Lines 983-1011 sub CanBookBeIssued { Link Here
983
    }
983
    }
984
984
985
    ## CHECK AGE RESTRICTION
985
    ## CHECK AGE RESTRICTION
986
    # get $marker from preferences. Could be something like "FSK|PEGI|Alter|Age:"
986
    my $agerestriction  = $biblioitem->{'agerestriction'};
987
    my $markers         = C4::Context->preference('AgeRestrictionMarker');
987
    my ($restriction_age, $daysToAgeRestriction) = GetAgeRestriction( $agerestriction, $borrower );
988
    my $bibvalues       = $biblioitem->{'agerestriction'};
988
    if ( $daysToAgeRestriction ) {
989
    my $restriction_age = GetAgeRestriction( $bibvalues );
989
        if ( C4::Context->preference('AgeRestrictionOverride') ) {
990
990
            $needsconfirmation{AGE_RESTRICTION} = "$agerestriction";
991
    if ( $restriction_age > 0 ) {
991
        }
992
        if ( $borrower->{'dateofbirth'} ) {
992
        else {
993
            my @alloweddate = split /-/, $borrower->{'dateofbirth'};
993
            $issuingimpossible{AGE_RESTRICTION} = "$agerestriction";
994
            $alloweddate[0] += $restriction_age;
995
996
            #Prevent runime eror on leap year (invalid date)
997
            if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) {
998
                $alloweddate[2] = 28;
999
            }
1000
1001
            if ( Date_to_Days(Today) < Date_to_Days(@alloweddate) - 1 ) {
1002
                if ( C4::Context->preference('AgeRestrictionOverride') ) {
1003
                    $needsconfirmation{AGE_RESTRICTION} = "$bibvalues";
1004
                }
1005
                else {
1006
                    $issuingimpossible{AGE_RESTRICTION} = "$bibvalues";
1007
                }
1008
            }
1009
        }
994
        }
1010
    }
995
    }
1011
996
Lines 3754-3761 sub IsItemIssued { Link Here
3754
    return $sth->fetchrow;
3739
    return $sth->fetchrow;
3755
}
3740
}
3756
3741
3742
=head2 GetAgeRestriction
3743
3744
  my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions, $borrower);
3745
  my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions);
3746
3747
@PARAM1 the koha.biblioitems.agerestriction value, like K18, PEGI 13, ...
3748
@PARAM2 a borrower-object with koha.borrowers.dateofbirth. (OPTIONAL)
3749
@RETURNS The age restriction age in years and the days to fulfill the age restriction for the given borrower.
3750
         Negative days mean the borrower has gone past the age restriction age.
3751
3752
=cut
3753
3757
sub GetAgeRestriction {
3754
sub GetAgeRestriction {
3758
    my ($record_restrictions) = @_;
3755
    my ($record_restrictions, $borrower) = @_;
3759
    my $markers = C4::Context->preference('AgeRestrictionMarker');
3756
    my $markers = C4::Context->preference('AgeRestrictionMarker');
3760
3757
3761
    # Split $record_restrictions to something like FSK 16 or PEGI 6
3758
    # Split $record_restrictions to something like FSK 16 or PEGI 6
Lines 3789-3795 sub GetAgeRestriction { Link Here
3789
        last if ( $restriction_year > 0 );
3786
        last if ( $restriction_year > 0 );
3790
    }
3787
    }
3791
3788
3792
    return $restriction_year;
3789
    #Check if the borrower is age restricted for this material and for how long.
3790
    if ($restriction_year && $borrower) {
3791
        if ( $borrower->{'dateofbirth'} ) {
3792
            my @alloweddate = split /-/, $borrower->{'dateofbirth'};
3793
            $alloweddate[0] += $restriction_year;
3794
3795
            #Prevent runime eror on leap year (invalid date)
3796
            if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) {
3797
                $alloweddate[2] = 28;
3798
            }
3799
3800
            #Get how many days the borrower has to reach the age restriction
3801
            my $daysToAgeRestriction = Date_to_Days(Today) - Date_to_Days(@alloweddate);
3802
            #Negative days means the borrower went past the age restriction age
3803
            return ($restriction_year, $daysToAgeRestriction);
3804
        }
3805
    }
3806
    
3807
    return ($restriction_year);
3793
}
3808
}
3794
3809
3795
1;
3810
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