forked from thiagorondon/CMD
/
Root.pm
172 lines (132 loc) · 4.33 KB
/
Root.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package CMD::Controller::Root;
use Moose;
use namespace::autoclean;
use Scalar::Util qw(looks_like_number);
use CMD::Utils qw(formata_real formata_valor formata_float bgcolor);
use CMD::Data::Utils qw(get_root_by_nodeid);
BEGIN { extends 'Catalyst::Controller' }
#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config( namespace => '' );
=head1 NAME
CMD::Controller::Root - Root Controller for CMD
=head1 DESCRIPTION
[enter your description here]
=head1 METHODS
=head2 index
The root page (/)
=cut
sub base : Chained('/') PathPart('') CaptureArgs(0) {
my ( $self, $c ) = @_;
$c->stash->{current_model} = 'DB';
# warning to add something in stash here, because json output.
}
sub root : Chained('base') PathPart('') Args(0) {
my ( $self, $c ) = @_;
$c->stash->{bases} = $c->model('DB::Base');
$c->res->redirect( '/node/1' );
}
sub faq : Chained('base') Args(0) {
}
sub contato : Chained('base') Args(0) {
}
sub todo : Chained('base') Args(0) {
}
sub node : Chained('base') Args(1) {
my ( $self, $c, $node ) = @_;
$c->stash->{node} = $node;
$c->stash->{objnode} = $c->model('DB::Node')->find($node);
$c->stash->{rootnode} = my $root = get_root_by_nodeid($c->stash->{objnode});
$c->stash->{bases} = $c->model('DB::Base');
}
sub programa : Chained('base') Args(1) {
my ( $self, $c, $id ) = @_;
$c->stash->{bases} = $c->model('DB::Base');
my $rs = $c->model( 'DBUTF8::Node');
my $collection = $rs->find( $id ) or $c->detach('/');
my $tt = '2010'; #c->stash->{tt};
my %yyout;
foreach my $yy (2006 .. 2010) {
$yyout{$yy} =
$rs->search_rs({ codigo => $collection->codigo,
funcao => $collection->funcao, subfuncao => $collection->subfuncao,
ano => $yy })->get_column('valor')->sum;
}
my $total = $rs->search_rs({ codigo => $collection->codigo,
funcao => $collection->funcao, subfuncao => $collection->subfuncao,
ano => $tt
})
->get_column('valor')->sum;
my $total_direto = $rs->search_rs({ codigo => $collection->codigo,
funcao => $collection->funcao, subfuncao => $collection->subfuncao,
estado => undef,
ano => $tt
})->get_column('valor')->sum;
my $total_repasse = $rs->search_rs({ codigo => $collection->codigo,
funcao => $collection->funcao, subfuncao => $collection->subfuncao,
estado => { '!=', undef },
ano => $tt
},
)->get_column('valor')->sum;
my $objs = $rs->search({ codigo => $collection->codigo,
funcao => $collection->funcao, subfuncao => $collection->subfuncao,
estado => { '!=' => undef },
ano => $tt
});
my %estado;
foreach my $item ($objs->all) {
next unless $item->valor or $item->estado;
if ( grep(keys %estado, $item->estado) ) {
$estado{$item->estado} += $item->valor;
} else {
$estado{$item->estado} = $item->valor;
}
}
my @estados;
foreach my $item (keys %estado) {
my $ufname = $item;
my $pc = $estado{$item} * 100 / $total;
$pc = formata_float($pc, 2);
#$ufname =~ s/([\w']+)/\u\L$1/g;
push(@estados, {
nome => $ufname . " ($pc\%)",
total => $estado{$item},
total_print => formata_real($estado{$item}) } );
}
$c->stash(
id => $id,
collection => $collection,
total => formata_real($total),
total_direto => formata_real($total_direto),
total_repasse => formata_real($total_repasse),
raw_total_direto => $total_direto || 0,
raw_total_repasse => $total_repasse || 0,
raw_total => $total || 0,
estados => [ @estados ],
yyout => \%yyout
);
}
=head2 default
Standard 404 error page
=cut
sub error_404 : Chained('base') PathPart('') Args {
my ( $self, $c ) = @_;
# $c->response->body('Page not found');
# $c->response->status(404);
$c->res->redirect('/');
}
=head2 end
Attempt to render a view, if needed.
=cut
sub end : ActionClass('RenderView') {
}
=head1 AUTHOR
Thiago Rondon
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
__PACKAGE__->meta->make_immutable;
1;