Ignore:
Timestamp:
12/18/15 07:25:11 (9 years ago)
Author:
nanardon
Message:
  • Add expiration list to expiration page and Employment things
Location:
trunk/LATMOS-Accounts/lib/LATMOS/Accounts
Files:
1 added
4 edited

Legend:

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

    r1413 r1500  
    392392        return; 
    393393    }; 
     394 
     395    if (!$pclass->checkValues($self, $id, %data)) { 
     396        la_log(LA_ERR, 
     397            'Cannot create %s (%s) in base %s (%s): wrong values', 
     398            $id, $otype, $self->label, $self->type 
     399        ); 
     400        return; 
     401    } 
     402 
    394403    if ($pclass->_create($self, $id, %data)) { 
    395404        la_log(LA_INFO, 
     
    404413        return; 
    405414    }; 
     415    warn $id; 
    406416    $self->get_object($otype, $id); 
    407417} 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r1453 r1500  
    320320=cut 
    321321 
     322=head2 checkValues ($base, $obj, %attributes) 
     323 
     324Allow to pre-check values when object are modified or created 
     325 
     326C<$obj> is either the new id at object creation or the object itself on modification. 
     327 
     328=cut 
     329 
     330sub checkValues { 
     331    my ($class, $base, $obj, %attributes) = @_; 
     332 
     333    return 1; 
     334} 
     335 
    322336=head2 check_allowed_values ($attr, $values) 
    323337 
     
    369383        }; 
    370384    } 
     385 
    371386    $self->_set_c_fields(%cdata); 
    372387} 
     
    399414            return; 
    400415        }; 
     416    } 
     417 
     418    if (!$self->checkValues($self->base, $self, %cdata)) { 
     419        $self->base->log(LA_ERR, 
     420            "Cannot update %s (%s): wrong values", 
     421            $self->id, $self->type 
     422        ); 
     423        return; 
    401424    } 
    402425 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm

    r1489 r1500  
    66 
    77use base qw(LATMOS::Accounts::Bases::Sql::objects); 
     8use LATMOS::Accounts::Log; 
     9use Date::Parse; 
     10use DateTime; 
    811 
    912our $VERSION = (q$Rev: 594 $ =~ /^Rev: (\d+) /)[0]; 
     
    3033        ( 
    3134            $user, 
    32             $self->get_c_field('l'), 
     35            $self->get_c_field('firstday'), 
    3336        ) 
    3437    ); 
     
    5255            }, 
    5356            description => { }, 
    54             firstday => { inline => 1, formtype => 'DATE', monitored => 1, }, 
    55             lastday => { inline => 1, formtype => 'DATE', monitored => 1, }, 
     57            firstday => { 
     58                inline => 1, 
     59                formtype => 'DATE', 
     60                monitored => 1, 
     61            }, 
     62            lastday => { 
     63                inline => 1, 
     64                formtype => 'DATE', 
     65                monitored => 1, 
     66            }, 
     67            'state' => { 
     68                managed => 1, 
     69                ro => 1, 
     70                get => sub { 
     71                    my ($self) = @_; 
     72                    my $now = DateTime->now; 
     73                    if ($now->epoch < str2time($self->object->get_attributes('firstday'))) { 
     74                        return 1; 
     75                    } elsif ( my $end = $self->object->get_attributes('lastday') ) { 
     76                        my $eend = str2time($end) + 86400; 
     77                        if ($now->epoch > $eend) { 
     78                            return -1; 
     79                        } 
     80                    } else { 
     81                        return 0; 
     82                    } 
     83                }, 
     84            }, 
    5685            contratType => { 
    5786                reference => 'group', 
     
    6190                monitored => 1, 
    6291            }, 
     92            managerContact => { 
     93                delayed => 1, 
     94                can_values => sub { 
     95                    my %uniq = map { $_ => 1 } grep { $_ } 
     96                    ($_[1] ? $_[1]->get_attributes('managerContact') : ()), 
     97                    $base->search_objects('user', 'active=*'); 
     98                    sort keys %uniq; 
     99                }, 
     100                reference => 'user', 
     101            }, 
     102            department => { 
     103                reference => 'group', 
     104                can_values => sub { 
     105                    $base->search_objects('group', 'sutype=dpmt') 
     106                }, 
     107                monitored => 1, 
     108            }, 
     109            company => { }, 
     110            endcircuit    => { formtype => 'DATE', monitored => 1, }, 
    63111        } 
    64112    ); 
     
    73121} 
    74122 
     123sub applyToUser { 
     124    my ($self) = @_; 
     125 
     126    my $user = $self->base->get_object('user', $self->get_attributes('user')) or do { 
     127        $self->base->log(LA_ERR, "Cannot fetch user %s to apply employment", $self->get_attributes('user')); 
     128        return; 
     129    }; 
     130 
     131    my $expire = str2time($user->get_attributes('expire') || '1970-01-01T00:00:00'); 
     132    my $endemploy = $user->get_attributes('endEmployment'); 
     133    my $nextexpire = str2time($endemploy || '1970-01-01T00:00:00'); 
     134 
     135    if ($expire != $nextexpire) { 
     136        $user->_set_c_fields(expire => $endemploy); 
     137    } 
     138 
     139    if (($user->get_attributes('currentEmployment') || '') ne $self->id) { 
     140        return; 
     141    } 
     142 
     143    my %attrsets = ( 
     144        appliedEmployement => $self->id, 
     145    ); 
     146    foreach my $attr (qw(company contratType department managerContact endcircuit)) { 
     147        my $uval = $user->get_attributes($attr) || ''; 
     148        my $cval = $self->get_attributes($attr) || ''; 
     149 
     150        if ($uval ne $cval) { 
     151            $attrsets{$attr} = $cval; 
     152        } 
     153    } 
     154 
     155    if (keys %attrsets) { 
     156        return $user->_set_c_fields(%attrsets); 
     157    } else { 
     158        return 1; 
     159    } 
     160} 
     161 
     162sub checkValues { 
     163    my ($class, $base, $obj, %changes) = @_; 
     164 
     165    my $user = $changes{user} || $obj->get_attributes('user'); 
     166    my $id = ref $obj ? $obj->id : $obj; 
     167 
     168    if ($changes{lastday}) { 
     169        my $sth = $base->db->prepare_cached(q{ 
     170            select name from employment where "user" = ? and name != ? and 
     171                firstday <= ? and (lastday is null or lastday >= ?) 
     172        }); 
     173        $sth->execute($user, $id, $changes{lastday}, $changes{lastday}); 
     174        my $res = $sth->fetchrow_hashref; 
     175        $sth->finish; 
     176        if ($res) { 
     177            $base->log(LA_ERR, "New ending overlap contrat %s (%s)", $res->{name}, $changes{lastday}); 
     178            return; 
     179        } 
     180    } elsif(exists($changes{lastday})) { 
     181        my $sth = $base->db->prepare_cached(q{ 
     182            select * from employment where "user" = ? and lastday is null 
     183            limit 1 
     184        }); 
     185        $sth->execute($user); 
     186        my $res = $sth->fetchrow_hashref; 
     187        if ($res && $res->{name}) { 
     188            if ($id ne $res->{name}) { 
     189                $base->log(LA_ERR, "Another contract has no ending (%s)", $res->{name} || ''); 
     190                return; 
     191            } 
     192        } 
     193    } 
     194    if ($changes{firstday}) { 
     195        my $sth = $base->db->prepare_cached(q{ 
     196            select name from employment where "user" = ? and name != ? and 
     197                firstday <= ? and (lastday is null or lastday >= ?) 
     198        }); 
     199        $sth->execute($user, $id, $changes{firstday}, $changes{firstday}); 
     200        my $res = $sth->fetchrow_hashref; 
     201        $sth->finish; 
     202        if ($res) { 
     203            $base->log(LA_ERR, "New starting overlap contrat %s (%s)", $res->{name}, $changes{firstday}); 
     204            return; 
     205        } 
     206    } 
     207 
     208 
     209    return 1; 
     210} 
     211 
     212sub ReportChange { 
     213    my ($self, $changetype, $message, @args) = @_; 
     214 
     215    $self->applyToUser(); 
     216 
     217    $self->SUPER::ReportChange($changetype, $message, @args); 
     218} 
     219 
    752201; 
    76221 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1498 r1500  
    99use LATMOS::Accounts::Log; 
    1010use POSIX qw(strftime); 
     11use Date::Parse qw(str2time); 
     12use DateTime; 
     13use DateTime::TimeZone; 
    1114use base qw(LATMOS::Accounts::Bases::Sql::objects); 
    1215 
     
    106109                        : ($value ? 1 : 0 ); 
    107110 
    108                     foreach my $al ($self->object->get_attributes('aliases'), $self->object->id) { 
     111                    foreach my $al (grep { $_ } $self->object->get_attributes('aliases'), $self->object->id) { 
    109112                        my $obj = $self->base->get_object('aliases', $al) or next; 
    110113                        $obj->_set_c_fields( 
     
    193196                monitored => 1, 
    194197            }, 
    195             expire        => { inline => 1, formtype => 'DATE', monitored => 1, }, 
    196             endcircuit    => { inline => 1, formtype => 'DATE', monitored => 1, }, 
     198            expire        => { inline => 1, formtype => 'DATETIME', monitored => 1, }, 
     199            endcircuit    => { inline => 1, formtype => 'DATE',     monitored => 1, }, 
     200            endEmployment => { 
     201                formtype => 'DATETIME', 
     202                managed => 1, 
     203                ro => 1, 
     204                get => sub { 
     205                    my ($attr) = @_; 
     206                    my $self = $attr->object; 
     207 
     208                    my $list_empl = $self->base->db->prepare_cached(q{ 
     209                        SELECT * FROM employment WHERE "user" = ? and 
     210                            (lastday is null or lastday >= now() - '1 days'::interval)  
     211                    }); 
     212                    $list_empl->execute($self->id); 
     213                    my $end; 
     214                    while (my $res = $list_empl->fetchrow_hashref) { 
     215                        if (!$res->{lastday}) { 
     216                            # Ultimate employment. 
     217                            $end = undef; 
     218                            last 
     219                        } 
     220                        if ($end) { 
     221                            my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); 
     222                            if ($end->ymd lt $nextstart->ymd) { 
     223                                last; 
     224                            } 
     225                        } 
     226                        $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); 
     227                        $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); 
     228                        $end->add(hours => 23, minutes => 59, seconds => 59); 
     229                    } 
     230                    $list_empl->finish; 
     231                    return $end ? $end->iso8601 : undef 
     232                }, 
     233            }, 
    197234            cn        => { 
    198235                inline => 1, ro => 1, 
     
    874911                        q{ 
    875912                        select name from employment where firstday <= now() and 
    876                         (lastday is null or lastday >= now()) and "user" = ? 
     913                        (lastday is null or lastday >= now() - '1 days'::interval) and "user" = ? 
    877914                        limit 1 
    878915                        } 
     
    887924                    } 
    888925                } 
     926            }, 
     927            appliedEmployement => { 
     928                hidden => 1, 
     929                reference => 'employment', 
    889930            }, 
    890931        } 
Note: See TracChangeset for help on using the changeset viewer.