RDBO Support.
[kivitendo-erp.git] / SL / DB / Helpers / AttrDate.pm
1 package SL::DB::Helpers::AttrDate;
2
3 use strict;
4
5 use Carp;
6 use English;
7
8 sub define {
9   my $package     = shift;
10   my $attribute   = shift;
11   my %params      = @_;
12
13   $params{places} = 2 if !defined($params{places});
14
15   my $code        = <<CODE;
16 package ${package};
17
18 sub ${attribute}_as_date {
19   my \$self = shift;
20
21   if (scalar \@_) {
22     if (\$_[0]) {
23       my (\$yy, \$mm, \$dd) = \$::locale->parse_date(\\\%::myconfig, \@_);
24       \$self->${attribute}(DateTime->new(year => \$yy, month => \$mm, day => \$dd));
25     } else {
26       \$self->${attribute}(undef);
27     }
28   }
29
30   return \$self->${attribute} ? \$::locale->reformat_date({ dateformat => 'yy-mm-dd' }, \$self->${attribute}->ymd, \$::myconfig{dateformat}) : undef;
31 }
32
33 1;
34 CODE
35
36   eval $code;
37   croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR;
38
39   return 1;
40 }
41
42 1;