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

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <link rel="shortcut icon" href="Verified-Solane-LPG.jpg" /> 
    <title>Home | <?php include('../dist/includes/title.php');?></title>
       <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">
  <!-- bootstrap datepicker -->
  <link rel="stylesheet" href="../plugins/datepicker/datepicker3.css">
  <!-- iCheck for checkboxes and radio inputs -->
  <link rel="stylesheet" href="../plugins/iCheck/all.css">
  <!-- Bootstrap Color Picker -->
  <link rel="stylesheet" href="../plugins/colorpicker/bootstrap-colorpicker.min.css">
  <!-- Bootstrap time Picker -->
  <link rel="stylesheet" href="../plugins/timepicker/bootstrap-timepicker.min.css">
  <!-- Select2 -->
  <link rel="stylesheet" href="../plugins/select2/select2.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <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;
          }
      }
	     #example2 {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#example2 td, #example2 th {
  border: 1px solid black;
  padding: 8px;
   color: black;
}


#example2 tr:hover {background-color: #ddd;}

#example2 th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #008B8B;
  color: white;
}

    </style>
 </head>
  <!-- ADD THE CLASS layout-top-nav TO REMOVE THE SIDEBAR. -->
  <body class="hold-transition skin-blue layout-top-nav">
    <div class="wrapper">
      
      <!-- Full Width Column -->
      <div class="content-wrapper">
        <div class="container">

          <section class="content">
            <div class="row">
	      <div class="col-md-12">

              <div class="col-md-12">
                <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>
		   <a class = "btn btn-primary btn-print" href = "home.php"><i class ="glyphicon glyphicon-arrow-left"></i> Back to Home</a>
        </form>
        
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->       
        </div>
              </div>
                
                <div class="box-body">

                  <!-- Date range -->
                  <form method="post" action="#">
<?php
 $branch=$_SESSION['branch'];    

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;
	
	
    $query=mysqli_query($con,"select * from branch where branch_id='$branch'")or die(mysqli_error());
  
        $row=mysqli_fetch_array($query);

if (isset($_POST['display']))
  {
    $date=$_POST['date'];
    $date=explode('-',$date);
    $branch=$_SESSION['branch'];    
      $start=date("Y-m-d",strtotime($date[0]));
      $end=date("Y-m-d",strtotime($date[1]));
    
        
?>			
                 <center> <h3><b>ROOFING AND HARDWARE DEPARTMENT</b> </h3>  </center>
                  <h6><?php echo $row['branch_address'];?></h6>
                  <h6>Contact #: <?php echo $row['branch_contact'];?></h6>
                  <h6>Date <?php echo date("Y-m-d");?> Time <?php echo date("h:i A");?></h6>
                  <h3 class="text-center">CITIBUILDERS Customer Report <?php echo date("Y-m-d",strtotime($start))." to ".date("Y-m-d",strtotime($end));?></h3>
                  <hr>
				 
                   <table id="example2" class="table table-bordered table-striped" >
                 <thead>
                 
                   
                      <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(mydate)>='$start' and date(mydate)<='$end' and status!='quote'")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'] ?? null;
		$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(mydate)>='$start' and date(mydate)<='$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(mydate)>='$start' and date(mydate)<='$end' and modeofpayment='cash'")or die(mysqli_error($con));
        $row10=mysqli_fetch_array($query10);	
		$mydisc11=$row10['discoko']?? null;		








	$query=mysqli_query($con,"select * from sales where date(mydate)>='$start' and date(mydate)<='$end' and modeofpayment!='quote' ")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";
				}else if($mybranch == 3){
					$branch="HARDWARE";
				}
				
				
		
				
			$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(mydate)>='$start' and date(mydate)<='$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(mydate)>='$start' and date(mydate)<='$end' and modeofpayment='partial'")or die(mysqli_error($con));
        $row12=mysqli_fetch_array($query12);	
		$mydisc12=$row12['discokofff']?? null;	

		
				
				
?>
            <tr style=" border: 1px solid black;">
			<td style=" border: 1px solid black;"><?php echo $branch;?></td>
            <td style=" border: 1px solid black;"><?php echo $row['sales_id'];?></td>
            <td style=" border: 1px solid black;"><?php echo $row1['cust_last'].", ".$row1['cust_first'];?></td>
            <td style=" border: 1px solid black;"><?php echo $row3['prod_name']?? null;?></td>
           
            <td style=" border: 1px solid black;"><?php echo $row2['qty']?? null;?></td>
			<td style=" border: 1px solid black;"><?php echo $row2['price']?? null;?></td>
			 <td style=" border: 1px solid black;"><?php echo $row['amount_due']?? null;?></td>
            <td style=" border: 1px solid black;"><?php echo $row['discount']?? null;?></td>
            <td style="text-align:right;border: 1px solid black;"><?php echo number_format($row['partial1'],2);
								?></td>
            <td style=" border: 1px solid black;"><?php echo date("M d, Y h:i a",strtotime($row['mydate']));?></td> 
