0e3e2b6fc2a46e701baade1b676c71f948e1bbd5
[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         charttype => 'A',
33         or => [
34           description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
35           accno       => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
36         ]
37       ],
38       limit => 2,
39     ) }) {
40       $self->charts($exact_matches);
41     }
42   }
43
44   my @hashes = map {
45    +{
46      value       => $_->displayable_name,
47      label       => $_->displayable_name,
48      id          => $_->id,
49      accno       => $_->accno,
50      description => $_->description,
51     }
52   } @{ $self->charts }; # neato: if exact match triggers we don't even need the init_parts
53
54   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
55 }
56
57 sub action_test_page {
58   $_[0]->render('chart/test_page', pre_filled_chart => SL::DB::Manager::Chart->get_first);
59 }
60
61 sub action_chart_picker_search {
62   $_[0]->render('chart/chart_picker_search', { layout => 0 }, charts => $_[0]->charts);
63 }
64
65 sub action_chart_picker_result {
66   $_[0]->render('chart/_chart_picker_result', { layout => 0 });
67 }
68
69 sub action_show {
70   my ($self) = @_;
71
72   if ($::request->type eq 'json') {
73     my $chart_hash;
74     if (!$self->chart) {
75       # TODO error
76     } else {
77       $chart_hash                     = $self->chart->as_tree;
78       $chart_hash->{displayable_name} = $self->chart->displayable_name;
79     }
80
81     $self->render(\ SL::JSON::to_json($chart_hash), { layout => 0, type => 'json', process => 0 });
82   }
83 }
84
85 sub init_charts {
86
87   # disable pagination when hiding chart details = paginate when showing chart details
88   if ($::form->{hide_chart_details}) {
89     $_[0]->models->disable_plugin('paginated');
90   }
91
92   $_[0]->models->get;
93 }
94
95 sub init_chart {
96   SL::DB::Chart->new(id => $::form->{id} || $::form->{chart}{id})->load;
97 }
98
99 sub init_models {
100   my ($self) = @_;
101
102   SL::Controller::Helper::GetModels->new(
103     controller => $self,
104     sorted => {
105       _default  => {
106         by  => 'accno',
107         dir => 1,
108       },
109       accno       => t8('Account number'),
110       description => t8('Description'),
111     },
112     query => [
113       charttype => 'A',
114     ],
115   );
116 }
117
118 sub init_filter { $_[0]->models->filtered->laundered }
119
120 1;