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 ean) ]
27 all_with_makemodel => sub {
28 my ($key, $value, $prefix) = @_;
29 return or => [ map { $prefix . $_ => $value } qw(partnumber description ean makemodels.model) ],
30 $prefix . 'makemodels';
32 all_with_customer_partnumber => sub {
33 my ($key, $value, $prefix) = @_;
34 return or => [ map { $prefix . $_ => $value } qw(partnumber description ean customerprices.customer_partnumber) ],
35 $prefix . 'customerprices';
40 my ($class, $type, $prefix) = @_;
42 return () unless $type;
46 # this is to make selections like part_type => { part => 1, service => 1 } work
47 if ('HASH' eq ref $type) {
48 $type = [ grep { $type->{$_} } keys %$type ];
51 my @types = grep { $_ } listify($type);
54 for my $type (@types) {
55 if ($type =~ m/^part/) {
56 push @filter, ($prefix . part_type => 'part');
57 } elsif ($type =~ m/^service/) {
58 push @filter, ($prefix . part_type => 'service');
59 } elsif ($type =~ m/^assembly/) {
60 push @filter, ($prefix . part_type => 'assembly');
61 } elsif ($type =~ m/^assortment/) {
62 push @filter, ($prefix . part_type => 'assortment');
66 return @filter > 2 ? (or => \@filter) : @filter;
73 return () unless @part_ids;
75 my $placeholders = join ',', ('?') x @part_ids;
77 SELECT oi.parts_id, SUM(oi.base_qty) AS qty
79 LEFT JOIN oe ON (oi.trans_id = oe.id)
80 WHERE (oi.parts_id IN ($placeholders))
81 AND (NOT COALESCE(oe.quotation, FALSE))
82 AND (NOT COALESCE(oe.closed, FALSE))
83 AND (NOT COALESCE(oe.delivered, FALSE))
84 AND (COALESCE(oe.vendor_id, 0) <> 0)
88 my %qty_by_id = map { $_->{parts_id} => $_->{qty} * 1 } @{ selectall_hashref_query($::form, $class->object_class->init_db->dbh, $query, @part_ids) };
89 map { $qty_by_id{$_} ||= 0 } @part_ids;
96 default => [ 'partnumber', 1 ],
113 SL::DB::Manager::Part - RDBO manager for the C<parts> table
119 =item C<get_ordered_qty @part_ids>
121 For each of the given part IDs the ordered quantity is
122 calculated. This is done by summing over all open purchase orders.
124 Returns a hash with the part IDs being the keys and the ordered
125 quantities being the values.
127 =item C<type_filter @types>
129 Constructs a partial filter for matching any of the article types
130 given with C<@types>. The returned partial filter is suitable for a
133 Each type can be either 'C<part>', 'C<service>' or 'C<assembly>'
134 (their plurals are recognized as well). If multiple types are given
135 then they're combined with C<OR>.
145 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
146 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>