<?php
ob_start();
session_start();

 ini_set('session.bug_compat_warn', 0);
 ini_set('session.bug_compat_42', 1);
 include("db.php");
 
 $delete_sql="TRUNCATE sms";
 $delete_query=$db ->query($delete_sql);
 
 $get_id=$_GET['id'];
  
 //transationInfo
 echo $transaction_info_sql="select * from customerpayment where id='$get_id'";
  
 $transaction_info_query= $db ->query($transaction_info_sql);
 $transaction_info_row=$transaction_info_query ->fetch_array();
 $customer_id=$transaction_info_row['customerId'];
 $transaction_info_dr_cr=$transaction_info_row['transacttype'];
 $transaction_info_transactCurrency=$transaction_info_row['transactCurrency'];
 $main_amoun=$transaction_info_row['amount'];
 $transaction_info_amount=number_format($main_amoun);
 $transaction_info_description=$transaction_info_row['description'];
 $date=$transaction_info_row['date'];
 
 $info_customer_sql="select * from customers where id='$customer_id'";
 $info_customer_query= $db ->query($info_customer_sql);
 $info_customer_row=$info_customer_query ->fetch_array();
 
 $tell=$info_customer_row['phone'];
 
 //currency
 $currency_sql="select * from currencies where id='$transaction_info_transactCurrency'";
 $currency_query= $db ->query($currency_sql);
 $currency_row=$currency_query ->fetch_array();
 $currency=$currency_row['name'];
 //currency
    
 if($transaction_info_dr_cr==1){$type= "رسیدگی";}
 if($transaction_info_dr_cr==2){$type="بردگی";}
 
  $insert_first_sql="insert into sms(customer_id,date,dr_cr,currency,amount,tell,update_id,description) values ('$customer_id','$date','$type','$currency','$main_amoun','$tell','$get_id','$transaction_info_description')";
  $insert_first_query= $db ->query($insert_first_sql);
  
