<?php
   $start_Date="1397-01-01";
   $endDate="1400-09-30";
include("../db.php");

           $findingCustomer_sql="select * from  customers ";
	       $findingCustomer_Query= $db ->query($findingCustomer_sql);
		   $ii=$countId;
   		  while($findingCustomer_Row=$findingCustomer_Query ->fetch_array()){
             $ii++;			  
		   $customerid=$findingCustomer_Row['id'];
		   $customerName=$findingCustomer_Row['name'];
		   
		    
		   
		  
	      
		   //===========afn====================================
			$cr_customerAmount_afn_sql="select sum(amount) as amount from customer_statement where customerId='$customerid' and transaction_type='1' and (date between '$start_Date' and '$endDate') and currency_id='1' ";
			$cr_customerAmount_afn_query= $db ->query($cr_customerAmount_afn_sql);
            $cr_customerAmount_afn_row=$cr_customerAmount_afn_query ->fetch_array();
            $cr_customerAmount_afn=$cr_customerAmount_afn_row['amount'];
                            
			$dr_customerAmount_afn_sql="select sum(amount) as amount from customer_statement where customerId='$customerid' and transaction_type='2' and (date between '$start_Date' and '$endDate') and currency_id='1' ";
			$dr_customerAmount_afn_query= $db ->query($dr_customerAmount_afn_sql);
            $dr_customerAmount_afn_row=$dr_customerAmount_afn_query ->fetch_array();
            $dr_customerAmount_afn=$dr_customerAmount_afn_row['amount'];

            $findingCurrency_2=$cr_customerAmount_afn-$dr_customerAmount_afn;
            
            $date="1400-03-16";
            $customerid;			
            $currencyId=1;
            $amount=$findingCurrency_2;			
           if($amount!=0){
		    echo $insert_sql="insert into customeropeningbalance_2 (date,customerId,currencyId,amount) 
			values
			('$date','$customerid','$currencyId','$amount')";
			$insert_query= $db ->query($insert_sql);
		    echo "<br>";
		   }
		   }
		   
		   ?>