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