Ignore:
Timestamp:
06/23/16 18:53:38 (8 years ago)
Author:
nanardon
Message:

Fix employment overlaping test

File:
1 edited

Legend:

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

    r1773 r1787  
    418418    my $id = ref $obj ? $obj->id : $obj; 
    419419 
    420     if ($changes{lastday}) { 
     420    my $firstday = exists($changes{firstday}) ? $changes{firstday} : $obj->get_attributes('firstday'); 
     421    my $lastday  = exists($changes{lastday}) ?  $changes{lastday}  : $obj->get_attributes('lastday'); 
     422 
     423    if ($lastday) { 
    421424        my $sth = $base->db->prepare_cached(q{ 
    422             select name from employment where "user" = ? and name != ? and 
    423                 firstday <= ? and (lastday is null or lastday >= ?) 
     425            select name, firstday, lastday from employment where "user" = ? and name != ? 
     426                and 
     427            ( 
     428            (firstday <= ? and (lastday is NULL or lastday >= ?)) 
     429                or 
     430            ((lastday is NOT NULL and lastday <= ?) and firstday >= ?) 
     431            ) 
    424432        }); 
    425         $sth->execute($user, $id, $changes{lastday}, $changes{lastday}); 
     433        $sth->execute($user, $id, $lastday, $firstday, $lastday, $firstday); 
    426434        my $res = $sth->fetchrow_hashref; 
    427435        $sth->finish; 
     436 
    428437        if ($res) { 
    429             $base->log(LA_ERR, "New ending overlap contrat %s (%s)", $res->{name}, $changes{lastday}); 
     438            $base->log(LA_ERR, "The change will overlap contrat %s (%s - %s)", $res->{name}, $res->{firstday}, $res->{lastday} || ''); 
    430439            return; 
    431440        } 
    432     } elsif(exists($changes{lastday})) { 
     441    } else { 
    433442        my $sth = $base->db->prepare_cached(q{ 
    434             select * from employment where "user" = ? and lastday is null 
     443            select * from employment where "user" = ? and name != ? 
     444            and lastday is NULL OR  lastday >= ? 
    435445            limit 1 
    436446        }); 
    437         $sth->execute($user); 
     447        $sth->execute($user, $id, $firstday); 
    438448        my $res = $sth->fetchrow_hashref; 
    439449        if ($res && $res->{name}) { 
     
    444454        } 
    445455    } 
    446     if ($changes{firstday}) { 
    447         my $sth = $base->db->prepare_cached(q{ 
    448             select name from employment where "user" = ? and name != ? and 
    449                 firstday <= ? and (lastday is null or lastday >= ?) 
    450         }); 
    451         $sth->execute($user, $id, $changes{firstday}, $changes{firstday}); 
    452         my $res = $sth->fetchrow_hashref; 
    453         $sth->finish; 
    454         if ($res) { 
    455             $base->log(LA_ERR, "New starting overlap contrat %s (%s)", $res->{name}, $changes{firstday}); 
    456             return; 
    457         } 
    458     } 
    459456 
    460457    return 1; 
Note: See TracChangeset for help on using the changeset viewer.