X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FProject.pm;h=ba6f12f0a744046a6ab035f892323472743f337c;hb=dbda14c263efd93aca3b7114015a47d86b8581e3;hp=1d1f1c4a8ec537da9d00f21a13424cfd474c53a0;hpb=159dd37836728abbb2caca3cb25904428ed99859;p=kivitendo-erp.git diff --git a/SL/DB/Project.pm b/SL/DB/Project.pm index 1d1f1c4a8..ba6f12f0a 100644 --- a/SL/DB/Project.pm +++ b/SL/DB/Project.pm @@ -2,16 +2,52 @@ package SL::DB::Project; use strict; +use List::MoreUtils qw(any); + use SL::DB::MetaSetup::Project; +use SL::DB::Manager::Project; use SL::DB::Helper::CustomVariables( module => 'Project', cvars_alias => 1, ); -__PACKAGE__->meta->make_manager_class; __PACKAGE__->meta->initialize; +sub validate { + my ($self) = @_; + + my @errors; + push @errors, $::locale->text('The project number is missing.') if !$self->projectnumber; + push @errors, $::locale->text('The project number is already in use.') if !$self->is_projectnumber_unique; + push @errors, $::locale->text('The description is missing.') if !$self->description; + + return @errors; +} + +sub is_used { + my ($self) = @_; + + # Unsaved projects are never referenced. + return 0 unless $self->id; + + return any { + my $column = $SL::DB::Manager::Project::project_id_column_prefixes{$_} . 'project_id'; + $self->db->dbh->selectrow_arrayref(qq|SELECT EXISTS(SELECT * FROM ${_} WHERE ${column} = ?)|, undef, $self->id)->[0] + } @SL::DB::Manager::Project::tables_with_project_id_cols; +} + +sub is_projectnumber_unique { + my ($self) = @_; + + return 1 unless $self->projectnumber; + + my @filter = (projectnumber => $self->projectnumber); + @filter = (and => [ @filter, '!id' => $self->id ]) if $self->id; + + return !SL::DB::Manager::Project->get_first(where => \@filter); +} + 1; __END__ @@ -28,7 +64,26 @@ This is a standard Rose::DB::Object based model and can be used as one. =head1 FUNCTIONS -None so far. +=over 4 + +=item C + +Checks whether or not all fields are set to valid values so that the +object can be saved. If valid returns an empty list. Returns an array +of translated error message otherwise. + +=item C + +Checks whether or not the project is referenced from any other +database table. Returns a boolean value. + +=item C + +Returns trueish if the project number is not used for any other +project in the database. Also returns trueish if no project number has +been set yet. + +=back =head1 AUTHOR