<?php
ini_set('memory_limit', '2048M');
// Checked for PHP5.6 / 8.x compatibility
$stkdesc = array();
$groupcodelist = array();
$tagged = array();
include("../../operainfo/getq2.php");

$x=getsecurity();
$companyid=trim(safetext2(isset($_GET['companyid']) ? $_GET['companyid'] : ''));
$cid=$companyid;
$status=safetext2(isset($_GET['status']) ? $_GET['status'] : '');
$hostcompanyid=trim(safetext2(isset($_GET['hostcompanyid']) ? $_GET['hostcompanyid'] : ''));
$filter=safetext2(isset($_GET['filter']) ? $_GET['filter'] : '');
$blanks=safetext2(isset($_GET['blanks']) ? $_GET['blanks'] : '');
$sql = "SELECT * FROM stocklist_tarriffs WHERE hostcompany_id = ?";
$stmt = pdoquery($sql, array($hostcompanyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$ustk=$row['stock_ref'];
	$stkdesc[$ustk]=$row['OperaDescription'];
	if ($stkdesc[$ustk]=='') {
	$stkdesc[$ustk]=$row['pricedesc'];
	}
}
$sql = "SELECT * FROM viamed_customer_stock_references_groupcodes WHERE hostcompanyid = ? AND companyid = ? AND active = '0' ORDER BY groupcode";
$stmt = pdoquery($sql, array($hostcompanyid, $companyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$groupcode=$row['groupcode'];
	$groupcodelist[$groupcode]=$row['groupcode'];
	}
$sql = "SELECT * FROM viamed_customer_stock_references_groupcodes_links , viamed_customer_stock_references_groupcodes WHERE viamed_customer_stock_references_groupcodes_links.groupcode = viamed_customer_stock_references_groupcodes.groupcode AND viamed_customer_stock_references_groupcodes.hostcompanyid = ? AND viamed_customer_stock_references_groupcodes.companyid = ?";
$stmt = pdoquery($sql, array($hostcompanyid, $companyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$stkref=$row['stockref'];
	$groupcode=$row['groupcode'];

	$link=$stkref."AXA".$groupcode;
	$tagged[$link]=1;

	}
print "<div class=\"sectionbreak\"><center>Tag Stock References $filter ";
print "<input type=button value='Blanks' onclick=\"tagstock('$filter','1')\"></center></div>";
print "<br><table class=CSSTable_n2>";
$headertag="<tr><td>Stock Ref</td><td>Supplier Ref</td><td size=16%>Description</td>";

foreach ($groupcodelist as $groupcode) {
	if ($filter=='' || $filter==$groupcode) {
$headertag .= "<td><input type=button value='$groupcode' onclick=\"tagstock( '$groupcode' ,'$blanks')\"></td>";
}
}
print "$headertag</tr>";

// Second header row with FeedBack buttons under each groupcode
$feedbackheader = "<tr><td></td><td></td><td></td>";
foreach ($groupcodelist as $groupcode) {
	if ($filter=='' || $filter==$groupcode) {
		$feedbackheader .= "<td><input type=button value='FeedBack' onclick=\"window.open('/intranet/databases/suppliers/productgroupsfeedback.php".$x."&hostcompanyid=".$hostcompanyid."&companyid=".$companyid."&groupcode=".urlencode($groupcode)."', '_blank')\"></td>";
	}
}
print $feedbackheader."</tr>";

// Third header row with Raw Report links under each groupcode
$rawheader = "<tr><td></td><td></td><td></td>";
foreach ($groupcodelist as $groupcode) {
	if ($filter=='' || $filter==$groupcode) {
		$rawheader .= "<td><a href='/intranet/databases/stock_groups_returns/groupstats_raw.php".$x."&hostcompanyid=".$hostcompanyid."&companyid=".$companyid."&group=".urlencode($groupcode)."' target='_blank'>Raw Report</a></td>";
	}
}
print $rawheader."</tr>";
$count=0;
$sql = "SELECT * FROM viamed_customer_stock_references WHERE hostcompanyid = ? AND companyid = ? AND status > '0' ORDER BY stock_ref";
$stmt = pdoquery($sql, array($hostcompanyid, $companyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$ustk=$row['stock_ref'];
    if (isset($stkdesc[$ustk]) && $stkdesc[$ustk] && $filter){
	$pos=strpos($stkdesc[$ustk],$filter,0);
	} else {
		$pos=0;
	}

		
		if ($count > 10 ) {
			$count=0;
			print "$headertag</tr>";
			print $feedbackheader."</tr>";
			print $rawheader."</tr>";
			}			
   $showline="";
	$rowblank=true;
	$stkdesc[$ustk] = isset ( $stkdesc[$ustk] ) ? $stkdesc[$ustk] : '';
	$showline .= "<tr>";
	$showline .=  "<td>$ustk $pos</td>";
	$showline .=  "<td>".$row['customer_ref']."</td>";
	$showline .=  "<td>".$stkdesc[$ustk]."</td>";

	foreach ($groupcodelist as $groupcode) {
		if ($filter=='' || $filter==$groupcode) {
	$link=$ustk."AXA".$groupcode;
	$tagged[$link]= isset ( $tagged[$link] ) ? $tagged[$link] : 0 ;
	if ($tagged[$link]==1) {
		$isticked="CHECKED";
		$rowblank=false;
		} else {
		$isticked="";	
		}
		$showline .=   "<td><input type=checkbox name=\"tag$ustk\" id=\"tag$ustk\" value='$groupcode' onclick=\"setgroupcode('$ustk','$groupcode')\" $isticked></td>";
		}
	}
	$showline .= "</tr>";

	if ($rowblank || $blanks=='') {
		 echo $showline;
		 $count++;
		}
}

?>
