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, bestbefore,
77 oe_id, orderitems_id, shippingdate,
78 employee_id, project_id, trans_id, trans_type_id, comment, qty)
79 VALUES (?, ?, ?, ?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?, ?, ?)|;
81 $sth = prepare_query($form, $dbh, $query);
83 my @directions = (undef, 'out', 'in', 'transfer');
87 my ($trans_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
89 my ($direction, @values) = (0);
91 $direction |= 1 if ($transfer->{src_warehouse_id} && $transfer->{src_bin_id});
92 $direction |= 2 if ($transfer->{dst_warehouse_id} && $transfer->{dst_bin_id});
94 push @values, conv_i($transfer->{parts_id}), "$transfer->{chargenumber}", conv_date($transfer->{bestbefore}), conv_i($transfer->{oe_id}), conv_i($transfer->{orderitems_id});
95 push @values, $transfer->{shippingdate} eq 'current_date' ? $now : conv_date($transfer->{shippingdate}), $form->{login}, conv_i($transfer->{project_id}), $trans_id;
97 if ($transfer->{transfer_type_id}) {
98 push @values, $transfer->{transfer_type_id};
100 push @values, $transfer_types{$directions[$direction]}->{$transfer->{transfer_type}};
103 push @values, "$transfer->{comment}";
105 my $qty = $transfer->{qty};
107 if ($transfer->{unit}) {
108 my $partunit = $partunits{$transfer->{parts_id}};
110 $qty *= $units->{$transfer->{unit}}->{factor};
111 $qty /= $units->{$partunit}->{factor} || 1 if ($partunit);
114 if ($direction & 1) {
115 do_statement($form, $sth, $query, conv_i($transfer->{src_warehouse_id}), conv_i($transfer->{src_bin_id}), @values, $qty * -1);
118 if ($direction & 2) {
119 do_statement($form, $sth, $query, conv_i($transfer->{dst_warehouse_id}), conv_i($transfer->{dst_bin_id}), @values, $qty);
127 $main::lxdebug->leave_sub();
130 sub transfer_assembly {
131 $main::lxdebug->enter_sub();
135 Common::check_params(\%params, qw(assembly_id dst_warehouse_id login qty unit dst_bin_id chargenumber bestbefore comment));
137 # my $maxcreate=WH->check_assembly_max_create(assembly_id =>$params{'assembly_id'}, dbh => $my_dbh);
139 my $myconfig = \%main::myconfig;
140 my $form = $main::form;
141 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
146 # ... Standard-Check oben Ende. Hier die eigentliche SQL-Abfrage
147 # select parts_id,qty from assembly where id=1064;
148 # Erweiterung für bug 935 am 23.4.09 -
149 # Erzeugnisse können Dienstleistungen enthalten, die ja nicht 'lagerbar' sind.
150 # select parts_id,qty from assembly inner join parts on assembly.parts_id = parts.id
151 # where assembly.id=1066 and inventory_accno_id IS NOT NULL;
153 # Erweiterung für bug 23.4.09 -2 Erzeugnisse in Erzeugnissen können nicht ausgelagert werden,
154 # wenn assembly nicht überprüft wird ...
155 # patch von joachim eingespielt 24.4.2009:
156 # my $query = qq|select parts_id,qty from assembly inner join parts
157 # on assembly.parts_id = parts.id where assembly.id = ? and
158 # (inventory_accno_id IS NOT NULL or parts.assembly = TRUE)|;
161 my $query = qq|select parts_id,qty from assembly inner join parts on assembly.parts_id = parts.id
162 where assembly.id = ? and (inventory_accno_id IS NOT NULL or parts.assembly = TRUE)|;
164 my $sth_part_qty_assembly = prepare_execute_query($form, $dbh, $query, $params{assembly_id});
166 # Hier wird das prepared Statement für die Schleife über alle Lagerplätze vorbereitet
167 my $transferPartSQL = qq|INSERT INTO inventory (parts_id, warehouse_id, bin_id, chargenumber, bestbefore, comment, employee_id, qty, trans_id, trans_type_id)
168 VALUES (?, ?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, nextval('id'),
169 (SELECT id FROM transfer_type WHERE direction = 'out' AND description = 'used'))|;
170 my $sthTransferPartSQL = prepare_query($form, $dbh, $transferPartSQL);
172 # der return-string für die fehlermeldung inkl. welche waren zum fertigen noch fehlen
173 my $kannNichtFertigen ="Für dieses Erzeugnis sind keine Einzelteile definiert.
174 Dementsprechend kann auch nichts hergestellt werden";
176 while (my $hash_ref = $sth_part_qty_assembly->fetchrow_hashref()) { #Schleife für select parts_id,(...) from assembly
177 $kannNichtFertigen =""; # Wieder auf erfolgreich setzen LEER == keine Fehlermeldung
178 my $partsQTY = $hash_ref->{qty} * $params{qty}; # benötigte teile * anzahl erzeugnisse
179 my $currentPart_ID = $hash_ref->{parts_id};
181 # Überprüfen, ob diese Anzahl gefertigt werden kann
182 my $max_parts = $self->get_max_qty_parts(parts_id => $currentPart_ID, # $self->method() == this.method()
183 warehouse_id => $params{dst_warehouse_id});
185 if ($partsQTY > $max_parts){
186 # Gibt es hier ein Problem mit nicht "escapten" Zeichen?
187 # 25.4.09 Antwort: Ja. Aber erst wenn im Frontend die locales-Funktion aufgerufen wird
189 $kannNichtFertigen .= "Zum Fertigen fehlen:" . abs($partsQTY - $max_parts) .
190 " Einheiten der Ware:" . $self->get_part_description(parts_id => $currentPart_ID) .
191 ", um das Erzeugnis herzustellen. <br>"; # Konnte die Menge nicht mit der aktuellen Anzahl der Waren fertigen
192 next; # die weiteren Überprüfungen sind unnötig, daher das nächste elemente prüfen (genaue Ausgabe, was noch fehlt)
195 # Eine kurze Vorabfrage, um den Lagerplatz, Chargennummer und die Mindesthaltbarkeit zu bestimmen
196 # Offen: Die Summe über alle Lagerplätze wird noch nicht gebildet
197 # Gelöst: Wir haben vorher schon die Abfrage durchgeführt, ob wir fertigen können.
198 # Noch besser gelöst: Wir laufen durch alle benötigten Waren zum Fertigen und geben eine Rückmeldung an den Benutzer was noch fehlt
199 # und lösen den Rest dann so wie bei xplace im Barcode-Programm
200 # S.a. Kommentar im bin/mozilla-Code mb übernimmt und macht das in ordentlich
202 my $tempquery = qq|SELECT SUM(qty), bin_id, chargenumber, bestbefore FROM inventory
203 WHERE warehouse_id = ? AND parts_id = ? GROUP BY bin_id, chargenumber, bestbefore having SUM(qty)>0|;
204 my $tempsth = prepare_execute_query($form, $dbh, $tempquery, $params{dst_warehouse_id}, $currentPart_ID);
206 # Alle Werte zu dem einzelnen Artikel, die wir später auslagern
207 my $tmpPartsQTY = $partsQTY;
209 while (my $temphash_ref = $tempsth->fetchrow_hashref()) {
210 my $temppart_bin_id = $temphash_ref->{bin_id}; # kann man hier den quelllagerplatz beim verbauen angeben?
211 my $temppart_chargenumber = $temphash_ref->{chargenumber};
212 my $temppart_bestbefore = $temphash_ref->{bestbefore};
213 my $temppart_qty = $temphash_ref->{sum};
215 if ($tmpPartsQTY > $temppart_qty) { # wir haben noch mehr waren zum wegbuchen.
216 # Wir buchen den kompletten Lagerplatzbestand und zählen die Hilfsvariable runter
217 $tmpPartsQTY = $tmpPartsQTY - $temppart_qty;
218 $temppart_qty = $temppart_qty * -1; # TODO beim analyiseren des sql-trace, war dieser wert positiv,
219 # wenn * -1 als berechnung in der parameter-übergabe angegeben wird.
220 # Dieser Wert IST und BLEIBT positiv!! Hilfe.
221 # Liegt das daran, dass dieser Wert aus einem SQL-Statement stammt?
222 do_statement($form, $sthTransferPartSQL, $transferPartSQL, $currentPart_ID, $params{dst_warehouse_id},
223 $temppart_bin_id, $temppart_chargenumber, $temppart_bestbefore, 'Verbraucht für ' .
224 $self->get_part_description(parts_id => $params{assembly_id}), $params{login}, $temppart_qty);
226 # hier ist noch ein fehler am besten mit definierten erzeugnissen debuggen 02/2009 jb
227 # idee: ausbuch algorithmus mit rekursion lösen und an- und abschaltbar machen
228 # das problem könnte sein, dass strict nicht an war und sth global eine andere zuweisung bekam
229 # auf jeden fall war der internal-server-error nach aktivierung von strict und warnings plus ein paar my-definitionen weg
230 } else { # okay, wir haben weniger oder gleich Waren die wir wegbuchen müssen, wir können also aufhören
232 do_statement($form, $sthTransferPartSQL, $transferPartSQL, $currentPart_ID, $params{dst_warehouse_id},
233 $temppart_bin_id, $temppart_chargenumber, $temppart_bestbefore, 'Verbraucht für ' .
234 $self->get_part_description(parts_id => $params{assembly_id}), $params{login}, $tmpPartsQTY);
235 last; # beendet die schleife (springt zum letzten element)
237 } # ende while SELECT SUM(qty), bin_id, chargenumber, bestbefore FROM inventory WHERE warehouse_id
238 } #ende while select parts_id,qty from assembly where id = ?
239 if ($kannNichtFertigen) {
240 return $kannNichtFertigen;
243 # soweit alles gut. Jetzt noch die wirkliche Lagerbewegung für das Erzeugnis ausführen ...
244 my $transferAssemblySQL = qq|INSERT INTO inventory (parts_id, warehouse_id, bin_id, chargenumber, bestbefore,
245 comment, employee_id, qty, trans_id, trans_type_id)
246 VALUES (?, ?, ?, ?, ?, ?, (SELECT id FROM employee WHERE login = ?), ?, nextval('id'),
247 (SELECT id FROM transfer_type WHERE direction = 'in' AND description = 'stock'))|;
248 my $sthTransferAssemblySQL = prepare_query($form, $dbh, $transferAssemblySQL);
249 do_statement($form, $sthTransferAssemblySQL, $transferAssemblySQL, $params{assembly_id}, $params{dst_warehouse_id},
250 $params{dst_bin_id}, $params{chargenumber}, $params{bestbefore}, $params{comment}, $params{login}, $params{qty});
253 $main::lxdebug->leave_sub();
254 return 1; # Alles erfolgreich
257 sub get_warehouse_journal {
258 $main::lxdebug->enter_sub();
263 my $myconfig = \%main::myconfig;
264 my $form = $main::form;
266 my $all_units = AM->retrieve_units($myconfig, $form);
268 # connect to database
269 my $dbh = $form->get_standard_dbh($myconfig);
272 my (@filter_ary, @filter_vars, $joins, %select_tokens, %select);
274 if ($filter{warehouse_id} ne '') {
275 push @filter_ary, "w1.id = ? OR w2.id = ?";
276 push @filter_vars, $filter{warehouse_id}, $filter{warehouse_id};
279 if ($filter{bin_id} ne '') {
280 push @filter_ary, "b1.id = ? OR b2.id = ?";
281 push @filter_vars, $filter{bin_id}, $filter{bin_id};
284 if ($filter{partnumber}) {
285 push @filter_ary, "p.partnumber ILIKE ?";
286 push @filter_vars, '%' . $filter{partnumber} . '%';
289 if ($filter{description}) {
290 push @filter_ary, "(p.description ILIKE ?)";
291 push @filter_vars, '%' . $filter{description} . '%';
294 if ($filter{chargenumber}) {
295 push @filter_ary, "i1.chargenumber ILIKE ?";
296 push @filter_vars, '%' . $filter{chargenumber} . '%';
299 if ($form->{bestbefore}) {
300 push @filter_ary, "?::DATE = i1.bestbefore::DATE";
301 push @filter_vars, $form->{bestbefore};
304 if ($form->{fromdate}) {
305 push @filter_ary, "?::DATE <= i1.itime::DATE";
306 push @filter_vars, $form->{fromdate};
309 if ($form->{todate}) {
310 push @filter_ary, "?::DATE >= i1.itime::DATE";
311 push @filter_vars, $form->{todate};
314 if ($form->{l_employee}) {
318 # prepare qty comparison for later filtering
319 my ($f_qty_op, $f_qty, $f_qty_base_unit);
320 if ($filter{qty_op} && defined($filter{qty}) && $filter{qty_unit} && $all_units->{$filter{qty_unit}}) {
321 $f_qty_op = $filter{qty_op};
322 $f_qty = $filter{qty} * $all_units->{$filter{qty_unit}}->{factor};
323 $f_qty_base_unit = $all_units->{$filter{qty_unit}}->{base_unit};
326 map { $_ = "(${_})"; } @filter_ary;
328 # if of a property number or description is requested,
329 # automatically check the matching id too.
330 map { $form->{"l_${_}id"} = "Y" if ($form->{"l_${_}description"} || $form->{"l_${_}number"}); } qw(warehouse bin);
332 # customize shown entry for not available fields.
333 $filter{na} = '-' unless $filter{na};
335 # make order, search in $filter and $form
336 my $sort_col = $form->{sort};
337 my $sort_order = $form->{order};
339 $sort_col = $filter{sort} unless $sort_col;
340 $sort_order = ($sort_col = 'itime') unless $sort_col;
341 $sort_col = 'itime' if $sort_col eq 'date';
342 $sort_order = $filter{order} unless $sort_order;
343 my $sort_spec = "${sort_col} " . ($sort_order ? " DESC" : " ASC");
345 my $where_clause = join(" AND ", @filter_ary) . " AND " if (@filter_ary);
347 $select_tokens{'trans'} = {
348 "parts_id" => "i1.parts_id",
349 "qty" => "ABS(SUM(i1.qty))",
350 "partnumber" => "p.partnumber",
351 "partdescription" => "p.description",
352 "bindescription" => "b.description",
353 "chargenumber" => "i1.chargenumber",
354 "bestbefore" => "i1.bestbefore",
355 "warehousedescription" => "w.description",
356 "partunit" => "p.unit",
357 "bin_from" => "b1.description",
358 "bin_to" => "b2.description",
359 "warehouse_from" => "w1.description",
360 "warehouse_to" => "w2.description",
361 "comment" => "i1.comment",
362 "trans_type" => "tt.description",
363 "trans_id" => "i1.trans_id",
364 "oe_id" => "COALESCE(i1.oe_id, i2.oe_id)",
365 "date" => "i1.itime::DATE",
366 "itime" => "i1.itime",
367 "employee" => "e.name",
368 "projectnumber" => "COALESCE(pr.projectnumber, '$filter{na}')",
371 $select_tokens{'out'} = {
372 "bin_to" => "'$filter{na}'",
373 "warehouse_to" => "'$filter{na}'",
376 $select_tokens{'in'} = {
377 "bin_from" => "'$filter{na}'",
378 "warehouse_from" => "'$filter{na}'",
381 # build the select clauses.
382 # take all the requested ones from the first hash and overwrite them from the out/in hashes if present.
383 for my $i ('trans', 'out', 'in') {
384 $select{$i} = join ', ', map { +/^l_/; ($select_tokens{$i}{"$'"} || $select_tokens{'trans'}{"$'"}) . " AS r_$'" }
385 ( grep( { !/qty$/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form), qw(l_parts_id l_qty l_partunit l_itime) );
388 my $group_clause = join ", ", map { +/^l_/; "r_$'" }
389 ( grep( { !/qty$/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form), qw(l_parts_id l_partunit l_itime) );
392 qq|SELECT DISTINCT $select{trans}
394 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
395 LEFT JOIN parts p ON i1.parts_id = p.id
396 LEFT JOIN bin b1 ON i1.bin_id = b1.id
397 LEFT JOIN bin b2 ON i2.bin_id = b2.id
398 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
399 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
400 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
401 LEFT JOIN project pr ON i1.project_id = pr.id
402 LEFT JOIN employee e ON i1.employee_id = e.id
403 WHERE $where_clause i2.qty = -i1.qty AND i2.qty > 0 AND
404 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 2 )
405 GROUP BY $group_clause
409 SELECT DISTINCT $select{out}
411 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
412 LEFT JOIN parts p ON i1.parts_id = p.id
413 LEFT JOIN bin b1 ON i1.bin_id = b1.id
414 LEFT JOIN bin b2 ON i2.bin_id = b2.id
415 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
416 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
417 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
418 LEFT JOIN project pr ON i1.project_id = pr.id
419 LEFT JOIN employee e ON i1.employee_id = e.id
420 WHERE $where_clause i1.qty < 0 AND
421 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 1 )
422 GROUP BY $group_clause
426 SELECT DISTINCT $select{in}
428 LEFT JOIN inventory i2 ON i1.trans_id = i2.trans_id
429 LEFT JOIN parts p ON i1.parts_id = p.id
430 LEFT JOIN bin b1 ON i1.bin_id = b1.id
431 LEFT JOIN bin b2 ON i2.bin_id = b2.id
432 LEFT JOIN warehouse w1 ON i1.warehouse_id = w1.id
433 LEFT JOIN warehouse w2 ON i2.warehouse_id = w2.id
434 LEFT JOIN transfer_type tt ON i1.trans_type_id = tt.id
435 LEFT JOIN project pr ON i1.project_id = pr.id
436 LEFT JOIN employee e ON i1.employee_id = e.id
437 WHERE $where_clause i1.qty > 0 AND
438 i1.trans_id IN ( SELECT i.trans_id FROM inventory i GROUP BY i.trans_id HAVING COUNT(i.trans_id) = 1 )
439 GROUP BY $group_clause
440 ORDER BY r_${sort_spec}|;
442 my $sth = prepare_execute_query($form, $dbh, $query, @filter_vars, @filter_vars, @filter_vars);
444 my ($h_oe_id, $q_oe_id);
445 if ($form->{l_oe_id}) {
448 CASE WHEN oe.quotation THEN oe.quonumber ELSE oe.ordnumber END AS number,
450 WHEN oe.customer_id IS NOT NULL AND COALESCE(oe.quotation, FALSE) THEN 'sales_quotation'
451 WHEN oe.customer_id IS NOT NULL AND NOT COALESCE(oe.quotation, FALSE) THEN 'sales_order'
452 WHEN oe.customer_id IS NULL AND COALESCE(oe.quotation, FALSE) THEN 'request_quotation'
453 ELSE 'purchase_order'
460 SELECT dord.id AS id, dord.donumber AS number,
462 WHEN dord.customer_id IS NULL THEN 'purchase_delivery_order'
463 ELSE 'sales_delivery_order'
465 FROM delivery_orders dord
470 SELECT ar.id AS id, ar.invnumber AS number, 'sales_invoice' AS type
476 SELECT ap.id AS id, ap.invnumber AS number, 'purchase_invoice' AS type
480 $h_oe_id = prepare_query($form, $dbh, $q_oe_id);
484 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
485 map { /^r_/; $ref->{"$'"} = $ref->{$_} } keys %$ref;
486 my $qty = $ref->{"qty"} * 1;
488 next unless ($qty > 0);
491 my $part_unit = $all_units->{$ref->{"partunit"}};
492 next unless ($part_unit && ($part_unit->{"base_unit"} eq $f_qty_base_unit));
493 $qty *= $part_unit->{"factor"};
494 next if (('=' eq $f_qty_op) && ($qty != $f_qty));
495 next if (('>=' eq $f_qty_op) && ($qty < $f_qty));
496 next if (('<=' eq $f_qty_op) && ($qty > $f_qty));
499 if ($h_oe_id && $ref->{oe_id}) {
500 do_statement($form, $h_oe_id, $q_oe_id, ($ref->{oe_id}) x 4);
501 $ref->{oe_id_info} = $h_oe_id->fetchrow_hashref() || {};
504 push @contents, $ref;
508 $h_oe_id->finish() if $h_oe_id;
510 $main::lxdebug->leave_sub();
516 # This sub is the primary function to retrieve information about items in warehouses.
517 # $filter is a hashref and supports the following keys:
518 # - warehouse_id - will return matches with this warehouse_id only
519 # - partnumber - will return only matches where the given string is a substring of the partnumber
520 # - partsid - will return matches with this parts_id only
521 # - description - will return only matches where the given string is a substring of the description
522 # - chargenumber - will return only matches where the given string is a substring of the chargenumber
523 # - bestbefore - will return only matches with this bestbefore date
524 # - ean - will return only matches where the given string is a substring of the ean as stored in the table parts (article)
525 # - charge_ids - must be an arrayref. will return contents with these ids only
526 # - expires_in - will only return matches that expire within the given number of days
527 # will also add a column named 'has_expired' containing if the match has already expired or not
528 # - hazardous - will return matches with the flag hazardous only
529 # - oil - will return matches with the flag oil only
530 # - qty, qty_op - quantity filter (more info to come)
531 # - sort, order_by - sorting (more to come)
532 # - reservation - will provide an extra column containing the amount reserved of this match
533 # note: reservation flag turns off warehouse_* or bin_* information. both together don't make sense, since reserved info is stored separately
535 sub get_warehouse_report {
536 $main::lxdebug->enter_sub();
541 my $myconfig = \%main::myconfig;
542 my $form = $main::form;
544 my $all_units = AM->retrieve_units($myconfig, $form);
546 # connect to database
547 my $dbh = $form->get_standard_dbh($myconfig);
550 my (@filter_ary, @filter_vars, @wh_bin_filter_ary, @wh_bin_filter_vars);
552 delete $form->{include_empty_bins} unless ($form->{l_warehousedescription} || $form->{l_bindescription});
554 if ($filter{warehouse_id}) {
555 push @wh_bin_filter_ary, "w.id = ?";
556 push @wh_bin_filter_vars, $filter{warehouse_id};
559 if ($filter{bin_id}) {
560 push @wh_bin_filter_ary, "b.id = ?";
561 push @wh_bin_filter_vars, $filter{bin_id};
564 push @filter_ary, @wh_bin_filter_ary;
565 push @filter_vars, @wh_bin_filter_vars;
567 if ($filter{partnumber}) {
568 push @filter_ary, "p.partnumber ILIKE ?";
569 push @filter_vars, '%' . $filter{partnumber} . '%';
572 if ($filter{description}) {
573 push @filter_ary, "p.description ILIKE ?";
574 push @filter_vars, '%' . $filter{description} . '%';
577 if ($filter{partsid}) {
578 push @filter_ary, "p.id = ?";
579 push @filter_vars, $filter{partsid};
582 if ($filter{chargenumber}) {
583 push @filter_ary, "i.chargenumber ILIKE ?";
584 push @filter_vars, '%' . $filter{chargenumber} . '%';
587 if ($form->{bestbefore}) {
588 push @filter_ary, "?::DATE = i.bestbefore::DATE";
589 push @filter_vars, $form->{bestbefore};
593 push @filter_ary, "p.ean ILIKE ?";
594 push @filter_vars, '%' . $filter{ean} . '%';
597 # prepare qty comparison for later filtering
598 my ($f_qty_op, $f_qty, $f_qty_base_unit);
600 if ($filter{qty_op} && defined $filter{qty} && $filter{qty_unit} && $all_units->{$filter{qty_unit}}) {
601 $f_qty_op = $filter{qty_op};
602 $f_qty = $filter{qty} * $all_units->{$filter{qty_unit}}->{factor};
603 $f_qty_base_unit = $all_units->{$filter{qty_unit}}->{base_unit};
606 map { $_ = "(${_})"; } @filter_ary;
608 # if of a property number or description is requested,
609 # automatically check the matching id too.
610 map { $form->{"l_${_}id"} = "Y" if ($form->{"l_${_}description"} || $form->{"l_${_}number"}); } qw(warehouse bin);
612 # make order, search in $filter and $form
613 my $sort_col = $form->{sort};
614 my $sort_order = $form->{order};
616 $sort_col = $filter{sort} unless $sort_col;
617 $sort_col = "parts_id" unless $sort_col;
618 $sort_order = $filter{order} unless $sort_order;
619 $sort_col =~ s/ASC|DESC//; # kill stuff left in from previous queries
620 my $orderby = $sort_col;
621 my $sort_spec = "${sort_col} " . ($sort_order ? " DESC" : " ASC");
623 my $where_clause = join " AND ", ("1=1", @filter_ary);
625 my %select_tokens = (
626 "parts_id" => "i.parts_id",
627 "qty" => "SUM(i.qty)",
628 "warehouseid" => "i.warehouse_id",
629 "partnumber" => "p.partnumber",
630 "partdescription" => "p.description",
631 "bindescription" => "b.description",
633 "chargenumber" => "i.chargenumber",
634 "bestbefore" => "i.bestbefore",
636 "chargeid" => "c.id",
637 "warehousedescription" => "w.description",
638 "partunit" => "p.unit",
639 "stock_value" => "p.lastcost / COALESCE(pfac.factor, 1)",
641 my $select_clause = join ', ', map { +/^l_/; "$select_tokens{$'} AS $'" }
642 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
643 qw(l_parts_id l_qty l_partunit) );
645 my $group_clause = join ", ", map { +/^l_/; "$'" }
646 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
647 qw(l_parts_id l_partunit) );
650 "stock_value" => "LEFT JOIN price_factors pfac ON (p.price_factor_id = pfac.id)",
653 my $joins = join ' ', grep { $_ } map { +/^l_/; $join_tokens{"$'"} }
654 ( grep( { !/qty/ and /^l_/ and $form->{$_} eq 'Y' } keys %$form),
655 qw(l_parts_id l_qty l_partunit) );
658 qq|SELECT $select_clause
660 LEFT JOIN parts p ON i.parts_id = p.id
661 LEFT JOIN bin b ON i.bin_id = b.id
662 LEFT JOIN warehouse w ON i.warehouse_id = w.id
665 GROUP BY $group_clause
666 ORDER BY $sort_spec|;
668 my $sth = prepare_execute_query($form, $dbh, $query, @filter_vars);
670 my (%non_empty_bins, @all_fields, @contents);
672 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
674 my $qty = $ref->{qty};
676 next unless ($qty != 0);
679 my $part_unit = $all_units->{$ref->{partunit}};
680 next if (!$part_unit || ($part_unit->{base_unit} ne $f_qty_base_unit));
681 $qty *= $part_unit->{factor};
682 next if (('=' eq $f_qty_op) && ($qty != $f_qty));
683 next if (('>=' eq $f_qty_op) && ($qty < $f_qty));
684 next if (('<=' eq $f_qty_op) && ($qty > $f_qty));
687 if ($form->{include_empty_bins}) {
688 $non_empty_bins{$ref->{binid}} = 1;
689 @all_fields = keys %{ $ref } unless (@all_fields);
692 $ref->{stock_value} = ($ref->{stock_value} || 0) * $ref->{qty};
694 push @contents, $ref;
699 if ($form->{include_empty_bins}) {
702 w.id AS warehouseid, w.description AS warehousedescription,
703 b.id AS binid, b.description AS bindescription
705 LEFT JOIN warehouse w ON (b.warehouse_id = w.id)|;
707 @filter_ary = @wh_bin_filter_ary;
708 @filter_vars = @wh_bin_filter_vars;
710 my @non_empty_bin_ids = keys %non_empty_bins;
711 if (@non_empty_bin_ids) {
712 push @filter_ary, qq|NOT b.id IN (| . join(', ', map { '?' } @non_empty_bin_ids) . qq|)|;
713 push @filter_vars, @non_empty_bin_ids;
716 $query .= qq| WHERE | . join(' AND ', map { "($_)" } @filter_ary) if (@filter_ary);
718 $sth = prepare_execute_query($form, $dbh, $query, @filter_vars);
720 while (my $ref = $sth->fetchrow_hashref()) {
721 map { $ref->{$_} ||= "" } @all_fields;
722 push @contents, $ref;
726 if (grep { $orderby eq $_ } qw(bindescription warehousedescription)) {
727 @contents = sort { ($a->{$orderby} cmp $b->{$orderby}) * (($form->{order}) ? 1 : -1) } @contents;
731 $main::lxdebug->leave_sub();
737 $main::lxdebug->enter_sub();
739 my ($self, $qty_op) = @_;
741 if (!$qty_op || ($qty_op eq "dontcare")) {
742 $main::lxdebug->leave_sub();
746 if ($qty_op eq "atleast") {
748 } elsif ($qty_op eq "atmost") {
754 $main::lxdebug->leave_sub();
759 sub retrieve_transfer_types {
760 $main::lxdebug->enter_sub();
763 my $direction = shift;
765 my $myconfig = \%main::myconfig;
766 my $form = $main::form;
768 my $dbh = $form->get_standard_dbh($myconfig);
770 my $types = selectall_hashref_query($form, $dbh, qq|SELECT * FROM transfer_type WHERE direction = ? ORDER BY sortkey|, $direction);
772 $main::lxdebug->leave_sub();
777 sub get_basic_bin_info {
778 $main::lxdebug->enter_sub();
783 Common::check_params(\%params, qw(id));
785 my $myconfig = \%main::myconfig;
786 my $form = $main::form;
788 my $dbh = $params{dbh} || $form->get_standard_dbh();
790 my @ids = 'ARRAY' eq ref $params{id} ? @{ $params{id} } : ($params{id});
793 qq|SELECT b.id AS bin_id, b.description AS bin_description,
794 w.id AS warehouse_id, w.description AS warehouse_description
796 LEFT JOIN warehouse w ON (b.warehouse_id = w.id)
797 WHERE b.id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)|;
799 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
801 if ('' eq ref $params{id}) {
802 $result = $result->[0] || { };
803 $main::lxdebug->leave_sub();
808 $main::lxdebug->leave_sub();
810 return map { $_->{bin_id} => $_ } @{ $result };
813 # Eingabe: Teilenummer, Lagernummer (warehouse)
814 # Ausgabe: Die maximale Anzahl der Teile in diesem Lager
816 sub get_max_qty_parts {
817 $main::lxdebug->enter_sub();
822 Common::check_params(\%params, qw(parts_id warehouse_id)); #die brauchen wir
824 my $myconfig = \%main::myconfig;
825 my $form = $main::form;
827 my $dbh = $params{dbh} || $form->get_standard_dbh();
829 my $query = qq| SELECT SUM(qty), bin_id, chargenumber, bestbefore FROM inventory where parts_id = ? AND warehouse_id = ? GROUP BY bin_id, chargenumber, bestbefore|;
831 my $sth_QTY = prepare_execute_query($form, $dbh, $query, ,$params{parts_id}, $params{warehouse_id}); #info: aufruf an DBUtils.pm
833 my $max_qty_parts = 0; #Initialisierung mit 0
834 while (my $ref = $sth_QTY->fetchrow_hashref()) { # wir laufen über alle Haltbarkeiten, chargen und Lagerorte (s.a. SQL-Query oben)
835 $max_qty_parts += $ref->{sum};
838 $main::lxdebug->leave_sub();
840 return $max_qty_parts;
844 # Eingabe: Teilenummer, Lagernummer (warehouse)
845 # Ausgabe: Die Beschreibung der Ware bzw. Erzeugnis
847 sub get_part_description {
848 $main::lxdebug->enter_sub();
853 Common::check_params(\%params, qw(parts_id)); #die brauchen wir
855 my $myconfig = \%main::myconfig;
856 my $form = $main::form;
858 my $dbh = $params{dbh} || $form->get_standard_dbh();
860 my $query = qq| SELECT partnumber, description FROM parts where id = ? |;
862 my $sth = prepare_execute_query($form, $dbh, $query, ,$params{parts_id}); #info: aufruf zu DBUtils.pm
864 my $ref = $sth->fetchrow_hashref();
865 my $part_description = $ref->{partnumber} . " " . $ref->{description};
867 $main::lxdebug->leave_sub();
869 return $part_description;