<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_login();
require(ROOT_PATH . 'includes/lib_reports.php');
require(ROOT_PATH . 'includes/lib_instructs.php');
$active=!empty($_GET['active']) ? intval($_GET['active']) : 2;
//var_dump($id);exit;
if(isset($id) && $id!='')
{
	$rep=rep_details($id);
	if($pic=get_row('','*','tbReportNumber',$rep['tbReportNumber'],'pics')){
	$pic['tbPackimg'] = $pic && $pic['tbPackimg'] != '' ? explode(',', $pic['tbPackimg']) : array();
	$pic['tbStyleimg'] = $pic && $pic['tbStyleimg'] != '' ? explode(',', $pic['tbStyleimg']) : array();
	$pic['tbAppearanceimg'] = $pic && $pic['tbAppearanceimg'] != '' ? explode(',', $pic['tbAppearanceimg']) : array();
	$pic['tbTestimg'] = $pic && $pic['tbTestimg'] != '' ? explode(',', $pic['tbTestimg']) : array();}
	$rep['tbMeasure'] = $rep && $rep['tbMeasure'] != '' ? show_color('Software','Cosmetic','blue', $rep['tbMeasure']) : '';
	if(($_SESSION['tbRank']>='50') || ($_SESSION['tbCompanyID']==$rep['tbClientID']) || ($_SESSION['tbCompanyID']==$rep['tbFactoryID']) || ($_SESSION['tbCompanyID']=='2' && $rep['tbClientID']=='15'))
	{
		$wi_summary=get_wi_summary($rep['tbProductName'],$rep['tbWI']);
		$rep_defect=get_rows("","*","`tbReportNumber`='".$rep['tbReportNumber']."' ","find_defects","tbFN");
		$sum_defect=sum_defect($rep['tbReportNumber']);
		/*
		$pic_name[]="";
		foreach($rep_defect as $value)
		{   $path="defection/".$value['tbReportNumber'].$value['tbFN'].".jpg";
			if(ispic_file($path))
			{
			$pic_name[]=$value['tbReportNumber'].$value['tbFN'];
			}
			else
			{
			$pic_name[]="No Picture";
			}
		}*/
		$mail_path=ROOT_PATH .'upload/files/' . $client . '/email/';
		//var_dump($mail_path);exit;
		$mail_file=ismail_file($mail_path,$rep['tbPN'],'.msg');
		$idf=$cp=$strf=$tested="";
		if($rep['tbInspectionDate']>'2023-04-19'){
		$tested=leftjoin('si',"a.*,b.*",'tbTestedID','tbSN','tbReportNo',$rep['tbReportNumber'],'tested','items','a.tbPosition');
		}else{
		$tested=leftjoin('si',"a.*,b.*",'tbTestedID','tbSN','tbReportNo',$rep['tbReportNumber'],'tested','test_items','a.tbTN');
		}
		//var_dump($items);exit;
		if($tested!='')
		{
			   foreach($tested as $value)
			   {
					if(strpos($value['tbTestedID'],","))
					{
						$idf=explode(',',$value['tbTestedID']);
						foreach($idf as $v)
						{
						$cp=get_row('si','tbCheckPoint','tbSN',trim($v),'items');
						$strf .=$cp['tbCheckPoint']."/";
						}
						$strf ="Function testing(" . rtrim($strf,"/") . ")";
					}
				}
		}	
		$serial_count=c_serials("tbReportNumber",$rep['tbReportNumber'],"serial1","serial2");
		$serial_details=get_serials("tbReportNumber",$rep['tbReportNumber'],"serial1","serial2","tbSerialNumber",'');
		$smarty->assign('active',$active); 
		$smarty->assign('pre',$pre);
		$smarty->assign('rep',$rep);
		$smarty->assign('pic',$pic);
		$smarty->assign('wi_summary',$wi_summary); 
		$smarty->assign('rep_defect',$rep_defect); 
		$smarty->assign('sum_defect',$sum_defect); 
		$smarty->assign('tested',$tested);
		$smarty->assign('testedf',$strf);
		$smarty->assign('serial_count',$serial_count); 
		$smarty->assign('serial_details',$serial_details); 
		$smarty->assign('mail_file',$mail_file); 
		$smarty->display('report_page.dwt');
	}else{
	$GLOBALS['err'] ->add($GLOBALS['_LANG']['permission']);
	$GLOBALS['err'] ->show();
	}
}
?>