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

$saleOb = new classSaleInvoice;
$recOb = new classReceiveInvoice;
if(isset($_POST['search_by_date'])&&isset($_POST['search_by_date'])&&isset($_POST['date']))
{
	$date = $_POST['date'];
}
else
{
	$date = date('M-Y',time());
	
}
$sale_array = $saleOb->productsSaleReportOfMonth($date,$_SESSION['security']['location_id']);	
$rec_array = $recOb->productsReceivesReportOfMonth($date,$_SESSION['security']['location_id']);	

$invoice_array = array();

if(isset($sale_array[0]['product_id']))
{

	
	foreach($sale_array as $data1)
	{
		foreach($rec_array as $data2)
		{
			if($data1['product_id'] == $data2['product_id'])
			{
				$tmp = array_merge($data1, $data2);
				$invoice_array[] = $tmp;
			}
		}
	}  
}
		


/*echo '<pre>';
print_r($invoice_array);
print_r($rec_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 href="../datepicker/jquery-ui.css" rel="stylesheet" >  
    <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" -->All Products Gross Profit 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%">Month: <input type="text" name="date" value="<?php echo $date;?>" id="datepicker" 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="4%" align="left">Sr.#</th>
                        <th width="44%" align="left">Item Name / Product Name</th>
                        <th width="14%" align="center">Quantity</th>
                        <th width="13%" align="center">Net Sales</th>
                        <th width="11%" align="right">Net Cost</th>
                        <th width="14%" align="right">Gross Profit</th>
                    </tr>
                </thead>
                
                <tbody>
                <?php
					$total_sales = 0;
					$total_rec = 0;
					$total_profit = 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 $r+1;?></td>
                                <td><?php echo $single_prod_array[0]['product_name'];?></td>
                                <td align="center"><?php echo $invoice_array[$r]['sales_qty']?></td>
                                <td align="right">Rs. <?php echo number_format($invoice_array[$r]['sales_total']);?></td>
                                <td align="right">Rs. <?php echo number_format($invoice_array[$r]['receives_total']);?></td>
                                <td align="right">Rs. <?php echo number_format($invoice_array[$r]['sales_total']-$invoice_array[$r]['receives_total']);?></td>
                            </tr>
                            <?php
							
							$total_sales += $invoice_array[$r]['sales_total'];
							$total_rec += $invoice_array[$r]['receives_total'];
							$total_profit += $invoice_array[$r]['sales_total']-$invoice_array[$r]['receives_total'];
						}
						
						?>
                                
                        <tr>
                        <td></td>
                        <td></td>
                        <td align="center">Total : </td>
                       	<td align="right">Rs. <?php echo number_format($total_sales,2);?></td>
                        <td align="right">Rs. <?php echo number_format($total_rec,2);?></td>
                        <td align="right">Rs. <?php echo number_format($total_profit,2);?></td>
                        </tr>
                        <?php
					}
					else
					{
						?>
                                
                        <tr>
                        <td>No Entry Found</td>
                        <td></td>
                        <td></td>
                       	<td></td>
                        <td></td>
                        <td></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>-->

   <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>
$("#datepicker").datepicker({
showOn: "button",
buttonImage: "../images/calendar.gif",
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: "yy-mm-dd",
showButtonPanel: true,

onClose: function(dateText, inst) {
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).val($.datepicker.formatDate('M-yy', new Date(year, month, 1)));
        }
    });

$("#datepicker").focus(function () {
        $(".ui-datepicker-calendar").hide();
        $("#ui-datepicker-div").position({
            my: "center top",
            at: "center bottom",
            of: $(this)
        });
    });
		
</script>