<?php
session_name("AUTHENUSER");
session_start();
$USERNAME = $_SESSION['SES_USERNAME'];
if(!session_name("AUTHENUSER"))
	{
?>
						<script language="javascript">
						 alert("Waktu Login telah habis.Silahkan Login Kembali");
						document.location="index.php";
						</script>
<?php
	}
if(!$USERNAME)
	{
?>
						<script language="javascript">
						 alert("Harap Login Dahulu.");
						document.location="index.php";
						</script>
<?php
	}
	?>
	<?php include "common/config.php";
	include "include/head.php"; ?>
    <?php
	$act  = $_REQUEST['act'];
	?>

<body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
   
  <h1><i class="icon-bar-chart"></i> Report Payment</h1>
</div>


   <div class="main-content">
   
            <div class="widget">
            <div class="widget-content-white glossed">
                    <div class="padded">
                      <form role="form" class="form-horizontal" method="post" action="report_payment.php?act=1">
                         <div class="form-group">
                         <div class="col-md-6"> 
                          <label class="control-label">Invoice Date</label>
                           <input type="text" class="inputDate" name="inputDate"  value="<?php echo date('d-m-Y'); ?>" id="inputDate" >
                           </label class="control-label">
                            until <input type="text" class="inputDate2" name="inputDate2"  value="<?php echo date('d-m-Y'); ?>" id="inputDate2" >
                            </div>
                        </div>
                        <div class="form-group">
                         <div class="col-md-6"> 
                          <label class="control-label">Customer Name</label>
                           <input type="text" class="customer" name="customer" >
                            </div>
                        </div>
                        <button class="btn btn-primary" type="submit">Submit</button>
                        <button class="btn btn-default" type="reset">Reset</button>
                      </form>
                    </div>
             </div>
              <?php if($act == 1) { ?>
              <div class="widget-content-white glossed">
                <div class="padded">
                <table class="table table-striped table-bordered table-hover">
                   <thead>
                        <tr>
                       	
                         	 <th>Invoice No.</th>
                            <th>Invoice Date</th>
                            <th>Customer No. - Customer Name</th>
                        
                             <th>Sales Name</th>
                             <th>Total Invoice</th>
                              <th>Total Payment</th>
                                <th>Sisa Payment</th>
                             <th colspan="2"></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
					list($d, $e, $f)=split ("[-]", $_REQUEST['inputDate']);
						$datepicker=$f."-".$e."-".$d;
						list($d, $e, $f)=split ("[-]", $_REQUEST['inputDate2']);
						$datepicker1=$f."-".$e."-".$d;
						$customer = $_REQUEST['customer'];
						if(strtotime($datepicker1) < strtotime($datepicker))
						{
						?>
						<script language="javascript">
										 alert("Date From < Date To");
										document.location="report_payment.php";
										</script>
						<?php
						}
						else
						{
							$sql = mysql_query("select *
							from  sales a
							left join tbl_customer b on a.id_customer  = b.id_customer
							where status_sales in ('1','3') and invoice_date between '$datepicker' and '$datepicker1' 
							and nama_customer like '%$customer%' order by id_sales");
						while($fetch = mysql_fetch_array($sql))
						{
								$id_sales = $fetch['id_sales'];
								list($y, $m, $d)=split ("[-]",$fetch['invoice_date']);
								$invoice_date=$d."-".$m."-".$y;
								$inventory = $fetch['inventory'];
								$status_sales = $fetch['status_sales'];
								$fetch2 = mysql_fetch_array(mysql_query("select * from salesdetails where id_sales='$id_sales' and status=1"));
								$barcodeno = $fetch2['barcode'];
								$sql2 = mysql_fetch_array(mysql_query("select sum(price) as total,currency from salesdetails 
								where id_sales  = '$id_sales' and status=1 group by currency"));
								$sql21 = mysql_fetch_array(mysql_query("select sum(price) as total,currency from salesservice
								where id_sales  = '$id_sales' group by currency"));
								$total = $sql2['total'];
								$currency = $sql2['currency'];
								$total1 = $sql21['total'];
								$currency1 = $sql21['currency'];
								$sql3 = mysql_fetch_array(mysql_query("select IFNULL(sum(nominal),0) as nominal  from sales_payment where id_sales  = '$id_sales'"));
								$nominal = $sql2['total'] - $sql3['nominal'];
								$nominal1 = $sql21['total'] - $sql3['nominal'];
								$payment = $sql3['nominal'];
								if((($nominal > 0) || ($nominal1 > 0)) && ($payment > 0)) {	
								//if(($nominal > 0) || ($nominal1 > 0)) {		
					?>
                      <tr class="gradeX">
                       	<td><a href="view_sales3.php?id_sales=<?php echo $id_sales; ?>"><?php echo $fetch['invoice'];?></a></td>
                            <td><?php echo $invoice_date;?></td>
                            <td><?php echo $fetch['kode_customer'];?> - <?php echo $fetch['nama_customer'];?></td>
                    
                            <td><?php echo $fetch['user_update'];?></td>
                            <td><?php if($total <> 0) { ?><?php echo $currency;?> <?php echo $total;?> <?php } ?><?php if($total1 <> 0) { ?><?php echo $currency1;?> <?php echo $total1;?><?php } ?></td>
                            <td><?php echo $currency;?> <?php echo $currency1;?> <?php echo $payment;?></td>
                            <td><?php if($total <> 0) { ?><?php echo $currency;?> <?php echo $nominal;?><?php } ?><?php if($total1 <> 0) { ?> <?php echo $currency1;?> <?php echo $nominal1;?><?php } ?></td>
                             <td class="actions aligncenter"><a href="histori_payment.php?id_sales=<?php echo $id_sales; ?>"class="btn btn-default btn-xs"><i class="icon-pencil"></i> histori</a> </td>
                            
                       </tr>
                       <?php
								}
						}
						}
						?>
                  </tbody>
                </table>
                </div>
              </div>
              <?php
			  }
			  ?>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</div>       

<?php include "include/foot.php"; ?>
<script src='assets/js/for_pages/dashboard.js'></script>
</body>

</html>