<?php

$revision_name_val = !empty($_GET['revision_name']) ? $_GET['revision_name'] : null;

if ((!empty($_GET["bom_config_header_id"])) && (!(is_array($_GET["bom_config_header_id"])))) {
 $bom_config_header_id = htmlentities($_GET["bom_config_header_id"]);
}

if ((empty($_GET["bom_config_header_id"])) && (!empty($_GET["reference_key_name"])) && (!empty($_GET["reference_key_value"]))) {
 $bom_config_header_byItem = bom_config_header::find_by_referenceData($_GET["reference_key_name"], $_GET["reference_key_value"]);
 if ($bom_config_header_byItem) {
  foreach ($bom_config_header_byItem as $k => $v) {
   $$class->$k = $v;
  }
  $bom_config_header_id = $$class->bom_config_header_id;
  $bom_config_line_object = bom_config_line::find_by_bom_configHeaderId_revNumber($bom_config_header_id, $revision_name_val);
 }
} else if ((empty($_GET["bom_config_header_id"])) && (!empty($_GET["item_id_m"])) && (!empty($_GET["org_id"]))) {
 $bom_config_header_byItem = bom_header::find_by_itemId_orgId($_GET["item_id_m"], $_GET["org_id"]);
 if ($bom_config_header_byItem) {
  foreach ($bom_config_header_byItem as $k => $v) {
   $$class->$k = $v;
  }
  $bom_config_header_id = $$class->bom_header_id;
  $bom_line_object = bom_line::find_by_bomHeaderId_revNumber($bom_config_header_id, $revision_name_val);
  $bom_config_line_object = [];
  foreach ($bom_line_object as $bom_lines) {
   $bcl = new bom_config_line();
   foreach ($bcl->field_a as $k => $v) {
    if (property_exists($bom_lines, $v)) {
     $bcl->$v = $bom_lines->$v;
    } else {
     $bcl->$v = null;
    }
   }
   array_push($bom_config_line_object, $bcl);
  }
 } else {
  $$class->item_id_m = $_GET["item_id_m"];
  $$class->org_id = $_GET["org_id"];
 }
}

if (!empty($bom_config_header_id)) {
 if (!empty($bom_config_header->item_id_m)) {
  $item = item::find_by_id($bom_config_header->item_id_m);
  $bom_config_header->item_number = $item->item_number;
  $bom_config_header->item_description = $item->item_description;
  $bom_config_header->uom = $item->uom_id;

//   $routing_line_details = bom_config_routing_line_v::find_by_itemIdM_OrgId($$class->item_id_m, $$class->org_id);
//   echo "<br><br><br><br>BOM Routing line";
//   pa($routing_line_details);
 }


 if (!empty($$class->common_bom_config_item_id_m) && !empty($$class->common_bom_config_org_id)) {
  $bom_config_header_byItem = bom_config_header::find_by_itemId_orgId($$class->common_bom_config_item_id_m, $$class->common_bom_config_org_id);
  $bom_config_lines = new bom_config_line();
  $bom_config_line_object = $bom_config_lines->findBy_parentId($bom_config_header_byItem->bom_config_header_id);
  $common_bom_config_details = bom_config_commonbom_config_line::find_by_parent_id($bom_config_header_id);
 }

 if (empty($bom_config_line_object)) {
  $bom_config_lines = new bom_config_line();
  $bom_config_line_object = array();
  array_push($bom_config_line_object, $bom_config_lines);
 } else {
  foreach ($bom_config_line_object as &$bom_config_lines) {
   $item = item::find_by_id($bom_config_lines->component_item_id_m);
   if ($item) {
    $bom_config_lines->component_item_number = $item->item_number;
    $bom_config_lines->component_description = $item->item_description;
    $bom_config_lines->component_uom = $item->uom_id;
   }
   if (empty($bom_config_lines->bom_config_commonbom_config_line_id)) {
    $bom_config_lines->bom_config_commonbom_config_line_id = null;
   }
   if (!empty($common_bom_config_details)) {
    foreach ($common_bom_config_details as $lines) {
     if (!empty($value)) {
      $bom_config_lines->routing_sequence = $lines->routing_sequence;
      $bom_config_lines->planning_percentage = $lines->planning_percentage;
      $bom_config_lines->yield = $lines->yield;
      $bom_config_lines->wip_supply_type = $lines->wip_supply_type;
      $bom_config_lines->supply_sub_inventory = $lines->supply_sub_inventory;
      $bom_config_lines->supply_locator = $lines->supply_locator;
      $bom_config_lines->bom_config_commonbom_config_line_id = $lines->bom_config_commonbom_config_line_id;
     }
    }
   }
  }
 }
}

if (!empty($$class->common_bom_config_item_id_m)) {
 $item_c = item::find_by_id($bom_config_header->common_bom_config_item_id_m);
 $$class->commonBom_item_number = $item_c->item_number;
 $$class->commonBom_item_description = $item_c->item_description;
 $readonly1 = true;
}

if (!empty($$class->item_id_m) && !empty($$class->org_id)) {
//  $item_rev_d = inv_item_revision::find_by_itemIdM_orgId($$class->item_id_m, $$class->org_id);
 $revision_name_a = inv_item_revision::find_by_itemIdM_orgId($$class->item_id_m, $$class->org_id);
} else {
 $revision_name_a = array();
}

if (!empty($_GET['quantity'])) {
 $quantity = $_GET['quantity'];
}

if ((!empty($_GET["reference_key_name"]))) {
 $$class->reference_key_name = $_GET["reference_key_name"];
}

if (!empty($_GET["reference_key_value"])) {
 $$class->reference_key_value = ($_GET["reference_key_value"]);
}

if (!empty($_GET['window_type']) && ( $_GET['window_type'] == 'popover') || $_GET['window_type'] == 'popup' ){
 $config_button_select = null;
} else {
 $config_button_select = ' disabled ';
}
?>