<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_login();
require(ROOT_PATH . 'includes/lib_statistics.php');
$active=!empty($_GET['active']) ? intval($_GET['active']) : 4;
$tbFactoryID=!empty($_REQUEST['tbFactoryID']) ? $_REQUEST['tbFactoryID'] : '';
$pro_select=!empty($_REQUEST['pro_select']) ? $_REQUEST['pro_select'] : '';
$start_year=!empty($_REQUEST['start_year']) ? intval($_REQUEST['start_year']) : date('Y');
$start_month=!empty($_REQUEST['start_month']) ? intval($_REQUEST['start_month']) : date('m');
$start_day=!empty($_REQUEST['start_day']) ? intval($_REQUEST['start_day']) : '1';
	if($start=!empty($_REQUEST['start'])){
	   $start=$_REQUEST['start'];
	}else{
	   $start=$start_year.'-'.$start_month.'-'.$start_day;
	}
$end_year=!empty($_REQUEST['end_year']) ? intval($_REQUEST['end_year']) : date('Y');
$end_month=!empty($_REQUEST['end_month']) ? intval($_REQUEST['end_month']) : date('m');
$end_day=!empty($_REQUEST['end_day']) ? intval($_REQUEST['end_day']) : '31';
    if($end=!empty($_REQUEST['end'])){
	   $end=$_REQUEST['end'];
	}else{
	   $end=$end_year.'-'.$end_month.'-'.$end_day;
	}
	
$topic=!empty($_REQUEST['topic']) ? $_REQUEST['topic'] : '';
$products_list=get_products_list();
$factory_list=get_rows("si","tbCompanyID,tbType,tbNickName,tbCooperation","`tbType`=2 and `tbCooperation` like '%".$pre."%' ","companys","tbNickName");
	$smarty->assign('act',$act);
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('tbFactoryID',$tbFactoryID);
	$smarty->assign('pro_select',$pro_select);
	$smarty->assign('start_year',$start_year);
	$smarty->assign('start_month',$start_month);
	$smarty->assign('start_day',$start_day);
	$smarty->assign('start',$start);
	$smarty->assign('end_year',$end_year);
	$smarty->assign('end_month',$end_month);
	$smarty->assign('end_day',$end_day);
	$smarty->assign('end',$end);
	$smarty->assign('pro_select',$pro_select);
	$smarty->assign('products_list',$products_list);
	$smarty->assign('factory_list',$factory_list);
//if($act=='QP')为了统一代码特改成ready
if($act=='ready' || $act=='ready')
{
	//按工厂分组得到总报告数量，和出货数量
    $list_qp=get_qp($tbFactoryID,$pro_select,$start,$end);
    //按工厂分组得到Final验货的出货数量
	$ship_qty=get_ship_qty($tbFactoryID,$pro_select,$start,$end);
	$ac_re_sc[]='';
	$first_list[]='';
	$first_arc[]='';
	$re_list[]='';
	$re_arc[]='';
		foreach($list_qp as $value)
		{
            //按工厂分组得到总的ARC报告数
		$ac_re_sc[$value['tbFactoryID']]=get_arc_count($value['tbFactoryID'],$start,$end,$pro_select);
            //按工厂分组得到Final验货的报告数和出货数量
		$first_list[$value['tbFactoryID']]=get_first_list($value['tbFactoryID'],$start,$end,$pro_select);
            //按工厂分组得到Final验货的ARC报告数量
		$first_arc[$value['tbFactoryID']]=get_first_arc($value['tbFactoryID'],$start,$end,$pro_select);
            //按工厂分组得非Final验货的报告数和出货数量
		$re_list[$value['tbFactoryID']]=get_re_list($value['tbFactoryID'],$start,$end,$pro_select);
            //按工厂分组得到非Final验货的ARC报告数量
		$re_arc[$value['tbFactoryID']]=get_re_arc($value['tbFactoryID'],$start,$end,$pro_select);
		}
	$smarty->assign('list_qp',$list_qp);
	$smarty->assign('ship_qty',$ship_qty);
	$smarty->assign('ac_re_sc',$ac_re_sc);
	$smarty->assign('first_list',$first_list);
	$smarty->assign('first_arc',$first_arc);
	$smarty->assign('re_list',$re_list);
	$smarty->assign('re_arc',$re_arc);
	$smarty->display('statistics.dwt');
}
elseif($act=='QD')
{
	$list_qd=get_qd($tbFactoryID,$pro_select,$start,$end);
	$smarty->assign('list_qd',$list_qd);
    $smarty->display('statistics_qd.dwt');
}
elseif($act=='TD')
{
	$top=get_top($tbFactoryID,$pro_select,$start,$end);
	$smarty->assign('top',$top);
    $smarty->display('statistics_top.dwt');
}
elseif($act=='top_details')
{
	$topd=get_topd($start,$end,$pro_select,$topic);
	$smarty->assign('topd',$topd);
    $smarty->display('statistics_topd.dwt');
}
?>