<?php 



include('db_coni.php');



$cur_date = date('Y-m-d');
$third_month_fdate = date('Y-m-01', strtotime('-1 months'));
  $third_month_name = date('Y-M', strtotime('-1 months'));
 
//Last date of current month.
$third_month_tdate = date("Y-m-t", strtotime($third_month_fdate));
          
$fourth_month_fdate = date('Y-m-01');
$fourth_month_name = date('Y-M');
  
 
//Last date of current month.
$fourth_month_tdate = date("Y-m-t", strtotime($fourth_month_fdate));





$query = "SELECT m.dated, sum(d.qty*d.rate) sale,sum(d.qty*d.cost) cost, sum(d.qty*d.rate) - sum(d.qty*d.cost) profit from sale_detail d, sale m where m.s_id=d.s_id and m.dated BETWEEN '".$third_month_fdate."' and '".$third_month_tdate."' group by dated";



$result = mysqli_query($connecti, $query);



$last_month_data = '';



while($row = mysqli_fetch_array($result))



{



 $last_month_data .= "{ dated:'".$row["dated"]."', sale:".$row["sale"].", cost:".$row["cost"].", profit:".$row["profit"]."}, ";



}



$last_month_data = substr($last_month_data, 0, -2);



$query = "SELECT m.dated, sum(d.qty*d.rate) sale,sum(d.qty*d.cost) cost, sum(d.qty*d.rate) - sum(d.qty*d.cost) profit from sale_detail d, sale m where m.s_id=d.s_id and m.dated BETWEEN '".$fourth_month_fdate."' and '".$fourth_month_tdate."' group by dated";



$result = mysqli_query($connecti, $query);



$this_month_data = '';



while($row = mysqli_fetch_array($result))



{



 $this_month_data .= "{ dated:'".$row["dated"]."', sale:".$row["sale"].", cost:".$row["cost"].", profit:".$row["profit"]."}, ";



}



$this_month_data = substr($this_month_data, 0, -2);



?>