<?php
if (!empty($_GET['bu_org_id']) && (!is_array($_GET['bu_org_id']))) {
 $$class->bu_org_id = $_GET['bu_org_id'];
}

if (!empty($_GET['po_header_id']) && (!is_array($_GET['po_header_id']))) {
 $$class->po_header_id = $_GET['po_header_id'];
 $$class->get_details_from_po_header();
}

if (!empty($$class->bu_org_id)) {
 $legal_org_id = org::find_by_id($$class->bu_org_id)->legal_org_id;
 $ledger_id = legal::find_by_orgId($legal_org_id)->ledger_id;
 $$class->ledger_id = $ledger_id;
}

if (!empty($$class->ledger_id)) {
 $ledger = new gl_ledger();
 $ledger_i = $ledger->findBy_id($$class->ledger_id);
 if($ledger_i){
 $$class->currency = $ledger_i->currency_code;
 }
 $gp = new gl_period();
 $all_open_periods = $gp->find_all_periods($$class->ledger_id, 'OPEN');
 if (empty($$class->period_id)) {
	$$class->period_id = $gp->current_open_period($$class->ledger_id, 'OPEN')->gl_period_id;
 }
 $period_name_stmt = $f->select_field_from_object('period_id', $all_open_periods, 'gl_period_id', 'period_name', $$class->period_id, 'gl_period_id', '', 1);
}

 if (!empty($$class->supplier_id)) {
  $supplier_details = supplier::find_by_id($$class->supplier_id);
  $$class->supplier_name = $supplier_details->supplier_name;
  $$class->supplier_number = !empty($supplier_details->supplier_number) ? $supplier_details->supplier_number : null;
 }

if (!empty($ap_transaction_line_object)) {
 foreach ($ap_transaction_line_object as &$ap_transaction_lines) {
	if (!empty($ap_transaction_lines->item_id_m)) {
	 $item = item::find_by_id($ap_transaction_lines->item_id_m);
	 $ap_transaction_lines->item_number = $item->item_number;
	 $ap_transaction_lines->uom_id = $item->uom_id;
	} else {
	 $ap_transaction_lines->item_number = null;
	 $ap_transaction_lines->uom_id = null;
	}
  	if (!empty($ap_transaction_lines->tax_code_id)) {
	 $tax = mdm_tax_code::find_by_id($ap_transaction_lines->tax_code_id);
	 $ap_transaction_lines->tax_code_value = (!empty($tax->percentage)) ? 'p_' . $tax->percentage : 'a_' . $tax->tax_amount;
	} else {
	 $ap_transaction_lines->tax_code_value = null;
	}
 }
 
}

if (!empty($$class->hr_employee_id)) {
 $emp_details = hr_employee::find_by_id($$class->hr_employee_id);
 $$class->document_owner = $emp_details->first_name . ' ' . $emp_details->last_name;
}else{
 $$class->document_owner = null;
}

if((empty($$class->transaction_status))){
$$class->transaction_status = 'INCOMPLETE';
}

if((empty($$class->transaction_type))){
$$class->transaction_type = 'INVOICE';
}

 $action_readonly = in_array($$class->transaction_status,array('CLOSED','INPROCESS','')) ? 1 : '';
 if(empty($$class->ap_transaction_header_id)){
  $$class->action_a = array(null);
 }else if($$class->transaction_type == 'INVOICE'){
  unset($$class->action_a['APPLY_TRANSACTION']);
 }else if($$class->transaction_type == 'DEBIT_MEMO' || $$class->transaction_type == 'CREDIT_MEMO'){
  unset($$class->action_a['MATCH_PO']);
 }

if((!empty($$class->approval_status))&&($$class->approval_status == 'CANCELLED') || 
  (!empty($$class->transaction_status))&&($$class->transaction_status == 'CLOSED')){
 $mode=2;
 $readonly=1;
}
?>
