Attribute Helper umgeschrieben.
[kivitendo-erp.git] / SL / DB / Helpers / Attr.pm
1 package SL::DB::Helper::Attr;
2
3 use strict;
4
5 sub auto_make {
6   my ($package, %params) = @_;
7
8   for my $col ($package->meta->columns) {
9     next if $col->primary_key_position; # don't make attr helper for primary keys
10     _make_by_type($package, $col->name, $col->type);
11   }
12
13   return $package;
14 }
15
16 sub make {
17   my ($package, %params) = @_;
18
19   for my $name (keys %params) {
20     my @types = ref $params{$name} eq 'ARRAY' ? @{ $params{$name} } : ($params{$name});
21     for my $type (@types) {
22       _make_by_type($package, $name, $type);
23     }
24   }
25   return $package;
26 }
27
28
29
30 sub _make_by_type {
31   my ($package, $name, $type) = @_;
32   _as_number ($package, $name, places => -2) if $type =~ /numeric | real | float/xi;
33   _as_percent($package, $name, places =>  0) if $type =~ /numeric | real | float/xi;
34   _as_number ($package, $name, places =>  0) if $type =~ /int/xi;
35   _as_date   ($package, $name)               if $type =~ /date | timestamp/xi;
36 }
37
38 sub _as_number {
39   my $package     = shift;
40   my $attribute   = shift;
41   my %params      = @_;
42
43   $params{places} = 2 if !defined($params{places});
44
45   no strict 'refs';
46   *{ $package . '::' . $attribute . '_as_number' } = sub {
47     my ($self, $string) = @_;
48
49     $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
50
51     return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
52   };
53 }
54
55 sub _as_percent {
56   my $package     = shift;
57   my $attribute   = shift;
58   my %params      = @_;
59
60   $params{places} = 2 if !defined($params{places});
61
62   no strict 'refs';
63   *{ $package . '::' . $attribute . '_as_percent' } = sub {
64     my ($self, $string) = @_;
65
66     $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
67
68     return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
69   };
70
71   return 1;
72 }
73
74 sub _as_date {
75   my $package     = shift;
76   my $attribute   = shift;
77   my %params      = @_;
78
79   no strict 'refs';
80   *{ $package . '::' . $attribute . '_as_date' } = sub {
81     my ($self, $string) = @_;
82
83     if (@_ > 1) {
84       if ($string) {
85         my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
86         $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
87       } else {
88         $self->$attribute(undef);
89       }
90     }
91
92     return $self->$attribute
93       ? $::locale->reformat_date(
94           { dateformat => 'yy-mm-dd' },
95           ( $self->$attribute eq 'now'
96              ? DateTime->now
97              : $self->$attribute
98           )->ymd,
99           $::myconfig{dateformat}
100         )
101       : undef;
102   };
103
104   return 1;
105 }
106
107 1;
108
109
110 1;
111
112 __END__
113
114 =head1 NAME
115
116 SL::DB::Helpers::Attr - attribute helpers
117
118 =head1 SYNOPSIS
119
120   use SL::DB::Helpers::Attr;
121   SL::DB::Helpers::Attr::make($class,
122     method_name => 'numeric(15,5)',
123     datemethod  => 'date'
124   );
125   SL::DB::Helpers::Attr::auto_make($class);
126
127 =head1 DESCRIPTION
128
129 =head1 FUNCTIONS
130
131 =head1 BUGS
132
133 =head1 AUTHOR
134
135 =cut