<?php
include 'get_user_info.php';

//  set default time format
date_default_timezone_set('Asia/Kolkata');      
$date=date("Y-m-d H:i:s");      //  for 24 hour
$user_id=$_REQUEST['user_idd'];
$balance=$_REQUEST['amount'];
$admin_id=$_REQUEST['take_user_id'];
$remarks=mysqli_real_escape_string($conn,$_REQUEST['remarks']);

   $get_transaction_id=get_unique_transaction_id();
   $admin_total_balace=get_admin_total_balance();  //  get admin total wallet balance
  $insert="insert into admin_wallet_details(wallet_user_id,receiver_amount,cr_dr,closer_amount,wallet_transaction_id,wallet_remarks,wallet_date)
            values('$user_id','$balance','cr','$admin_total_balace'+'$balance','$get_transaction_id','Deduct money from user','$date')";
  $con=mysqli_query($conn,$insert);
  $qry6 ="insert into wallet(receiver_id,balance,sender_id,source,transaction_date,transaction_id,remarks,pending)
         value('$admin_id','$balance','$user_id','$remarks','$date','$get_transaction_id','$remarks','1')";
  $exe=mysqli_query($conn,$qry6);
   if($exe)
   {
        //   amount addded into account
           $qry2="update wallet_balance set total_balance=total_balance+'$balance' where balance_user_id='$admin_id'  ";
           $con3=mysqli_query($conn,$qry2);
           if($con3)
           {
               //echo 'success';
           }
           //  amount deducted from this account
           $qry5="update wallet_balance set total_balance=total_balance-'$balance' where balance_user_id='$user_id'  ";
           $con5=mysqli_query($conn,$qry5);
           if($con5)
           {
              // echo 'success';
           }
       
       echo '<script> alert("Money deducted successfully"); window.location="deduct_money.php"; </script>';
   }
   else
   {
        echo '<script> alert("Money not deduct"); window.location="deduct_money.php"; </script>';
   }
   
   function get_unique_transaction_id(){
       include 'include/db.php';
       $qr2="select MAX(transaction_id) from wallet LIMIT 1";
        $con2=mysqli_query($conn,$qr2);
        if(mysqli_num_rows($con2)>0)
        {
        	while($rw=mysqli_fetch_array($con2))
        	{
        		 $xyz=$rw['MAX(transaction_id)'];
        		 $ap=substr($xyz,2);
        		 $old_unique_id=intval($ap);  //  convert string to int
        	}
        	for ($num = $old_unique_id; $num <= $old_unique_id+1; $num++)
            $net_val=sprintf("%'.06d", $num);
            return 'WT'.$net_val;
        }
   }
   
   
?>