<?php
ob_start();
ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 1);

 include("db.php");
 include("../pdate.php");
     $get_id=$_GET['editID'];
    
     $table=$_GET['table'];
   
     $customerRetriveSql="select * from `$table` where id='$get_id'   ";
	 $customerRetriveQuery=$db ->query($customerRetriveSql);
	 $customerRetriveRow=$customerRetriveQuery ->fetch_array();
	 $dateretrive=$customerRetriveRow['date'];
	 $referenceNoretrive=$customerRetriveRow['referenceNo'];
	 $status=$customerRetriveRow['transacttype'];
	 $transactCurrency_retrive=$customerRetriveRow['transactCurrency'];
	  
	 $customerassetretrive=$customerRetriveRow['customerasset'];
	 $amount_retrive=$customerRetriveRow['amount'];
	 $dollarTotal_retrive=$customerRetriveRow['dollarTotal'];
	 $description_retrive=$customerRetriveRow['description'];
	 $customerId_retrive=$customerRetriveRow['customerId'];
	 
	 
	 //=========================Back Asset================================
	      $oldAssetSql="select * from assets where currencies_id='$transactCurrency_retrive'";
	      $oldAssetQuery= $db ->query($oldAssetSql);
		  $oldAssetRow=$oldAssetQuery ->fetch_array();
		  $oldAmount=$oldAssetRow['amount'];
		   
		  $oldDollarAmount=$oldAssetRow['dollarAmount'];
		   
		  if($status==1){
		     $newAmount=$oldAmount-$amount_retrive;
		     
			 $newDollarAmount=$oldDollarAmount-$dollarTotal_retrive;
		    
		  } if($status==2){
		     $newAmount=$oldAmount+$amount_retrive;
		     $newDollarAmount=$oldDollarAmount+$dollarTotal_retrive;
		  
		  }
		  
		  $backUpdateAssetSql="update assets set amount='$newAmount',dollarAmount='$newDollarAmount' where currencies_id='$transactCurrency_retrive'";
	      $backUpdateAssetQuery=$db ->query($backUpdateAssetSql);
           
	 //=========================End of Back Asset================================ 
	 //=========================Back CustomerBalance================================
	      $oldCustomerSql="select * from customerbalance where customerId='$customerId_retrive' and currencyId='$transactCurrency_retrive'"; 
		  $oldCustomerQuery= $db ->query($oldCustomerSql);
		  $oldCustomerRow=$oldCustomerQuery ->fetch_array();
		   $oldCustomerAmount=$oldCustomerRow['amount'];
		   
		   $oldCustomerDollarAmount=$oldCustomerRow['dollarAmount'];
		  
		  if($status==1){
		    $oldCustomerAmounUpdate=$oldCustomerAmount-$amount_retrive;
		    
		    $oldCustomerdollarAmounUpdate=$oldCustomerDollarAmount-$dollarTotal_retrive;
		  
		  } if($status==2){
		    $oldCustomerAmounUpdate=$oldCustomerAmount+$amount_retrive;
		    $oldCustomerdollarAmounUpdate=$oldCustomerDollarAmount+$dollarTotal_retrive;
		  
		  }
		   
		  $backUpdateCustomerSql="update customerbalance set amount='$oldCustomerAmounUpdate',dollarAmount='$oldCustomerdollarAmounUpdate' where currencyId='$transactCurrency_retrive' and customerId='$customerId_retrive'";
	      $backUpdateCustomerQuery=$db ->query($backUpdateCustomerSql);
          
	//=========================End of CustomerBalance================================
	 
	   $removeSql="update customerpayment set remove='1' where id='$get_id'";
	   $removeQuery= $db ->query($removeSql);
	   
	   //delete statement
	    $deleteStatement = $db->query("delete from customer_statement where transaction_id ='$get_id' and reference ='معاملات مشتری'");
	   //delete journal
	   $deleteJoournal = $db->query("delete from journal where reference_id ='$get_id' and reference ='بردگی مشتری'");
	    header("location:../report/customerReciveReport.php");	
?>  