kivitendo/SL/DB/Manager/Part.pm @ df33875f
82515b2d | Sven Schöling | package SL::DB::Manager::Part;
|
||
use strict;
|
||||
ac552280 | Moritz Bunkus | use SL::DB::Helper::Manager;
|
||
9d391728 | Sven Schöling | use SL::DB::Helper::Sorted;
|
||
use SL::DB::Helper::Paginated;
|
||||
use SL::DB::Helper::Filtered;
|
||||
ac552280 | Moritz Bunkus | use base qw(SL::DB::Helper::Manager);
|
||
82515b2d | Sven Schöling | |||
use Carp;
|
||||
use SL::DBUtils;
|
||||
60009b97 | Sven Schöling | use SL::MoreCommon qw(listify);
|
||
82515b2d | Sven Schöling | |||
sub object_class { 'SL::DB::Part' }
|
||||
__PACKAGE__->make_manager_methods;
|
||||
9d391728 | Sven Schöling | __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) ]
|
||||
}
|
||||
);
|
||||
82515b2d | Sven Schöling | |||
sub type_filter {
|
||||
9d391728 | Sven Schöling | my ($class, $type, $prefix) = @_;
|
||
60009b97 | Sven Schöling | |||
return () unless $type;
|
||||
9d391728 | Sven Schöling | $prefix //= '';
|
||
# this is to make selection like type => { part => 1, service => 1 } work
|
||||
if ('HASH' eq ref $type) {
|
||||
84ad2fe8 | Moritz Bunkus | $type = [ grep { $type->{$_} } keys %$type ];
|
||
9d391728 | Sven Schöling | }
|
||
80d5f170 | Moritz Bunkus | my @types = grep { $_ } listify($type);
|
||
60009b97 | Sven Schöling | my @filter;
|
||
for my $type (@types) {
|
||||
if ($type =~ m/^part/) {
|
||||
9d391728 | Sven Schöling | push @filter, (and => [ or => [ $prefix . assembly => 0, $prefix . assembly => undef ],
|
||
"!${prefix}inventory_accno_id" => 0,
|
||||
"!${prefix}inventory_accno_id" => undef,
|
||||
60009b97 | Sven Schöling | ]);
|
||
} elsif ($type =~ m/^service/) {
|
||||
9d391728 | Sven Schöling | push @filter, (and => [ or => [ $prefix . assembly => 0, $prefix . assembly => undef ],
|
||
or => [ $prefix . inventory_accno_id => 0, $prefix . inventory_accno_id => undef ],
|
||||
60009b97 | Sven Schöling | ]);
|
||
} elsif ($type =~ m/^assembl/) {
|
||||
9d391728 | Sven Schöling | push @filter, ($prefix . assembly => 1);
|
||
60009b97 | Sven Schöling | }
|
||
82515b2d | Sven Schöling | }
|
||
9d391728 | Sven Schöling | return @filter > 2 ? (or => \@filter) :
|
||
@filter ? @filter : ();
|
||||
82515b2d | Sven Schöling | }
|
||
sub get_ordered_qty {
|
||||
my $class = shift;
|
||||
my @part_ids = @_;
|
||||
return () unless @part_ids;
|
||||
my $placeholders = join ',', ('?') x @part_ids;
|
||||
my $query = <<SQL;
|
||||
SELECT oi.parts_id, SUM(oi.base_qty) AS qty
|
||||
FROM orderitems oi
|
||||
LEFT JOIN oe ON (oi.trans_id = oe.id)
|
||||
WHERE (oi.parts_id IN ($placeholders))
|
||||
AND (NOT COALESCE(oe.quotation, FALSE))
|
||||
AND (NOT COALESCE(oe.closed, FALSE))
|
||||
AND (NOT COALESCE(oe.delivered, FALSE))
|
||||
AND (COALESCE(oe.vendor_id, 0) <> 0)
|
||||
GROUP BY oi.parts_id
|
||||
SQL
|
||||
my %qty_by_id = map { $_->{parts_id} => $_->{qty} * 1 } @{ selectall_hashref_query($::form, $class->object_class->init_db->dbh, $query, @part_ids) };
|
||||
map { $qty_by_id{$_} ||= 0 } @part_ids;
|
||||
return %qty_by_id;
|
||||
}
|
||||
2504ebe1 | Sven Schöling | sub _sort_spec {
|
||
(
|
||||
default => [ 'partnumber', 1 ],
|
||||
columns => {
|
||||
SIMPLE => 'ALL',
|
||||
},
|
||||
nulls => {},
|
||||
);
|
||||
}
|
||||
82515b2d | Sven Schöling | 1;
|
||
6858b66b | Moritz Bunkus | __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
|