<?php

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

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

if (!empty($$class->lines_from_transaction_header) && $$class->lines_from_transaction_header == 9 && !empty($$class->ap_transaction_header_id)) {
 $ap_trns_lines = ap_payment_trnx_v::find_by_transactionId($$class->ap_transaction_header_id);
 $ap_payment_line_object = [];
 if ($ap_trns_lines) {
  foreach ($ap_trns_lines as $ap_trns_line) {
   $ap_payment_line = new ap_payment_line();
   foreach ($ap_payment_line->field_a as $k => $v) {
    if (isset($ap_trns_line->$v)) {
     $ap_payment_line->$v = $ap_trns_line->$v;
    }
   }
   $ap_payment_line->amount = $ap_trns_line->inv_line_price;
   $ap_payment_line->gl_amount = $ap_trns_line->gl_inv_line_price;
   array_push($ap_payment_line_object, $ap_payment_line);
  }
 }
// pa($ap_payment_line_object);
}

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->po_header_id)) {
 $po = new po_header();
 $po_i = $po->findBy_id($$class->po_header_id);
 $$class->po_number = $po_i->po_number;
}

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_payment_line_object)) {
 foreach ($ap_payment_line_object as &$ap_payment_lines) {
  if (!empty($ap_payment_lines->ap_transaction_header_id)) {
   $apth = new ap_transaction_header();
   $apth_i = $apth->findBy_id($ap_payment_lines->ap_transaction_header_id);
   $ap_payment_lines->transaction_number = $apth_i->transaction_number;
   $ap_payment_lines->invoice_amount = $apth_i->header_amount;
   $ap_payment_lines->paid_amount = $apth_i->paid_amount;
   $ap_payment_lines->remaining_amount = $apth_i->header_amount - $apth_i->paid_amount;
  } else {
   $ap_payment_lines->remaining_amount = null;
   $ap_payment_lines->transaction_number = null;
   $ap_payment_lines->invoice_amount = null;
   $ap_payment_lines->paid_amount = null;
  }
 }
}

$$class->exchange_rate = empty($$class->exchange_rate) ?  1 : $$class->exchange_rate;

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

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