Your IP : 216.73.216.40


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

<h3 align='left'>Stockbook</h3>
<center>
<table border='1' width='98%'>
<tr><th>Sr</th><th>Dated</th><th>Medicine Name</th><th>Opening Stock</th><th>Received Qty</th><th>Total Qty</th><th>Used Qty</th><th>Balance Qty</th></tr>

<?php

$tbl = 'medicines';
$summary = $_REQUEST["summary"];
if ($summary == 1) $dated = date('Y-m-d');

$rs = mysql_query("select med_id,med_name,opening_stock,date(date_creation) as dated from $tbl order by med_name");
while ($o = mysql_fetch_object($rs)) {
	$i++;
	if (!$summary) $dated = $o->dated;
	$opening_stock = $o->opening_stock;
	$qty_total = $opening_stock;
	$qty_balance = $opening_stock;
        
	echo "<tr>
	<td><a name='$i'></a>$i</td>
	<td>$dated</td>
 	<td><a href='?pg=$pg&medid=$o->med_id#$i'>$o->med_name</a></td>
	<td align='right'>$opening_stock</td>";

	if ($summary) {
        	//receipt summary
        	$sr = mysql_query("select sum(qty_receipt) as qty_received from receipts where med_id=$o->med_id group by med_id");
        	if (mysql_num_rows($sr)) {
                	$q = mysql_fetch_object($sr);
                	$qty_total = $qty_total + $q->qty_received;

                	echo "<td align='right'>$q->qty_received</td>
                	<td align='right'>$qty_total</td>";
        	} else {
                	echo "<td></td>
                	<td align='right'>$qty_total</td>";
        	}

        	//consumption summary
        	$rr = mysql_query("select sum(qty) as qty_used from medications where med_id=$o->med_id group by med_id");
        	if (mysql_num_rows($rr)) {
                	$r = mysql_fetch_object($rr);
        	        $qty_balance = $qty_total - $r->qty_used;

	                echo "<td align='right'>$r->qty_used</td>
                	<td align='right'>$qty_balance</td>";
        	} else {
	                echo "<td></td>
                	<td align='right'>$qty_balance</td>";
        	}
	        echo "</tr>";

	} else {
		//receipt on opening day
		$sr = mysql_query("select date(rec_date) as rec_date,sum(qty_receipt) as qty_received from receipts 
        	where med_id=$o->med_id and date(rec_date)=$o->dated group by med_id,date(rec_date)");
       		if (mysql_num_rows($sr)) {
                	while ($q = mysql_fetch_object($sr)) {
				$qty_total = $qty_total + $q->qty_received;

				echo "<td align='right'>$q->qty_received</td>
				<td align='right'>$qty_total</td>";
			}
        	} else {
			echo "<td></td>
			<td align='right'>$qty_total</td>";
		} 
        
		//consumption on opening day
        	$rr = mysql_query("select date(dis_datetime) as dis_date,sum(qty) as qty_used from medications
        	where med_id=$o->med_id and date(dis_datetime)=$o->dated group by med_id,date(dis_datetime)");
        	if (mysql_num_rows($rr)) {
        		while ($r = mysql_fetch_object($rr)) {
                       		$qty_balance = $qty_total - $r->qty_used;

				echo "<td align='right'>$r->qty_used</td>
				<td align='right'>$qty_balance</td>";
			}
		} else {
			echo "<td></td>
			<td align='right'>$qty_balance</td>";
		}
		echo "</tr>"; 

		$j = 0;
	
        	$sr = mysql_query("select rec_date,sum(qty_receipt) as qty_received
		from receipts where med_id=$o->med_id group by rec_date");
        	if (mysql_num_rows($sr)) {//procurement is done, check for consumption
                	while ($q = mysql_fetch_object($sr)) {
				$rr = mysql_query("select date(dis_datetime) as dis_date,sum(qty) as qty_used from medications
        			where med_id=$o->med_id and date(dis_datetime) >= $dated group by date(dis_datetime)");
        			if (mysql_num_rows($rr)) {//consumption is done, check dates
					while ($r = mysql_fetch_object($rr)) {
                        			if (strtotime($r->dis_date) > strtotime($dated) && strtotime($r->dis_date) < strtotime($q->rec_date)) {
							//consumption is earlier to procurement
							$j++;

							echo "<tr>
                        				<td>$i . $j</td>
                        				<td>$r->dis_date [-]</td>
                        				<td></td>
                        				<td align='right'>$qty_balance</td>";
				
                        				$qty_balance = $qty_total - $r->qty_used;
                        	
							echo "<td></td>
                        				<td align='right'>$qty_total</td>
							<td align='right'>$r->qty_used</td>
	                				<td align='right'>$qty_balance</td>
							</tr>";
							
							$qty_total = $qty_balance;
							$dated = $r->dis_date;
						} else if (strtotime($q->rec_date) == strtotime($r->dis_date)) {
							//simultaneous procurement/consumption
							$j++;
                        				$qty_total = $qty_total + $q->qty_received;

							echo "<tr>
                        				<td>$i . $j</td>
                        				<td>$q->rec_date [+-]</td>
                        				<td></td>
                        				<td align='right'>$qty_balance</td>";
				
							$qty_balance = $qty_total - $r->qty_used;
                        	
							echo "<td align='right'>$q->qty_received</td>
                        				<td align='right'>$qty_total</td>
							<td align='right'>$r->qty_used</td>
	                				<td align='right'>$qty_balance</td>
							</tr>";
					
							$qty_total = $qty_balance;
							$dated = $r->dis_date;
						} else if (strtotime($q->rec_date) > strtotime($dated) && strtotime($q->rec_date) < strtotime($r->dis_date)) {
							//procurement is earlier to consumption
							$j++;
                        				$qty_total = $qty_total + $q->qty_received;

							echo "<tr>
                        				<td>$i . $j</td>
                        				<td>$q->rec_date [+]</td>
                        				<td></td>
                        				<td align='right'>$qty_balance</td>";
				
							$qty_balance = $qty_total;
                        	
							echo "<td align='right'>$q->qty_received</td>
                        				<td align='right'>$qty_total</td>
							<td align='right'></td>
	                				<td align='right'>$qty_balance</td>
							</tr>";
					
							$dated = $q->rec_date;
						} else {//consumption is after procurement
							//$dated = (strtotime($q->rec_date) > strtotime($r->dis_date) ? $q->rec_date : $r->dis_date);
							continue;
						}
						//$dated = (strtotime($q->rec_date) > strtotime($r->dis_date) ? $q->rec_date : $r->dis_date);
					}
				} else {//first procurement is earlier to first consumption
                        		$j++;
					$qty_total = $qty_total + $q->qty_received;

					echo "<tr>
                       			<td>$i . $j</td>
                      			<td>$q->rec_date [+]</td>
                        		<td></td>
                        		<td align='right'>$qty_balance</td>";

					$qty_balance = $qty_total;

                        		echo "<td align='right'>$q->qty_received</td>
                        		<td align='right'>$qty_total</td>
					<td></td>
	                		<td align='right'>$qty_balance</td>
					</tr>";
				}
				$dated = (strtotime($q->rec_date) > strtotime($r->dis_date) ? $q->rec_date : $r->dis_date);
			}
			//remaining consumption
               		$rr = mysql_query("select date(dis_datetime) as dis_date,sum(qty) as qty_used from medications
               		where med_id=$o->med_id group by date(dis_datetime)");
               		if (mysql_num_rows($rr)) {
                       		while ($r = mysql_fetch_object($rr)) {
                               		if (strtotime($r->dis_date) > strtotime($dated)) {
						$j++;
 
	                               		echo "<tr>
        	                       		<td>$i . $j</td>
                	               		<td>$r->dis_date [-]</td>
                        	       		<td></td>
                        			<td align='right'>$qty_balance</td>";

                               			$qty_balance = $qty_total - $r->qty_used;

                        			echo "<td></td>
                        	       		<td align='right'>$qty_total</td>
                               			<td align='right'>$r->qty_used</td>
                               			<td align='right'>$qty_balance</td>
                               			</tr>";
                               		
						$qty_total = $qty_balance;
						$dated = $r->dis_date;
					}
				}
			}
		} else {//no procurement, only consumption
			$rr = mysql_query("select date(dis_datetime) as dis_date,sum(qty) as qty_used from medications
        		where med_id=$o->med_id and date(dis_datetime) > $dated group by date(dis_datetime)");
        		if (mysql_num_rows($rr)) {
				while ($r = mysql_fetch_object($rr)) {
					$j++;
                       
					echo "<tr>
                        		<td>$i . $j</td>
                        		<td>$r->dis_date [-]</td>
                        		<td></td>
                        		<td align='right'>$qty_balance</td>";

        				$qty_balance = $qty_total - $r->qty_used;

                        		echo "<td></td>
                        		<td align='right'>$qty_total</td>
					<td align='right'>$r->qty_used</td>
	                		<td align='right'>$qty_balance</td>
					</tr>";
					$qty_total = $qty_balance;
                		}       
			}
		}
	}
}
?>

</table>
</center>