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 #======================================================================
45 $main::lxdebug->enter_sub();
50 $main::lxdebug->leave_sub();
54 my $myconfig = \%main::myconfig;
55 my $form = $main::form;
57 my $dbh = $form->get_standard_dbh($myconfig);
59 my $units = AM->retrieve_units($myconfig, $form);
61 my $query = qq|SELECT * FROM transfer_type|;
62 my $sth = prepare_execute_query($form, $dbh, $query);
66 while (my $ref = $sth->fetchrow_hashref()) {
67 $transfer_types{$ref->{direction}} ||= { };
68 $transfer_types{$ref->{direction}}->{$ref->{description}} = $ref->{id};
71 my @part_ids = map { $_->{parts_id} } @_;
72 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);
74 my ($now) = selectrow_query($form, $dbh, qq|SELECT current_date|);
76 $query = qq|INSERT INTO inventory (warehouse_id, bin_id, parts_id, chargenumber, oe_id, orderitems_id, shippingdate,
77 employee_id, project_id, trans_id, trans_type_id, comment, qty)
78 VALUES (?, ?, ?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?, ?, ?)|;
80 $sth = prepare_query($form, $dbh, $query);
82 my @directions = (undef, 'out', 'in', 'transfer');
86 my ($trans_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
88 my ($direction, @values) = (0);
90 $direction |= 1 if ($transfer->{src_warehouse_id} && $transfer->{src_bin_id});
91 $direction |= 2 if ($transfer->{dst_warehouse_id} && $transfer->{dst_bin_id});
93 push @values, conv_i($transfer->{parts_id}), "$transfer->{chargenumber}", conv_i($transfer->{oe_id}), conv_i($transfer->{orderitems_id});
94 push @values, $transfer->{shippingdate} eq 'current_date' ? $now : conv_date($transfer->{shippingdate}), $form->{login}, conv_i($transfer->{project_id}), $trans_id;
96 if ($transfer->{transfer_type_id}) {
97 push @values, $transfer->{transfer_type_id};
99 push @values, $transfer_types{$directions[$direction]}->{$transfer->{transfer_type}};
102 push @values, "$transfer->{comment}";
104 my $qty = $transfer->{qty};
106 if ($transfer->{unit}) {
107 my $partunit = $partunits{$transfer->{parts_id}};
109 $qty *= $units->{$transfer->{unit}}->{factor};
110 $qty /= $units->{$partunit}->{factor} || 1 if ($partunit);
113 if ($direction & 1) {
114 do_statement($form, $sth, $query, conv_i($transfer->{src_warehouse_id}), conv_i($transfer->{src_bin_id}), @values, $qty * -1);
117 if ($direction & 2) {
118 do_statement($form, $sth, $query, conv_i($transfer->{dst_warehouse_id}), conv_i($transfer->{dst_bin_id}), @values, $qty);
126 $main::lxdebug->leave_sub();
129 sub transfer_assembly {
130 $main::lxdebug->enter_sub();
134 Common::check_params(\%params, qw(assembly_id dst_warehouse_id login qty unit dst_bin_id chargenumber comment));
136 # my $maxcreate=WH->check_assembly_max_create(assembly_id =>$params{'assembly_id'}, dbh => $my_dbh);
138 my $myconfig = \%main::myconfig;
139 my $form = $main::form;
140 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
145 # ... Standard-Check oben Ende. Hier die eigentliche SQL-Abfrage
146 # select parts_id,qty from assembly where id=1064;
147 # Erweiterung für bug 935 am 23.4.09 - Erzeugnisse können Dienstleistungen enthalten, die ja nicht 'lagerbar' sind.
148 # 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;
149 # Erweiterung für bug 23.4.09 -2 Erzeugnisse in Erzeugnissen können nicht ausgelagert werden, wenn assembly nicht überprüft wird ...
150 # patch von joachim eingespielt 24.4.2009:
151 # my $query = qq|select parts_id,qty from assembly inner join parts
152 # on assembly.parts_id = parts.id where assembly.id = ? and
153 # (inventory_accno_id IS NOT NULL or parts.assembly = TRUE)|;
156 # my $query = qq|select parts_id,qty from assembly where id = ?|;
157 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 or parts.assembly = TRUE)|;
159 my $sth_part_qty_assembly = prepare_execute_query($form, $dbh, $query, $params{assembly_id});
161 # Hier wird das prepared Statement für die Schleife über alle Lagerplätze vorbereitet
162 my $transferPartSQL = qq|INSERT INTO inventory (parts_id, warehouse_id, bin_id, chargenumber, comment, employee_id, qty, trans_id, trans_type_id)
163 VALUES (?, ?, ?, ?, ?,(SELECT id FROM employee WHERE login = ?), ?, nextval('id'),
164 (SELECT id FROM transfer_type WHERE direction = 'out' AND description = 'used'))|;
165 my $sthTransferPartSQL = prepare_query($form, $dbh, $transferPartSQL);
167 # der return-string für die fehlermeldung inkl. welche waren zum fertigen noch fehlen
168 my $kannNichtFertigen ="Für dieses Erzeugnis sind keine Einzelteile definiert.
169 Dementsprechend kann auch nichts hergestellt werden";
171 while (my $hash_ref = $sth_part_qty_assembly->fetchrow_hashref()) { # Schleife für $query=select parts_id,qty from assembly
172 $kannNichtFertigen =""; # Wieder auf erfolgreich setzen LEER == keine Fehlermeldung
173 my $partsQTY = $hash_ref->{qty} * $params{qty}; # benötigte teile * anzahl erzeugnisse
174 my $currentPart_ID = $hash_ref->{parts_id};
176 # Überprüfen, ob diese Anzahl gefertigt werden kann
177 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
179 if ($partsQTY > $max_parts){
180 # Gibt es hier ein Problem mit nicht "escapten" Zeichen? 25.4.09 Antwort: Ja. Aber erst wenn im Frontend die locales-Funktion aufgerufen wird
181 $kannNichtFertigen .= "Zum Fertigen fehlen:" . abs($partsQTY - $max_parts) . " Einheiten der Ware:" . $self->get_part_description(parts_id => $currentPart_ID) . ", um das Erzeugnis herzustellen. <br>"; # Konnte die Menge nicht mit der aktuellen Anzahl der Waren fertigen
182 next; # die weiteren Überprüfungen sind unnötig
185 # Eine kurze Vorabfrage, um den Lagerplatz und die Chargennummber zu bestimmen
186 # Offen: Die Summe über alle Lagerplätze wird noch nicht gebildet
187 # Gelöst: Wir haben vorher schon die Abfrage durchgeführt, ob wir fertigen können.
188 # Noch besser gelöst: Wir laufen durch alle benötigten Waren zum Fertigen und geben eine Rückmeldung an den Benutzer was noch fehlt
189 # und lösen den Rest dann so wie bei xplace im Barcode-Programm
190 # S.a. Kommentar im bin/mozilla-Code mb übernimmt und macht das in ordentlich
192 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|;
193 my $tempsth = prepare_execute_query($form, $dbh, $tempquery, $params{dst_warehouse_id}, $currentPart_ID);
195 # Alle Werte zu dem einzelnen Artikel, die wir später auslagern
196 my $tmpPartsQTY = $partsQTY;
198 while (my $temphash_ref = $tempsth->fetchrow_hashref()) {
199 my $temppart_bin_id = $temphash_ref->{bin_id}; # kann man hier den quelllagerplatz beim verbauen angeben?
200 my $temppart_chargenumber = $temphash_ref->{chargenumber};
201 my $temppart_qty = $temphash_ref->{sum};
202 if ($tmpPartsQTY > $temppart_qty) { # wir haben noch mehr waren zum wegbuchen. Wir buchen den kompletten Lagerplatzbestand und zählen die Hilfsvariable runter
203 $tmpPartsQTY = $tmpPartsQTY - $temppart_qty;
204 $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?
205 do_statement($form, $sthTransferPartSQL, $transferPartSQL, $currentPart_ID, $params{dst_warehouse_id}, $temppart_bin_id, $temppart_chargenumber, 'Verbraucht für ' . $self->get_part_description(parts_id => $params{assembly_id}), $params{login}, $temppart_qty);
207 # hier ist noch ein fehler am besten mit definierten erzeugnissen debuggen 02/2009 jb
208 # idee: ausbuch algorithmus mit rekursion lösen und an- und abschaltbar machen
209 # das problem könnte sein, dass strict nicht an war und sth global eine andere zuweisung bekam
210 # auf jeden fall war der internal-server-error nach aktivierung von strict und warnings plus ein paar my-definitionen weg
211 } else { # okay, wir haben weniger oder gleich Waren die wir wegbuchen müssen, wir können also aufhören
213 do_statement($form, $sthTransferPartSQL, $transferPartSQL, $currentPart_ID, $params{dst_warehouse_id}, $temppart_bin_id, $temppart_chargenumber, 'Verbraucht für ' . $self->get_part_description(parts_id => $params{assembly_id}), $params{login}, $tmpPartsQTY);
214 last; # beendet die schleife (springt zum letzten element)
216 } # ende while SELECT SUM(qty), bin_id, chargenumber FROM inventory WHERE warehouse_id
217 } #ende while select parts_id,qty from assembly where id = ?
218 if ($kannNichtFertigen) {
219 return $kannNichtFertigen;
222 # soweit alles gut. Jetzt noch die wirkliche Lagerbewegung für das Erzeugnis ausführen ...
223 my $transferAssemblySQL = qq|INSERT INTO inventory (parts_id, warehouse_id, bin_id, chargenumber, comment, employee_id, qty, trans_id, trans_type_id)
224 VALUES (?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, nextval('id'),
225 (SELECT id FROM transfer_type WHERE direction = 'in' AND description = 'stock'))|;
226 my $sthTransferAssemblySQL = prepare_query($form, $dbh, $transferAssemblySQL);
227 do_statement($form, $sthTransferAssemblySQL, $transferAssemblySQL, $params{assembly_id}, $params{dst_warehouse_id}, $params{dst_bin_id}, $params{chargenumber}, $params{comment}, $params{login}, $params{qty});
230 $main::lxdebug->leave_sub();
231 return 1; # Alles erfolgreich
234 sub get_warehouse_journal {
235 $main::lxdebug->enter_sub();
240 my $myconfig = \%main::myconfig;
241 my $form = $main::form;
243 my $all_units = AM->retrieve_units($myconfig, $form);
245 # connect to database
246 my $dbh = $form->get_standard_dbh($myconfig);
249 my (@filter_ary, @filter_vars, $joins, %select_tokens, %select);
251 if ($filter{warehouse_id} ne '') {
252 push @filter_ary, "w1.id = ? OR w2.id = ?";
253 push @filter_vars, $filter{warehouse_id}, $filter{warehouse_id};
256 if ($filter{bin_id} ne '') {
257 push @filter_ary, "b1.id = ? OR b2.id = ?";
258 push @filter_vars, $filter{bin_id}, $filter{bin_id};
261 if ($filter{partnumber}) {
262 push @filter_ary, "p.partnumber ILIKE ?";
263 push @filter_vars, '%' . $filter{partnumber} . '%';
266 if ($filter{description}) {
267 push @filter_ary, "(p.description ILIKE ?)";
268 push @filter_vars, '%' . $filter{description} . '%';
271 if ($filter{chargenumber}) {
272 push @filter_ary, "i1.chargenumber ILIKE ?";
273 push @filter_vars, '%' . $filter{chargenumber} . '%';
276 if ($form->{fromdate}) {
277 push @filter_ary, "?::DATE <= i1.itime::DATE";
278 push @filter_vars, $form->{fromdate};
281 if ($form->{todate}) {
282 push @filter_ary, "?::DATE >= i1.itime::DATE";
283 push @filter_vars, $form->{todate};
286 if ($form->{l_employee}) {
290 # prepare qty comparison for later filtering
291 my ($f_qty_op, $f_qty, $f_qty_base_unit);
292 if ($filter{qty_op} && defined($filter{qty}) && $filter{qty_unit} && $all_units->{$filter{qty_unit}}) {
293 $f_qty_op = $filter{qty_op};
294 $f_qty = $filter{qty} * $all_units->{$filter{qty_unit}}->{factor};
295 $f_qty_base_unit = $all_units->{$filter{qty_unit}}->{base_unit};
298 map { $_ = "(${_})"; } @filter_ary;
300 # if of a property number or description is requested,
301 # automatically check the matching id too.
302 map { $form->{"l_${_}id"} = "Y" if ($form->{"l_${_}description"} || $form->{"l_${_}number"}); } qw(warehouse bin);
304 # customize shown entry for not available fields.
305 $filter{na} = '-' unless $filter{na};
307 # make order, search in $filter and $form
308 my $sort_col = $form->{sort};
309 my $sort_order = $form->{order};
311 $sort_col = $filter{sort} unless $sort_col;
312 $sort_order = ($sort_col = 'itime') unless $sort_col;
313 $sort_col = 'itime' if $sort_col eq 'date';
314 $sort_order = $filter{order} unless $sort_order;
315 my $sort_spec = "${sort_col} " . ($sort_order ? " DESC" : " ASC");
317 my $where_clause = join(" AND ", @filter_ary) . " AND " if (@filter_ary);
319 $select_tokens{'trans'} = {
320 "parts_id" => "i1.parts_id",
321 "qty" => "ABS(SUM(i1.qty))",
322 "partnumber" => "p.partnumber",
323 "partdescription" => "p.description",
324 "bindescription" => "b.description",
325 "chargenumber" => "i1.chargenumber",
326 "warehousedescription" => "w.description",
327 "partunit" => "p.unit",
328 "bin_from" => "b1.description",
329 "bin_to" => "b2.description",
330 "warehouse_from" => "w1.description",
331 "warehouse_to" => "w2.description",
332 "comment" => "i1.comment",
333 "trans_type" => "tt.description",
334 "trans_id" => "i1.trans_id",
335 "oe_id" => "COALESCE(i1.oe_id, i2.oe_id)",
336 "date" => "i1.itime::DATE",
337 "itime" => "i1.itime",
338 "employee" => "e.name",
339 "projectnumber" => "COALESCE(pr.projectnumber, '$filter{na}')",
342 $select_tokens{'out'} = {
343 "bin_to" => "'$filter{na}'",
344 "warehouse_to" => "'$filter{na}'",
347 $select_tokens{'in'} = {
348 "bin_from" => "'$filter{na}'",
349 "warehouse_from" => "'$filter{na}'",
352 # build the select clauses.
353 # take all the requested ones from the first hash and overwrite them from the out/in hashes if present.
354 for my $i ('trans', 'out', 'in') {
355 $select{$i} = join ', ', map { +/^l_/; ($select_tokens{$i}{"$'"} || $select_tokens{'trans'}{"$'"}) . " AS r_$'" }
356 ( grep( { !/qty$/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form), qw(l_parts_id l_qty l_partunit l_itime) );
359 my $group_clause = join ", ", map { +/^l_/; "r_$'" }
360 ( grep( { !/qty$/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form), qw(l_parts_id l_partunit l_itime) );
363 qq|SELECT DISTINCT $select{trans}
365 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
366 LEFT JOIN parts p ON i1.parts_id = p.id
367 LEFT JOIN bin b1 ON i1.bin_id = b1.id
368 LEFT JOIN bin b2 ON i2.bin_id = b2.id
369 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
370 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
371 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
372 LEFT JOIN project pr ON i1.project_id = pr.id
373 LEFT JOIN employee e ON i1.employee_id = e.id
374 WHERE $where_clause i2.qty = -i1.qty AND i2.qty > 0 AND
375 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 2 )
376 GROUP BY $group_clause
380 SELECT DISTINCT $select{out}
382 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
383 LEFT JOIN parts p ON i1.parts_id = p.id
384 LEFT JOIN bin b1 ON i1.bin_id = b1.id
385 LEFT JOIN bin b2 ON i2.bin_id = b2.id
386 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
387 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
388 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
389 LEFT JOIN project pr ON i1.project_id = pr.id
390 LEFT JOIN employee e ON i1.employee_id = e.id
391 WHERE $where_clause i1.qty < 0 AND
392 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 1 )
393 GROUP BY $group_clause
397 SELECT DISTINCT $select{in}
399 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
400 LEFT JOIN parts p ON i1.parts_id = p.id
401 LEFT JOIN bin b1 ON i1.bin_id = b1.id
402 LEFT JOIN bin b2 ON i2.bin_id = b2.id
403 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
404 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
405 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
406 LEFT JOIN project pr ON i1.project_id = pr.id
407 LEFT JOIN employee e ON i1.employee_id = e.id
408 WHERE $where_clause i1.qty > 0 AND
409 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 1 )
410 GROUP BY $group_clause
411 ORDER BY r_${sort_spec}|;
413 my $sth = prepare_execute_query($form, $dbh, $query, @filter_vars, @filter_vars, @filter_vars);
415 my ($h_oe_id, $q_oe_id);
416 if ($form->{l_oe_id}) {
419 CASE WHEN oe.quotation THEN oe.quonumber ELSE oe.ordnumber END AS number,
421 WHEN oe.customer_id IS NOT NULL AND COALESCE(oe.quotation, FALSE) THEN 'sales_quotation'
422 WHEN oe.customer_id IS NOT NULL AND NOT COALESCE(oe.quotation, FALSE) THEN 'sales_order'
423 WHEN oe.customer_id IS NULL AND COALESCE(oe.quotation, FALSE) THEN 'request_quotation'
424 ELSE 'purchase_order'
431 SELECT dord.id AS id, dord.donumber AS number,
433 WHEN dord.customer_id IS NULL THEN 'purchase_delivery_order'
434 ELSE 'sales_delivery_order'
436 FROM delivery_orders dord
441 SELECT ar.id AS id, ar.invnumber AS number, 'sales_invoice' AS type
447 SELECT ap.id AS id, ap.invnumber AS number, 'purchase_invoice' AS type
451 $h_oe_id = prepare_query($form, $dbh, $q_oe_id);
455 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
456 map { /^r_/; $ref->{"$'"} = $ref->{$_} } keys %$ref;
457 my $qty = $ref->{"qty"} * 1;
459 next unless ($qty > 0);
462 my $part_unit = $all_units->{$ref->{"partunit"}};
463 next unless ($part_unit && ($part_unit->{"base_unit"} eq $f_qty_base_unit));
464 $qty *= $part_unit->{"factor"};
465 next if (('=' eq $f_qty_op) && ($qty != $f_qty));
466 next if (('>=' eq $f_qty_op) && ($qty < $f_qty));
467 next if (('<=' eq $f_qty_op) && ($qty > $f_qty));
470 if ($h_oe_id && $ref->{oe_id}) {
471 do_statement($form, $h_oe_id, $q_oe_id, ($ref->{oe_id}) x 4);
472 $ref->{oe_id_info} = $h_oe_id->fetchrow_hashref() || {};
475 push @contents, $ref;
479 $h_oe_id->finish() if $h_oe_id;
481 $main::lxdebug->leave_sub();
487 # This sub is the primary function to retrieve information about items in warehouses.
488 # $filter is a hashref and supports the following keys:
489 # - warehouse_id - will return matches with this warehouse_id only
490 # - partnumber - will return only matches where the given string is a substring of the partnumber
491 # - partsid - will return matches with this parts_id only
492 # - description - will return only matches where the given string is a substring of the description
493 # - chargenumber - will return only matches where the given string is a substring of the chargenumber
494 # - ean - will return only matches where the given string is a substring of the ean as stored in the table parts (article)
495 # - charge_ids - must be an arrayref. will return contents with these ids only
496 # - expires_in - will only return matches that expire within the given number of days
497 # will also add a column named 'has_expired' containing if the match has already expired or not
498 # - hazardous - will return matches with the flag hazardous only
499 # - oil - will return matches with the flag oil only
500 # - qty, qty_op - quantity filter (more info to come)
501 # - sort, order_by - sorting (more to come)
502 # - reservation - will provide an extra column containing the amount reserved of this match
503 # note: reservation flag turns off warehouse_* or bin_* information. both together don't make sense, since reserved info is stored separately
505 sub get_warehouse_report {
506 $main::lxdebug->enter_sub();
511 my $myconfig = \%main::myconfig;
512 my $form = $main::form;
514 my $all_units = AM->retrieve_units($myconfig, $form);
516 # connect to database
517 my $dbh = $form->get_standard_dbh($myconfig);
520 my (@filter_ary, @filter_vars, @wh_bin_filter_ary, @wh_bin_filter_vars);
522 delete $form->{include_empty_bins} unless ($form->{l_warehousedescription} || $form->{l_bindescription});
524 if ($filter{warehouse_id}) {
525 push @wh_bin_filter_ary, "w.id = ?";
526 push @wh_bin_filter_vars, $filter{warehouse_id};
529 if ($filter{bin_id}) {
530 push @wh_bin_filter_ary, "b.id = ?";
531 push @wh_bin_filter_vars, $filter{bin_id};
534 push @filter_ary, @wh_bin_filter_ary;
535 push @filter_vars, @wh_bin_filter_vars;
537 if ($filter{partnumber}) {
538 push @filter_ary, "p.partnumber ILIKE ?";
539 push @filter_vars, '%' . $filter{partnumber} . '%';
542 if ($filter{description}) {
543 push @filter_ary, "p.description ILIKE ?";
544 push @filter_vars, '%' . $filter{description} . '%';
547 if ($filter{partsid}) {
548 push @filter_ary, "p.id = ?";
549 push @filter_vars, $filter{partsid};
552 if ($filter{chargenumber}) {
553 push @filter_ary, "i.chargenumber ILIKE ?";
554 push @filter_vars, '%' . $filter{chargenumber} . '%';
557 push @filter_ary, "p.ean ILIKE ?";
558 push @filter_vars, '%' . $filter{ean} . '%';
561 # prepare qty comparison for later filtering
562 my ($f_qty_op, $f_qty, $f_qty_base_unit);
564 if ($filter{qty_op} && defined $filter{qty} && $filter{qty_unit} && $all_units->{$filter{qty_unit}}) {
565 $f_qty_op = $filter{qty_op};
566 $f_qty = $filter{qty} * $all_units->{$filter{qty_unit}}->{factor};
567 $f_qty_base_unit = $all_units->{$filter{qty_unit}}->{base_unit};
570 map { $_ = "(${_})"; } @filter_ary;
572 # if of a property number or description is requested,
573 # automatically check the matching id too.
574 map { $form->{"l_${_}id"} = "Y" if ($form->{"l_${_}description"} || $form->{"l_${_}number"}); } qw(warehouse bin);
576 # make order, search in $filter and $form
577 my $sort_col = $form->{sort};
578 my $sort_order = $form->{order};
580 $sort_col = $filter{sort} unless $sort_col;
581 $sort_col = "parts_id" unless $sort_col;
582 $sort_order = $filter{order} unless $sort_order;
583 $sort_col =~ s/ASC|DESC//; # kill stuff left in from previous queries
584 my $orderby = $sort_col;
585 my $sort_spec = "${sort_col} " . ($sort_order ? " DESC" : " ASC");
587 my $where_clause = join " AND ", ("1=1", @filter_ary);
589 my %select_tokens = (
590 "parts_id" => "i.parts_id",
591 "qty" => "SUM(i.qty)",
592 "warehouseid" => "i.warehouse_id",
593 "partnumber" => "p.partnumber",
594 "partdescription" => "p.description",
595 "bindescription" => "b.description",
597 "chargenumber" => "i.chargenumber",
599 "chargeid" => "c.id",
600 "warehousedescription" => "w.description",
601 "partunit" => "p.unit",
602 "stock_value" => "p.lastcost / COALESCE(pfac.factor, 1)",
604 my $select_clause = join ', ', map { +/^l_/; "$select_tokens{$'} AS $'" }
605 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
606 qw(l_parts_id l_qty l_partunit) );
608 my $group_clause = join ", ", map { +/^l_/; "$'" }
609 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
610 qw(l_parts_id l_partunit) );
613 "stock_value" => "LEFT JOIN price_factors pfac ON (p.price_factor_id = pfac.id)",
616 my $joins = join ' ', grep { $_ } map { +/^l_/; $join_tokens{"$'"} }
617 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
618 qw(l_parts_id l_qty l_partunit) );
621 qq|SELECT $select_clause
623 LEFT JOIN parts p ON i.parts_id = p.id
624 LEFT JOIN bin b ON i.bin_id = b.id
625 LEFT JOIN warehouse w ON i.warehouse_id = w.id
628 GROUP BY $group_clause
629 ORDER BY $sort_spec|;
631 my $sth = prepare_execute_query($form, $dbh, $query, @filter_vars);
633 my (%non_empty_bins, @all_fields, @contents);
635 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
637 my $qty = $ref->{qty};
639 next unless ($qty != 0);
642 my $part_unit = $all_units->{$ref->{partunit}};
643 next if (!$part_unit || ($part_unit->{base_unit} ne $f_qty_base_unit));
644 $qty *= $part_unit->{factor};
645 next if (('=' eq $f_qty_op) && ($qty != $f_qty));
646 next if (('>=' eq $f_qty_op) && ($qty < $f_qty));
647 next if (('<=' eq $f_qty_op) && ($qty > $f_qty));
650 if ($form->{include_empty_bins}) {
651 $non_empty_bins{$ref->{binid}} = 1;
652 @all_fields = keys %{ $ref } unless (@all_fields);
655 $ref->{stock_value} = ($ref->{stock_value} || 0) * $ref->{qty};
657 push @contents, $ref;
662 if ($form->{include_empty_bins}) {
665 w.id AS warehouseid, w.description AS warehousedescription,
666 b.id AS binid, b.description AS bindescription
668 LEFT JOIN warehouse w ON (b.warehouse_id = w.id)|;
670 @filter_ary = @wh_bin_filter_ary;
671 @filter_vars = @wh_bin_filter_vars;
673 my @non_empty_bin_ids = keys %non_empty_bins;
674 if (@non_empty_bin_ids) {
675 push @filter_ary, qq|NOT b.id IN (| . join(', ', map { '?' } @non_empty_bin_ids) . qq|)|;
676 push @filter_vars, @non_empty_bin_ids;
679 $query .= qq| WHERE | . join(' AND ', map { "($_)" } @filter_ary) if (@filter_ary);
681 $sth = prepare_execute_query($form, $dbh, $query, @filter_vars);
683 while (my $ref = $sth->fetchrow_hashref()) {
684 map { $ref->{$_} ||= "" } @all_fields;
685 push @contents, $ref;
689 if (grep { $orderby eq $_ } qw(bindescription warehousedescription)) {
690 @contents = sort { ($a->{$orderby} cmp $b->{$orderby}) * (($form->{order}) ? 1 : -1) } @contents;
694 $main::lxdebug->leave_sub();
700 $main::lxdebug->enter_sub();
702 my ($self, $qty_op) = @_;
704 if (!$qty_op || ($qty_op eq "dontcare")) {
705 $main::lxdebug->leave_sub();
709 if ($qty_op eq "atleast") {
711 } elsif ($qty_op eq "atmost") {
717 $main::lxdebug->leave_sub();
722 sub retrieve_transfer_types {
723 $main::lxdebug->enter_sub();
726 my $direction = shift;
728 my $myconfig = \%main::myconfig;
729 my $form = $main::form;
731 my $dbh = $form->get_standard_dbh($myconfig);
733 my $types = selectall_hashref_query($form, $dbh, qq|SELECT * FROM transfer_type WHERE direction = ? ORDER BY sortkey|, $direction);
735 $main::lxdebug->leave_sub();
740 sub get_basic_bin_info {
741 $main::lxdebug->enter_sub();
746 Common::check_params(\%params, qw(id));
748 my $myconfig = \%main::myconfig;
749 my $form = $main::form;
751 my $dbh = $params{dbh} || $form->get_standard_dbh();
753 my @ids = 'ARRAY' eq ref $params{id} ? @{ $params{id} } : ($params{id});
756 qq|SELECT b.id AS bin_id, b.description AS bin_description,
757 w.id AS warehouse_id, w.description AS warehouse_description
759 LEFT JOIN warehouse w ON (b.warehouse_id = w.id)
760 WHERE b.id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
762 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
764 if ('' eq ref $params{id}) {
765 $result = $result->[0] || { };
766 $main::lxdebug->leave_sub();
771 $main::lxdebug->leave_sub();
773 return map { $_->{bin_id} => $_ } @{ $result };
776 # Eingabe: Teilenummer, Lagernummer (warehouse)
777 # Ausgabe: Die maximale Anzahl der Teile in diesem Lager
779 sub get_max_qty_parts {
780 $main::lxdebug->enter_sub();
785 Common::check_params(\%params, qw(parts_id warehouse_id)); #die brauchen wir
787 my $myconfig = \%main::myconfig;
788 my $form = $main::form;
790 my $dbh = $params{dbh} || $form->get_standard_dbh();
792 my $query = qq| SELECT SUM(qty), bin_id, chargenumber FROM inventory where parts_id = ? AND warehouse_id = ? GROUP BY bin_id, chargenumber|;
794 my $sth_QTY = prepare_execute_query($form, $dbh, $query, ,$params{parts_id}, $params{warehouse_id}); #info: aufruf an DBUtils.pm
796 my $max_qty_parts = 0; #Initialisierung mit 0
797 while (my $ref = $sth_QTY->fetchrow_hashref()) { # wir laufen über alle chargen und Lagerorte (s.a. SQL-Query oben)
798 $max_qty_parts += $ref->{sum};
801 $main::lxdebug->leave_sub();
803 return $max_qty_parts;
807 # Eingabe: Teilenummer, Lagernummer (warehouse)
808 # Ausgabe: Die Beschreibung der Ware bzw. Erzeugnis
810 sub get_part_description {
811 $main::lxdebug->enter_sub();
816 Common::check_params(\%params, qw(parts_id)); #die brauchen wir
818 my $myconfig = \%main::myconfig;
819 my $form = $main::form;
821 my $dbh = $params{dbh} || $form->get_standard_dbh();
823 my $query = qq| SELECT partnumber, description FROM parts where id = ? |;
825 my $sth = prepare_execute_query($form, $dbh, $query, ,$params{parts_id}); #info: aufruf zu DBUtils.pm
827 my $ref = $sth->fetchrow_hashref();
828 my $part_description = $ref->{partnumber} . " " . $ref->{description};
830 $main::lxdebug->leave_sub();
832 return $part_description;