<?php
include("../../operainfo/getq.php");

$x=getsecurity();
$companyid=trim(safetext2(isset($_POST['companyid']) ? $_POST['companyid'] : ''));
$cid=$companyid;
$status=safetext2(isset($_POST['status']) ? $_POST['status'] : '');

include ("showaddress.php");
$usedoctype=substr (basename( $_FILES['uploadedfile']['name']),-4);
$usedocname="$status".$cid."$companyid"."_$vui";
$target_path = "/home/network/intrastats/intranet/databases/crm/xlsuploads/$usedocname$usedoctype";
print " <br>";
print "$usedocname <br>";
print " $usedoctype<br>";
print " $target_path<br>";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
	
}


$hostcompanyid = '';
$s="SELECT companyid FROM viamedquoteaddress where ID=?";
$stmt = pdoquery($s, array($companyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$hostcompanyid=$row['companyid'];
}

$currid = array();
$s="SELECT currency,id FROM global_companys_currencys where hostcompanyid=?";
$stmt = pdoquery($s, array($hostcompanyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$curr=$row['currency'];
	$currid[$curr]=$row['id'];
}


$isvalid = array();
$s="SELECT stock_ref FROM stocklist_tarriffs where hostcompany_id=?";
$stmt = pdoquery($s, array($hostcompanyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$stkref=$row['stock_ref'];
	$isvalid[$stkref]=1;
}

// Debug: show what's in FILES
print_r($_FILES);

$csvfilename = $_FILES['uploadedfile']['tmp_name'];

$custref = array();
$custdesc = array();
$custprice = array();
$s="SELECT stock_ref, customer_ref, customerdescription, rawprice FROM viamed_customer_stock_references where companyid=? and status=?";
$stmt = pdoquery($s, array($companyid, $status));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$stkref=$row['stock_ref'];
	$custref[$stkref]=$row['customer_ref'];
	$custdesc[$stkref]=$row['customerdescription'];
	$custprice[$stkref]=$row['rawprice'];
}

$testline = "<table width=100%>";
$fp = fopen ("$csvfilename","r");
while ($data = fgetcsv ($fp, 1000, ",")) {
$num = count ($data);
$count='0';
if ($data[0]<>'CIDNumber of Supplier') {
$testline .= "<tr>";
while ($count<($num))
{
$data[$count]= safetext2($data[$count]);
$testline .= "<td>$data[$count]</td>";

$count++;
}
$testline .= "</tr>";
$curr=$data[5];
$stkref=$data[1];
if ($isvalid[$stkref]==1) {
$s="SELECT Stock_ref, customer_ref, opera_account, status, addedby, addedon, customerdescription, companyid, hostcompanyid, rawprice, currenyid FROM viamed_customer_stock_references where hostcompanyid=? and companyid=? and stock_ref=? and status=?";
$stmt = pdoquery($s, array($hostcompanyid, $companyid, $stkref, $status));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num = count($rows);
if ($num==0) {
	// NEW Record can simply Insert
	$i="INSERT INTO viamed_customer_stock_references (Stock_ref , customer_ref , opera_account , status , addedby , addedon , customerdescription , companyid , hostcompanyid , rawprice , currenyid) VALUES (?, ?, '', ?, ?, ?, ?, ?, ?, ?, ?)";
	pdoquery($i, array($stkref, $data[2], $status, $vui, $nowis, $data[3], $companyid, $hostcompanyid, $data[4], $currid[$curr]));
	print "$i";
} else {
	// Has Record need to update history, and update line
	foreach ($rows as $row) {
		$i="INSERT INTO viamed_customer_stock_references_history (Stock_ref , customer_ref , opera_account , status , addedby , addedon , customerdescription , companyid , hostcompanyid , rawprice , currenyid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		pdoquery($i, array($row['Stock_ref'], $row['customer_ref'], $row['opera_account'], $row['status'], $row['addedby'], $row['addedon'], $row['customerdescription'], $row['companyid'], $row['hostcompanyid'], $row['rawprice'], $row['currenyid']));
		//print "$i";
	}
	
	$u="UPDATE viamed_customer_stock_references set addedby=?, addedon=?, customer_ref=?, customerdescription=?, companyid=?, hostcompanyid=?, rawprice=?, currenyid=? where hostcompanyid=? and companyid=? and stock_ref=?";
	pdoquery($u, array($vui, $nowis, $data[2], $data[3], $companyid, $hostcompanyid, $data[4], $currid[$curr], $hostcompanyid, $companyid, $stkref));
	//print "$u<br>";
}



/// DO WHAT EVER PER LINE IN HERE,
// EG.
// $i="INSERT INTO viamed_xref (stock_ref , oem , oem_pn , type , xrefcode , importtag)  VALUES ('$data[0]','$data[1]','$data[2]','$data[3]','Sampling Lines','d1520557261')";
//print "$i<br>";
}
}
}
$testline .= "</table>";

// Display DAta Toggle //
 echo $testline;
?>
</body>
</html>

