<?php
// Checked for PHP5.6 / 8.x compatibility
?>
<title>Quotes</title>
<?php
$stockquotelist = array(); // Ensure initialized
$qtystockinquotes = array(); // Optional: initialize if used in foreach
// Quotes and Proformas are midnight updated via the server with direct DBF access to files.
// opera_B_IHEAD and opera_B_ITRAN python files does the leg work
// they run twice, once to bring in VIAMED data and once to bring in VST data. BOTH in same file.

include("../../operainfo/getq.php");
$userid = safetext2(isset($_GET['userid']) ? $_GET['userid'] : '');
$filter = safetext2(isset($_GET['filter']) ? $_GET['filter'] : '');
if ($userid <> '') {
    $userid = $vui;
}
?>
<script type="text/javascript">
function getdetails(quote) {

var butloc = "option"+quote;
var link = "getquotedetails.php<?php print $x;?>&quote="+quote;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.open("GET","getquotedetails.php<?php print $x;?>&"+"quote="+quote,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(quote).innerHTML=xmlhttp.responseText;
	document.getElementById(butloc).innerHTML="<button onclick=\"hidedetails('"+quote+"')\">Hide</button>";
	//document.getElementById(butloc).innerHTML=link ;
    }
  }
}
</script>

 

<script type="text/javascript">
function getdocuments(orderid) {
//document.getElementById(orderid).innerHTML='hi';
var link = "../review_neworders/getorderdocs.php<?php print $x."&orderid=";?>"+orderid;
//document.getElementById(orderid).innerHTML=link;
	if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET",link,true);
//document.getElementById('results').innerHTML='hi2';
xmlhttp.send();


xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	document.getElementById(orderid).innerHTML=xmlhttp.responseText;
    }
  }

}
</script>
 


<script type="text/javascript">
function updatestatus(quote , stkref) {

var boxid="status"+quote+""+stkref;
var chanceid="chance"+quote+""+stkref;
var notesid="notes"+quote+""+stkref;
var userid="user"+quote+""+stkref;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 var link ="updatequotestatus.php<?php print $x;?>&quote="+quote+"&stkref="+stkref+"&value="+document.getElementById(boxid).value+"&notes="+document.getElementById(notesid).value+"&respuser="+document.getElementById(userid).value+"&chance="+document.getElementById(chanceid).value;
xmlhttp.open("GET",link,true);
//document.getElementById('gg').innerHTML=link;
xmlhttp.send();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById('BUT'+quote).innerHTML='Updated';
	getdetails(quote);
    }
  }
}
</script>

<script type="text/javascript">
function hidedetails(quote ) {
var butloc = "option"+quote;
    document.getElementById(quote).innerHTML='';
	document.getElementById(butloc).innerHTML="<button onclick=\"getdetails('"+quote+"')\">Details</button>";
}
</script>

<script type="text/javascript">
function getquotes( stkref) {

var boxid="quotes"+stkref;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.open("GET","getquotes.php<?php print $x;?>&stkref="+stkref,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(boxid).innerHTML=xmlhttp.responseText;
 
    }
  }
}
</script>

<script type="text/javascript">
function sethost(hostid) {
 window.open("index.php<?php print $x;?>&hostcompanyid="+hostid,"_self");
}
</script>

<div id='gg'></div>

<?php

$visitlognotes = array();
$sql = "SELECT orderid, timestampraised, status, contact, memo FROM viamedquotestatus WHERE orderid > '0'";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $orderid = $row['orderid'];
    $dprndate = date("d M Y", $row['timestampraised']);
	$visitlognotes[$orderid] = isset ( $visitlognotes[$orderid] ) ? $visitlognotes[$orderid] : '';
    $visitlognotes[$orderid] .= "<br>" . $dprndate . " - " . $row['status'] . " - " . $row['contact'] . " <br>" . $row['memo'] . "<br>";
}

$useropts = "<option value=''></option>";
$sql = "SELECT id, username FROM pw WHERE active='1' ORDER BY username";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $useropts .= "<option value='" . $row['id'] . "'>" . ucwords($row['username']) . "</option>\n";
}

