<head>
<script src="dist/sweetalert-dev.js"></script>
<link rel="stylesheet" href="dist/sweetalert.css">
</head>
<?php
session_start();
if($_POST['submit']){
 include("db.php");
 
	   $day = $_POST['day'];
	  $month = $_POST['month'];
	  $year = $_POST['year'];
	//echo "Date:";
	 $date=$year."-".$month."-".$day;
   // echo "<br>Transaction Type:";
	$transactType=$_POST['transactType'];
	//echo "<br>CustomerID:";
    $customerId=$_POST['customerId'];
	//echo "<br>Description:";
	$description=$_POST['desc'];
	//echo "<br>CurrencyID:"; 
	$currencyId=$_POST['basecurrency'];
    //echo "<br>Opening Balance:"; 
    $openingBalanceP=$_POST['openingBalance'];
    $openingBalance = preg_replace('/[^\d\.-]/', '', $openingBalanceP);
    echo "<br>";
	
	//======================================================		
	 $dollarRatePriceRow=$_POST['dollarRatePrice'];
	 $dollarRatePriceRowmix=split("/",$dollarRatePriceRow);
	 $first_dollarRatePrice =$dollarRatePriceRowmix[0];
	 $second_dollarRatePrice=$dollarRatePriceRowmix[1]; 
	 
	 if($first_dollarRatePrice=='1'){
	          
             $unit_dollarRate   =$first_dollarRatePrice; 	  
			 $dollarRatePrice = $second_dollarRatePrice;		  
	   }else{
	         
	         $unit_dollarRate=$second_dollarRatePrice;  
			 $dollarRatePrice= $first_dollarRatePrice;
		  
	   }
	  
	//=====================================================
	$totalDollarP=$_POST['totalDollar'];
    $totalDollar = preg_replace('/[^\d\.-]/', '', $totalDollarP);
     
     if($transactType=='1'){
	        $openingBalance; 
           		  
	        $totalDollar;
	 }
	 if($transactType=='2'){
	        $openingBalance=-$openingBalance;  
	         
			$totalDollar=-$totalDollar;
	 
	 }
	  if(strlen(trim($customerId))==0 or strlen(trim($transactType))==0 or strlen(trim($currencyId))==0 or strlen(trim($openingBalance))==0 or strlen($date)==0){
	  
		  
		echo' <script>
					 sweetAlert("اخطا ر... ", "لطفا جاهای خالی را پرکنید !", "error");
				  </script>';

        echo "<meta http-equiv='refresh' content ='1; url=openingBalance.php?customerID=$customerId' >";
		die;
		  
		  
	  }
 //================================================================================
	 //if(strlen(trim($transactType))==0 or strlen(trim($openingBalance))==0 or strlen(trim($first_dollarRatePrice))==0){
	 if(strlen(trim($transactType))==0 or strlen(trim($openingBalance))==0 ){
		 echo' <script>
					 sweetAlert("اخطار...", " لطفا قیمت پول مورد نظر را نسبت به دالر مشخص کنید!", "error");

				  </script>'; 
				  echo '<meta http-equiv="refresh" content ="1.70; url=assetRegistration.php" >';	
				  echo"<span style='color: red;font-size: 20px;text-align: center'>لطفا قیمت پول مورد نظر را به دالر مشخص کنید</span>";
				 die;
		}
		 
		 //$dollarRatePrice=($unit_dollarRate*1)/($dollarRatePrice);
   //===============checking========================
      $checking_Sql="select * from customeropeningbalance where customerId='$customerId' and currencyId='$currencyId' and remove =0 ";
      $checking_Query=$db ->query($checking_Sql);
	  $checking_Row=$checking_Query ->fetch_array();
	  $checking=$checking_Row['currencyId'];
	  if(isset($checking)){
	    echo' <script>
					sweetAlert("اخطار...", "اطلاعات وارده در سیستم موجود است", "error");
			   </script>'; 
			 echo "<meta http-equiv='refresh' content ='1; url=openingBalance.php?customerID=$customerId' >";
			   // header("location: add_customer.php");
			 die;
	  
	  
	  }
	  else{
	        $checking_Sql="select * from customerbalance where customerId='$customerId' and currencyId='$currencyId' ";
            $checking_Query=$db ->query($checking_Sql);
	        $checking_Row=$checking_Query ->fetch_array();
			$amount=$checking_Row['amount'];
			$dollarAmount=$checking_Row['dollarAmount'];
			if(isset($amount)){
	        
		      $newAmount=$amount+$openingBalance;
			  $newAmountdollar=$dollarAmount+$totalDollar;
		      
			  $updateCustomerBalance="update customerbalance set amount='$newAmount',dollarAmount='$newAmountdollar'  where customerId='$customerId' and currencyId='$currencyId'  ";
		      $updateCustomerBalanceQuery=$db ->query($updateCustomerBalance);
		       //===============================================

               $insertOpening_sql="insert into customeropeningbalance (date,customerId,currencyId,amount,rate,dollarAmount,description)
      		   values('$date','$customerId','$currencyId','$openingBalance','$dollarRatePrice','$totalDollar','$description')";
		       $insertOpening_Query= $db ->query($insertOpening_sql);
			   if($insertOpening_Query){
				   //select last Id
				   $selectOpLastId = "select id from customeropeningbalance order by id desc limit 1";
				   $selectOpLastIdquery = $db->query($selectOpLastId);
				   $selectOpLastIdRow = $selectOpLastIdquery->fetch_array();
				   $OpLastId = $selectOpLastIdRow['id'];
				   
				   //customer statment insertion
				   if($openingBalance<0){
					   //$openingBalance=-($openingBalance);
					   $openingBalance=($openingBalance);
				   }
				   $insertStatement ="insert into customer_statement
				  (reference,transaction_id,customerId,currency_id,amount,date,transaction_type,description)values('بلانس افتتاحیه','$OpLastId','$customerId','$currencyId','$openingBalance','$date','$transactType','$description')";
					 $iinsertStatementQuery = $db->query($insertStatement);
		  
				   
			   }



			   
		       //===============================================   





				  echo' <script>
			 	 
					
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
				</script>';
	             
				echo "<meta http-equiv='refresh' content ='1; url=openingBalance.php?customerID=$customerId' >";
			 
			 
			 die;
		    }
			if(!isset($amount)){
			   $insert_sql="insert into customerbalance (customerId,currencyId,amount,dollarAmount)
      		   values('$customerId','$currencyId','$openingBalance','$totalDollar')";
		       $insert_Query= $db ->query($insert_sql);
			   
			   
			   $insertOpening_sql="insert into customeropeningbalance (date,customerId,currencyId,amount,rate,dollarAmount,description)
      		   values('$date','$customerId','$currencyId','$openingBalance','$dollarRatePrice','$totalDollar','$description')";
		       $insertOpening_Query= $db ->query($insertOpening_sql);
			   if($insertOpening_Query){
				   
				//select last Id
			   $selectOpLastId = "select id from customeropeningbalance order by id desc limit 1";
			   $selectOpLastIdquery = $db->query($selectOpLastId);
			   $selectOpLastIdRow = $selectOpLastIdquery->fetch_array();
			   $OpLastId = $selectOpLastIdRow['id'];
			   
			   //customer statment insertion
			   $insertStatement ="insert into customer_statement
			  (reference,transaction_id,customerId,currency_id,amount,date,transaction_type,description)values('بلانس افتتاحیه','$OpLastId','$customerId','$currencyId','$openingBalance','$date','$transactType','$description')";
				 $iinsertStatementQuery = $db->query($insertStatement);
				   
			   }
               
			    echo' <script>
			 	 
					
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
				</script>';
	            
				echo "<meta http-equiv='refresh' content ='1; url=openingBalance.php?customerID=$customerId' >";
			}
		   
	       
	       
	  
	  }
   //===============End of checking========================
   
   
   
}

?>