<?php

$current_open_period = 'No Open Period';
$next_open_period = [];
if (!empty($_GET['ledger_id'])) {
 $ledger_id = $_GET['ledger_id'];
 $ledger_c = new gl_ledger();
 $ledger_i = $ledger_c->findBy_id($ledger_id);
 $cop = $$class->current_open_period($ledger_id);
 $gl_period_object = gl_period::find_all_periods($ledger_id);
 if (!empty($cop)) {
	$nc = new gl_calendar();
	$nc_i = $nc->findBy_id($cop->gl_calendar_id);
	$current_open_period = $nc_i->name;
	$next_open_period = $$class->next_open_period($ledger_id);
 } else {
	$next_open_period = $$class->find_all_periods_ofLedger($ledger_id);
 }
}else{
 $ledger_id = null;
}

if(empty($gl_period_object)){
 $gl_period_object = [new gl_period()];
}

$cop_stmt = form::text_field_ap(array('name' => 'current_open_period', 'value' => $current_open_period,
						'readonly' => 1));
//$nop_stmt = form::select_field_from_object('gl_calendar_id', $next_open_period, 'gl_calendar_id', 'name', '', 'new_gl_calendar_id', $readonly);
//$gl_period_object = [];

if (!(empty($gl_period_object))) {
 foreach ($gl_period_object as &$object) {
//	pa($object);
	if (!empty($ledger_id)) {
	 $gl_period_i = $$class->find_by_ledgerId_calendarId($ledger_id, $object->gl_calendar_id);
	 $status = !empty($gl_period_i) ? $gl_period_i->status : null;
	 $object->ledger_id = $ledger_id;
	}
	if (!empty($status)) {
	 $object->status = $status;
	 $object->gl_period_id = $gl_period_i->gl_period_id;
	 $object->period_name = $gl_period_i->period_name;
	 $object->en_dis = 0;
	} else {
	 $object->status = 'AVAILABLE';
	 $object->en_dis = 1;
	 $object->gl_period_id = null;
	 $object->period_name = null;
	}
	
//	array_push($gl_period_object, $object);
	unset($object->disabled);
 }
} 

$pageno = !empty($_GET['pageno']) ? $_GET['pageno'] : 1;
$per_page = !empty($_GET['per_page']) ? $_GET['per_page'] : 10;
$total_count = count($gl_period_object);
$pagination = new pagination($pageno, $per_page, $total_count);
$pagination->setProperty('_path', 'form');
$position = ($pageno - 1) * $per_page;
?>