<?php
session_start();
require("php_con.php");
require("function.php");

if(isset($_POST['Start_balance_end_amount'])){
	$Start_balance_end_amount=mysqli_real_escape_string($connation,$_POST['Start_balance_end_amount']);
	mysqli_query($connation,"UPDATE start_balance_details SET Start_balance_end_amount='$Start_balance_end_amount',Start_balance_end_time='$current_date $current_time' WHERE Start_balance_date='$current_date' AND Start_balance_start_user='$_COOKIE[user_id]'");
}

if(isset($_POST['Start_balance_start_amount'])){
	$Start_balance_start_amount=mysqli_real_escape_string($connation,$_POST['Start_balance_start_amount']);
	mysqli_query($connation,"UPDATE start_balance_details SET Start_balance_start_amount='$Start_balance_start_amount',Start_balance_end_time='$current_date $current_time' WHERE Start_balance_date='$current_date' AND Start_balance_start_user='$_COOKIE[user_id]'");
}

if(isset($_POST['end_btn'])){
	mysqli_query($connation,"UPDATE start_balance_details SET Start_balance_status='1',Start_balance_end_time='$current_date $current_time' WHERE Start_balance_date='$current_date' AND Start_balance_start_user='$_COOKIE[user_id]'");
	echo "<script>window.location='ajax_today_my_summery.php?print';</script>";
}

$current_user_qury=mysqli_query($connation,"SELECT * FROM user_details WHERE user_id='$_COOKIE[user_id]'");
$current_user_resalt=mysqli_fetch_array($current_user_qury);

$total=(float)(start_amount($current_date,$_COOKIE['user_id'])+user_cash_amount($current_date,$_COOKIE['user_id']))-(user_expenses($current_date,$_COOKIE['user_id']));

$check_qury=mysqli_query($connation,"SELECT Start_balance_status FROM start_balance_details WHERE Start_balance_date='$current_date' AND Start_balance_start_user='$_COOKIE[user_id]'");
$check_resalt=mysqli_fetch_assoc($check_qury);

?>
<link href="data_table/css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<?php if($check_resalt['Start_balance_status']==1){ ?>
<p class="text-danger text-center font-weight-bold m-0 p-0">Already closed today</p>
<?php } ?>

Name: <?php echo $current_user_resalt['user_fname']; ?> <?php echo $current_user_resalt['user_lname']; ?><br>
Valid Date: <?php echo $current_date; ?>

<table class="table table-sm table-bordered table-hover mt-3" style="font-size: 14px;">
  <tbody>
    <tr>
      <td>Day Start Cash Amount</td>
      <td class="p-0">		
		<form method="post" class="mb-0">
			<input name="Start_balance_start_amount" type="number" required="required" class="form-control form-control-sm" id="Start_balance_start_amount" style="background-color: rgba(255,255,0,0.2);" step="0.01" autocomplete="off" onClick="this.select();" value="<?php echo number_format((float)start_amount($current_date,$_COOKIE['user_id']),2,".",""); ?>" <?php if($check_resalt['Start_balance_status']==1){echo "disabled";} ?>>
			</form>
	  </td>
    </tr>
    <tr>
      <td>Sale Cash Amount</td>
      <td><?php echo number_format((float)user_cash_amount($current_date,$_COOKIE['user_id']),2); ?></td>
    </tr>
    <tr>
      <td>Sale Card Amount</td>
      <td><?php echo number_format((float)user_card_amount($current_date,$_COOKIE['user_id']),2); ?></td>
    </tr>
    <tr>
      <td>Discount Amount</td>
      <td><?php echo number_format((float)user_discount_amount($current_date,$_COOKIE['user_id'])+user_discount_amount_sale_list($current_date,$_COOKIE['user_id']),2); ?></td>
    </tr>
    <tr>
      <td>Expenses</td>
      <td><?php echo number_format((float)user_expenses($current_date,$_COOKIE['user_id']),2); ?></td>
    </tr>
    <tr class="font-weight-bold" style="background-color: rgba(0,128,0,0.1);">
      <td>Cash Balance</td>
      <td><?php echo number_format((float)(start_amount($current_date,$_COOKIE['user_id'])+user_cash_amount($current_date,$_COOKIE['user_id']))-(user_expenses($current_date,$_COOKIE['user_id'])),2); ?></td>
    </tr>
    <tr>
      <td>Hand Count Cash Amount</td>
      <td class="p-0">
		<form method="post" class="mb-0">
			<input name="Start_balance_end_amount" type="number" required="required" class="form-control form-control-sm" id="Start_balance_end_amount" style="background-color: rgba(255,255,0,0.2);" step="0.01" autocomplete="off" onClick="this.select();" value="<?php echo number_format((float)end_amount($current_date,$_COOKIE['user_id']),2,".",""); ?>" <?php if($check_resalt['Start_balance_status']==1){echo "disabled";} ?>>
		</form>
	  </td>
    </tr>
    <tr>
      <td>Difference</td>
      <td><?php echo number_format((float)(end_amount($current_date,$_COOKIE['user_id']))-$total,2); ?></td>
    </tr>
    <tr>
		
		<?php if($check_resalt['Start_balance_status']==1){ ?>
      <td colspan="2" align="right">
	    <a href="ajax_today_my_summery.php?print" class="btn btn-sm btn-dark"><i class="fa fa-print"></i> Print Report</a>
		</td>
		<?php }else{ ?>
      <td colspan="2" align="right">
		<form method="post" class="p-0 m-0" onSubmit="return confirm('Are your sure?');">
			<button type="submit" name="end_btn" class="btn btn-sm btn-info"><i class="fa fa-check"></i> Day End &amp; Print</button>
			</form>
		</td>
		<?php } ?>
    </tr>
  </tbody>
</table>

<?php 
if(isset($_GET['print'])){
?>
<iframe src="summery_print.php" style="display: none;"></iframe>
<?php } ?>