From: Moritz Bunkus Date: Tue, 24 Nov 2020 11:10:33 +0000 (+0100) Subject: AttrDuration für minutes: _in_hours und _in_hours_as_number X-Git-Tag: kivitendo-mebil_0.1-0~9^2~624 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=b9cd11d78faef7815e435c11aa6a12793e9cb9c9;p=kivitendo-erp.git AttrDuration für minutes: _in_hours und _in_hours_as_number Generierte Helfer-Methoden für Attribute, die normalerweise Minuten speichern. Die Funktion `attribute_in_hours` rechnet die Minuten in Stunden um (beim Lesen) und umgekehrt (beim Schreiben). `attribute_in_hours_as_number` formatiert zusätzlich den Wert in Stunden zusätzlich in das Zahlenformat der Anwender*in bzw. parst dieses Format beim Schreiben. --- diff --git a/SL/DB/Helper/AttrDuration.pm b/SL/DB/Helper/AttrDuration.pm index 5bdd4bf7d..c627f190c 100644 --- a/SL/DB/Helper/AttrDuration.pm +++ b/SL/DB/Helper/AttrDuration.pm @@ -116,6 +116,22 @@ sub _make_minutes { my $as_minutes = "${attribute}_as_minutes"; return defined($self->$attribute) ? sprintf('%d:%02d', $self->$as_hours, $self->$as_minutes) : undef; }; + + *{ $package . '::' . $attribute . '_in_hours' } = sub { + my ($self, $value) = @_; + + $self->$attribute(int($value * 60 + 0.5)) if @_ > 1; + return $self->$attribute / 60.0; + }; + + *{ $package . '::' . $attribute . '_in_hours_as_number' } = sub { + my ($self, $value) = @_; + + my $sub = "${attribute}_in_hours"; + + $self->$sub($::form->parse_amount(\%::myconfig, $value)) if @_ > 1; + return $::form->format_amount(\%::myconfig, $self->$sub, 2); + }; } 1; @@ -234,6 +250,17 @@ Access the full value as a formatted string in the form C, e.g. C<1:30> for the value 90 minutes. Parsing such a string is supported, too. +=item C + +Access the full value but convert to and from hours when +reading/writing the value. + +=item C + +Access the full value but convert to and from hours when +reading/writing the value. The value is formatted to/parsed from the +user's number format. + =back =head1 FUNCTIONS diff --git a/t/db_helper/attr_duration.t b/t/db_helper/attr_duration.t index efc2d5402..0b023b66d 100644 --- a/t/db_helper/attr_duration.t +++ b/t/db_helper/attr_duration.t @@ -7,17 +7,18 @@ __PACKAGE__->meta->setup( columns => [ dummy => { type => 'numeric', precision => 2, scale => 12 }, inty => { type => 'integer' }, + miny => { type => 'integer' }, ] ); use SL::DB::Helper::AttrDuration; __PACKAGE__->attr_duration('dummy'); -__PACKAGE__->attr_duration_minutes('inty'); +__PACKAGE__->attr_duration_minutes('inty', 'miny'); package main; -use Test::More tests => 120; +use Test::More tests => 130; use Test::Exception; use strict; @@ -218,6 +219,22 @@ is($item->inty_as_minutes, 1, 'write as_duration_string 03:1 read a is($item->inty_as_hours, 3, 'write as_duration_string 03:1 read as_hours'); is($item->inty_as_duration_string, "3:01", 'write as_duration_string 03:1 read as_duration_string'); +local %::myconfig = (numberformat => "1.000,00"); + +$item = new_item(miny_in_hours => 2.5); +is($item->miny, 150, 'write in_hours 2.5 read raw'); +is($item->miny_as_minutes, 30, 'write in_hours 2.5 read as_minutes'); +is($item->miny_as_hours, 2, 'write in_hours 2.5 read as_hours'); +is($item->miny_in_hours, 2.5, 'write in_hours 2.5 read in_hours'); +is($item->miny_in_hours_as_number, '2,50', 'write in_hours 2.5 read in_hours_as_number'); + +$item = new_item(miny_in_hours_as_number => '4,25'); +is($item->miny, 255, 'write in_hours_as_number 4,25 read raw'); +is($item->miny_as_minutes, 15, 'write in_hours_as_number 4,25 read as_minutes'); +is($item->miny_as_hours, 4, 'write in_hours_as_number 4,25 read as_hours'); +is($item->miny_in_hours, 4.25, 'write in_hours_as_number 4,25 read in_hours'); +is($item->miny_in_hours_as_number, '4,25', 'write in_hours_as_number 4,25 read in_hours_as_number'); + # Parametervalidierung throws_ok { new_item()->inty_as_duration_string('invalid') } qr/invalid.*format/i, 'invalid duration format';