$isallowed = array();
$sql = "SELECT company_id FROM pw_companys WHERE vui = ?";
$stmt = pdoquery($sql, array($vui));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $allowedhostid = $row['company_id'];
    $isallowed[$allowedhostid] = $allowedhostid;
}

$msg = "<center><h3>Please Select Host Company</h3>";
$msg .= "<table width=50%>";

$sql = "SELECT global_companys.id as id, global_companys.fullname as fullname
        FROM global_companys, pw_companys
        WHERE pw_companys.company_id = global_companys.id
          AND pw_companys.vui = ?
          AND global_companys.active = '0'
        ORDER BY name";
$stmt = pdoquery($sql, array($vui));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $hostid = $row['id'];
    if (isset($isallowed[$hostid]) && $isallowed[$hostid] <> '') {
        $msg .= "<TR bgcolor=\"#92adc8\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#92adc8';\">";
        $msg .= "<td><a onclick=\"sethost('" . $hostid . "')\">" . $global_company_logo48[$hostid] . "</a></td><td><h1><button onclick=\"sethost('" . $hostid . "')\">" . ucwords($row['fullname']) . "</button></h1></td></tr>";
    }
}
$msg .= "</table>";

$hostcompanyid = safetext2(isset($_GET['hostcompanyid']) ? $_GET['hostcompanyid'] : '');
if ($hostcompanyid == '') {
    echo $msg;
}
print "<div id='hostsel'></div>";

// CAN BE OPTIMISED to only search active quotes!!
$sql = "SELECT id, quote, addedon FROM viamed_quotes_history WHERE daylink = ''";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $quote = $row['quote'];
    $qlist[$quote] = $quote;
    $datelink = date("dmy", $row['addedon']);
    $usql = "UPDATE viamed_quotes_history SET daylink = ? WHERE id = ?";
    pdoquery($usql, array($datelink, $row['id']));
}

$chasecount = array();
$sql = "SELECT count(*) as tot, quote FROM viamed_quotes_history GROUP BY quote, daylink";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $quote = $row['quote'];
    $chasecount[$quote] = $row['tot'];
}

// First lets See if any new quotes need adding to the list from the New system
$quoteinviamed_quotes = array();
$sql = "SELECT quote FROM viamed_quotes";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $orderid = $row['quote'];
    $quoteinviamed_quotes[$orderid] = 1;
}

$accountname = array();
$accountac = array();
$sql = "SELECT ID, COMPANY, KEY5 FROM viamedquoteaddress";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $companyid = $row['ID'];
    $accountname[$companyid] = $row['COMPANY'];
    $accountac[$companyid] = $row['KEY5'];
}

// Determine the hosttopricelist currency
$currency = array();
$prefix = array();
$sql = "SELECT global_companys_bankaccounts.companyid, global_companys_bankaccounts.pricelist, global_companys_bankaccounts.currency, global_companys.prefix
        FROM global_companys_bankaccounts, global_companys
        WHERE global_companys.id = global_companys_bankaccounts.companyid";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $hostid = $row['companyid'];
    $pricelist = $row['pricelist'];
    $hostpricelink = $hostid . "A" . $pricelist;
    $currency[$hostpricelink] = $row['currency'];
    $prefix[$hostid] = $row['prefix'];
}

