Revert "Buchungsliste: Kontonamen werden nicht angezeigt."
[kivitendo-erp.git] / SL / Controller / Project.pm
1 package SL::Controller::Project;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Clone qw(clone);
8
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::ParseFilter;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::CVar;
13 use SL::DB::Customer;
14 use SL::DB::DeliveryOrder;
15 use SL::DB::Invoice;
16 use SL::DB::Order;
17 use SL::DB::Project;
18 use SL::DB::PurchaseInvoice;
19 use SL::DB::ProjectType;
20 use SL::Helper::Flash;
21 use SL::Locale::String;
22
23 use Rose::Object::MakeMethods::Generic
24 (
25  scalar => [ qw(project linked_records) ],
26  'scalar --get_set_init' => [ qw(models) ],
27 );
28
29 __PACKAGE__->run_before('check_auth');
30 __PACKAGE__->run_before('load_project',       only => [ qw(edit update destroy) ]);
31
32 #
33 # actions
34 #
35
36 sub action_search {
37   my ($self) = @_;
38
39   my %params;
40
41   $params{ALL_PROJECT_TYPES} = SL::DB::Manager::ProjectType->get_all_sorted;
42   $params{CUSTOM_VARIABLES}  = CVar->get_configs(module => 'Projects');
43   ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
44     = CVar->render_search_options(variables      => $params{CUSTOM_VARIABLES},
45                                   include_prefix => 'l_',
46                                   include_value  => 'Y');
47
48   $self->render('project/search', %params);
49 }
50
51 sub action_list {
52   my ($self) = @_;
53
54   # $self->make_filter_summary;
55
56   my $projects = $self->models->get;
57
58   $self->prepare_report;
59
60   $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
61 }
62
63 sub action_new {
64   my ($self) = @_;
65
66   $self->project(SL::DB::Project->new);
67   $self->display_form(title    => $::locale->text('Create a new project'),
68                       callback => $::form->{callback} || $self->url_for(action => 'new'));
69 }
70
71 sub action_edit {
72   my ($self) = @_;
73
74   $self->linked_records([
75     map  { @{ $_ } }
76     grep { $_      } (
77       SL::DB::Manager::Order->          get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
78       SL::DB::Manager::DeliveryOrder->  get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
79       SL::DB::Manager::Invoice->        get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer'           ], sort_by => 'transdate ASC'),
80       SL::DB::Manager::PurchaseInvoice->get_all(where => [ globalproject_id => $self->project->id ], with_objects => [             'vendor' ], sort_by => 'transdate ASC'),
81     )]);
82
83   $self->display_form(title    => $::locale->text('Edit project #1', $self->project->projectnumber),
84                       callback => $::form->{callback} || $self->url_for(action => 'edit', id => $self->project->id));
85 }
86
87 sub action_create {
88   my ($self) = @_;
89
90   $self->project(SL::DB::Project->new);
91   $self->create_or_update;
92 }
93
94 sub action_update {
95   my ($self) = @_;
96   $self->create_or_update;
97 }
98
99 sub action_destroy {
100   my ($self) = @_;
101
102   if (eval { $self->project->delete; 1; }) {
103     flash_later('info',  $::locale->text('The project has been deleted.'));
104   } else {
105     flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
106   }
107
108   $self->redirect_to(action => 'search');
109 }
110
111 #
112 # filters
113 #
114
115 sub check_auth {
116   $::auth->assert('project_edit');
117 }
118
119 #
120 # helpers
121 #
122
123 sub display_form {
124   my ($self, %params) = @_;
125
126   $params{ALL_CUSTOMERS}     = SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, id => $self->project->customer_id ]]);
127   $params{ALL_PROJECT_TYPES} = SL::DB::Manager::ProjectType->get_all_sorted;
128   $params{CUSTOM_VARIABLES}  = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
129   CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
130
131   $self->render('project/form', %params);
132 }
133
134 sub create_or_update {
135   my $self   = shift;
136   my $is_new = !$self->project->id;
137   my $params = delete($::form->{project}) || { };
138
139   delete $params->{id};
140   $self->project->assign_attributes(%{ $params });
141
142   my @errors = $self->project->validate;
143
144   if (@errors) {
145     flash('error', @errors);
146     $self->display_form(title    => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
147                         callback => $::form->{callback});
148     return;
149   }
150
151   $self->project->save;
152
153   CVar->save_custom_variables(
154     dbh          => $self->project->db->dbh,
155     module       => 'Projects',
156     trans_id     => $self->project->id,
157     variables    => $::form,
158     always_valid => 1,
159   );
160
161   flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
162
163   $self->redirect_to($::form->{callback} || (action => 'search'));
164 }
165
166 sub load_project {
167   my ($self) = @_;
168   $self->project(SL::DB::Project->new(id => $::form->{id})->load);
169 }
170
171 sub setup_db_args_from_filter {
172   my ($self) = @_;
173
174   $self->{filter} = {};
175   my %args = parse_filter(
176     $self->_pre_parse_filter($::form->{filter}, $self->{filter}),
177     with_objects => [ 'customer', 'project_type' ],
178     launder_to   => $self->{filter},
179   );
180
181   $self->db_args(\%args);
182 }
183
184 sub prepare_report {
185   my ($self)      = @_;
186
187   my $callback    = $self->models->get_callback;
188
189   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
190   $self->{report} = $report;
191
192   my @columns     = qw(projectnumber description customer active valid project_type);
193   my @sortable    = qw(projectnumber description customer              project_type);
194
195   my %column_defs = (
196     projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
197     description   => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
198     project_type  => { sub  => sub { $_[0]->project_type->description } },
199     customer      => { sub  => sub { $_[0]->customer ? $_[0]->customer->name     : '' } },
200     active        => { sub  => sub { $_[0]->active   ? $::locale->text('Active') : $::locale->text('Inactive') },
201                        text => $::locale->text('Active') },
202     valid         => { sub  => sub { $_[0]->valid    ? $::locale->text('Valid')  : $::locale->text('Invalid')  },
203                        text => $::locale->text('Valid')  },
204   );
205
206   map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
207
208   $report->set_options(
209     std_column_visibility => 1,
210     controller_class      => 'Project',
211     output_format         => 'HTML',
212     top_info_text         => $::locale->text('Projects'),
213     title                 => $::locale->text('Projects'),
214     allow_pdf_export      => 1,
215     allow_csv_export      => 1,
216   );
217   $report->set_columns(%column_defs);
218   $report->set_column_order(@columns);
219   $report->set_export_options(qw(list filter));
220   $report->set_options_from_form;
221   $self->models->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
222   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
223   $report->set_options(
224     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
225   );
226 }
227
228 sub init_models {
229   my ($self) = @_;
230
231   SL::Controller::Helper::GetModels->new(
232     controller => $self,
233     sorted => {
234       _default => {
235         by    => 'projectnumber',
236         dir   => 1,
237       },
238       customer      => t8('Customer'),
239       description   => t8('Description'),
240       projectnumber => t8('Project Number'),
241       project_type  => t8('Project Type'),
242     },
243     with_objects => [ 'customer' ],
244   );
245 }
246
247 1;