epic-ts
[kivitendo-erp.git] / SL / DB / Manager / Part.pm
index 1564b9b..0b751bd 100644 (file)
@@ -2,37 +2,61 @@ package SL::DB::Manager::Part;
 
 use strict;
 
-use SL::DB::Helpers::Manager;
-use base qw(SL::DB::Helpers::Manager);
+use SL::DB::Helper::Manager;
+use SL::DB::Helper::Sorted;
+use SL::DB::Helper::Paginated;
+use SL::DB::Helper::Filtered;
+use base qw(SL::DB::Helper::Manager);
 
 use Carp;
 use SL::DBUtils;
+use SL::MoreCommon qw(listify);
 
 sub object_class { 'SL::DB::Part' }
 
 __PACKAGE__->make_manager_methods;
+__PACKAGE__->add_filter_specs(
+  type => sub {
+    my ($key, $value, $prefix) = @_;
+    return __PACKAGE__->type_filter($value, $prefix);
+  },
+  all => sub {
+    my ($key, $value, $prefix) = @_;
+    return or => [ map { $prefix . $_ => $value } qw(partnumber description) ]
+  }
+);
 
 sub type_filter {
-  my $class = shift;
-  my $type  = lc(shift || '');
+  my ($class, $type, $prefix) = @_;
 
-  if ($type =~ m/^part/) {
-    return (and => [ or                    => [ assembly => 0, assembly => undef ],
-                     '!inventory_accno_id' => 0,
-                     '!inventory_accno_id' => undef,
-                   ]);
+  return () unless $type;
 
-  } elsif ($type =~ m/^service/) {
-    return (and => [ or => [ assembly           => 0, assembly           => undef ],
-                     or => [ inventory_accno_id => 0, inventory_accno_id => undef ],
-                   ]);
+  $prefix //= '';
 
-  } elsif ($type =~ m/^assembl/) {
-    return (assembly => 1);
+  # this is to make selection like type => { part => 1, service => 1 } work
+  if ('HASH' eq ref $type) {
+    $type = [ grep { $type->{$_} } keys %$type ];
+  }
 
+  my @types = grep { $_ } listify($type);
+  my @filter;
+
+  for my $type (@types) {
+    if ($type =~ m/^part/) {
+      push @filter, (and => [ or                             => [ $prefix . assembly => 0, $prefix . assembly => undef ],
+                              "!${prefix}inventory_accno_id" => 0,
+                              "!${prefix}inventory_accno_id" => undef,
+                     ]);
+    } elsif ($type =~ m/^service/) {
+      push @filter, (and => [ or => [ $prefix . assembly           => 0, $prefix . assembly           => undef ],
+                              or => [ $prefix . inventory_accno_id => 0, $prefix . inventory_accno_id => undef ],
+                     ]);
+    } elsif ($type =~ m/^assembl/) {
+      push @filter, ($prefix . assembly => 1);
+    }
   }
 
-  return ();
+  return @filter > 2 ? (or => \@filter) : @filter;
 }
 
 sub get_ordered_qty {
@@ -60,4 +84,58 @@ SQL
   return %qty_by_id;
 }
 
+sub _sort_spec {
+  (
+    default  => [ 'partnumber', 1 ],
+    columns  => {
+      SIMPLE => 'ALL',
+    },
+    nulls    => {},
+  );
+}
+
 1;
+__END__
+
+=pod
+
+=encoding utf8
+
+=head1 NAME
+
+SL::DB::Manager::Part - RDBO manager for the C<parts> table
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item C<get_ordered_qty @part_ids>
+
+For each of the given part IDs the ordered quantity is
+calculated. This is done by summing over all open purchase orders.
+
+Returns a hash with the part IDs being the keys and the ordered
+quantities being the values.
+
+=item C<type_filter @types>
+
+Constructs a partial filter for matching any of the article types
+given with C<@types>. The returned partial filter is suitable for a
+Rose manager query.
+
+Each type can be either 'C<part>', 'C<service>' or 'C<assembly>'
+(their plurals are recognized as well). If multiple types are given
+then they're combined with C<OR>.
+
+=back
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
+Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
+
+=cut