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