Skip to content
Snippets Groups Projects
get_entity_process_delay.php 9.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_request.php");
    require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_security.php");
    require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_manage_status.php");
    require_once("apps".DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_list_show.php");
    require_once('modules'.DIRECTORY_SEPARATOR.'reports'.DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_graphics.php");
    
    require_once("modules/entities/entities_tables.php");
    
    $_ENV['date_pattern'] = "/^[0-3][0-9]-[0-1][0-9]-[1-2][0-9][0-9][0-9]$/";
    
    
    $graph = new graphics();
    $req = new request();
    $sec = new security();
    
    $db = new Database();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    //var_dump($_POST['entities_chosen']);
    $entities_chosen = explode("#", $_POST['entities_chosen']);
    $entities_chosen = "'" . join("','", $entities_chosen) . "'";
    
    $status_chosen = '';
    $where_status = '';
    
    if (!empty($_POST['status_chosen'])) {
        $status_chosen = explode("#", $_POST['status_chosen']);
        $status_chosen = "'" . join("','", $status_chosen) . "'";
        $where_status = ' AND status in (' . $status_chosen . ') ';
    }
    
    $priority_chosen = '';
    $where_priority = '';
    if (!empty($_POST['priority_chosen']) || $_POST['priority_chosen'] === "0") {
        $priority_chosen = explode("#", $_POST['priority_chosen']);
        $priority_chosen = "'" . join("','", $priority_chosen) . "'";
        $where_priority = ' AND priority in (' . $priority_chosen . ') ';
    }
    
    SNA's avatar
    SNA committed
    
    
    $period_type = $_REQUEST['period_type'];
    $status_obj = new manage_status();
    $ind_coll = $sec->get_ind_collection('letterbox_coll');
    $table = $_SESSION['collections'][$ind_coll]['table'];
    $view = $_SESSION['collections'][$ind_coll]['view'];
    $search_status = $status_obj->get_searchable_status();
    $default_year = date('Y');
    $report_type = $_REQUEST['report_type'];
    $core_tools = new core_tools();
    $core_tools->load_lang();
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    //Récupération de l'ensemble des types de documents
    if (!$_REQUEST['entities_chosen']){
        $stmt = $db->query("select entity_id, short_label from ".ENT_ENTITIES." where enabled = 'Y' order by short_label");
    }else{
        $stmt = $db->query("select entity_id, short_label from ".ENT_ENTITIES." where enabled = 'Y' and entity_id IN (".$entities_chosen.") order by short_label",array());
    }
    
    $entities = array();
    while($res = $stmt->fetchObject())
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        array_push($entities, array('ID' => $res->entity_id, 'LABEL' => $res->short_label));
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    	if(empty($_REQUEST['the_year']) || !isset($_REQUEST['the_year']))
    	{
    		?>
    		<div class="error"><?php echo _YEAR.' '._MISSING;?></div>
    		<?php
    		exit();
    	}
    	if(	!preg_match('/^[1-2](0|9)[0-9][0-9]$/', $_REQUEST['the_year']))
    	{
    		?>
    		<div class="error"><?php echo _YEAR.' '._WRONG_FORMAT;?></div>
    		<?php
    		exit();
    	}
    	$where_date = " and ".$req->extract_date('creation_date', 'year')." = '".$_REQUEST['the_year']."'";
    	$date_title = _FOR_YEAR.' '.$_REQUEST['the_year'];
    
    else if($period_type == 'period_month')
    {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    	$arr_month = array('01','02','03','04','05','06','07','08','09','10','11','12');
    	if(empty($_REQUEST['the_month']) || !isset($_REQUEST['the_month']))
    	{
    		?>
    		<div class="error"><?php echo _MONTH.' '._MISSING;?></div>
    		<?php
    		exit();
    	}
    	if(	!in_array($_REQUEST['the_month'], $arr_month))
    	{
    		?>
    		<div class="error"><?php echo _MONTH.' '._WRONG_FORMAT;?></div>
    		<?php
    		exit();
    	}
    	$where_date = " and ".$req->extract_date('creation_date', 'year')." = '".$default_year."' and ".$req->extract_date('creation_date', 'month')." = '".$_REQUEST['the_month']."'";
    	$month = '';
    	switch($_REQUEST['the_month'])
    	{
    		case '01':
    			$month = _JANUARY;
    			break;
    		case '02':
    			$month = _FEBRUARY;
    			break;
    		case '03':
    			$month = _MARCH;
    			break;
    		case '04':
    			$month = _APRIL;
    			break;
    		case '05':
    			$month = _MAY;
    			break;
    		case '06':
    			$month = _JUNE;
    			break;
    		case '07':
    			$month = _JULY;
    			break;
    		case '08':
    			$month = _AUGUST;
    			break;
    		case '09':
    			$month = _SEPTEMBER;
    			break;
    		case '10':
    			$month = _OCTOBER;
    			break;
    		case '11':
    			$month = _NOVEMBER;
    		case '12':
    			$month = _DECEMBER;
    			break;
    		default:
    			$month = '';
    	}
    	$date_title = _FOR_MONTH.' '.$month;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    else if($period_type == 'custom_period')
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    	if (empty($_REQUEST['date_start']) && empty($_REQUEST['date_fin'])){
    		echo '<div class="error">'._DATE.' '._IS_EMPTY.''.$_REQUEST['date_start'].'</div>';
    		exit();
    	}
    	
    	
    	if( preg_match($_ENV['date_pattern'],$_REQUEST['date_start'])==false  && $_REQUEST['date_start'] <> ''  )
    	{
    		
    		echo '<div class="error">'._WRONG_DATE_FORMAT.' : '.$_REQUEST['date_start'].'</div>';
    		exit();
    	
    	}
    	if( preg_match($_ENV['date_pattern'],$_REQUEST['date_fin'])==false && $_REQUEST['date_fin'] <> '' )
    	{
    		
    		echo '<div class="error">'._WRONG_DATE_FORMAT.' : '.$_REQUEST['date_fin'].'</div>';
    		exit();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    	if(isset($_REQUEST['date_start']) && $_REQUEST['date_start'] <> '')
    	{
    		$where_date  .= " AND ".$req->extract_date('creation_date')." > '".$db->format_date_db($_REQUEST['date_start'])."'";
    		$date_title .= strtolower(_SINCE).' '.$_REQUEST['date_start'].' ';
    	}
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    	if(isset($_REQUEST['date_fin']) && $_REQUEST['date_fin'] <> '')
    	{
    		$where_date  .= " AND ".$req->extract_date('creation_date')." < '".$db->format_date_db($_REQUEST['date_fin'])."'";
    		$date_title.= strtolower(_FOR).' '.$_REQUEST['date_fin'].' ';
    	}
    	if(empty($where_date))
    	{
    		$where_date = $req->extract_date('creation_date', 'year')." = '".$default_year."'";
    		$date_title = _FOR_YEAR.' '.$default_year;
    	}
    
    SNA's avatar
    SNA committed
    }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    else
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    	?>
    	<div class="error"><?php echo _PERIOD.' '._MISSING;?></div>
    	<?php
    	exit();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    //$title = _ENTITY_PROCESS_DELAY.' '.$date_title ;
    $db = new Database();
    
    
        $val_an = array();
        $_SESSION['labels1'] = array();
    
        $data = array();
    
    }
    $has_data = false;
    
    //Utilisation de la clause de sécurité de Maarch
    
    $where_clause = $sec->get_where_clause_from_coll_id('letterbox_coll');
    //var_dump($where_clause);
    if ($where_clause)
    
        $where_clause = " and ".$where_clause;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    $totalEntities = count($entities);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    for($i=0; $i<count($entities);$i++)
    
        //Permet d'afficher ou non les entités dont le nombre de courrier est égal à 0
    	$valid = true;
    
        if ($_SESSION['user']['entities']){
            foreach($_SESSION['user']['entities'] as $user_ent){
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                if ($entities[$i]['ID'] == $user_ent['ENTITY_ID']){
    
                    $valid = true;
                }
            }
        }
        if ($valid == 'true' || $_SESSION['user']['UserId'] == "superadmin")
        {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            $stmt = $db->query("SELECT ".$req->get_date_diff('closing_date', 'creation_date' )." AS delay, creation_date FROM ".$view
                . " WHERE destination = ? AND status not in ('DEL','BAD') AND closing_date IS NOT NULL".$where_date." ".$where_status." ".$where_priority,array($entities[$i]['ID']));
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            
    
            if( $stmt->rowCount() > 0)
    
                while($res = $stmt->fetchObject())
    
                    if ($res->delay <> "") {
                        $tmp = $tmp + $res->delay;
                        $nbDoc++;
                    }
                    
    
                if ($nbDoc == 0) $nbDoc = 1;
    
                if($report_type == 'graph')
                {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    array_push($val_an, (string)round($tmp / $nbDoc,0));
    
                }
                elseif($report_type == 'array')
                {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    array_push($data, array('LABEL' => $db->show_string($entities[$i]['LABEL']), 'VALUE' => (string)round($tmp / $nbDoc,0)));
    
                if($tmp / $nbDoc > 0)
    
                {
                    $has_data = true;
                }
            }
            else
            {
                if($report_type == 'graph')
                {
                    array_push($val_an, 0);
                }
                elseif($report_type == 'array')
                {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    array_push($data, array('LABEL' => $db->show_string($entities[$i]['LABEL']), 'VALUE' => _UNDEFINED));
    
                }
            }
            if($report_type == 'graph')
            {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                array_push($_SESSION['labels1'], addslashes($db->show_string($entities[$i]['LABEL'])));
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        $largeur=50*$totalEntities;
        if ($totalEntities<20){
    
    SNA's avatar
    SNA committed
        $src1 = $_SESSION['config']['businessappurl']."index.php?display=true&module=reports&page=graphs&type=histo&largeur=$largeur&hauteur=600&marge_bas=300&title=".$title."&labelY="._N_DAYS;
    
        for($i=0;$i<count($_SESSION['labels1']);$i++)
        {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            //$src1 .= "&labels[]=".$_SESSION['labels1'][$i];
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        $_SESSION['GRAPH']['VALUES']='';
    
        for($i=0;$i<count($val_an);$i++)
        {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            //$src1 .= "&values[]=".$val_an[$i];
            $_SESSION['GRAPH']['VALUES'][$i]=$val_an[$i];
    
        array_unshift($data, array('LABEL' => _DOCTYPE, 'VALUE' => _PROCESS_DELAY));
    
        if($report_type == 'graph')
        {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            $labels1 = "'".implode("','", $_SESSION['labels1'])."'";
            echo "{label: [".$labels1."] ".
    
                ", data: ['".utf8_encode(str_replace(",", "','", addslashes(implode(",", $_SESSION['GRAPH']['VALUES']))))."']".
                ", title: '".addslashes($title)."'}";
            exit;
    
         }
        elseif($report_type == 'array')
        {
    
    		$data2 = urlencode(json_encode($data));
    
    		$form =	"<input type='button' class='button' value='Exporter les données' onclick='record_data(\"" . $_SESSION['config']['businessappurl']."index.php?display=true&dir=reports&page=record_data \",\"".$data2."\")' style='float:right;'/>";
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    		echo $form;
    		
    
            $graph->show_stats_array($title, $data);
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        $error = _NO_DATA_MESSAGE;
        echo "{status : 2, error_txt : '".addslashes(functions::xssafe($error))."'}";