624a65b1da52324e90bbbf890aa5ff822752d2fa
[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 SL::DB::Chart;
8 use SL::Controller::Helper::GetModels;
9 use SL::Locale::String qw(t8);
10 use SL::JSON;
11
12 use Rose::Object::MakeMethods::Generic (
13   'scalar --get_set_init' => [ qw(charts models chart filter) ],
14 );
15
16 sub action_ajax_autocomplete {
17   my ($self, %params) = @_;
18
19   my $value = $::form->{column} || 'description';
20
21   # if someone types something, and hits enter, assume he entered the full name.
22   # if something matches, treat that as sole match
23   # unfortunately get_models can't do more than one per package atm, so we do it
24   # the oldfashioned way.
25   if ($::form->{prefer_exact}) {
26     my $exact_matches;
27     # we still need the type filter so that we can't choose an illegal chart
28     # via exact_match if we have preset a link type, e.g. AR_paid
29     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Chart->get_all(
30       query => [
31         SL::DB::Manager::Chart->type_filter($::form->{filter}{type}),
32         or => [
33           description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
34           accno       => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
35         ]
36       ],
37       limit => 2,
38     ) }) {
39       $self->charts($exact_matches);
40     }
41   }
42
43   my @hashes = map {
44    +{
45      value       => $_->displayable_name,
46      label       => $_->displayable_name,
47      id          => $_->id,
48      accno       => $_->accno,
49      description => $_->description,
50     }
51   } @{ $self->charts }; # neato: if exact match triggers we don't even need the init_parts
52
53   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
54 }
55
56 sub action_test_page {
57   $_[0]->render('chart/test_page');
58 }
59
60 sub action_chart_picker_search {
61   $_[0]->render('chart/chart_picker_search', { layout => 0 }, charts => $_[0]->charts);
62 }
63
64 sub action_chart_picker_result {
65   $_[0]->render('chart/_chart_picker_result', { layout => 0 });
66 }
67
68 sub action_show {
69   my ($self) = @_;
70
71   if ($::request->type eq 'json') {
72     my $chart_hash;
73     if (!$self->chart) {
74       # TODO error
75     } else {
76       $chart_hash                     = $self->chart->as_tree;
77       $chart_hash->{displayable_name} = $self->chart->displayable_name;
78     }
79
80     $self->render(\ SL::JSON::to_json($chart_hash), { layout => 0, type => 'json', process => 0 });
81   }
82 }
83
84 sub init_charts {
85
86   # disable pagination when hiding chart details = paginate when showing chart details
87   if ($::form->{hide_chart_details}) {
88     $_[0]->models->disable_plugin('paginated');
89   }
90
91   $_[0]->models->get;
92 }
93
94 sub init_chart {
95   SL::DB::Chart->new(id => $::form->{id} || $::form->{chart}{id})->load;
96 }
97
98 sub init_models {
99   my ($self) = @_;
100
101   SL::Controller::Helper::GetModels->new(
102     controller => $self,
103     sorted => {
104       _default  => {
105         by  => 'accno',
106         dir => 1,
107       },
108       accno       => t8('Account number'),
109       description => t8('Description'),
110     },
111   );
112 }
113
114 sub init_filter { $_[0]->models->filtered->laundered }
115
116 1;