<?
	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/';
	
	include $g_path."app/webroot/iso/_all_ulazni.php";
	
	switch($_GET['report_id']){
		case 0:
			$l_name = 'primka.pdf';
			$l_dokument = 'Primka';
			$l_stupac = 'broj_primka';
			break;
			
		case 1:
			$l_name = 'otpis.pdf';
			$l_dokument = 'Otpis';
			$l_stupac = 'broj_otpis';
			break;
			
		case 2:
			$l_name = 'povratnica.pdf';
			$l_dokument = 'Povratnica';
			$l_stupac = 'broj_povratnica';
			break;
			
		case 3:
			$l_name = 'medjuskladisnica.pdf';
			$l_dokument = 'Medjuskladisnica';
			$l_stupac = 'broj_medjuskladisnica';
			break;
			
		case 4:
			$l_name = 'inventura.pdf';
			$l_dokument = 'Inventura';
			$l_stupac = 'broj_inventura';
			break;
			
		case 1001:
			$l_name = 'medjuskladisnica-proizvodnja.pdf';
			$l_dokument = 'Medjuskladisnica proizvodnja';
			$l_stupac = 'broj';
			break;
	}
	
	
	if(isset($_GET['report_id']) && isset($l_name) && isset($_GET['receiver']) && $_GET['receiver'] > 0){
		
		$a_param = array();
		
		$a_param['destination'] = 'email';
		
		$a_param['restName']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.name" ', 0);
		$a_param['restpost']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.post" ', 0);
		$a_param['restComm']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.resort" ', 0);
		$a_param['restStreet']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.street" ', 0);
		$a_param['restStrNum']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.str_num" ', 0);
		$a_param['restStrNumMark']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.str_num_mark" ', 0);
		$a_param['restPlaceid']['Setting'] = get_data('select id, name, value, description, type from settings where name = "general.placeid" ', 0);
		
		$a_param['rec']['Receiving'] = get_data('select * from receivings where id = '.$_GET['rbr'], 0);
		
		if(isset($a_param['rec']['Receiving']['supplier_id'])){
			$a_param['rec']['Supplier'] = get_data('select * from suppliers where id = '.$a_param['rec']['Receiving']['supplier_id'], 0);
		}
		
		$x = get_data('select * from receiving_items where receiving_id = '.$a_param['rec']['Receiving']['id'], 1);
		$y = array();
		$l_cnt = 0;
		foreach($x as $key => $value){
			foreach($value as $key2 => $value2){
				$y[$key2][$key] = $value2;
			}
		}
		$a_param['rec']['ReceivingItem'] = $y;
		
		foreach($y as $key => $value){
			$a_param['items'][$key]['ReceivingItem'] = $value;
			$a_param['items'][$key]['Receiving'] = $a_param['rec']['Receiving'];
			
			$z = get_data('select * from articles where id = '.$value['article_id'], 0);
			$a_param['items'][$key]['Article'] = $z;
			$a_param['items'][$key]['Article']['formated_display'] = $z['barcode'].' '.$z['display'];
		}
		
		
		$l_do_it = mailer_check_date();
		
		switch($_GET['report_id']){
			case 0:
				if($l_do_it){
					all_print_receiving($a_param, $_GET['rbr']);
				}else{
					all_print_receiving($a_param, 0);
				}
				break;
				
			case 1:
				if($l_do_it){
					all_print_writeOff($a_param, $_GET['rbr']);
				}else{
					all_print_writeOff($a_param, 0);
				}
				break;
				
			case 2:
				if($l_do_it){
					all_print_returnee($a_param, $_GET['rbr']);
				}else{
					all_print_returnee($a_param, 0);
				}
				break;
				
			case 3:
				if($l_do_it){
					all_print_transfer_note($a_param, $_GET['rbr']);
				}else{
					all_print_transfer_note($a_param, 0);
				}
				break;
				
			case 4:
				if($l_do_it){
					all_print_inventura($a_param, $_GET['rbr']);
				}else{
					all_print_inventura($a_param, 0);
				}
				break;
				
			case 1001:
				if($l_do_it){
					all_print_medjus($a_param, $_GET['rbr']);
				}else{
					all_print_medjus($a_param, 0);
				}
				break;
		}
		
		//$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;
		}
		
		switch($_GET['report_id']){
			case 1001:
				$l_rbr = get_item('select '.$l_stupac.' from medjus where id = '.$_GET['rbr']);
				break;
				
			default:
				$l_rbr = get_item('select '.$l_stupac.' from receivings where id = '.$_GET['rbr']);
		}
		
		$is_sent = mail_attachment($l_name, $l_path,
			$l_to, $l_email_sender, $l_email_name, $l_email_sender, $l_dokument.' ('.$l_rbr.')',
			'Dobar dan,<br><br>u privitku se nalazi '.$l_dokument.' br. '.$l_rbr.
			'<br><br>Srdacan pozdrav,<br><br>'.$l_email_name.'<br><br>');
		
		if($is_sent){
			$result = 1;
		}
	}
	
	$la_data = array();
	$la_data['result'] = $result;
	
	$la_json = json_encode($la_data);
	print $la_json;
	exit;
?>
