Changeset 1500 for trunk/LATMOS-Accounts/lib/LATMOS/Accounts
- Timestamp:
- 12/18/15 07:25:11 (9 years ago)
- 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 392 392 return; 393 393 }; 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 394 403 if ($pclass->_create($self, $id, %data)) { 395 404 la_log(LA_INFO, … … 404 413 return; 405 414 }; 415 warn $id; 406 416 $self->get_object($otype, $id); 407 417 } -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm
r1453 r1500 320 320 =cut 321 321 322 =head2 checkValues ($base, $obj, %attributes) 323 324 Allow to pre-check values when object are modified or created 325 326 C<$obj> is either the new id at object creation or the object itself on modification. 327 328 =cut 329 330 sub checkValues { 331 my ($class, $base, $obj, %attributes) = @_; 332 333 return 1; 334 } 335 322 336 =head2 check_allowed_values ($attr, $values) 323 337 … … 369 383 }; 370 384 } 385 371 386 $self->_set_c_fields(%cdata); 372 387 } … … 399 414 return; 400 415 }; 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; 401 424 } 402 425 -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm
r1489 r1500 6 6 7 7 use base qw(LATMOS::Accounts::Bases::Sql::objects); 8 use LATMOS::Accounts::Log; 9 use Date::Parse; 10 use DateTime; 8 11 9 12 our $VERSION = (q$Rev: 594 $ =~ /^Rev: (\d+) /)[0]; … … 30 33 ( 31 34 $user, 32 $self->get_c_field(' l'),35 $self->get_c_field('firstday'), 33 36 ) 34 37 ); … … 52 55 }, 53 56 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 }, 56 85 contratType => { 57 86 reference => 'group', … … 61 90 monitored => 1, 62 91 }, 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, }, 63 111 } 64 112 ); … … 73 121 } 74 122 123 sub 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 162 sub 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 212 sub ReportChange { 213 my ($self, $changetype, $message, @args) = @_; 214 215 $self->applyToUser(); 216 217 $self->SUPER::ReportChange($changetype, $message, @args); 218 } 219 75 220 1; 76 221 -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm
r1498 r1500 9 9 use LATMOS::Accounts::Log; 10 10 use POSIX qw(strftime); 11 use Date::Parse qw(str2time); 12 use DateTime; 13 use DateTime::TimeZone; 11 14 use base qw(LATMOS::Accounts::Bases::Sql::objects); 12 15 … … 106 109 : ($value ? 1 : 0 ); 107 110 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) { 109 112 my $obj = $self->base->get_object('aliases', $al) or next; 110 113 $obj->_set_c_fields( … … 193 196 monitored => 1, 194 197 }, 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 }, 197 234 cn => { 198 235 inline => 1, ro => 1, … … 874 911 q{ 875 912 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" = ? 877 914 limit 1 878 915 } … … 887 924 } 888 925 } 926 }, 927 appliedEmployement => { 928 hidden => 1, 929 reference => 'employment', 889 930 }, 890 931 }
Note: See TracChangeset
for help on using the changeset viewer.