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

set_time_limit(0);

require_once 'vendor/autoload.php';

// include('barcode/src/BarcodeGenerator.php');
// include('barcode/src/BarcodeGeneratorPNG.php');
// include('barcode/src/BarcodeGeneratorSVG.php');
// include('barcode/src/BarcodeGeneratorJPG.php');
// include('barcode/src/BarcodeGeneratorHTML.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 SF.staff_id_fk='.$_GET['id']:'';
			$cond1 = isset($_GET['tr'])? ' AND SF.safe_transaction="'.$_GET['tr'].'"':'';

			if(isset($_GET['branch_id']) && $_GET['branch_id'] !=0){ 
				$cond = $cond . ' AND SF.branch_id_fk ='.$_GET['branch_id'];
			}

			$cond = $cond . $cond1;

			$table  =  '(
				SELECT
					safe_id,
					safe_number,
					safe_amount,
					date_of_add,
					safe_transaction,
					
					transaction_type,
					capital_currency,
					table_name,
					id_fk,
					safe_sequence_no,
					safe_note,
					staff_id_fk,
					SF.branch_id_fk,
					S.full_name,
					branch_name,
					
					
					(SELECT sum(safe_amount) FROM safe SF  WHERE SF.is_deleted =0 AND transaction_type =1 '.$cond.') AS total_amount,

					(SELECT sum(safe_amount) FROM safe SF  WHERE SF.is_deleted =0 AND transaction_type =2 '.$cond.') AS total_subtract
				FROM
					safe SF
					LEFT JOIN staff S ON (SF.staff_id_fk = S.staff_id )
					LEFT JOIN branch B ON (SF.branch_id_fk = B.branch_id )
					
				WHERE
					( SF.is_deleted = 0 '.$cond.') 
			) AS ALLTBL';
			// Table's primary key
			//echo $table;
			$primaryKey = 'safe_id';
			$columns = array(
			    array( 'db' => 'safe_id', 'dt' => 0 ),

			    array( 'db' => 'safe_number', 'dt' => 1 ),
			    array( 'db' => 'safe_amount', 'dt' => 2 ),
			    array( 'db' => 'date_of_add',   'dt' => 3 , 'formatter'=> function($d){
									return date('Y-m-d',strtotime($d));
									 } ),
			    array( 'db' => 'safe_transaction', 'dt' => 4 ),
			    array( 'db' => 'transaction_type', 'dt' => 5 ),
			    array( 'db' => 'full_name', 'dt' => 6 ),
			    array( 'db' => 'safe_note', 'dt' => 7 ),
			    array( 'db' => 'branch_name', 'dt' => 8 ),
			    array( 'db' => 'total_amount', 'dt' => 9 ),
			    array( 'db' => 'total_subtract', 'dt' => 10 ),
			    array( 'db' => 'staff_id_fk', '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']=="sell_report") {

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$report_type 	= $_POST['report_type'];
			$customer_id_fk = $_POST['customer_id_fk'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (Inv.invoice_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = "";

			if($customer_id_fk > 0){
				$cond1 = "  AND Inv.customer_id_fk = $customer_id_fk ";

			}
			

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 2){

				$res = $db->query("SELECT
									
									

									sum(invoice_total_price_actual) AS final_price,

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0
									)  as actual_discount
									
											
						
								FROM
								 invoice_archive Inv
								 
							WHERE
								(Inv.is_deleted = 0 AND Inv.invoice_type = 0  AND Inv.invoice_status <3  $cond) 

					 "); // all type

				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['final_price']  		= $row['final_price'];
							$result['actual_discount']  	= $row['actual_discount'];
							
										
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

			
				$res = $db->query("SELECT

									invoice_id,
									invoice_number,
									invoice_date,
									invoice_status,
									invoice_usd_rate,
									invoice_currency,
									invoice_total_price_actual,
									invoice_total_net_balance,
									invoice_note,
									customer_id_fk,
									pay_terms,
									return_date,
									Inv.created_at, 
									full_name,

				          			(invoice_total_price_actual) as final_price,


									(invoice_total_price_actual
								    	+
								    (SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0 )
										
									) as total_invoice_price_no_discount,

									
									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0
									)  as actual_discount,

										customer_full_name AS name,
										customer_email AS email,
										customer_phone 
									FROM
									 invoice_archive Inv
									 	LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id) 
								 		LEFT JOIN staff Stf 	ON (Inv.created_by = Stf.staff_id) 
									 
								WHERE
									(Inv.customer_id_fk=Cus.customer_id  AND Inv.is_deleted = 0 AND Inv.invoice_type = 0  AND Inv.invoice_status <3 $cond) ORDER BY invoice_id DESC

					 "); // all type

			
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['invoice_id']  			= $row['invoice_id'];
						$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('Y-m-d');

						$result['time']  				= $time;				
						$result['customer_name']  		= $row['name'];
						$result['staff']  				= $row['full_name'];
						$result['total_invoice_discount']  = $row['total_invoice_price_no_discount'];
							
						$result['invoice_note']  		= $row['invoice_note'];					
							
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}
			}
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$report_type 	= $_POST['report_type'];
			$customer_id_fk = $_POST['customer_id_fk'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (Inv.invoice_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = "";

			if($customer_id_fk > 0){
				$cond1 = "  AND Inv.customer_id_fk = $customer_id_fk ";

			}
			

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 2){

				$res = $db->query("SELECT
									
									

									sum(invoice_total_price_actual) AS final_price,

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0
									)  as actual_discount
									
											
						
								FROM
								 invoice_archive Inv
								 
							WHERE
								(Inv.is_deleted = 0 AND Inv.invoice_type = 1 AND Inv.is_single=0  AND Inv.invoice_status <3  $cond) 

					 "); // all type

				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['final_price']  		= $row['final_price'];
							$result['actual_discount']  	= $row['actual_discount'];
							
										
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

			
				$res = $db->query("SELECT

									invoice_id,
									invoice_number,
									invoice_date,
									invoice_status,
									invoice_usd_rate,
									invoice_currency,
									invoice_total_price_actual,
									invoice_total_net_balance,
									invoice_note,
									customer_id_fk,
									pay_terms,
									return_date,
									
									Inv.created_at, 
									full_name,

									( invoice_total_price_actual + 

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0 )
									) as total_invoice_price_no_discount,
				          
				          			( invoice_total_price_actual ) as final_price,

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0 )  as actual_discount,
										

										customer_full_name AS name,
										customer_email AS email,
										customer_phone 
									FROM
									 invoice_archive Inv
									 	LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id) 
								 		LEFT JOIN staff Stf 	ON (Inv.created_by = Stf.staff_id) 
									 
								WHERE
									(Inv.customer_id_fk=Cus.customer_id  AND Inv.is_deleted = 0 AND Inv.invoice_type = 1 AND Inv.is_single=0  AND Inv.invoice_status <3 $cond) ORDER BY invoice_id DESC

					 "); // all type

			
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['invoice_id']  			= $row['invoice_id'];
						$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('Y-m-d');

						$result['time']  				= $time;				
						$result['customer_name']  		= $row['name'];
						$result['staff']  				= $row['full_name'];
						$result['total_invoice_discount']  = $row['total_invoice_price_no_discount'];
							
						$result['invoice_note']  		= $row['invoice_note'];					
							
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}
			}
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$report_type 	= $_POST['report_type'];
			$customer_id_fk = $_POST['customer_id_fk'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (Inv.invoice_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = "";

			if($customer_id_fk > 0){
				$cond1 = "  AND Inv.customer_id_fk = $customer_id_fk ";

			}
			

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 2){

				$res = $db->query("SELECT
									
									

									sum(invoice_total_price_actual) AS final_price,

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0
									)  as actual_discount
									
											
						
								FROM
								 invoice_archive Inv
								 
							WHERE
								(Inv.is_deleted = 0 AND Inv.invoice_type = 1 AND Inv.is_single=1  AND Inv.invoice_status <3  $cond) 

					 "); // all type

				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['final_price']  		= $row['final_price'];
							$result['actual_discount']  	= $row['actual_discount'];
							
										
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

			
				$res = $db->query("SELECT

									invoice_id,
									invoice_number,
									invoice_date,
									invoice_status,
									invoice_usd_rate,
									invoice_currency,
									invoice_total_price_actual,
									invoice_total_net_balance,
									invoice_note,
									customer_id_fk,
									pay_terms,
									return_date,
									
									Inv.created_at, 
									full_name,

									( invoice_total_price_actual + 

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0 )
									) as total_invoice_price_no_discount,
				          
				          			( invoice_total_price_actual ) as final_price,

									(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = Inv.invoice_id AND SI.is_deleted=0 )  as actual_discount,
										

										customer_full_name AS name,
										customer_email AS email,
										customer_phone 
									FROM
									 invoice_archive Inv
									 	LEFT JOIN customer Cus ON (Inv.customer_id_fk=Cus.customer_id) 
								 		LEFT JOIN staff Stf 	ON (Inv.created_by = Stf.staff_id) 
									 
								WHERE
									(Inv.customer_id_fk=Cus.customer_id  AND Inv.is_deleted = 0 AND Inv.invoice_type = 1 AND Inv.is_single=1  AND Inv.invoice_status <3 $cond) ORDER BY invoice_id DESC

					 "); // all type

			
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['invoice_id']  			= $row['invoice_id'];
						$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('Y-m-d');

						$result['time']  				= $time;				
						$result['customer_name']  		= $row['name'];
						$result['staff']  				= $row['full_name'];
						$result['total_invoice_discount']  = $row['total_invoice_price_no_discount'];
							
						$result['invoice_note']  		= $row['invoice_note'];					
							
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}
			}
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$box_id_fk 		= $_POST['box_id_fk'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (SF.date_of_add) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['box_id_fk']) && $_POST['box_id_fk'] !=0){ 
				$cond1 = ' AND SF.box_id_fk ='.$_POST['box_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
									safe_id,
									safe_number,
									safe_amount,
									date_of_add,
									safe_transaction,
									
									transaction_type,
									capital_currency,
									table_name,
									id_fk,
									safe_sequence_no,
									safe_note,
									staff_id_fk,
									SF.box_id_fk,
									S.full_name,
									box_name,
									area,
									tac,
									isik,
									
									
									(SELECT sum(safe_amount) FROM safe_archive SF  WHERE SF.is_deleted =0 AND SF.is_suspended=0 AND transaction_type =1 $cond) AS total_amount,

									(SELECT sum(safe_amount) FROM safe_archive SF  WHERE SF.is_deleted =0 AND SF.is_suspended=0 AND transaction_type =2 $cond) AS total_subtract,

									(SELECT sum(safe_amount) FROM safe_archive SF  WHERE SF.is_deleted =0 AND transaction_type =3 $cond) AS total_sell
								FROM
									safe_archive SF
									LEFT JOIN staff S ON (SF.staff_id_fk = S.staff_id )
									LEFT JOIN box B ON (SF.box_id_fk = B.box_id )
									
								WHERE
									( SF.is_deleted = 0 AND SF.is_suspended=0 $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							$result['safe_id']  			= $row['safe_id'];
							$result['safe_number']  		= $row['safe_number'];
							$result['safe_amount']  		= $row['safe_amount'];
							$result['date_of_add']  		= $row['date_of_add'];

						
		
							$result['safe_transaction']  	= $row['safe_transaction'];
							$result['transaction_type']  	= $row['transaction_type'];
							$result['safe_note']  			= $row['safe_note'];
							$result['box_name']  			= $row['box_name'];
							$result['full_name']  			= $row['full_name'];					
							$result['area']  				= $row['area'];					
							$result['tac']  				= $row['tac'];					
							$result['isik']  			= $row['isik'];					
							$result['total_amount']  		= $row['total_amount'];					
							$result['total_subtract']  		= $row['total_subtract'];					
							$result['total_sell']  			= $row['total_sell'];					
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
									
									box_name,
									sum(area) as area,
									sum(tac) as tac,
									sum(isik) as isik,
									
									
									(SELECT sum(safe_amount) FROM safe SF  WHERE SF.is_deleted =0 AND transaction_type =1 $cond) AS total_amount,

									(SELECT sum(safe_amount) FROM safe SF  WHERE SF.is_deleted =0 AND transaction_type =2 $cond) AS total_subtract,

									(SELECT sum(safe_amount) FROM safe SF  WHERE SF.is_deleted =0 AND transaction_type =3 $cond) AS total_sell
								FROM
									safe_archive SF
									
									LEFT JOIN box B ON (SF.box_id_fk = B.box_id )
									
								WHERE
									( SF.is_deleted = 0 $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['box_name']  			= $row['box_name'];
							$result['area']  				= $row['area'];					
							$result['tac']  				= $row['tac'];					
							$result['isik']  			= $row['isik'];					
							$result['total_amount']  		= $row['total_amount'];					
							$result['total_subtract']  		= $row['total_subtract'];				
							$result['total_sell']  			= $row['total_sell'];					
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$box_id_fk 		= $_POST['box_id_fk'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (E.expenses_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['box_id_fk']) && $_POST['box_id_fk'] !=0){ 
				$cond1 = ' AND E.box_id_fk ='.$_POST['box_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
									expenses_id,
									expenses_name,
									expenses_quantity,
									expenses_unit_price,
									expenses_date,
									
									expenses_note,
									bill_number,
									attach_bill,
									expenses_group_id_fk,
									box_id_fk,
									
									box_name,
									group_name,
									
									
									
									(SELECT sum( expenses_unit_price) FROM expenses_archive E  WHERE E.is_deleted =0   $cond) AS total_amount
								FROM
									expenses_archive E
									LEFT JOIN expenses_group EG ON (E.expenses_group_id_fk = EG.expenses_group_id )
									LEFT JOIN box B ON (E.box_id_fk = B.box_id )
									
								WHERE
									( E.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							$result['expenses_id']  		= $row['expenses_id'];
							$result['expenses_name']  		= $row['expenses_name'];
							$result['expenses_quantity']  	= $row['expenses_quantity'];
							$result['expenses_unit_price']  = $row['expenses_unit_price'];
		
							$result['expenses_date']  		= $row['expenses_date'];
							$result['expenses_note']  		= $row['expenses_note'];
							$result['bill_number']  		= $row['bill_number'];
							$result['attach_bill']  			= $row['attach_bill'];
							$result['box_name']  			= $row['box_name'];					
							$result['group_name']  				= $row['group_name'];					
								
							$result['total_amount']  		= $row['total_amount'];					
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
									box_name,
									sum( expenses_unit_price) as final_price
								FROM
									expenses_archive E
									
									LEFT JOIN box B ON (E.box_id_fk = B.box_id )
									
								WHERE
									( E.is_deleted = 0 $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['box_name']  			= $row['box_name'];
							$result['final_price']  		= $row['final_price'];					
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}



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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$box_id_fk 		= $_POST['box_id_fk'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (E.expenses_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['box_id_fk']) && $_POST['box_id_fk'] !=0){ 
				$cond1 = ' AND E.box_id_fk ='.$_POST['box_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
									asset_id,
									expenses_name,
									expenses_quantity,
									expenses_unit_price,
									expenses_date,
									
									expenses_note,
									bill_number,
									attach_bill,
									expenses_group_id_fk,
									box_id_fk,
									
									box_name,
									
									
									
									(SELECT sum( expenses_unit_price) FROM assets_archive E  WHERE E.is_deleted =0   $cond) AS total_amount
								FROM
									assets_archive E
									LEFT JOIN box B ON (E.box_id_fk = B.box_id )
									
								WHERE
									( E.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							$result['asset_id']  			= $row['asset_id'];
							$result['expenses_name']  		= $row['expenses_name'];
							$result['expenses_quantity']  	= $row['expenses_quantity'];
							$result['expenses_unit_price']  = $row['expenses_unit_price'];
		
							$result['expenses_date']  		= $row['expenses_date'];
							$result['expenses_note']  		= $row['expenses_note'];
							$result['bill_number']  		= $row['bill_number'];
							$result['attach_bill']  		= $row['attach_bill'];
							$result['box_name']  			= $row['box_name'];					
								
							$result['total_amount']  		= $row['total_amount'];					
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
									box_name,
									sum( expenses_unit_price) as final_price
								FROM
									assets_archive E
									
									LEFT JOIN box B ON (E.box_id_fk = B.box_id )
									
								WHERE
									( E.is_deleted = 0 $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['box_name']  			= $row['box_name'];
							$result['final_price']  		= $row['final_price'];					
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}



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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$box_id_fk 		= $_POST['box_id_fk'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (E.date_of_add) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['box_id_fk']) && $_POST['box_id_fk'] !=0){ 
				$cond2 = ' AND E.box_id_fk ='.$_POST['box_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
									capital_id,
									capital_number,
									capital_amount,
									date_of_add,
									amount_owner_staff_id_fk,
									S.full_name,
									capital_transaction,
									capital_note,
									
									(SELECT sum(capital_amount) FROM capital_archive E WHERE is_deleted =0 AND capital_transaction='Add Capital Amount' $cond) AS total_amount,
									(SELECT sum(capital_amount) FROM capital_archive E WHERE is_deleted =0 AND capital_transaction='Transfer to Factory' $cond) AS total_transfered
								FROM
									capital_archive E
									LEFT JOIN staff S ON (E.amount_owner_staff_id_fk = S.staff_id )
									
									
								WHERE
									( E.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							$result['capital_id']  			= $row['capital_id'];
							$result['capital_number']  		= $row['capital_number'];
							$result['capital_amount']  		= $row['capital_amount'];
							$result['date_of_add']  		= $row['date_of_add'];
		
							$result['full_name']  			= $row['full_name'];
							$result['capital_transaction']  = $row['capital_transaction'];
							$result['capital_note']  		= $row['capital_note'];
							
							$result['total_amount']  		= $row['total_amount'];					
							$result['total_transfered']  	= $row['total_transfered'];					
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
									 sum(capital_amount)  AS total_amount,
									(SELECT sum(capital_amount) FROM capital_archive E  WHERE is_deleted =0 AND capital_transaction='Transfer to Factory' $cond) AS total_transfered
								FROM
									capital_archive E
									
									
								WHERE
									( E.is_deleted = 0 AND capital_transaction='Add Capital Amount' $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['total_amount']  			= $row['total_amount'];
							$result['total_transfered']  		= $row['total_transfered'];					
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$box_id_fk 		= $_POST['box_id_fk'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (E.date_of_add) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['box_id_fk']) && $_POST['box_id_fk'] !=0){ 
				$cond2 = ' AND E.box_id_fk ='.$_POST['box_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
									capital_id,
									capital_number,
									capital_amount,
									date_of_add,
									amount_owner_staff_id_fk,
									S.full_name,
									capital_transaction,
									capital_note,
									
									(SELECT sum(capital_amount) FROM capital_archive E  WHERE is_deleted =0 AND capital_transaction='Transfer to Factory' $cond) AS total_amount,

									(SELECT sum(capital_amount) FROM capital_archive E  WHERE is_deleted =0 AND is_approved=1 AND capital_transaction='Transfer to Factory' $cond) AS total_approved,

									(SELECT sum(capital_amount) FROM capital_archive E  WHERE is_deleted =0 AND is_approved=0 AND capital_transaction='Transfer to Factory' $cond) AS total_not_approved

								FROM
									capital_archive E
									LEFT JOIN staff S ON (E.amount_owner_staff_id_fk = S.staff_id )
									
									
								WHERE
									( E.is_deleted = 0 AND (E.capital_transaction='Transfer to Factory' OR E.capital_transaction='Request Transfer')  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							$result['capital_id']  			= $row['capital_id'];
							$result['capital_number']  		= $row['capital_number'];
							$result['capital_amount']  		= $row['capital_amount'];
							$result['date_of_add']  		= $row['date_of_add'];
		
							$result['full_name']  			= $row['full_name'];
							$result['capital_transaction']  = $row['capital_transaction'];
							$result['capital_note']  		= $row['capital_note'];
							
							$result['total_amount']  		= $row['total_amount'];					
							$result['total_approved']  		= $row['total_approved'];					
							$result['total_not_approved']  	= $row['total_not_approved'];					
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
									sum(capital_amount)  AS total_amount,

									(SELECT sum(capital_amount) FROM capital_archive E  WHERE is_deleted =0 AND is_approved=1 AND capital_transaction='Transfer to Factory' $cond) AS total_approved,

									(SELECT sum(capital_amount) FROM capital_archive E  WHERE is_deleted =0 AND is_approved=0 AND capital_transaction='Transfer to Factory' $cond) AS total_not_approved
								FROM
									capital_archive E
									
									
								WHERE
									( E.is_deleted = 0 AND (E.capital_transaction='Transfer to Factory')  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['total_amount']  			= $row['total_amount'];
							$result['total_approved']  			= $row['total_approved'];					
							$result['total_not_approved']  		= $row['total_not_approved'];					
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}



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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (CW.withdraw_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['box_id_fk']) && $_POST['box_id_fk'] !=0){ 
				$cond2 = ' AND E.box_id_fk ='.$_POST['box_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
									capital_withdraw_id,
									withdraw_amount,
									withdraw_date,
									withdraw_by,
									for_company,
									withdraw_usd_rate,
									withdraw_currency,
									withdraw_status,
									withdraw_note,
									staff_customer,
									gived_to_id_fk,
									full_name,
									customer_full_name,
									(SELECT sum(withdraw_amount) FROM capital_withdraw_archive CW  WHERE CW.is_deleted =0) AS total_amount

								FROM
									capital_withdraw_archive CW
									LEFT JOIN staff S ON (CW.withdraw_by = S.staff_id )
									LEFT JOIN customer C ON (CW.gived_to_id_fk = C.customer_id )
									
								WHERE
									( CW.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['capital_withdraw_id']  = $row['capital_withdraw_id'];
						$result['withdraw_amount']  	= $row['withdraw_amount'];
						$result['withdraw_date']  		= $row['withdraw_date'];
	
						$result['full_name']  			= $row['full_name'];
						$result['customer_full_name']  	= $row['customer_full_name'];
						$result['withdraw_note']  		= $row['withdraw_note'];
						
						$result['total_amount']  		= $row['total_amount'];					
						
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
									sum(withdraw_amount) AS total_amount

								FROM
									capital_withdraw_archive CW
									
									
								WHERE
									( CW.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['total_amount']  			= $row['total_amount'];
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}



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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (PY.paid_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['customer_id_fk']) && $_POST['customer_id_fk'] !=''){ 
				$cond1 = ' AND PY.customer_id_fk ='.$_POST['customer_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
										payment_id,
										payment_number,
										pay_usd_rate,
										pay_currency,
										paid_amount,
										paid_date,
										payment_note,
										payment_transaction,
										invoice_id_fk,
										customer_id_fk,
										
										customer_full_name,
										full_name,
										(SELECT sum(paid_amount) FROM payments_archive PY  WHERE is_deleted =0 AND payment_transaction ='Payment' $cond) AS total_paid,
										(SELECT sum(paid_amount) FROM payments_archive PY  WHERE is_deleted =0 AND payment_transaction ='Discount' $cond) AS total_account_discount,
										(SELECT sum(paid_amount) FROM payments_archive PY  WHERE is_deleted =0 AND payment_transaction ='Return' $cond) AS total_returned
									FROM
										payments_archive PY
										
										LEFT JOIN customer C ON (PY.customer_id_fk = C.customer_id )
										LEFT JOIN staff S ON (PY.created_by = S.staff_id )
										
									WHERE
										( PY.is_deleted = 0 $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['payment_id']  				= $row['payment_id'];
						$result['customer_full_name']  		= $row['customer_full_name'];
						$result['payment_number']  			= $row['payment_number'];
						$result['paid_amount']  			= $row['paid_amount'];
	
						$result['paid_date']  				= $row['paid_date'];
						$result['payment_transaction']  	= $row['payment_transaction'];
						$result['payment_note']  			= $row['payment_note'];
						
						$result['total_paid']  				= $row['total_paid'];					
						$result['total_account_discount'] 	= $row['total_account_discount'];					
						$result['total_returned']  			= $row['total_returned'];					
						
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
										
										sum(paid_amount)  AS total_paid,
										(SELECT sum(paid_amount) FROM payments_archive PY  WHERE is_deleted =0 AND payment_transaction ='Discount' $cond) AS total_account_discount,
										(SELECT sum(paid_amount) FROM payments_archive PY  WHERE is_deleted =0 AND payment_transaction ='Return' $cond) AS total_returned
									FROM
										payments_archive PY
										
										
										
									WHERE
										( PY.is_deleted = 0 AND PY.payment_transaction ='Payment' $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['total_paid']  				= $row['total_paid'];
							$result['total_account_discount']  	= $row['total_account_discount'];
							$result['total_returned']  			= $row['total_returned'];
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (PIDA.updated_at) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
										purchase_item_detail_id,
										item_length,
										purchase_item_detail_note,
										item_status,
										sold_length,
										damage_length,
										used_length,
										previous_status,
										is_part,
										
										purchase_item_detail_id_fk,
										purchase_id_fk,
										purchase_item_id_fk,
										product_id_fk,
										stock_id_fk,
										stock_name,
										stock_type,
										product_name,
										erbil_code,
										turkish_code,
										barcode
										
									FROM
										purchase_item_detail_archive PIDA
										LEFT JOIN stock S ON (S.stock_id = PIDA.stock_id_fk)
										LEFT JOIN product P ON (P.product_id = PIDA.product_id_fk)
										
									WHERE 
										(PIDA.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['purchase_item_detail_id']  	= $row['purchase_item_detail_id'];
						$result['item_length']  				= $row['item_length'];
						$result['purchase_item_detail_note']  	= $row['purchase_item_detail_note'];
						$result['item_status']  				= $row['item_status'];
	
						$result['sold_length']  				= $row['sold_length'];
						$result['damage_length']  				= $row['damage_length'];
						$result['used_length']  				= $row['used_length'];
						
						$result['stock_name']  					= $row['stock_name'];
						$result['stock_type']  					= $row['stock_type'];
						$result['product_name']  				= $row['product_name'];
						$result['erbil_code']  					= $row['erbil_code'];
						$result['turkish_code']  				= $row['turkish_code'];
						$result['barcode']  					= $row['barcode'];
						
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				

			}

			
			
		}


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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			$box_id_fk 		= $_POST['box_id_fk'];
			$report_type 	= $_POST['report_type'];


			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? ' AND (PY.paid_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			
			$cond1 = '';
			if(isset($_POST['customer_id_fk']) && $_POST['customer_id_fk'] !=''){ 
				$cond2 = ' AND PY.customer_id_fk ='.$_POST['customer_id_fk'];
			}

			$cond   = $cond_d . ' ' . $cond1 ;

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

			if($report_type == 1){
				$res = $db->query("SELECT
										payment_id,
										payment_number,
										pay_usd_rate,
										pay_currency,
										paid_amount,
										paid_date,
										payment_note,
										payment_transaction,
										purchase_id_fk,
										PY.supplier_id_fk,
										paper_number,
										purchase_number,
										purchase_auto_number,
										supplier_full_name,
										(SELECT sum(paid_amount) FROM supplier_payments_archive PY  WHERE is_deleted =0 AND payment_transaction ='Payment' $cond) AS total_paid

										
										
									FROM
										supplier_payments_archive PY
										
										LEFT JOIN suppliers S ON (PY.supplier_id_fk = S.supplier_id )
										LEFT JOIN purchase P ON (PY.purchase_id_fk = P.purchase_id )
										
									WHERE
										( PY.is_deleted = 0  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
						$result['payment_id']  				= $row['payment_id'];
						$result['supplier_full_name']  		= $row['supplier_full_name'];
						$result['payment_number']  			= $row['payment_number'];
						$result['paid_amount']  			= $row['paid_amount'];
	
						$result['paid_date']  				= $row['paid_date'];
						$result['payment_transaction']  	= $row['payment_transaction'];
						$result['payment_note']  			= $row['payment_note'];
						
						$result['purchase_auto_number']  	= $row['purchase_auto_number'];					
						$result['total_paid']  				= $row['total_paid'];					
						
						array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}else{

				$res = $db->query("SELECT
										sum(paid_amount)   AS total_paid

										
										
									FROM
										supplier_payments_archive PY
										
										
										
									WHERE
										( PY.is_deleted = 0 AND payment_transaction ='Payment'  $cond) 

					 "); // all type
				if ($res) {				
					
					
					while ($row = $res->fetch_assoc()) {				   
							
							$result['total_paid']  				= $row['total_paid'];
												
							
							array_push($op['data'],$result);
							
					}

					$op['is_success'] = true;
					echo json_encode($op);
					exit;
				}else{
					
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);

					exit;
				}

			}

			
			
		}



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

			$d1 			= $_POST['d1'];
			$d2 			= $_POST['d2'];
			

			$cond_d = (isset($_POST['d1']) && isset($_POST['d2']))? '  (I.invoice_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			
			$cond_p = (isset($_POST['d1']) && isset($_POST['d2']))? '  (PY.paid_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_w = (isset($_POST['d1']) && isset($_POST['d2']))? '  (CW.withdraw_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_ps = (isset($_POST['d1']) && isset($_POST['d2']))? '  (PS.transaction_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_psk = (isset($_POST['d1']) && isset($_POST['d2']))? '  AND (PSK.transaction_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			
			$cond_pr = (isset($_POST['d1']) && isset($_POST['d2']))? '  (PR.purchase_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_s = (isset($_POST['d1']) && isset($_POST['d2']))? '  (S.date_of_add) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_e = (isset($_POST['d1']) && isset($_POST['d2']))? '  (E.expenses_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_c = (isset($_POST['d1']) && isset($_POST['d2']))? '  (C.date_of_add) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_a = (isset($_POST['d1']) && isset($_POST['d2']))? '  (A.expenses_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';

			$cond_sp = (isset($_POST['d1']) && isset($_POST['d2']))? '  (SP.paid_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			$cond_sa = (isset($_POST['d1']) && isset($_POST['d2']))? '  (SA.add_date) BETWEEN "'.$_POST['d1'].'" AND "'.$_POST['d2'].'"':'';
			
			

			$cond   = $cond_d ;

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

			$db->autocommit(FALSE); // to rollback insertion if update fail


			$res = $db->query("INSERT INTO invoice_archive 
										SELECT * FROM invoice I 
										WHERE ( $cond) 

									"); // all type


			if ($res) {	

				$res1 = $db->query("INSERT INTO sell_item_archive 
										SELECT SI.* FROM sell_item SI
										LEFT JOIN invoice I ON (SI.invoice_id_fk = I.invoice_id) 
										WHERE  $cond

									"); // all type

				if (!$res1) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}	


				$res3 = $db->query("INSERT INTO sell_item_detail_archive 
										SELECT SI.* FROM sell_item_detail SI
										LEFT JOIN invoice I ON (SI.invoice_id_fk = I.invoice_id) 
										WHERE  $cond

									"); // all type

				if (!$res3) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}	


				

				$res2 = $db->query("INSERT INTO payments_archive
										SELECT PY.* FROM payments PY
										
										WHERE  $cond_p

									"); // all type

				if (!$res2) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


				$res6 = $db->query("INSERT INTO capital_withdraw_archive
										SELECT CW.* FROM capital_withdraw CW
										
										WHERE  $cond_w

									"); // all type

				if (!$res6) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


				


				


				$res9 = $db->query("INSERT INTO purchase_item_detail_archive
										SELECT PI.* FROM sell_item_detail SID 

											LEFT JOIN purchase_item_detail PI ON ( PI.purchase_item_detail_id=SID.purchase_item_detail_id_fk  )
											LEFT JOIN invoice I ON (SID.invoice_id_fk = I.invoice_id)
											WHERE PI.item_status =1 AND $cond
											GROUP BY `PI`.`purchase_item_detail_id`

										

									"); // all type

				if (!$res9) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


				


				// now we should calc initial for abstract


				$reg_date = date("Y-m-d H:i:s", time());

				$lastaction = "Yearly Closer";


			
				// now we should add the remained of suppliers payments & customer account and product_stock

				


				$res20 = $db->query("

									SELECT 	customer_id,


									(

				          			COALESCE((SELECT sum(invoice_total_price_actual) 
				          			FROM invoice I 
								    WHERE I.customer_id_fk = C.customer_id AND I.invoice_status <3  AND I.is_deleted = 0 AND $cond_d),0)

									+

									COALESCE((SELECT sum(withdraw_amount) FROM capital_withdraw CW WHERE  CW.is_deleted = 0  AND CW.gived_to_id_fk = C.customer_id AND $cond_w),0)
									
									) as total_invoice_price,


									(
										COALESCE((SELECT sum(paid_amount) FROM payments PY 
       								
        								WHERE PY.is_returned=0 AND PY.is_deleted =0 AND PY.customer_id_fk = C.customer_id AND PY.payment_transaction ='Payment' AND $cond_p),0)

        							) as total_Paid,

        							(
        								COALESCE((SELECT sum(paid_amount) FROM payments PY WHERE PY.customer_id_fk = C.customer_id AND PY.is_returned=0 AND PY.is_deleted =0 AND PY.payment_transaction ='Discount' AND  $cond_p ),0)
        							) AS total_payment_discount,

									(
										COALESCE((SELECT sum(paid_amount) FROM payments PY WHERE PY.customer_id_fk = C.customer_id AND PY.is_returned=0 AND PY.is_deleted =0 AND PY.payment_transaction ='Return'  AND $cond_p ),0)
									) AS total_payment_return		

	

					FROM customer C WHERE C.is_deleted=0 

								");

		
				if ($res20) {
					
					$Numb_Year = 'CW-' . date("Y", time());
					$Nots = "ديون سابقة - غلق الحساب السنوي من تاريخ  ($d1) الى  ($d2)";
					
					while ($row = $res20->fetch_assoc()) {
						
						$remaind  	= $row['total_invoice_price'] - $row['total_Paid'] - $row['total_payment_discount'] + $row['total_payment_return'];

						$customer_id 	= $row['customer_id'];

						if ($remaind !=0) {
							
						
						
							$res21 = $db->query("INSERT INTO capital_withdraw 
																(
																 withdraw_amount,
																 withdraw_date,
																 withdraw_by,
																 gived_to_id_fk,
																 withdraw_note,
																 created_by,
																 created_at,
																 updated_at,
																 last_action,
																 last_action_by)
														  VALUES(
														  		 '$remaind',
														  		 '$reg_date',
														  		 '".$_SESSION['user_id']."',
														  		 '$customer_id',
														  		 '$Nots',
														  		 '".$_SESSION['user_id']."',
														  		 '$reg_date',
														  		 '$reg_date',
														  		 '$lastaction',
														  		 '".$_SESSION['user_id']."')"); // all type

							if (!$res21) {	
								$op['is_success'] = false;
								$op['data'] 	  = $db->error;
								echo json_encode($op);
					    		$db->rollback();

								exit;
							}
						}
							
					}		
					
				}else{
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}



				$resd = $db->query("DELETE I FROM invoice I 
										WHERE ( $cond) 

									"); // all type

				if (!$resd) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


				$resd1 = $db->query("DELETE SI FROM sell_item SI
										LEFT JOIN invoice I ON (SI.invoice_id_fk = I.invoice_id) 
										WHERE  $cond

									"); // all type

				if (!$resd1) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


	


				$resd3 = $db->query("DELETE SI FROM sell_item_detail SI
										LEFT JOIN invoice I ON (SI.invoice_id_fk = I.invoice_id) 
										WHERE  $cond

									"); // all type

				if (!$resd3) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}	


				$resd2 = $db->query("DELETE PY FROM payments PY
										
										WHERE  $cond_p

									"); // all type

				if (!$resd2) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


				$resd6 = $db->query("DELETE CW FROM capital_withdraw CW
										
										WHERE  $cond_w

									"); // all type

				if (!$resd6) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


				


				$resd9 = $db->query("DELETE PI FROM purchase_item_detail PI 

									LEFT JOIN purchase_item_detail_archive PIA ON ( PIA.is_deleted=0  )
											
									WHERE PI.purchase_item_detail_id=PIA.purchase_item_detail_id
									"); // all type


				
				if (!$resd9) {	
					$op['is_success'] = false;
					$op['data'] 	  = $db->error;
					echo json_encode($op);
		    		$db->rollback();

					exit;
				}


			






				$db->commit();

				$op['is_success'] = true;
				$op['data'] 	  = "تمت عملية الترحيل بنجاح وتم أرشفت  البيانات";

				echo json_encode($op);
				exit;
				
				
			}else{
				
				$op['is_success'] = false;
				$op['data'] 	  = $db->error;
				echo json_encode($op);
	    		$db->rollback();

				exit;
			}

			

			
			
		}


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

			$id = $_POST['id'];


			$res = $db->query("SELECT invoice_id, 
									invoice_number, 
									invoice_date, 
									invoice_status, 
									invoice_usd_rate, 
									invoice_currency,
									invoice_total_price_actual, 
									invoice_note, 
									sell_item_quantity, 
									customer_id_fk, 
									sell_discount, 
									sell_discount_type, 
									actual_discount,
									item_sell_price_per_meter,
									decorate_price_per_meter, 
									is_paid,
									sell_item_notes, 
									S.item_type_id_fk, 
								 	other_id_fk, 
									S.product_id_fk, 
									sell_item_id,
									customer_full_name,
									customer_email,
									customer_phone,
									customer_phone2,
									customer_address, 
									CONCAT(COALESCE(erbil_code,''), COALESCE(other_name,'')) AS product_name,
									(SELECT sum(paid_amount) FROM payments_archive IP WHERE I.invoice_id = IP.invoice_id_fk AND IP.is_returned=0 AND IP.is_deleted =0 AND IP.payment_transaction='Payment') AS total_paid,

									((SELECT sum(sell_item_detail_height * item_sell_price_per_meter) FROM sell_item_detail_archive SID, sell_item_archive S 

								        WHERE I.invoice_id = SID.invoice_id_fk AND S.invoice_id_fk = I.invoice_id AND S.is_deleted=0 AND  SID.part_status =0 AND SID.is_deleted=0 AND S.sell_item_id = SID.sell_item_id_fk AND S.other_id_fk =0)
								    +
								    COALESCE((SELECT sum(sell_item_quantity * item_sell_price_per_meter) FROM sell_item_archive S  
														WHERE I.invoice_id = S.invoice_id_fk AND  S.other_id_fk >0 AND S.is_deleted=0),0)
										-
								 	(SELECT sum(actual_discount) from sell_item_archive SI where SI.invoice_id_fk = I.invoice_id AND SI.is_deleted=0 )
									) as total_invoice_price,

									(SELECT sum(actual_discount) FROM sell_item_archive SI WHERE I.invoice_id = SI.invoice_id_fk AND SI.is_deleted =0) AS total_discount,

									(SELECT sum(sell_item_detail_height) FROM sell_item_detail_archive SID WHERE SID.invoice_id_fk = '$id' AND SID.sell_item_id_fk=S.sell_item_id AND part_status=0 AND SID.is_deleted = 0) AS total_length

					FROM invoice_archive I
				 		LEFT JOIN sell_item_archive S ON (S.invoice_id_fk = I.invoice_id AND S.is_deleted=0)
				 		LEFT JOIN others O ON (S.other_id_fk = O.other_id AND S.other_id_fk >0)
						LEFT JOIN product P ON (S.product_id_fk = P.product_id )
						LEFT JOIN customer C ON (I.customer_id_fk = C.customer_id )
				 	WHERE I.invoice_id='$id'  AND I.is_deleted = 0");
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array('invoice'=>array(),'details'=>array(),'account'=>array());
				while ($row = $res->fetch_assoc()) {
				   
					$result['invoice_id']  					= $row['invoice_id'];
					$result['invoice_number']  				= $row['invoice_number'];
					$result['invoice_date']  				= date('Y-m-d',strtotime($row['invoice_date'])) ;
					$result['invoice_status']  				= $row['invoice_status'];
					$result['invoice_usd_rate']  			= $row['invoice_usd_rate'];
					$result['invoice_currency']  			= $row['invoice_currency'];
					$result['invoice_total_price_actual'] 	= $row['total_invoice_price'];
					$result['invoice_note']  				= $row['invoice_note'];
					$result['sell_item_quantity']  			= $row['sell_item_quantity'];
					$result['sell_discount']  				= $row['sell_discount'];
					$result['sell_discount_type']  			= $row['sell_discount_type'];
					$result['actual_discount']  			= $row['actual_discount'];
					$result['item_sell_price_per_meter']  	= $row['item_sell_price_per_meter'];
					$result['decorate_price_per_meter']  	= $row['decorate_price_per_meter'];
					$result['is_paid']  					= $row['is_paid'];
					$result['sell_item_notes']  			= $row['sell_item_notes'];
					$result['product_name']  				= $row['product_name'];
					$result['customer_id_fk']  				= $row['customer_id_fk'];
					$result['customer_full_name']  			= $row['customer_full_name'];
					$result['customer_phone']  				= $row['customer_phone'];
					$result['customer_phone2']  			= $row['customer_phone2'];
					$result['customer_address']  			= $row['customer_address'];
					$result['total_paid']  					= $row['total_paid'];
					$result['product_id_fk']  				= $row['product_id_fk'];
					$result['other_id_fk']  				= $row['other_id_fk'];
					$result['sell_item_id']  				= $row['sell_item_id'];
					$result['sell_item_notes']  			= $row['sell_item_notes'];
					$result['total_discount']  				= $row['total_discount'];
					$result['total_remained']  				= $row['total_invoice_price'] - $row['total_paid'];
					$result['total_length']  				= $row['total_length'];
					
					array_push($op['invoice'],$result);


					$res1 = $db->query("SELECT 	sell_item_detail_id, 
												sell_item_detail_height, 
												sell_item_detail_width, 
												is_part_sold, 
											  	damage_length, 
											  	part_status,
											  	invoice_id_fk,
											  	sell_item_id_fk
												  
											FROM sell_item_detail_archive SID
								 			WHERE SID.invoice_id_fk='$id' AND SID.sell_item_id_fk='$row[sell_item_id]' AND part_status=0 AND SID.is_deleted = 0");
						
					while ($row1 = $res1->fetch_assoc()) {
						$result1['sell_item_detail_id']  	= $row1['sell_item_detail_id'];
						$result1['sell_item_detail_height'] = $row1['sell_item_detail_height'];
						$result1['sell_item_detail_width']  = $row1['sell_item_detail_width'] ;
						$result1['is_part_sold']  			= $row1['is_part_sold'];
						$result1['damage_length'] 			= $row1['damage_length'];
						$result1['part_status']  			= $row1['part_status'];
						$result1['sell_item_id_fk']  		= $row1['sell_item_id_fk'];
						
						array_push($op['details'],$result1);
					}
						
				}

				// we will load totals of all customer account


				$id = $result['customer_id_fk'];

				$res = $db->query("SELECT 	customer_id,
									  		customer_full_name,
									  		CONCAT(COALESCE(customer_phone,''),';', COALESCE(customer_phone2,''))	AS phones, 

									  		(

						          			COALESCE((SELECT sum(invoice_total_price_actual) FROM invoice I 
									        WHERE  I.customer_id_fk = C.customer_id AND I.invoice_status <3  AND I.is_deleted = 0),0)
										   
											+

												COALESCE((SELECT sum(withdraw_amount) FROM capital_withdraw CW1 WHERE  CW1.is_deleted = 0  AND CW1.gived_to_id_fk = C.customer_id ),0)
											
											
											) as total_invoice_price,



											(
												COALESCE((SELECT sum(paid_amount) FROM payments IP1 
		       									
		        								WHERE IP1.is_returned=0 AND IP1.is_deleted =0 AND IP1.customer_id_fk = C.customer_id AND IP1.payment_transaction ='Payment' ),0)
		        							

		        							) as total_Paid,

		        							(
		        								SELECT sum(paid_amount) FROM payments IP WHERE IP.customer_id_fk = C.customer_id AND IP.is_returned=0 AND IP.is_deleted =0 AND IP.payment_transaction ='Discount'
		        							) AS total_payment_discount,

											(
												SELECT sum(paid_amount) FROM payments IP WHERE IP.customer_id_fk = C.customer_id AND IP.is_returned=0 AND IP.is_deleted =0 AND IP.payment_transaction ='Return'
											) AS total_payment_return
        						
							



									FROM customer C 
									WHERE C.is_deleted=0 AND C.customer_id = $id ");

				while ($row = $res->fetch_assoc()) {
				   
					
					$result2['total_invoice_price_all']  = $row['total_invoice_price'];
					$result2['total_Paid_all']  			= $row['total_Paid'];
					$result2['total_payment_discount'] 	= $row['total_payment_discount'];
					$result2['total_payment_return']  	= $row['total_payment_return'];
					$result2['total_remained_all']  		= $row['total_invoice_price'] - $row['total_Paid'] - $row['total_payment_discount'] + $row['total_payment_return'];
					

					array_push($op['account'],$result2);
						
				}	


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


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

			$id = $_POST['id'];


			$res = $db->query("SELECT invoice_id, 
									  invoice_number, 
									  invoice_date, 
									  invoice_status, 
									  invoice_usd_rate, 
									  invoice_currency,
									  invoice_total_price_actual, 
									  invoice_note, 
									  sell_item_quantity, 
									  customer_id_fk, 
									  sell_discount, 
									  sell_discount_type, 
									  actual_discount,
									  item_sell_price_per_meter,
									  decorate_price_per_meter, 
									  is_paid,
									  sell_item_notes, 
									  S.item_type_id_fk, 
									  other_id_fk, 
									  S.product_id_fk, 
									  sell_item_id,
									  customer_full_name,
									  customer_email,
									  customer_phone,
									  customer_address, 
									  CONCAT(COALESCE(erbil_code,''), COALESCE(other_name,'')) AS product_name,

									(SELECT sum(paid_amount) FROM invoice_payment IP WHERE I.invoice_id = IP.invoice_id_fk AND IP.is_returned=0 AND IP.is_deleted =0) AS total_paid,


									(invoice_total_price_actual) as total_invoice_price,




									(SELECT sum(actual_discount) FROM sell_item_archive SI WHERE I.invoice_id = SI.invoice_id_fk AND SI.is_deleted =0) AS total_discount,

									(SELECT sum(sell_item_detail_height) FROM sell_item_detail_archive SID WHERE SID.invoice_id_fk = '$id' AND SID.sell_item_id_fk=S.sell_item_id AND part_status=0 AND SID.is_deleted = 0) AS total_length,

									(SELECT sum(sell_item_detail_height * sell_item_detail_width) FROM sell_item_detail_archive SID WHERE SID.invoice_id_fk = '$id' AND SID.sell_item_id_fk=S.sell_item_id AND part_status=0 AND SID.is_deleted = 0 AND SID.sell_item_detail_width > 0) AS total_area,

									COALESCE((SELECT sum(sell_item_detail_width) FROM sell_item_detail_archive SID WHERE SID.invoice_id_fk = '$id' AND SID.sell_item_id_fk=S.sell_item_id AND part_status=0 AND SID.is_deleted = 0 AND SID.sell_item_detail_width > 0),0) AS total_decor_width

								FROM invoice_archive I
							 		LEFT JOIN sell_item_archive S ON (S.invoice_id_fk = I.invoice_id AND S.is_deleted=0)
							 		LEFT JOIN others O ON (S.other_id_fk = O.other_id AND S.other_id_fk >0)
									LEFT JOIN product P ON (S.product_id_fk = P.product_id )
									LEFT JOIN customer C ON (I.customer_id_fk = C.customer_id )
							 	WHERE I.invoice_id='$id'  AND I.is_deleted = 0");
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array('invoice'=>array(),'details'=>array(),'account'=>array());
				while ($row = $res->fetch_assoc()) {
				   
					$result['invoice_id']  					= $row['invoice_id'];
					$result['invoice_number']  				= $row['invoice_number'];
					$result['invoice_date']  				= date('Y-m-d',strtotime($row['invoice_date'])) ;
					$result['invoice_status']  				= $row['invoice_status'];
					$result['invoice_usd_rate']  			= $row['invoice_usd_rate'];
					$result['invoice_currency']  			= $row['invoice_currency'];
					$result['invoice_total_price_actual'] 	= $row['total_invoice_price'];
					$result['invoice_note']  				= $row['invoice_note'];
					$result['sell_item_quantity']  			= $row['sell_item_quantity'];
					$result['sell_discount']  				= $row['sell_discount'];
					$result['sell_discount_type']  			= $row['sell_discount_type'];
					$result['actual_discount']  			= $row['actual_discount'];
					$result['item_sell_price_per_meter']  = $row['item_sell_price_per_meter'];
					$result['decorate_price_per_meter']  	= $row['decorate_price_per_meter'];
					$result['is_paid']  					= $row['is_paid'];
					$result['sell_item_notes']  			= $row['sell_item_notes'];
					$result['product_name']  				= $row['product_name'];
					$result['customer_id_fk']  				= $row['customer_id_fk'];
					$result['customer_full_name']  			= $row['customer_full_name'];
					$result['customer_phone']  				= $row['customer_phone'];
					$result['customer_address']  			= $row['customer_address'];
					$result['total_paid']  					= $row['total_paid'];
					$result['product_id_fk']  				= $row['product_id_fk'];
					$result['other_id_fk']  				= $row['other_id_fk'];
					$result['sell_item_id']  				= $row['sell_item_id'];
					$result['total_discount']  				= $row['total_discount'];
					$result['total_remained']  				= $row['total_invoice_price'] - $row['total_paid'];
					$result['total_length']  				= $row['total_length'];
					$result['total_area']  					= $row['total_area'];
					$result['total_decor_width']  			= $row['total_decor_width'];

					
					array_push($op['invoice'],$result);


					$res1 = $db->query("SELECT 	sell_item_detail_id, 
												sell_item_detail_height, 
												sell_item_detail_width, 
												is_part_sold, 
											  	damage_length, 
											  	part_status,
											  	invoice_id_fk,
											  	sell_item_id_fk
											  
										FROM sell_item_detail_archive SID
							 			WHERE SID.invoice_id_fk='$id' AND SID.sell_item_id_fk='$row[sell_item_id]' AND part_status=0 AND SID.is_deleted = 0");
						
					while ($row1 = $res1->fetch_assoc()) {
								
						$result1['sell_item_detail_id']  	= $row1['sell_item_detail_id'];
						$result1['sell_item_detail_height'] = $row1['sell_item_detail_height'];
						$result1['sell_item_detail_width']  = $row1['sell_item_detail_width'] ;
						$result1['is_part_sold']  			= $row1['is_part_sold'];
						$result1['damage_length'] 			= $row1['damage_length'];
						$result1['part_status']  			= $row1['part_status'];
						$result1['sell_item_id_fk']  		= $row1['sell_item_id_fk'];
						
						array_push($op['details'],$result1);
					}
						
				}


				// we will load totals of all customer account

				$id = $result['customer_id_fk'];

				$res = $db->query("SELECT customer_id,
									  customer_full_name,
									  CONCAT(COALESCE(customer_phone,''),';', COALESCE(customer_phone2,''))	AS phones,  

									
									(

				          				COALESCE((SELECT sum(invoice_total_price_actual) FROM 
				          				 invoice I 
								        WHERE  I.customer_id_fk = C.customer_id AND I.invoice_status <3  AND I.is_deleted = 0),0)
								   

									+

										COALESCE((SELECT sum(withdraw_amount) FROM capital_withdraw CW1 WHERE  CW1.is_deleted = 0  AND CW1.gived_to_id_fk = C.customer_id ),0)
									
									) as total_invoice_price,



									(
										COALESCE((SELECT sum(paid_amount) FROM payments IP1 
       									
        								WHERE IP1.is_returned=0 AND IP1.is_deleted =0 AND IP1.customer_id_fk = C.customer_id AND IP1.payment_transaction ='Payment' ),0)
        							

        							) as total_Paid,

        							(
        								SELECT sum(paid_amount) FROM payments IP WHERE IP.customer_id_fk = C.customer_id AND IP.is_returned=0 AND IP.is_deleted =0 AND IP.payment_transaction ='Discount'
        							) AS total_payment_discount,

									(
										SELECT sum(paid_amount) FROM payments IP WHERE IP.customer_id_fk = C.customer_id AND IP.is_returned=0 AND IP.is_deleted =0 AND IP.payment_transaction ='Return'
									) AS total_payment_return
        						
							



									FROM customer C 
									WHERE C.is_deleted=0 AND C.customer_id = $id");

				while ($row = $res->fetch_assoc()) {
				   
					
					$result['total_invoice_price_all']  = $row['total_invoice_price'];
					$result['total_Paid_all']  			= $row['total_Paid'];
					$result['total_payment_discount'] 	= $row['total_payment_discount'];
					$result['total_payment_return']  	= $row['total_payment_return'];
					$result['total_remained_all']  		= $row['total_invoice_price'] - $row['total_Paid'] - $row['total_payment_discount'] + $row['total_payment_return'];
					

					array_push($op['account'],$result);
						
				}

				echo json_encode($op);
				exit;
			}else{
				echo 0;
				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;
			}
		}

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