<?php

if (empty($_SESSION['pick_list'])) {
 return;
} else {
// pa($_SESSION['pick_list']);
 $posData = get_postArray_From_jqSearializedArray($_SESSION['pick_list'][0]);
}

$data_ar = [];
foreach ($posData['sd_so_line_id'] as $k => $v) {
 $curren_r = new stdClass();
 $sd_so_i = new sd_so_all_v();
 $sd_so_v = $sd_so_i->findBY_soLineId($v);
 if ($sd_so_v->kit_cb) {
  if (!empty($sd_so_v->bom_config_header_id)) {
   $bom_lines_all = bom_config_line::find_by_parent_id($sd_so_v->bom_config_header_id);
   if (empty($bom_lines_all)) {
    $show_message = 'No configuration found for SO# ' . $sd_so_v->so_number . ' & Line Number ' . $sd_so_v->line_number;
    continue;
   }
   foreach ($bom_lines_all as $bom_lines) {
    $curren_r = new stdClass();
    $item_i = item::find_by_item_id_m($bom_lines->component_item_id_m);
    $curren_r->item_number = $item_i->item_number;
    $curren_r->item_description = $item_i->item_description;
    $curren_r->staging_subinventory = $sd_so_v->staging_subinventory;
    $curren_r->staging_locator = $sd_so_v->staging_locator;
    $curren_r->so_number = $sd_so_v->so_number;
    $curren_r->line_number = $sd_so_v->line_number;
    $curren_r->sd_so_line_id = $sd_so_v->sd_so_line_id;
    $picked_qty = 0;
    $onhand_v = onhand_v::find_by_itemId_orgId($bom_lines->component_item_id_m, $sd_so_v->shipping_org_id);
    if (empty($onhand_v)) {
     $curren_r->line_quantity = 'No Onhand';
     array_push($data_ar, $curren_r);
     continue;
    }
    foreach ($onhand_v as $onhand_r) {
     if (empty($onhand_r->onhand) || ($onhand_r->onhand < 0)) {
      continue;
     }
     if ($picked_qty >= $sd_so_v->line_quantity) {
      break;
     }
     $remaining_qty = $sd_so_v->line_quantity - $picked_qty;
     if ($onhand_r->onhand <= $remaining_qty) {
      $curren_r->from_subinventory = $onhand_r->subinventory;
      $curren_r->from_locator = $onhand_r->locator;
      $curren_r->line_quantity = $onhand_r->onhand;
      $picked_qty += $onhand_r->onhand;
      array_push($data_ar, $curren_r);
     } else {
      $curren_r->from_subinventory = $onhand_r->subinventory;
      $curren_r->from_locator = $onhand_r->locator;
      $curren_r->line_quantity = $remaining_qty;
      $picked_qty += $remaining_qty;
      array_push($data_ar, $curren_r);
      break;
     }
    }
   }
   continue;
  } else {
   $show_message = 'No configuration found for SO# ' . $sd_so_v->so_number . 'Line Number ' . $sd_so_v->line_number;
  }
 }
 $curren_r->item_number = $sd_so_v->item_number;
 $curren_r->item_description = $sd_so_v->item_description;
 $curren_r->staging_subinventory = $sd_so_v->staging_subinventory;
 $curren_r->staging_locator = $sd_so_v->staging_locator;
 $curren_r->so_number = $sd_so_v->so_number;
 $curren_r->line_number = $sd_so_v->line_number;
 $curren_r->sd_so_line_id = $sd_so_v->sd_so_line_id;

 $picked_qty = 0;
 $onhand_v = onhand_v::find_by_itemId_orgId($sd_so_v->item_id_m, $sd_so_v->shipping_org_id);
 if (empty($onhand_v)) {
  $curren_r->line_quantity = 'No Onhand';

  array_push($data_ar, $curren_r);
  continue;
 }

 foreach ($onhand_v as $onhand_r) {
  if (empty($onhand_r->onhand) || ($onhand_r->onhand < 0)) {
   continue;
  }
  if ($picked_qty >= $sd_so_v->line_quantity) {
   break;
  }
  $remaining_qty = $sd_so_v->line_quantity - $picked_qty;
  if ($onhand_r->onhand <= $remaining_qty) {
   $curren_r->from_subinventory = $onhand_r->subinventory;
   $curren_r->from_locator = $onhand_r->locator;
   $curren_r->line_quantity = $onhand_r->onhand;
   $picked_qty += $onhand_r->onhand;
   array_push($data_ar, $curren_r);
  } else {
   $curren_r->from_subinventory = $onhand_r->subinventory;
   $curren_r->from_locator = $onhand_r->locator;
   $curren_r->line_quantity = $remaining_qty;
   $picked_qty += $remaining_qty;
   array_push($data_ar, $curren_r);
   break;
  }
 }
}
?>