<?php
 include("db.php");
 
  $money_type_sql="select * from currencies";
  $money_type_query= $db ->query($money_type_sql);
  while($money_type_row=$money_type_query->fetch_array()){
  
  $money_type=$money_type_row['id'];  
 
 
 
 $customerId_sql="select * from customers where remove='0'";
 $customerId_query=$db ->query($customerId_sql);
 while($customerId_row=$customerId_query ->fetch_array()){
 //echo "customerID:";
 $customerId=$customerId_row['id'];
 
  
 
  echo "customerOpeningBalance:";
  $customerOpeningBalance_sql="select * from customeropeningbalance where customerId='$customerId' and currencyId='$money_type' and  remove='0'  ";
  $customerOpeningBalance_query= $db ->query($customerOpeningBalance_sql);
  $customerOpeningBalance_row=$customerOpeningBalance_query ->fetch_array();
  echo $customerOpeningBalance=$customerOpeningBalance_row['amount'];
  
  echo "<br>customerRecive:";
  $customerRecive_sql="select sum(amount) from customerpayment where customerId='$customerId' and transacttype='1' and transactCurrency='$money_type' and  remove='0' ";
  $customerRecive_query= $db ->query($customerRecive_sql);
  $customerRecive_row=$customerRecive_query ->fetch_array();
  echo $customerRecive=$customerRecive_row['sum(amount)'];
  echo "<br>CustomerPayment:";
  
  $customerPayment_sql="select sum(amount) from customerpayment where customerId='$customerId' and transacttype='2' and transactCurrency='$money_type' and  remove='0'  ";
  $customerPayment_query= $db ->query($customerPayment_sql);
  $customerPayment_row=$customerPayment_query ->fetch_array();
  $customerPayment=$customerPayment_row['sum(amount)'];
  echo "<br>Result:";
  
  echo $result=($customerOpeningBalance+$customerRecive)-$customerPayment;
  
  $update_sql="update customerbalance set amount='$result' where customerId='$customerId' and currencyId='$money_type'";
  $update_query=$db ->query($update_sql);  
  echo "<br>";
  
   }
  
  
  }

   



?>