Revision 6d14c91b
Von Kivitendo Admin vor 9 Tagen hinzugefügt
SL/DB/PartsGroup.pm | ||
---|---|---|
8 | 8 |
use SL::DB::MetaSetup::PartsGroup; |
9 | 9 |
use SL::DB::Manager::PartsGroup; |
10 | 10 |
use SL::DB::Helper::ActsAsList; |
11 |
use SL::DB::Helper::AttrSorted; |
|
12 |
use SL::DBUtils qw(selectall_array_query); |
|
13 |
|
|
14 |
__PACKAGE__->configure_acts_as_list(group_by => [qw(parent_id)]); |
|
15 |
__PACKAGE__->attr_sorted({ unsorted => 'children', position => 'sortkey' }); |
|
11 | 16 |
|
12 | 17 |
__PACKAGE__->meta->add_relationship( |
13 | 18 |
custom_variable_configs => { |
... | ... | |
18 | 23 |
type => 'one to many', |
19 | 24 |
class => 'SL::DB::Part', |
20 | 25 |
column_map => { id => 'partsgroup_id' }, |
26 |
add_methods => ['count'], |
|
21 | 27 |
}, |
28 |
children => { |
|
29 |
type => 'one to many', |
|
30 |
class => 'SL::DB::PartsGroup', |
|
31 |
column_map => { id => 'parent_id' }, |
|
32 |
add_methods => ['count'], |
|
33 |
} |
|
22 | 34 |
); |
23 | 35 |
|
24 | 36 |
__PACKAGE__->meta->initialize; |
... | ... | |
26 | 38 |
sub displayable_name { |
27 | 39 |
my $self = shift; |
28 | 40 |
|
29 |
return join ' ', grep $_, $self->id, $self->partsgroup; |
|
41 |
return $self->partsgroup; |
|
42 |
} |
|
43 |
|
|
44 |
sub indented_name { |
|
45 |
my $self = shift; |
|
46 |
# used for label in select_tag |
|
47 |
|
|
48 |
return ' - ' x $self->get_level . $self->partsgroup; |
|
30 | 49 |
} |
31 | 50 |
|
32 | 51 |
sub validate { |
... | ... | |
59 | 78 |
eval "require SL::DB::PriceRuleItem"; |
60 | 79 |
return 0 if SL::DB::Manager::PriceRuleItem->get_all_count(query => [ type => 'partsgroup', value_int => $self->id ]); |
61 | 80 |
|
81 |
return 0 if SL::DB::Manager::PartsGroup->get_all_count(query => [ parent_id => $self->id ]); |
|
82 |
|
|
62 | 83 |
return 1; |
63 | 84 |
} |
64 | 85 |
|
86 |
sub ancestors { |
|
87 |
my ($self) = @_; |
|
88 |
|
|
89 |
my @ancestors = (); |
|
90 |
my $pg = $self; |
|
91 |
|
|
92 |
return \@ancestors unless defined $self->parent; |
|
93 |
|
|
94 |
while ( $pg->parent_id ) { |
|
95 |
$pg = $pg->parent; |
|
96 |
unshift(@ancestors, $pg); |
|
97 |
}; |
|
98 |
|
|
99 |
return \@ancestors; |
|
100 |
} |
|
101 |
|
|
102 |
sub ancestor_ids { |
|
103 |
my ($self) = @_; |
|
104 |
|
|
105 |
my $query = <<SQL; |
|
106 |
WITH RECURSIVE rec (id) as |
|
107 |
( |
|
108 |
SELECT partsgroup.id, partsgroup.parent_id from partsgroup where partsgroup.id = ? |
|
109 |
UNION ALL |
|
110 |
SELECT partsgroup.id, partsgroup.parent_id from rec, partsgroup where partsgroup.id = rec.parent_id |
|
111 |
) |
|
112 |
SELECT id as ancestors |
|
113 |
FROM rec |
|
114 |
SQL |
|
115 |
my @ids = selectall_array_query($::form, $self->dbh, $query, $self->id); |
|
116 |
return \@ids; |
|
117 |
} |
|
118 |
|
|
119 |
sub partsgroup_iterator_dfs { |
|
120 |
# partsgroup iterator that starts with a partsgroup, using depth first search |
|
121 |
# to iterate over partsgroups you have to first find the roots (level 0) and |
|
122 |
# then use this method to recursively dig down and find all the children |
|
123 |
my ($self) = @_; |
|
124 |
my @queue ; |
|
125 |
|
|
126 |
@queue = @{ $self->children_sorted } if $self->children_count; |
|
127 |
|
|
128 |
return sub { |
|
129 |
while ( @queue ) { |
|
130 |
my $pg = shift @queue; |
|
131 |
|
|
132 |
if ( scalar @{ $pg->children_sorted } ) { |
|
133 |
unshift @queue, @{ $pg->children_sorted }; |
|
134 |
}; |
|
135 |
return $pg; |
|
136 |
}; |
|
137 |
return; |
|
138 |
}; |
|
139 |
} |
|
140 |
|
|
141 |
sub get_level { |
|
142 |
my ($self) = @_; |
|
143 |
# iterate through parents to calculate the level |
|
144 |
|
|
145 |
return 0 unless defined $self->parent; |
|
146 |
return $self->{cached_level} if exists $self->{cached_level}; |
|
147 |
my $level = 1; |
|
148 |
my $parent = $self->parent; |
|
149 |
while ( $parent->parent ) { |
|
150 |
$level++; |
|
151 |
$parent = $parent->parent; |
|
152 |
}; |
|
153 |
return $self->{cached_level} = $level; |
|
154 |
} |
|
155 |
|
|
65 | 156 |
1; |
Auch abrufbar als: Unified diff
Warengruppen hierarchisch - DB