]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/Chart.pm
kivitendo 3.9.2-0.2
[mfinanz.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   my @all_charts = sort { $a->accno cmp $b->accno } @{ SL::DB::Manager::Chart->get_all(inject_results => 1) };
91   my @types      = qw(bilanz bwa er eur);
92   my %headings   = (
93     bilanz       => t8('Balance'),
94     bwa          => t8('BWA'),
95     er           => t8('Erfolgsrechnung'),
96     eur          => t8('EUER'),
97   );
98
99   my @data;
100
101   foreach my $type (@types) {
102     my $method = "pos_${type}";
103     my $names  = $type eq 'bwa' ? AM->get_bwa_categories(\%::myconfig, $::form)
104                : $type eq 'eur' ? AM->get_eur_categories(\%::myconfig, $::form)
105                :                  {};
106     my %charts = partition_by { $_->$method // '' } @all_charts;
107     delete $charts{''};
108
109     next if !%charts;
110
111     push @data, {
112       type      => $type,
113       heading   => $headings{$type},
114       charts    => \%charts,
115       positions => [ sort { ($a * 1) <=> ($b * 1) } keys %charts ],
116       names     => $names,
117     };
118   }
119
120   $self->render('chart/report_configuration_overview', DATA => \@data, title => t8('Chart configuration overview regarding reports'));
121 }
122
123 sub init_charts {
124
125   # disable pagination when hiding chart details = paginate when showing chart details
126   if ($::form->{hide_chart_details}) {
127     $_[0]->models->disable_plugin('paginated');
128   }
129
130   $_[0]->models->get;
131 }
132
133 sub init_chart {
134   SL::DB::Chart->new(id => $::form->{id} || $::form->{chart}{id})->load;
135 }
136
137 sub init_models {
138   my ($self) = @_;
139
140   SL::Controller::Helper::GetModels->new(
141     controller => $self,
142     sorted => {
143       _default  => {
144         by  => 'accno',
145         dir => 1,
146       },
147       accno       => t8('Account number'),
148       description => t8('Description'),
149     },
150     query => [
151       charttype => 'A',
152     ],
153   );
154 }
155
156 sub init_filter { $_[0]->models->filtered->laundered }
157
158 1;