<?php
if (isset($_SESSION['success_msg'])) { ?>
	<script>
		alert("<?php echo ($_SESSION['success_msg']); ?>");
	</script>
<?php
}

unset($_SESSION['success_msg']);
if ($_REQUEST['Save']) {

	for ($i = 0; $i < count($_REQUEST['purchid']); $i++) {
		if ($_REQUEST['approve'][$i] == 1) {

			$data['Approval_status'] = 1;
			$cond = "id='" . $_REQUEST['purchid'][$i] . "'";
			$row = $s->RecordEdit('addmrn', $data, $cond);
		}
	}
	$_SESSION['success_msg'] = "Approved Successfully";
	$s->pageLocation("index.php?route=Inventory/transation/grn_approval" . $rurl);
}

$DeptId = $s->GetDeptIdByUser($_SESSION['UserID'], $_SESSION['ACGCompanyId']);
$DeptHead = $s->GetDeptHead($DeptId, $_SESSION['UserID'], 'GRNApproval');



if ($DeptHead != '') {
	$AllDeptWiseEmp = $s->GetDeptWiseEmp($DeptId, $_SESSION['ACGCompanyId']);
	if ($AllDeptWiseEmp != '') {
		$DeptWiseEmplist = implode(",", $AllDeptWiseEmp);
	}
}


$explAuthList = explode(",", $DeptHead);



//$SQLAuthorized=DB_query("select * from dept_indet_approval where ApprovalType='GRNApproval'",$db);
//while($Authorized1=DB_fetch_array($SQLAuthorized))
//{
//	$authList[]=$Authorized1['userid'];
//}
//$ImplauthList=implode(",",$authList);
//$explAuthList=explode(",",$ImplauthList);


if (in_array($_SESSION['UserID'], $explAuthList)) {
	$authchk = "Authorized";
} else {
	$authchk = "UnAuthorized";
}



