<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
if(!isset($_SESSION['is_login']) ){
header("location: login.php");
?>
<script>
window.location="login.php";
</script>
<?php
}

// $permission  = explode(',',$_SESSION['permission_id']);
// if(!in_array(19,$permission) && !in_array(1,$permission)  ){

// $op['is_success'] = false;
// $op['data'] 	  = "عذرا انت غير مخول للوصل الى هذه الصفحة";
// echo json_encode($op);
// exit;

// }

date_default_timezone_set($_SESSION['SYSTimezone']);


if (isset($_POST['type']) || isset($_GET['type'])){
		require("db_connect.php");

		




		if ($_POST['type']=="get_total_sells") {

			$cond_y = (isset($_POST['yy']) && $_POST['yy'] !='not')? ' AND YEAR(Inv.invoice_date)  	='.$_POST['yy']:'';
			$cond_m = (isset($_POST['mm']) && $_POST['mm'] !='not')? ' AND MONTH(Inv.invoice_date) 	='.$_POST['mm']:'';
			$cond_d = (isset($_POST['dd']) && $_POST['dd'] !='not')? ' AND DAY(Inv.invoice_date) 	='.$_POST['dd']:'';

			$cond   = $cond_y . ' ' . $cond_m . ' ' . $cond_d;


			$res = $db->query("SELECT
					



									sum(invoice_total_net_balance) AS total_price
											
						
								 FROM
								 invoice Inv
								 
								 
							WHERE
								(Inv.is_deleted = 0 AND  Inv.invoice_status <3 AND Inv.invoice_transaction ='Invoice' AND Inv.created_by= ".$_SESSION['user_id']." $cond )"); // all type
			
			if ($res) {				
				$op  = array();
				$row = $res->fetch_assoc();

				$result['total_price']  			= $row['total_price'];
					
				

				array_push($op,$result);

				echo json_encode($op);
				exit;
			}else{
				
				echo 0;
				exit;
			}
		}


		if ($_POST['type']=="get_total_payments") {

			$cond_y = (isset($_POST['yy']) && $_POST['yy'] !='not')? ' AND YEAR(PY.paid_date)  	='.$_POST['yy']:'';
			$cond_m = (isset($_POST['mm']) && $_POST['mm'] !='not')? ' AND MONTH(PY.paid_date) 	='.$_POST['mm']:'';
			$cond_d = (isset($_POST['dd']) && $_POST['dd'] !='not')? ' AND DAY(PY.paid_date) 	='.$_POST['dd']:'';

			$cond   = $cond_y . ' ' . $cond_m . ' ' . $cond_d;


			$res = $db->query("
								SELECT sum(paid_amount) AS total_price  
								FROM payments PY  
								WHERE PY.is_deleted =0 AND PY.payment_transaction ='Payment' AND PY.created_by= ".$_SESSION['user_id']." $cond

							  "); // all type

			$res2 = $db->query("
								SELECT sum(paid_amount) AS total_price  
								FROM payments PY  
								WHERE PY.is_deleted =0 AND PY.payment_transaction ='Return' AND PY.created_by= ".$_SESSION['user_id']." $cond

							  "); // all type
			
			if ($res && $res2) {				
				$op  	= array();
				$row 	= $res->fetch_assoc();
				$row2 	= $res2->fetch_assoc();

				$result['total_price']  			= $row['total_price'] - $row2['total_price'];
					
				

				array_push($op,$result);

				echo json_encode($op);
				exit;
			}else{
				
				echo 0;
				exit;
			}
		}

		
		

		if ($_POST['type']=="get_daily_invoice") {
			
			$reg_date = date("Y-m-d", time());
			// $reg_date = date( '2018-05-01');

			$res = $db->query("SELECT

									invoice_number,
									invoice_date,
									author,
									Inv.actual_discount,
									invoice_total_net_balance,
									customer_id_fk,
									customer_full_name AS name,
									customer_email AS email,
									customer_phone AS phone,
									invoice_transaction,
									Inv.created_at,  
						

									(invoice_total_net_balance) AS final_price,
									St.full_name
											
						
								FROM
								 invoice Inv
								 LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id) 
								 LEFT JOIN staff St ON (Inv.created_by=St.staff_id) 
								 
							WHERE
								(Inv.is_deleted = 0 AND  Inv.invoice_status <3 AND Inv.invoice_date >= '$reg_date' AND Inv.created_by= ".$_SESSION['user_id'].") ORDER BY invoice_id DESC

					 "); // all type
			if ($res) {				
				$op  = array();

				
				while ($row = $res->fetch_assoc()) {				   
						$result['invoice_number']  		= $row['invoice_number'];
						$result['final_price']  		= $row['final_price'];
						$result['actual_discount']  	= $row['actual_discount'];

						$dt 							= new DateTime($row['created_at']);
						$time 							= $dt->format('h:i A');

						$result['time']  				= $time;				
						$result['name']  				= $row['name'];
						$result['email']  				= $row['email'];
						$result['phone']  				= $row['phone'];
						$result['customer_id_fk']  		= $row['customer_id_fk'];
						$result['invoice_transaction']  = $row['invoice_transaction'];					
						$result['full_name']  			= $row['full_name'];					
						$result['author']  			= $row['author'];					
						array_push($op,$result);
				}

				echo json_encode($op);
				exit;
			}else{
				
				echo $db->error;
				exit;
			}
		}



		if ($_POST['type']=="get_daily_payment") {
			
			$reg_date = date("Y-m-d", time());
			// $reg_date = date( '2018-05-01');

			$res = $db->query("SELECT

									payment_number,
									paid_amount,
									payment_note,
									paid_date,
									payment_transaction,
									customer_full_name AS name,
									customer_email AS email,
									customer_phone AS phone,
									customer_id_fk,
									
									PY.created_at,  
						
									St.full_name
											
						
								FROM
								 payments PY
								 LEFT JOIN customer Cus ON (PY.customer_id_fk=Cus.customer_id) 
								 LEFT JOIN staff St ON (Py.created_by=St.staff_id) 
								 
							WHERE
								(PY.is_deleted = 0  AND PY.paid_date >= '$reg_date' AND PY.created_by= ".$_SESSION['user_id'].") ORDER BY payment_id DESC

					 "); // all type
			if ($res) {				
				$op  = array();

				
				while ($row = $res->fetch_assoc()) {				   
						$result['payment_number']  		= $row['payment_number'];
						$result['paid_amount']  		= $row['paid_amount'];
						
						$dt 							= new DateTime($row['created_at']);
						$time 							= $dt->format('h:i A');

						$result['time']  				= $time;				
						$result['name']  				= $row['name'];
						$result['email']  				= $row['email'];
						$result['phone']  				= $row['phone'];
						$result['customer_id_fk']  		= $row['customer_id_fk'];
						$result['payment_transaction']  = $row['payment_transaction'];					
						$result['full_name']  			= $row['full_name'];					
						array_push($op,$result);
				}

				echo json_encode($op);
				exit;
			}else{
				
				echo $db->error;
				exit;
			}
		}


		


		if ($_POST['type']=="get_order") {
			$reg_date = date("Y-m-d", time());
			

			$cond_y = (isset($_POST['yy']) && $_POST['yy'] !='not')? ' AND YEAR(Inv.invoice_date)  	='.$_POST['yy']:'';
			$cond_m = (isset($_POST['mm']) && $_POST['mm'] !='not')? ' AND MONTH(Inv.invoice_date) 	='.$_POST['mm']:'';
			$cond_d = (isset($_POST['dd']) && $_POST['dd'] !='not')? ' AND DAY(Inv.invoice_date) 	='.$_POST['dd']:'';

			$cond   = $cond_y . ' ' . $cond_m . ' ' . $cond_d;

			if(strlen($cond) < 15){

				$limits = 'LIMIT 15';

			}else{
				$limits = '';

			}

			
			$res = $db->query("SELECT

									Inv.invoice_number,
									Inv.invoice_date,
									Inv.delivery_time,
									Inv.delivery_status,
									Inv.author,
									Inv.customer_id_fk,
									customer_full_name AS name,
									customer_email AS email,
									customer_phone AS phone,
									Inv.invoice_transaction,
									Inv.original_invoice_id_fk,
									Inv1.invoice_number AS original_no
									
											
						
								FROM
								 invoice Inv
								 LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id) 
								 LEFT JOIN invoice Inv1 ON (Inv.original_invoice_id_fk=Inv1.invoice_id) 
								 
							WHERE
								(Inv.is_deleted = 0  AND Inv.created_by= ".$_SESSION['user_id']." $cond ) ORDER BY Inv.invoice_id DESC  $limits

					 "); // all type

			
			if ($res) {				
				$op  = array();
	
				while ($row = $res->fetch_assoc()) {				   
					$result['invoice_number'] 			= $row['invoice_number'];
					$result['delivery_time']  			= $row['delivery_time'];
					$result['delivery_status']  		= $row['delivery_status'];
					$result['author']  					= $row['author'];
					$result['name']  					= $row['name'];
					$result['phone']  					= $row['phone'];

					$dt 								= new DateTime($row['invoice_date']);
					$time 								= $dt->format('Y-m-d');

					$result['time']  					= $time;
					$result['invoice_transaction']  	= $row['invoice_transaction'];
					$result['original_invoice_id_fk']  	= $row['original_no'];
													
					array_push($op,$result);
				}

				echo json_encode($op);
				exit;
			}else{
				
				echo $db->error;
				exit;
			}
		}



		


		



		if ($_POST['type']=="get_user_sells") {
			$reg_date  =  date("Y-m-d", time());
			$yesterday = date('Y-m-d',strtotime("-1 days"));

			

			$res = $db->query("SELECT

									
								
									St.username, 
									St.full_name,
									St.profile_img, 
						

									SUM(invoice_total_net_balance) AS total_price,

									(SELECT sum(paid_amount) FROM payments PY WHERE   PY.is_deleted = 0 AND PY.created_by= ".$_SESSION['user_id']." AND PY.payment_transaction ='Payment' AND PY.created_by = Inv.created_by AND PY.paid_date >= '$reg_date') AS total_paid
											
						
								FROM
								 invoice Inv
								 LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id)
								 LEFT JOIN staff St ON (Inv.created_by=St.staff_id) 
								 
							WHERE
								(Inv.is_deleted = 0 AND  Inv.invoice_status <3  AND Inv.created_by= ".$_SESSION['user_id']."  AND Inv.invoice_date >= '$reg_date') GROUP BY Inv.created_by ORDER BY invoice_id DESC

					 "); // all type

			$res1 = $db->query("SELECT

									
								
									St.username, 
									St.full_name,
									St.profile_img, 
						

									SUM(invoice_total_net_balance) AS total_price,

									(SELECT sum(paid_amount) FROM payments PY WHERE   PY.is_deleted = 0 AND PY.payment_transaction ='Payment' AND PY.created_by = Inv.created_by AND PY.paid_date >= '$yesterday' AND PY.paid_date < '$reg_date') AS total_paid
											
						
								FROM
								 invoice Inv
								 LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id)
								 LEFT JOIN staff St ON (Inv.created_by=St.staff_id) 
								 
							WHERE
								(Inv.is_deleted = 0 AND  Inv.invoice_status <3  AND Inv.created_by= ".$_SESSION['user_id']." AND Inv.invoice_date >= '$yesterday' AND Inv.invoice_date < '$reg_date') GROUP BY Inv.created_by ORDER BY invoice_id DESC

					 "); // all type

			
			if ($res) {				
				$op  = array();


				while ($row = $res->fetch_assoc()) {				   
						$result[0]  = $row['total_paid'];
						$result[1]  = $row['total_price'];
						$result[2]  = $row['full_name'];
						$result[3]  = $row['username'];
						$result[4]  = $row['profile_img'];
						$result[5]  = '0';					
						array_push($op,$result);
				}

				while ($row = $res1->fetch_assoc()) {				   
						$result[0]  = $row['total_paid'];
						$result[1]  = $row['total_price'];
						$result[2]  = $row['full_name'];
						$result[3]  = $row['username'];
						$result[4]  = $row['profile_img'];
						$result[5]  = '1';					
						array_push($op,$result);
				}

				echo json_encode($op);
				exit;
			}else{
				
				echo $db->error;
				exit;
			}
		}





	}
	else{
		header("Location:../");
		exit;
	}
?>
