1 package SL::DB::Helper::AttrDuration;
 
   5 use parent qw(Exporter);
 
   6 our @EXPORT = qw(attr_duration);
 
  11   my ($package, @attributes) = @_;
 
  13   _make($package, $_) for @attributes;
 
  17   my ($package, $attribute) = @_;
 
  21   *{ $package . '::' . $attribute . '_as_hours' } = sub {
 
  22     my ($self, $value) = @_;
 
  24     $self->$attribute(int($value) + ($self->$attribute - int($self->$attribute))) if @_ > 1;
 
  25     return int($self->$attribute // 0);
 
  28   *{ $package . '::' . $attribute . '_as_minutes' } = sub {
 
  29     my ($self, $value) = @_;
 
  31     $self->$attribute(int($self->$attribute) * 1.0 + ($value // 0) / 60.0) if @_ > 1;
 
  32     return int(($self->$attribute // 0) * 60.0 + 0.5) % 60;
 
  35   *{ $package . '::' . $attribute . '_as_duration_string' } = sub {
 
  36     my ($self, $value) = @_;
 
  38     $self->$attribute(defined($value) ? $::form->parse_amount(\%::myconfig, $value) * 1 : undef) if @_ > 1;
 
  39     return defined($self->$attribute) ? $::form->format_amount(\%::myconfig, $self->$attribute // 0, 2) : undef;
 
  42   *{ $package . '::' . $attribute . '_as_man_days' } = sub {
 
  43     my ($self, $value) = @_;
 
  46       return undef if !defined $value;
 
  47       $self->$attribute($value);
 
  49     $value = $self->$attribute // 0;
 
  50     return $value >= 8.0 ? $value / 8.0 : $value;
 
  53   *{ $package . '::' . $attribute . '_as_man_days_unit' } = sub {
 
  54     my ($self, $unit) = @_;
 
  57       return undef if !defined $unit;
 
  58       croak "Unknown unit '${unit}'"                    if $unit !~ m/^(?:h|hour|man_day)$/;
 
  59       $self->$attribute(($self->$attribute // 0) * 8.0) if $unit eq 'man_day';
 
  62     return ($self->$attribute // 0) >= 8.0 ? 'man_day' : 'h'
 
  65   *{ $package . '::' . $attribute . '_as_man_days_string' } = sub {
 
  66     my ($self, $value) = @_;
 
  67     my $method         = "${attribute}_as_man_days";
 
  70       return undef if !defined $value;
 
  71       $self->$method($::form->parse_amount(\%::myconfig, $value));
 
  74     return $::form->format_amount(\%::myconfig, $self->$method // 0, 2);
 
  87 SL::DB::Helper::AttrDuration - Attribute helper for duration stored in
 
  93   use SL::DB::Helper::AttrDuration;
 
  94   __PACKAGE__->attr_duration('time_estimation');
 
  97   print "Minutes: " . $obj->time_estimation_as_minutes . " hours: " . $obj->time_estimation_as_hours . "\n";
 
  99   # Use formatted strings in input fields in templates:
 
 102     [% L.input_tag('time_estimation_as_duration_string', SELF.obj.time_estimation_as_duration_string) %]
 
 107 This is a helper for columns that store a duration as a numeric or
 
 108 floating point number representing a number of hours. So the value
 
 109 1.75 would stand for "1 hour, 45 minutes".
 
 111 The helper methods created are:
 
 115 =item C<attribute_as_minutes [$new_value]>
 
 117 Access only the minutes. Return values are in the range [0 - 59].
 
 119 =item C<attribute_as_hours [$new_value]>
 
 121 Access only the hours. Returns an integer value.
 
 123 =item C<attribute_as_duration_string [$new_value]>
 
 125 Access the full value as a formatted string according to the user's
 
 128 =item C<attribute_as_man_days [$new_value]>
 
 130 Access the attribute as a number of man days which are assumed to be 8
 
 131 hours long. If the underlying attribute is less than 8 then the value
 
 132 itself will be returned. Otherwise the value divided by 8 is returned.
 
 134 If used as a setter then the underlying attribute is simply set to
 
 135  C<$new_value>. Intentional use is to set the man days first and the
 
 138   $obj->attribute_as_man_days($::form->{attribute_as_man_days});
 
 139   $obj->attribute_as_man_days_unit($::form->{attribute_as_man_days_unit});
 
 141 Note that L<SL::DB::Object/assign_attributes> is aware of this and
 
 142 handles this case correctly.
 
 144 =item C<attribute_as_man_days_unit [$new_unit]>
 
 146 Returns the unit that the number returned by L</attribute_as_man_days>
 
 147 represents. This can be either C<h> if the underlying attribute is
 
 148 less than 8 and C<man_day> otherwise.
 
 150 If used as a setter then the underlying attribute is multiplied by 8
 
 151 if C<$new_unit> equals C<man_day>. Otherwise the underlying attribute
 
 152 is not modified. Intentional use is to set the man days first and the
 
 155   $obj->attribute_as_man_days($::form->{attribute_as_man_days});
 
 156   $obj->attribute_as_man_days_unit($::form->{attribute_as_man_days_unit});
 
 158 Note that L<SL::DB::Object/assign_attributes> is aware of this and
 
 159 handles this case correctly.
 
 167 =item C<attr_duration @attributes>
 
 169 Package method. Call with the names of attributes for which the helper
 
 170 methods should be created.
 
 180 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>