<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');
error_reporting(0);


$sitesname = $_REQUEST['site'];
$function_filter[] = $sitesname;
$function_filter[] = '';
//$data_date=explode('/',$_REQUEST['date']);
// $date55=$data_date[2].'-'.$data_date[1].'-'.$data_date[0];
// $date22= strtotime(date("Y-m-d", strtotime($date55)) . " +0 day");
// $date1= date("Y-m-d",$date22);
$function_filter[] = date('Y-m-d');



?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
	<div class="tab_div">
		<table width="100%" border="0" cellspacing="0" cellpadding="3">
			<tr class="row1">
				<td width="13%" align="right">Main Category</td>
				<td width="35%"><select id="srch_cateory" onchange="getSubCategory()">
						<option value="">Select</option>
						<?php
						$item_query = "SELECT * FROM stockmaincategory where CompanyId='" . $_SESSION['ACGCompanyId'] . "'";
						$res = DB_query($item_query, $db);
						while ($item_row1 = DB_fetch_array($res)) {
							$p = '';
							if ($item_row1['id'] == $_REQUEST['CategoryID'])
								$p = "selected"; ?>
							<option value="<?php echo $item_row1['id']; ?>" <?php echo $p; ?>><?php echo $item_row1['stockmaincategoryname']; ?></option>
						<?php } ?>
					</select></td>
				<td width="13%" align="right">Sub Category</td>
				<td><select id="srch_subcateory">
						<option value="">Select</option>
						<?php
						$item_subquery = "SELECT * FROM stockcategory where CompanyId='" . $_SESSION['ACGCompanyId'] . "'";
						$subres = DB_query($item_subquery, $db);
						while ($item_subrow = DB_fetch_array($subres)) {
							$p = '';
							if ($item_subrow['id'] == $_REQUEST['CategoryID'])
								$p = "selected"; ?>
							<option value="<?php echo $item_subrow['id']; ?>" <?php echo $p; ?>><?php echo $item_subrow['categorydescription']; ?></option>
						<?php } ?>
					</select></td>
			</tr>
			<tr class="row1">
				<td align="right">Code</td>
				<td><input type="text" id="srch_code" placeholder="Item Code" /></td>
				<td align="right">Name</td>
				<td><input type="text" id="srch_name" placeholder="Item Name" /></td>
			</tr>
			<tr class="row1">
				<td colspan="4" class="text-center"><input type="button" value="Search" onclick="getdata1()" class="search" /></td>
			</tr>
		</table>
		<br />
		<div id="ItemListDiv">
			<form name="form1" action="" method="post">
				<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
					<tr>
						<th width="5%">Select</th>
						<th width="12%">Item Code</th>
						<th>Item Name</th>
						<th width="32%">Item Description</th>
						<th width="9%">UOM</th>
						<th width="14%">Stock Bal.</th>
					</tr>
					<?php
					$flg = 1;
					/*if($_REQUEST['vend_flg']=='0')
{

$sql=DB_query("SELECT id,StockID,item_name,item_desc,mst_unitid,mrp_rate,net_rate,item_mst_form_rate FROM item_master WHERE CompanyId='5' ORDER BY id DESC LIMIT 10",$db); 
$flg=1;
}
else
{
	$vend=DB_fetch_assoc(DB_query("select * from suppliers where id='".$_REQUEST['vend']."'",$db));
	 if($vend['assigned_items']!='')
	 {
		$sql=DB_query("Select id,StockID,item_name,item_desc,mst_unitid,mrp_rate,net_rate,item_mst_form_rate from item_master where CompanyId='".$_SESSION['ACGCompanyId']."' and id  in(".$vend['assigned_items'].") ",$db); 
		$flg=1;
	}
	else
	{
		$msg="No Item Mapped to this vendor";
		$flg=0;
	}
}*/
					$sql = DB_query("SELECT id,StockID,item_name,mst_unitid,item_no,conv_factor, equivalent_qty FROM item_master WHERE 1 
AND id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(SELECT hmis_storeid FROM www_users WHERE id='" . $_SESSION['UserID'] . "'))
 OR  
