<?php
require_once ("varables.php");
$myJSON['message'] = "4004";
$myJSON['days'] = array();

if(isset($_POST["nekot"]) && isset($_POST["an1d"]))
{
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8;");
        $date = jdate('Y/m/d');
        $mdays = 31;
        $date1 = remMounths($date,1);
        $date3 = addNumbers($date1,$mdays,12);
        if($date == $date3)
          $mdays = 31;
        else 
          $mdays = 30;
			$query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
			$allres = @mysqli_query($connect,$query_all);
			if($allres){
        $countins = 0;
        $price = 0;
			 while($row = @mysqli_fetch_assoc($allres)){
          $countins++;
          $price = $price + $row["endprice"];
       }
          $a = array();
          $a["date"] = "امروز";
          $a["orders"] = $countins;
          $a["prices"] = $price;
          array_push($myJSON["days"], $a);
          $myJSON['message']="200";
       }
        else{
          $myJSON['message']="4004.";
        }
        $date = addNumbers($date1,$mdays-7,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = "هفته پیش";
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

        $date = addNumbers($date1,$mdays-1,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = "دیروز";
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

          $date = addNumbers($date1,$mdays-2,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = "پری روز";
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

          $date = addNumbers($date1,$mdays-3,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = $date;
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

          $date = addNumbers($date1,$mdays-4,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = $date;
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

          $date = addNumbers($date1,$mdays-5,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = $date;
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

          $date = addNumbers($date1,$mdays-6,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = $date;
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }
         
          $date = addNumbers($date1,$mdays-7,12);
        $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
          $price = 0;
          while($row = @mysqli_fetch_assoc($allres)){
            $countins++;
            $price = $price + $row["endprice"];
         }
            $a = array();
            $a["date"] = $date;
            $a["orders"] = $countins;
            $a["prices"] = $price;
            array_push($myJSON["days"], $a);
            $myJSON['message']="200";
         }
          else{
            $myJSON['message']="4004.";
          }

          $date = addNumbers($date1,$mdays-8,12);
          $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
          $allres = @mysqli_query($connect,$query_all);
          if($allres){
            $countins = 0;
            $price = 0;
            while($row = @mysqli_fetch_assoc($allres)){
              $countins++;
              $price = $price + $row["endprice"];
           }
              $a = array();
              $a["date"] = $date;
              $a["orders"] = $countins;
              $a["prices"] = $price;
              array_push($myJSON["days"], $a);
              $myJSON['message']="200";
           }
            else{
              $myJSON['message']="4004.";
            }
            $date = addNumbers($date1,$mdays-9,12);
            $query_all = "SELECT * FROM ".$tbl_orders." WHERE DATE LIKE '".$date."%' AND type !='K'";
            $allres = @mysqli_query($connect,$query_all);
            if($allres){
              $countins = 0;
              $price = 0;
              while($row = @mysqli_fetch_assoc($allres)){
                $countins++;
                $price = $price + $row["endprice"];
             }
                $a = array();
                $a["date"] = $date;
                $a["orders"] = $countins;
                $a["prices"] = $price;
                array_push($myJSON["days"], $a);
                $myJSON['message']="200";
             }
              else{
                $myJSON['message']="4004.";
              }
	}
	 else
		 $myJSON['message']="400";

}
echo json_encode($myJSON);
?>
