source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Json/Employment.pm @ 1676

Last change on this file since 1676 was 1676, checked in by nanardon, 8 years ago

always add today on graph

File size: 2.6 KB
Line 
1package LATMOS::Accounts::Web::Controller::Json::Employment;
2use Moose;
3use namespace::autoclean;
4
5use DateTime;
6
7BEGIN { extends 'Catalyst::Controller'; }
8
9=head1 NAME
10
11LATMOS::Accounts::Web::Controller::Json::Employment - Catalyst Controller
12
13=head1 DESCRIPTION
14
15Catalyst Controller.
16
17=head1 METHODS
18
19=cut
20
21
22=head2 index
23
24=cut
25
26sub index :Path :Args(0) {
27    my ( $self, $c ) = @_;
28
29    my $base = $c->model('Accounts')->db;
30
31    my %employments;
32    my $now = DateTime->now->ymd;
33
34    my $key = $c->req->param('key') || 'department';
35
36    my %end = $base->attributes_summary_by_object('employment', 'lastday');
37    my %start = $base->attributes_summary_by_object('employment', 'firstday');
38    my %keys = $base->attributes_summary_by_object('employment', $key);
39
40    my %keysentry;
41    foreach my $emp (keys %start) {
42        # We filter pasted employment
43        next if ($end{$emp}[0] && $end{$emp}[0] lt $now);
44
45        $employments{ $emp } = {
46           start => $start{$emp}[0],
47           end   => $end  {$emp}[0],
48           key   => $keys {$emp}[0],
49       };
50       $keysentry{$keys {$emp}[0] || $c->loc('(none)') } = [];
51    }
52
53    my %done;
54
55    my @dates =
56        map { $employments{$_}->{end}  }
57        grep { $employments{$_}->{end} } (keys %employments);
58
59    if (!@dates) {
60        foreach (1 .. 12) {
61            push(@dates, DateTime->now->add(months => $_)->ymd);
62        }
63    }
64
65    push(@dates, $now);
66
67    foreach my $emp (sort { $a cmp $b } @dates) {
68        $done{$emp} and next;
69        $done{$emp} = 1;
70
71        my %sum = map { $_ => 0 } keys %keysentry;
72        $sum{ $c->loc('(none)') } = 0;
73
74        foreach my $cemp (keys %employments) {
75            #warn "$cemp $emp" . $employments{$cemp}->{start} . " " . $employments{$cemp}->{end} ;
76            if ($employments{$cemp}->{start} le $emp && ((!$employments{$cemp}->{end}) || $employments{$cemp}->{end} ge $emp)) {
77                my $k = $employments{$cemp}->{key} || $c->loc('(none)');
78                $sum{$k} ++;
79            }
80        }
81
82        foreach (keys %keysentry) {
83           push (@{ $keysentry{$_} }, [ $emp, $sum{$_}+0 ]);
84       } 
85    }
86
87    my $data;
88    foreach my $k (sort { $keysentry{$a}->[0][1] <=> $keysentry{$b}->[0][1] } keys %keysentry) {
89        push(@{ $data->{data} }, $keysentry{$k});
90        push(@{ $data->{series} }, { label => $k || $c->loc('(none)') });
91    }
92
93    $c->stash->{data} = $data;
94
95}
96
97
98
99
100
101
102=encoding utf8
103
104=head1 AUTHOR
105
106Olivier Thauvin,Guyancourt - B1428,+33 1 80285052,
107
108=head1 LICENSE
109
110This library is free software. You can redistribute it and/or modify
111it under the same terms as Perl itself.
112
113=cut
114
115__PACKAGE__->meta->make_immutable;
116
1171;
Note: See TracBrowser for help on using the repository browser.