<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_login();
require(ROOT_PATH . 'includes/lib_projects.php');
$active=!empty($_GET['active']) ? intval($_GET['active']) : 1;
//var_dump($id);exit;
if($act=='show')
{
	if(isset($id) && $id!='')
	{
		$pro=get_summary($id);
		if(($_SESSION['tbRank']>='50') || ($_SESSION['tbCompanyID']==$pro['tbClientID']) || ($_SESSION['tbCompanyID']==$pro['tbFactoryID']) || ($_SESSION['tbCompanyID']=='2' && $pro['tbClientID']=='15'))
		{
		$smarty->assign('active',$active);
		$smarty->assign('pre',$pre);
		$rep_summary=get_rows('',"tbPN,tbPONumber,tbProductName,tbModelNO,tbClientPONo,tbShippingQty,tbSerialNO,tbRemark,tbCTNQty,tbSampleSize","`tbPONumber`='".$pro['tbPONumber']."' ","reports","tbPN");
		$smarty->assign('pro_summary',$pro);
		$smarty->assign('confirmedDate',date('Y-m-d',strtotime($pro['tbInspectionDate'])-60*60*24));
		$smarty->assign('rep_summary',$rep_summary); 
		$smarty->display('project_page.dwt');
		}
		else
		{
		$GLOBALS['err'] ->add($GLOBALS['_LANG']['permission']);
		$GLOBALS['err'] ->show();
		}
	}
}
elseif($act=='edit')
{
  check_rank();
  if(isset($id) && $id!='')
  {
    $client_list=get_rows('si','tbCompanyID,tbType,tbNickName','tbType=1','companys','tbNickName');
	$factory_list=get_rows('si','tbCompanyID,tbType,tbNickName','tbType=2','companys','tbNickName');
	$staff_list=get_users('tbCompanyID',1);
	$pro=get_summary($id);
	if ($pro['tbStatus']=='Finished') exit('This Project Has Finished,Access Denied!');
	$rep_summary=get_rows('',"tbPN,tbPONumber,tbProductName,tbModelNO,tbClientPONo,tbShippingQty,tbSerialNO,tbRemark,tbCTNQty,tbSampleSize,tbSpec,tbPiece","`tbPONumber`='".$pro['tbPONumber']."' ","reports","tbPN");
	$teamen=explode(',',$pro['tbTeam']);
	$teamcn=explode(',',$pro['tbATeam']);
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('teamen',$teamen);
	$smarty->assign('teamcn',$teamcn);
	$smarty->assign('client_list',$client_list);
	$smarty->assign('factory_list',$factory_list);
	$smarty->assign('staff_list',$staff_list);
	$smarty->assign('pro_summary',$pro);
	$smarty->assign('rep_summary',$rep_summary);
	$smarty->assign('confirmedDate',date('Y-m-d',strtotime($pro['tbInspectionDate'])-60*60*24));
	$smarty->display('project_edit.dwt');
  }
}
elseif($act=='save')
{
	check_rank();//这一段本来想与project_create.php共用但保存不成功
    $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'] : '';
	$tbBookingDate=!empty($_POST['tbBookingDate']) ? $_POST['tbBookingDate'] : '';
	$tbInspectionDate=!empty($_POST['tbInspectionDate']) ? $_POST['tbInspectionDate'] : '';
	$tbInspectionTime=!empty($_POST['tbInspectionTime']) ? $_POST['tbInspectionTime'] : '';	
	
	$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='$tbBookingDate',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',tbRevisedCount=tbRevisedCount+1 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=edit_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();
		}
}
?>