Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm	(revision 1521)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm	(revision 1522)
@@ -184,5 +184,10 @@
 
     my $expire = str2time($user->get_attributes('expire') || '1970-01-01T00:00:00');
-    my $endemploy = $user->get_attributes('endEmployment');
+
+    # If there is no current employment we try to find any to not let expire
+    # unset
+    my $endemploy = $currentemployment
+        ? $user->get_attributes('endEmployment')
+        : $user->get_attributes('endLastEmployment');
     my $nextexpire = str2time($endemploy || '1970-01-01T00:00:00');
 
Index: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm
===================================================================
--- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm	(revision 1521)
+++ trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm	(revision 1522)
@@ -215,7 +215,28 @@
                     my ($attr) = @_;
                     my $self = $attr->object;
+                    $self->computeEndEmployment($self->base->config('employment_delay'));
+                },
+            },
+            endStrictEmployment => {
+                formtype => 'DATETIME',
+                managed => 1,
+                ro => 1,
+                get => sub {
+                    my ($attr) = @_;
+                    my $self = $attr->object;
+                    $self->computeEndEmployment();
+                },
+            },
+            endCurrentEmployment => {
+                formtype => 'DATETIME',
+                managed => 1,
+                ro => 1,
+                get => sub {
+                    my ($attr) = @_;
+                    my $self = $attr->object;
 
                     my $list_empl = $self->base->db->prepare_cached(q{
                         SELECT * FROM employment WHERE "user" = ? and
+                            firstday <= now() and
                             (lastday is null or lastday >= now() - '1 days'::interval) 
                             order by firstday asc
@@ -228,85 +249,13 @@
                             $list_empl->finish;
                             return undef;
-                        }
-                        if ($end) {
-                            my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday}));
-                            my $tend = $end->clone;
-                            if (my $days = $self->base->config('employment_delay')) {
-                                $tend->add(days => $days);
-                            }
-                            if ($tend->ymd lt $nextstart->ymd) {
-                                last;
-                            }
-                        }
-                        $end = DateTime->from_epoch(epoch => str2time($res->{lastday}));
-                        $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
-                        $end->add(hours => 23, minutes => 59, seconds => 59);
-                    }
-                    $list_empl->finish;
-
-                    if (!$end) {
-                        my $listold = $self->base->db->prepare_cached(q{
-                            SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and
-                            lastday IS NOT NULL and lastday <= now() - '1 days'::interval
-                        });
-                        $listold->execute($self->id);
-                        my $res = $listold->fetchrow_hashref;
-                        if ($res && $res->{lastday}) {
+                        } else {
                             $end = DateTime->from_epoch(epoch => str2time($res->{lastday}));
                             $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
                             $end->add(hours => 23, minutes => 59, seconds => 59);
                         }
-                        $listold->finish;
-                    }
-                    return $end ? $end->iso8601 : undef
-                },
-            },
-            endStrictEmployment => {
-                formtype => 'DATETIME',
-                managed => 1,
-                ro => 1,
-                get => sub {
-                    my ($attr) = @_;
-                    my $self = $attr->object;
-
-                    my $list_empl = $self->base->db->prepare_cached(q{
-                        SELECT * FROM employment WHERE "user" = ? and
-                            (lastday is null or lastday >= now() - '1 days'::interval) 
-                            order by firstday asc
-                    });
-                    $list_empl->execute($self->id);
-                    my $end;
-                    while (my $res = $list_empl->fetchrow_hashref) {
-                        if (!$res->{lastday}) {
-                            # Ultimate employment.
-                            $list_empl->finish;
-                            return undef;
-                        }
-                        if ($end) {
-                            my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday}));
-                            if ($end->ymd lt $nextstart->ymd) {
-                                last;
-                            }
-                        }
-                        $end = DateTime->from_epoch(epoch => str2time($res->{lastday}));
-                        $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
-                        $end->add(hours => 23, minutes => 59, seconds => 59);
+                        last;
                     }
                     $list_empl->finish;
 
-                    if (!$end) {
-                        my $listold = $self->base->db->prepare_cached(q{
-                            SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and
-                            lastday IS NOT NULL and lastday <= now() - '1 days'::interval
-                        });
-                        $listold->execute($self->id);
-                        my $res = $listold->fetchrow_hashref;
-                        if ($res && $res->{lastday}) {
-                            $end = DateTime->from_epoch(epoch => str2time($res->{lastday}));
-                            $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
-                            $end->add(hours => 23, minutes => 59, seconds => 59);
-                        }
-                        $listold->finish;
-                    }
                     return $end ? $end->iso8601 : undef
                 },
@@ -1084,4 +1033,62 @@
 }
 
+
+sub computeEndEmployment {
+    my ($self, $delay) = @_;
+
+    my $list_empl = $self->base->db->prepare_cached(q{
+        SELECT *, firstday <= now() as "started" FROM employment WHERE "user" = ? and
+        (lastday is null or lastday >= now() - '1 days'::interval) 
+        order by firstday asc
+        });
+    $list_empl->execute($self->id);
+    my $end;
+    while (my $res = $list_empl->fetchrow_hashref) {
+        if (!$res->{lastday}) {
+            # Ultimate employment.
+            $list_empl->finish;
+            return undef;
+        }
+        if ($end) {
+            my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday}));
+            my $tend = $end->clone;
+            if ($delay) {
+                $tend->add(days => $delay);
+            }
+            if ($tend->ymd lt $nextstart->ymd) {
+                last;
+            }
+        } else {
+            if (!$res->{started}) {
+                last;
+            }
+        }
+        $end = DateTime->from_epoch(epoch => str2time($res->{lastday}));
+        $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
+        $end->add(hours => 23, minutes => 59, seconds => 59);
+    }
+    $list_empl->finish;
+
+    if (!$end) {
+        my $listold = $self->base->db->prepare_cached(q{
+            SELECT max(lastday) as lastday FROM employment WHERE "user" = ? and
+            lastday IS NOT NULL and lastday <= now() - '1 days'::interval
+            });
+        $listold->execute($self->id);
+        my $res = $listold->fetchrow_hashref;
+        if ($res && $res->{lastday}) {
+            $end = DateTime->from_epoch(epoch => str2time($res->{lastday}));
+            $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
+            $end->add(hours => 23, minutes => 59, seconds => 59);
+        } else {
+            if (my $default = $self->base->config('unemployed_expire')) {
+                $end = DateTime->from_epoch(epoch => str2time($default));
+            }
+        }
+        $listold->finish;
+    }
+    return $end ? $end->iso8601 : undef
+}
+
 1;
 
