<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_rank();
$active=!empty($_GET['active']) ? intval($_GET['active']) : 1;
$tbPONumber=!empty($_POST['tbPONumber']) ? $_POST['tbPONumber'] : '';
if($act=='ready')
{	
	if($tbPONumber=='')
	{
		$client_list=get_rows('si','tbCompanyID,tbType,tbPrefix,tbNickName','tbType=1','companys','tbNickName');
		$factory_list=get_rows('si','tbCompanyID,tbType,tbNickName','tbType=2','companys','tbNickName');
		$staff_list=get_users('tbCompanyID',1);
		$smarty->assign('active',$active);
		$smarty->assign('pre',$pre);
		$smarty->assign('isshow',$isshow);
		$smarty->assign('client_list',$client_list);
		$smarty->assign('factory_list',$factory_list);
		$smarty->assign('staff_list',$staff_list);
		$smarty->display('project_create.dwt');
	}
	else
	{
		$tbPONumber=!empty($_POST['tbPONumber']) ? $_POST['tbPONumber'] : '';
		$checkone=!empty($_POST['checkone']) ? $_POST['checkone'] : '';
		$tbClientID=!empty($_POST['tbClientID']) ? $_POST['tbClientID'] : '';
		$tbFactoryID=!empty($_POST['tbFactoryID']) ? $_POST['tbFactoryID'] : '';
		$tbFactoryBookNO=!empty($_POST['tbFactoryBookNO']) ? $_POST['tbFactoryBookNO'] : '';
		$Leader=!empty($_POST['Leader']) ? $_POST['Leader'] : '';
		$tbOverWork=!empty($_POST['tbOverWork']) ? $_POST['tbOverWork'] : '';
		$tbTaxi=!empty($_POST['tbTaxi']) ? $_POST['tbTaxi'] : '';
		$tbFinishDate=!empty($_POST['tbFinishDate']) ? $_POST['tbFinishDate'] : '';
		$tbShippingDate=!empty($_POST['tbShippingDate']) ? $_POST['tbShippingDate'] : '';
		$inspect_year=!empty($_POST['inspect_year']) ? $_POST['inspect_year'] : '';
		$inspect_month=!empty($_POST['inspect_month']) ? $_POST['inspect_month'] : '';
		$inspect_day=!empty($_POST['inspect_day']) ? $_POST['inspect_day'] : '1';
		$tbInspectionDate=$inspect_year."-".$inspect_month."-".$inspect_day;
		
		$inspect_h=!empty($_POST['inspect_h']) ? $_POST['inspect_h'] : '';
		$inspect_m=!empty($_POST['inspect_m']) ? $_POST['inspect_m'] : '';
		$tbInspectionTime=$inspect_h.":".$inspect_m.":00";	
		
		$yuLeader=!empty($_POST['yuLeader']) ? $_POST['yuLeader'] : '';
		$i=!empty($_POST['team_i']) ? $_POST['team_i'] : '';
		$k=!empty($_POST['team_k']) ? $_POST['team_k'] : '';
		$tbTeam="";
		$tbCnTeam="";
		for($a=0;$a<=8;$a++)
		{
		$tbTeam.=!empty($_POST["tbTeam_$a"]) ? $_POST["tbTeam_$a"].',' : '';
		$tbCnTeam.=!empty($_POST["tbCnTeam_$a"]) ? $_POST["tbCnTeam_$a"].',' : '';
		}
		$sql_p="UPDATE " .$GLOBALS['prefix']->table('projects'). " SET". " tbCheckone='$checkone',tbFactoryID='$tbFactoryID',tbFinishDate='$tbFinishDate',tbBookingDate='$tbFinishDate',tbShippingDate='$tbShippingDate',tbInspectionDate='$tbInspectionDate',tbInspectionTime='$tbInspectionTime',tbFactoryBookNO='$tbFactoryBookNO',tbPM='$Leader',tbStatus='On Going',tbTeam='" .substr($tbTeam,0,-1). "',tbFrom='" .$_SESSION['tbName']. "',tbATeam ='" .substr($tbCnTeam,0,-1) ."',tbOverWork='$tbOverWork',tbTaxi='$tbTaxi' WHERE tbPONumber='$tbPONumber'";
		$sql_r="UPDATE " .$GLOBALS['prefix']->table('reports'). " SET". " tbClientID='$tbClientID',tbFactoryID='$tbFactoryID',tbInspectionDate='$tbInspectionDate',tbFactoryBookNO='$tbFactoryBookNO',tbWI='1' WHERE tbPONumber='$tbPONumber'";
		 if($GLOBALS['db']->query($sql_p))
			{
				if($GLOBALS['db']->query($sql_r))
				{
					$book=get_rows("","tbPN,tbPONumber,tbReportNumber","`tbPONumber`='".$tbPONumber."' ","reports","tbPN");//序列化报告号码
					$k=1;
					foreach($book as $v)
					{
					$tbReportNumber=substr($v['tbReportNumber'],0,strpos($v['tbReportNumber'],"_")+1).$k;
					$sql="UPDATE " .$GLOBALS['prefix']->table('reports'). " SET". " tbReportNumber='$tbReportNumber' WHERE tbPN='" . $v['tbPN'] . "'";
						if($GLOBALS['db']->query($sql))
						{$k++;}
					}
				ecs_header("Location:projects.php?client=".$client);	//ecs_header("Location:send_mail.php?tbPONumber=$tbPONumber&act=creat_book");
				}
				else
				{
				$GLOBALS['err'] ->add($GLOBALS['_LANG']['rep_update_fail']);
				$GLOBALS['err'] ->show();
				}
			}
			else
			{
			$GLOBALS['err'] ->add($GLOBALS['_LANG']['pro_save_fail']);
			$GLOBALS['err'] ->show();
			}
	}
}
//获取产品名称及料号
elseif($act=='get')
{
	$products=get_products_list();
	echo json_encode($products);
}
//保存单条产品记录
elseif($act=='save')
{
	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/get_pdata.php');
	$begin=$inspect_year."-".$inspect_month."-01";
    $end=$inspect_year."-".$inspect_month."-31";
	$pro_count=get_count('tbClientID',$tbClientID,'projects','tbInspectionDate',$begin,$end);
	$client_short=get_row('si','*','tbCompanyID',$tbClientID,'companys');
	//$s=array($tbShippingQty,$tbSampleSize,$tbInspectionDate);
	//echo json_encode($s);
	if($tbPONumber=='')
	{
		$tbPONumber=$client_short['tbShortName'].substr($inspect_year,-2).$inspect_month.sprintf("%03d",$pro_count+1);
		$tcount=get_count('tbPONumber',$tbPONumber,'reports','','','')+1;
		$re_number="IR-".$tbPONumber."_".$tcount;//这段与下面相同，但需重新载入
		require(str_replace('sicms','',dirname(__FILE__)) . '/includes/in_pdata.php');
		if(get_count('tbPONumber',$tbPONumber,'projects','','','')!=true){	
				if($GLOBALS['db']->query($sql_p)){
				      if($GLOBALS['db']->query($sql_r)){
					     require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_pitem.php');
					     $s=array($tbPONumber,$str);
					     echo json_encode($s);
					  }
				}
		}else{
		$s=array($tbPONumber,"PO Number is Exist,Error!");
		echo json_encode($s);
		}
	}
	else
	{
		$tcount=get_count('tbPONumber',$tbPONumber,'reports','','','')+1;
		$re_number="IR-".$tbPONumber."_".$tcount;//这段与上面相同，但需重新载入
		require(str_replace('sicms','',dirname(__FILE__)) . '/includes/in_pdata.php');
        if(get_count('tbPONumber',$tbPONumber,'projects','','','')!=true)
		{
			$s=array($tbPONumber,"PO Number is Exist,Error!");
			echo json_encode($s);
		}else{
		 	if($GLOBALS['db']->query($sql_r)){
		 	   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_pitem.php');
			   $s=array($tbPONumber,$str);
			   echo json_encode($s);
		 	}
		}
	}
}
//删掉Booking中一条产品记录
elseif($act=='delitem')
{
	$tbPONumber=!empty($_POST['po']) ? $_POST['po'] : '';
	$id=!empty($_POST['id']) ? $_POST['id'] : '';
	$sql_r = "DELETE FROM " . $GLOBALS['prefix']->table('reports') . "WHERE tbPN='" . trim($id) . "' AND tbPONumber='" . trim($tbPONumber) . "'";
	if($GLOBALS['db']->query($sql_r))
	{
	   if(get_count('tbPONumber',trim($tbPONumber),'reports','','','')!=true)
	   {
		   $sql_p = "DELETE FROM " . $GLOBALS['prefix']->table('projects') . "WHERE tbPONumber='" . trim($tbPONumber) . "'";
		   $GLOBALS['db']->query($sql_p);
		   echo "<script language='javascript'>alert('You have deleted all products,this booking have been removed too!')</script>";
		   ecs_header("Location:projects.php?active=1&client=".$client);
	   }
	   else
	   {
    	   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_pitem.php');
		   $s=array($tbPONumber,$str);
		   echo json_encode($s);
	   }
	}
}
//修改BOOK中的某一条产品记录
elseif ($act=='getitem')
{
	$tbPONumber=!empty($_POST['po']) ? $_POST['po'] : '';
	$id=!empty($_POST['id']) ? $_POST['id'] : '';
	$sql_r = "SELECT  tbPN,tbPONumber,tbProductName,tbModelNO,tbClientPONo,tbShippingQty,tbSampleSize,tbSerialNO,tbSpec,tbPiece,tbRemark FROM " . $GLOBALS['prefix']->table('reports') . " WHERE tbPN='" . trim($id) ."' AND  tbPONumber='" . trim($tbPONumber) . "'";
	$rep=$GLOBALS['db']->getRow($sql_r);
	$s=array(trim($rep['tbPN']),trim($rep['tbPONumber']),trim($rep['tbProductName']),trim($rep['tbModelNO']),trim($rep['tbClientPONo']),trim($rep['tbShippingQty']),trim($rep['tbSampleSize']),trim($rep['tbSerialNO']),trim($rep['tbSpec']),trim($rep['tbRemark']));
	echo json_encode($s);
}
//保存BOOK中的某一条产品修改后的结果
elseif ($act=='saveitem')
{
	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/get_pdata.php');
	$sql_r="UPDATE " .$GLOBALS['prefix']->table('reports'). " SET". " tbProductName='$tbProductName',tbModelNO='$tbModelNO',tbClientPONo='$tbClientPONo',tbShippingQty='$tbShippingQty',tbSerialNO='$tbSerialNO',tbRemark='$tbRemark',tbSampleSize='$tbSampleSize',tbUnit='$tbUnit',tbPiece='$tbPiece',tbSpec='$tbSpec' WHERE tbPN='$tbPN' AND tbPONumber='$tbPONumber'";
	if($GLOBALS['db']->query($sql_r)){
	   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_pitem.php');
	   $s=array(trim($tbPONumber),$str);
	   echo json_encode($s);
	}
}
?>