<?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index.php');
endif;
if(empty($_SESSION['branch'])):
header('Location:../index.php');
endif;

date_default_timezone_set("Asia/Manila"); 
	$date = date("Y-m-d");
	include('../dist/includes/dbcon.php');
	
$id=$_SESSION['id']; 
$query8=mysqli_query($con,"select SUM(amount) as gogo1 from expenses where date='$date' and user_id='$id'")or die(mysqli_error($con));
$row8=mysqli_fetch_array($query8);	
$myexpe=$row8['gogo1']?? null; 
//echo $myexpe;

$query11=mysqli_query($con,"select SUM(amount) as remit from remit where date='$date' and user_id='$id'")or die(mysqli_error($con));
        $row11=mysqli_fetch_array($query11);	
		$muyremit=$row11['remit']?? null;
	
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Sales Report | <?php include('../dist/includes/title.php');?></title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
      <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- daterange picker -->
  <link rel="stylesheet" href="../plugins/daterangepicker/daterangepicker-bs3.css">
  <link rel="stylesheet" href="../plugins/datepicker/datepicker3.css">
  <link rel="stylesheet" href="../plugins/iCheck/all.css">
  <link rel="stylesheet" href="../plugins/colorpicker/bootstrap-colorpicker.min.css">
  <link rel="stylesheet" href="../plugins/timepicker/bootstrap-timepicker.min.css">
  <link rel="stylesheet" href="../plugins/select2/select2.min.css">
  <link rel="stylesheet" href="../dist/css/AdminLTE.min.css">
  <link rel="stylesheet" href="../dist/css/skins/_all-skins.min.css">
  <script type="text/javascript" src="../dist/js/jquery.min.js"></script>
<script type="text/javascript" src="../dist/js/moment.min.js"></script>
<link rel="stylesheet" type="text/css" href="../dist/css/bootstrap.css" />
 
