Your IP : 216.73.216.40


Current Path : /var/www/html/mmishra/icure/doctor/
Upload File :
Current File : /var/www/html/mmishra/icure/doctor/monthly_medications.inc

<h3 align='left'>Monthly Medications</h3>

<?php

$tbl 		= 'medicines';
$monthyear 	= $_REQUEST["monthyear"];

if (!$monthyear) $monthyear = date("Y-m");
$cal = explode("-", $monthyear);
$m = $cal[1];
$y = $cal[0];

echo "Select month <select name='monthyear' onchange=\"javascript: document.location.href='?pg=$pg&monthyear='+this.value\">
<option value=''></option>";

$rs = mysql_query("select distinct concat(year(dis_datetime),'-',month(dis_datetime)) as monthyear from medications order by dis_datetime desc");
while ($o = mysql_fetch_object($rs)) {
	if (strlen($o->monthyear) == 6) $myear = str_replace("-","-0",$o->monthyear); 
	else $myear = $o->monthyear; 
	echo "<option value='$myear'";
	if ($myear == $monthyear) echo " selected";
	echo ">$myear</option>";
}
echo "</select> 
<center>";

$no = cal_days_in_month(CAL_GREGORIAN, $m, $y);
echo "<table border='1' width='90%'>
<tr><th rowspan='2'>Sr</th><th rowspan='2'>Medicines</th>
<th rowspan='2'>Open<br>Stock</th><th rowspan='2'>Receipt<br>Qty</th>
<th colspan='$no'>Medicines used each day this month ($monthyear)</th><th rowspan='2'>Used<br>Qty</th>
<th rowspan='2'>Expired<br>Qty</th><th rowspan='2'>Close<br>Stock</th></tr>
<tr>";
for ($i=1; $i <= $no; $i++) {
	echo "<th>".str_pad($i, 2, '0', STR_PAD_LEFT)."</th>";
}
echo "</tr>";
$sl = 0;
$rs = mysql_query("select med_id,med_name,opening_stock,qty_inhand,qty_expired from $tbl order by med_name");
while ($o = mysql_fetch_object($rs)) {
	$sl++;
	$mid = $o->med_id;
	$ostock0 = $o->opening_stock;
	$xqty = $o->qty_expired;
	echo "<tr><td align='center'>$sl</td><td>$o->med_name</td>";

    // total receipt qty in previous months (INFLOW)
	$rqty1 = 0;
	$sr = mysql_query("select sum(qty_receipt) as rq from receipts where med_id=$mid and rec_date<'$monthyear-01' group by med_id");
    $q = mysql_fetch_object($sr);
    if ($q->rq) $rqty1 = $q->rq;
	
	// total receipt qty in this month (INFLOW)
    $rqty2 = 0;
	$sr = mysql_query("select sum(qty_receipt) as rq from receipts where med_id=$mid and rec_date like '$monthyear-%' group by med_id");
    $q = mysql_fetch_object($sr);
    if ($q->rq) $rqty2 = $q->rq;

	// total medication qty in previous months (OUTFLOW)
    $uqty1 = 0;
	$sr = mysql_query("select sum(qty) as uq from medications where med_id=$mid and date(dis_datetime)<'$monthyear-01' group by med_id");
    $q = mysql_fetch_object($sr);
    if ($q->uq) $uqty1 = $q->uq;
    
	// opening stock = (opening stock + total receipt - total used) before this month
	$ostock = $ostock0 + $rqty1 - $uqty1;
    echo "<td align='center'>". ($ostock < 0 ? "<font color='red'>$oqty</font>" : $ostock). "</td><td align='center'>$rqty2</td>";

	// total medication qty in this month (OUTFLOW)
	$tot = 0;
	for ($i=1; $i <= $no; $i++) {
		$d = str_pad($i, 2, '0', STR_PAD_LEFT);
		$sr = mysql_query("select sum(qty) as uq from medications where med_id=$mid and date(dis_datetime)='$monthyear-$d' group by med_id");
		$q = mysql_fetch_object($sr);
		$uqty2 = '';
		if ($q->uq) {
			$uqty2 = $q->uq;
			$tot += $uqty2;
		}
		echo "<td align='center'>$uqty2</td>";
	}
	// total qty used this month
	echo "<td align='center'>$tot</td><td align='center'>$xqty</td>";

	//closing stock = (opening stock - total received - total used) this month
	$cstock = ($ostock0 + $rqty1 + $rqty2) - ($uqty1 + $tot+ $xqty);
	echo "<td align='center'>". ($cstock < 0 ? "<font color='red'>$cstock</font>" : $cstock). "</td></tr>";
}
	
?>
</table>
</center>