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) ]
 
  30   my ($class, $type, $prefix) = @_;
 
  32   return () unless $type;
 
  36   # this is to make selections like part_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, ($prefix . part_type => 'part');
 
  47     } elsif ($type =~ m/^service/) {
 
  48       push @filter, ($prefix . part_type => 'service');
 
  49     } elsif ($type =~ m/^assembly/) {
 
  50       push @filter, ($prefix . part_type => 'assembly');
 
  51     } elsif ($type =~ m/^assortment/) {
 
  52       push @filter, ($prefix . part_type => 'assortment');
 
  56   return @filter > 2 ? (or => \@filter) : @filter;
 
  63   return () unless @part_ids;
 
  65   my $placeholders = join ',', ('?') x @part_ids;
 
  67     SELECT oi.parts_id, SUM(oi.base_qty) AS qty
 
  69     LEFT JOIN oe ON (oi.trans_id = oe.id)
 
  70     WHERE (oi.parts_id IN ($placeholders))
 
  71       AND (NOT COALESCE(oe.quotation, FALSE))
 
  72       AND (NOT COALESCE(oe.closed,    FALSE))
 
  73       AND (NOT COALESCE(oe.delivered, FALSE))
 
  74       AND (COALESCE(oe.vendor_id, 0) <> 0)
 
  78   my %qty_by_id = map { $_->{parts_id} => $_->{qty} * 1 } @{ selectall_hashref_query($::form, $class->object_class->init_db->dbh, $query, @part_ids) };
 
  79   map { $qty_by_id{$_} ||= 0 } @part_ids;
 
  86     default  => [ 'partnumber', 1 ],
 
 103 SL::DB::Manager::Part - RDBO manager for the C<parts> table
 
 109 =item C<get_ordered_qty @part_ids>
 
 111 For each of the given part IDs the ordered quantity is
 
 112 calculated. This is done by summing over all open purchase orders.
 
 114 Returns a hash with the part IDs being the keys and the ordered
 
 115 quantities being the values.
 
 117 =item C<type_filter @types>
 
 119 Constructs a partial filter for matching any of the article types
 
 120 given with C<@types>. The returned partial filter is suitable for a
 
 123 Each type can be either 'C<part>', 'C<service>' or 'C<assembly>'
 
 124 (their plurals are recognized as well). If multiple types are given
 
 125 then they're combined with C<OR>.
 
 135 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
 
 136 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>