<?
	header("Content-Type: text/html; charset=utf-8");
	
	$g_path = '../../my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	
	$can_live_alone = 1;
	include $g_path."u_headers.php";
	
	$result = 0;
	
	$g_path = get_item('select value from settings where name = "putanja.abs" ');
	$l_path = $g_path.'app/webroot/res/files/';
	if($_GET['type'] == 1){
		$l_name = $_GET['report'].'.pdf';
	}
	if($_GET['type'] == 2){
		$l_name = $_GET['report'].'.csv';
	}
	if($_GET['type'] == 3){
		$l_name = $_GET['report'].'.txt';
	}
	
	
	if(isset($_GET['report']) && is_file($l_path.$l_name)){
		
		$l_do_it = mailer_check_date();
		
		$l_add_mail = '';
		switch($_GET['report_id']){
			case 1:
			case 8:
				// dnevni promet
				if($l_do_it){
					$l_do_date = $_GET['param1'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}else{
					$l_do_date = '01-01-2000';
					$l_period = 'za period od 01-01-2000 do 01-01-2000';
				}
				
				$la_tmp = explode('-', $l_do_date);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				break;
				
			case 7:
			case 10:
				// stanje skladista
				if($l_do_it){
					$l_do_date = $_GET['param1'].' u '.$_GET['param2'].'h';
					$l_period = 'na dan '.$l_do_date;
				}else{
					$l_do_date = '01-01-2000 u 23:59h';
					$l_period = 'na dan '.$l_do_date;
				}
				
				$la0_tmp = explode(' ', $l_do_date);
				$la_tmp = explode('-', $la0_tmp[0]);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				break;
				
			case 12:
			case 13:
				// Kartica Artikla
				if($l_do_it){
					$l_do_date = u_datum(0, 1).'-'.u_mjesec(0, 1).'-'.u_godina();
					$l_artikl = get_item('select name from articles where id = '.$_GET['param1']);
				}else{
					$l_do_date = '01-01-2000';
					$l_artikl = '---';
				}
				
				$la_tmp = explode('-', $l_do_date);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				$l_period = 'za artikl "'.$l_artikl.'", za period do '.$l_do_date;
				break;
				
			case 14:
				// kolicina_materijala_po_stolovima
				$l_do_date = u_datum(0, 1).'-'.u_mjesec(0, 1).'-'.u_godina().' u '.u_sat().':'.u_minuta().'h';
				$l_period = 'na dan '.$l_do_date;
				
				$la0_tmp = explode(' ', $l_do_date);
				$la_tmp = explode('-', $la0_tmp[0]);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				break;
				
			case 15:
				// keub
				if($l_do_it){
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}else{
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}
				
				$la_tmp = explode('-', $l_do_date);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				break;
				
			case 16:
				// Rekapitulacija proizvodnih medjuskladisnica
				if($l_do_it){
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}else{
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}
				
				if($_GET['param3'] == 1){
					$l_add_mail = ' (Prikaz samo KEUB produkata)';
				}
				
				$la_tmp = explode('-', $l_do_date);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
			case 17:
				// Rekapitulacija primki
				if($l_do_it){
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}else{
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}
				
				$la_tmp = explode('-', $l_do_date);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
			case 18:
				// Ispis normativa
				if($l_do_it){
					$l_do_date = '';
					$l_period = '';
				}else{
					$l_do_date = '';
					$l_period = '';
				}
				
				$l_datum_1 = '';
				
			default:
				// sva ostala izvjesca
				if($l_do_it){
					$l_do_date = $_GET['param2'];
					$l_period = 'za period od '.$_GET['param1'].' do '.$l_do_date;
				}else{
					$l_do_date = '01-01-2000';
					$l_period = 'za period od 01-01-2000 do 01-01-2000';
				}
				
				$la_tmp = explode('-', $l_do_date);
				$l_datum_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				break;
		}
		
		if($_GET['receiver'] == 1){
			$l_time_upis = '"'.u_sat().':'.u_minuta().'"';
		}
		if($_GET['receiver'] == 2){
			$l_time_upis = 'null';
		}
		
		$l_sql = 'insert into mailer values(0, '.$_GET['report_id'].', '.$_GET['receiver'].', "'.$l_datum_1.'", '.$l_time_upis.', '.
			' 0, 0, 0, now(), null, null, null, null)';
		$l_rbr = sql_exe($l_sql, true);
		
		if($l_rbr > 0){
			
			//$l_email_sender = get_item('select value from settings where name = "mailer.sender" ');
			$l_email_sender = 'prometi@itr.hr';
			$l_email_name = get_item('select value from settings where name = "mailer.name" ');
			$l_email_owner = get_item('select value from settings where name = "mailer.owner" ');
			$l_email_accounting = get_item('select value from settings where name = "mailer.accounting" ');
			
			if($_GET['receiver'] == 1){
				$l_to = $l_email_owner;
			}
			if($_GET['receiver'] == 2){
				$l_to = $l_email_accounting;
			}
			
			$is_sent = mail_attachment($l_name, $l_path,
				$l_to, $l_email_sender, $l_email_name, $l_email_sender, $l_name.' - '.$l_do_date,
				'Dobar dan,<br><br>u privitku se nalazi izvjestaj "'.$l_name.'" '.$l_period.$l_add_mail.
				'<br><br><font color="red">Ovo je automatska poruka sustava molimo ne odgovarati na ovaj mail.</font>'.
				'<br><br><font color="red">U slucaju potrebe kontaktirajte nas na mobitel.</font>'.
				'<br><br>Srdacan pozdrav,<br><br>'.$l_email_name.'<br><br>');
			
			if($is_sent){
				if($_GET['receiver'] == 1){
					$l_sql = 'update mailer '.
						' set sent_to_owner = 1, email_sent_to_owner = now(), email_owner = "'.$l_email_owner.'" '.
						' where id = '.$l_rbr;
				}
				if($_GET['receiver'] == 2){
					$l_sql = 'update mailer '.
						' set sent_to_accounting = 1, email_sent_to_accounting = now(), email_accounting = "'.$l_email_accounting.'" '.
						' where id = '.$l_rbr;
				}
				sql_exe($l_sql);
				
				$result = 1;
			}
		}
	}
	
	$la_data = array();
	$la_data['result'] = $result;
	
	$la_json = json_encode($la_data);
	print $la_json;
	exit;
?>
