From 59626ee231b3487878faea59a2ca325e499ccb63 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Fri, 5 Jun 2020 14:26:35 +0200 Subject: [PATCH] Rose-Attr-Helfer: _as_null_number von odyn abgeguckt, aber nicht mit SL::Helper::Number implementiert (gibt es in kivitendo nicht) siehe auch odyn: commit b4177a76db52e94795314b527774f515fd8ee42f --- SL/DB/Helper/Attr.pm | 31 +++++++++++++++++++++++++------ t/helper/attr.t | 15 +++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/SL/DB/Helper/Attr.pm b/SL/DB/Helper/Attr.pm index 6f2dd55e2..21645f4e1 100644 --- a/SL/DB/Helper/Attr.pm +++ b/SL/DB/Helper/Attr.pm @@ -29,12 +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_timestamp($package, $name) if $type =~ /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 { @@ -54,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; diff --git a/t/helper/attr.t b/t/helper/attr.t index 5d6238ed9..a8f69f114 100644 --- a/t/helper/attr.t +++ b/t/helper/attr.t @@ -1,4 +1,4 @@ -use Test::More tests => 32; +use Test::More tests => 44; use lib 't'; @@ -23,6 +23,18 @@ is($p->sellprice_as_number, '2,30'); is($p->sellprice_as_number('2,3442'), '2,3442'); is($p->sellprice, 2.3442); is($p->sellprice_as_number, '2,3442'); +is($p->listprice_as_null_number('2,30'), '2,30'); +is($p->listprice, 2.30); +is($p->listprice_as_null_number, '2,30'); +is($p->listprice_as_null_number('2,3442'), '2,3442'); +is($p->listprice, 2.3442); +is($p->listprice_as_null_number, '2,3442'); +is($p->listprice_as_null_number(''), ''); +is($p->listprice, undef); +is($p->listprice_as_null_number, ''); +is($p->listprice_as_null_number('0'), '0,00'); +is($p->listprice, 0); +is($p->listprice_as_null_number, '0,00'); my $o = new_ok 'SL::DB::Order'; is($o->reqdate_as_date('11.12.2007'), '11.12.2007'); @@ -59,4 +71,3 @@ is $o->closed_as_bool_yn, 'Nein', 'bool 2'; # defaults according to the database $i->taxincluded(undef); is $i->taxincluded_as_bool_yn, '', 'bool 3'; - -- 2.20.1