<?php
ob_start();
session_name('CAKEPHP'); session_start();
$today_date = date('m/d/Y');
$date2 = strtotime(date("Y-m-d", strtotime($today_date)) . " +30 day");
$added_date = date("d/m/Y", $date2);

//echo $rurl;
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['Purchase_Approval_status']=1;
//		$data['Approved_by'] = $_SESSION['UserID'];
//		$cond="id='".$_REQUEST['purchid'][$i]."'";
//		$row= $s->RecordEdit('purchorders',$data,$cond);
//		}
//	}
//	$_SESSION['success_msg']="Approved Successfully";
//	$s->pageLocation("index.php?route=POModule/purchase_head_order_approval".$rurl);
//}

if ($_REQUEST['Save']) {

	for ($i = 0; $i <= count($_REQUEST['cpid']); $i++) {
		if ($_REQUEST['approve'][$i] == 1) {
			$data['cp_no'] = $_REQUEST['cpid'][$i];
			$data['approval_status'] = 1;
			$data['sr_no'] = $_REQUEST['hidsr_no'];
			$data['approved_by'] = $_SESSION['UserID'];
			$s->RecordInsert('cp_head_approval', $data);
		}
	}
	$_SESSION['success_msg'] = "Approved Successfully";
}
$SQLex = DB_query("select * from conf_purch_app_details where id='" . $_REQUEST['id'] . "'", $db);
$record = DB_fetch_array($SQLex);

?>
<div class="body_contain">
	<div class="tab_div">
		<h1><?php echo $record['approval_name'] ?></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%"><strong>Branch</strong></td>
						<td width="20%"><select style="width:98%;" name="Site" id="Site">
								<?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 width="13%"><strong>From Date</strong></td>
						<td width="20%"><input type="text" name="fdate" style="width:98%;" class="datepicker" id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']); ?>" onchange="javascript:checkdate(this.value)" /></td>
						<td width="13%"><strong>To Date</strong></td>
						<td><input type="text" name="todate" style="width:98%;" 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 type="hidden" name="hidsr_no" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" /><input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td>
					</tr>
				</table>
			</form>
			<?php
			if ($_REQUEST['Go']) {
				if ($_REQUEST['Site'] != '') {
					$filter[] = "site='" . $_REQUEST['Site'] . "'";
				}
				if ($_REQUEST['fdate'] != '' && $_REQUEST['todate'] != '') {
					$filter[] = "(createdDate between '" . FormatDateFORSQL($_REQUEST['fdate']) . "' and '" . FormatDateFORSQL($_REQUEST['todate']) . "')";
				}
				if ($filter != '') {
					$filter1 = implode(" and ", $filter);

					$sqlPoApp = DB_query("SELECT * FROM indentpr_mapping WHERE $filter1 and (quotaion_status='1' || quotaion_status='2')", $db);
				} ?>
				<form method="post">
					<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border-collapse:collapse; border:#808080 solid 1px;">
						<thead>
							<tr>
								<th width="5%"><strong>S. No.</strong></th>
								<th><strong>RFQ No.</strong></th>
								<th width="10%"><strong>RFQ Date</strong></th>
								<th width="10%"><strong>Opening Date</strong></th>
								<th width="10%"><strong>Quote Posting Date</strong></th>
								<th width="10%"><strong>Approve</strong></th>
							</tr>
						</thead>
						<tbody>
							<?php
							$i = 0;
							$flag = 0;
							if (DB_num_rows($sqlPoApp) > 0) {
								while ($PoApp = DB_fetch_array($sqlPoApp)) {
									$sqlqry = DB_query("select id from cp_head_approval where cp_no='" . $PoApp['id'] . "' and approval_status ='1' and sr_no='" . $_REQUEST['hidsr_no'] . "'", $db);
									if (DB_num_rows($sqlqry) == 0) {

										if ($_REQUEST['hidsr_no'] > 1) {
											$sr = $_REQUEST['hidsr_no'] - 1;
										} else {
											$sr = '';
										}
										if ($sr == '') {
											$numrow2 = 1;
										} else {
											$sqlqry2 = DB_query("select id from cp_head_approval where cp_no='" . $PoApp['id'] . "' and approval_status='1'
                                         and sr_no='" . $sr . "'", $db);
											$numrow2 = DB_num_rows($sqlqry2);
										}
										if ($numrow2 > 0) {

							?>
											<tr bgcolor="#fff">
												<td><?php echo $i + 1; ?></td>
												<td><a href="index.php?route=POModule/mapped_indent_view1<?php echo $rurl; ?>&mapid=<?php echo $PoApp['id'] ?>&action=edit&modify=viewonly"><?php echo $PoApp['mapNo']; ?></a></td>
												<td><?php echo ($PoApp['createdDate'] != '' ? ConvertSQLDate($PoApp['createdDate']) : ''); ?></td>
												<td><?php echo ($PoApp['openingDate'] != '' ? ConvertSQLDate($PoApp['openingDate']) : ''); ?></td>
												<td><?php echo ($PoApp['postingDate'] != '' ? ConvertSQLDate($PoApp['postingDate']) : ''); ?></td>
												<td>
													<input type="hidden" name="cpid[<?php echo $i; ?>]" value="<?php echo $PoApp['id'] ?>">

													<input type="checkbox" name="approve[<?php echo $i; ?>]" value="1" id="approve">

												</td>
											</tr>
											<?php $i++;
											$flag++;
										}
									}
								} ?><?php if ($flag == 0) {
										echo "<tr bgcolor='#fff'><td colspan='7' style='color:#FF0000;'>No Record Found</td>
                         </tr>";
									} else { ?>
											<tr bgcolor="#fff">
												<td colspan="7"><input type="hidden" name="hidsr_no" id="hidsr_no" value="<?php echo $record['seq_no']; ?>" />
													<p align="center"><input type="submit" name="Save" value="Save" class="search"></p>
												</td>
											</tr>
										<?php }
								} else { ?>
										<tr bgcolor="#fff">
											<td colspan="7" style="color:#FF0000;">No Record Found</td>
										</tr>
									<?php  } ?>


						</tbody>

					</table>
				</form>
			<?php } ?>

		</div>
	</div>
</div>