$quotevalue = array();
$ordernotes = array();
$usecol = array();
$sql = "SELECT
        viamedquote_orders.id as orderid,
        viamedquote_orders.id as notes,
        viamedquote_orders_details.pricelist as pricelist,
        viamedquote_orders_details.unitprice as unitprice,
        viamedquote_orders_details.qty as qty,
        viamedquote_orders.hostcompany as hostcompany_id
        FROM viamedquote_orders, viamedquote_orders_details
        WHERE (viamedquote_orders.quote_proforma = '1' OR viamedquote_orders.quote_proforma = '2')
          AND viamedquote_orders.order_stage = '1'
          AND viamedquote_orders_details.orderid = viamedquote_orders.id
        GROUP BY viamedquote_orders_details.pricelist";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $quote = $row['orderid'];
    $ordernotes[$quote] = $row['notes'];
    $quotevalue[$quote] = (isset($quotevalue[$quote]) ? $quotevalue[$quote] : 0) + ($row['unitprice'] * $row['qty']);
    $hostid = $row['hostcompany_id'];
    $pricelist = $row['pricelist'];
    $hostpricelink = $hostid . "A" . $pricelist;
    $usecol[$quote] = "<TR bgcolor=\"#92adc8\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#92adc8';\">";
    if (isset($currency[$hostpricelink]) && $currency[$hostpricelink] == 'EUR') {
        $usecol[$quote] = "<TR bgcolor=\"#99cc99\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#99cc99';\">";
    }
    if (isset($currency[$hostpricelink]) && $currency[$hostpricelink] == 'USD') {
        $usecol[$quote] = "<TR bgcolor=\"" . $pastelyellow . "\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='" . $pastelyellow . "';\">";
    }
}

$quoteagreedby = array();
$quoteagreedon = array();
$sql = "SELECT id, quoteagreedby, quoteagreedon FROM viamedquote_orders WHERE quote_proforma = '1'";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $quote = $row['id'];
    $quoteagreedby[$quote] = getusername($row['quoteagreedby']);
    $quoteagreedon[$quote] = $row['quoteagreedon'];
}

$quoteslist = array();
$quotelink = array();
$quotehost = array();
$ordercompany = array();
$usequotedate = array();
$usename = array();
$usenameid = array();
$quotedisplay = array();
$quotedoc = array();
$accounts = array();
$sql = "SELECT id, quote_proforma, order_stage, hostcompany, company_id, notes, createdon, paperworkname, quoteagreedby, quoteagreedon
        FROM viamedquote_orders
        WHERE (quote_proforma = '1' OR quote_proforma = '2') AND order_stage = '1'";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $quote = $row['id'];
    $quoteslist[$quote] = $quote;
    $quotelink[$quote] = $quote;
    $hostid = $row['hostcompany'];
    $companyid = $row['company_id'];
    $quotehost[$quote] = $row['hostcompany'];
    $ordernotes[$quote] = $row['notes'];
    $accounts[$quote] = isset($accountac[$companyid]) ? $accountac[$companyid] : '';
    $ordercompany[$quote] = $companyid;
    $accountname[$quote] = isset($accountname[$companyid]) ? $accountname[$companyid] : '';
    $usequotedate[$quote] = date("d M Y", $row['createdon']);
    $usename[$quote] = getusername($row['paperworkname']);
    $usenameid[$quote] = $row['paperworkname'];
    $quoteagreedby[$quote] = getusername($row['quoteagreedby']);
    $quoteagreedon[$quote] = $row['quoteagreedon'];
    if ($row['quote_proforma'] == 1) {
        $quotedisplay[$quote] = "Q" . $prefix[$hostid] . $quote . "(" . (isset($quotevalue[$quote]) ? $quotevalue[$quote] : 0) . ")";
        $quotedoc[$quote] = "Q" . $prefix[$hostid] . $quote;
    } else {
        $quotedisplay[$quote] = "M" . $prefix[$hostid] . $quote . "(" . (isset($quotevalue[$quote]) ? $quotevalue[$quote] : 0) . ")";
        $quotedoc[$quote] = "M" . $prefix[$hostid] . $quote;
    }
    if (!isset($quoteinviamed_quotes[$quote]) || $quoteinviamed_quotes[$quote] <> 1) {
        $isql = "INSERT INTO viamed_quotes (quote) VALUES (?)";
        pdoquery($isql, array($quote));
    }
}

$pricesymbol = array();
$sql = "SELECT companyid, pricelist, bankcurrsymbol FROM global_companys_bankaccounts";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $hostid = $row['companyid'];
    $pricelist = $row['pricelist'];
    $pricelink = $hostid . "SSS" . $pricelist;
    $pricesymbol[$pricelink] = $row['bankcurrsymbol'];
}

