<?php

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

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


if (!empty($$class->lines_from_transaction_header) && $$class->lines_from_transaction_header == 9 && !empty($$class->ar_transaction_header_id)) {
 $ar_trns_lines = ar_unpaid_transaction_v::find_by_transactionId($$class->ar_transaction_header_id);
  if (!empty($ar_trns_lines)) {
   $ar_receipt_line_object = [];
  foreach ($ar_trns_lines as $ar_trns_line) {
   $ar_receipt_line = new ar_receipt_line();
   foreach ($ar_receipt_line->field_a as $k => $v) {
    if (isset($ar_trns_line->$v)) {
     $ar_receipt_line->$v = $ar_trns_line->$v;
    }
   }
   $ar_receipt_line->amount = $ar_trns_line->header_amount;
   $ar_receipt_line->exchange_rate = !empty($ar_trns_line->exchange_rate) ? $ar_trns_line->exchange_rate : 1;
   $ar_receipt_line->gl_amount = $ar_receipt_line->amount * $ar_receipt_line->exchange_rate;
   array_push($ar_receipt_line_object, $ar_receipt_line);
  }
 }
}

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->sd_so_header_id)) {
 $po = new sd_so_header();
 $po_i = $po->findBy_id($$class->sd_so_header_id);
 $$class->so_number = $po_i->so_number;
}

if (!empty($$class->ledger_id)) {
 $ledger = new gl_ledger();
 $ledger_i = $ledger->findBy_id($$class->ledger_id);
 $$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 = form::select_field_from_object('period_id', $all_open_periods, 'gl_period_id', 'period_name', $$class->period_id, 'gl_period_id', '', '', '', 1);
}
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 (isset($ar_receipt_header)) {
 $customer_name_stmt = $f->text_field('customer_name', $ar_receipt_header->customer_name, '20', 'customer_name', 'select_customer_name', 1, $readonly1);
 $customer_number_stmt = $f->text_field('customer_number', $ar_receipt_header->customer_number, '8', 'customer_number', 'select_customer_number', 1, $readonly1);
}

if (!empty($ar_receipt_line_object)) {
 foreach ($ar_receipt_line_object as &$ar_receipt_lines) {
  if (!empty($ar_receipt_lines->ar_transaction_header_id)) {
   $arth = new ar_transaction_header();
   $arth_i = $arth->findBy_id($ar_receipt_lines->ar_transaction_header_id);
   $ar_receipt_lines->transaction_number = $arth_i->transaction_number;
   $ar_receipt_lines->invoice_amount = $arth_i->header_amount;
   $ar_receipt_lines->receipt_amount = $arth_i->receipt_amount;
   $ar_receipt_lines->remaining_amount = $arth_i->header_amount - $arth_i->receipt_amount;
  } else {
   $ar_receipt_lines->remaining_amount = null;
   $ar_receipt_lines->transaction_number = null;
   $ar_receipt_lines->invoice_amount = null;
   $ar_receipt_lines->receipt_amount = null;
  }
 }
}


//if (!empty($$class->gl_journal_header_id) || (!empty($$class->receipt_status)) && ($$class->receipt_status == 'CANCELLED' || 'CLOSED')) {
if ( (!empty($$class->receipt_status)) && ($$class->receipt_status == 'CANCELLED' || $$class->receipt_status == 'CLOSED')) {
 $mode = 2;
 $readonly = 1;
 $$class->receipt_action_a = [];
 $$class->receipt_action_a['REVERSE'] = 'Reverse Receipt';
}

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

if (empty($$class->document_date)) {
 $$class->document_date = current_time(1);
}
?>
