<?php
include('../config/config.php');
include('../classes/classSaleInvoice.php');
include('../classes/classProduct.php');

$saleOb = new classSaleInvoice;


if(isset($_POST['search_by_date'])&&isset($_POST['search_by_date'])&&isset($_POST['to']))
{
	$to = $_POST['to'];
	$from = $_POST['from'];
}
else
{
	$to = date('Y-m-d',time());
	$from = date('Y-m-d',time());
}

$invoice_array = $saleOb->productsSalesDetailReport($from,$to,$_SESSION['security']['location_id']);	
$invoice_total_array = $saleOb->productsInvoiceTotalSalesReport($from,$to,$_SESSION['security']['location_id']);

/*echo '<pre>';
print_r($invoice_array);
echo '</pre>';
*/


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/print.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link rel="shortcut icon" href="../images/favicon.png">
    <title>Med2TEC Hospital Management Information System</title> 
    <link href="../css/print.css" rel="stylesheet">
    <link rel="stylesheet" href="../datepicker/jquery-ui.css">  
    <script type="text/javascript" src="../datepicker/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="../datepicker/jquery-ui.js"></script> 
</head>

<body> 
<div align="right"> 
 <SCRIPT LANGUAGE="JavaScript">  
if (window.print) {
document.write('<form class="no-print"><input type=button name=print value="Print" onClick="window.print()"></form>');
}
</script></div>

<section class="wrapper">
<div style="background:#FFF; padding:0px 0px 0; height:auto;">
  <table cellspacing="0" cellpadding="0" width="100%" border="0" style="font-size:11px;">
    <tbody>
      <tr>
        <td align="left" style="background:#fff;"><img alt="" src="../images/org_print_logos/<?php echo $_SESSION['security']['org_id']?>.png" style="width:100%;"></td>
      </tr>
    </tbody>
  </table>
  
</br>

</div>

<div class="row">
          <section class="panel">
            <header class="panel-heading">
              <h2><!-- InstanceBeginEditable name="title" -->PRODUCTS SALES DETAILED REPORT<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
              <?php
			if(isset($error_message)&&$error_message!='')
			{
				?>
              <span class="message_red"><?php echo $error_message;?></span>
              <?php
			}
			?>

	<section class="wrapper">
		<form action="" method="post">
        <table width="100%" border="0" cellpadding="1" cellspacing="0">
            <tr>
                <td width="1%" align="right"></td>
                
                <td width="22%">From: <input type="text" name="from" value="<?php echo $from;?>" id="datepicker" class="form-control" readonly style="display:inline; width:65%;"></td>
                
                
                <td width="20%">To: <input type="text" name="to" value="<?php echo $to;?>" id="datepicker1" class="form-control" readonly style="display:inline; width:65%;"></td>
                
                <td width="10%"><input type="submit" class="btn btn-success btn-sm" name="search_by_date" value="Search"></td>
                
                <td width="37%"></td>
            </tr>
        </table>
        </form>
      <!-- page start-->
      <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            <div class="panel-body">
              <div class="adv-table">
               
                <table class="bottomBorder" width="100%">
                  <thead>
                    <tr>
                    	<th width="10%" align="left">Product ID</th>
            			<th width="25%" align="left">Product Name</th>
            			<th width="10%" align="center">INV.No</th>
            			<th width="15%" align="center">V.Date</th>
           			  	<th width="10%" align="right">Qty</th>
           			  	<th width="10%" align="right">Rate</th>
            			<th width="10%" align="right">Amount Payable</th>
                        <th width="10%" align="right">Payments</th>
                    </tr>
                  </thead>
                  <tbody>
							<?php
							$old = 0;
							$total_sales = 0;
							if(isset($invoice_array[0]['product_id']))
							{
								
								for($r=0;$r<count($invoice_array);$r++)
								{
									if ($invoice_array[$r]['product_id'] != $old)
									{
										$singleProdOb = new classProduct;
										$singleProdOb->setProductID($invoice_array[$r]['product_id']);
										$single_prod_array = $singleProdOb->retrieveProductByProductID();
										
										$product_name = $single_prod_array[0]['product_name'];
										$product_id = $invoice_array[$r]['product_id'];
										$old = $invoice_array[$r]['product_id'];
										
									}
									else
									{
										$product_name = '';
										$product_id = '';
									}
									?>
									<tr>
										<td><?php echo $product_id;?></td>
										<td><?php echo $product_name;?></td>
										<td align="right"><?php echo $invoice_array[$r]['saleinvoice_id'];?></td>
                                        <td align="right"><?php echo date('d M Y',$invoice_array[$r]['sale_date']);?></td>
                                        <td align="right"><?php echo $invoice_array[$r]['unitsaleqty'];?></td>
										<td align="right"><?php echo @number_format($invoice_array[$r]['unitretail_price'],2);?></td>
										<td align="right"><?php echo @number_format($invoice_array[$r]['net_total'],2);?></td>
                                        <td align="right"></td>
									</tr>
									<?php
									
									$total_sales += $invoice_array[$r]['net_total'];
								}
								
								?>
                                
                                <tr>
                                <td></td>
                                <td></td>
                                <td align="right"></td>
                                <td align="right"></td>
                                <td align="right"></td>
                                <td align="right"><strong>Total: </strong></td>
                                <td align="right"><strong><?php echo @number_format($total_sales,2);?></strong></td>
                                <td align="right"><strong><?php echo @number_format($invoice_total_array[0]['sum(payment)'],2);?></strong></td>
                            	</tr>
                            	<?php
							}
							?>
                            
                            
                  			
                  </tbody>
                  <tfoot>
                  </tfoot>
                </table>
              
              </div>
            </div>
          </section>
        </div>
      </div>
      <!-- page end-->
    </section>
  <!-- page end-->
  
<!-- InstanceEndEditable -->
</section>
</div>

<!--<p align="center" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; margin-top:260px;"><strong><!--317 A-1, Nursery Stop, Township, Lahore. 
Ph: 042-35151666</strong><br />
For Appointment call at 042-35151666 (bring your Patient ID Card) </p>-->


<style>


#block{
    display:block;
    margin:auto;
    position:absolute;
	float:right;
    }

#text {
    padding:0;
    margin:0;
    position:relative;
    font-size:9px;
    -webkit-transform: rotate(-90deg);
    -moz-transform: rotate(-90deg);
    -ms-transform: rotate(-90deg);
    -o-transform: rotate(-90deg);
	left: 880px; top: 348px; 
}
</style> 
<div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color: rgb(0, 0, 0); text-align: right; font-size: 10px; margin-top: 4px; position: absolute; right: 180px;">© Software by Softusion Inc. 0322 6155 219</div>   
    </div>
</div>

<!--<div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color:#000; text-align:right; font-size:9px;">© Software designed by Softusion Inc. Contact us at 0322 6155 219</div>   
    </div>
</div>-->


          
</body>
<!-- InstanceEnd --></html>
<script>
	$(function() {
		
		$("#datepicker").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd"
		});
		
		$("#datepicker1").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd"
		
		});
	
	});
</script>