56a7e0b86fcb9490b5a1e540b229e23fff26df05
[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 ne 'orphaned';
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       project_status => 'project_status.description',
52     });
53 }
54
55 sub is_not_used_filter {
56   my ($class, $prefix) = @_;
57
58   my $query = join ' UNION ', map {
59     my $column = $project_id_column_prefixes{$_} . 'project_id';
60     qq|SELECT DISTINCT ${column} FROM ${_} WHERE ${column} IS NOT NULL|
61   } @tables_with_project_id_cols;
62
63   return ("!${prefix}id" => [ \"(${query})" ]);
64 }
65
66 sub default_objects_per_page {
67   20;
68 }
69
70 1;
71 __END__
72
73 =pod
74
75 =encoding utf8
76
77 =head1 NAME
78
79 SL::DB::Manager::Project - Manager for models for the 'project' table
80
81 =head1 SYNOPSIS
82
83 This is a standard Rose::DB::Manager based model manager and can be
84 used as such.
85
86 =head1 FUNCTIONS
87
88 =over 4
89
90 =item C<is_not_used_filter>
91
92 Returns an array containing a partial filter suitable for the C<query>
93 parameter that limits to projects that are not referenced from any
94 other database table.
95
96 =back
97
98 =head1 BUGS
99
100 Nothing here yet.
101
102 =head1 AUTHOR
103
104 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
105
106 =cut