<?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->productsSalesReport($from,$to,$_SESSION['security']['location_id']);	
$invoice_total_array = $saleOb->productsInvoiceTotalSalesReport($from,$to,$_SESSION['security']['location_id']);



/*$query = "select product_id, sum(unitsaleqty) as unit_quantity from product_saleitems group by product_id order by product_id ASC ";
$queryOb = new classQueryGlobal;
$invoice_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");*/


/*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 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
    }
    ?>
    <!--<style type="text/css" media="print">
        @page 
        {
            size: auto;   /* auto is the initial value */
            margin: 0mm;  /* this affects the margin in the printer settings */
        }
    </style>-->
	<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">
                <!--<p style="font-family:Tahoma, Geneva, sans-serif; font-size:16px;">Date From: 02.Oct-2014 to 13.Oct-2014</p>-->
                
                <table  class="bottomBorder" width="100%">
                
                <thead>
                    <tr>
                    	<th width="10%" align="left">Product ID</th>
                        <th width="30%" align="left">Product Name</th>
                        <th width="15%" align="right">Qty</th>
                        <th width="15%" align="right">Rate</th>
                        <th width="15%" align="right">Amount Payable</th>
                        <th width="15%" align="right">Payments</th>
                    </tr>
                </thead>
                
                <tbody>
                	<?php
					$total_sales = 0;
					if(isset($invoice_array[0]['product_id']))
					{
						for($r=0;$r<count($invoice_array);$r++)
						{
							
							$singleProdOb = new classProduct;
							$singleProdOb->setProductID($invoice_array[$r]['product_id']);
							$single_prod_array = $singleProdOb->retrieveProductByProductID();
							?>
							<tr>
                            	<td><?php echo $invoice_array[$r]['product_id'];?></td>
								<td><?php echo $single_prod_array[0]['product_name'];?></td>
								<td align="right"><?php echo $invoice_array[$r]['sum(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]['sum(net_total)'],2);?></td>
                                <td align="right"></td>
							</tr>
							<?php
							$total_sales += $invoice_array[$r]['sum(net_total)'];
						}
						
						?>
                                
                        <tr>
                            <td></td>
                            <td></td>
                            <td></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>