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

Last change on this file since 1950 was 1950, checked in by nanardon, 7 years ago

Cleanup useless code

File size: 12.5 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        previous => {
179            ro => 1,
180            managed => 1,
181            get => sub {
182                my ($self) = @_;
183                my $find = $self->object->base->db->prepare_cached(q{
184                    SELECT name from employment where "user" = ?
185                        and (lastday is not null and lastday < ?)
186                        order by lastday desc
187                });
188                $find->execute(
189                    $self->object->get_field('user'),
190                    $self->object->get_field('firstday')
191                );
192                my $res = $find->fetchrow_hashref;
193                $find->finish;
194                return $res->{name};
195            },
196            label => l('Previous'),
197        },
198        next => {
199            ro => 1,
200            managed => 1,
201            get => sub {
202                my ($self) = @_;
203                my $find = $self->object->base->db->prepare_cached(q{
204                    SELECT name from employment where "user" = ?
205                        and firstday > ?
206                        order by firstday asc
207                });
208                $find->execute(
209                    $self->object->get_field('user'),
210                    $self->object->get_field('lastday')
211                );
212                my $res = $find->fetchrow_hashref;
213                $find->finish;
214                return $res->{name};
215            },
216            label => l('Next'),
217        },
218        minFirstDay => {
219            ro => 1,
220            hidden => 1,
221            managed => 1,
222            formtype => 'DATE',
223            get => sub {
224                my ($attr) = @_;
225                my $self = $attr->object;
226
227                my $find = $self->base->db->prepare_cached(q{
228                    SELECT max(lastday) as lastday FROM employment where "user" = ?
229                        and lastday is not NULL and lastday < ?
230                    });
231
232                my $first = $self->get_field('firstday');
233
234                $find->execute(
235                    $self->get_field('user'),
236                    $first
237                );
238                my $res = $find->fetchrow_hashref;
239                $find->finish;
240
241                if ($res && $res->{lastday}) {
242                    my $dt = DateTime->from_epoch( epoch => str2time($res->{lastday}));
243                    $dt->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
244                    $dt->add(days => 1);
245                    if ($first eq $dt->ymd('-')) {
246                        return $first;
247                    } else {
248                        return $dt->ymd('-');
249                    }
250                } else {
251                    return;
252                }
253            },
254            label => l('Minimal first day'),
255        },
256        maxLastDay => {
257            ro => 1,
258            hidden => 1,
259            managed => 1,
260            formtype => 'DATE',
261            get => sub {
262                my ($attr) = @_;
263                my $self = $attr->object;
264
265                my $find = $self->base->db->prepare_cached(q{
266                    SELECT min(firstday) as firstday FROM employment where "user" = ?
267                        and firstday > ?
268                    });
269
270                my $last = $self->get_field('lastday') or do {
271                    # This contract is last then...
272                    return;
273                };
274
275                $find->execute(
276                    $self->get_field('user'),
277                    $last
278                );
279                my $res = $find->fetchrow_hashref;
280                $find->finish;
281
282                if ($res && $res->{firstday}) {
283                    my $dt = DateTime->from_epoch( epoch => str2time($res->{firstday}));
284                    $dt->set_time_zone( DateTime::TimeZone->new( name => 'local' ) );
285                    $dt->subtract(days => 1);
286                    if ($last eq $dt->ymd('-')) {
287                        return $last;
288                    } else {
289                        return $dt->ymd('-');
290                    }
291                } else {
292                    return;
293                }
294            },
295            label => l('Maximal last day'),
296        },
297    };
298
299    if (! $base->config("allow_pasted_employment")) {
300    # Completed contract are RO, we allow to still set lastday
301        foreach (qw(endcircuit firstday contratType department managerContact company employer)) {
302            $attrs->{$_}{ro} = sub {
303                my ($self) = $_[0];
304                $self or return 0;
305                my $st = $self->get_attributes('state') || '0';
306                return $st < 0 ? 1 : 0;
307            };
308        }
309    }
310    $class->SUPER::_get_attr_schema($base, $attrs);
311}
312
313sub _create {
314    my ($class, $base, $id, %data) = @_;
315    $data{user} or return;
316    my $user = $base->get_object('user', $data{user});
317    $user or return;
318    my $res = $class->SUPER::_create($base, $id, %data);
319    $user->applyCurrentEmployment;
320    $res;
321}
322
323sub _delete {
324    my ($class, $base, $id) = @_;
325
326    my $obj = $base->get_object($class->type, $id)
327        or return;
328
329    my $user = $obj->_get_attributes('user');
330
331    my $res = $class->SUPER::_delete($base, $id);
332    if ($res) {
333        my $ouser = $base->get_object('user', $user);
334        $ouser->applyCurrentEmployment;
335    }
336
337    $res
338}
339
340sub set_fields {
341    my ($self, %data) = @_;
342
343    my $res = $self->SUPER::set_fields(%data);
344
345    my $user = $self->base->get_object('user', $self->get_attributes('user')) or do {
346        $self->base->log(LA_ERR, "Cannot fetch user %s to apply employment", $self->get_attributes('user'));
347        return;
348    };
349
350    $user->applyCurrentEmployment;
351
352    return $res;
353}
354
355sub checkValues {
356    my ($class, $base, $obj, %changes) = @_;
357
358    my $user = $changes{user} || $obj->get_attributes('user');
359    my $id = ref $obj ? $obj->id : $obj;
360
361    my $firstday = exists($changes{firstday}) ? $changes{firstday} : $obj->get_attributes('firstday');
362    my $lastday  = exists($changes{lastday}) ?  $changes{lastday}  : $obj->get_attributes('lastday');
363
364    if ($lastday) {
365        my $sth = $base->db->prepare_cached(q{
366            select name, firstday, lastday from employment where "user" = ? and name != ?
367                and
368            (
369            (firstday <= ? and (lastday is NULL or lastday >= ?))
370                or
371            ((lastday is NOT NULL and lastday <= ?) and firstday >= ?)
372            )
373        });
374        $sth->execute($user, $id, $lastday, $firstday, $lastday, $firstday);
375        my $res = $sth->fetchrow_hashref;
376        $sth->finish;
377
378        if ($res) {
379            $base->log(LA_ERR, "The change will overlap contrat %s (%s - %s)", $res->{name}, $res->{firstday}, $res->{lastday} || '');
380            return;
381        }
382    } else {
383        my $sth = $base->db->prepare_cached(q{
384            select * from employment where "user" = ? and name != ?
385            and (lastday is NULL OR lastday >= ?)
386            limit 1
387        });
388        $sth->execute($user, $id, $firstday);
389        my $res = $sth->fetchrow_hashref;
390        if ($res && $res->{name}) {
391            if ($id ne $res->{name}) {
392                $base->log(LA_ERR, "Another contract has no ending (%s)", $res->{name} || '');
393                return;
394            }
395        }
396    }
397
398    return 1;
399}
400
4011;
402
403__END__
404
405=head1 SEE ALSO
406
407L<LATMOS::Accounts::Bases::Sql>
408
409=head1 AUTHOR
410
411Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
412
413=head1 COPYRIGHT AND LICENSE
414
415Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
416
417This library is free software; you can redistribute it and/or modify
418it under the same terms as Perl itself, either Perl version 5.10.0 or,
419at your option, any later version of Perl 5 you may have available.
420
421
422=cut
Note: See TracBrowser for help on using the repository browser.