<?php $javako=number_format($row['amount_due'] - $row['partial1'],2);?>			
			  <td style="text-align:right;border: 1px solid black"><?php if($javako < 0 ) { echo "CHARGE"; }else { echo $javako; }?></td>
		
 <?php } ?>                       
                      </tr>
		
                    </tbody>		
                    <tfoot>
          <tr>
            <th style=" border: 1px solid black;" colspan="2">TOTAL</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo  $grand + $mytotal + $mycashtotal;?></b></h4></th>
			 <th style=" border: 1px solid black;" colspan="2">START UP CASH</th>
			 <?php 
		$queryfund=mysqli_query($con,"select * from pettycash where note='begin' and date(date)>='$start' and date(date)<='$end'")or die(mysqli_error($con));
        $rowfund=mysqli_fetch_array($queryfund);
		
		$tara9=$rowfund['ii']?? null * 1;
		$timetara=$tara9;
			 ?>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $timetara; ?></b></h4></th>
          </tr>             
					<tr>
            <th style=" border: 1px solid black;" colspan="2">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>
			<th style=" border: 1px solid black;" colspan="2">TODAYS PAID</th>
			<?php 
		$querypaid=mysqli_query($con,"select SUM(partial1) as gogola from sales where date(mydate)>='$start' and date(mydate)<='$end' and modeofpayment='partial' ")or die(mysqli_error($con));
        $rowpaid=mysqli_fetch_array($querypaid);
		?>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo  $rowpaid['gogola']; ?></b></h4></th>
          </tr>   
		   <tr>
            <th style=" border: 1px solid black;" colspan="2">TOTAL EXPENSES</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $myexpe;?></b></h4></th>
			<th style=" border: 1px solid black;" colspan="2">CHARGE SALES</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $rowpaid['gogola'];  ?></b></h4></th>
		</tr> 
		  <?php 
	$totaldiscountpo=$mydisc12; 
	 $gama=$grand + $mytotal + $mycashtotal - $myexpe;  $gamapo=$gama - $muyremit;
		  ?>

          <tr>
            <th style=" border: 1px solid black;" colspan="2">TOTAL CASH SALES</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $gama?></b></h4></th>
			<th style=" border: 1px solid black;" colspan="2">TODAYS UNPAID</th>
			<?php 
		$queryunpaid=mysqli_query($con,"select SUM(partial3) as hahasi from sales where date(mydate)>='$start' and date(mydate)<='$end' ")or die(mysqli_error($con));
        $rowunpaid=mysqli_fetch_array($queryunpaid);
		?>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $rowunpaid['hahasi'];?></b></h4></th>
			    </tr>		
		            <tr>
            <th style=" border: 1px solid black;" colspan="2">TOTAL REMITTANCES</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $muyremit;?></b></h4></th>
			<th style=" border: 1px solid black;" colspan="2">TOTAL PAYABLES</th>
			<?php 
		$querybalance=mysqli_query($con,"select SUM(balance) as loloya from customer where balance > 0 ")or die(mysqli_error($con));
        $rowbalance=mysqli_fetch_array($querybalance);
		?>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo $rowbalance['loloya']; ?></b></h4></th>
			    </tr>		
		   <tr>
            <th colspan="2" style=" border: 1px solid black;" >REMAINING CASH TO REMIT</th>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php $gama=$grand + $mytotal + $mycashtotal + $timetara - $myexpe; echo $gamapo=$gama - $muyremit;?></b></h4></th>
			<th style=" border: 1px solid black;" colspan="2">TOTAL TODAYS CUSTOMERS</th>
			<?php
		$querytimeko=mysqli_query($con,"select DISTINCT(cust_id) from sales where date(mydate)>='$start' and date(mydate)<='$end'")or die(mysqli_error($con));
        $rowtimeko=mysqli_num_rows($querytimeko);
		?>
            <th style=" border: 1px solid black;" style="text-align:right;"><h4><b><?php echo  $rowtimeko; ?></b></h4></th>
			    </tr>		
          <tr>
		  
            <th colspan="2" style=" border: 1px solid black;" ></th>
            <th style=" border: 1px solid black;" style="text-align:right;"></th>
			<th style=" border: 1px solid black;color:maroon;font-size:20px;" colspan="2"> TODAYS REMITTABLES</th>
			
            <th style="border: 1px solid black;"> <p style="text-align:right;color:maroon;font-size:20px;"> ₱ <b><?php $gama=$grand + $mytotal + $mycashtotal + $timetara - $myexpe;  echo $gamapo=number_format($gama - $muyremit,2);?> </b></p></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>
	   
  

                   
  <?php } }?>

				   
               
				</form>	
                </div><!-- /.box-body -->
               
            </div><!-- /.col (right) -->
           
          </div><!-- /.row -->
	  
             
             
 
             
             
          </section><!-- /.content -->
        </div><!-- /.container -->
      </div><!-- /.content-wrapper -->
     
    </div><!-- ./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 src="../plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="../plugins/datatables/dataTables.bootstrap.min.js"></script>
    <script>
      $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
          "paging": false,
          "lengthChange": false,
          "searching": true,
          "ordering": true,
          "info": false,
          "autoWidth": false
        });
      });
    </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>
