source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm @ 2204

Last change on this file since 2204 was 2204, checked in by nanardon, 5 years ago

Track parent object in SQL log

File size: 13.1 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Employment;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Sql::objects);
8use LATMOS::Accounts::Log;
9use LATMOS::Accounts::I18N;
10use Date::Parse;
11use DateTime;
12use Date::Calc;
13
14our $VERSION = (q$Rev: 594 $ =~ /^Rev: (\d+) /)[0];
15
16=head1 NAME
17
18LATMOS::Accounts::Bases::Sql::Address - Physical office Adress Support
19
20=head1 DESCRIPTION
21
22C<Address> objects allow to store user's several office addresses.
23
24Notice most of other bases (Ldap, ...) support only one address.
25
26=cut
27
28sub stringify {
29    my ($self) = @_;
30
31    my $user = $self->base->get_object('user', $self->_get_c_field('user'))
32        or return $self->id;
33
34    return join(', ', grep { $_ }
35        (
36            $user,
37            $self->get_c_field('firstday'),
38        )
39    );
40}
41
42sub _object_table { 'employment' }
43
44sub _key_field { 'name' }
45
46sub _has_extended_attributes { 1 }
47
48sub _get_attr_schema {
49    my ($class, $base) = @_;
50
51    my $attrs = {
52        name =>         { inline => 1, },
53        exported =>     { inline => 1, },
54        user =>         { inline => 1,
55            reference => 'user',
56            label => l('User'),
57        },
58        description => {
59            label => l('Description'),
60        },
61        firstday => {
62            inline => 1,
63            formtype => 'DATE',
64            monitored => 1,
65            label => l('Contract start'),
66        },
67        lastday => {
68            inline => 1,
69            formtype => 'DATE',
70            monitored => 1,
71            label => l('Contract end'),
72        },
73        length => {
74            ro => 1,
75            managed => 1,
76            get => sub {
77                my ($self) = @_;
78                my $lastday = $self->object->get_attributes('lastday') || DateTime->now->ymd('-');
79                my $firstday = $self->object->get_attributes('firstday');
80
81                my @fd = split('-', $firstday);
82                my @ld = split('-', $lastday);
83
84                return Date::Calc::Delta_Days(@fd, @ld) +1;
85            },
86            label => l('Length'),
87        },
88        lengthText => {
89            ro => 1,
90            managed => 1,
91            get => sub {
92                my ($self) = @_;
93                my $lastday = $self->object->get_attributes('lastday')|| DateTime->now->ymd('-');
94                {
95                    my $dtlast = DateTime->from_epoch(epoch => str2time($lastday));
96                    $dtlast->add(days => 1);
97                    $lastday = $dtlast->ymd('-');
98                }
99                my $firstday = $self->object->get_attributes('firstday');
100
101                return if ($firstday gt $lastday);
102
103                my @fd = split('-', $firstday) or return;
104                my @ld = split('-', $lastday)  or return;
105
106                my ($Dy,$Dm,$Dd) = Date::Calc::N_Delta_YMD(@fd, @ld);
107                return join(', ',
108                    ($Dy ? l('%d years', $Dy)  : ()),
109                    ($Dm ? l('%d months', $Dm) : ()),
110                    ($Dd ? l('%d days', $Dd)   : ()),
111                );
112            },
113            label => l('Length'),
114        },
115        'state' => {
116            managed => 1,
117            ro => 1,
118            get => sub {
119                my ($self) = @_;
120                my $now = DateTime->now;
121                if ($now->epoch < str2time($self->object->get_attributes('firstday'))) {
122                    return 1;
123                } elsif ( my $end = $self->object->get_attributes('lastday') ) {
124                    my $eend = str2time($end) + 86400;
125                    if ($now->epoch > $eend) {
126                        return -1;
127                    } else {
128                        return 0;
129                    }
130                } else {
131                    return 0;
132                }
133            },
134            label => l('State'),
135        },
136        contratType => {
137            reference => 'group',
138            can_values => sub {
139                $base->search_objects('group', 'sutype=contrattype')
140            },
141            monitored => 1,
142            label => l('Type of contract'),
143        },
144        managerContact => {
145            delayed => 1,
146            can_values => sub {
147                my %uniq = map { $_ => 1 } grep { $_ }
148                ($_[1] ? $_[1]->get_attributes('managerContact') : ()),
149                $base->search_objects('user', 'active=*');
150                sort keys %uniq;
151            },
152            reference => 'user',
153            label => l('Direct manager'),
154        },
155        department => {
156            reference => 'group',
157            can_values => sub {
158                $base->search_objects('group', 'sutype=dpmt')
159            },
160            monitored => 1,
161            label => l('Department'),
162        },
163        company => {
164            label => l('Company'),
165        },
166        employer => {
167            label => l('Employer'),
168        },
169        endcircuit    => {
170            formtype => 'DATE',
171            monitored => 1,
172            label => l('End of entrance circuit'),
173        },
174        hosted => {
175            formtype => 'CHECKBOX',
176            label => l('Hosted'),
177        },
178        assigned => {
179            formtype => 'CHECKBOX',
180            label => l('Assigned'),
181        },
182        requestId => {
183            label => l('Request id')
184        },
185        previous => {
186            ro => 1,
187            managed => 1,
188            get => sub {
189                my ($self) = @_;
190                my $find = $self->object->base->db->prepare_cached(q{
191                    SELECT name from employment where "user" = ?
192                        and (lastday is not null and lastday < ?)
193                        order by lastday desc
194                });
195                $find->execute(
196                    $self->object->get_field('user'),
197                    $self->object->get_field('firstday')
198                );
199                my $res = $find->fetchrow_hashref;
200                $find->finish;
201                return $res->{name};
202            },
203            label => l('Previous'),
204        },
205        'next' => {
206            ro => 1,
207            managed => 1,
208            get => sub {
209                my ($self) = @_;
210                my $find = $self->object->base->db->prepare_cached(q{
211                    SELECT name from employment where "user" = ?
212                        and firstday > ?
213                        order by firstday asc
214                });
215                $find->execute(
216                    $self->object->get_field('user'),
217                    $self->object->get_field('lastday')
218                );
219                my $res = $find->fetchrow_hashref;
220                $find->finish;
221                return $res->{name};
222            },
223            label => l('Next'),
224        },
225        minFirstDay => {
226            ro => 1,
227            hidden => 1,
228            managed => 1,
229            formtype => 'DATE',
230            get => sub {
231                my ($attr) = @_;
232                my $self = $attr->object;
233
234                my $find = $self->base->db->prepare_cached(q{
235                    SELECT max(lastday) as lastday FROM employment where "user" = ?
236                        and lastday is not NULL and lastday < ?
237                    });
238
239                my $first = $self->get_field('firstday');
240
241                $find->execute(
242                    $self->get_field('user'),
243                    $first
244                );
245                my $res = $find->fetchrow_hashref;
246                $find->finish;
247
248                if ($res && $res->{lastday}) {
249                    my $dt = DateTime->from_epoch( epoch => str2time($res->{lastday}));
250                    $dt->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
251                    $dt->add(days => 1);
252                    if ($first eq $dt->ymd('-')) {
253                        return $first;
254                    } else {
255                        return $dt->ymd('-');
256                    }
257                } else {
258                    return;
259                }
260            },
261            label => l('Minimal first day'),
262        },
263        maxLastDay => {
264            ro => 1,
265            hidden => 1,
266            managed => 1,
267            formtype => 'DATE',
268            get => sub {
269                my ($attr) = @_;
270                my $self = $attr->object;
271
272                my $find = $self->base->db->prepare_cached(q{
273                    SELECT min(firstday) as firstday FROM employment where "user" = ?
274                        and firstday > ?
275                    });
276
277                my $last = $self->get_field('lastday') or do {
278                    # This contract is last then...
279                    return;
280                };
281
282                $find->execute(
283                    $self->get_field('user'),
284                    $last
285                );
286                my $res = $find->fetchrow_hashref;
287                $find->finish;
288
289                if ($res && $res->{firstday}) {
290                    my $dt = DateTime->from_epoch( epoch => str2time($res->{firstday}));
291                    $dt->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
292                    $dt->subtract(days => 1);
293                    if ($last eq $dt->ymd('-')) {
294                        return $last;
295                    } else {
296                        return $dt->ymd('-');
297                    }
298                } else {
299                    return;
300                }
301            },
302            label => l('Maximal last day'),
303        },
304    };
305
306    if (! $base->config("allow_pasted_employment")) {
307    # Completed contract are RO, we allow to still set lastday
308        foreach (qw(endcircuit firstday contratType department managerContact company employer)) {
309            $attrs->{$_}{ro} = sub {
310                my ($self) = $_[0];
311                $self or return 0;
312                my $st = $self->get_attributes('state') || '0';
313                return $st < 0 ? 1 : 0;
314            };
315        }
316    }
317    $class->SUPER::_get_attr_schema($base, $attrs);
318}
319
320sub ParentObject {
321    my ($self) = @_;
322
323    return $self->base->get_object('user', $self->_get_attributes('user'));
324}
325
326sub _create {
327    my ($class, $base, $id, %data) = @_;
328    $data{user} or return;
329    my $user = $base->get_object('user', $data{user});
330    $user or return;
331    my $res = $class->SUPER::_create($base, $id, %data);
332    $user->applyCurrentEmployment;
333    $res;
334}
335
336sub _delete {
337    my ($class, $base, $id) = @_;
338
339    my $obj = $base->get_object($class->type, $id)
340        or return;
341
342    my $user = $obj->_get_attributes('user');
343
344    my $res = $class->SUPER::_delete($base, $id);
345    if ($res) {
346        my $ouser = $base->get_object('user', $user);
347        $ouser->applyCurrentEmployment;
348    }
349
350    $res
351}
352
353sub _set_c_fields {
354    my ($self, %data) = @_;
355
356    my $res = $self->SUPER::_set_c_fields(%data);
357
358    if ($res) {
359        my $user = $self->base->get_object('user', $self->get_attributes('user')) or do {
360            $self->base->log(LA_ERR, "Cannot fetch user %s to apply employment", $self->get_attributes('user'));
361            return;
362        };
363
364        $user->applyCurrentEmployment;
365    }
366
367    return $res;
368}
369
370sub checkValues {
371    my ($class, $base, $obj, %changes) = @_;
372
373    my $user = $changes{user} || $obj->get_attributes('user');
374    my $id = ref $obj ? $obj->id : $obj;
375
376    my $firstday = exists($changes{firstday}) 
377        ? $changes{firstday} : 
378            ref $obj ? $obj->get_attributes('firstday') : undef;
379    my $lastday  = exists($changes{lastday})
380        ?  $changes{lastday} :
381            ref $obj ? $obj->get_attributes('lastday') : undef;
382
383    if ($lastday) {
384        my $sth = $base->db->prepare_cached(q{
385            select name, firstday, lastday from employment where "user" = ? and name != ?
386                and
387            (
388            (firstday <= ? and (lastday is NULL or lastday >= ?))
389                or
390            ((lastday is NOT NULL and lastday <= ?) and firstday >= ?)
391            )
392        });
393        $sth->execute($user, $id, $lastday, $firstday, $lastday, $firstday);
394        my $res = $sth->fetchrow_hashref;
395        $sth->finish;
396
397        if ($res) {
398            $base->log(LA_ERR, "The change will overlap contrat %s (%s - %s)", $res->{name}, $res->{firstday}, $res->{lastday} || '');
399            return;
400        }
401    } elsif (!$firstday) {
402        $base->log(LA_ERR, "No firstday given");
403        return;
404    } else {
405        my $sth = $base->db->prepare_cached(q{
406            select * from employment where "user" = ? and name != ?
407            and (lastday is NULL OR lastday >= ?)
408            limit 1
409        });
410        $sth->execute($user, $id, $firstday);
411        my $res = $sth->fetchrow_hashref;
412        if ($res && $res->{name}) {
413            if ($id ne $res->{name}) {
414                $base->log(LA_ERR, "Another contract has no ending (%s)", $res->{name} || '');
415                return;
416            }
417        }
418    }
419
420    return 1;
421}
422
4231;
424
425__END__
426
427=head1 SEE ALSO
428
429L<LATMOS::Accounts::Bases::Sql>
430
431=head1 AUTHOR
432
433Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
434
435=head1 COPYRIGHT AND LICENSE
436
437Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
438
439This library is free software; you can redistribute it and/or modify
440it under the same terms as Perl itself, either Perl version 5.10.0 or,
441at your option, any later version of Perl 5 you may have available.
442
443
444=cut
Note: See TracBrowser for help on using the repository browser.