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->initialize;
 
  21   push @errors, $::locale->text('The project number is missing.')        if !$self->projectnumber;
 
  22   push @errors, $::locale->text('The project number is already in use.') if !$self->is_projectnumber_unique;
 
  23   push @errors, $::locale->text('The description is missing.')           if !$self->description;
 
  31   # Unsaved projects are never referenced.
 
  32   return 0 unless $self->id;
 
  35     my $column = $SL::DB::Manager::Project::project_id_column_prefixes{$_} . 'project_id';
 
  36     $self->db->dbh->selectrow_arrayref(qq|SELECT EXISTS(SELECT * FROM ${_} WHERE ${column} = ?)|, undef, $self->id)->[0]
 
  37   } @SL::DB::Manager::Project::tables_with_project_id_cols;
 
  40 sub is_projectnumber_unique {
 
  43   return 1 unless $self->projectnumber;
 
  45   my @filter = (projectnumber => $self->projectnumber);
 
  46   @filter    = (and => [ @filter, '!id' => $self->id ]) if $self->id;
 
  48   return !SL::DB::Manager::Project->get_first(where => \@filter);
 
  51 sub full_description {
 
  52   my ($self, %params) = @_;
 
  54   $params{style} ||= 'both';
 
  57   if ($params{style} =~ m/number/) {
 
  58     $description = $self->projectnumber;
 
  60   } elsif ($params{style} =~ m/description/) {
 
  61     $description = $self->description;
 
  63   } elsif (($params{style} =~ m/full/) && $self->customer) {
 
  64     $description = $self->projectnumber;
 
  65     if ($self->description && do { my $desc = quotemeta $self->description; $self->projectnumber !~ m/$desc/ }) {
 
  66       $description .= ' ' . $self->description;
 
  69     $description = $self->customer->name . " (${description})";
 
  72     $description = $self->projectnumber;
 
  73     if ($self->description && do { my $desc = quotemeta $self->description; $self->projectnumber !~ m/$desc/ }) {
 
  74       $description .= ' (' . $self->description . ')';
 
  89 SL::DB::Project: Model for the 'project' table
 
  93 This is a standard Rose::DB::Object based model and can be used as one.
 
 101 Checks whether or not all fields are set to valid values so that the
 
 102 object can be saved. If valid returns an empty list. Returns an array
 
 103 of translated error message otherwise.
 
 107 Checks whether or not the project is referenced from any other
 
 108 database table. Returns a boolean value.
 
 110 =item C<is_projectnumber_unique>
 
 112 Returns trueish if the project number is not used for any other
 
 113 project in the database. Also returns trueish if no project number has
 
 116 =item C<full_description %params>
 
 118 Returns a full description for the project which can consist of the
 
 119 project number, its description or both. This is determined by the
 
 120 parameter C<style> which defaults to C<both>:
 
 126 Returns the project's number followed by its description in
 
 127 parenthesis (e.g. "12345 (Secret Combinations)"). If the project's
 
 128 description is already part of the project's number then it will not
 
 131 =item C<projectnumber> (or simply C<number>)
 
 133 Returns only the project's number.
 
 135 =item C<projectdescription> (or simply C<description>)
 
 137 Returns only the project's description.
 
 141 Returns the customer name followed by the project number and project
 
 142 description in parenthesis (e.g. "Evil Corp (12345 World
 
 143 domination)"). If the project's description is already part of the
 
 144 project's number then it will not be appended.
 
 146 If this project isn't linked to a customer then the style C<both> is
 
 155 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>