1 package SL::DB::Project;
5 use List::MoreUtils qw(any);
7 use SL::DB::MetaSetup::Project;
8 use SL::DB::Manager::Project;
10 use SL::DB::Helper::CustomVariables(
15 __PACKAGE__->meta->add_relationship(
16 employee_invoice_permissions => {
17 type => 'many to many',
18 map_class => 'SL::DB::EmployeeProjectInvoices',
22 __PACKAGE__->meta->initialize;
28 push @errors, $::locale->text('The project number is missing.') if !$self->projectnumber;
29 push @errors, $::locale->text('The project number is already in use.') if !$self->is_projectnumber_unique;
30 push @errors, $::locale->text('The description is missing.') if !$self->description;
38 # Unsaved projects are never referenced.
39 return 0 unless $self->id;
42 my $column = $SL::DB::Manager::Project::project_id_column_prefixes{$_} . 'project_id';
43 $self->db->dbh->selectrow_arrayref(qq|SELECT EXISTS(SELECT * FROM ${_} WHERE ${column} = ?)|, undef, $self->id)->[0]
44 } @SL::DB::Manager::Project::tables_with_project_id_cols;
47 sub is_projectnumber_unique {
50 return 1 unless $self->projectnumber;
52 my @filter = (projectnumber => $self->projectnumber);
53 @filter = (and => [ @filter, '!id' => $self->id ]) if $self->id;
55 return !SL::DB::Manager::Project->get_first(where => \@filter);
58 sub displayable_name {
61 return join ' ', grep $_, $self->projectnumber, $self->description;
64 sub full_description {
65 my ($self, %params) = @_;
67 $params{style} ||= 'both';
70 if ($params{style} =~ m/number/) {
71 $description = $self->projectnumber;
73 } elsif ($params{style} =~ m/description/) {
74 $description = $self->description;
76 } elsif (($params{style} =~ m/full/) && $self->customer) {
77 $description = $self->projectnumber;
78 if ($self->description && do { my $desc = quotemeta $self->description; $self->projectnumber !~ m/$desc/ }) {
79 $description .= ' ' . $self->description;
82 $description = $self->customer->name . " (${description})";
85 $description = $self->projectnumber;
86 if ($self->description && do { my $desc = quotemeta $self->description; $self->projectnumber !~ m/$desc/ }) {
87 $description .= ' (' . $self->description . ')';
94 sub may_employee_view_project_invoices {
95 my ($self, $employee) = @_;
97 return undef if !$self->id;
99 my $employee_id = ref($employee) ? $employee->id : $employee * 1;
102 FROM employee_project_invoices
103 WHERE (employee_id = ?)
108 return !!$self->db->dbh->selectrow_arrayref($query, undef, $employee_id, $self->id)->[0];
119 SL::DB::Project: Model for the 'project' table
123 This is a standard Rose::DB::Object based model and can be used as one.
131 Checks whether or not all fields are set to valid values so that the
132 object can be saved. If valid returns an empty list. Returns an array
133 of translated error message otherwise.
137 Checks whether or not the project is referenced from any other
138 database table. Returns a boolean value.
140 =item C<is_projectnumber_unique>
142 Returns trueish if the project number is not used for any other
143 project in the database. Also returns trueish if no project number has
146 =item C<displayable_name>
148 Returns a human-readable description of the project, consisting of projectnumber
151 =item C<full_description %params>
153 Returns a full description for the project which can consist of the
154 project number, its description or both. This is determined by the
155 parameter C<style> which defaults to C<both>:
161 Returns the project's number followed by its description in
162 parenthesis (e.g. "12345 (Secret Combinations)"). If the project's
163 description is already part of the project's number then it will not
166 =item C<projectnumber> (or simply C<number>)
168 Returns only the project's number.
170 =item C<projectdescription> (or simply C<description>)
172 Returns only the project's description.
176 Returns the customer name followed by the project number and project
177 description in parenthesis (e.g. "Evil Corp (12345 World
178 domination)"). If the project's description is already part of the
179 project's number then it will not be appended.
181 If this project isn't linked to a customer then the style C<both> is
190 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>