Projekttypen verwaltbar gemacht
[kivitendo-erp.git] / SL / DB / Manager / Project.pm
1 package SL::DB::Manager::Project;
2
3 use strict;
4
5 use parent qw(SL::DB::Helper::Manager);
6
7 use SL::DB::Helper::Paginated;
8 use SL::DB::Helper::Sorted;
9 use SL::DB::Helper::Filtered;
10
11 sub object_class { 'SL::DB::Project' }
12
13 __PACKAGE__->make_manager_methods;
14 __PACKAGE__->add_filter_specs(
15   active => sub {
16     my ($key, $value, $prefix) = @_;
17     # TODO add boolean context
18     return ()                        if $value eq 'both';
19     return ($prefix . "active" => 1) if $value eq 'active';
20     return (or => [ $prefix . "active" => 0, $prefix . "active" => undef ]) if $value eq 'inactive';
21   },
22   valid => sub {
23     my ($key, $value, $prefix) = @_;
24     return ()                       if $value eq 'both';
25     return ($prefix . "valid" => 1) if $value eq 'valid';
26     return (or => [ $prefix . "valid" => 0, $prefix . "valid" => undef ]) if $value eq 'invalid';
27   },
28   status => sub {
29     my ($key, $value, $prefix) = @_;
30     return () if $value eq 'all';
31     return __PACKAGE__->is_not_used_filter($prefix);
32   },
33 );
34
35 our %project_id_column_prefixes = (
36   ar              => 'global',
37   ap              => 'global',
38   oe              => 'global',
39   delivery_orders => 'global',
40 );
41
42 our @tables_with_project_id_cols = qw(acc_trans ap ar delivery_order_items delivery_orders invoice oe orderitems);
43
44 sub _sort_spec {
45   return (
46     default        => [ 'projectnumber', 1 ],
47     columns        => {
48       SIMPLE       => 'ALL',
49       customer     => 'customer.name',
50       project_type => 'project_type.description',
51     });
52 }
53
54 sub is_not_used_filter {
55   my ($class, $prefix) = @_;
56
57   my $query = join ' UNION ', map {
58     my $column = $project_id_column_prefixes{$_} . 'project_id';
59     qq|SELECT DISTINCT ${column} FROM ${_} WHERE ${column} IS NOT NULL|
60   } @tables_with_project_id_cols;
61
62   return ("!${prefix}id" => [ \"(${query})" ]);
63 }
64
65 sub default_objects_per_page {
66   20;
67 }
68
69 1;
70 __END__
71
72 =pod
73
74 =encoding utf8
75
76 =head1 NAME
77
78 SL::DB::Manager::Project - Manager for models for the 'project' table
79
80 =head1 SYNOPSIS
81
82 This is a standard Rose::DB::Manager based model manager and can be
83 used as such.
84
85 =head1 FUNCTIONS
86
87 =over 4
88
89 =item C<is_not_used_filter>
90
91 Returns an array containing a partial filter suitable for the C<query>
92 parameter that limits to projects that are not referenced from any
93 other database table.
94
95 =back
96
97 =head1 BUGS
98
99 Nothing here yet.
100
101 =head1 AUTHOR
102
103 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
104
105 =cut