<?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']);

require_once 'vendor/autoload.php';

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
			$cond = isset($_GET['id'])? ' AND Pr.product_id_fk='.$_GET['id']:'';
			$table  =  '(
				SELECT
					purchase_id,
					purchase_number,
					purchase_auto_number,
					purchase_date,
					purchase_note,
					P.stock_id_fk,
					purchase_item_id,
					purchase_quantity,
					buy_price_per_meter,
					item_length,
					stock_name,
					supplier_full_name,
					sum(buy_price_per_meter * item_length) AS total_price
					
					 FROM
					 	 purchase P
					 	 LEFT JOIN stock S ON (S.stock_id = P.stock_id_fk)
					 	 LEFT JOIN suppliers SU ON (P.supplier_id_fk = SU.supplier_id)
					 	 , purchase_item PP, purchase_item_detail PID 
					WHERE 
					(PP.purchase_id_fk = P.purchase_id AND P.is_deleted = 0 AND PP.is_deleted = 0 AND PID.purchase_item_id_fk = PP.purchase_item_id AND PID.is_deleted = 0 AND PID.is_part=0  '.$cond.' ) GROUP BY PP.purchase_id_fk
				
			) AS ALLTBL';
			// Table's primary key
			//echo $table;
			$primaryKey = 'purchase_id';
			$columns = array(
			    array( 'db' => 'purchase_id', 'dt' => 0 ),

			    array( 'db' => 'purchase_auto_number', 'dt' => 1 ),
			    array( 'db' => 'purchase_number', 'dt' => 2 ),
			    
			    array( 'db' => 'purchase_date', 'dt' => 3, 'formatter'=> function($d){
							return date('Y-m-d',strtotime($d));
					} ),
			    array( 'db' => 'stock_name', 'dt' => 4 ),
			    array( 'db' => 'total_price', 'dt' => 5 ),
				array( 'db' => 'supplier_full_name', 'dt' => 6 ),
				array( 'db' => 'purchase_note', 'dt' => 7 ),

			);
			// 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") {

			$user_id = $_POST['cusid'];

			$res = $db->query("SELECT * FROM users WHERE user_id = $user_id");
			if ($res) {
				$row = $res->fetch_assoc();
				echo json_encode($row);
				exit;
			}else{
				echo 0;
				exit;
			}
		}


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

			$id = $_POST['id'];


			$res = $db->query("SELECT purchase_id, 
									  purchase_number, 
									  purchase_auto_number, 
									  purchase_date, 
									  purchase_note, 
									  P.stock_id_fk,
									  supplier_id_fk,
									  purchase_item_id, 
									  purchase_quantity, 
									  buy_price_per_meter, 
									  purchase_usd_rate, 
									  purchase_currency, 
									  purchase_item_note, 
									  PI.purchase_id_fk,
									  PI.product_id_fk, 
									  erbil_code AS product_name,
									  stock_name,
									  turkish_code,
									  supplier_full_name,									
																	  
									(SELECT sum(buy_price_per_meter * item_length) FROM purchase_item_detail PID WHERE PID.purchase_id_fk = P.purchase_id AND PID.purchase_item_id_fk=PI.purchase_item_id  AND PID.is_deleted = 0 AND PID.is_part=0) AS total_row_price,

								  	(SELECT sum(buy_price_per_meter * item_length) FROM purchase_item_detail PID, purchase_item PI 
									          			
										WHERE PID.purchase_id_fk= P.purchase_id  AND PI.purchase_id_fk = P.purchase_id AND PI.is_deleted=0 AND  PID.is_deleted=0 AND PI.purchase_item_id = PID.purchase_item_id_fk AND PID.is_part=0) AS total_purchase_price,

									(SELECT sum(item_length) FROM purchase_item_detail PID WHERE PID.purchase_id_fk = P.purchase_id AND PID.purchase_item_id_fk=PI.purchase_item_id  AND PID.is_deleted = 0 AND PID.is_part=0) AS total_length,

									(SELECT COUNT(item_length) FROM purchase_item_detail PID WHERE PID.purchase_id_fk = P.purchase_id AND PID.purchase_item_id_fk=PI.purchase_item_id  AND PID.is_deleted = 0 AND PID.is_part=0) AS total_quantity

										FROM purchase P
										LEFT JOIN purchase_item PI ON (PI.purchase_id_fk = P.purchase_id AND PI.is_deleted=0)
										LEFT JOIN product Pr ON (PI.product_id_fk = Pr.product_id )
									    LEFT JOIN stock S ON (P.stock_id_fk = S.stock_id )
									    LEFT JOIN suppliers SU ON (P.supplier_id_fk = SU.supplier_id )
										WHERE P.purchase_id='$id'  AND P.is_deleted = 0");
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array('purchase'=>array(),'details'=>array());
				while ($row = $res->fetch_assoc()) {
				   
					$result['purchase_id']  			= $row['purchase_id'];
					$result['purchase_number']  		= $row['purchase_number'];
					$result['purchase_date']  			= date('Y-m-d',strtotime($row['purchase_date'])) ;
					$result['purchase_auto_number']  	= $row['purchase_auto_number'];
					$result['purchase_note']  			= $row['purchase_note'];
					$result['stock_id_fk']  			= $row['stock_id_fk'];
					$result['purchase_item_id'] 		= $row['purchase_item_id'];
					$result['purchase_quantity']  		= $row['purchase_quantity'];
					$result['buy_price_per_meter']  	= $row['buy_price_per_meter'];
					$result['purchase_usd_rate']  		= $row['purchase_usd_rate'];
					$result['purchase_currency']  		= $row['purchase_currency'];
					$result['purchase_item_note']  		= $row['purchase_item_note'];
					$result['product_id_fk']  			= $row['product_id_fk'];
					$result['product_name']  			= $row['product_name'];
					$result['stock_name']  				= $row['stock_name'];
					$result['total_row_price']  		= $row['total_row_price'];
					$result['total_purchase_price']  	= $row['total_purchase_price'];
					$result['total_length']  			= $row['total_length'];
					$result['total_quantity']  			= $row['total_quantity'];
					$result['turkish_code']  			= $row['turkish_code'];
					$result['supplier_full_name']  		= $row['supplier_full_name'];
					
					array_push($op['purchase'],$result);


					$res1 = $db->query("SELECT purchase_item_detail_id, 
												item_length, 
												item_status, 
												sold_length, 
											    damage_length, 
											    used_length,
											    purchase_id_fk,
											    purchase_item_id_fk,
											    stock_id_fk
													  
										FROM purchase_item_detail PID
									 	WHERE PID.purchase_id_fk='$id' AND PID.purchase_item_id_fk='$row[purchase_item_id]' AND  PID.is_deleted = 0 AND PID.is_part=0");
						
						while ($row1 = $res1->fetch_assoc()) {
								$result1['purchase_item_detail_id']  	= $row1['purchase_item_detail_id'];
								$result1['item_length'] 				= $row1['item_length'];
								$result1['item_status']  				= $row1['item_status'] ;
								$result1['sold_length']  				= $row1['sold_length'];
								$result1['damage_length'] 				= $row1['damage_length'];
								$result1['used_length']  				= $row1['used_length'];
								$result1['purchase_id_fk']  			= $row1['purchase_id_fk'];
								$result1['purchase_item_id_fk']  		= $row1['purchase_item_id_fk'];
								$result1['stock_id_fk']  				= $row1['stock_id_fk'];
								
								array_push($op['details'],$result1);
						}
						
				}		
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}


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

			$id = $_POST['id'];


			$res = $db->query("SELECT purchase_id, 
									  purchase_number, 
									  purchase_auto_number, 
									  purchase_date, 
									  purchase_note, 
									  P.stock_id_fk,
									  purchase_item_id, 
									  purchase_quantity, 
									  buy_price_per_meter, 
									  purchase_usd_rate, 
									  purchase_currency, 
									  purchase_item_note, 
									  PI.purchase_id_fk,
									  PI.product_id_fk, 
									  erbil_code AS product_name,
									  stock_name,
									  turkish_code,									
																	  
									(SELECT sum(buy_price_per_meter * item_length) FROM purchase_item_detail PID WHERE PID.purchase_id_fk = P.purchase_id AND PID.purchase_item_id_fk=PI.purchase_item_id  AND PID.is_deleted = 0) AS total_row_price,

								  	(SELECT sum(buy_price_per_meter * item_length) FROM purchase_item_detail PID, purchase_item PI 
									          			
										WHERE PID.purchase_id_fk= P.purchase_id  AND PI.purchase_id_fk = P.purchase_id AND PI.is_deleted=0 AND  PID.is_deleted=0 AND PI.purchase_item_id = PID.purchase_item_id_fk ) AS total_purchase_price,

									(SELECT sum(item_length) FROM purchase_item_detail PID WHERE PID.purchase_id_fk = P.purchase_id AND PID.purchase_item_id_fk=PI.purchase_item_id  AND PID.is_deleted = 0) AS total_length,

									(SELECT COUNT(item_length) FROM purchase_item_detail PID WHERE PID.purchase_id_fk = P.purchase_id AND PID.purchase_item_id_fk=PI.purchase_item_id  AND PID.is_deleted = 0) AS total_quantity

										FROM purchase P
										LEFT JOIN purchase_item PI ON (PI.purchase_id_fk = P.purchase_id AND PI.is_deleted=0)
										LEFT JOIN product Pr ON (PI.product_id_fk = Pr.product_id )
									    LEFT JOIN stock S ON (P.stock_id_fk = S.stock_id )
										WHERE P.purchase_id='$id'  AND P.is_deleted = 0");
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array('purchase'=>array(),'details'=>array());
				while ($row = $res->fetch_assoc()) {
				   
					$result['purchase_id']  			= $row['purchase_id'];
					$result['purchase_number']  		= $row['purchase_number'];
					$result['purchase_date']  			= date('Y-m-d',strtotime($row['purchase_date'])) ;
					$result['purchase_auto_number']  	= $row['purchase_auto_number'];
					$result['purchase_note']  			= $row['purchase_note'];
					$result['stock_id_fk']  			= $row['stock_id_fk'];
					$result['purchase_item_id'] 		= $row['purchase_item_id'];
					$result['purchase_quantity']  		= $row['purchase_quantity'];
					$result['buy_price_per_meter']  	= $row['buy_price_per_meter'];
					$result['purchase_usd_rate']  		= $row['purchase_usd_rate'];
					$result['purchase_currency']  		= $row['purchase_currency'];
					$result['purchase_item_note']  		= $row['purchase_item_note'];
					$result['product_id_fk']  			= $row['product_id_fk'];
					$result['product_name']  			= $row['product_name'];
					$result['stock_name']  				= $row['stock_name'];
					$result['total_row_price']  		= $row['total_row_price'];
					$result['total_purchase_price']  	= $row['total_purchase_price'];
					$result['total_length']  			= $row['total_length'];
					$result['total_quantity']  			= $row['total_quantity'];
					$result['turkish_code']  			= $row['turkish_code'];
					
						array_push($op['purchase'],$result);


						$res1 = $db->query("SELECT purchase_item_detail_id, 
													item_length, 
													item_status, 
													sold_length, 
												    damage_length, 
												    used_length,
												    purchase_id_fk,
												    purchase_item_id_fk,
												    stock_id_fk
														  
											FROM purchase_item_detail PID
										 	WHERE PID.purchase_id_fk='$id' AND PID.purchase_item_id_fk='$row[purchase_item_id]' AND  PID.is_deleted = 0");
						
						while ($row1 = $res1->fetch_assoc()) {
								$result1['purchase_item_detail_id']  	= $row1['purchase_item_detail_id'];
								$result1['item_length'] 				= $row1['item_length'];
								$result1['item_status']  				= $row1['item_status'] ;
								$result1['sold_length']  				= $row1['sold_length'];
								$result1['damage_length'] 				= $row1['damage_length'];
								$result1['used_length']  				= $row1['used_length'];
								$result1['purchase_id_fk']  			= $row1['purchase_id_fk'];
								$result1['purchase_item_id_fk']  		= $row1['purchase_item_id_fk'];
								$result1['stock_id_fk']  				= $row1['stock_id_fk'];
								
								array_push($op['details'],$result1);
						}
						
				}		
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}

		if ($_POST['type']=="PDF" || $_POST['type']=="Print") {

				$invoice_html = $_POST['html'];
				$inv_no		  	= $_POST['inv_no'];
				$pay    	  	= $_POST['pay'];

				if($pay != NULL || $pay !=''){ // print Payments
						$mpdf = new mPDF('utf-8','A4',0,'sans-serif',15,15,12,12, 0, 0, 'P');
						$mpdf->autoScriptToLang = true;
						$mpdf->autoLangToFont = true;
						$mpdf->allow_charset_conversion = false;

						$mpdf->WriteHTML($invoice_html);
						$inv_dir = 'purchases/'.$inv_no;
						if(! file_exists('../'.$inv_dir)){
							mkdir('../'.$inv_dir);
						}
						$invoice_url = $inv_dir.'/'.$inv_no.'_Purchase.pdf';
						$invoice_real_url = '../'.$invoice_url;
						$mpdf->Output($invoice_real_url,'F');
				}else{
						$mpdf = new mPDF('utf-8','A4',0,'sans-serif',15,15,12,12, 0, 0, 'P');
						$mpdf->WriteHTML($invoice_html);
						$inv_dir = 'purchases/'.$inv_no;
						if(! file_exists('../'.$inv_dir)){
							mkdir('../'.$inv_dir);
						}
						$invoice_url = $inv_dir.'/'.$inv_no.'.pdf';
						$invoice_real_url = '../'.$invoice_url;
						$mpdf->Output($invoice_real_url,'F');
				}



					$out['is_success'] = true;
					$out['data'] 	   = $invoice_url;
				echo json_encode($out);

		}

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

			$id 										= $_POST['obj_id'];
			$quantity								= addslashes($_POST["quantity"]);
			$product_id							= addslashes($_POST["product_id"]);
			$buy_price							= addslashes($_POST["buy_price"]);
			$created_at							= addslashes($_POST["created_at"]);
			$sell_price							= addslashes($_POST["sell_price"]);
			$sell_price_percentage	= addslashes($_POST["sell_price_percentage"]);
			$average_price					= addslashes($_POST["average_price"]);
			$note										= addslashes($_POST["note"]);

			$lastaction = "Update Purchase";
			$db->autocommit(FALSE); // to rollback update if update fail
			$update_qry = "UPDATE purchase SET  purchase_quantity = '$quantity', buy_price='$buy_price',product_id_fk='$product_id',
										purchase_date='$created_at', purchase_note = '$note',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE purchase_id = $id";

			$res = $db->query($update_qry);
			if ($res) {
						// update Product table for sell price and avarage price
						$update_qry = "UPDATE product SET  sell_price='$sell_price',sell_price_percentage='$sell_price_percentage',
						average_price='$average_price', last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE product_id = $product_id";

						$res = $db->query($update_qry);
						if ($res) {
							$db->commit();
							echo 1;
						}else{
							$db->rollback();
							echo $db->error;
							echo 0;
						}

			}else{
				$db->rollback();
				echo $db->error;
				echo 0;
			}
		}


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

			$id	 = addslashes($_POST["obj_id"]);
			$op  = array('is_success'=>false, 'data' => '');

			
			// check if remained quantity greater than current purchase quantity then it can be deleted
			$res = $db->query("SELECT * from purchase_item_detail where purchase_id_fk = $id AND is_deleted=0 AND item_status !=0 AND item_status !=2");

			if (mysqli_num_rows($res)>0) {
			
				$op['is_success'] = false;
				$op['data'] 	  = "خطأ لا يمكن حذف قائمة الشراء لوجود مواد فيها قد استخدمت او بيعت (تغيرت حالة بعض المواد)";
				echo json_encode($op);
				exit;
			}

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


			$db->autocommit(FALSE); // to rollback update if update fail
			$res = $db->query("UPDATE purchase SET is_deleted = 1 ,deleted_at = '$reg_date',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE purchase_id=$id");

			if ($res){

				$update_qry = "UPDATE purchase_item SET  is_deleted = 1, deleted_at='$reg_date', last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE purchase_id_fk = $id";

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

				if ($res) {

					$update_qry1 = "UPDATE purchase_item_detail SET  is_deleted = 1, deleted_at='$reg_date', last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE purchase_id_fk = $id";

					$res1 = $db->query($update_qry1);

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

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





		}

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

            			$purchase_number					=addslashes($_POST["purchase_number"]);
						$purchase_date						=addslashes($_POST["purchase_date"]);
						$stock_id_fk						=addslashes($_POST["stock_id_fk"]);
						$purchase_note						=addslashes($_POST["purchase_note"]);
						$supplier_id_fk						=addslashes($_POST["supplier_id_fk"]);

						$purchase_quantity					=$_POST["purchase_quantity"];
						$buy_price_per_meter				=$_POST["buy_price_per_meter"];
						$purchase_item_note					=$_POST["purchase_item_note"];
						$product_id_fk						=$_POST["product_id_fk"];
						$item_length						=$_POST["item_length"];
						$expenses_currency					= 1; //USD

						if($purchase_date == null || $purchase_date ==''){
							$purchase_date = date("Y-m-d h:i:s", time());
						}

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

						$res = $db->query("SELECT * FROM purchase ORDER BY purchase_id DESC LIMIT 1");
						$row = $res->fetch_assoc();
						$last_purchase_id 	= $row['purchase_id'];

						$dd = date("d-m-y", time());
						$purchase_auto_number = "PUR-".$dd."-".($last_purchase_id +1);

						// get stock type to set item_status according to stock type

						$res = $db->query("SELECT * FROM stock where stock_id='$stock_id_fk'");
						$row = $res->fetch_assoc();
						$stock_type 	= $row['stock_type'];

						if($stock_type==1){ // production stock
							$item_status = 2; 
						}else{
							$item_status = 0;
						}


						$db->autocommit(FALSE); // to rollback insertion if update fail
						$sql ="INSERT INTO purchase (purchase_number,
													 purchase_auto_number,
													 purchase_date,
													 purchase_note,
													 stock_id_fk,
													 supplier_id_fk,
													 created_by,
													 last_action,
													 last_action_by)
											 VALUES('$purchase_number',
											 		'$purchase_auto_number',
											 		'$purchase_date',
											 		'$purchase_note',
											 		'$stock_id_fk',
											 		'$supplier_id_fk',
											 		'".$_SESSION['user_id']."',
											 		'$lastaction',
											 		'".$_SESSION['user_id']."')";
						
						$res = $db->query($sql);

						if ($res) {

									$last_inserted_purchase_id = $db->insert_id;

									// insert purchase_items Table

									$check_all_inserted_items = false;

									for ($i=1;$i<count($product_id_fk);$i++){

											$sql ="INSERT INTO purchase_item (purchase_quantity,buy_price_per_meter,purchase_usd_rate,purchase_currency,purchase_item_note,purchase_id_fk,product_id_fk,created_by,last_action,last_action_by)
											VALUES('$purchase_quantity[$i]','$buy_price_per_meter[$i]','".$_SESSION['USD_TO_IQD']."','$expenses_currency','$purchase_item_note[$i]','$last_inserted_purchase_id','$product_id_fk[$i]','".$_SESSION['user_id']."','$lastaction','".$_SESSION['user_id']."')";

											$res = $db->query($sql);
											if ($res) {
												$check_all_inserted_items = true;
												$last_inserted_purchase_item_id = $db->insert_id;
												
												
												 // insert into purchase details 
													$sql_multi ="";
													for ($j=1;$j<count($item_length[$i]);$j++){
														$item = $item_length[$i][$j];
														$sql_multi = $sql_multi . "  ('$item','$item_status','$last_inserted_purchase_id','$last_inserted_purchase_item_id','$product_id_fk[$i]','$stock_id_fk','".$_SESSION['user_id']."','$lastaction','".$_SESSION['user_id']."') ,";
													}
													$sql_multi = rtrim( $sql_multi,',');

													$sql ="INSERT INTO purchase_item_detail (item_length,item_status,purchase_id_fk,purchase_item_id_fk,product_id_fk,stock_id_fk,created_by,last_action,last_action_by)
													VALUES " . $sql_multi;
													
													$res = $db->query($sql);
													if ($res) {
														$check_all_inserted_items_details = true;
													}else{
														$check_all_inserted_items_details = false;
														$db->rollback();
														// echo $db->error;
														$op['is_success'] = false;
														$op['data'] 	  = "هناك خطأ : لم يتم اضافة المشتريات الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
														echo json_encode($op);
														exit;

													}

												// end of insertion purchase details


											}else{
												$check_all_inserted_items = false;
												$db->rollback();
												// echo $db->error;
												$op['is_success'] = false;
												$op['data'] 	  = "هناك خطأ : لم يتم اضافة المشتريات الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
												echo json_encode($op);
												exit;
											}

									}


									// end insert in purchase_items table

									if($check_all_inserted_items == true && $check_all_inserted_items_details == true ){
										$db->commit();
										$op['is_success'] = true;
										$op['data'] 	  = "تمت عملية الاضافة بنجاح";
										echo json_encode($op);
										exit;
									}else{
										$db->rollback();
										$op['is_success'] = false;
										$op['data'] 	  = "هناك خطأ : لم يتم اضافة المشتريات الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
										echo json_encode($op);
										exit;
									}

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


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