<?php 
include("../config.php");
$sd = date("Y-m-d", strtotime($_GET['start_date']));
$ed = date("Y-m-d", strtotime($_GET['end_date']));

$where = '';
$type  = $_GET['account_name'];
if($type != '' && $type != 'null' && !empty($type)){
	$select 		= "SELECT * FROM `act_master` WHERE id = '".$type."'";	
}else{
	$select 		= "SELECT * FROM `act_master` ORDER BY ac_name ASC";
}

$result 		= mysqli_query($conn,$select);
$total       	= mysqli_num_rows($result);
$table       	= '';
$total_count    = 0;
$year  			= explode('-', $_GET['start_date']);
$start_date     = '01-04-' . $year[2];

if(count($total) > 0) {
	while ($row = mysqli_fetch_array($result)) {
		
		$gl_dr 		 = "SELECT sum(amount) as dr_amnt FROM `general_ledger` WHERE `debit_account` = '".$row['act_code']."' AND voucher_date >= '".$start_date."' AND voucher_date <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$gl_dr_res  = mysqli_query($conn,$gl_dr);
		$gl_dr_rows = mysqli_fetch_array($gl_dr_res);
		$dr 		= $gl_dr_rows['dr_amnt'];
		$dr_open_bal= $row['dr_open_bal'];

		$gl_cr 		 = "SELECT sum(amount) as cr_amnt FROM `general_ledger` WHERE `credit_account` = '".$row['act_code']."' AND voucher_date >= '".$start_date."' AND voucher_date <= '".$ed."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$gl_cr_res  = mysqli_query($conn,$gl_cr);
		$gl_cr_rows = mysqli_fetch_array($gl_cr_res);
		$cr 		= $gl_cr_rows['cr_amnt'];
		$cr_open_bal= $row['cr_open_bal'];
		$dr 		= ($dr+$dr_open_bal);
		$cr 		= ($cr+$cr_open_bal);
		$op = $dr - $cr;
		if($op >= 0){
			$flag = "DR";
		}else{
			$flag = "CR";
		}
		$table .= '<tr>';
		$table .= '<td><a href="#" id='.$row["act_code"].' onclick="viewData(this);">'.$row["act_code"].'</a></td>';
		/*$table .= '<td class="text-center"><a href="index.php?page=report/general_ledger_details.php&id='.$row["act_code"].'">' . $row['act_code'] . '</td>';
*/		$table .= '<td class="text-left">' . $row['ac_name'] . '</td>';
		///$table .= '<td class="text-right">' . abs($op) .'.00</td>';
		$table .= '<td class="text-right">' . customFormatMoney(abs($op)) .'</td>';
		$table .= '<td class="text-center">' . $flag	 . '</td>';
		$table .= '</tr>';
	}
}
$data = array('status' => 'success', 'total_count' => $total, 'tbody' => $table);
	echo json_encode($data);
    die();
?>