id IN(SELECT itemcode FROM opening_stockitemwise WHERE store IN(SELECT store_id FROM www_users_dtls WHERE user_id='" . $_SESSION['UserID'] . "')) ORDER BY StockID DESC LIMIT 10" . $w, $db);
					$i = 0;
					if ($flg == 1) {
						if (DB_num_rows($sql) > 0) {
							while ($myrow = DB_fetch_array($sql)) {
								$purchase_vat = '';
								$$purchase_cst = '';
								$parameter = $myrow['id'];
								$unitname = $s->GetFieldValue('unitsofmeasure', "unitid='" . $myrow['mst_unitid'] . "'", 'unitname');
								$op = openingBalance($function_filter, $myrow['id']);
								$in_1 = receivedMRN($function_filter, $myrow['id']);
								$out_1 = issueSlip($function_filter, $myrow['id']);
								$out_2 = issueSiteToSite($function_filter, $myrow['id']);
								$in_2 = receivedSiteToSite($function_filter, $myrow['id']);
								$out_3 = issueMatarialReturn($function_filter, $myrow['id']);
								$in_3 = receivedReturn($function_filter, $myrow['id']);
								//$out_4=Rgpchallan($function_filter,$myrow['id']);
								//$out_5=Nrgp($function_filter,$myrow['id']);
								//$in_4=Rgpreturn($function_filter,$myrow['id']);  
								//$in_5=FinishedMR($function_filter,$myrow['id']);
								$in_6 = MaterialConReceived($function_filter, $myrow['id']);
								$out_6 = MaterialConIssue($function_filter, $myrow['id']);
								$in_7 = PhysicalStockPlus($function_filter, $myrow['id']);
								$out_7 = PhysicalStockMinus($function_filter, $myrow['id']);
								$out_8 = dispatchIssue($function_filter, $myrow['id']);
								//$out_9=jobWorkChallan57Issue($function_filter,$myrow['id']);
								//$in_8=jobWorkChallan57Received($function_filter,$myrow['id']);
								$in_9 = directPurchase($function_filter, $myrow['id']);
								$in_10 = saleReturn($function_filter, $myrow['id']);
								$out_10 = invoice($function_filter, $myrow['id']);
								$in_11 = storereceipt($function_filter, $myrow['id']);
								$out_11 = drugissue($function_filter, $myrow['id']);

								$invalue = ($op + $in_1 + $in_2 + $in_3 + $in_6 + $in_7 + $in_9 + $in_10 + $in_11);
								$outvalue = ($out_1 + $out_2 + $out_3 + $out_6 + $out_7 + $out_8 + $out_10 + $out_11);
								$tot = $invalue - $outvalue;
								$stockallitems = $tot;

								$total_current = $tot;
								$vatid = '';
								$purchase_vat = $s->GetFieldValue('item_master', "id='" . $myrow['id'] . "'", 'vat_purchase');
								$purchase_cst = $s->GetFieldValue('item_master', "id='" . $myrow['id'] . "'", 'cst_purchase');
								if ($vendor_state == $loc_state) {
									$vatid = $purchase_vat;
								} else {
									$vatid = $purchase_cst;
								}
								$taxdtls = '';

								$Taxsql = "SELECT salestax_id,tax_code, taxrate,surcharge_rate FROM sales_tax_master where salestax_id='" . $vatid . "'";
								$Taxresult1 = DB_query($Taxsql, $db);
								if (DB_num_rows($Taxresult1) > 0) {
									$rowdtls = DB_fetch_array($Taxresult1);
									$vatdtlsid = $rowdtls['salestax_id'];
									$vatdesc = $rowdtls['taxrate'];
									$vatName = $rowdtls['tax_code'];
									$surcharge_rate = $rowdtls['surcharge_rate'];
								}

					?>
								<tr>
									<?php if ($total_current > 0) { ?>
										<td><input type="checkbox" name="row_id[<?php echo $i; ?>]" id="row_id_<?php echo $i; ?>" value="<?php echo $parameter; ?>" onclick="getItemsList('<?php echo $parameter; ?>','<?php echo htmlspecialchars($myrow['StockID'], ENT_QUOTES); ?>','<?php echo  htmlspecialchars($myrow['item_name'], ENT_QUOTES); ?>','<?php echo $total_current; ?>','<?php echo $unitname; ?>','<?php echo $i; ?>','<?php echo $vatdesc; ?>','<?php echo $vatdtlsid; ?>','<?php echo $surcharge_rate; ?>','<?php echo $vatName; ?>','<?php echo htmlspecialchars($myrow['equivalent_qty'], ENT_QUOTES); ?>')" /></td>
										<td><strong><?php echo $myrow['StockID']; ?></strong></td>
										<td><?php echo  $myrow['item_name']; ?></td>
										<td><?php echo $myrow['item_desc']; ?></td>
										<td><?php echo $unit; ?></td>
										<td><?php echo $total_current; ?></td>
									<?php } ?>
								</tr>
						<?php $i++;
							}
						}
					} else { ?>
						<tr>
							<td colspan="6"><?php echo  $msg ?></td>
						</tr>
					<?php } ?>

				</table>
			</form>
		</div>
	</div>