//============================================================================== 
    //**************select currency******************************
	//avordegi Pool afn
	$avordegi_afn_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=1 and currency_id='1' ";
	$avordegi_afn_query= $db ->query($avordegi_afn_sql);
	$avordegi_afn_row=$avordegi_afn_query ->fetch_array();
	$avordegi_afn=$avordegi_afn_row['amount'];
	//$avordegi_afn_sql 
	//bordegi Pool afn
	$bordegi_afn_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=2 and currency_id='1'  ";
	$bordegi_afn_query= $db ->query($bordegi_afn_sql);
	$bordegi_afn_row=$bordegi_afn_query ->fetch_array();
	$bordegi_afn=$bordegi_afn_row['amount'];
	//$bordegi 
	$balance_afn=($avordegi_afn)-$bordegi_afn;
	if($balance_afn>0){$type="رسیدگی";}
	if($balance_afn<0){$type="بردگی";}
    if($balance_afn!=0){$des_afn="حساب فعلی افغانی : ".number_format($balance_afn)."(".$type.")*";}
	//****************************************
 
	//avordegi Pool usd
	$avordegi_usd_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=1 and currency_id='2' ";
	$avordegi_usd_query= $db ->query($avordegi_usd_sql);
	$avordegi_usd_row=$avordegi_usd_query ->fetch_array();
	$avordegi_usd=$avordegi_usd_row['amount'];
	//$avordegi_usd_sql 
	//bordegi Pool usd
	$bordegi_usd_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=2 and currency_id='2'  ";
	$bordegi_usd_query= $db ->query($bordegi_usd_sql);
	$bordegi_usd_row=$bordegi_usd_query ->fetch_array();
	$bordegi_usd=$bordegi_usd_row['amount'];
	//$bordegi 
	$balance_usd=($avordegi_usd)-$bordegi_usd;
	if($balance_usd>0){$type="رسیدگی";}
	if($balance_usd<0){$type="بردگی";}
	
	 if($balance_usd!=0){$des_usd="حساب فعلی دالر : ".number_format($balance_usd)."(".$type.")*";}
	//****************************************
	//****************************************
 
	//avordegi Pool eruro
	$avordegi_euro_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=1 and currency_id='3' ";
	$avordegi_euro_query= $db ->query($avordegi_euro_sql);
	$avordegi_euro_row=$avordegi_euro_query ->fetch_array();
	$avordegi_euro=$avordegi_euro_row['amount'];
	//$avordegi_euro 
	//bordegi Pool euro
	$bordegi_euro_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=2 and currency_id='3'  ";
	$bordegi_euro_query= $db ->query($bordegi_euro_sql);
	$bordegi_euro_row=$bordegi_euro_query ->fetch_array();
	$bordegi_euro=$bordegi_euro_row['amount'];
	//$bordegi 
	$balance_euro=($avordegi_euro)-$bordegi_euro;
	
	if($balance_euro>0){$type="رسیدگی";}
	if($balance_euro<0){$type="بردگی";}
	
	if($balance_euro!=0){$des_euro="حساب فعلی یورو : ".number_format($balance_euro)."(".$type.")*";}
	//****************************************
	//****************************************
 
	//avordegi Pool pkr
	$avordegi_pkr_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=1 and currency_id='4' ";
	$avordegi_pkr_query= $db ->query($avordegi_pkr_sql);
	$avordegi_pkr_row=$avordegi_pkr_query ->fetch_array();
	$avordegi_pkr=$avordegi_pkr_row['amount'];
	//$avordegi_pkr_sql 
	//bordegi Pool pkr
	$bordegi_pkr_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=2 and currency_id='4'  ";
	$bordegi_pkr_query= $db ->query($bordegi_pkr_sql);
	$bordegi_pkr_row=$bordegi_pkr_query ->fetch_array();
	$bordegi_pkr=$bordegi_pkr_row['amount'];
	//$bordegi 
	$balance_pkr=($avordegi_pkr)-$bordegi_pkr;
	
	if($balance_pkr>0){$type="رسیدگی";}
	if($balance_pkr<0){$type="بردگی";}
	
   if($balance_pkr!=0){$des_pkr="حساب فعلی کالدار : ".number_format($balance_pkr)."(".$type.")*";}
	//****************************************
	//****************************************
 
	//avordegi Pool Irr
	$avordegi_irr_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=1 and currency_id='5' ";
	$avordegi_irr_query= $db ->query($avordegi_irr_sql);
	$avordegi_irr_row=$avordegi_irr_query ->fetch_array();
	$avordegi_irr=$avordegi_irr_row['amount'];
	//$avordegi_irr
	//bordegi irr
	$bordegi_irr_sql="select sum(amount) as amount from customer_statement where customerId='$customer_id' and transaction_type=2 and currency_id='5'  ";
	$bordegi_irr_query= $db ->query($bordegi_irr_sql);
	$bordegi_irr_row=$bordegi_irr_query ->fetch_array();
	$bordegi_irr=$bordegi_irr_row['amount'];
	//$bordegi 
	$balance_irr=($avordegi_irr)-$bordegi_irr;
	if($balance_irr>0){$type="رسیدگی";}
	if($balance_irr<0){$type="بردگی";}
	if($balance_irr!=0){$des_irr="حساب فعلی تومان : ".number_format($balance_irr)."(".$type.")*";}
	//****************************************
	
	//==========================================================================						 
 
	//******************************Currency Name*******************************
	$buycurrency_sql="select name from  `currencies` where id='$currencyId'";
	$buycurrency_query=$db ->query($buycurrency_sql);
	$buycurrency_row=$buycurrency_query ->fetch_array();
	$buycurrency=$buycurrency_row['name'];	
	//=============================================================
	//=============================================================
	$ha_pool=$des_afn.$des_usd.$des_euro.$des_pkr.$des_irr;
	
	 $pool_sql="update sms set pool='$ha_pool'";
	$pool_query= $db ->query($pool_sql);
 
	 
    header("location: send_to_whatsapp.php?customer_id=$customer_id");
    
?>