<?php

 session_start();

?>
<head>
<script src="dist/sweetalert-dev.js"></script>
<link rel="stylesheet" href="dist/sweetalert.css">
</head>
<?php
    include("db.php");
    $length=$_POST['length'];
    $date=$_POST['date'];
	 
    /* $day=$_POST['day'];
    $month=$_POST['month'];
    $year=$_POST['year'];
    $date=$year."-".$month."-".$day ; */
     	  
	$delete_Sql="delete from dailyasset where date='$date'";
    $delete_Query= $db ->query($delete_Sql);
	
	//===============DELETE DAILY PROFIT====================================
	$deleteDaillyProfit_Sql="delete from dailyprofit where date='$date'";
    $deleteDaillyProfit_Query= $db ->query($deleteDaillyProfit_Sql);
	
	
	//===============DELETE DAILY PROFIT====================================
	  
//============================================================================	 
 for($i=0;$i<$length;$i++){
 
   
   
    
     $amountp=$_POST['amount'][$i];
	 $amount = preg_replace('/[^\d\.-]/', '', $amountp);
     //echo "<br>";
	 $currency_and_rate=$_POST['currency_and_rate'][$i];
	 $currencyId=$_POST['currencyId'][$i];
    // echo "<br>==================================</br>";
     $ratep=$_POST['rate'][$i];
     $rateTake = preg_replace('/[^\d\.-]/', '', $ratep);
      
      
   
    if(strlen(trim($amount))==0  or strlen(trim($currencyId))==0 or strlen(trim($rateTake))==0){
    
	
	       echo "You have Problem";
      /*   echo' <script>
			 	 
					
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
			  </script>'; */
	    //echo '<meta http-equiv="refresh" content ="1; url=moneyPrice.php" >';
                  
			 
         die;


}
     $courencyName_sql="select id,name from  currencies where id='$currencyId'";
     $courencyName_Query =$db ->query($courencyName_sql);
	 $courencyName_Row=$courencyName_Query ->fetch_array();
	 $courencyId=$courencyName_Row['id'];
	 $courencyName=$courencyName_Row['name'];
      

     if($courencyName=='یورو'){
		 
		  
		$total=$amount*$rateTake; 
		 
	 }if($courencyName!='یورو'){
		
	    $total=$amount/$rateTake; 
		 
	 }
     //$total=$amount/$rate;
	 //echo "<br>";
	
      $totalAsset+=$total;
	   
	 
	              
    $insert_sql="insert into dailyAsset (currencyId,amount,rate,dollarTotal,date) 
                  values 
   ('$currencyId','$amount','$rateTake','$total','$date')";
   echo "<br>";
  $insert_query = $db ->query($insert_sql);
 	     
			} 

			 
			
 include("payable_Receivable_Account.php");	
 	 
	 		$equity=($sumOf_all);
  //===========================INSERT DAILY PROFIT ==================================
               		 
			 $insert_Profit_sql="insert into dailyprofit(totalAsset,dariyaft,pardakht,equity,date) 
							values
							('$totalAsset','$all_dollar_customerResidegi','$all_dollar_customerBordegi','$equity','$date') ";
     
                echo "<br>";
				$insert_Profit_query= $db ->query($insert_Profit_sql);
				
				echo' <script>
			 	 
				
			
				   swal("ذخیره موفق!", "اطلاعات وارده با موفقیت ثبت سیستم شد", "success")
				</script>';			 
           echo '<meta http-equiv="refresh" content ="1; url=select_AssetReport.php" >';
   
   //===========================END OFINSERT DAILY PROFIT=============================
			    
          

 ?>