$main::lxdebug->leave_sub();
}
-
-sub get_shipped_qty {
- $main::lxdebug->enter_sub();
-
- # Drei Fälle:
- # $params{oe_id} : Alle Lieferscheine zu diesem Auftrag durchsuchen und pro Auftragsposition die Mengen zurückgeben
- # Wird zur Darstellung der gelieferten Mengen im Auftrag benötigt
- # $params{do_id} : Alle Aufträge zu diesem Lieferschein durchsuchen und pro Lieferscheinposition die Mengen zurückgeben
- # Wird für LaTeX benötigt um im Lieferschein pro Position die Mengen auszugeben
- # $params{delivered}: Alle Aufträge zum Lieferschein $params{do_id} prüfen ob sie vollständiger ausgeliefert sind
- # Wird für das Setzen des 'delivered' Flag in der Datenbank beim "save" des Lieferscheins benötigt
-
- my $self = shift;
- my %params = @_;
-
- # Eigentlich unkritisch: wenn keine der Parameter gesetzt ist kommt ein leerer Hash zurück
- croak ("Need at least one parameter oe_id or do_id") unless $params{oe_id} || $params{do_id};
-
- my $myconfig = \%main::myconfig;
- my $form = $main::form;
- my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
- my %ship = ();
-
- my @oe_ids;
-
- if ($params{oe_id} ) {
- push @oe_ids, $params{oe_id};
- } elsif ($params{do_id}) {
- my @links = RecordLinks->get_links( 'dbh' => $dbh,
- 'from_table' => 'oe',
- 'to_table' => 'delivery_orders',
- 'to_id' => $params{do_id});
-
- @oe_ids = map { $_->{from_id} } @links;
- }
-
- if (scalar (@oe_ids) > 0 ) {
- my $all_units = AM->retrieve_all_units();
- my $placeholders = join(', ', ('?') x scalar @oe_ids);
- my $query = qq|SELECT oi.id, oi.position, oi.parts_id, oi.qty, oi.unit, oi.trans_id,
- p.unit AS partunit FROM orderitems oi
- LEFT JOIN parts p ON (oi.parts_id = p.id)
- WHERE trans_id IN (${placeholders})
- ORDER BY position ASC|;
-
- my $orderitems = selectall_hashref_query($form, $dbh, $query, @oe_ids);
- foreach my $oe_entry (@{ $orderitems }) {
- $oe_entry->{qty} *= AM->convert_unit($oe_entry->{unit}, $oe_entry->{partunit}, $all_units);
- $oe_entry->{qty_notdelivered} = $oe_entry->{qty};
-
- # Bei oe Modus auf jeden Fall einen Record anlegen
- if ( $params{oe_id} ) {
- $ship{$oe_entry->{position}} = {
- 'qty_ordered' => $oe_entry->{qty} ,
- 'qty_notdelivered' => $oe_entry->{qty}
- };
- }
- }
-
- my @dolinks = RecordLinks->get_links('dbh' => $dbh,
- 'from_table' => 'oe',
- 'to_table' => 'delivery_orders',
- 'from_id' => \@oe_ids);
-
- my @do_ids = map { $_->{to_id} } @dolinks ;
- if (scalar (@do_ids) == 0) {
- $main::lxdebug->leave_sub();
- return %ship;
- }
-
- my %oeitems_by_id = map { $_->{id} => $_ } @{ $orderitems };
-
-
- $placeholders = join(', ', ('?') x scalar @do_ids);
- $query = qq|SELECT doi.parts_id, doi.id, doi.qty, doi.unit, doi.position,
- doi.delivery_order_id, COALESCE(rlitem.from_id,0) as from_id,
- p.unit AS partunit
- FROM delivery_order_items doi
- LEFT JOIN parts p ON (doi.parts_id = p.id)
- LEFT JOIN record_links rlitem
- ON (rlitem.to_id = doi.id AND rlitem.to_table='delivery_order_items')
- WHERE doi.delivery_order_id IN (${placeholders})|;
-
- my $deliveryorderitems = selectall_hashref_query($form, $dbh, $query, @do_ids);
-
- # erst mal qty der links bearbeiten
- foreach my $do_entry (@{ $deliveryorderitems }) {
- $do_entry->{qty} *= AM->convert_unit($do_entry->{unit}, $do_entry->{partunit}, $all_units);
- if ($do_entry->{from_id} > 0 ) {
- # record link zwischen items vorhanden, kann auch von anderem Auftrag sein
- my $oe_entry = $oeitems_by_id{$do_entry->{from_id}};
- if ( $oe_entry ) {
- $oe_entry->{qty_notdelivered} -= $do_entry->{qty};
- # derzeit nur ein link pro do_item
- $do_entry->{oe_entry} = $oe_entry;
- }
- } else {
- $main::lxdebug->message(LXDebug->DEBUG2(),"no entry for=".$do_entry->{id}." part=".$do_entry->{parts_id});
- }
- }
- # nun den rest ohne links bearbeiten
- foreach my $do_entry (@{ $deliveryorderitems }) {
- next if $do_entry->{from_id} > 0;
- next if $do_entry->{qty} == 0;
-
- foreach my $oe_entry (@{ $orderitems }) {
- next if $oe_entry->{qty_notdelivered} == 0;
- if ( $do_entry->{parts_id} == $oe_entry->{parts_id} ) {
- # zu viele geliefert auf andere position ?
- if ( $oe_entry->{qty_notdelivered} < 0 ) {
- $do_entry->{qty} += - $oe_entry->{qty_notdelivered};
- $oe_entry->{qty_notdelivered} = 0;
- } else {
- if ( $do_entry->{qty} < $oe_entry->{qty_notdelivered} ) {
- $oe_entry->{qty_notdelivered} -= $do_entry->{qty};
- $do_entry->{qty} = 0;
- } else {
- $do_entry->{qty} -= $oe_entry->{qty_notdelivered};
- $oe_entry->{qty_notdelivered} = 0;
- }
- # derzeit nur ein link pro do_item
- $do_entry->{oe_entry} = $oe_entry if !$do_entry->{oe_entry};
- }
- }
- last if $do_entry->{qty} <= 0;
- }
- }
- if ( $params{oe_id} ) {
- $ship{$_->{position}}->{qty_notdelivered} = $_->{qty_notdelivered} for @{ $orderitems };
- }
- elsif ($params{do_id} && $params{delivered}) {
- foreach my $oe_entry (@{ $orderitems }) {
- if ( !$ship{$oe_entry->{trans_id}} ) {
- $ship{$oe_entry->{trans_id}} = { 'delivered' => 1 };
- }
- $ship{$oe_entry->{trans_id}}->{delivered} = 0 if $oe_entry->{qty_notdelivered} > 0;
- }
- }
- elsif ($params{do_id}) {
- foreach my $do_entry (@{ $deliveryorderitems }) {
- next if $params{do_id} != $do_entry->{delivery_order_id};
- my $position = $do_entry->{position};
- if ( $position > 0 && $do_entry->{oe_entry}) {
- if ( !$ship{$position} ) {
- $ship{$position} = {
- 'qty_ordered' => $do_entry->{oe_entry}->{qty} ,
- 'qty_notdelivered' => $do_entry->{oe_entry}->{qty_notdelivered}
- };
- } else {
- $ship{$position}->{qty_ordered} += $do_entry->{oe_entry}->{qty};
- $ship{$position}->{qty_notdelivered} += $do_entry->{oe_entry}->{qty_notdelivered};
- }
- }
- }
- }
- }
- $main::lxdebug->leave_sub();
- return %ship;
-}
-
sub is_marked_as_delivered {
$main::lxdebug->enter_sub();