<?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( isset($_GET['type']) && ($_GET['type'] == "load")){
			// DB table to use
			// show All products for specific category
			// $cond1 = isset($_GET['id'])? ' AND CW.withdraw_by='.$_GET['id']:'';
			$cond = isset($_GET['id'])? ' AND PN.payment_gived_to_id_fk='.$_GET['id']:'';
			
			// $cond = $cond1 . '  ' . $cond2;
			
			$table  =  '(
				SELECT
					payment_notify_id,
					call_date,
					expect_pay_amount,
					pay_usd_rate,
					pay_currency,
					expect_pay_date,
					is_paid,
					real_paid_amount,
					paid_date,
					pay_note,
					customer_id_fk,
					
					customer_full_name,
					customer_phone,
					TIMESTAMPDIFF(DAY, expect_pay_date, NOW()) AS remain_day
					
				FROM
					payment_notify PN
					
					LEFT JOIN customer C ON (PN.customer_id_fk = C.customer_id )
					
				WHERE
					( PN.is_deleted = 0 '.$cond.') 
			) AS ALLTBL';
			// Table's primary key
			//echo $table;
			$primaryKey = 'payment_notify_id';
			$columns = array(
					    array( 'db' => 'payment_notify_id', 'dt' => 0 ),

					    array( 'db' => 'customer_full_name', 'dt' => 1 ),
					    array( 'db' => 'customer_phone', 'dt' => 2 ),
					    array( 'db' => 'call_date',   'dt' => 3 , 'formatter'=> function($d){
											return date('Y-m-d',strtotime($d));
											 } ),
					    array( 'db' => 'expect_pay_amount', 'dt' => 4 ),
					    array( 'db' => 'expect_pay_date',   'dt' => 5 , 'formatter'=> function($d){
											return date('Y-m-d',strtotime($d));
											 } ),
					    array( 'db' => 'paid_date',   'dt' => 6  ),
					    array( 'db' => 'is_paid', 'dt' => 7 ),
					    array( 'db' => 'pay_note', 'dt' => 8 ),
					    array( 'db' => 'remain_day', 'dt' => 9 ),
					    array( 'db' => 'customer_id_fk', 'dt' => 10 ),
			    
			   
			);
			// SQL server connection information
			$sql_details = array(
			    'user' => $mysql_user,
			    'pass' => $mysql_password,
			    'db'   => $mysql_db,
			    'host' => $mysql_server
			);
			require( 'ssp.php' );
			echo json_encode(
			    SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns ,null, '' )
			);
			exit;
		}

		

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

			// $user_id = $_POST['obj_id'];
			$op  = array('is_success'=>false, 'data' => array());

			$res = $db->query("SELECT * FROM staff WHERE is_deleted=0 ");
			if ($res) {
				
				$op['is_success'] = true;
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result['staff_id'] = $row['staff_id'];
						$result['full_name'] = $row['full_name'];
						array_push($op['data'],$result);
				}

				echo json_encode($op);
				exit;
			}else{
				$op['is_success'] = false;
				$op['data'] 	  	= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
				echo json_encode($op);
				exit;
			}
		}

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

			// $user_id = $_POST['obj_id'];
			$op  = array('is_success'=>false, 'data' => array());

			$res = $db->query("SELECT * FROM customer WHERE is_deleted=0 AND customer_type =0");
			if ($res) {
				
				$op['is_success'] = true;
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result['customer_id'] 			= $row['customer_id'];
						$result['customer_full_name'] 	= $row['customer_full_name'];
						array_push($op['data'],$result);
				}

				echo json_encode($op);
				exit;
			}else{
				$op['is_success'] = false;
				$op['data'] 	  	= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
				echo json_encode($op);
				exit;
			}
		}

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

			$id=addslashes($_POST["obj_id"]);

			$reg_date = date("Y-m-d h:i:s", time());
			$lastaction = "Make it paid";

			$op  = array('is_success'=>false, 'data' => '');

			

			

			

	  		$res = $db->query("UPDATE payment_notify SET is_paid = 1 ,last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE payment_notify_id=$id");
	    	if ($res){
	        $op['is_success'] = true;
					$op['data'] 	  = "تمت العملية بنجاح";
					echo json_encode($op);
					exit;
	     	}else{
	      	$op['is_success'] = false;
					$op['data'] 	  = "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
					echo json_encode($op);
					exit;
	     	}
		}

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

			$id 						= $_POST['obj_id'];
			$customer_id_fk			=addslashes($_POST["customer_id_fk"]);
			$call_date				=addslashes($_POST["call_date"]);
			$expect_pay_amount		=addslashes($_POST["expect_pay_amount"]);
			$expect_pay_date		=addslashes($_POST["expect_pay_date"]);
			$pay_note				=addslashes($_POST["pay_note"]);
				
			$withdraw_currency			=1;
			$staff_customer				=0; // 0:customer; 1: staff
			
			$op  = array('is_success'=>false, 'data' => '');

					


			$lastaction = "Update Payment Notification";

			$update_qry = "UPDATE payment_notify SET  customer_id_fk 	='$customer_id_fk',
													  call_date		 	='$call_date',
													  expect_pay_amount	='$expect_pay_amount',
													  expect_pay_date	='$expect_pay_date' ,
													  pay_note			='$pay_note',
													  last_action 		='$lastaction',
													  last_action_by	='".$_SESSION['user_id']."'
								WHERE payment_notify_id = $id";


			$res = $db->query($update_qry);
			if ($res) {
					$op['is_success'] = true;
					$op['data'] 	 	  = "تمت عملية التحديث بنجاح";
					echo json_encode($op);
					exit;
			}else{
				$op['is_success'] = false;
				$op['data'] 	  	= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
				echo json_encode($op);
				exit;
			}
		}


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

			$id=addslashes($_POST["obj_id"]);

			$reg_date = date("Y-m-d h:i:s", time());
			$lastaction = "Delete Payment Notification";

			$op  = array('is_success'=>false, 'data' => '');

			

			

			

	  		$res = $db->query("UPDATE payment_notify SET is_deleted = 1 ,deleted_at = '$reg_date',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE payment_notify_id=$id");
	    	if ($res){
	        $op['is_success'] = true;
					$op['data'] 	  = "تمت عملية الحذف بنجاح";
					echo json_encode($op);
					exit;
	     	}else{
	      	$op['is_success'] = false;
					$op['data'] 	  = "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
					echo json_encode($op);
					exit;
	     	}
		}

		if ($_POST['type'] == "create") {
	      		
	      		$customer_id_fk			=addslashes($_POST["customer_id_fk"]);
				$call_date				=addslashes($_POST["call_date"]);
				$expect_pay_amount		=addslashes($_POST["expect_pay_amount"]);
				$expect_pay_date		=addslashes($_POST["expect_pay_date"]);
				$pay_note				=addslashes($_POST["pay_note"]);
				
				$withdraw_currency			=1;
				
				$rnd_no						=rand(100000000,1000000000).uniqid();
			
				$delete = 0;
				if($call_date =='' || $call_date == NULL){
					$call_date = date("Y-m-d", time());
				}

				$reg_date = date("Y-m-d h:i:s", time());
				$lastaction = "Add New Payment Notification";

				$sql ="INSERT INTO payment_notify (expect_pay_amount,
											call_date,
											pay_usd_rate,
											pay_currency,
											expect_pay_date,
											pay_note,
											customer_id_fk,
											created_by,
											last_action,
											last_action_by)
									VALUES( '$expect_pay_amount',
											'$call_date',
											'".$_SESSION['USD_TO_IQD']."',
											'$withdraw_currency',
											'$expect_pay_date',
											'$pay_note',
											'$customer_id_fk',
											'".$_SESSION['user_id']."',
											'$lastaction',
											'".$_SESSION['user_id']."')";

				$res = $db->query($sql);

					if ($res) {

							$op['is_success'] = true;
							$op['data'] 	  = "تمت عملية الاضافة بنجاح";
							echo json_encode($op);
							exit;
					}
					else{
								$op['is_success'] = false;
								$op['data'] 	  = "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
								echo json_encode($op);
								exit;
					}
			

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