source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Employment.pm @ 2325

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

Fix: name of function

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1package LATMOS::Accounts::Task::Employment;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts::Task);
6use LATMOS::Accounts;
7use LATMOS::Accounts::Log;
8use LATMOS::Accounts::Utils;
9use LATMOS::Accounts::Bases::Sql::Employment;
10use DateTime;
11use DateTime::TimeZone;
12
13=head1 NAME
14
15LATMOS::Accounts::Task::Stats - Store value to perform some statistics
16
17=head1 DESCRIPTION
18
19=cut
20
21sub order { 0 }
22
23sub init {
24    my ($self) = @_;
25    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
26    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
27    $labase && $labase->load or die "Cannot load base";
28
29    $self->{_la} = $LA;
30    $self->{_base} = $labase;
31
32    1;
33}
34
35# Run it every 10 minutes, not more often
36sub runDelay { 10 * 60 }
37
38sub run {
39    my ($self) = @_;
40
41
42    # Export user is employement is incoming
43    foreach my $user ($self->{_base}->search_objects('user',
44            'exported=0',
45            'arrival>now()'
46        )) {
47            my $ouser = $self->{_base}->get_object('user', $user);
48            if ($ouser->_set_c_fields( 'exported', 1 )) {
49                $self->{_base}->commit;
50            } else {
51                $self->{_base}->rollback;
52            }
53    }
54
55    my %users = map { $_ => undef } $self->{_base}->search_objects('user', 'exported=true');
56    my %old = $self->{_base}->attributes_summary_by_object('user', 'appliedEmployement');
57    foreach (keys %old) {
58        exists($users{$_}) or next;
59        $users{$_} = $old{$_}->[0] || '';
60    }
61
62    my $now = DateTime->now()->iso8601 . 'Z';
63
64    my $listempl = $self->{_base}->db->prepare_cached(q{
65        SELECT * FROM employment where firstday <= ?::timestamp and (lastday is null or lastday >= ?::timestamp - '1 days'::interval)
66            and exported = true
67            order by lastday, firstday
68    });
69    $listempl->execute($now, $now);
70    my %new  = ();
71    while (my $res = $listempl->fetchrow_hashref) {
72        my $employment = $res->{name};
73        my $user = $res->{user};
74        exists($users{$user}) or next;
75        # avoid undef:
76        $users{$user} ||= '';
77        if ($employment ne $users{$user}) {
78            my $ouser = $self->{_base}->get_object('user', $user) or next;
79
80            $self->{_base}->log(LA_NOTICE, "Updating user $user to match employment `%s'", $employment || '');
81            $ouser->applyCurrentEmployment;
82            $self->{_base}->commit;
83        }
84        delete($users{$user});
85    }
86
87    # Change has old employment => nothing
88    # if user is still active, expired account remains untouched
89    foreach my $user ($self->{_base}->search_objects('user', 'exported=true')) {
90        if ($users{$user}) {
91            my $ouser = $self->{_base}->get_object('user', $user);
92            if ($ouser->resetEmployment) {
93                $self->{_base}->commit;
94            } else {
95                $self->{_base}->rollback;
96            }
97        }
98    }
99
100    return 1;
101}
102
1031;
104
105__END__
106
107=head1 SEE ALSO
108
109L<LATMOS::Accounts>, L<LATMOS::Accounts::Task>
110
111=head1 AUTHOR
112
113Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
114
115=head1 COPYRIGHT AND LICENSE
116
117Copyright (C) 2012 CNRS SA/CETP/LATMOS
118
119This library is free software; you can redistribute it and/or modify
120it under the same terms as Perl itself, either Perl version 5.10.0 or,
121at your option, any later version of Perl 5 you may have available.
122
123=cut
124
Note: See TracBrowser for help on using the repository browser.