1 package SL::DB::Manager::Part;
5 use SL::DB::Helper::Manager;
6 use SL::DB::Helper::Sorted;
7 use SL::DB::Helper::Paginated;
8 use SL::DB::Helper::Filtered;
9 use base qw(SL::DB::Helper::Manager);
13 use SL::MoreCommon qw(listify);
15 sub object_class { 'SL::DB::Part' }
17 __PACKAGE__->make_manager_methods;
18 __PACKAGE__->add_filter_specs(
20 my ($key, $value, $prefix) = @_;
21 return __PACKAGE__->type_filter($value, $prefix);
24 my ($key, $value, $prefix) = @_;
25 return or => [ map { $prefix . $_ => $value } qw(partnumber description) ]
30 my ($class, $type, $prefix) = @_;
32 return () unless $type;
36 # this is to make selection like type => { part => 1, service => 1 } work
37 if ('HASH' eq ref $type) {
38 $type = [ grep { $type->{$_} } keys %$type ];
41 my @types = grep { $_ } listify($type);
44 for my $type (@types) {
45 if ($type =~ m/^part/) {
46 push @filter, (and => [ or => [ $prefix . assembly => 0, $prefix . assembly => undef ],
47 "!${prefix}inventory_accno_id" => 0,
48 "!${prefix}inventory_accno_id" => undef,
50 } elsif ($type =~ m/^service/) {
51 push @filter, (and => [ or => [ $prefix . assembly => 0, $prefix . assembly => undef ],
52 or => [ $prefix . inventory_accno_id => 0, $prefix . inventory_accno_id => undef ],
54 } elsif ($type =~ m/^assembl/) {
55 push @filter, ($prefix . assembly => 1);
59 return @filter > 2 ? (or => \@filter) : @filter;
66 return () unless @part_ids;
68 my $placeholders = join ',', ('?') x @part_ids;
70 SELECT oi.parts_id, SUM(oi.base_qty) AS qty
72 LEFT JOIN oe ON (oi.trans_id = oe.id)
73 WHERE (oi.parts_id IN ($placeholders))
74 AND (NOT COALESCE(oe.quotation, FALSE))
75 AND (NOT COALESCE(oe.closed, FALSE))
76 AND (NOT COALESCE(oe.delivered, FALSE))
77 AND (COALESCE(oe.vendor_id, 0) <> 0)
81 my %qty_by_id = map { $_->{parts_id} => $_->{qty} * 1 } @{ selectall_hashref_query($::form, $class->object_class->init_db->dbh, $query, @part_ids) };
82 map { $qty_by_id{$_} ||= 0 } @part_ids;
89 default => [ 'partnumber', 1 ],
106 SL::DB::Manager::Part - RDBO manager for the C<parts> table
112 =item C<get_ordered_qty @part_ids>
114 For each of the given part IDs the ordered quantity is
115 calculated. This is done by summing over all open purchase orders.
117 Returns a hash with the part IDs being the keys and the ordered
118 quantities being the values.
120 =item C<type_filter @types>
122 Constructs a partial filter for matching any of the article types
123 given with C<@types>. The returned partial filter is suitable for a
126 Each type can be either 'C<part>', 'C<service>' or 'C<assembly>'
127 (their plurals are recognized as well). If multiple types are given
128 then they're combined with C<OR>.
138 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
139 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>