<?php
$PageSecurity = 5;
$title = _('Vendor Master');
include('includes/SQL_CommonFunctions.inc');
include('cocoon_config.php');
$_SESSION['modid'] = $_REQUEST['modid'];
$_SESSION['frmid'] = $_REQUEST['frmid'];
$frmpermission = $px->GetFileAccess($_SESSION['modid'], $_SESSION['frmid'], $_SESSION['ACGRoleId']);
$_SESSION['frmAdd'] = $frmpermission[0];
$_SESSION['frmEdit'] = $frmpermission[1];
$_SESSION['frmDelete'] = $frmpermission[2];
$_SESSION['frmView'] = $frmpermission[3];
if ($_REQUEST['action'] == 'delete' && $_REQUEST['ID'] != '') {
	$checkLedger = DB_fetch_array(DB_query("Select AccountsID from accountledger where vender_client='" . $_REQUEST['ID'] . "' and ledgertype='vendor'", $db));
	$ledgerId = $checkLedger['AccountsID'];
	$checkLedgerEntries = DB_query("Select * from account_book_details where ledgerid='" . $ledgerId . "'", $db);
	$num = DB_num_rows($checkLedgerEntries);

	if ($num > 0) {
		echo "<script>alert('Sorry this vendor cannot be deleted as its ledger is already in use');</script>";
		$s->pageLocation("index.php?route=Supplier_list" . $rurl);
	} else {
		$delete1 = DB_query("Delete from suppliers where id='" . $_REQUEST['ID'] . "'", $db);
		$delete2 = DB_query("Delete from contact_information_master where master_code='" . $_REQUEST['ID'] . "' and master_prefix='S'", $db);
		$delete3 = DB_query("Delete from accountledger where AccountsID='" . $ledgerId . "'", $db);
		$delete2 = mysqli_query($link1, "Delete from suppliers where nippon_supplier_id='" . $_REQUEST['ID'] . "'");

		$s->pageLocation("index.php?route=Supplier_list" . $rurl);
	}
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
	<div class="tab_div">
		<h1><?php echo $page_title; ?></h1>
		<div class="centre" id="slidingDiv">
			<form action="index.php?route=Supplier_list&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>" method="post">
				<table border="0" cellpadding="3" cellspacing="0" width="100%">
					<tr class="row1">
						<td width="13%" align="right">Branch</td>
						<?php $result = DB_query("SELECT locationname, loccode FROM locations where  loccode in('" . $_SESSION['AssignedBranch'] . "') ", $db); ?>
						<td width="20%"><select name="site">
								<option value="">select</option>
								<?php
								while ($myrow = DB_fetch_array($result)) {
									$p = '';
									if ($_REQUEST['site'] == $myrow['loccode'])
										$p = "selected";
								?>
									<option VALUE="<?php echo $myrow['loccode']; ?>" <?php echo $p; ?>><?php echo $myrow['locationname']; ?></option>
								<?php } ?>
								</option>
							</select></td>
						<td width="13%" align="right">Vendor Code</td>
						<td width="20%"><input type="text" value="<?php echo $_REQUEST['vendor_code']; ?>" name="vendor_code" /></td>
						<td width="13%" align="right">Vendor Name</td>
						<td><!--<input type="text" style="width:150px;" value="<?php echo $_REQUEST['vendor_name']; ?>"  name="vendor_name" /> -->
							<input type="text" id="vendor_name" name="vendor_name" list="vendor1" placeholder="Vendor Name" value="<?php echo $_REQUEST['vendor_name']; ?>" autocomplete="off">
							<datalist id="vendor1">
								<?php
								$q = DB_query("select vendor_name,id from suppliers where CompanyId='" . $_SESSION['ACGCompanyId'] . "' order by vendor_name", $db);
								while ($res = DB_fetch_assoc($q)) {
									echo "<option value='" . $res['vendor_name'] . "' if($vendor_name=='" . $res['vendor_name'] . "') echo 'selected';></option>";
								}
								?>
							</datalist>
						</td>
					</tr>
					<tr class="row1">
						<td align="right">Category</td>
						<td><select name="category" class="cap"><!--onChange="VendorCodeGenerator(this.value)"-->
								<option value="000">Select Category</option>
								<?php
								$get_category = DB_query("Select * from category_master", $db);
								while ($cat_res = DB_fetch_array($get_category)) {
								?>
									<option value="<?php echo $cat_res['id']; ?>" <?php echo ($_REQUEST['category'] == $cat_res['id'] ? 'selected' : '') ?>><?php echo $cat_res['name']; ?></option>
								<?php } ?>
							</select>
						</td>
						<td align="right">Item Name</td>
						<td><input type="text" id="item_name" name="item_name" list="item_name1" placeholder="Item Name" value="<?php echo $_REQUEST['client']; ?>" autocomplete="off">
							<datalist id="item_name1">
								<?php
								$q = DB_query("select * from item_master order by item_name ASC", $db);

								while ($res = DB_fetch_array($q)) {
									//$x=$res[0];
									echo "<option  data-xyz='" . $res['id'] . "' value='" . $res['item_name'] . "' if($client=='" . $res['c_name'] . "') echo 'selected';></option>";
								}
								?>
							</datalist><input type="hidden" name="bbb" id="bbb" value="<?php echo $_REQUEST['bbb']; ?>">
						</td>
						<td colspan="2"></td>
					</tr>
					<tr class="row1">
						<td style="text-align:center;" colspan="6"><input type="submit" name="submit" value="Search" class="search" /></td>
					</tr>
				</table>
			</form>
			<?php if ($_SESSION['frmAdd'] == '1') { ?>
				<p align="left">
					<a href="index.php?route=Suppliers_new&NewIssue=yes&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>"><input type="button" value="ADD VENDOR" class="search" /></a>
					<a href="index.php?route=suppiler_import&NewIssue=yes&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>"><input type="button" value="IMPORT" class="search" /></a>
				</p>
			<?php } ?>
			<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
				<tr>
					<th width="5%">S.No.</th>
					<th>Branch</th>
					<th width="15%">Vendor Code</th>
					<th width="15%">Vendor Name</th>
					<th width="10%">Category</th>
					<th width="10%">Vendor Type</th>
					<th width="10%">Under Group</th>
					<!--	<th>Site</th>
--> <!--<th>View</th>-->
					<?php if ($_SESSION['frmEdit'] == '1') { ?><th width="5%" style="text-align:center;">Edit</th><?php } ?>
					<?php if ($_SESSION['frmDelete'] == '1') { ?><th width="5%" style="text-align:center;">Delete</th><?php } ?>
				</tr>
				<?php
				//---------------------------Search Starts here-------------------------------
				if (isset($_REQUEST['submit'])) {
					$sql_search = '';
					$vendor_code1 = $_REQUEST['vendor_code'];
					$vendor_name1 = $_REQUEST['vendor_name'];
					$category1 = $_REQUEST['category'];

					$site1 = $_REQUEST['site'];
					$ItemId1 = $_REQUEST['bbb'];
					if ($vendor_code1 != '') {
						$sql_search[] = "vendor_code LIKE '" . $vendor_code1 . "%'";
					}
					if ($vendor_name1 != '') {
						$sql_search[] = "vendor_name LIKE '" . $vendor_name1 . "%'";
					}
					if ($category1 != '000') {
						$sql_search[] = "category='" . $category1 . "%'";
					}
					if ($site1 != '') {
						$sql_search[] = "FIND_IN_SET('" . $site1 . "',site)";
					}
					if ($ItemId1 != '') {
						$sql_search[] = " FIND_IN_SET($ItemId1,assigned_items)";
					}

					if ($sql_search != '') {
						$sql_search1 = implode(" AND ", $sql_search);
						$SQLSearch2 = "select * from suppliers where $sql_search1";
						$search_result = DB_query($SQLSearch2, $db);
					} else {
						$SQLSearch2 = "select * from suppliers";
						$search_result = DB_query($SQLSearch2, $db);
					}
					// echo $SQLSearch2;

				}
				$i = 0;

				if (isset($_REQUEST['submit'])) {
					//echo "inside IF IF ";
					if (DB_num_rows($search_result) > 0) {
						while ($my_search_row = DB_fetch_array($search_result)) {
							$ChkAs1 = DB_query("SELECT ReceivedFrom FROM addmrn where ReceivedFrom='" . $my_search_row['id'] . "'", $db);
							$ChkAs2 = DB_query("SELECT vendorId FROM purchorders where vendorId='" . $my_search_row['id'] . "'", $db);
							$ChkAs3 = DB_query("SELECT mst_supplierno FROM venderquotations where mst_supplierno='" . $my_search_row['id'] . "'", $db);
							$checkLedger = DB_fetch_array(DB_query("Select AccountsID from accountledger where vender_client='" . $my_search_row['id'] . "' and ledgertype='vendor'", $db));
							$ledgerId = $checkLedger['AccountsID'];
							$checkLedgerEntries = DB_query("Select * from account_book_details where ledgerid='" . $ledgerId . "'", $db);
							$num = DB_num_rows($checkLedgerEntries);
							$aks1 = DB_num_rows($ChkAs1);
							$aks2 = DB_num_rows($ChkAs2);
							$aks3 = DB_num_rows($ChkAs2);
							if ($aks1 > 0 || $aks2 > 0 || $aks3 > 0) {
								$ChkDelStatus = "No";
							} else {
								$ChkDelStatus = "Yes";
							}

				?>
							<tr>
								<td><?php echo $i + 1; ?></td>
								<td><?php $sites = explode(",", $my_search_row['site']);
									for ($j = 0; $j < count($sites); $j++) {
										echo $s->GetFieldValue('locations', "loccode='" . $sites[$j] . "'", 'locationname') . "<br>";
									}   ?></td>
								<td><?php echo $my_search_row['vendor_code'];
									echo "<br />" ?></td>
								<td><?php echo $my_search_row['vendor_name']; ?></td>
								<td><?php echo $s->getField('category_master', 'id', $my_search_row['category'], 'name'); ?></td>
								<td><?php echo $my_search_row['vendor_type']; ?></td>
								<td><?php echo $s->GetFieldValue('accountgroupmaster', "GroupId='" . $my_search_row['account_group'] . "'", 'GroupName'); ?></td>
								<!--<td><?php echo $s->getField('locations', 'loccode', $my_search_row['site'], 'locationname'); ?></td>-->
								<!--<td><?php if ($_SESSION['frmView'] == '1') { ?><div align="center"><a href="index.php?route=view_vendor&action=view&vendorcode=<?php echo $my_search_row['vendor_code']; ?>&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>"><img src="images/search.gif" height="15" width="15"></a></div><?php } else {
																																																																																								echo "No Permission";
																																																																																							} ?></td>-->
								<?php if ($_SESSION['frmEdit'] == '1') { ?>
									<td align="center">
										<div align="center"><a href="index.php?route=Suppliers_new&ID=<?php echo $my_search_row['id']; ?>&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>"><img src="images/icon-edit.png" height="15" width="15" title="Edit Document" /></a></div>
									</td><?php } ?>
								<?php if ($_SESSION['frmDelete'] == '1') { ?>
									<td align="center"><?php if ($ChkDelStatus == 'Yes') {
														?><div align="center"><a href="index.php?route=Supplier_list&action=delete&ID=<?php echo $my_search_row['id']; ?>&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>" onClick="return confirm('Are you sure you wish to delete this supplier?')"><img src="images/Icon_Delete.png" height="15" width="15" title="Delete Document" /></a></div><?php
																																																																																																								} else { ?>
											<img src="images/Icon_Delete.png" style="opacity:0.6;" height="15" width="15" title="No Permission to Delete Document" />
										<?php } ?>
									</td><?php } ?>
							</tr>
				<?php
							$i++;
						}
					}
				}

				//----------------end of Search---------------- 

				//end of page full new headings if
				//end of while loop
				?>
			</table>

			<?php
			include('includes/footer.inc');
			?>

			<script type="text/javascript">
				$(document).ready(function() {

					$(function() {
						$("#item_name").change(function() {
							var val = $('#item_name').val()
							var xyz = $('#item_name1 option').filter(function() {
								return this.value == val;
							}).data('xyz');
							//	 alert(xyz);
							// var msg = xyz ? '' + xyz : 'No Match';
							$('#bbb').val(xyz);
							// alert(msg)
						});
					})
					//alert();
					// var subjectId = $('#subjects option:selected').attr('data-subjectid'); 
					//  var subjectId = $('#subjects option:selected').attr('data-subjectid');
				});
			</script>
		</div>
	</div>
</div>