eb2b4739a369b4dc0f788523301f5c964a7aee66
[kivitendo-erp.git] / SL / Controller / Chart.pm
1 package SL::Controller::Chart;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use Clone qw(clone);
7 use List::UtilsBy qw(partition_by sort_by);
8
9 use SL::AM;
10 use SL::DB::Chart;
11 use SL::Controller::Helper::GetModels;
12 use SL::Locale::String qw(t8);
13 use SL::JSON;
14
15 use Rose::Object::MakeMethods::Generic (
16   'scalar --get_set_init' => [ qw(charts models chart filter) ],
17 );
18
19 sub action_ajax_autocomplete {
20   my ($self, %params) = @_;
21
22   my $value = $::form->{column} || 'description';
23
24   # if someone types something, and hits enter, assume he entered the full name.
25   # if something matches, treat that as sole match
26   # unfortunately get_models can't do more than one per package atm, so we do it
27   # the oldfashioned way.
28   if ($::form->{prefer_exact}) {
29     my $exact_matches;
30     # we still need the type filter so that we can't choose an illegal chart
31     # via exact_match if we have preset a link type, e.g. AR_paid
32     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Chart->get_all(
33       query => [
34         SL::DB::Manager::Chart->type_filter($::form->{filter}{type}),
35         charttype => 'A',
36         or => [
37           description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
38           accno       => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
39         ]
40       ],
41       limit => 2,
42     ) }) {
43       $self->charts($exact_matches);
44     }
45   }
46
47   my @hashes = map {
48    +{
49      value       => $_->displayable_name,
50      label       => $_->displayable_name,
51      id          => $_->id,
52      accno       => $_->accno,
53      description => $_->description,
54     }
55   } @{ $self->charts }; # neato: if exact match triggers we don't even need the init_parts
56
57   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
58 }
59
60 sub action_test_page {
61   $_[0]->render('chart/test_page', pre_filled_chart => SL::DB::Manager::Chart->get_first);
62 }
63
64 sub action_chart_picker_search {
65   $_[0]->render('chart/chart_picker_search', { layout => 0 }, charts => $_[0]->charts);
66 }
67
68 sub action_chart_picker_result {
69   $_[0]->render('chart/_chart_picker_result', { layout => 0 });
70 }
71
72 sub action_show {
73   my ($self) = @_;
74
75   if ($::request->type eq 'json') {
76     my $chart_hash;
77     if (!$self->chart) {
78       # TODO error
79     } else {
80       $chart_hash                     = $self->chart->as_tree;
81       $chart_hash->{displayable_name} = $self->chart->displayable_name;
82     }
83
84     $self->render(\ SL::JSON::to_json($chart_hash), { layout => 0, type => 'json', process => 0 });
85   }
86 }
87
88 sub action_show_report_configuration_overview {
89   my ($self) = @_;
90
91   my @all_charts = sort { $a->accno cmp $b->accno } @{ SL::DB::Manager::Chart->get_all(inject_results => 1) };
92   my @types      = qw(bilanz bwa er eur);
93   my %headings   = (
94     bilanz       => t8('Balance'),
95     bwa          => t8('BWA'),
96     er           => t8('Erfolgsrechnung'),
97     eur          => t8('EUER'),
98   );
99
100   my @data;
101
102   foreach my $type (@types) {
103     my $method = "pos_${type}";
104     my $names  = $type eq 'bwa' ? AM->get_bwa_categories(\%::myconfig, $::form)
105                : $type eq 'eur' ? AM->get_eur_categories(\%::myconfig, $::form)
106                :                  {};
107     my %charts = partition_by { $_->$method // '' } @all_charts;
108     delete $charts{''};
109
110     next if !%charts;
111
112     push @data, {
113       type      => $type,
114       heading   => $headings{$type},
115       charts    => \%charts,
116       positions => [ sort { ($a * 1) <=> ($b * 1) } keys %charts ],
117       names     => $names,
118     };
119   }
120
121   $self->render('chart/report_configuration_overview', DATA => \@data);
122 }
123
124 sub init_charts {
125
126   # disable pagination when hiding chart details = paginate when showing chart details
127   if ($::form->{hide_chart_details}) {
128     $_[0]->models->disable_plugin('paginated');
129   }
130
131   $_[0]->models->get;
132 }
133
134 sub init_chart {
135   SL::DB::Chart->new(id => $::form->{id} || $::form->{chart}{id})->load;
136 }
137
138 sub init_models {
139   my ($self) = @_;
140
141   SL::Controller::Helper::GetModels->new(
142     controller => $self,
143     sorted => {
144       _default  => {
145         by  => 'accno',
146         dir => 1,
147       },
148       accno       => t8('Account number'),
149       description => t8('Description'),
150     },
151     query => [
152       charttype => 'A',
153     ],
154   );
155 }
156
157 sub init_filter { $_[0]->models->filtered->laundered }
158
159 1;