Revision 7f883269
Von Sven Schöling vor mehr als 1 Jahr hinzugefügt
SL/DB/Helper/RecordLink.pm | ||
---|---|---|
1 |
package SL::DB::Helper::RecordLink; |
|
2 |
|
|
3 |
use strict; |
|
4 |
use parent qw(Exporter); |
|
5 |
|
|
6 |
use constant RECORD_ID => 'converted_from_record_id'; |
|
7 |
use constant RECORD_TYPE_REF => 'converted_from_record_type_ref'; |
|
8 |
use constant RECORD_ITEM_ID => 'converted_from_record_item_id'; |
|
9 |
use constant RECORD_ITEM_TYPE_REF => 'converted_from_record_item_type_ref'; |
|
10 |
|
|
11 |
our @EXPORT_OK = qw(RECORD_ID RECORD_TYPE_REF RECORD_ITEM_ID RECORD_ITEM_TYPE_REF); |
|
12 |
|
|
13 |
|
|
14 |
sub link_records { |
|
15 |
my ($self, $allowed_linked_records, $allowed_linked_record_items) = @_; |
|
16 |
|
|
17 |
my %allowed_linked_records = map {$_ => 1} @$allowed_linked_records; |
|
18 |
my %allowed_linked_record_items = map {$_ => 1} @$allowed_linked_record_items; |
|
19 |
|
|
20 |
return 1 unless my $from_record_id = $self->{RECORD_ID()}; |
|
21 |
|
|
22 |
my $from_record_type = $self->{RECORD_TYPE_REF()}; |
|
23 |
unless ($allowed_linked_records{$from_record_type}) { |
|
24 |
croak("Not allowed @{[ RECORD_TYPE_REF ]}: $from_record_type"); |
|
25 |
} |
|
26 |
|
|
27 |
$from_record_type->new(id => $from_record_id)->load |
|
28 |
->link_to_record($self); |
|
29 |
|
|
30 |
#clear converted_from; |
|
31 |
delete $self->{$_} for RECORD_ID, RECORD_TYPE_REF; |
|
32 |
|
|
33 |
for my $item (@{ $self->items_sorted }) { |
|
34 |
link_record_item($self, $item, \%allowed_linked_record_items); |
|
35 |
} |
|
36 |
|
|
37 |
1; |
|
38 |
} |
|
39 |
|
|
40 |
sub link_record_item { |
|
41 |
my ($self, $record_item, $allowed_linked_record_items) = @_; |
|
42 |
|
|
43 |
return 1 unless my $from_item_id = $record_item->{RECORD_ITEM_ID()}; |
|
44 |
|
|
45 |
my $from_item_type = $record_item->{RECORD_ITEM_TYPE_REF()}; |
|
46 |
unless ($allowed_linked_record_items->{$from_item_type}) { |
|
47 |
croak("Not allowed @{[ RECORD_ITEM_TYPE_REF() ]}: $from_item_type"); |
|
48 |
} |
|
49 |
|
|
50 |
$from_item_type->new(id => $from_item_id)->load |
|
51 |
->link_to_record($record_item); |
|
52 |
|
|
53 |
#clear converted_from; |
|
54 |
delete $record_item->{$_} for RECORD_ITEM_ID, RECORD_ITEM_TYPE_REF; |
|
55 |
} |
|
56 |
|
|
57 |
1; |
|
58 |
|
|
59 |
__END__ |
|
60 |
|
|
61 |
=encoding utf-8 |
|
62 |
|
|
63 |
=head1 NAME |
|
64 |
|
|
65 |
SL::DB::Helper::RecordLink - creates record links that are stored in the gived objects |
|
66 |
|
|
67 |
=head1 SYNOPSIS |
|
68 |
|
|
69 |
# in the consuming class |
|
70 |
__PCAKAGE__->after_save_hook("link_records_hook"); |
|
71 |
|
|
72 |
sub link_records_hook { |
|
73 |
my ($self) = @_; |
|
74 |
SL::DB::Helper::RecordLink::link_records( |
|
75 |
$self, |
|
76 |
qw(SL::DB::Order), # list of allowed record sources |
|
77 |
qw(SL::DB::OrderItem), # list of allowed record item sources |
|
78 |
) |
|
79 |
} |
|
80 |
|
|
81 |
=head1 DESCRIPTION |
|
82 |
|
|
83 |
... |
|
84 |
|
|
85 |
=head1 METHODS |
|
86 |
|
|
87 |
... |
|
88 |
|
|
89 |
=head1 BUGS |
|
90 |
|
|
91 |
None yet. :) |
|
92 |
|
|
93 |
=head1 AUTHOR |
|
94 |
|
|
95 |
Sven Schöling $<lt>s.schoeling@googlemail.comE<gt> |
|
96 |
|
|
97 |
=cut |
SL/DB/Reclamation.pm | ||
---|---|---|
18 | 18 |
use SL::DB::Helper::PriceTaxCalculator; |
19 | 19 |
use SL::DB::Helper::PriceUpdater; |
20 | 20 |
use SL::DB::Helper::TransNumberGenerator; |
21 |
use SL::DB::Helper::RecordLink qw(RECORD_ID RECORD_TYPE_REF); |
|
21 | 22 |
use SL::Locale::String qw(t8); |
22 | 23 |
use SL::RecordLinks; |
23 | 24 |
use Rose::DB::Object::Helpers qw(as_tree strip); |
... | ... | |
85 | 86 |
|
86 | 87 |
sub _after_save_link_records { |
87 | 88 |
my ($self) = @_; |
88 |
my %allowed_linked_records = map {$_ => 1} qw( |
|
89 |
SL::DB::Reclamation |
|
90 |
SL::DB::Order |
|
91 |
SL::DB::DeliveryOrder |
|
92 |
SL::DB::Invoice |
|
93 |
SL::DB::PurchaseInvoice |
|
94 |
); |
|
95 |
my %allowed_linked_record_items = map {$_ => 1} qw( |
|
96 |
SL::DB::ReclamationItem |
|
97 |
SL::DB::OrderItem |
|
98 |
SL::DB::DeliveryOrderItem |
|
99 |
SL::DB::InvoiceItem |
|
100 |
); |
|
101 | 89 |
|
102 |
my $from_record_id = $self->{converted_from_record_id}; |
|
103 |
if ($from_record_id) { |
|
104 |
my $from_record_type = $self->{converted_from_record_type_ref}; |
|
105 |
unless ($allowed_linked_records{$from_record_type}) { |
|
106 |
croak("Not allowed converted_from_record_type_ref: '" . $from_record_type); |
|
107 |
} |
|
108 |
my $src = ${from_record_type}->new(id => $from_record_id)->load; |
|
109 |
$src->link_to_record($self); |
|
110 |
#clear converted_from; |
|
111 |
delete $self->{$_} for qw(converted_from_record_id converted_from_record_type_ref); |
|
112 |
|
|
113 |
if (scalar @{ $self->items_sorted || [] }) { |
|
114 |
for my $idx (0 .. $#{ $self->items_sorted }) { |
|
115 |
my $reclamation_item = $self->items_sorted->[$idx]; |
|
116 |
my $from_item_id = $reclamation_item->{converted_from_record_item_id}; |
|
117 |
next if !$from_item_id; |
|
118 |
my $from_item_type = $reclamation_item->{converted_from_record_item_type_ref}; |
|
119 |
unless ($allowed_linked_record_items{$from_item_type}) { |
|
120 |
croak("Not allowed converted_from_record_item_type_ref: '" . $from_item_type); |
|
121 |
} |
|
122 |
my $src_item = ${from_item_type}->new(id => $from_item_id)->load; |
|
123 |
$src_item->link_to_record($reclamation_item); |
|
124 |
#clear converted_from; |
|
125 |
delete $reclamation_item->{$_} for qw(converted_from_record_item_id converted_from_record_item_type_ref); |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
90 |
my @allowed_record_sources = qw(SL::DB::Reclamation SL::DB::Order SL::DB::DeliveryOrder SL::DB::Invoice SL::DB::PurchaseInvoice); |
|
91 |
my @allowed_item_sources = qw(SL::DB::ReclamationItem SL::DB::OrderItem SL::DB::DeliveryOrderItem SL::DB::InvoiceItem); |
|
129 | 92 |
|
130 |
return 1; |
|
93 |
SL::DB::Helper::RecordLink::link_records( |
|
94 |
$self, |
|
95 |
\@allowed_record_sources, |
|
96 |
\@allowed_item_sources, |
|
97 |
); |
|
131 | 98 |
} |
132 | 99 |
|
133 | 100 |
# methods |
... | ... | |
509 | 476 |
$reclamation->assign_attributes(%{ $params{attributes} }) if $params{attributes}; |
510 | 477 |
|
511 | 478 |
unless ($params{no_linked_records}) { |
512 |
$reclamation->{"converted_from_record_type_ref"} = ref($source);
|
|
513 |
$reclamation->{"converted_from_record_id"} = $source->id;
|
|
479 |
$reclamation->{RECORD_TYPE_REF()} = ref($source);
|
|
480 |
$reclamation->{RECORD_ID()} = $source->id;
|
|
514 | 481 |
}; |
515 | 482 |
|
516 | 483 |
my $items = delete($params{items}) || $source->items; |
SL/DB/ReclamationItem.pm | ||
---|---|---|
12 | 12 |
use SL::DB::Helper::ActsAsList; |
13 | 13 |
use SL::DB::Helper::LinkedRecords; |
14 | 14 |
use SL::DB::Helper::RecordItem; |
15 |
use SL::DB::Helper::RecordLink qw(RECORD_ITEM_ID RECORD_ITEM_TYPE_REF); |
|
15 | 16 |
use SL::DB::Helper::CustomVariables ( |
16 | 17 |
sub_module => 'reclamation_items', |
17 | 18 |
cvars_alias => 1, |
... | ... | |
30 | 31 |
sub is_linked_to_record { |
31 | 32 |
my ($self) = @_; |
32 | 33 |
|
33 |
if(scalar(@{$self->linked_records}) || $self->{converted_from_record_item_type_ref}) {
|
|
34 |
if(scalar(@{$self->linked_records}) || $self->{RECORD_ITEM_TYPE_REF()}) {
|
|
34 | 35 |
return 1; |
35 | 36 |
} |
36 | 37 |
|
... | ... | |
99 | 100 |
$item->assign_attributes(%{ $params{attributes} }) if $params{attributes}; |
100 | 101 |
|
101 | 102 |
unless ($params{no_linked_records}) { |
102 |
$item->{"converted_from_record_item_type_ref"} = ref($source);
|
|
103 |
$item->{"converted_from_record_item_id"} = $source->id;
|
|
103 |
$item->{RECORD_ITEM_TYPE_REF()} = ref($source);
|
|
104 |
$item->{RECORD_ITEM_ID()} = $source->id;
|
|
104 | 105 |
} |
105 | 106 |
|
106 | 107 |
return $item; |
Auch abrufbar als: Unified diff
RecordLinks: Reclamation after save hook in ein SL::DB::Helper modul verschoben
RecordLinks werden in reclamations in einem after save hook behandelt,
und sind damit deutlich robuster als die manuelle Behandlung. Die
ausgelagerte Variante hat jetzt auch Konstanten für die magischen
Strings in den Objekten.