// Can now go pick up the viamed_quotes data including any new entries
$quotestatus = array();
$quotechance = array();
$quoteby = array();
$quoteon = array();
$quoteoncheck = array();
$statusrespuser = array();
$statusrespuserid = array();
$quotenotes = array();
$logged = array();
$sql = "SELECT quote, status, chance, updatedby, updatedon, responsibleuser, notes FROM viamed_quotes";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $quote = $row['quote'];
    $quotestatus[$quote] = $row['status'];
    $quotechance[$quote] = $row['chance'];
    // viamed_quotes has no addedby column; use updatedby only
    $quoteby[$quote] = ($row['updatedby'] != 0) ? getusername($row['updatedby']) : '';
    if ($row['updatedon'] > 0) {
        $quoteon[$quote] = date("d M Y", $row['updatedon']);
        $quoteoncheck[$quote] = $row['updatedon'] + (86400 * 25);
    } else {
        $quoteon[$quote] = '';
    }
    $statusrespuser[$quote] = getusername($row['responsibleuser']);
    $statusrespuserid[$quote] = $row['responsibleuser'];
    $quotenotes[$quote] = $row['notes'];
    $logged[$quote] = 1;
}

// Just pick up Address Details FROM Opera Address
$creditlimit = array();
$sql = "SELECT KEY5, Opera_Creditlimit FROM viamedquoteaddress WHERE hideme = ''";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $useaccount = $row['KEY5'];
    $creditlimit[$useaccount] = $row['Opera_Creditlimit'];
}

$accountstop = array();
$sql = "SELECT KEY5 FROM viamedquoteaddress WHERE stoponaccount = '1'";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $useaccount = $row['KEY5'];
    $accountstop[$useaccount] = 'Yes';
}

$quoteopts = "<option value=''></option>";
$statusdescription = array();
$sql = "SELECT id, description FROM viamed_quotes_status ORDER BY description";
$stmt = pdoquery($sql, array());
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $qoptid = $row['id'];
    $statusdescription[$qoptid] = ucwords($row['description']);
    $quoteopts .= "<option value='" . $row['id'] . "'>" . $statusdescription[$qoptid] . "</option>";
}

$chanceopts = "
<option value='0'>0%</option>
<option value='25'>25%</option>
<option value='50'>50%</option>
<option value='75'>75%</option>
<option value='100'>100%</option>
";

$usercode = '';
if ($userid <> '') {
    $usercode = getusername($userid) . "'s ";
}

print "<center><h3>" . $usercode . " Outstanding Quotes and proforma's</h3>";
print "<a href=\"qnp.php" . $x . "\"><input type=button value='Summary Screen'> </a><br><br>";
print "<a href=\"qnpcan.php" . $x . "\"><input type=button value='Cancelled List'> </a><br><br>";
print "<table class=n3>";
print "<tr>";
print "<td>KEY</td></tr>";
print "<TR bgcolor=\"#92adc8\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#92adc8';\"><td>GBP</td></tr>";
print "<TR bgcolor=\"#99cc99\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#99cc99';\"><td>Euros</td></tr>";
print "<TR bgcolor=\"" . $pastelyellow . "\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='" . $pastelyellow . "';\"><td>Dollars</td></tr>";
print "<TR bgcolor=\"" . $pastelred . "\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='" . $pastelred . "';\"><td>Account on Stop</td></tr>";
print "</tr></table><br>";
print "<table class=n3 width=95%><tr><td>Chased</td><td>25D+</td><td></td><td></td>
<td>Quote / Proforma</td>
<td>Users / Status <div id='addstat'></div> / Notes</td><Td>Status Update by</td><td>Status on</td><td>Approved</td><td>Update</td></tr>";