?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
	<div class="tab_div">
		<h1>GRN Approval</h1>
		<div class="centre" id="slidingDiv">
			<form method="post" action="">
				<table width="100%" cellpadding="3" cellspacing="0" border="0">
					<tr class="row1">
						<td width="13%" align="right">Branch</td>
						<td width="20%"><select name="Site" id="Site">
								<option value="0">--Select--</option>
								<?php
								$SiteName = DB_query("Select * from locations where mst_companyid='" . $_SESSION['ACGCompanyId'] . "' and loccode in('" . $_SESSION['AssignedBranch'] . "')", $db);
								while ($SiteRow = DB_fetch_array($SiteName)) { ?>
									<option value="<?php echo $SiteRow['loccode'] ?>" <?php echo ($_REQUEST['Site'] == $SiteRow['loccode'] ? 'selected' : ''); ?>><?php echo $SiteRow['locationname'] ?></option>

								<?php } ?>
							</select>
						</td>
						<!--<td class="tabledata1">Department</strong></td>
	<td><select style="width:150px;" name="deptId">
			<?php $DeptName = DB_query("SELECT * FROM deptmaster where deptname='PURCHASE' and CompanyId='" . $_SESSION['ACGCompanyId'] . "'", $db);
			while ($DeptIdRow = DB_fetch_array($DeptName)) { ?>
			<option value="<?php echo $DeptIdRow['id'] ?>"<?php echo ($_REQUEST['deptId'] == $DeptIdRow['id'] ? 'selected' : ''); ?>><?php echo $DeptIdRow['deptname'] ?></option>
		<?php } ?>
		</select>
	</td>-->
						<td width="13%" align="right">From Date</td>
						<td width="20%"><input type="text" name="fdate" id="fromdate" class="datepicker" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']); ?>" onchange="javascript:checkdate(this.value)" /></td>
						<td width="13%" align="right">To Date</td>
						<td><input type="text" name="todate" class="datepicker" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']); ?>" onchange="javascript:checkdate(this.value)" />
							<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']); ?>" id='from'>
							<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']); ?>" id='to'>
						</td>
					</tr>
					<tr class="row1">
						<td colspan="6" style="text-align:center;"><input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td>
					</tr>
				</table>

			</form>
			<?php if ($authchk == 'Authorized') { ?>
				<?php if ($_REQUEST['Go']) {
					if ($_REQUEST['Site'] != '') {
						$filter[] = "site='" . $_REQUEST['Site'] . "'";
					}
					if ($_REQUEST['fdate'] != '' && $_REQUEST['todate'] != '') {
						$filter[] = "MRNDate between '" . FormatDateFORSQL($_REQUEST['fdate']) . "' and '" . FormatDateFORSQL($_REQUEST['todate']) . "'";
					}

					if ($filter != '') {
						$filter1 = implode(" and ", $filter);
						//echo "SELECT * FROM addmrn where Approval_status=0  Cancel_Status='N' and $filter1";
						$sqlPoApp = DB_query("SELECT * FROM addmrn where Approval_status=0 and  Cancel_Status='N' and $filter1", $db);
					}
				?>
					<br>
					<form method="post">
						<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
							<tr>
								<th width="4%">S. No.</th>
								<th width="12%">GRN No.</th>
								<th width="8%">GRN Date</th>
								<th>Vendor</th>
								<!--	<th class="tabledata">Valid Till</th>
-->
								<th width="12%">GRN Type</th>
								<th width="6%" style="text-align:center;">Approve</th>
							</tr>

							<?php
							$i = 0;
							if (DB_num_rows($sqlPoApp) > 0) {
								while ($PoApp = DB_fetch_array($sqlPoApp)) { ?>
									<tr>
										<td><?php echo $i + 1; ?></td>
										<td><?php echo $PoApp['MRNNo']; ?></td>
										<td><?php echo ConvertSQLDate($PoApp['MRNDate']); ?></td>
										<td>
											<?php
											if ($PoApp['ReceivedAgainst'] == 'stock_transfer') {
												echo $s->GetFieldValue('clientmaster', "id='" . $PoApp['ReceivedFrom'] . "'", 'c_name');
											} else {
												echo $s->GetFieldValue('suppliers', "id='" . $PoApp['ReceivedFrom'] . "'", 'vendor_name');
											}
											?>
										</td>
										<!--	<td><?php echo ConvertSQLDate($PoApp['valid_till']); ?></td>
-->
										<td><?php echo $PoApp['ReceivedAgainst']; ?></td>
										<td style="text-align:center;"><input type="hidden" name="purchid[<?php echo $i; ?>]" value="<?php echo $PoApp['id'] ?>"><input type="checkbox" name="approve[<?php echo $i; ?>]" value="1" id="approve"></td>
									</tr>
								<?php $i++;
								} ?>
								<tr>
									<td colspan="7" style="text-align:center"><input type="submit" name="Save" value="Save" class="search" /></td>
								</tr>
							<?php  } else { ?>
								<tr>
									<td colspan="6" style="color:#FF0000; text-align:center;">No Record Found</td>
								</tr>
							<?php  } ?>
						</table>
					</form>
				<?php  } ?>
			<?php } else { ?>
				<p align="center" style="color:#FF0000;">You Are Not Authorized Person For GRN Approval</p>
			<?php } ?>
		</div>
	</div>
</div>
<script type="text/javascript">
	function checkdate(date) {
		var from = document.getElementById('from').value;
		var to = document.getElementById('to').value;

		var cdate = date.split("/");
		var sdate = from.split("/");
		var edate = to.split("/");

		cdate[1] = cdate[1] - 1;
		sdate[1] = sdate[1] - 1;
		edate[1] = edate[1] - 1;

		var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
		var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
		var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

		if (dateS > dateE) {
			alert("Start Date is not greather then Date End.");
			document.getElementById('fromdate').value = from;
			document.getElementById('todate').value = to;

		}

		if (dateC > dateE) {
			alert("Date Not greather than " + to);
			document.getElementById('fromdate').value = from;
			document.getElementById('todate').value = to;

		}

		if (dateC < dateS) {
			alert(" Date Not less than " + from);
			document.getElementById('fromdate').value = from;
			document.getElementById('todate').value = to;

		}


	}
</script>