1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1999-2003
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
33 #======================================================================
43 $main::lxdebug->enter_sub();
48 $main::lxdebug->leave_sub();
52 my $myconfig = \%main::myconfig;
53 my $form = $main::form;
55 my $dbh = $form->get_standard_dbh($myconfig);
57 my $units = AM->retrieve_units($myconfig, $form);
59 my $query = qq|SELECT * FROM transfer_type|;
60 my $sth = prepare_execute_query($form, $dbh, $query);
64 while (my $ref = $sth->fetchrow_hashref()) {
65 $transfer_types{$ref->{direction}} ||= { };
66 $transfer_types{$ref->{direction}}->{$ref->{description}} = $ref->{id};
69 my @part_ids = map { $_->{parts_id} } @_;
70 my %partunits = selectall_as_map($form, $dbh, qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids ) . qq|)|, 'id', 'unit', @part_ids);
72 my ($now) = selectrow_query($form, $dbh, qq|SELECT current_date|);
74 $query = qq|INSERT INTO inventory (warehouse_id, bin_id, parts_id, chargenumber, oe_id, orderitems_id, shippingdate,
75 employee_id, project_id, trans_id, trans_type_id, comment, qty)
76 VALUES (?, ?, ?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?, ?, ?)|;
78 $sth = prepare_query($form, $dbh, $query);
80 my @directions = (undef, 'out', 'in', 'transfer');
84 my ($trans_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
86 my ($direction, @values) = (0);
88 $direction |= 1 if ($transfer->{src_warehouse_id} && $transfer->{src_bin_id});
89 $direction |= 2 if ($transfer->{dst_warehouse_id} && $transfer->{dst_bin_id});
91 push @values, conv_i($transfer->{parts_id}), "$transfer->{chargenumber}", conv_i($transfer->{oe_id}), conv_i($transfer->{orderitems_id});
92 push @values, $transfer->{shippingdate} eq 'current_date' ? $now : conv_date($transfer->{shippingdate}), $form->{login}, conv_i($transfer->{project_id}), $trans_id;
94 if ($transfer->{transfer_type_id}) {
95 push @values, $transfer->{transfer_type_id};
97 push @values, $transfer_types{$directions[$direction]}->{$transfer->{transfer_type}};
100 push @values, "$transfer->{comment}";
102 my $qty = $transfer->{qty};
104 if ($transfer->{unit}) {
105 my $partunit = $partunits{$transfer->{parts_id}};
107 $qty *= $units->{$transfer->{unit}}->{factor};
108 $qty /= $units->{$partunit}->{factor} || 1 if ($partunit);
111 if ($direction & 1) {
112 do_statement($form, $sth, $query, conv_i($transfer->{src_warehouse_id}), conv_i($transfer->{src_bin_id}), @values, $qty * -1);
115 if ($direction & 2) {
116 do_statement($form, $sth, $query, conv_i($transfer->{dst_warehouse_id}), conv_i($transfer->{dst_bin_id}), @values, $qty);
124 $main::lxdebug->leave_sub();
127 sub transfer_assembly {
128 $main::lxdebug->enter_sub();
132 Common::check_params(\%params, qw(assembly_id dst_warehouse_id login qty unit dst_bin_id chargenumber comment));
134 # my $maxcreate=WH->check_assembly_max_create(assembly_id =>$params{'assembly_id'}, dbh => $my_dbh);
136 my $myconfig = \%main::myconfig;
137 my $form = $main::form;
138 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
143 # ... Standard-Check oben Ende. Hier die eigentliche SQL-Abfrage
144 # select parts_id,qty from assembly where id=1064;
145 # Erweiterung für bug 935 am 23.4.09 - Erzeugnisse können Dienstleistungen enthalten, die ja nicht 'lagerbar' sind.
146 # select parts_id,qty from assembly inner join parts on assembly.parts_id = parts.id where assembly.id=1066 and inventory_accno_id IS NOT NULL;
150 # my $query = qq|select parts_id,qty from assembly where id = ?|;
151 my $query = qq|select parts_id,qty from assembly inner join parts on assembly.parts_id = parts.id where assembly.id = ? and inventory_accno_id IS NOT NULL|;
153 my $sth_part_qty_assembly = prepare_execute_query($form, $dbh, $query, $params{assembly_id});
155 # Hier wird das prepared Statement für die Schleife über alle Lagerplätze vorbereitet
156 my $transferPartSQL = qq|INSERT INTO inventory (parts_id, warehouse_id, bin_id, chargenumber, comment, employee_id, qty, trans_id, trans_type_id)
157 VALUES (?, ?, ?, ?, ?,(SELECT id FROM employee WHERE login = ?), ?, nextval('id'),
158 (SELECT id FROM transfer_type WHERE direction = 'out' AND description = 'used'))|;
159 my $sthTransferPartSQL = prepare_query($form, $dbh, $transferPartSQL);
161 my $kannNichtFertigen =""; # der return-string für die fehlermeldung inkl. welche waren zum fertigen noch fehlen
163 while (my $hash_ref = $sth_part_qty_assembly->fetchrow_hashref()) { # Schleife für $query=select parts_id,qty from assembly
165 my $partsQTY = $hash_ref->{qty} * $params{qty}; # benötigte teile * anzahl erzeugnisse
166 my $currentPart_ID = $hash_ref->{parts_id};
168 # Überprüfen, ob diese Anzahl gefertigt werden kann
169 my $max_parts = get_max_qty_parts($self, parts_id => $currentPart_ID, warehouse_id => $params{dst_warehouse_id}); #$self angeben, damit die Standardkonvention (Name, Parameter) eingehalten wird
171 if ($partsQTY > $max_parts){
173 # Gibt es hier ein Problem mit nicht "escapten" Zeichen?
174 $kannNichtFertigen .= "Zum Fertigen fehlen:" . abs($partsQTY) . " Stueck der Ware:" . get_part_description($self, parts_id => $currentPart_ID) . ", um das Erzeugnis herzustellen. <br>"; # Konnte die Menge nicht mit der aktuellen Anzahl der Waren fertigen
177 # Eine kurze Vorabfrage, um den Lagerplatz und die Chargennummber zu bestimmen
178 # Offen: Die Summe über alle Lagerplätze wird noch nicht gebildet
179 # Gelöst: Wir haben vorher schon die Abfrage durchgeführt, ob wir fertigen können.
180 # Noch besser gelöst: Wir laufen durch alle benötigten Waren zum Fertigen und geben eine Rückmeldung an den Benutzer was noch fehlt
181 # und lösen den Rest dann so wie bei xplace im Barcode-Programm
182 # S.a. Kommentar im bin/mozilla-Code mb übernimmt und macht das in ordentlich
184 my $tempquery = qq|SELECT SUM(qty), bin_id, chargenumber FROM inventory WHERE warehouse_id = ? AND parts_id = ? GROUP BY bin_id, chargenumber having SUM(qty)>0|;
185 my $tempsth = prepare_execute_query($form, $dbh, $tempquery, $params{dst_warehouse_id}, $currentPart_ID);
187 # Alle Werte zu dem einzelnen Artikel, die wir später auslagern
188 my $tmpPartsQTY = $partsQTY;
190 while (my $temphash_ref = $tempsth->fetchrow_hashref()) {
191 my $temppart_bin_id = $temphash_ref->{bin_id}; # kann man hier den quelllagerplatz beim verbauen angeben?
192 my $temppart_chargenumber = $temphash_ref->{chargenumber};
193 my $temppart_qty = $temphash_ref->{sum};
194 if ($tmpPartsQTY > $temppart_qty) { # wir haben noch mehr waren zum wegbuchen. Wir buchen den kompletten Lagerplatzbestand und zählen die Hilfsvariable runter
195 $tmpPartsQTY = $tmpPartsQTY - $temppart_qty;
196 $temppart_qty = $temppart_qty * -1; # beim analyiseren des sql-trace, war dieser wert positiv, wenn * -1 als berechnung in der parameter-übergabe angegeben wird. Dieser Wert IST und BLEIBT positiv!! Hilfe. Liegt das daran, dass dieser Wert aus einem SQL-Statement stammt?
197 do_statement($form, $sthTransferPartSQL, $transferPartSQL, $currentPart_ID, $params{dst_warehouse_id}, $temppart_bin_id, $temppart_chargenumber, 'Verbraucht für ' . get_part_description($self, parts_id => $params{assembly_id}), $params{login}, $temppart_qty);
199 # hier ist noch ein fehler am besten mit definierten erzeugnissen debuggen 02/2009 jb
200 # idee: ausbuch algorithmus mit rekursion lösen und an- und abschaltbar machen
201 # das problem könnte sein, dass strict nicht an war und sth global eine andere zuweisung bekam
202 # auf jeden fall war der internal-server-error nach aktivierung von strict und warnings plus ein paar my-definitionen weg
203 } else { # okay, wir haben weniger oder gleich Waren die wir wegbuchen müssen, wir können also aufhören
205 do_statement($form, $sthTransferPartSQL, $transferPartSQL, $currentPart_ID, $params{dst_warehouse_id}, $temppart_bin_id, $temppart_chargenumber, 'Verbraucht für ' . get_part_description($self, parts_id => $params{assembly_id}), $params{login}, $tmpPartsQTY);
206 last; # beendet die schleife (springt zum letzten element)
208 } # ende while SELECT SUM(qty), bin_id, chargenumber FROM inventory WHERE warehouse_id
209 } #ende while select parts_id,qty from assembly where id = ?
210 if ($kannNichtFertigen) {
211 return $kannNichtFertigen;
214 # soweit alles gut. Jetzt noch die wirkliche Lagerbewegung für das Erzeugnis ausführen ...
215 my $transferAssemblySQL = qq|INSERT INTO inventory (parts_id, warehouse_id, bin_id, chargenumber, comment, employee_id, qty, trans_id, trans_type_id)
216 VALUES (?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, nextval('id'),
217 (SELECT id FROM transfer_type WHERE direction = 'in' AND description = 'stock'))|;
218 my $sthTransferAssemblySQL = prepare_query($form, $dbh, $transferAssemblySQL);
219 do_statement($form, $sthTransferAssemblySQL, $transferAssemblySQL, $params{assembly_id}, $params{dst_warehouse_id}, $params{dst_bin_id}, $params{chargenumber}, $params{comment}, $params{login}, $params{qty});
222 $main::lxdebug->leave_sub();
223 return 1; # Alles erfolgreich
226 sub get_warehouse_journal {
227 $main::lxdebug->enter_sub();
232 my $myconfig = \%main::myconfig;
233 my $form = $main::form;
235 my $all_units = AM->retrieve_units($myconfig, $form);
237 # connect to database
238 my $dbh = $form->get_standard_dbh($myconfig);
241 my (@filter_ary, @filter_vars, $joins);
243 if ($filter{warehouse_id} ne '') {
244 push @filter_ary, "w1.id = ? OR w2.id = ?";
245 push @filter_vars, $filter{warehouse_id}, $filter{warehouse_id};
248 if ($filter{bin_id} ne '') {
249 push @filter_ary, "b1.id = ? OR b2.id = ?";
250 push @filter_vars, $filter{bin_id}, $filter{bin_id};
253 if ($filter{partnumber}) {
254 push @filter_ary, "p.partnumber ILIKE ?";
255 push @filter_vars, '%' . $filter{partnumber} . '%';
258 if ($filter{description}) {
259 push @filter_ary, "(p.description ILIKE ?)";
260 push @filter_vars, '%' . $filter{description} . '%';
263 if ($filter{chargenumber}) {
264 push @filter_ary, "i1.chargenumber ILIKE ?";
265 push @filter_vars, '%' . $filter{chargenumber} . '%';
268 if ($form->{fromdate}) {
269 push @filter_ary, "?::DATE <= i1.itime::DATE";
270 push @filter_vars, $form->{fromdate};
273 if ($form->{todate}) {
274 push @filter_ary, "?::DATE >= i1.itime::DATE";
275 push @filter_vars, $form->{todate};
278 if ($form->{l_employee}) {
282 # prepare qty comparison for later filtering
283 my ($f_qty_op, $f_qty, $f_qty_base_unit);
284 if ($filter{qty_op} && defined($filter{qty}) && $filter{qty_unit} && $all_units->{$filter{qty_unit}}) {
285 $f_qty_op = $filter{qty_op};
286 $f_qty = $filter{qty} * $all_units->{$filter{qty_unit}}->{factor};
287 $f_qty_base_unit = $all_units->{$filter{qty_unit}}->{base_unit};
290 map { $_ = "(${_})"; } @filter_ary;
292 # if of a property number or description is requested,
293 # automatically check the matching id too.
294 map { $form->{"l_${_}id"} = "Y" if ($form->{"l_${_}description"} || $form->{"l_${_}number"}); } qw(warehouse bin);
296 # customize shown entry for not available fields.
297 $filter{na} = '-' unless $filter{na};
299 # make order, search in $filter and $form
300 $form->{sort} = $filter{sort} unless $form->{sort};
301 $form->{order} = ($form->{sort} = 'itime') unless $form->{sort};
302 $form->{sort} = 'itime' if $form->{sort} eq "date";
303 $form->{order} = $filter{order} unless $form->{order};
304 $form->{sort} .= (($form->{order}) ? " DESC" : " ASC");
306 my $where_clause = join(" AND ", @filter_ary) . " AND " if (@filter_ary);
308 $select_tokens{'trans'} = {
309 "parts_id" => "i1.parts_id",
310 "qty" => "ABS(SUM(i1.qty))",
311 "partnumber" => "p.partnumber",
312 "partdescription" => "p.description",
313 "bindescription" => "b.description",
314 "chargenumber" => "i1.chargenumber",
315 "warehousedescription" => "w.description",
316 "partunit" => "p.unit",
317 "bin_from" => "b1.description",
318 "bin_to" => "b2.description",
319 "warehouse_from" => "w1.description",
320 "warehouse_to" => "w2.description",
321 "comment" => "i1.comment",
322 "trans_type" => "tt.description",
323 "trans_id" => "i1.trans_id",
324 "date" => "i1.itime::DATE",
325 "itime" => "i1.itime",
326 "employee" => "e.name",
327 "projectnumber" => "COALESCE(pr.projectnumber, '$filter{na}')",
330 $select_tokens{'out'} = {
331 "bin_to" => "'$filter{na}'",
332 "warehouse_to" => "'$filter{na}'",
335 $select_tokens{'in'} = {
336 "bin_from" => "'$filter{na}'",
337 "warehouse_from" => "'$filter{na}'",
340 # build the select clauses.
341 # take all the requested ones from the first hash and overwrite them from the out/in hashes if present.
342 for my $i ('trans', 'out', 'in') {
343 $select{$i} = join ', ', map { +/^l_/; ($select_tokens{$i}{"$'"} || $select_tokens{'trans'}{"$'"}) . " AS r_$'" }
344 ( grep( { !/qty$/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form), qw(l_parts_id l_qty l_partunit l_itime) );
347 my $group_clause = join ", ", map { +/^l_/; "r_$'" }
348 ( grep( { !/qty$/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form), qw(l_parts_id l_partunit l_itime) );
351 qq|SELECT DISTINCT $select{trans}
353 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
354 LEFT JOIN parts p ON i1.parts_id = p.id
355 LEFT JOIN bin b1 ON i1.bin_id = b1.id
356 LEFT JOIN bin b2 ON i2.bin_id = b2.id
357 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
358 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
359 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
360 LEFT JOIN project pr ON i1.project_id = pr.id
361 LEFT JOIN employee e ON i1.employee_id = e.id
362 WHERE $where_clause i2.qty = -i1.qty AND i2.qty > 0 AND
363 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 2 )
364 GROUP BY $group_clause
368 SELECT DISTINCT $select{out}
370 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
371 LEFT JOIN parts p ON i1.parts_id = p.id
372 LEFT JOIN bin b1 ON i1.bin_id = b1.id
373 LEFT JOIN bin b2 ON i2.bin_id = b2.id
374 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
375 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
376 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
377 LEFT JOIN project pr ON i1.project_id = pr.id
378 LEFT JOIN employee e ON i1.employee_id = e.id
379 WHERE $where_clause i1.qty < 0 AND
380 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 1 )
381 GROUP BY $group_clause
385 SELECT DISTINCT $select{in}
387 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
388 LEFT JOIN parts p ON i1.parts_id = p.id
389 LEFT JOIN bin b1 ON i1.bin_id = b1.id
390 LEFT JOIN bin b2 ON i2.bin_id = b2.id
391 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
392 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
393 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
394 LEFT JOIN project pr ON i1.project_id = pr.id
395 LEFT JOIN employee e ON i1.employee_id = e.id
396 WHERE $where_clause i1.qty > 0 AND
397 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 1 )
398 GROUP BY $group_clause
399 ORDER BY r_$form->{sort}|;
401 my $sth = prepare_execute_query($form, $dbh, $query, @filter_vars, @filter_vars, @filter_vars);
404 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
405 map { /^r_/; $ref->{"$'"} = $ref->{$_} } keys %$ref;
406 my $qty = $ref->{"qty"} * 1;
408 next unless ($qty > 0);
411 my $part_unit = $all_units->{$ref->{"partunit"}};
412 next unless ($part_unit && ($part_unit->{"base_unit"} eq $f_qty_base_unit));
413 $qty *= $part_unit->{"factor"};
414 next if (('=' eq $f_qty_op) && ($qty != $f_qty));
415 next if (('>=' eq $f_qty_op) && ($qty < $f_qty));
416 next if (('<=' eq $f_qty_op) && ($qty > $f_qty));
419 push @contents, $ref;
424 $main::lxdebug->leave_sub();
430 # This sub is the primary function to retrieve information about items in warehouses.
431 # $filter is a hashref and supports the following keys:
432 # - warehouse_id - will return matches with this warehouse_id only
433 # - partnumber - will return only matches where the given string is a substring of the partnumber
434 # - partsid - will return matches with this parts_id only
435 # - description - will return only matches where the given string is a substring of the description
436 # - chargenumber - will return only matches where the given string is a substring of the chargenumber
437 # - charge_ids - must be an arrayref. will return contents with these ids only
438 # - expires_in - will only return matches that expire within the given number of days
439 # will also add a column named 'has_expired' containing if the match has already expired or not
440 # - hazardous - will return matches with the flag hazardous only
441 # - oil - will return matches with the flag oil only
442 # - qty, qty_op - quantity filter (more info to come)
443 # - sort, order_by - sorting (more to come)
444 # - reservation - will provide an extra column containing the amount reserved of this match
445 # note: reservation flag turns off warehouse_* or bin_* information. both together don't make sense, since reserved info is stored separately
447 sub get_warehouse_report {
448 $main::lxdebug->enter_sub();
453 my $myconfig = \%main::myconfig;
454 my $form = $main::form;
456 my $all_units = AM->retrieve_units($myconfig, $form);
458 # connect to database
459 my $dbh = $form->get_standard_dbh($myconfig);
462 my (@filter_ary, @filter_vars, @wh_bin_filter_ary, @wh_bin_filter_vars, $columns, $group_by);
464 delete $form->{include_empty_bins} unless ($form->{l_warehousedescription} || $form->{l_bindescription});
466 if ($filter{warehouse_id}) {
467 push @wh_bin_filter_ary, "w.id = ?";
468 push @wh_bin_filter_vars, $filter{warehouse_id};
471 if ($filter{bin_id}) {
472 push @wh_bin_filter_ary, "b.id = ?";
473 push @wh_bin_filter_vars, $filter{bin_id};
476 push @filter_ary, @wh_bin_filter_ary;
477 push @filter_vars, @wh_bin_filter_vars;
479 if ($filter{partnumber}) {
480 push @filter_ary, "p.partnumber ILIKE ?";
481 push @filter_vars, '%' . $filter{partnumber} . '%';
484 if ($filter{description}) {
485 push @filter_ary, "p.description ILIKE ?";
486 push @filter_vars, '%' . $filter{description} . '%';
489 if ($filter{partsid}) {
490 push @filter_ary, "p.id = ?";
491 push @filter_vars, $filter{partsid};
494 if ($filter{chargenumber}) {
495 push @filter_ary, "i.chargenumber ILIKE ?";
496 push @filter_vars, '%' . $filter{chargenumber} . '%';
499 # prepare qty comparison for later filtering
500 my ($f_qty_op, $f_qty, $f_qty_base_unit);
502 if ($filter{qty_op} && defined $filter{qty} && $filter{qty_unit} && $all_units->{$filter{qty_unit}}) {
503 $f_qty_op = $filter{qty_op};
504 $f_qty = $filter{qty} * $all_units->{$filter{qty_unit}}->{factor};
505 $f_qty_base_unit = $all_units->{$filter{qty_unit}}->{base_unit};
508 map { $_ = "(${_})"; } @filter_ary;
510 # if of a property number or description is requested,
511 # automatically check the matching id too.
512 map { $form->{"l_${_}id"} = "Y" if ($form->{"l_${_}description"} || $form->{"l_${_}number"}); } qw(warehouse bin);
514 # make order, search in $filter and $form
515 $form->{sort} = $filter{sort} unless $form->{sort};
516 $form->{sort} = "parts_id" unless $form->{sort};
517 $form->{order} = $filter{order} unless $form->{order};
518 $form->{sort} =~ s/ASC|DESC//; # kill stuff left in from previous queries
519 my $orderby = $form->{sort};
520 $form->{sort} .= (($form->{order}) ? " DESC" : " ASC");
522 my $where_clause = join " AND ", ("1=1", @filter_ary);
524 my %select_tokens = (
525 "parts_id" => "i.parts_id",
526 "qty" => "SUM(i.qty)",
527 "warehouseid" => "i.warehouse_id",
528 "partnumber" => "p.partnumber",
529 "partdescription" => "p.description",
530 "bindescription" => "b.description",
532 "chargenumber" => "i.chargenumber",
533 "chargeid" => "c.id",
534 "warehousedescription" => "w.description",
535 "partunit" => "p.unit",
536 "stock_value" => "p.lastcost / COALESCE(pfac.factor, 1)",
538 my $select_clause = join ', ', map { +/^l_/; "$select_tokens{$'} AS $'" }
539 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
540 qw(l_parts_id l_qty l_partunit) );
542 my $group_clause = join ", ", map { +/^l_/; "$'" }
543 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
544 qw(l_parts_id l_partunit) );
547 "stock_value" => "LEFT JOIN price_factors pfac ON (p.price_factor_id = pfac.id)",
550 my $joins = join ' ', grep { $_ } map { +/^l_/; $join_tokens{"$'"} }
551 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
552 qw(l_parts_id l_qty l_partunit) );
555 qq|SELECT $select_clause
558 LEFT JOIN parts p ON i.parts_id = p.id
559 LEFT JOIN bin b ON i.bin_id = b.id
560 LEFT JOIN warehouse w ON i.warehouse_id = w.id
563 GROUP BY $group_clause $group_by
564 ORDER BY $form->{sort}|;
566 my $sth = prepare_execute_query($form, $dbh, $query, @filter_vars);
568 my (%non_empty_bins, @all_fields, @contents);
570 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
572 my $qty = $ref->{qty};
574 next unless ($qty != 0);
577 my $part_unit = $all_units->{$ref->{partunit}};
578 next if (!$part_unit || ($part_unit->{base_unit} ne $f_qty_base_unit));
579 $qty *= $part_unit->{factor};
580 next if (('=' eq $f_qty_op) && ($qty != $f_qty));
581 next if (('>=' eq $f_qty_op) && ($qty < $f_qty));
582 next if (('<=' eq $f_qty_op) && ($qty > $f_qty));
585 if ($form->{include_empty_bins}) {
586 $non_empty_bins{$ref->{binid}} = 1;
587 @all_fields = keys %{ $ref } unless (@all_fields);
590 $ref->{stock_value} *= $ref->{qty};
592 push @contents, $ref;
597 if ($form->{include_empty_bins}) {
600 w.id AS warehouseid, w.description AS warehousedescription,
601 b.id AS binid, b.description AS bindescription
603 LEFT JOIN warehouse w ON (b.warehouse_id = w.id)|;
605 @filter_ary = @wh_bin_filter_ary;
606 @filter_vars = @wh_bin_filter_vars;
608 my @non_empty_bin_ids = keys %non_empty_bins;
609 if (@non_empty_bin_ids) {
610 push @filter_ary, qq|NOT b.id IN (| . join(', ', map { '?' } @non_empty_bin_ids) . qq|)|;
611 push @filter_vars, @non_empty_bin_ids;
614 $query .= qq| WHERE | . join(' AND ', map { "($_)" } @filter_ary) if (@filter_ary);
616 $sth = prepare_execute_query($form, $dbh, $query, @filter_vars);
618 while (my $ref = $sth->fetchrow_hashref()) {
619 map { $ref->{$_} ||= "" } @all_fields;
620 push @contents, $ref;
624 if (grep { $orderby eq $_ } qw(bindescription warehousedescription)) {
625 @contents = sort { ($a->{$orderby} cmp $b->{$orderby}) * (($form->{order}) ? 1 : -1) } @contents;
629 $main::lxdebug->leave_sub();
635 $main::lxdebug->enter_sub();
637 my ($self, $qty_op) = @_;
639 if (!$qty_op || ($qty_op eq "dontcare")) {
640 $main::lxdebug->leave_sub();
644 if ($qty_op eq "atleast") {
646 } elsif ($qty_op eq "atmost") {
652 $main::lxdebug->leave_sub();
657 sub retrieve_transfer_types {
658 $main::lxdebug->enter_sub();
661 my $direction = shift;
663 my $myconfig = \%main::myconfig;
664 my $form = $main::form;
666 my $dbh = $form->get_standard_dbh($myconfig);
668 my $types = selectall_hashref_query($form, $dbh, qq|SELECT * FROM transfer_type WHERE direction = ? ORDER BY sortkey|, $direction);
670 $main::lxdebug->leave_sub();
675 sub get_basic_bin_info {
676 $main::lxdebug->enter_sub();
681 Common::check_params(\%params, qw(id));
683 my $myconfig = \%main::myconfig;
684 my $form = $main::form;
686 my $dbh = $params{dbh} || $form->get_standard_dbh();
688 my @ids = 'ARRAY' eq ref $params{id} ? @{ $params{id} } : ($params{id});
691 qq|SELECT b.id AS bin_id, b.description AS bin_description,
692 w.id AS warehouse_id, w.description AS warehouse_description
694 LEFT JOIN warehouse w ON (b.warehouse_id = w.id)
695 WHERE b.id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
697 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
699 if ('' eq ref $params{id}) {
700 $result = $result->[0] || { };
701 $main::lxdebug->leave_sub();
706 $main::lxdebug->leave_sub();
708 return map { $_->{bin_id} => $_ } @{ $result };
711 # Eingabe: Teilenummer, Lagernummer (warehouse)
712 # Ausgabe: Die maximale Anzahl der Teile in diesem Lager
714 sub get_max_qty_parts {
715 $main::lxdebug->enter_sub();
720 Common::check_params(\%params, qw(parts_id warehouse_id)); #die brauchen wir
722 my $myconfig = \%main::myconfig;
723 my $form = $main::form;
725 my $dbh = $params{dbh} || $form->get_standard_dbh();
727 my $query = qq| SELECT SUM(qty), bin_id, chargenumber FROM inventory where parts_id = ? AND warehouse_id = ? GROUP BY bin_id, chargenumber|;
729 my $sth_QTY = prepare_execute_query($form, $dbh, $query, ,$params{parts_id}, $params{warehouse_id}); #info: aufruf an DBUtils.pm
731 my $max_qty_parts = 0; #Initialisierung mit 0
732 while (my $ref = $sth_QTY->fetchrow_hashref()) { # wir laufen über alle chargen und Lagerorte (s.a. SQL-Query oben)
733 $max_qty_parts += $ref->{sum};
736 $main::lxdebug->leave_sub();
738 return $max_qty_parts;
742 # Eingabe: Teilenummer, Lagernummer (warehouse)
743 # Ausgabe: Die Beschreibung der Ware bzw. Erzeugnis
745 sub get_part_description {
746 $main::lxdebug->enter_sub();
751 Common::check_params(\%params, qw(parts_id )); #die brauchen wir
753 my $myconfig = \%main::myconfig;
754 my $form = $main::form;
756 my $dbh = $params{dbh} || $form->get_standard_dbh();
758 my $query = qq| SELECT partnumber, description FROM parts where id = ? |;
760 my $sth = prepare_execute_query($form, $dbh, $query, ,$params{parts_id}); #info: aufruf zu DBUtils.pm
762 my $ref = $sth->fetchrow_hashref();
763 my $part_description = $ref->{partnumber} . " " . $ref->{description};
765 $main::lxdebug->leave_sub();
767 return $part_description;