<?php

// FIND SUMMARY TOTAL

include('r_dbc.php');

$sqlm = "select sum(r.rab_number) as mtotal
		from ccmanager r
		where r.pid = '".$pid."'
		and r.mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)";
$resultm = mysqli_query($conn, $sqlm);		
if (mysqli_num_rows($resultm) > 0) {
      while($rowm = mysqli_fetch_assoc($resultm)) {
		$mtotal 		= $rowm["mtotal"];
		}		
    }
mysqli_close($conn);


// GET TOTAL MONTHS FROM CALENDAR

include('r_dbc.php');

$sqlm = "select count(distinct date_format(calday,'%m-%Y')) as count_months
		from calendar
		where calday >= (select min(rab_date) from ccmanager where pid = '".$pid."')
		and calday <= (select max(rab_date) from ccmanager where pid = '".$pid."')";
$resultm = mysqli_query($conn, $sqlm);		
if (mysqli_num_rows($resultm) > 0) {
      while($rowm = mysqli_fetch_assoc($resultm)) {
		$count_months	= $rowm["count_months"];
		}		
    }
mysqli_close($conn);


$mtotal = 0;
$mtotr = 0;
$mtotr_td = '';
$mcol_img = 0;
$myear_td = '';
$icolor = 0;
$colheight = 250;

if ($count_months <= 3) 
	{ 
		$dateqry1 = "date_format(r.rab_date,'%d-%b-%y') as myear"; 
		$dateqry2 = "date_format(r.rab_date,'%d-%b-%y')"; 
		$title_chart = $title_chart_line1_daily;
		$title_chart1 = $n_day_exps;
		$title_chart2 = $n_day_exps_sum;		
	}
if ($count_months > 3 && $count_months <= 6) 	
	{ 
		$dateqry1 = "concat('".$nweek." ',date_format(r.rab_date,'%u-%y')) as myear"; 
		$dateqry2 = "date_format(r.rab_date,'%u-%y')"; 
		$title_chart = $title_chart_line1_weekly;
		$title_chart1 = $n_week_exps;
		$title_chart2 = $n_week_exps_sum;		
	}
if ($count_months > 6 && $count_months <= 24) 
	{ 
		$dateqry1 = "date_format(r.rab_date,'%b-%y') as myear"; 
		$dateqry2 = "date_format(r.rab_date,'%b-%y')"; 
		$title_chart = $title_chart_line1_monthly;
		$title_chart1 = $n_month_exps;		
		$title_chart2 = $n_month_exps_sum;				
	}
if ($count_months > 24 and $count_months <= 72) 
	{ 
		$dateqry1 = "concat('Q',quarter(r.rab_date),'-',date_format(r.rab_date,'%Y')) as myear"; 
		$dateqry2 = "quarter(r.rab_date),date_format(r.rab_date,'%Y')"; 
		$title_chart = $title_chart_line1_quarter;
		$title_chart1 = $n_quarter_exps;				
		$title_chart2 = $n_quarter_exps_sum;						
	}
	
if ($count_months > 72) 
	{ 
		$dateqry1 = "date_format(r.rab_date,'%Y') as myear"; 
		$dateqry2 = "date_format(r.rab_date,'%Y')"; 
		$title_chart = $title_chart_line1_year;
		$title_chart1 = $n_year_exps;				
		$title_chart2 = $n_year_exps_sum;						
	}	

include('r_dbc.php');

$sql = "select ".$dateqry1.",sum(r.rab_number) as mtot
		from ccmanager r
		where r.pid = '".$pid."'
		and r.mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)
		group by ".$dateqry2."
		order by r.rab_date";

$result = mysqli_query($conn, $sql);


if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		$myear 			= $row["myear"];
		$mtot 			= $row["mtot"];	
		$mtotr			= $mtotr + $mtot;
		$c_mtotr	    = number_format($mtotr, 0, $septh, $sepdc);		
		$c_mtot	    	= number_format($mtot, 0, $septh, $sepdc);		

	$myear_td = $myear_td.",'".$myear."'";		
	$mtotal   = $mtotal.",".$mtot;		
	$mtotr_td = $mtotr_td.",".$mtotr;			

	
		}		
    }

 
mysqli_close($conn);

$myear_td 		= "[".substr($myear_td,1)."]";
$mtotal 		= "[".substr($mtotal,2)."]";
$mtotr_td 		= "[".substr($mtotr_td,1)."]";


?> 

<script>
Chart.defaults.global.defaultFontFamily = "Segoe UI";
Chart.defaults.global.defaultFontSize = 14;
new Chart(document.getElementById("line-chart"), {
  type: 'line',
  data: {
    labels: <?php echo $myear_td ?>,
    datasets: [{ 
        data: <?php echo $mtotal ?>,
        label: "<?php echo $title_chart1 ?>",
        borderColor: "green",
        fill: false
      }, { 
        data: <?php echo $mtotr_td ?>,
        label: "<?php echo $title_chart2 ?>",
        borderColor: "blue",
        fill: false
      }
    ]
  },
  options: {
	  responsive: true,
    title: {
	  display: true,
      text: '<?php echo $desc_manager." ".$title_chart ?>',
	  height:300
    }
  }
});
</script>
</canvas>

