X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FAttr.pm;h=21645f4e15f308f6d3c47c98ed53e322de557ec9;hb=c235f7be04aeadf4c69612356c0453f11a3ea3b2;hp=8c99903d3686f5daddd9776017db93575838da23;hpb=07b14d1f5c08e87ac700564520bc70e1c1ea1923;p=kivitendo-erp.git diff --git a/SL/DB/Helper/Attr.pm b/SL/DB/Helper/Attr.pm index 8c99903d3..21645f4e1 100644 --- a/SL/DB/Helper/Attr.pm +++ b/SL/DB/Helper/Attr.pm @@ -29,11 +29,14 @@ sub make { sub _make_by_type { my ($package, $name, $type) = @_; - _as_number ($package, $name, places => -2) if $type =~ /numeric | real | float/xi; - _as_percent($package, $name, places => 2) if $type =~ /numeric | real | float/xi; - _as_number ($package, $name, places => 0) if $type =~ /int/xi; - _as_date ($package, $name) if $type =~ /date | timestamp/xi; - _as_bool_yn($package, $name) if $type =~ /bool/xi; + _as_number ($package, $name, places => -2) if $type =~ /numeric | real | float/xi; + _as_null_number($package, $name, places => -2) if $type =~ /numeric | real | float/xi; + _as_percent ($package, $name, places => 2) if $type =~ /numeric | real | float/xi; + _as_number ($package, $name, places => 0) if $type =~ /int/xi; + _as_null_number($package, $name, places => 0) if $type =~ /int/xi; + _as_date ($package, $name) if $type =~ /date | timestamp/xi; + _as_timestamp ($package, $name) if $type =~ /timestamp/xi; + _as_bool_yn ($package, $name) if $type =~ /bool/xi; } sub _as_number { @@ -53,6 +56,23 @@ sub _as_number { }; } +sub _as_null_number { + my $package = shift; + my $attribute = shift; + my %params = @_; + + $params{places} = 2 if !defined($params{places}); + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_null_number' } = sub { + my ($self, $string) = @_; + + $self->$attribute($string eq '' ? undef : $::form->parse_amount(\%::myconfig, $string)) if @_ > 1; + + return defined $self->$attribute ? $::form->format_amount(\%::myconfig, $self->$attribute, $params{places}) : ''; + }; +} + sub _as_percent { my $package = shift; my $attribute = shift; @@ -93,7 +113,7 @@ sub _as_date { return $self->$attribute ? $::locale->reformat_date( { dateformat => 'yy-mm-dd' }, - ( $self->$attribute eq 'now' + ( ($self->$attribute eq 'now' || $self->$attribute eq 'now()') ? DateTime->now : $self->$attribute )->ymd, @@ -105,6 +125,32 @@ sub _as_date { return 1; } +sub _as_timestamp { + my $package = shift; + my $attribute = shift; + my %params = @_; + + my $accessor = sub { + my ($precision, $self, $string) = @_; + + $self->$attribute($string ? $::locale->parse_date_to_object($string) : undef) if @_ > 2; + + my $dt = $self->$attribute; + return undef unless $dt; + + $dt = DateTime->now if !ref($dt) && ($dt eq 'now'); + + return $::locale->format_date_object($dt, precision => $precision); + }; + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_timestamp' } = sub { $accessor->('minute', @_) }; + *{ $package . '::' . $attribute . '_as_timestamp_s' } = sub { $accessor->('second', @_) }; + *{ $package . '::' . $attribute . '_as_timestamp_ms' } = sub { $accessor->('millisecond', @_) }; + + return 1; +} + sub _as_bool_yn { my ($package, $attribute, %params) = @_; @@ -158,6 +204,7 @@ None yet. =head1 AUTHOR -Sven Schöling +Sven Schöling , +Moritz Bunkus =cut