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 > 0 ) {
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/C4/Members.pm (+43 lines)
Lines 1748-1753 sub GetAge{ Link Here
1748
    return $age;
1748
    return $age;
1749
}    # sub get_age
1749
}    # sub get_age
1750
1750
1751
=head2 SetAge
1752
1753
  $borrower = C4::Members::SetAge($borrower, $datetimeduration);
1754
  $borrower = C4::Members::SetAge($borrower, '0015-12-10');
1755
  $borrower = C4::Members::SetAge($borrower, $datetimeduration, $datetime_reference);
1756
  
1757
  eval { $borrower = C4::Members::SetAge($borrower, '015-1-10'); };
1758
  if ($@) {print $@;} #Catch a bad ISO Date or kill your script!
1759
1760
This function sets the borrower's dateofbirth to match the given age.
1761
Optionally relative to the given $datetime_reference.
1762
1763
@PARAM1 koha.borrowers-object
1764
@PARAM2 DateTime::Duration-object as the desired age
1765
        OR a ISO 8601 Date. (To make the API more pleasant)
1766
@PARAM3 DateTime-object as the relative date, defaults to now().
1767
RETURNS The given borrower reference @PARAM1.
1768
DIES    If there was an error with the ISO Date handling.
1769
1770
=cut
1771
1772
#'
1773
sub SetAge{
1774
    my ( $borrower, $datetimeduration, $datetime_ref ) = @_;
1775
    $datetime_ref = DateTime->now() unless $datetime_ref;
1776
    
1777
    if ($datetimeduration && ref $datetimeduration ne 'DateTime::Duration') {
1778
        if ($datetimeduration =~ /^(\d{4})-(\d{2})-(\d{2})/) {
1779
            $datetimeduration = DateTime::Duration->new(years => $1, months => $2, days => $3);
1780
        }
1781
        else {
1782
            die "C4::Members::SetAge($borrower, $datetimeduration), datetimeduration not a valid ISO 8601 Date!\n";
1783
        }
1784
    }
1785
    
1786
    my $new_datetime_ref = $datetime_ref->clone();
1787
    $new_datetime_ref->subtract_duration( $datetimeduration );
1788
    
1789
    $borrower->{dateofbirth} = $new_datetime_ref->ymd();
1790
    
1791
    return $borrower;
1792
}    # sub SetAge
1793
1751
=head2 GetCities
1794
=head2 GetCities
1752
1795
1753
  $cityarrayref = GetCities();
1796
  $cityarrayref = GetCities();
