Revision 78034f24
Von Sven Schöling vor etwa 14 Jahren hinzugefügt
SL/DB/Business.pm | ||
---|---|---|
7 | 7 |
|
8 | 8 |
use SL::DB::MetaSetup::Business; |
9 | 9 |
|
10 |
__PACKAGE__->attr_percent('discount', places => -2); |
|
11 |
|
|
12 | 10 |
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. |
13 | 11 |
__PACKAGE__->meta->make_manager_class; |
14 | 12 |
|
SL/DB/DeliveryOrder.pm | ||
---|---|---|
8 | 8 |
|
9 | 9 |
use List::Util qw(first); |
10 | 10 |
|
11 |
for my $field (qw(transdate reqdate)) { |
|
12 |
__PACKAGE__->attr_date($field); |
|
13 |
} |
|
14 |
|
|
15 | 11 |
__PACKAGE__->meta->add_relationship(orderitems => { type => 'one to many', |
16 | 12 |
class => 'SL::DB::DeliveryOrderItem', |
17 | 13 |
column_map => { id => 'trans_id' }, |
SL/DB/DeliveryOrderItem.pm | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use SL::DB::MetaSetup::DeliveryOrderItem; |
6 | 6 |
|
7 |
for my $field (qw(qty sellprice discount base_qty lastcost price_factor marge_price_factor)) { |
|
8 |
__PACKAGE__->attr_number($field, places => -2); |
|
9 |
} |
|
10 |
|
|
11 | 7 |
__PACKAGE__->meta->make_manager_class; |
12 | 8 |
|
13 | 9 |
# methods |
SL/DB/DeliveryOrderItemsStock.pm | ||
---|---|---|
7 | 7 |
|
8 | 8 |
use SL::DB::MetaSetup::DeliveryOrderItemsStock; |
9 | 9 |
|
10 |
for my $field (qw(qty)) { |
|
11 |
__PACKAGE__->attr_number($field, places => -2); |
|
12 |
} |
|
13 |
|
|
14 | 10 |
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. |
15 | 11 |
__PACKAGE__->meta->make_manager_class; |
16 | 12 |
|
SL/DB/GLTransaction.pm | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use SL::DB::MetaSetup::GLTransaction; |
6 | 6 |
|
7 |
for my $field (qw(transdate gldate)) { |
|
8 |
__PACKAGE__->attr_date($field); |
|
9 |
} |
|
10 |
|
|
11 | 7 |
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. |
12 | 8 |
__PACKAGE__->meta->make_manager_class; |
13 | 9 |
|
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 |
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 |
no strict 'refs'; |
|
14 |
*{ $package . '::' . $attribute . '_as_date' } = sub { |
|
15 |
my ($self, $string) = @_; |
|
16 |
|
|
17 |
if (@_ > 1) { |
|
18 |
if ($string) { |
|
19 |
my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string); |
|
20 |
$self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd)); |
|
21 |
} else { |
|
22 |
$self->$attribute(undef); |
|
23 |
} |
|
24 |
} |
|
25 |
|
|
26 |
return $self->$attribute |
|
27 |
? $::locale->reformat_date( |
|
28 |
{ dateformat => 'yy-mm-dd' }, |
|
29 |
$self->${attribute}->ymd, |
|
30 |
$::myconfig{dateformat} |
|
31 |
) |
|
32 |
: undef; |
|
33 |
}; |
|
34 |
|
|
35 |
return 1; |
|
36 |
} |
|
37 |
|
|
38 |
1; |
SL/DB/Helpers/AttrNumber.pm | ||
---|---|---|
1 |
package SL::DB::Helpers::AttrNumber; |
|
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 |
no strict 'refs'; |
|
16 |
*{ $package . '::' . $attribute . '_as_number' } = sub { |
|
17 |
my ($self, $string) = @_; |
|
18 |
|
|
19 |
$self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1; |
|
20 |
|
|
21 |
return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places}); |
|
22 |
}; |
|
23 |
|
|
24 |
return 1; |
|
25 |
} |
|
26 |
|
|
27 |
1; |
SL/DB/Helpers/AttrPercent.pm | ||
---|---|---|
1 |
package SL::DB::Helpers::AttrPercent; |
|
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 |
no strict 'refs'; |
|
16 |
*{ $package . '::' . $attribute . '_as_percent' } = sub { |
|
17 |
my ($self, $string) = @_; |
|
18 |
|
|
19 |
$self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1; |
|
20 |
|
|
21 |
return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places}); |
|
22 |
}; |
|
23 |
|
|
24 |
return 1; |
|
25 |
} |
|
26 |
|
|
27 |
1; |
SL/DB/Helpers/Metadata.pm | ||
---|---|---|
15 | 15 |
return 'SL::DB::Helpers::Manager'; |
16 | 16 |
} |
17 | 17 |
|
18 |
sub initialize { |
|
19 |
my $self = shift; |
|
20 |
$self->make_attr_auto_helpers; |
|
21 |
$self->SUPER::initialize(@_); |
|
22 |
} |
|
23 |
|
|
24 |
sub make_attr_helpers { |
|
25 |
my ($self, %params) = @_; |
|
26 |
SL::DB::Helper::Attr::make($self->class, %params); |
|
27 |
} |
|
28 |
|
|
29 |
sub make_attr_auto_helpers { |
|
30 |
my ($self) = @_; |
|
31 |
SL::DB::Helper::Attr::auto_make($self->class); |
|
32 |
} |
|
33 |
|
|
18 | 34 |
1; |
SL/DB/Invoice.pm | ||
---|---|---|
10 | 10 |
use SL::DB::MetaSetup::Invoice; |
11 | 11 |
use SL::DB::Manager::Invoice; |
12 | 12 |
|
13 |
__PACKAGE__->attr_number($_, places => -2) for qw(amount netamount paid marge_total marge_percent taxamount); |
|
14 |
__PACKAGE__->attr_date($_) for qw(transdate gldate datepaid duedate deliverydate orddate quodate); |
|
15 |
__PACKAGE__->attr_percent($_) for qw(abschlag_percentage); |
|
16 |
|
|
17 | 13 |
__PACKAGE__->meta->add_relationship( |
18 | 14 |
invoiceitems => { |
19 | 15 |
type => 'one to many', |
... | ... | |
59 | 55 |
return $self->amount - $self->netamount; |
60 | 56 |
} |
61 | 57 |
|
58 |
__PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)'); |
|
59 |
|
|
62 | 60 |
1; |
SL/DB/InvoiceItem.pm | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use SL::DB::MetaSetup::InvoiceItem; |
6 | 6 |
|
7 |
for my $field (qw( |
|
8 |
qty allocated sellprice fxsellprice discount base_qty marge_total |
|
9 |
marge_percent lastcost price_factor marge_price_factor |
|
10 |
)) { |
|
11 |
__PACKAGE__->attr_number($field, places => -2); |
|
12 |
} |
|
13 |
|
|
14 | 7 |
__PACKAGE__->meta->add_relationship( |
15 | 8 |
part => { |
16 | 9 |
type => 'one to one', |
SL/DB/Object.pm | ||
---|---|---|
7 | 7 |
use List::MoreUtils qw(any); |
8 | 8 |
|
9 | 9 |
use SL::DB; |
10 |
use SL::DB::Helpers::AttrNumber; |
|
11 |
use SL::DB::Helpers::AttrDate; |
|
12 |
use SL::DB::Helpers::AttrPercent; |
|
10 |
use SL::DB::Helpers::Attr; |
|
13 | 11 |
use SL::DB::Helpers::Metadata; |
14 | 12 |
use SL::DB::Helpers::Manager; |
15 | 13 |
|
... | ... | |
80 | 78 |
return $self; |
81 | 79 |
} |
82 | 80 |
|
83 |
sub make_attr_helper { |
|
84 |
my ($self) = @_; |
|
85 |
my $package = ref $self || $self; |
|
86 |
|
|
87 |
for my $col ($package->meta->columns) { |
|
88 |
next if $col->primary_key_position; # don't make attr helper for primary keys |
|
89 |
|
|
90 |
attr_number ($package, $col->name, -2) if $col->type =~ /numeric | real | float/xi; |
|
91 |
attr_percent($package, $col->name, -2) if $col->type =~ /numeric | real | float/xi; |
|
92 |
attr_number ($package, $col->name, 0) if $col->type =~ /int/xi; |
|
93 |
attr_date ($package, $col->name) if $col->type =~ /date | timestamp/xi; |
|
94 |
} |
|
95 |
|
|
96 |
return $self; |
|
97 |
} |
|
98 |
|
|
99 |
sub attr_number { |
|
100 |
SL::DB::Helpers::AttrNumber::define(@_); |
|
101 |
} |
|
102 |
|
|
103 |
sub attr_date { |
|
104 |
SL::DB::Helpers::AttrDate::define(@_); |
|
105 |
} |
|
106 |
|
|
107 |
sub attr_percent { |
|
108 |
SL::DB::Helpers::AttrPercent::define(@_); |
|
109 |
} |
|
110 |
|
|
111 | 81 |
1; |
112 | 82 |
|
113 | 83 |
__END__ |
SL/DB/Order.pm | ||
---|---|---|
8 | 8 |
use SL::DB::Manager::Order; |
9 | 9 |
use SL::DB::Invoice; |
10 | 10 |
|
11 |
__PACKAGE__->attr_number($_, places => -2) for qw(amount netamount marge_total marge_percent); |
|
12 |
__PACKAGE__->attr_date($_) for qw(transdate reqdate); |
|
13 |
__PACKAGE__->attr_percent($_) for qw(marge_percent); |
|
14 |
|
|
15 | 11 |
__PACKAGE__->meta->add_relationship( |
16 | 12 |
orderitems => { |
17 | 13 |
type => 'one to many', |
SL/DB/Part.pm | ||
---|---|---|
7 | 7 |
use SL::DB::MetaSetup::Part; |
8 | 8 |
use SL::DB::Manager::Part; |
9 | 9 |
|
10 |
__PACKAGE__->attr_number('lastcost', places => -2); |
|
11 |
__PACKAGE__->attr_number('listprice', places => -2); |
|
12 |
__PACKAGE__->attr_number('sellprice', places => -2); |
|
13 |
|
|
14 | 10 |
__PACKAGE__->meta->add_relationships( |
15 | 11 |
unit_obj => { |
16 | 12 |
type => 'one to one', |
SL/DB/PurchaseInvoice.pm | ||
---|---|---|
5 | 5 |
use SL::DB::MetaSetup::PurchaseInvoice; |
6 | 6 |
use SL::DB::Manager::PurchaseInvoice; |
7 | 7 |
|
8 |
for my $field (qw(transdate gldate datepaid duedate orddate quodate)) { |
|
9 |
__PACKAGE__->attr_date($field); |
|
10 |
} |
|
11 |
|
|
12 | 8 |
__PACKAGE__->meta->add_relationship(invoiceitems => { type => 'one to many', |
13 | 9 |
class => 'SL::DB::InvoiceItem', |
14 | 10 |
column_map => { id => 'trans_id' }, |
t/helper/attr.t | ||
---|---|---|
1 |
use Test::More tests => 25;
|
|
1 |
use Test::More tests => 29;
|
|
2 | 2 |
|
3 | 3 |
use DateTime; |
4 | 4 |
|
5 | 5 |
use_ok 'SL::DB::Part'; |
6 | 6 |
use_ok 'SL::DB::Order'; |
7 |
use_ok 'SL::DB::Invoice'; |
|
7 | 8 |
use_ok 'SL::Dispatcher'; |
8 | 9 |
|
9 | 10 |
SL::Dispatcher::pre_startup_setup(); |
... | ... | |
30 | 31 |
$o->reqdate(DateTime->new(year => 2010, month => 4, day => 12)); |
31 | 32 |
is($o->reqdate_as_date, '12.04.2010'); |
32 | 33 |
|
33 |
is($o->marge_percent_as_percent('40'), '40,00');
|
|
34 |
is($o->marge_percent_as_percent('40'), '40'); |
|
34 | 35 |
is($o->marge_percent, 0.40); |
35 |
is($o->marge_percent_as_percent, '40,00');
|
|
36 |
is($o->marge_percent_as_percent('22,4'), '22,40');
|
|
36 |
is($o->marge_percent_as_percent, '40'); |
|
37 |
is($o->marge_percent_as_percent('22,4'), '22'); |
|
37 | 38 |
is($o->marge_percent, 0.224); |
38 |
is($o->marge_percent_as_percent, '22,40');
|
|
39 |
is($o->marge_percent_as_percent, '22'); |
|
39 | 40 |
is($o->marge_percent(0.231), 0.231); |
40 |
is($o->marge_percent_as_percent, '23,10'); |
|
41 |
is($o->marge_percent_as_percent, '23'); |
|
42 |
|
|
43 |
# overloaded attr: invoice taxamount |
|
44 |
my $i = new_ok 'SL::DB::Invoice'; |
|
45 |
|
|
46 |
is($i->taxamount_as_number, '0,00'); |
|
47 |
$i->amount(12); |
|
48 |
$i->netamount(10.34); |
|
49 |
is($i->taxamount_as_number, '1,66'); |
|
50 |
|
Auch abrufbar als: Unified diff
Attribute Helper umgeschrieben.
Siehe Dokumentation SL::DB::Helpers::Attr.
Attributhelper werden jetzt beim Rose Start automatisch geladen.
numeric Felder bekommen immer einen as_number udn einen as_percent helper.
date Felder bekommen immer einen as_date helper.
as_date Helper kann jetzt auch mit 'now' umgehen.
Zusaätzliche Helper können zur Compilezeit mit
erstellt werden, wobei 'type' einfach das ist, was auch bei der autdetection
ind er Datenbank erkannt wird, z.B. "numeric(15,5)" oder "date". Die passenden
Helper werden dann installiert.