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



date_default_timezone_set($_SESSION['SYSTimezone']);

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

		if( isset($_GET['type']) && ($_GET['type'] == "load")){


			$cond1 = isset($_GET['id'])? ' AND PS.product_id_fk='.$_GET['id']:'';
			$cond2 = isset($_GET['pid'])? ' AND P.product_type="'.$_GET['pid'].'"':'';

			$cond3 = '';
			if(isset($_GET['branch_id']) && $_GET['branch_id'] !=0){ 
				$cond3 = ' AND Inv.branch_id_fk ='.$_GET['branch_id'];
			}
			
			$cond = $cond1 . $cond2 . $cond3 ;
			
			$table  =  '(

				SELECT
					
						invoice_id,
						invoice_number,
						invoice_date,
						is_returned,
						
						invoice_total_net_balance,
						invoice_note,
						Inv.actual_discount,
						
						invoice_transaction,
																
						customer_full_name AS name,
						
						full_name,
						branch_name,
						saleman_full_name,

						sell_item_id,
						sell_item_quantity,
						item_buy_price_per_unit,
						item_sell_price_per_unit,
						product_name


					 FROM  invoice Inv
					 		LEFT JOIN sell_item SI 	ON (Inv.invoice_id 	= SI.invoice_id_fk) 
					 		LEFT JOIN product P 	ON (P.product_id 	= SI.product_id_fk) 
					 		LEFT JOIN customer Cus 	ON (Inv.customer_id_fk 	= Cus.customer_id) 
					 		LEFT JOIN staff Stf 	ON (Inv.created_by 		= Stf.staff_id) 
					 		LEFT JOIN saleman SM 	ON (Inv.saleman_id_fk 	= SM.saleman_id) 
					 		LEFT JOIN branch B 		ON (Inv.branch_id_fk 	= B.branch_id) 
					
				WHERE
					( Inv.is_deleted = 0 AND SI.is_deleted=0 AND  Inv.is_returned = 1 '.$cond.') 
		
				) AS ALLTBL';
			
			
			// Table's primary key
			// echo $table;
			$primaryKey = 'invoice_id';
			$columns =  array(
				  			  	array( 'db' => 'sell_item_id', 'dt' => 0 ),
			    				array( 'db' => 'invoice_number','dt' => 1 ),
			    				array( 'db' => 'invoice_date', 'dt' => 2, 'formatter'=> function($d){
										return date('Y-m-d',strtotime($d));
								} ),
								array( 'db' => 'is_returned', 'dt' => 3, 'formatter'=> function($d){
										if ($d==0) {
											return 'Invoice';
										}else{
											return 'Return';
										}
								} ),
			    				array( 'db' => 'name','dt' => 4 ),
			    				array( 'db' => 'product_name','dt' => 5 ),
			    				array( 'db' => 'sell_item_quantity','dt' => 6 ),
			    				array( 'db' => 'item_buy_price_per_unit','dt' => 7 ),
			    				array( 'db' => 'item_sell_price_per_unit','dt' => 8 ),
			    				array( 'db' => 'branch_name','dt' => 9 ),
			    				array( 'db' => 'saleman_full_name','dt' => 10 ),
			    				array( 'db' => 'invoice_id','dt' => 11 ),
			    				
			    				
								
			);
			// 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'] == "cost") {

            			$cost 			=addslashes($_POST["cost_price"]);
            			$id 			=addslashes($_POST["id"]);

						

							
						$delete 	= 0;
						$reg_date 	= date("Y-m-d H:i:s", time());
						$lastaction = "Update Buy Price";
						
						$db->autocommit(FALSE); // to rollback insertion if update fail

						$sql ="UPDATE sell_item SET item_buy_price_per_unit 	= '$cost',
														  
														
														  
														  last_action 		= '$lastaction',
														  last_action_by 	= '".$_SESSION['user_id']."',
														  updated_at 		= '$reg_date'
										WHERE sell_item_id = '$id' ";

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

		     			if ($res){

								$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{
		header("Location:../");
		exit;
	}
?>

