Bankerweiterung - Zwischenstand, erster Entwurf
[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::ProjectType;
19 use SL::DB::ProjectStatus;
20 use SL::DB::PurchaseInvoice;
21 use SL::DB::ProjectType;
22 use SL::Helper::Flash;
23 use SL::Locale::String;
24
25 use Data::Dumper;
26
27 use Rose::Object::MakeMethods::Generic
28 (
29  scalar => [ qw(project linked_records) ],
30  'scalar --get_set_init' => [ qw(models customers project_types project_statuses) ],
31 );
32
33 __PACKAGE__->run_before('check_auth');
34 __PACKAGE__->run_before('load_project',        only => [ qw(edit update destroy) ]);
35
36 #
37 # actions
38 #
39
40 sub action_search {
41   my ($self) = @_;
42
43   my %params;
44
45   $params{CUSTOM_VARIABLES}  = CVar->get_configs(module => 'Projects');
46
47   ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
48     = CVar->render_search_options(variables      => $params{CUSTOM_VARIABLES},
49                                   include_prefix => 'l_',
50                                   include_value  => 'Y');
51
52   $self->render('project/search', %params);
53 }
54
55 sub action_list {
56   my ($self) = @_;
57
58   $self->make_filter_summary;
59
60   my $projects = $self->models->get;
61
62   $self->prepare_report;
63
64   $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
65 }
66
67 sub action_new {
68   my ($self) = @_;
69
70   $self->project(SL::DB::Project->new);
71   $self->display_form(title    => $::locale->text('Create a new project'),
72                       callback => $::form->{callback} || $self->url_for(action => 'new'));
73 }
74
75 sub action_edit {
76   my ($self) = @_;
77
78   $self->get_linked_records;
79   $self->display_form(title    => $::locale->text('Edit project #1', $self->project->projectnumber),
80                       callback => $::form->{callback} || $self->url_for(action => 'edit', id => $self->project->id));
81 }
82
83 sub action_create {
84   my ($self) = @_;
85
86   $self->project(SL::DB::Project->new);
87   $self->create_or_update;
88 }
89
90 sub action_update {
91   my ($self) = @_;
92   $self->create_or_update;
93 }
94
95 sub action_destroy {
96   my ($self) = @_;
97
98   if (eval { $self->project->delete; 1; }) {
99     flash_later('info',  $::locale->text('The project has been deleted.'));
100   } else {
101     flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
102   }
103
104   $self->redirect_to(action => 'search');
105 }
106
107 #
108 # filters
109 #
110
111 sub check_auth {
112   $::auth->assert('project_edit');
113 }
114
115 #
116 # helpers
117 #
118
119 sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
120 sub init_project_types    { SL::DB::Manager::ProjectType->get_all_sorted   }
121
122 sub init_customers {
123   my ($self)      = @_;
124   my @customer_id = $self->project && $self->project->customer_id ? (id => $self->project->customer_id) : ();
125
126   return SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, @customer_id ]]);
127 }
128
129 sub display_form {
130   my ($self, %params) = @_;
131
132   $params{CUSTOM_VARIABLES}  = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
133
134   if ($params{keep_cvars}) {
135     for my $cvar (@{ $params{CUSTOM_VARIABLES} }) {
136       $cvar->{value} = $::form->{"cvar_$cvar->{name}"} if $::form->{"cvar_$cvar->{name}"};
137     }
138   }
139
140   CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
141
142   $self->render('project/form', %params);
143 }
144
145 sub create_or_update {
146   my $self   = shift;
147   my $is_new = !$self->project->id;
148   my $params = delete($::form->{project}) || { };
149
150   delete $params->{id};
151   $self->project->assign_attributes(%{ $params });
152
153   my @errors = $self->project->validate;
154
155   if (@errors) {
156     flash('error', @errors);
157     $self->display_form(title    => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
158                         callback => $::form->{callback},
159                         keep_cvars => 1);
160     return;
161   }
162
163   $self->project->save;
164
165   CVar->save_custom_variables(
166     dbh          => $self->project->db->dbh,
167     module       => 'Projects',
168     trans_id     => $self->project->id,
169     variables    => $::form,
170     always_valid => 1,
171   );
172
173   flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
174
175   $self->redirect_to($::form->{callback} || (action => 'search'));
176 }
177
178 sub load_project {
179   my ($self) = @_;
180   $self->project(SL::DB::Project->new(id => $::form->{id})->load);
181 }
182
183 sub get_linked_records {
184   my ($self) = @_;
185
186   $self->linked_records([
187     map  { @{ $_ } }
188     grep { $_      } (
189       SL::DB::Manager::Order->          get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
190       SL::DB::Manager::DeliveryOrder->  get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
191       SL::DB::Manager::Invoice->        get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer'           ], sort_by => 'transdate ASC'),
192       SL::DB::Manager::PurchaseInvoice->get_all(where => [ globalproject_id => $self->project->id ], with_objects => [             'vendor' ], sort_by => 'transdate ASC'),
193     )]);
194 }
195
196 sub prepare_report {
197   my ($self)      = @_;
198
199   my $callback    = $self->models->get_callback;
200
201   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
202   $self->{report} = $report;
203
204   my @columns     = qw(project_status customer projectnumber description active valid project_type);
205   my @sortable    = qw(projectnumber description customer              project_type project_status);
206
207   my %column_defs = (
208     projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
209     description   => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
210     project_type  => { sub  => sub { $_[0]->project_type->description } },
211     project_status => { sub  => sub { $_[0]->project_status->description }, text => t8('Status') },
212     customer      => { raw_data  => sub { $_[0]->customer_id ? $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) : '' } },
213     active        => { sub  => sub { $_[0]->active   ? $::locale->text('Active') : $::locale->text('Inactive') },
214                        text => $::locale->text('Active') },
215     valid         => { sub  => sub { $_[0]->valid    ? $::locale->text('Valid')  : $::locale->text('Invalid')  },
216                        text => $::locale->text('Valid')  },
217   );
218
219   map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
220
221   if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
222     $self->models->disable_plugin('paginated');
223   }
224   $report->set_options(
225     std_column_visibility => 1,
226     controller_class      => 'Project',
227     output_format         => 'HTML',
228     raw_top_info_text     => $self->render('project/report_top', { output => 0 }),
229     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
230     title                 => $::locale->text('Projects'),
231     allow_pdf_export      => 1,
232     allow_csv_export      => 1,
233   );
234   $report->set_columns(%column_defs);
235   $report->set_column_order(@columns);
236   $report->set_export_options(qw(list filter));
237   $report->set_options_from_form;
238   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
239   $report->set_options(
240     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
241   );
242 }
243
244 sub init_models {
245   my ($self) = @_;
246
247   SL::Controller::Helper::GetModels->new(
248     controller => $self,
249     sorted => {
250       _default => {
251         by    => 'projectnumber',
252         dir   => 1,
253       },
254       customer      => t8('Customer'),
255       description   => t8('Description'),
256       projectnumber => t8('Project Number'),
257       project_type  => t8('Project Type'),
258     },
259     with_objects => [ 'customer' ],
260   );
261 }
262
263 sub make_filter_summary {
264   my ($self) = @_;
265
266   my $filter = $::form->{filter} || {};
267   my @filter_strings;
268
269   my @filters = (
270     [ $filter->{"projectnumber:substr::ilike"},  t8('Project Number') ],
271     [ $filter->{"description:substr::ilike"},    t8('Description')    ],
272     [ $filter->{customer}{"name:substr::ilike"}, t8('Customer')       ],
273     [ $filter->{"project_type_id"},              t8('Project Type'),    sub { SL::DB::Manager::ProjectType->find_by(id => $filter->{"project_type_id"})->description }   ],
274     [ $filter->{"project_status_id"},            t8('Project Status'),  sub { SL::DB::Manager::ProjectStatus->find_by(id => $filter->{"project_status_id"})->description } ],
275   );
276
277   my @flags = (
278     [ $filter->{active} eq 'active',    $::locale->text('Active')      ],
279     [ $filter->{active} eq 'inactive',  $::locale->text('Inactive')    ],
280     [ $filter->{valid}  eq 'valid',     $::locale->text('Valid')       ],
281     [ $filter->{valid}  eq 'invalid',   $::locale->text('Invalid')     ],
282     [ $filter->{orphaned},              $::locale->text('Orphaned')    ],
283   );
284
285   for (@flags) {
286     push @filter_strings, "$_->[1]" if $_->[0];
287   }
288   for (@filters) {
289     push @filter_strings, "$_->[1]: " . ($_->[2] ? $_->[2]->() : $_->[0]) if $_->[0];
290   }
291
292   $self->{filter_summary} = join ', ', @filter_strings;
293 }
294 1;