<!-- Include Date Range Picker -->
<script type="text/javascript" src="../plugins/daterangepicker/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="../plugins/daterangepicker/daterangepicker.css" />
    <style type="text/css">
      h5,h6{
        text-align:center;
      }
		

      @media print {
          .btn-print {
            display:none !important;
		  }
		  .main-footer	{
			display:none !important;
		  }
		  .box.box-primary {
			  border-top:none !important;
		  }
		  .angel{
			  display:none !important;
		  }
          
      }
    </style>
 </head>
  <!-- ADD THE CLASS layout-top-nav TO REMOVE THE SIDEBAR. -->
  <body class="hold-transition skin-<?php echo $_SESSION['skin'];?> layout-top-nav">
    <div class="wrapper">
      <?php include('../dist/includes/header.php');?>
      <!-- Full Width Column -->
      <div class="content-wrapper">
        <div class="container">
          <!-- Content Header (Page header) -->
          

          <!-- Main content -->
          <section class="content">
            <div class="col-md-12">
			  <div class="box box-primary angel">
				<div class="box-header">
				  <h3 class="box-title">Select Date</h3>
				</div>
				<div class="box-body">
				
				  <!-- /.form group -->
				  <form method="post">
					<div class="form-group col-md-6">
						<label>Date range:</label>

						<div class="input-group">
						  <div class="input-group-addon">
							<i class="fa fa-calendar"></i>
						  </div>
						<input type="text" name="date" class="form-control pull-right active" id="reservation" required>
					</div>
                <!-- /.input group -->
					</div>
              <!-- /.form group --><br>
					<button type="submit" class="btn btn-primary" name="display">Display</button>
				</form>
				
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->       
        </div>
		<?php
		if (isset($_POST['display']))
	{
		$date=$_POST['date'];
		$date=explode('-',$date);
		$id=$_SESSION['id'];		
			$start=date("Y/m/d",strtotime($date[0]));
			$end=date("Y/m/d",strtotime($date[1]));
		
		?>
		<div class="col-md-14">
		<?php
include('../dist/includes/dbcon.php');

$branch=$_SESSION['branch'];
    $query=mysqli_query($con,"select * from branch where branch_id='$branch'")or die(mysqli_error());
  
        $row=mysqli_fetch_array($query);
        
?>      
                  <h5><b>GLASS and ROOFING DEPARTMENT</b> </h5>  
                  <h6>Address: <?php echo $row['branch_address'];?></h6>
                  <h6>Contact #: <?php echo $row['branch_contact'];?></h6>
                  
				<center>  <h3><b>Cash Sales Report as of <?php echo date("M d, Y",strtotime($start))." to ".date("M d, Y",strtotime($end));?></b></h3> </center>
                  
				  <a class = "btn btn-success btn-print" href = "" onclick = "window.print()"><i class ="glyphicon glyphicon-print"></i> Print</a>
							<a class = "btn btn-primary btn-print" href = "home.php"><i class ="glyphicon glyphicon-arrow-left"></i> Back to Homepage</a>   
						
		
		<!--	<table id="example1" class="table table-bordered table-striped">
                    <thead>
					<h4>ALL TRANSACTION TODAY</h4>
                      <tr>
					    <th>Branch</th>
                        <th>TR #</th>
                        <th>Customer Name</th>
                        <th>Product</th>
                        
                        <th>Qty</th>
						<th>Price</th>
						<th>Total</th>
                        <th>Discount</th>
                        <th>Amount Paid</th>
                        <th>Date Paid</th>
                      </tr>
                    </thead>
                    <tbody>
<?php
	$query=mysqli_query($con,"select * from sales where date(date_added)>='$start' and date(date_added)<='$end' and user_id='$id'")or die(mysqli_error($con));
		$qty=0;$grand=0;$discount=0;
				while($row=mysqli_fetch_array($query)){
				$sales_id=$row['sales_id'];
				$cust_id=$row['cust_id'];
				
                $total=$row['amount_due'];
				$grand=$grand+$total;
                $discount=$discount+$row['discount'];
				$mybranch=$row['branch_id'];
				if($mybranch == 1){
					$branch="GLASS";
				}else if($mybranch == 2){
					$branch="ROOF";
				}
			$query1=mysqli_query($con,"select * from customer where cust_id='$cust_id'")or die(mysqli_error($con));
        $row1=mysqli_fetch_array($query1);	
				
				
		$query2=mysqli_query($con,"select * from sales_details where sales_id='$sales_id'")or die(mysqli_error($con));
        $row2=mysqli_fetch_array($query2);
				$prod_id=$row2['prod_id'];
		$query3=mysqli_query($con,"select * from product where prod_id='$prod_id'")or die(mysqli_error($con));
        $row3=mysqli_fetch_array($query3);	

		
			$query9=mysqli_query($con,"select SUM(total) as cashgo from sales where date(date_added)>='$start' and date(date_added)<='$end' and modeofpayment='cash'")or die(mysqli_error($con));
        $row9=mysqli_fetch_array($query9);	
		$mycashtotal=$row9['cashgo'];	
			

$query10=mysqli_query($con,"select SUM(discount) as discoko from sales where date(date_added)>='$start' and date(date_added)<='$end' and modeofpayment='cash'")or die(mysqli_error($con));
        $row10=mysqli_fetch_array($query10);	
		$mydisc11=$row10['discoko']?? null;			
?>
            <tr>
			<td><?php echo $branch;?></td>
            <td><?php echo $row['sales_id'];;?></td>
            <td><?php echo $row1['cust_last'].", ".$row1['cust_first'];?></td>
            <td><?php echo $row3['prod_name'];?></td>
           
            <td><?php echo $row2['qty'];?></td>
			<td><?php echo $row2['price'];?></td>
			 <td><?php echo $row['amount_due'];?></td>
            <td><?php echo $row['discount'];?></td>
            <td style="text-align:right"><?php echo number_format($row['total'],2);
								?></td>
            <td><?php echo date("M d, Y h:i a",strtotime($row['date_added']));?></td>    
			
		
 <?php }?>                       
                      </tr>
		
                    </tbody>  -->
					
			<table id="example1" class="table table-bordered table-striped">
                    <thead>
					<h4>PARTIAL/CASH PAYMENT TODAY</h4>
                      <tr>
					    <th>Branch</th>
                        <th>TR #</th>
                        <th>Customer Name</th>
                        <th>Product</th>
                        
                        <th>Qty</th>
						<th>Price</th>
						<th>Total</th>
                        <th>Discount</th>
                        <th>Partial/CASH Paid</th>
                        <th>Date Paid</th>
						<th>UNPAID</th>
                      </tr>
                    </thead>
                    <tbody>
<?php
	$query=mysqli_query($con,"select * from sales where date(date_added)>='$start' and date(date_added)<='$end' and user_id='$id' ")or die(mysqli_error($con));
		$qty=0;$grand=0;$discount=0;
				while($row=mysqli_fetch_array($query)){
				$sales_id=$row['sales_id'];
				$cust_id=$row['cust_id'];
				
                $total=$row['amount_due'];
                $discount=$row['discount'];
				$mybranch=$row['branch_id'];
				if($mybranch == 1){
					$branch="GLASS";
				}else if($mybranch == 2){
					$branch="ROOF";
				}
			$query1=mysqli_query($con,"select * from customer where cust_id='$cust_id'")or die(mysqli_error($con));
        $row1=mysqli_fetch_array($query1);	
		$query7=mysqli_query($con,"select SUM(partial1) as gogo from sales where date(date_added)>='$start' and date(date_added)<='$end' and modeofpayment='partial'")or die(mysqli_error($con));
        $row7=mysqli_fetch_array($query7);	
		$mytotal=$row7['gogo']?? null;
				
		$query2=mysqli_query($con,"select * from sales_details where sales_id='$sales_id'")or die(mysqli_error($con));
        $row2=mysqli_fetch_array($query2);
				$prod_id=$row2['prod_id']?? null;
		$query3=mysqli_query($con,"select * from product where prod_id='$prod_id'")or die(mysqli_error($con));
        $row3=mysqli_fetch_array($query3);	

$query12=mysqli_query($con,"select SUM(discount) as discokofff from sales where date(date_added)>='$start' and date(date_added)<='$end' and modeofpayment='partial'")or die(mysqli_error($con));
        $row12=mysqli_fetch_array($query12);	
		$mydisc12=$row12['discokofff']?? null;	

		
				
				
?>
            <tr>
			<td><?php echo $branch;?></td>
            <td><?php echo $row['sales_id'];?></td>
            <td><?php echo $row1['cust_last'].", ".$row1['cust_first'];?></td>
            <td><?php echo $row3['prod_name']?? null;?></td>
           
            <td><?php echo $row2['qty']?? null;?></td>
			<td><?php echo $row2['price']?? null;?></td>
			 <td><?php echo $row['amount_due']?? null;?></td>
            <td><?php echo $row['discount']?? null;?></td>
            <td style="text-align:right"><?php echo number_format($row['partial1'],2);
								?></td>
            <td><?php echo date("M d, Y h:i a",strtotime($row['date_added']));?></td> 
<?php $javako=number_format($row['amount_due'] - $row['partial1'],2);?>			
			  <td style="text-align:right"><?php if($javako < 0 ) { echo "PARTIAL"; }else { echo $javako; }?></td>
		
 <?php } ?>                       
                      </tr>
		
                    </tbody>		
                    <tfoot>
          <tr>
            <th style=" border: 1px solid black;" colspan="8">Total</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo  $grand + $mytotal + $mycashtotal;?></b></h4></th>
          </tr>             
					<tr>
            <th style=" border: 1px solid black;" colspan="8">Less: Total Discount</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo  number_format($mydisc11 + $mydisc12,2);?></b></h4></th>
          </tr>   
          
		   <tr>
            <th style=" border: 1px solid black;" colspan="8">Total Expenses</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $myexpe;?></b></h4></th>
			    </tr>
		  
		  <?php 
		  $totaldiscountpo=$mydisc12; 
		  ?>
		  
          <tr>
            <th style=" border: 1px solid black;" colspan="8">Total Cash Sales</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo  number_format(($grand + $mytotal + $mycashtotal - $myexpe - $totaldiscountpo),2);?></b></h4></th>
			    </tr>		
          <tr>
		  
		  
		            <tr>
            <th style=" border: 1px solid black;" colspan="8">Total Remmitances</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $muyremit;?></b></h4></th>
			    </tr>		
          <tr>
		  
		  
		  
		   <tr>
            <th colspan="8" style=" border: 1px solid black;" >Remaining Cash Sales For Remmitances</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php $gama=$grand + $mytotal + $mycashtotal - $myexpe; echo $gamapo=$gama - $muyremit;?></b></h4></th>
			    </tr>		
          <tr>
		  
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                      </tr> 
                      <tr>
                        <th>Prepared by:</th>
                        <th></th>
                        <th></th>
                        <th></th>
                      </tr> 
<?php
    $id=$_SESSION['id'];
    $query=mysqli_query($con,"select * from user where user_id='$id'")or die(mysqli_error($con));
    $row=mysqli_fetch_array($query);
 
?>                      
                      <tr>
                        <th><?php echo $row['name'];?></th>
                        <th></th>
                        <th></th>
                        <th></th>
                      </tr>  			  
        </tfoot>
       </table>
	   
	   
	   
	   
	  <center> <h3>QUOTE HISTORY TODAY</h3> </center>
	   <table id="example1" class="table table-bordered table-striped">
                    <thead>
                      <tr>
					    <th>Branch</th>
                        <th>TR #</th>
                        <th>Customer Name</th>
                        <th>Product</th>
                        <th>Product Code</th>
                        <th>Qty</th>
						<th>Price</th>
                        <th>Discount</th>
                        <th>Payable </th>
                        <th>Date Paid</th>
                      </tr>
                    </thead>
                    <tbody>
<?php
	$query=mysqli_query($con,"select * from sales natural join sales_details natural join product natural join customer where date(date_added)>='$start' and date(date_added)<='$end' and modeofpayment='quote'")or die(mysqli_error($con));
		$qty=0;$grand=0;$discount=0;
								while($row=mysqli_fetch_array($query)){
                $total=$row['qty']*$row['price'];
				$grand=$grand+$total-$row['discount'];
                $discount=$discount+$row['discount'];
				$mybranch=$row['branch_id'];
				if($mybranch == 1){
					$branch="GLASS";
				}else if($mybranch == 2){
					$branch="ROOF";
				}
?>
            <tr>
			<td><?php echo $branch;?></td>
            <td><?php echo $row['sales_id'];;?></td>
            <td><?php echo $row['cust_last'].", ".$row['cust_first'];?></td>
            <td><?php echo $row['prod_name'];?></td>
            <td><?php echo $row['serial'];?></td>
            <td><?php echo $row['qty'];?></td>
						<td><?php echo $row['price'];?></td>
            <td><?php echo $row['discount'];?></td>
            <td style="text-align:right"><?php echo number_format($row['total'],2);
								?></td>
            <td><?php echo date("M d, Y h:i a",strtotime($row['date_added']));?></td>    
			
		
	<?php } }?>                       
                      </tr>
		
                    </tbody>
                   </table>
	   
	 
	   
	   
	   
		</div>
            
          </section><!-- /.content -->
        </div><!-- /.container -->
      </div><!-- /.content-wrapper -->
      <?php include('../dist/includes/footer.php');?>
    </div><!-- ./wrapper -->

    <script src="../plugins/jQuery/jQuery-2.2.0.min.js"></script>
	<!-- Bootstrap 3.3.6 -->
	<script src="../bootstrap/js/bootstrap.min.js"></script>
	<!-- Select2 -->
	<script src="../plugins/select2/select2.full.min.js"></script>
	<!-- InputMask -->
	<script src="../plugins/input-mask/jquery.inputmask.js"></script>
	<script src="../plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
	<script src="../plugins/input-mask/jquery.inputmask.extensions.js"></script>
	<!-- date-range-picker -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
	<script src="../plugins/daterangepicker/daterangepicker.js"></script>
	<!-- bootstrap datepicker -->
	<script src="../plugins/datepicker/bootstrap-datepicker.js"></script>
	<!-- bootstrap color picker -->
	<script src="../plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
	<!-- bootstrap time picker -->
	<script src="../plugins/timepicker/bootstrap-timepicker.min.js"></script>
	<!-- SlimScroll 1.3.0 -->
	<script src="../plugins/slimScroll/jquery.slimscroll.min.js"></script>
	<!-- iCheck 1.0.1 -->
	<script src="../plugins/iCheck/icheck.min.js"></script>
	<!-- FastClick -->
	<script src="../plugins/fastclick/fastclick.js"></script>
	<!-- AdminLTE App -->
	<script src="../dist/js/app.min.js"></script>
	<!-- AdminLTE for demo purposes -->
	<script src="../dist/js/demo.js"></script>
    <script>
  $(function () {
    //Initialize Select2 Elements
    $(".select2").select2();

    //Datemask dd/mm/yyyy
    $("#datemask").inputmask("dd/mm/yyyy", {"placeholder": "dd/mm/yyyy"});
    //Datemask2 mm/dd/yyyy
    $("#datemask2").inputmask("mm/dd/yyyy", {"placeholder": "mm/dd/yyyy"});
    //Money Euro
    $("[data-mask]").inputmask();

    //Date range picker
    $('#reservation').daterangepicker();
    //Date range picker with time picker
    $('#reservationtime').daterangepicker({timePicker: true, timePickerIncrement: 30, format: 'MM/DD/YYYY h:mm A'});
    //Date range as a button
    $('#daterange-btn').daterangepicker(
        {
          ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
          },
          startDate: moment().subtract(29, 'days'),
          endDate: moment()
        },
        function (start, end) {
          $('#daterange-btn span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        }
    );

    //Date picker
    $('#datepicker').datepicker({
      autoclose: true
    });

    //iCheck for checkbox and radio inputs
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
      checkboxClass: 'icheckbox_minimal-blue',
      radioClass: 'iradio_minimal-blue'
    });
    //Red color scheme for iCheck
    $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
      checkboxClass: 'icheckbox_minimal-red',
      radioClass: 'iradio_minimal-red'
    });
    //Flat red color scheme for iCheck
    $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
      checkboxClass: 'icheckbox_flat-green',
      radioClass: 'iradio_flat-green'
    });

    //Colorpicker
    $(".my-colorpicker1").colorpicker();
    //color picker with addon
    $(".my-colorpicker2").colorpicker();

    //Timepicker
    $(".timepicker").timepicker({
      showInputs: false
    });
  });
</script>
  </body>
</html>
