<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_rank();
$active=!empty($_GET['active']) ? intval($_GET['active']) : 1;
if ($act=='ready')
{
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('act',$act);
	$smarty->display('send_mail.dwt');
}
elseif($act=='creat_book' || $act=='edit_book')
{
    $tbPONumber=!empty($_GET['tbPONumber']) ? $_GET['tbPONumber'] : '';
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('act',$act);
	$smarty->assign('tbNumber',$tbPONumber);
	$smarty->display('send_mail.dwt');
}
elseif($act=='release_rep' || $act=='chgresult')
{
    $id=!empty($_GET['id']) ? $_GET['id'] : '';
 if($act=='release_rep'){
	$sql="UPDATE " .$GLOBALS['prefix']->table('reports'). " SET tbVerifier='" .$_SESSION['tbName']. "',tbVerifyDate='". date('Y-m-d'). "',tbReport='3' WHERE tbPN='$id'";
	if($GLOBALS['db']->query($sql)){}
	else
	{
	   $GLOBALS['err'] ->add($GLOBALS['_LANG']['rep_update_fail']);
	   $GLOBALS['err'] ->show();
	}
  }
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('act',$act);
	$smarty->assign('tbNumber',$id);
	$smarty->display('send_mail.dwt');
}
elseif($act=='send_book' || $act=='revise_book')
{
	$tbPONumber=!empty($_POST['xid']) ? $_POST['xid'] : '';
	$book_pury=get_book_pury($tbPONumber);
	$tcount=!empty($book_pury['tbTeam']) ? substr_count($book_pury['tbTeam'],",")+2 :1;
	$book_rury=get_rows('',"tbPN,tbPONumber,tbReportNumber,tbProductName,tbClientID,tbFactoryID,tbModelNO,tbClientPONo,tbShippingQty,tbSerialNO,tbRemark,tbCTNQty,tbSampleSize","`tbPONumber`='".$tbPONumber."' ","reports","tbPN");
	$ProductName=$tShippingQty='';
	$c=$k=0;
	$c=count($book_rury);
		foreach($book_rury as $value)
		{
		$k++;
		if($k<$c){
		$ProductName.=$value['tbProductName'] . ",";
		$tShippingQty.=$value['tbShippingQty'] . ",";
		}
		else{
		$ProductName.=$value['tbProductName'];
		$tShippingQty.=$value['tbShippingQty'];}
		}
	 $mail_list=get_mail_list('All');
	 /*发新Book时用这段*/
	 if($act=='send_book'){
		 $subject = "Pure Inspection Booking Notification,".$tbPONumber;
		 $content=file_get_contents(ROOT_PATH .'data/mail_booking.php');
	 }
	 /*修改Book时用这段*/
	 if($act=='revise_book'){
		 $subject = "Pure Inspection Booking Notification,".$tbPONumber.",Revised ".$book_pury["tbRevisedCount"];
		 $content=file_get_contents(ROOT_PATH .'data/mail_rbooking.php');
		 $content=str_replace("{tbRevisedCount}",$book_pury["tbRevisedCount"],$content);
	 }
	 $content=str_replace("{uri}",APPURI,$content);
	 $content=str_replace("{tbCheckone}",$book_pury["tbCheckone"],$content);
	 $content=str_replace("{tbCompanyName}",$book_pury["tbCompanyName"],$content);
	 $content=str_replace("{client}",$book_pury["client"],$content);
	 $content=str_replace("{tbBookingDate}",$book_pury["tbBookingDate"],$content);
	 $content=str_replace("{tbInspectionDate}",$book_pury["tbInspectionDate"],$content);
	 $content=str_replace("{tbFactoryBookNO}",$book_pury["tbFactoryBookNO"],$content);
	 $content=str_replace("{tbPONumber}",$book_pury["tbPONumber"],$content);
	 $content=str_replace("{ProductName}",$ProductName,$content);
	 $content=str_replace("{tShippingQty}",$tShippingQty,$content);
	 $content=str_replace("{tcount}",$tcount,$content);
	 $content=str_replace("{tbPM}",$book_pury["tbPM"],$content);
	 $content=str_replace("{tbTeam}",$book_pury["tbTeam"],$content);
	 $content=str_replace("{tbSN}",$book_pury["tbSN"],$content);
	 $content=str_replace("{tbFrom}",$book_pury["tbFrom"],$content);
		foreach($mail_list as $value)
		{	
			//$content=str_replace("{tbName}",$value["tbName"],$content);
			$content=dear_name("Dear "," :</td></tr>",$value["tbName"],$content);
			//echo htmlentities($data);
			if(send_mail($value["tbName"], $value["tbMail"], $subject, $content, $type = 1, $notification=false))
			{
			  echo '<li style="list-style:none;">'.$value["tbMail"] . '........Send Ok!</li>';
			}
			else
			{
			  echo '<li style="list-style:none;">'.$value["tbMail"] . '........Send Fail!</li>';
			}
		}
	  echo"<div id='send_mail'>
	  <input name='GoBack' type='button' value='Go Back To Project Page' onclick=\"location.href='projects.php'\" />
	  </div>";
}
elseif($act=='send_report' || $act=='send_revise')
{
	 $id=!empty($_POST['xid']) ? $_POST['xid'] : '';
	 require_once(ROOT_PATH . 'includes/lib_reports.php');
	 //require_once(ROOT_PATH . 'includes/lib_defects.php');
	 $rep=rep_details($id);
	 $sum_defect=sum_defect($rep['tbReportNumber']);
	 if($rep["tbResult"]=="Accepted"){
	  $Result="<font color='#0000ff'><b>Accepted</b></font>";
	  $ResultDetail='';
	}else if($rep["tbResult"]=="Rejected"){
	  $Result="<font color='#ff0000'><b>Rejected</b></font>";
	  $ResultDetail='';
	}else if($rep["tbResult"]="Concessed"){
	  $Result="<font color='green'><b>Concessed</b></font>";
	  $ResultDetail="<b>Customer Instruction : </b><font color='#ff0000'><b>Conditional Approval by ".$rep['client']."</b></font><br><br>Customer must be fully noticed that an instruction of Conditional Approval was made for this ".$rep['tbCheckone'].", and must provide a further instruction if any part of the Inspection Criteria is required to revise before the next Inspection Booking of the same model of product. Otherwise, CY will continuously apply the same criteria for the next inspection.";
	 }
	 //准备备注的数据格式
	 $mark='';
	 if($rep['tbSupplement']!=''){
	 $remark=explode("\n",$rep['tbSupplement']);
	 foreach($remark as $k=>$v){
		   	 $mark.= $k+1 .": ".$v."<br>";
		}
	 }	
	 $mail_list=get_mail_list(1,$rep["tbClientID"],$rep["tbFactoryID"]);
	 if($act=='send_report'){
		 $var='';
		 $f='mail_report.php';
	 }
	 elseif($act=='send_revise'){
		 $var='Concessed ';
		 $f='mail_rreport.php';
	 }
	 $subject = $rep["tbInspectionDate"] ." ". $rep["tbProductName"] ." [". $rep["tbResult"] . "], Chunyin Inspection Report " . $var . " Notification";
	 $CR=!empty($sum_defect['CR']) ? $sum_defect['CR'] : '0';
	 $MA=!empty($sum_defect['MA']) ? $sum_defect['MA'] : '0';
	 $MI=!empty($sum_defect['MI']) ? $sum_defect['MI'] : '0';
	 $defects =" CR: <font color='#0000ff'>".$CR."</font>, MA: <font color='#0000ff'>".$MA."</font>, MI: <font color='#0000ff'>".$MI."</font>";
	 $content=file_get_contents(ROOT_PATH .'data/'.$f);
	 $content=str_replace("{uri}",APPURI,$content);
	 $content=str_replace("{client}",$client,$content);
	 $content=str_replace("{tbCheckone}",$rep["tbCheckone"],$content);
	 $content=str_replace("{tbFactory}",$rep["tbCompanyName"],$content);
	 $content=str_replace("{tbInspectionDate}",$rep["tbInspectionDate"],$content);
	 $content=str_replace("{tbCompany}",$rep["client"],$content);
	 $content=str_replace("{tbBookingDate}",$rep["tbBookingDate"],$content);
	 $content=str_replace("{tbInspectionDate}",$rep["tbInspectionDate"],$content);
	 $content=str_replace("{tbProductName}",$rep["tbProductName"],$content);
	 $content=str_replace("{tbLotQty}",$rep["tbLotQty"],$content);
	 $content=str_replace("{tbFactoryBookNO}",$rep["tbFactoryBookNO"],$content);
	 $content=str_replace("{tbPONumber}",$rep["tbPONumber"],$content);
	 $content=str_replace("{tbReportNumber}",$rep["tbReportNumber"],$content);
	 $content=str_replace("{tbSupplement}",$mark,$content);
	 $content=str_replace("{Result}",$Result,$content);
	 $content=str_replace("{Defects}",$defects,$content);
	 //Concess报告加一项
	 if($act=='send_revise'){
	 $content=str_replace("{tbIssue}",less($rep["tbIssue"]),$content);
	 $content=str_replace("{tbModifyReason}",less($rep["tbModifyReason"]),$content);
	 }
	 $content=str_replace("{ResultDetail}",$ResultDetail,$content);
	 $content=str_replace("{tbVerifier}",$rep["tbVerifier"],$content);
	 $content=str_replace("{tbPN}",$rep["tbPN"],$content);
	 $str='';
		foreach($mail_list as $value)
		{	
			$con=''; //此处必须新启用一个变量，不然邮件底部releasy by处的名字会和dear name处相同。
			$con=dear_name("Dear "," :</td></tr>",$value["tbName"],$content);
			if(send_mail($value["tbName"], $value["tbMail"], $subject, $con, $type = 1, $notification=false))
			{
			  $str.= '<li style="list-style:none;text-align:right;">'.$value["tbMail"] . '........Send Ok!</li>';
			}else{
			  $str.= '<li style="list-style:none;color:#FF0000;">'.$value["tbMail"] . '........Send Fail!</li>';
			}
			sleep(1);
		}
	   $str.="<div id='send_mail'><input name='GoBack' type='button' value='Go Back To Report Page' onclick=\"location.href='reports.php'\" /></div>";
	   echo json_encode($str);
}

?>