From 9d391728884d035ab7bd1dfbcc3b38e3ecb90de2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 27 May 2013 19:52:42 +0200 Subject: [PATCH] =?utf8?q?Custom=20Filter=20'type'=20und=20'all'=20f=C3=BC?= =?utf8?q?r=20Parts?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DB/Manager/Part.pm | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/SL/DB/Manager/Part.pm b/SL/DB/Manager/Part.pm index 6c781a9fe..e23834625 100644 --- a/SL/DB/Manager/Part.pm +++ b/SL/DB/Manager/Part.pm @@ -3,6 +3,9 @@ package SL::DB::Manager::Part; use strict; 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; @@ -12,31 +15,49 @@ 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, $type) = @_; + my ($class, $type, $prefix) = @_; return () unless $type; + $prefix //= ''; + + # this is to make selection like type => { part => 1, service => 1 } work + if ('HASH' eq ref $type) { + $type = grep { $type->{$_} } keys %$type; + } + my @types = listify($type); my @filter; for my $type (@types) { if ($type =~ m/^part/) { - push @filter, (and => [ or => [ assembly => 0, assembly => undef ], - '!inventory_accno_id' => 0, - '!inventory_accno_id' => undef, + 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 => [ assembly => 0, assembly => undef ], - or => [ inventory_accno_id => 0, inventory_accno_id => undef ], + 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, (assembly => 1); + push @filter, ($prefix . assembly => 1); } } - return @filter ? (or => \@filter) : (); + return @filter > 2 ? (or => \@filter) : + @filter ? @filter : (); } sub get_ordered_qty { -- 2.20.1