<?php 
include ('pdf_header.php');
///include ('includes/config/config.php');
//include ('includes/functions.php');

$from_date = $_REQUEST ['startDate2'];
$to_date = $_REQUEST ['endDate2'];
$ohc_type = $_SESSION ['current_ohcttype'];
?>


<link href="includes/css-js/admin.css" rel="stylesheet" type="text/css" />
<body>


	<table width="100%">
		<tr>
			<td width="50%" align="center" style="font-size: 40px"><strong>Dispensary
					Stock Issue Report</strong></td>
		</tr>
		<tr>
			<td>Range : <?php  echo $from_date; ?> to <?php  echo $to_date ;?></td>
		</tr>
	</table>
	<table width="100%" border="1">
		<tr>
			<th width="3%" align="left">Sr</th>

			<th align="left" width="60%">Item</th>

			<th width="10%" align="left">Stock qty</th>
			<th width="10%" align="left">Issue qty</th>


		</tr>
<?php 

$count=0;
if (isset ( $from_date ) && isset ( $to_date )) {

		$sql_diect_issue_query = "SELECT sum(dd.issue_qty) as direct_issue,item_id FROM direct_medicine_issue_log_details dd LEFT JOIN direct_medicine_issue_log dm ON dd.issue_log_id = dm.issue_log_id WHERE  date_format(date(dm.date_of_issue),'%Y-%m-%d') between STR_TO_DATE('" . $from_date . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $to_date . "', '%d-%m-%Y') and dm.ohc_location_id='$ohc_type' group by item_id  order by dm.date_of_issue DESC";

		error_log("sql_diect_issue_query: " . $sql_diect_issue_query);

		$result_sql_diect_issue_query = mysqli_query($conn,$sql_diect_issue_query);

		while ($row_result_sql_diect_issue_query = mysqli_fetch_array($result_sql_diect_issue_query)) {

			$item_desc = getItemWithFormName ( $row_result_sql_diect_issue_query ['item_id'] );
	
			$stock_qty = getStockQtyAtDispensaryLevel ( $row_result_sql_diect_issue_query ['item_id'], $ohc_type );
			if ($stock_qty == null) {
				$stock_qty = 0;
			}
			$itemUnit = getItemUnitName($row_result_sql_diect_issue_query ['item_id']);
			error_log("cached Item Unit:".$itemUnit);
			$issue_qty = $row_result_sql_diect_issue_query['direct_issue'];
			?>
			<tr>
					<td><?php  echo ++$count?></td>
					<td><?php  echo $item_desc?></td>
					<td><?php  echo $stock_qty?> <?php  echo $itemUnit?></td>
					<td><?php  echo $direct_issue?> <?php  echo $itemUnit?></td>

				</tr>
		<?php }

		$sql_opd_consumables = "SELECT sum(c.issued_qty) as consumable_issue,medicine FROM opd_consumables c LEFT JOIN employee_appointment e ON c.consume_id = e.appointment_id where date_format(date(e.appointment_date),'%Y-%m-%d') between STR_TO_DATE('" . $from_date . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $to_date . "', '%d-%m-%Y') and e.ohc_type_id='$ohc_type' group by medicine  order by e.appointment_date DESC";

		error_log("sql_opd_consumables: " . $sql_opd_consumables);

		$result_sql_opd_consumables = mysqli_query($conn,$sql_opd_consumables);

		while ($row_result_sql_opd_consumables = mysqli_fetch_array($result_sql_opd_consumables)) {
			$item_desc = getItemWithFormName ( $row_result_sql_opd_consumables ['medicine'] );
	
			$stock_qty = getStockQtyAtDispensaryLevel ( $row_result_sql_opd_consumables ['medicine'], $ohc_type );
			if ($stock_qty == null) {
				$stock_qty = 0;
			}
			$itemUnit = getItemUnitName($row_result_sql_opd_consumables ['medicine']);
			error_log("cached Item Unit:".$itemUnit);
			$issue_qty = $row_result_sql_opd_consumables ['consumable_issue'];
	
			?>
			<tr>
					<td><?php  echo ++$count?></td>

					<td><?php  echo $item_desc?></td>
					<td><?php  echo $stock_qty?> <?php  echo $itemUnit?></td>
					<td><?php  echo $issue_qty?> <?php  echo $itemUnit?></td>

				</tr>
	<?php }

		$sql_detention_query = "select sum(a.issued_qty) as total_issued,a.medicine from detention_intake a left join employee_appointment b on b.appointment_id = a.det_id where date_format(date(b.appointment_date),'%Y-%m-%d') between STR_TO_DATE('" . $from_date . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $to_date . "', '%d-%m-%Y') and b.ohc_type_id='$ohc_type' group by medicine  order by b.appointment_date DESC";
		
		
		error_log("sql_detention_query::" . $sql_detention_query);

		$result_detention = mysqli_query($conn,$sql_detention_query);

		while ($row_detention = mysqli_fetch_array($result_detention)) {
			$item_desc = getItemWithFormName ( $row_detention ['medicine'] );
	
			$stock_qty = getStockQtyAtDispensaryLevel ( $row_detention ['medicine'], $ohc_type );
			if ($stock_qty == null) {
				$stock_qty = 0;
			}
			$itemUnit = getItemUnitName($row_detention ['medicine']);
			error_log("cached Item Unit:".$itemUnit);
			$issue_qty = $row_detention ['total_issued'];
			?>
			<tr>
					<td><?php  echo ++$count?></td>

					<td><?php  echo $item_desc?></td>
					<td><?php  echo $stock_qty?> <?php  echo $itemUnit?></td>
					<td><?php  echo $issue_qty?> <?php  echo $itemUnit?></td>

				</tr>
		<?php 
			
		}



		$sql_consume = "select sum(a.issued_qty) as total_issued,a.item_id from treatment a left join employee_appointment b on a.appointment_id = b.appointment_id where date_format(date(b.appointment_date),'%Y-%m-%d') between STR_TO_DATE('" . $from_date . "', '%d-%m-%Y')  AND STR_TO_DATE('" . $to_date . "', '%d-%m-%Y') and b.ohc_type_id='$ohc_type' group by item_id  order by b.appointment_date DESC";

		error_log("cccccccons" . $sql_consume);

		$result_consume = mysqli_query($conn,$sql_consume);

		while ($row_consume = mysqli_fetch_array($result_consume)) {
			$item_desc = getItemWithFormName ( $row_consume ['item_id'] );
	
			$stock_qty = getStockQtyAtDispensaryLevel ( $row_consume ['item_id'], $ohc_type );
			if ($stock_qty == null) {
				$stock_qty = 0;
			}
			$itemUnit = getItemUnitName($row_consume ['item_id']);
			error_log("cached Item Unit:".$itemUnit);
			$issue_qty = $row_consume ['total_issued'];
			?>
			<tr>
					<td><?php  echo ++$count?></td>

					<td><?php  echo $item_desc?></td>
					<td><?php  echo $stock_qty?> <?php  echo $itemUnit?></td>
					<td><?php  echo $issue_qty?> <?php  echo $itemUnit?></td>

				</tr>
		<?php 
			
		}
	}
?>

</table>
<?php 
include ('pdf_footer.php');
?>