e2383462582e9040ec83c64b99702051515e04a0
[kivitendo-erp.git] / SL / DB / Manager / Part.pm
1 package SL::DB::Manager::Part;
2
3 use strict;
4
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);
10
11 use Carp;
12 use SL::DBUtils;
13 use SL::MoreCommon qw(listify);
14
15 sub object_class { 'SL::DB::Part' }
16
17 __PACKAGE__->make_manager_methods;
18 __PACKAGE__->add_filter_specs(
19   type => sub {
20     my ($key, $value, $prefix) = @_;
21     return __PACKAGE__->type_filter($value, $prefix);
22   },
23   all => sub {
24     my ($key, $value, $prefix) = @_;
25     return or => [ map { $prefix . $_ => $value } qw(partnumber description) ]
26   }
27 );
28
29 sub type_filter {
30   my ($class, $type, $prefix) = @_;
31
32   return () unless $type;
33
34   $prefix //= '';
35
36   # this is to make selection like type => { part => 1, service => 1 } work
37   if ('HASH' eq ref $type) {
38     $type = grep { $type->{$_} } keys %$type;
39   }
40
41   my @types = listify($type);
42   my @filter;
43
44   for my $type (@types) {
45     if ($type =~ m/^part/) {
46       push @filter, (and => [ or                    => [ $prefix . assembly => 0, $prefix . assembly => undef ],
47                        "!${prefix}inventory_accno_id" => 0,
48                        "!${prefix}inventory_accno_id" => undef,
49                      ]);
50     } elsif ($type =~ m/^service/) {
51       push @filter, (and => [ or => [ $prefix . assembly           => 0, $prefix . assembly           => undef ],
52                        or => [ $prefix . inventory_accno_id => 0, $prefix . inventory_accno_id => undef ],
53                      ]);
54     } elsif ($type =~ m/^assembl/) {
55       push @filter, ($prefix . assembly => 1);
56     }
57   }
58
59   return @filter > 2 ? (or => \@filter) :
60          @filter     ? @filter          : ();
61 }
62
63 sub get_ordered_qty {
64   my $class    = shift;
65   my @part_ids = @_;
66
67   return () unless @part_ids;
68
69   my $placeholders = join ',', ('?') x @part_ids;
70   my $query        = <<SQL;
71     SELECT oi.parts_id, SUM(oi.base_qty) AS qty
72     FROM orderitems oi
73     LEFT JOIN oe ON (oi.trans_id = oe.id)
74     WHERE (oi.parts_id IN ($placeholders))
75       AND (NOT COALESCE(oe.quotation, FALSE))
76       AND (NOT COALESCE(oe.closed,    FALSE))
77       AND (NOT COALESCE(oe.delivered, FALSE))
78       AND (COALESCE(oe.vendor_id, 0) <> 0)
79     GROUP BY oi.parts_id
80 SQL
81
82   my %qty_by_id = map { $_->{parts_id} => $_->{qty} * 1 } @{ selectall_hashref_query($::form, $class->object_class->init_db->dbh, $query, @part_ids) };
83   map { $qty_by_id{$_} ||= 0 } @part_ids;
84
85   return %qty_by_id;
86 }
87
88 1;
89 __END__
90
91 =pod
92
93 =encoding utf8
94
95 =head1 NAME
96
97 SL::DB::Manager::Part - RDBO manager for the C<parts> table
98
99 =head1 FUNCTIONS
100
101 =over 4
102
103 =item C<get_ordered_qty @part_ids>
104
105 For each of the given part IDs the ordered quantity is
106 calculated. This is done by summing over all open purchase orders.
107
108 Returns a hash with the part IDs being the keys and the ordered
109 quantities being the values.
110
111 =item C<type_filter @types>
112
113 Constructs a partial filter for matching any of the article types
114 given with C<@types>. The returned partial filter is suitable for a
115 Rose manager query.
116
117 Each type can be either 'C<part>', 'C<service>' or 'C<assembly>'
118 (their plurals are recognized as well). If multiple types are given
119 then they're combined with C<OR>.
120
121 =back
122
123 =head1 BUGS
124
125 Nothing here yet.
126
127 =head1 AUTHOR
128
129 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
130 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
131
132 =cut