1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use LATMOS::Accounts::I18N; |
---|
9 | use Date::Parse; |
---|
10 | use DateTime; |
---|
11 | |
---|
12 | =head1 NAME |
---|
13 | |
---|
14 | la-query - Tools to query base in LATMOS::Accounts system |
---|
15 | |
---|
16 | =head1 SYNOPSIS |
---|
17 | |
---|
18 | la-query [options] [obj_id] |
---|
19 | |
---|
20 | =head1 DESCRIPTION |
---|
21 | |
---|
22 | [obj_id] : If present, all set attributes (rw) will be displayed for that obj_id. |
---|
23 | If none is given, all obj_ids will be printed. |
---|
24 | |
---|
25 | For the default object_type (user), obj_id = login. |
---|
26 | |
---|
27 | Example : la-query lambda |
---|
28 | |
---|
29 | =cut |
---|
30 | |
---|
31 | GetOptions( |
---|
32 | 'c|config=s' => \my $config, |
---|
33 | 'b|base=s' => \my $base, |
---|
34 | 'd=i' => \my $delay, |
---|
35 | 'help' => sub { pod2usage(0) }, |
---|
36 | ) or pod2usage(); |
---|
37 | |
---|
38 | my $otype = 'user'; |
---|
39 | $delay ||= 60; # Jours |
---|
40 | |
---|
41 | =head1 OPTIONS |
---|
42 | |
---|
43 | =over 4 |
---|
44 | |
---|
45 | =item -c|--config configdir |
---|
46 | |
---|
47 | Use this configuration directory instead of the default one. |
---|
48 | |
---|
49 | =item -b|--base basename |
---|
50 | |
---|
51 | Query this specific base instead of the default one. |
---|
52 | |
---|
53 | =item -d days |
---|
54 | |
---|
55 | The delay for new employment in days |
---|
56 | |
---|
57 | =back |
---|
58 | |
---|
59 | =cut |
---|
60 | |
---|
61 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
62 | my $labase = $LA->base($base); |
---|
63 | $labase && $labase->load or die l("Cannot load base %s\n", $base); |
---|
64 | |
---|
65 | $labase->unexported(0); |
---|
66 | |
---|
67 | foreach my $user (@ARGV) { |
---|
68 | my $ouser = $labase->get_object($otype, $user) or die "Cannot find user $user\n"; |
---|
69 | |
---|
70 | my @employements = $ouser->listEmployment; |
---|
71 | |
---|
72 | my $lastemp = $employements[0] or do { |
---|
73 | warn "$user has no employment\n"; |
---|
74 | next; |
---|
75 | }; |
---|
76 | |
---|
77 | my $oemp = $labase->get_object('employment', $lastemp); |
---|
78 | |
---|
79 | my $lastday = $oemp->get_attributes('lastday') or do { |
---|
80 | warn "$user has unlimited employement\n"; |
---|
81 | next; |
---|
82 | }; |
---|
83 | |
---|
84 | my %newvalues = ( user => $user ); |
---|
85 | foreach (qw(company managerContact contratType department)) { |
---|
86 | $newvalues{$_} = $oemp->get_attributes($_); |
---|
87 | } |
---|
88 | my $time = DateTime->from_epoch(epoch => str2time($lastday)); |
---|
89 | $time->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
90 | $time->add( days => 1 ); |
---|
91 | $newvalues{firstday} = $time->ymd('-'); |
---|
92 | my $name = "$user-$newvalues{firstday}"; |
---|
93 | $time->add( days => $delay ); |
---|
94 | $newvalues{lastday} = $time->ymd('-'); |
---|
95 | |
---|
96 | $labase->create_object('employment', $name, %newvalues); |
---|
97 | |
---|
98 | print "$user: new end $newvalues{lastday} ($newvalues{contratType})\n"; |
---|
99 | $labase->commit; |
---|
100 | } |
---|
101 | |
---|