(-)a/t/Circulation/AgeRestrictionMarkers.t (-1 / +20 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
19
20
##Testing age restriction for a borrower.
21
my $now = DateTime->now();
22
my $borrower = {};
23
C4::Members::SetAge( $borrower, '0015-00-00' );
24
25
my ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('FSK 16', $borrower);
26
is ( ($daysToAgeRestriction > 0), 1, 'FSK 16 blocked for a 15 year old' );
27
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI 15', $borrower);
28
is ( ($daysToAgeRestriction <= 0), 1, 'PEGI 15 allowed for a 15 year old' );
29
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI14', $borrower);
30
is ( ($daysToAgeRestriction <= 0), 1, 'PEGI14 allowed for a 15 year old' );
31
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('Age 10', $borrower);
32
is ( ($daysToAgeRestriction <= 0), 1, 'Age 10 allowed for a 15 year old' );
33
($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('K18', $borrower);
34
is ( ($daysToAgeRestriction > 0), 1, 'K18 blocked for a 15 year old' );
(-)a/t/db_dependent/Members.t (-6 / +77 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 56;
20
use Test::More tests => 67;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Data::Dumper;
22
use Data::Dumper;
23
use C4::Context;
23
use C4::Context;
Lines 82-87 my %data = ( Link Here
82
    userid => 'tomasito'
82
    userid => 'tomasito'
83
);
83
);
84
84
85
testAgeAccessors(\%data); #Age accessor tests don't touch the db so it is safe to run them with just the object.
86
85
my $addmem=AddMember(%data);
87
my $addmem=AddMember(%data);
86
ok($addmem, "AddMember()");
88
ok($addmem, "AddMember()");
87
89
Lines 182-192 C4::Context->clear_syspref_cache(); Link Here
182
$checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
184
$checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
183
is ($checkcardnum, "2", "Card number is too long");
185
is ($checkcardnum, "2", "Card number is too long");
184
186
185
my $age=GetAge("1992-08-14", "2011-01-19");
186
is ($age, "18", "Age correct");
187
187
188
$age=GetAge("2011-01-19", "1992-01-19");
189
is ($age, "-19", "Birthday In the Future");
190
188
191
C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' );
189
C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' );
192
C4::Context->clear_syspref_cache();
190
C4::Context->clear_syspref_cache();
Lines 329-332 sub _find_member { Link Here
329
    return $found;
327
    return $found;
330
}
328
}
331
329
330
### ------------------------------------- ###
331
### Testing GetAge() / SetAge() functions ###
332
### ------------------------------------- ###
333
#USES the package $member-variable to mock a koha.borrowers-object
334
sub testAgeAccessors {
335
    my ($member) = @_;
336
337
    ##Testing GetAge()
338
    my $age=GetAge("1992-08-14", "2011-01-19");
339
    is ($age, "18", "Age correct");
340
    
341
    $age=GetAge("2011-01-19", "1992-01-19");
342
    is ($age, "-19", "Birthday In the Future");
343
    
344
    ##Testing SetAge() for now()
345
    my $dt_now = DateTime->now();
346
    my $age = DateTime::Duration->new(years => 12, months => 6, days => 1);
347
    C4::Members::SetAge( $member, $age );
348
    $age = C4::Members::GetAge( $member->{dateofbirth} );
349
    is ($age, '12', "SetAge 12 years");
350
    
351
    $age = DateTime::Duration->new(years => 18, months => 12, days => 31);
352
    C4::Members::SetAge( $member, $age );
353
    $age = C4::Members::GetAge( $member->{dateofbirth} );
354
    is ($age, '19', "SetAge 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18.
355
    
356
    $age = DateTime::Duration->new(years => 18, months => 12, days => 30);
357
    C4::Members::SetAge( $member, $age );
358
    $age = C4::Members::GetAge( $member->{dateofbirth} );
359
    is ($age, '19', "SetAge 18 years");
360
    
361
    $age = DateTime::Duration->new(years => 0, months => 1, days => 1);
362
    C4::Members::SetAge( $member, $age );
363
    $age = C4::Members::GetAge( $member->{dateofbirth} );
364
    is ($age, '0', "SetAge 0 years");
365
    
366
    $age = '0018-12-31';
367
    C4::Members::SetAge( $member, $age );
368
    $age = C4::Members::GetAge( $member->{dateofbirth} );
369
    is ($age, '19', "SetAge ISO_Date 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18.
370
    
371
    $age = '0018-12-30';
372
    C4::Members::SetAge( $member, $age );
373
    $age = C4::Members::GetAge( $member->{dateofbirth} );
374
    is ($age, '19', "SetAge ISO_Date 18 years");
375
    
376
    $age = '18-1-1';
377
    eval { C4::Members::SetAge( $member, $age ); };
378
    is ((length $@ > 1), '1', "SetAge ISO_Date $age years FAILS");
379
    
380
    $age = '0018-01-01';
381
    eval { C4::Members::SetAge( $member, $age ); };
382
    is ((length $@ == 0), '1', "SetAge ISO_Date $age years succeeds");
383
    
384
    ##Testing SetAge() for relative_date
385
    my $relative_date = DateTime->new(year => 3010, month => 3, day => 15);
386
    
387
    $age = DateTime::Duration->new(years => 10, months => 3);
388
    C4::Members::SetAge( $member, $age, $relative_date );
389
    $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
390
    is ($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd());
391
    
392
    $age = DateTime::Duration->new(years => 112, months => 1, days => 1);
393
    C4::Members::SetAge( $member, $age, $relative_date );
394
    $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
395
    is ($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
396
    
397
    $age = '0112-01-01';
398
    C4::Members::SetAge( $member, $age, $relative_date );
399
    $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
400
    is ($age, '112', "SetAge ISO_Date, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
401
    
402
} #sub testAgeAccessors
403
332
1;
404
1;
333
- 

Return to bug 13106