<?php
require_once ("varables.php");

$myJSON['factures'] = array();
$myJSON['success']="کمبود منابع...!";

if(isset($_POST["nekot"]) && isset($_POST["an1d"]) 
	&& isset($_POST["typ3"]) && isset($_POST["az"]) 
	&& isset($_POST["ta"]) && isset($_POST["VID"]))
{
	
    $type = $_POST["typ3"];
    $from = $_POST["az"];
	$to = $_POST["ta"];
	$V_CODE = $_POST["VID"];
	$ff = jalali_to_gregorian(substr( $from,0,4),substr( $from,5,2),substr( $from,8,2));
	$tt = jalali_to_gregorian(substr( $to,0,4),substr( $to,5,2),substr( $to,8,2));
	$from = $ff[0];
	if($ff[1]<10)
	$from .= "/0".$ff[1];
	else
	$from .= "/".$ff[1];
	if($ff[2]<10)
	$from .= "/0".$ff[2];
	else
	$from .= "/".$ff[2];

	$to = $tt[0];
	if($tt[1]<10)
	$to .= "/0".$tt[1];
	else
	$to .= "/".$tt[1];
	if($tt[2]<10)
	$to .= "/0".$tt[2];
	else
	$to .= "/".$tt[2];

	$from .=" 12:00:00 ق.ظ";
	$to .=" 12:59:00 ق.ظ";
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8mb4;");
			$query_all = "SELECT ".$tbl_factor.".Fac_Date,".$tbl_factor.".Fac_Time,".$tbl_factor.".DateUser,".$tbl_factor.".TimeUser,".$tbl_factor.".Porsant,".$tbl_factor.".Sum_Price,".$tbl_factor.".Fac_Code,".$tbl_factor.".Fac_Code_C,".$tbl_factor.".Takhfif, ".$tbl_customer.".C_Name, ".$tbl_customer.".C_Code,".$tbl_customer.".C_acc FROM ".$tbl_factor.", ".$tbl_customer." WHERE ".$tbl_factor.".C_Code = ".$tbl_customer.".C_id AND ".$tbl_factor.".Vasete = '".$V_CODE."' AND (".$tbl_factor.".Fac_Date BETWEEN '".$from."' AND '".$to."')  AND ".$tbl_factor.".Fac_Type = 'F' ORDER BY ".$tbl_factor.".Fac_Date,".$tbl_factor.".Fac_Code_C";
			$allres = @mysqli_query($connect,$query_all);
			if($allres){
			 while($row = @mysqli_fetch_assoc($allres))
				array_push($myJSON["factures"], $row);
				
				$myJSON['success']="200";

					
				$qv  = "SELECT V_acc FROM ".$tbl_visitors." WHERE V_id = '".$V_CODE."' LIMIT 1";
					$vres = @mysqli_query($connect,$qv);
				if($vres){
				while($acc = @mysqli_fetch_assoc($vres))
					$myJSON['success'].="&!".$acc["V_acc"];
				}
				else{
					$myJSON['success'].="&!0";
				}
			}
			else{
				$myJSON['success']=@mysqli_error($connect);
			}
		
	}
	 else
		 $myJSON['success']="خطای ارتباط با پایگاه داده...!";
}
echo json_encode($myJSON);
?>