Ignore:
Timestamp:
12/22/15 16:35:18 (9 years ago)
Author:
nanardon
Message:

Factorise code, add unemployed_expire parameters

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

    r1521 r1522  
    184184 
    185185    my $expire = str2time($user->get_attributes('expire') || '1970-01-01T00:00:00'); 
    186     my $endemploy = $user->get_attributes('endEmployment'); 
     186 
     187    # If there is no current employment we try to find any to not let expire 
     188    # unset 
     189    my $endemploy = $currentemployment 
     190        ? $user->get_attributes('endEmployment') 
     191        : $user->get_attributes('endLastEmployment'); 
    187192    my $nextexpire = str2time($endemploy || '1970-01-01T00:00:00'); 
    188193 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1514 r1522  
    215215                    my ($attr) = @_; 
    216216                    my $self = $attr->object; 
     217                    $self->computeEndEmployment($self->base->config('employment_delay')); 
     218                }, 
     219            }, 
     220            endStrictEmployment => { 
     221                formtype => 'DATETIME', 
     222                managed => 1, 
     223                ro => 1, 
     224                get => sub { 
     225                    my ($attr) = @_; 
     226                    my $self = $attr->object; 
     227                    $self->computeEndEmployment(); 
     228                }, 
     229            }, 
     230            endCurrentEmployment => { 
     231                formtype => 'DATETIME', 
     232                managed => 1, 
     233                ro => 1, 
     234                get => sub { 
     235                    my ($attr) = @_; 
     236                    my $self = $attr->object; 
    217237 
    218238                    my $list_empl = $self->base->db->prepare_cached(q{ 
    219239                        SELECT * FROM employment WHERE "user" = ? and 
     240                            firstday <= now() and 
    220241                            (lastday is null or lastday >= now() - '1 days'::interval)  
    221242                            order by firstday asc 
     
    228249                            $list_empl->finish; 
    229250                            return undef; 
    230                         } 
    231                         if ($end) { 
    232                             my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); 
    233                             my $tend = $end->clone; 
    234                             if (my $days = $self->base->config('employment_delay')) { 
    235                                 $tend->add(days => $days); 
    236                             } 
    237                             if ($tend->ymd lt $nextstart->ymd) { 
    238                                 last; 
    239                             } 
    240                         } 
    241                         $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
    242                         $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    243                         $end->add(hours => 23, minutes => 59, seconds => 59); 
    244                     } 
    245                     $list_empl->finish; 
    246  
    247                     if (!$end) { 
    248                         my $listold = $self->base->db->prepare_cached(q{ 
    249                             SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and 
    250                             lastday IS NOT NULL and lastday <= now() - '1 days'::interval 
    251                         }); 
    252                         $listold->execute($self->id); 
    253                         my $res = $listold->fetchrow_hashref; 
    254                         if ($res && $res->{lastday}) { 
     251                        } else { 
    255252                            $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
    256253                            $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    257254                            $end->add(hours => 23, minutes => 59, seconds => 59); 
    258255                        } 
    259                         $listold->finish; 
    260                     } 
    261                     return $end ? $end->iso8601 : undef 
    262                 }, 
    263             }, 
    264             endStrictEmployment => { 
    265                 formtype => 'DATETIME', 
    266                 managed => 1, 
    267                 ro => 1, 
    268                 get => sub { 
    269                     my ($attr) = @_; 
    270                     my $self = $attr->object; 
    271  
    272                     my $list_empl = $self->base->db->prepare_cached(q{ 
    273                         SELECT * FROM employment WHERE "user" = ? and 
    274                             (lastday is null or lastday >= now() - '1 days'::interval)  
    275                             order by firstday asc 
    276                     }); 
    277                     $list_empl->execute($self->id); 
    278                     my $end; 
    279                     while (my $res = $list_empl->fetchrow_hashref) { 
    280                         if (!$res->{lastday}) { 
    281                             # Ultimate employment. 
    282                             $list_empl->finish; 
    283                             return undef; 
    284                         } 
    285                         if ($end) { 
    286                             my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); 
    287                             if ($end->ymd lt $nextstart->ymd) { 
    288                                 last; 
    289                             } 
    290                         } 
    291                         $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
    292                         $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    293                         $end->add(hours => 23, minutes => 59, seconds => 59); 
     256                        last; 
    294257                    } 
    295258                    $list_empl->finish; 
    296259 
    297                     if (!$end) { 
    298                         my $listold = $self->base->db->prepare_cached(q{ 
    299                             SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and 
    300                             lastday IS NOT NULL and lastday <= now() - '1 days'::interval 
    301                         }); 
    302                         $listold->execute($self->id); 
    303                         my $res = $listold->fetchrow_hashref; 
    304                         if ($res && $res->{lastday}) { 
    305                             $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
    306                             $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
    307                             $end->add(hours => 23, minutes => 59, seconds => 59); 
    308                         } 
    309                         $listold->finish; 
    310                     } 
    311260                    return $end ? $end->iso8601 : undef 
    312261                }, 
     
    10841033} 
    10851034 
     1035 
     1036sub computeEndEmployment { 
     1037    my ($self, $delay) = @_; 
     1038 
     1039    my $list_empl = $self->base->db->prepare_cached(q{ 
     1040        SELECT *, firstday <= now() as "started" FROM employment WHERE "user" = ? and 
     1041        (lastday is null or lastday >= now() - '1 days'::interval)  
     1042        order by firstday asc 
     1043        }); 
     1044    $list_empl->execute($self->id); 
     1045    my $end; 
     1046    while (my $res = $list_empl->fetchrow_hashref) { 
     1047        if (!$res->{lastday}) { 
     1048            # Ultimate employment. 
     1049            $list_empl->finish; 
     1050            return undef; 
     1051        } 
     1052        if ($end) { 
     1053            my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); 
     1054            my $tend = $end->clone; 
     1055            if ($delay) { 
     1056                $tend->add(days => $delay); 
     1057            } 
     1058            if ($tend->ymd lt $nextstart->ymd) { 
     1059                last; 
     1060            } 
     1061        } else { 
     1062            if (!$res->{started}) { 
     1063                last; 
     1064            } 
     1065        } 
     1066        $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
     1067        $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
     1068        $end->add(hours => 23, minutes => 59, seconds => 59); 
     1069    } 
     1070    $list_empl->finish; 
     1071 
     1072    if (!$end) { 
     1073        my $listold = $self->base->db->prepare_cached(q{ 
     1074            SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and 
     1075            lastday IS NOT NULL and lastday <= now() - '1 days'::interval 
     1076            }); 
     1077        $listold->execute($self->id); 
     1078        my $res = $listold->fetchrow_hashref; 
     1079        if ($res && $res->{lastday}) { 
     1080            $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
     1081            $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
     1082            $end->add(hours => 23, minutes => 59, seconds => 59); 
     1083        } else { 
     1084            if (my $default = $self->base->config('unemployed_expire')) { 
     1085                $end = DateTime->from_epoch(epoch => str2time($default)); 
     1086            } 
     1087        } 
     1088        $listold->finish; 
     1089    } 
     1090    return $end ? $end->iso8601 : undef 
     1091} 
     1092 
    108610931; 
    10871094 
Note: See TracChangeset for help on using the changeset viewer.