<?
	header("Content-Type: text/html; charset=utf-8");
	
	$la_tmp = explode('/app/', $_SERVER['SCRIPT_FILENAME']);
	
	$l_path_minus = $la_tmp[0].'/app/webroot/iso/';
	$g_path = $la_tmp[0].'/app/webroot/iso/my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	
	include $l_path_minus.'_all_reports.php';
	
	$can_live_alone = 1;
	include $g_path."u_headers.php";
	
	
	$la_time = get_mailer_time_data();
	
	$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
	
	$x = get_data('select id, name, redoslijed from reports where app_type = "'.$l_tip_prog.'" order by redoslijed ', 1);
	for($i = 0; $i < sizeof($x['id']); $i++){
		
		$l_sql = 'select mailer.id, mailer.creation_date, mailer.automatic, mailer.sent_to_owner, mailer.sent_to_accounting '.
			' from mailer '.
			' where mailer.report_id = '.$x['id'][$i].
			' and mailer_report_type_id = 2 '.
			' and mailer.creation_date = "'.$la_time['en']['last'].'" '.
			' and automatic = 1 '.
			' order by mailer.id desc limit 1 ';
			
		$y = get_data($l_sql, 0);
		
		if(empty($y['id'])){
			$l_rbr = 0;
		}else{
			$l_rbr = $y['id'];
		}
		$l_sent = create_accounting_report_and_send($l_rbr, $i, $x, $y, $la_time);
		if($l_sent == 1){
			break;
		}
		
		
		
		$l_sql = 'select mailer.id, mailer.creation_date, mailer.creation_time, mailer.automatic, mailer.sent_to_owner '.
			' from mailer '.
			' where mailer.report_id = '.$x['id'][$i].
			' and mailer_report_type_id = 1 '.
			' and mailer.creation_date = "'.$la_time['en']['today'].'" '.
			' and automatic = 1 '.
			' order by mailer.id desc limit 1 ';
		
		$y = get_data($l_sql, 0);
		
		if(empty($y['id'])){
			$l_rbr = 0;
		}else{
			$l_rbr = $y['id'];
		}
		$l_sent = create_owner_report_and_send($l_rbr, $i, $x, $y, $la_time);
		if($l_sent == 1){
			break;
		}
	}
	
	
	$la_data = array();
	$la_data['result'] = 1;
	
	if($l_sent == 1){
		if($_GET['mode'] == 1){
			$la_data['break_and_send'] = 1;
		}
		if($_GET['mode'] == 2){
			$la_data['break_and_send'] = 2;
		}
	}else{
		$la_data['break_and_send'] = 0;
	}
	
	$la_json = json_encode($la_data);
	print $la_json;
	exit;
?>