foreach ($quoteslist as $quote) {
    $line = '';
    $hostid = $quotehost[$quote];
    if (isset($isallowed[$hostid]) && $isallowed[$hostid] <> '' && ($hostcompanyid == '' || $hostcompanyid == $hostid)) {
        $col = isset($usecol[$quote]) ? $usecol[$quote] : '';

        $ac = $accounts[$quote];

        if (isset($accountstop[$ac]) && $accountstop[$ac] <> '') {
            $col = "<TR bgcolor=\"" . $pastelred . "\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='" . $pastelred . "';\">";
        }

        if ($col == '') {
            $col = "<TR bgcolor=\"#92adc8\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#92adc8';\">";
        }

        $qoptid = isset($quotestatus[$quote]) ? $quotestatus[$quote] : '';

        if ($userid == '' || $userid == $statusrespuserid[$quote]) {

            $line .= $col;

            $line .= "<td valign=top><a href=\"quotehistory.php" . $x . "&quote" . $quote . "\">";

            $line .= (isset($chasecount[$quote]) ? $chasecount[$quote] : '') . "</a></td>";

            $line .= "<td valign=top>";

            $udate2 = $usequotedate[$quote];

            try {
                $year = substr($udate2, 0, 4);
                $month = substr($udate2, 5, 2);
                $day = substr($udate2, 8, 2);

                $expdate = mktime(1, 1, 1, $month + 0, $day + 25, $year + 0);
                $expiredate = date("d/M/Y", $expdate);
                if (isset($quoteoncheck[$quote]) && $quoteoncheck[$quote] > 0) {
                    $expiredate2 = "\nNext Check Date: " . date("d/M/Y", $quoteoncheck[$quote]);
                } else {
                    $expiredate2 = "";
                }
                $iscross = 0;
                if ($expdate < $nowis && (!isset($quoteoncheck[$quote]) || $quoteoncheck[$quote] == '' || $quoteoncheck[$quote] < $nowis)) {
                    $line .= "<a href=\"index.php" . $x . "&filter=1\"><img src=\"/intranet/images/small_cross.png\" width=16 height=16 title=\"Expire Date: " . $expiredate . $expiredate2 . "\"></a>";
                    $iscross = 1;
                } else {
                    $line .= "<img src=\"/intranet/images/small_tick.png\" width=16 height=16 title=\"Expire Date: " . $expiredate . $expiredate2 . "\">";
                }
            } catch (Exception $e) {
                $line .= $e;
            }

            if (isset($statusrespuser[$quote]) && $statusrespuser[$quote] == 'Other') {
                $statusrespuser[$quote] = 'Office';
            }

            $line .= "</td><td valign=top>";
            if (isset($isrep[$quote]) && $isrep[$quote] == 1) {
                $line .= "<img src=\"../../images/menu/warranty_sm.png\" width=16 height=16>";
            }
            $line .= "</td>";

            $line .= "<td valign=top><div id='option" . $quote . "'><button onclick=\"getdetails('" . $quote . "')\">" . $quote . " Quote Details</button></div><br><button onclick=\"getdocuments('" . $quote . "')\">Documents</button></td><td valign=top><table><tr><td>";
            $line .= $global_company_logo24[$hostid] . $global_company_name[$hostid];

            $line .= " Ref:</td><td> ";

            $line .= "&nbsp" . neworderlinks($x, $quote, $float, $size) . "" . operadoclinks($x, $quotedoc[$quote], '') . "
	" . $quotedisplay[$quote] . "</td></tr><tr><td valign=top>Entered Date</td><td>&nbsp " . $usequotedate[$quote] . " &nbsp </td></tr><tr><td>Entered By</td><td>" . $usename[$quote] . "</td></tr>";
            $line .= "<tr><td valign=top>Account</td><td> &nbsp " . $ac . " &nbsp </td></tr><tr><td>Name</td><td valign=top>&nbsp";
            if ($ordercompany[$quote] > 0) {
                $line .= linkcrm($x, $ordercompany[$quote], '') . "CID" . $ordercompany[$quote] . " ";
            }
            $line .= " " . $accountname[$quote] . "</td></tr></table></td>";
            $line .= "<td valign=top>";
            if ($statusrespuserid[$quote] == 0 && $usenameid[$quote] != 0) {
                $statusrespuserid[$quote] = $usenameid[$quote];
                $statusrespuser[$quote] = $usename[$quote];
            }
            $line .= "<table><tr><td valign=top>User</td><Td valign=top><select id='user" . $quote . "'><option value='" . $statusrespuserid[$quote] . "'>" . $statusrespuser[$quote] . "</option>" . $useropts . "</select>";
            $line .= "";
            $line .= "</td></tr>";

            $line .= "<tr><Td valign=top>Status</td><td valign=top><select id='status" . $quote . "'><option value='" . $qoptid . "'>" . (isset($statusdescription[$qoptid]) ? $statusdescription[$qoptid] : '') . "</option>" . $quoteopts . "</select>";
            $line .= "</tr>";

            $line .= "<tr><Td valign=top>Chance</td><td valign=top><select id='chance" . $quote . "'><option value='" . (isset($quotechance[$quote]) ? $quotechance[$quote] : 0) . "'>" . (isset($quotechance[$quote]) ? $quotechance[$quote] : 0) . "%</option>" . $chanceopts . "</select>";
            $line .= "</tr>";

            $prnnotes = str_replace("\n", "<br>", isset($quotenotes[$quote]) ? $quotenotes[$quote] : '');
            $line .= "<tr><Td valign=top>Notes</td><td>";
            if (isset($visitlognotes[$quote]) && $visitlognotes[$quote]) {
                $line .= $visitlognotes[$quote];
            }
            $line .= "<b>Order Notes</b>: " . (isset($ordernotes[$quote]) ? $ordernotes[$quote] : '') . " <br><br><b>Quote Notes</b> " . $prnnotes . "<br>Add Quote Notes:<textarea id='notes" . $quote . "' rows=6 cols=48></textarea></td>
	</tr></table></td><td valign=top>";
            if (isset($quoteby[$quote]) && $quoteby[$quote] == 'Other') {
                $line .= "  ";
            } else {
                $line .= isset($quoteby[$quote]) ? $quoteby[$quote] : '';
            }
            $line .= "</td><Td valign=top>" . (isset($quoteon[$quote]) ? $quoteon[$quote] : '') . "</td>";
            $line .= "<td valign=top>";
            if (isset($quoteagreedon[$quote]) && $quoteagreedon[$quote] > 0) {
                $ddate = date("d M Y", $quoteagreedon[$quote]);
                $line .= (isset($quoteagreedby[$quote]) ? $quoteagreedby[$quote] : '') . " " . $ddate;
            }
            $line .= "</td>";
            $line .= "<td valign=top><div id='BUT" . $quote . "'><button onclick=\"updatestatus('" . $quote . "','')\">Update</button></div></td>";
            $line .= "</tr>";
            $line .= "<tr><Td colspan=16 valign=top><div id='" . $quote . "'></div></td></tr><tr><td colspan=16> &nbsp </td></tr>";

            $line .= "</tr>";

            if ($filter == '' || $iscross == 1) {
                print $line;
            }
        }
    }
}
print "</table>";

if ($userid == '') {
    ksort($stockquotelist);
    print "<h3>Stock Items in Quotes and Proforma's</h3><table class=n3 width=50%><tr><td></td><td>Stock Reference</td><td>#Quotes</td><td>Total Qty in Quotes</td><td>More Information</td></tr>";
    foreach ($stockquotelist as $stkref => $count) {
        print "<TR bgcolor=\"#92adc8\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='#92adc8';\">";
        print "<td valign=top><button onclick=getquotes('" . $stkref . "')>Info</button><td valign=top>" . $stkref . "</td>
	<td valign=top>" . $count . "</td><td valign=top>" . (isset($qtystockinquotes[$stkref]) ? $qtystockinquotes[$stkref] : '') . "</td><td valign=top><div id='quotes" . $stkref . "'></div></tr>";
    }
    print "</table>";
}
?>
