X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/e3aa3f5b7ea363bf7ec8e547c583b3b4a0758492..4180aaea33e9ff3bb35f3fa6cf91651a6225f7ad:/SL/DB/Helper/AttrDuration.pm diff --git a/SL/DB/Helper/AttrDuration.pm b/SL/DB/Helper/AttrDuration.pm new file mode 100644 index 000000000..704c340ae --- /dev/null +++ b/SL/DB/Helper/AttrDuration.pm @@ -0,0 +1,182 @@ +package SL::DB::Helper::AttrDuration; + +use strict; + +use parent qw(Exporter); +our @EXPORT = qw(attr_duration); + +use Carp; + +sub attr_duration { + my ($package, @attributes) = @_; + + _make($package, $_) for @attributes; +} + +sub _make { + my ($package, $attribute) = @_; + + no strict 'refs'; + + *{ $package . '::' . $attribute . '_as_hours' } = sub { + my ($self, $value) = @_; + + $self->$attribute(int($value) + ($self->$attribute - int($self->$attribute))) if @_ > 1; + return int($self->$attribute // 0); + }; + + *{ $package . '::' . $attribute . '_as_minutes' } = sub { + my ($self, $value) = @_; + + $self->$attribute(int($self->$attribute) * 1.0 + ($value // 0) / 60.0) if @_ > 1; + return int(($self->$attribute // 0) * 60.0 + 0.5) % 60; + }; + + *{ $package . '::' . $attribute . '_as_duration_string' } = sub { + my ($self, $value) = @_; + + $self->$attribute(defined($value) ? $::form->parse_amount(\%::myconfig, $value) * 1 : undef) if @_ > 1; + return defined($self->$attribute) ? $::form->format_amount(\%::myconfig, $self->$attribute // 0, 2) : undef; + }; + + *{ $package . '::' . $attribute . '_as_man_days' } = sub { + my ($self, $value) = @_; + + if (@_ > 1) { + return undef if !defined $value; + $self->$attribute($value); + } + $value = $self->$attribute // 0; + return $value >= 8.0 ? $value / 8.0 : $value; + }; + + *{ $package . '::' . $attribute . '_as_man_days_unit' } = sub { + my ($self, $unit) = @_; + + if (@_ > 1) { + return undef if !defined $unit; + croak "Unknown unit '${unit}'" if $unit !~ m/^(?:h|hour|man_day)$/; + $self->$attribute(($self->$attribute // 0) * 8.0) if $unit eq 'man_day'; + } + + return ($self->$attribute // 0) >= 8.0 ? 'man_day' : 'h' + }; + + *{ $package . '::' . $attribute . '_as_man_days_string' } = sub { + my ($self, $value) = @_; + my $method = "${attribute}_as_man_days"; + + if (@_ > 1) { + return undef if !defined $value; + $self->$method($::form->parse_amount(\%::myconfig, $value)); + } + + return $::form->format_amount(\%::myconfig, $self->$method // 0, 2); + }; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::AttrDuration - Attribute helper for duration stored in +numeric columns + +=head1 SYNOPSIS + + # In a Rose model: + use SL::DB::Helper::AttrDuration; + __PACKAGE__->attr_duration('time_estimation'); + + # Read access: + print "Minutes: " . $obj->time_estimation_as_minutes . " hours: " . $obj->time_estimation_as_hours . "\n"; + + # Use formatted strings in input fields in templates: +
+ +=head1 OVERVIEW + +This is a helper for columns that store a duration as a numeric or +floating point number representing a number of hours. So the value +1.75 would stand for "1 hour, 45 minutes". + +The helper methods created are: + +=over 4 + +=item C