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

/*echo '<pre>';
print_r($_POST);
echo '</pre>';
exit;*/
if(isset($_SESSION['print']['search'])&&isset($_SESSION['print']['sortby']))
{
	$search = $_SESSION['print']['search'];
	$sortby = $_SESSION['print']['sortby'];
}
else
{
	$search = '';
	$sortby = '';
}
	
if(isset($_GET['type'])&&base64_decode($_GET['type'])=='report')
{
	
	try
	{
		$retrieveProdOb = new classProduct;
		$product_array = $retrieveProdOb->retrieveActiveProductsThroughFilter($search,$sortby,$_SESSION['security']['org_id']);
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
}
else
{
	
	################################################################################
	#########################Pagination Starts here. {########################
	################################################################################
	try
	{
		$retrieveProdOb = new classProduct;
		$count_products = $retrieveProdOb->countActiveProductsThroughFilter($search,$sortby,$_SESSION['security']['org_id']);
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}	
	
	
	$array['item_count'] = $count_products;
	if(isset($_GET['page']) && isset($_GET['items']))
	{
		if($array['item_count'] > $_GET['items'])
		{
			$array['pages'] = ceil($array['item_count']/$_GET['items']);
			if($_GET['page'] >$array['pages'])
			{
				$_GET['page'] = 1;
			}
			$array['current_page'] = $_GET['page'];
			if($_GET['page'] > 1)
			{
				$starting_record = $_GET['items']*($_GET['page']-1);
				$ending_record = $_GET['items'];
				$array['limit'] = $starting_record . "," . $ending_record;
				$limit = $array['limit'];
			}
			else
			{
				$limit = $_GET['items'];
			}
		}
		else
		{
			$array['current_page'] = $_GET['current_page'] = 1;
			$array['pages'] = 1;
			$array['limit'] = $_GET['items'];
			$limit = $array['limit'];
		}
	}
	else
	{
		if($array['item_count'] > 20)
		{
			$array['pages'] = ceil($array['item_count']/20);
		}
		else
		{
			$array['pages'] = 1;
		}
		$array['current_page'] = 1;
		$array['limit'] = 20;
		$limit = $array['limit'];
	}
	
	$_SESSION['pagination']['item_count']=$array['item_count'];
	$_SESSION['pagination']['pages']=$array['pages'];
	$_SESSION['pagination']['current_page']=$array['current_page'];
	$_SESSION['pagination']['limit']=$limit;
	
	################################################################################
	######################### Pagination Ends here. }##########################
	################################################################################
	
	
	try
	{
		$retrieveProdOb = new classProduct;
		$product_array = $retrieveProdOb->retrieveActiveProductsThroughFilter($search,$sortby,$_SESSION['security']['org_id'],$limit);
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
	
}
?>
<!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" -->PRODUCTS INVENTORY<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
  
  
              <table  class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                
                    <thead>
                        <tr>
                            <th width="5%" rowspan="2" align="center">ID</th>
                            <th width="15%" rowspan="2" align="left">Product Name</th>
                            <th width="20%" rowspan="2" align="center">Supplier</th>
                            <th colspan="3" align="center">Trade Price (Rs.)</th>
                            <th colspan="3" align="center">Retail Price (Rs.)</th>
                            <th width="10%" rowspan="2" align="right">Unit Qty<br />in Stock</th>
                        </tr>
                        <tr>
                            <th width="8%" align="center">Box</th>
                            <th width="8%" align="center">Leaf</th>
                            <th width="8%" align="center">Unit</th>
                            <th width="8%" align="center">Box</th>
                            <th width="8%" align="center">Leaf</th>
                            <th width="8%" align="center">Unit</th>
                        </tr>
                    </thead>
                    
                    <tbody>
                    <?php
					if(isset($product_array[0]['product_id'])&&$product_array[0]['product_id']!='')
					{
						$location_id = $_SESSION['security']['location_id'];
						
						
									
						for($p=0;$p<count($product_array);$p++)
						{
							$supplier_name = '';
							$box_tradeprice ='';
							$leaf_tradeprice = '';
							$unit_tradeprice ='';
							
							$product_id = $product_array[$p]['product_id'];
							$product_name = $product_array[$p]['product_name'];
							$medicine_type = $product_array[$p]['medicine_type'];
							$manf_id = $product_array[$p]['manf_id'];
							
							
							$productUnitQtyOb = new classProduct;
							$product_detail_array = $productUnitQtyOb->retrieveProductLocationRelationship($product_id,$_SESSION['security']['location_id']);
							$unit_qtyinstock = $product_detail_array[0]['unit_qtyinstock'];
							$box_retailprice = $product_detail_array[0]['box_retailprice'];
							$leaf_retailprice = $product_detail_array[0]['leaf_retailprice'];
							$unit_retailprice = $product_detail_array[0]['unit_retailprice'];
							
							$productUnitQtyOb->setManufacturerID($manf_id);
							$manu_array = $productUnitQtyOb->retrieveManufacturerByID();
							
							$productUnitQtyOb->setProductID($product_id);
							$supplier_array = $productUnitQtyOb->retrieveProductSupplierDataByProductID();
							if(isset($supplier_array[0]))
							{
								for($s=0;$s<count($supplier_array);$s++)
								{
									$supplier_data = $productUnitQtyOb->retrieveSupplierBySupplierID($supplier_array[$s]['supplier_id']);
									$supplier_name .= ucwords($supplier_data[0]['supplier_name']).'<br>';
									$box_tradeprice .=  number_format($supplier_array[$s]['box_tradeprice'],2).'<br>';
									$leaf_tradeprice .=  number_format($supplier_array[$s]['leaf_tradeprice'],2).'<br>';
									$unit_tradeprice .=  number_format($supplier_array[$s]['unit_tradeprice'],2).'<br>';
								}
								
							}
							
							$lastClosingQtyOb = new classProductClosingReconcilation;
							$lastClosingQtyOb->setProductID($product_id);
							$last_closing_array = $lastClosingQtyOb->retrieveLastClosingQtyOfProduct();
							
							if(isset($last_closing_array[0]['closing_date'])&&$last_closing_array[0]['closing_date']!='')
							{
								$start_time = $last_closing_array[0]['closing_date'];
								$end_time = time();
								
								$lastClosingQty = $last_closing_array[0]['unit_phy_qtyinstock'];
							}
							else
							{
								$start_time = '';
								$end_time = '';
								$lastClosingQty = 0;
							}
							
							$recInvOb = new classReceiveInvoice;
							$saleInvOb = new classSaleInvoice;
							
							$sale_refund_items_array = $saleInvOb->retrieveSumOfInvoiceRefundItemsByProductID($product_id,$start_time,$end_time);
							$receive_refund_items_array = $recInvOb->retrieveSumOfInvoiceRefundItemsByProductID($product_id,$start_time,$end_time);
							
							$receive_items_array = $recInvOb->retrieveSumOfInvoiceItemsByProductID($product_id,$location_id,$start_time,$end_time);
							$rec_unitqty_recieved = $receive_items_array[0]['SUM(unitqty_recieved)']+$sale_refund_items_array[0]['SUM(qty)'];
							$rec_net_total = $receive_items_array[0]['SUM(net_total)']+$sale_refund_items_array[0]['SUM(refund_amount)'];
							
							
							
							$sale_items_array = $saleInvOb->retrieveSumOfInvoiceItemsByProductID($product_id,$location_id,$start_time,$end_time);
							$sale_unitsaleqty = $sale_items_array[0]['SUM(unitsaleqty)']+$receive_refund_items_array[0]['SUM(qty)'];
							$sale_net_total = $sale_items_array[0]['SUM(net_total)']+$receive_refund_items_array[0]['SUM(refund_amount)'];
							
							?>
							<tr class="gradeX">
								<td align="center"><?php echo $product_id;?></td>
								<td align="left"><?php echo ucwords($product_name);?><br />
                                <span class="message_grey"><?php echo $medicine_type;?></span></td>
                                <td align="left"><?php echo $supplier_name;?></td>
								<td align="right"><?php echo $box_tradeprice;?></td>
								<td align="right"><?php echo $leaf_tradeprice;?></td>
								<td align="right"><?php echo $unit_tradeprice;?></td>
								
                                
								<td align="right"><?php echo number_format($box_retailprice,2);?></td>
								<td align="right"><?php echo number_format($leaf_retailprice,2);?></td>
								<td align="right"><?php echo number_format($unit_retailprice,2);?></td>
								<td align="right"><?php echo number_format($unit_qtyinstock,2);?></td>
							</tr>
							<?php
						}
					}
					?>
                    </tbody>
                    
                  	<tfoot>
                  </tfoot>
                  
                </table>
  <!-- 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>