0b751bd34b58bd6af513252f8dfc9cb28ab5f20f
[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 = grep { $_ } 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) : @filter;
60 }
61
62 sub get_ordered_qty {
63   my $class    = shift;
64   my @part_ids = @_;
65
66   return () unless @part_ids;
67
68   my $placeholders = join ',', ('?') x @part_ids;
69   my $query        = <<SQL;
70     SELECT oi.parts_id, SUM(oi.base_qty) AS qty
71     FROM orderitems oi
72     LEFT JOIN oe ON (oi.trans_id = oe.id)
73     WHERE (oi.parts_id IN ($placeholders))
74       AND (NOT COALESCE(oe.quotation, FALSE))
75       AND (NOT COALESCE(oe.closed,    FALSE))
76       AND (NOT COALESCE(oe.delivered, FALSE))
77       AND (COALESCE(oe.vendor_id, 0) <> 0)
78     GROUP BY oi.parts_id
79 SQL
80
81   my %qty_by_id = map { $_->{parts_id} => $_->{qty} * 1 } @{ selectall_hashref_query($::form, $class->object_class->init_db->dbh, $query, @part_ids) };
82   map { $qty_by_id{$_} ||= 0 } @part_ids;
83
84   return %qty_by_id;
85 }
86
87 sub _sort_spec {
88   (
89     default  => [ 'partnumber', 1 ],
90     columns  => {
91       SIMPLE => 'ALL',
92     },
93     nulls    => {},
94   );
95 }
96
97 1;
98 __END__
99
100 =pod
101
102 =encoding utf8
103
104 =head1 NAME
105
106 SL::DB::Manager::Part - RDBO manager for the C<parts> table
107
108 =head1 FUNCTIONS
109
110 =over 4
111
112 =item C<get_ordered_qty @part_ids>
113
114 For each of the given part IDs the ordered quantity is
115 calculated. This is done by summing over all open purchase orders.
116
117 Returns a hash with the part IDs being the keys and the ordered
118 quantities being the values.
119
120 =item C<type_filter @types>
121
122 Constructs a partial filter for matching any of the article types
123 given with C<@types>. The returned partial filter is suitable for a
124 Rose manager query.
125
126 Each type can be either 'C<part>', 'C<service>' or 'C<assembly>'
127 (their plurals are recognized as well). If multiple types are given
128 then they're combined with C<OR>.
129
130 =back
131
132 =head1 BUGS
133
134 Nothing here yet.
135
136 =head1 AUTHOR
137
138 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
139 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
140
141 =cut