<?php


if (!empty($$class->hd_svo_header_id) && !empty($$class->item_id_m)) {
 $item_p = item::find_by_id($$class->item_id_m);
 $$class->item_number = $item_p->item_number;
 $$class->item_description = $item_p->item_description;
}

if (!empty($$class->hd_svo_header_id) && !empty($hd_svo_line_object)) {
 foreach ($hd_svo_line_object as &$hd_svo_lines) {
  if (!empty($hd_svo_lines->item_id_m)) {
   $item = item::find_by_id($hd_svo_lines->item_id_m);
   $hd_svo_lines->item_number = $item->item_number;
   $hd_svo_lines->item_description = $item->item_description;
//   $hd_svo_lines->uom_id = $item->uom_id;
  }

  if (empty($hd_svo_lines->sd_so_line_id)) {
   $hd_svo_lines->so_number = null;
   $hd_svo_lines->so_line_number = null;
  }
 }
}

//asign values of fields on form but not in databse only if $$class->$primary_cloumn is not null
if (!empty($$class->ar_customer_id)) {
 $ac = new ar_customer();
 $ac->findBy_id($$class->ar_customer_id);
 $$class->customer_name = $ac->customer_name;
 $$class->customer_number = $ac->customer_number;
}

$customer_site_obj = [];
if (!empty($$class->ar_customer_site_id)) {
 $arc = new ar_customer_site();
 $arc_i = $arc->findBy_id($$class->ar_customer_site_id);
 array_push($customer_site_obj, $arc_i);
}

if (empty($$class->order_status)) {
 $$class->so_status = 'ENTERED';
}

if (empty($$class->order_source_type)) {
 $$class->order_source_type = 'MANUAL';
}

if (!empty($$class->service_person_employee_id)) {
 $emp_details = hr_employee::find_by_id($$class->service_person_employee_id);
 $$class->service_person = $emp_details->first_name . ' ' . $emp_details->last_name;
}

if (empty($hd_svo_estimates_object) && !empty($$class->hd_svo_header_id)) {
 $hd_svo_estimates_object = hd_svo_estimates::find_by_parent_id($$class->hd_svo_header_id);
}
if (empty($hd_svo_estimates_object)) {
 $hd_svo_estimates_object = array(new hd_svo_estimates());
}

if (empty($hd_svo_actuals_object) && !empty($$class->hd_svo_header_id)) {
 $hd_svo_actuals_object = hd_svo_actuals::find_by_parent_id($$class->hd_svo_header_id);
}
if (empty($hd_svo_actuals_object)) {
 $hd_svo_actuals_object = array(new hd_svo_actuals());
}


if ((!empty($$class->arproval_status)) && ($$class->arproval_status == 'CANCELLED')) {
 $mode = 2;
 $readonly = 1;
}

$add_to_order_a = [];

 foreach ($hd_svo_line_object as $hd_svo_line) {
  if(!in_array($hd_svo_line->sd_so_header_id, $add_to_order_a)){
   $add_to_order_a[$hd_svo_line->sd_so_header_id] = $hd_svo_line->sd_so_header_id;
  }
 }
?>

