Ignore:
Timestamp:
01/10/16 20:15:54 (8 years ago)
Author:
nanardon
Message:

Fix start / end Employment computation

Location:
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm

    r1621 r1623  
    1010use Date::Parse; 
    1111use DateTime; 
     12use Date::Calc; 
    1213 
    1314our $VERSION = (q$Rev: 594 $ =~ /^Rev: (\d+) /)[0]; 
     
    7172            monitored => 1, 
    7273            label => l('Contract end'), 
     74        }, 
     75        length => { 
     76            ro => 1, 
     77            managed => 1, 
     78            get => sub { 
     79                my ($self) = @_; 
     80                my $lastday = $self->object->get_attributes('lastday') || DateTime->now->ymd('-'); 
     81                my $firstday = $self->object->get_attributes('firstday'); 
     82 
     83                my @fd = split('-', $firstday); 
     84                my @ld = split('-', $lastday); 
     85 
     86                return Date::Calc::Delta_Days(@fd, @ld) +1; 
     87            }, 
     88            label => l('Length'), 
     89        }, 
     90        lengthText => { 
     91            ro => 1, 
     92            managed => 1, 
     93            get => sub { 
     94                my ($self) = @_; 
     95                my $lastday = $self->object->get_attributes('lastday')|| DateTime->now->ymd('-'); 
     96                { 
     97                    my $dtlast = DateTime->from_epoch(epoch => str2time($lastday)); 
     98                    $dtlast->add(days => 1); 
     99                    $lastday = $dtlast->ymd('-'); 
     100                } 
     101                my $firstday = $self->object->get_attributes('firstday'); 
     102 
     103                my @fd = split('-', $firstday); 
     104                my @ld = split('-', $lastday); 
     105 
     106                my ($Dy,$Dm,$Dd) = Date::Calc::N_Delta_YMD(@fd, @ld); 
     107                return join(', ', 
     108                    ($Dy ? l('%d years', $Dy)  : ()), 
     109                    ($Dm ? l('%d months', $Dm) : ()), 
     110                    ($Dd ? l('%d days', $Dd)   : ()), 
     111                ); 
     112            }, 
     113            label => l('Length'), 
    73114        }, 
    74115        'state' => { 
     
    315356        my $endemploy = $currentemployment 
    316357            ? $user->_get_attributes("_$expireOn") 
    317             : ($self->base->config('unemployed_expire') eq 'no' 
     358            : (($self->base->config('unemployed_expire') ||'') eq 'no' 
    318359                ? undef 
    319360                : $user->_get_attributes('create')); 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1620 r1623  
    1414use base qw(LATMOS::Accounts::Bases::Sql::objects); 
    1515use LATMOS::Accounts::I18N; 
     16use Date::Calc; 
    1617 
    1718our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
     
    407408                ro => 1, 
    408409                label => l('Start of any employment'), 
     410            }, 
     411            employmentLength => { 
     412                ro => 1, 
     413                managed => 1, 
     414                get => sub { 
     415                    my ($self) = @_; 
     416                    my $lastday = $self->object->get_attributes('endEmployment') || DateTime->now->ymd('-'); 
     417                    my $firstday = $self->object->get_attributes('startEmployment') or return; 
     418 
     419                    my @fd = split('-', DateTime->from_epoch(epoch => str2time($firstday))->ymd('-')); 
     420                    my @ld = split('-', DateTime->from_epoch(epoch => str2time($lastday))->ymd('-')); 
     421 
     422                    return Date::Calc::Delta_Days(@fd, @ld) +1; 
     423                }, 
     424                label => l('Work duration'), 
     425            }, 
     426            employmentLengthText => { 
     427                ro => 1, 
     428                managed => 1, 
     429                get => sub { 
     430                    my ($self) = @_; 
     431                    my $firstday = $self->object->get_attributes('startEmployment') or return; 
     432                    my $lastday = $self->object->get_attributes('endEmployment')|| DateTime->now->ymd('-'); 
     433                    { 
     434                        my $dtlast = DateTime->from_epoch(epoch => str2time($lastday)); 
     435                        $dtlast->add(days => 1); 
     436                        $lastday = $dtlast->ymd('-'); 
     437                    } 
     438 
     439                    my @fd = split('-', DateTime->from_epoch(epoch => str2time($firstday))->ymd('-')); 
     440                    my @ld = split('-', DateTime->from_epoch(epoch => str2time($lastday))->ymd('-')); 
     441 
     442                    my ($Dy,$Dm,$Dd) = Date::Calc::N_Delta_YMD(@fd, @ld); 
     443                    return join(', ', 
     444                        ($Dy ? l('%d years', $Dy)  : ()), 
     445                        ($Dm ? l('%d months', $Dm) : ()), 
     446                        ($Dd ? l('%d days', $Dd)   : ()), 
     447                    ); 
     448                }, 
     449                label => l('Work duration'), 
    409450            }, 
    410451            cn        => { 
     
    12961337    my ($self, $delay) = @_; 
    12971338 
     1339    $delay ||= 0; 
     1340 
    12981341    my $list_empl = $self->base->db->prepare_cached(q{ 
    12991342        SELECT *, (lastday is null or lastday >= now()) as "current" FROM employment WHERE "user" = ? 
     1343        and firstday < now() 
    13001344        order by firstday desc 
    13011345        }); 
     
    13051349        if ($start) { 
    13061350            my $prevend = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
     1351            $prevend->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    13071352            my $tstart = $start->clone; 
    1308             if ($delay) { 
    1309                 $tstart->subtract(days => $delay); 
    1310             } 
     1353            $tstart->subtract(days => $delay + 1); 
    13111354            if ($tstart->ymd gt $prevend->ymd) { 
    13121355                last; 
     
    13221365    $list_empl->finish; 
    13231366 
    1324     if (!$start) { 
    1325         my $listold = $self->base->db->prepare_cached(q{ 
    1326             SELECT min(firstday) as firstday FROM employment WHERE "user" = ? and 
    1327             lastday IS NOT NULL and lastday <= now() - '1 days'::interval 
    1328             }); 
    1329         $listold->execute($self->id); 
    1330         my $res = $listold->fetchrow_hashref; 
    1331         if ($res && $res->{firstday}) { 
    1332             $start = DateTime->from_epoch(epoch => str2time($res->{firstday})); 
    1333             $start->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    1334         } 
    1335         $listold->finish; 
    1336     } 
    13371367    return $start ? $start->iso8601 : undef 
    13381368} 
     
    13401370sub _computeEndEmployment { 
    13411371    my ($self, $delay) = @_; 
     1372 
     1373    $delay ||= 0; 
    13421374 
    13431375    my $list_empl = $self->base->db->prepare_cached(q{ 
     
    13561388        if ($end) { 
    13571389            my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); 
     1390            $nextstart->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    13581391            my $tend = $end->clone; 
    1359             if ($delay) { 
    1360                 $tend->add(days => $delay); 
    1361             } 
     1392            $tend->add(days => $delay + 1); 
    13621393            if ($tend->ymd lt $nextstart->ymd) { 
    13631394                last; 
     
    13741405    $list_empl->finish; 
    13751406 
    1376     if (!$end) { 
    1377         my $listold = $self->base->db->prepare_cached(q{ 
    1378             SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and 
    1379             lastday IS NOT NULL and lastday <= now() - '1 days'::interval 
    1380             }); 
    1381         $listold->execute($self->id); 
    1382         my $res = $listold->fetchrow_hashref; 
    1383         if ($res && $res->{lastday}) { 
    1384             $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
    1385             $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    1386             $end->add(hours => 23, minutes => 59, seconds => 59); 
    1387         } 
    1388         $listold->finish; 
    1389     } 
    13901407    return $end ? $end->iso8601 : undef 
    13911408} 
Note: See TracChangeset for help on using the changeset viewer.