Neues Feature: Chartpicker
[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::DB::Helper::Paginated;
10 use SL::Locale::String qw(t8);
11 use SL::JSON;
12
13 use Rose::Object::MakeMethods::Generic (
14   'scalar --get_set_init' => [ qw(charts models chart) ],
15 );
16
17 sub action_ajax_autocomplete {
18   my ($self, %params) = @_;
19
20   my $value = $::form->{column} || 'description';
21
22   # if someone types something, and hits enter, assume he entered the full name.
23   # if something matches, treat that as sole match
24   # unfortunately get_models can't do more than one per package atm, so we do it
25   # the oldfashioned way.
26   if ($::form->{prefer_exact}) {
27     my $exact_matches;
28     # we still need the type filter so that we can't choose an illegal chart
29     # via exact_match if we have preset a link type, e.g. AR_paid
30     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Chart->get_all(
31       query => [
32         SL::DB::Manager::Chart->type_filter($::form->{filter}{type}),
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');
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       require Rose::DB::Object::Helpers;
78         $chart_hash                     = $self->chart->as_tree;
79         $chart_hash->{displayable_name} = $self->chart->displayable_name;
80     }
81
82     $self->render(\ SL::JSON::to_json($chart_hash), { layout => 0, type => 'json', process => 0 });
83   }
84 }
85
86 sub init_charts {
87
88   # disable pagination when hiding chart details = paginate when showing chart details
89   if ($::form->{hide_chart_details}) {
90     $_[0]->models->disable_plugin('paginated');
91   }
92
93   $_[0]->models->get;
94 }
95
96 sub init_chart {
97   SL::DB::Chart->new(id => $::form->{id} || $::form->{chart}{id})->load;
98 }
99
100 sub init_models {
101   my ($self) = @_;
102
103   SL::Controller::Helper::GetModels->new(
104     controller => $self,
105     sorted => {
106       _default  => {
107         by  => 'accno',
108         dir => 1,
109       },
110       accno       => t8('Account number'),
111       description => t8('Description'),
112     },
113   );
114 }
115
116 1;