<?
include_once('../hdr_general.php');
/* **************************************************************************************************** */
if (!isset($_REQUEST["module"])) $_REQUEST["module"] = "general";
if (!isset($_REQUEST["task"])) $_REQUEST["task"] = "main";
$pubmodule = $_REQUEST["module"];
$pubtask = $_REQUEST["task"];
$pubitemid = $_REQUEST["itemid"];
$pubsubid = $_REQUEST["subid"];
$pubtype = $_REQUEST["type"];
$pubitemno = $_REQUEST["itemno"];
$publevel = $_REQUEST["level"];
if ($publevel=="") $publevel="0";

$filtersubtrade = $_POST['subtrade'];
$filtersubsubtrade = $_POST['subsubtrade'];
$filterall = $_POST['all'];
if ($filterall=="") $filterall="no";

/* **************************************************************************************************** */
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../css/<?=$_SESSION['Xcssfile'];?>" type="text/css" media=all>
<title>Purchasing Online (ZIKAY)</title>

<script type="text/javascript">

function trade_tree_select(node,tlevel) {

  var parentfield = document.getElementById('parent'+node);
  var tradefield = document.getElementById('trade'+node);
  var codefield = document.getElementById('code'+node);
  var descfield = document.getElementById('desc'+node);
  var desc2field = document.getElementById('desc2'+node);
  var unitfield = document.getElementById('unit'+node);
  var quantityfield = document.getElementById('quantity'+node);
  var balancefield = document.getElementById('balance'+node);
  var budgetflagfield = document.getElementById('budgetflag'+node);

  opener.document.forms[0].item_subtradepkey.value = parentfield.value;
  opener.document.forms[0].item_subsubtradepkey.value = tradefield.value;
  opener.document.forms[0].item_code.value = codefield.value;
  //opener.document.forms[0].item_description.value = descfield.value;
  opener.document.forms[0].item_description.value = desc2field.innerHTML;
  opener.document.forms[0].item_unit.value = unitfield.value;
  //opener.document.forms[0].item_level.value = "4";
  opener.document.forms[0].item_totalquantity.value = quantityfield.value;
  opener.document.forms[0].item_totalbalance.value = balancefield.value;
  opener.document.forms[0].item_budgetflag.value = budgetflagfield.value;

  //if (tlevel==3) {
  //  opener.document.forms[0].item_level.value = "3";
  //  opener.document.forms[0].item_subtradepkey.value = tradefield.value;
  //  opener.document.forms[0].item_subsubtradepkey.value = "";
  //} else if (tlevel==4) {
  //  opener.document.forms[0].item_level.value = "4";
  //}
  self.close();
}

function updateSelect(seedurl) {
}

function cancelSelect() {
  self.close();
}

</script>

</head>
<body leftmargin=5 topmargin=5 style="background: #dddddd;">
<table border=0 cellspacing=0 cellpadding=0 width=578>

<tr valign=top>
<td valign=top width=100%>

  <?
  include_once('../models/mblModel.php');
  include_once('../models/mroModel.php');
  $mymblModel=new mblModel;
  $mymroModel=new mroModel;

  include_once('../models/tradeModel.php');
  $mytradeModel=new tradeModel;

  if ($pubsubid!="") {
    $trade_list = $mytradeModel->getlist_tradesbyparent($pubsubid,"","tradecode");
  } else {
    //$trade_list = $mytradeModel->getlist_tradesbylevel(4,"","tradecode");
    if (($filtersubtrade=="")&&($filtersubsubtrade=="")&&($filterall=="no")) {
      $trade_list = $mytradeModel->getlist_tradesbylevelfiltered(4,"xxx","xxx","tradecode");
    } else {
      $trade_list = $mytradeModel->getlist_tradesbylevelfiltered(4,$filtersubtrade,$filtersubsubtrade,"tradecode");
    }
  }

  // also get current subsubtrades in mro
  $item_list = $mymroModel->getlist_mroitems($pubitemid);
  ?>

  <? if ($pubsubid=="") { ?>
    <table border=1 cellspacing=1 cellpadding=2 width=568 style="border: 1px inset;">
    <form action="ibox_selecttrade-mro.php?subid=<?=$pubsubid;?>&type=<?=$pubtype;?>&itemid=<?=$pubitemid;?>&itemno=<?=$pubitemno;?>" method="post" name="filterform" id="filterform">
    <tr valign=top>
    <td valign=top width=568 colspan=5 class=listfilterheader>Filters</td>
    </tr>
    <tr valign=top>
    <td valign=top width=60 class=listcontent>Subtrade:</td>
    <td valign=top width=120 class=listcontent><input name="subtrade" id="subtrade" value="<?=$filtersubtrade;?>" style="width: 110px;"></td>
    <td valign=top width=60 class=listcontent>Sub-sub:</td>
    <td valign=top width=120 class=listcontent><input name="subsubtrade" id="subsubtrade" value="<?=$filtersubsubtrade;?>" style="width: 110px;"></td>
    <td valign=top width=208 class=listcontent>
    <input name="all" id="all" type="hidden" value="no">
    <button name="filter" id="filter" type="submit" style="width: 80px;">&nbsp;<img src='../gfx/options/b_find.png' class='actionbutton'>&nbsp;Search&nbsp;</button>&nbsp;
    <button name="showall" id="showall" style="width: 90px;" onClick="unFilter();">&nbsp;<img src='../gfx/options/b_clear.png' class='actionbutton'>&nbsp;Show All&nbsp;</button>&nbsp;
    </td>
    </tr>
    <tr valign=top>
    <td valign=top width=568 colspan=5 class=listcontent><img src="../pixel.gif" height=1 width=1></td>
    </tr>
    </form>
    </table>
  <? } ?>

  <table border=1 cellspacing=1 cellpadding=2 width=568 style="border: 1px inset;">

  <tr valign=top>
  <td valign=top width=568 class=listheader style="border-bottom: 1px solid;">
  Select Trade :
  </td>
  </tr>

  <? $catindex=0; ?>
  <? foreach ($trade_list as $mytrade) { ?>

    <?
    $budgetexists="1";
    $totalquantity=0;
    $totalbalance=0;
    $islumpsum=true;
    //$totalsubbalance=0;
    //$totalsubsubbalance=0;

    // for each trade, get existence in MBL (self or parent) and get budget and balance

    $mblsubsubquantity = $mymblModel->getquantity_mblsubsubtrade($pubprojectpkey, $mytrade['tradepkey']);
    if ($mblsubsubquantity>0) {
      $islumpsum=false;
      $mrosubsubquantity = $mymroModel->getquantity_mrosubsubtrade($pubprojectpkey, $mytrade['tradepkey']);
      $totalquantity = $mblsubsubquantity;
      if ($mblsubsubquantity>=$mrosubsubquantity) {
        //$totalsubsubbalance = $mblsubsubquantity - $mrosubsubquantity;
        $totalbalance = $mblsubsubquantity - $mrosubsubquantity;
      }
    } else {
      $mblsubquantity = $mymblModel->getquantity_mblsubtrade($pubprojectpkey, $mytrade['tradeparentpkey']);
      if ($mblsubquantity>0) {
        $mrosubquantity = $mymroModel->getquantity_mrosubtrade($pubprojectpkey, $mytrade['tradeparentpkey']);
        $totalquantity = $mblsubquantity;
        if ($mblsubquantity>=$mrosubquantity) {
          //$totalsubbalance = $mblsubquantity - $mrosubquantity;
          $totalbalance = $mblsubquantity - $mrosubquantity;
        }
      } else {
        $budgetexists="0";	// vo/subcon!
      }
    }
    ?>

    <?
    $itemexists=false;
    foreach ($item_list as $myitem) {
      if ($myitem['item_subsubtradepkey']==$mytrade['tradepkey']) {
        $itemexists=true;
        break;
      }
    }
    ?>

    <?
    // subcon now can contain budgeted items..
    //if ((($pubtype=="0")||(($pubtype=="1")||($pubtype=="2")&&(($mblsubquantity>0)||($mblsubsubquantity>0)))||(($pubtype=="3")||($pubtype=="4")&&(($mblsubquantity==0)&&($mblsubsubquantity==0))))&&($itemexists==false)) {
   //if ((($pubtype=="0")||($pubtype=="4")||(($pubtype=="1")||($pubtype=="2")&&(($mblsubquantity>0)||($mblsubsubquantity>0)))||(($pubtype=="3")&&(($mblsubquantity==0)&&($mblsubsubquantity==0))))&&($itemexists==false)) {
    ?>

      <?
      // check existence of brother items (subsubs in mbl sharing this item's parent)
      $othersubsub = $mymblModel->getcount_mblsubsubbysubtrade($pubprojectpkey, $mytrade['tradeparentpkey'], $mytrade['tradepkey']);

      //if (((($pubtype=="1")||($pubtype=="2"))&&($budgetexists=="0")&&($othersubsub>0)&&($pubitemno!="1"))||(($pubtype=="4")&&($pubitemno!="1")&&(($mblsubquantity==0)&&($mblsubsubquantity==0)))) {
      //if ((($pubtype=="1")||($pubtype=="2"))&&($budgetexists=="0")&&($othersubsub>0)&&($pubitemno!="1")) {
        // if normal/exceeded and item not in MBL but others in the same sub are and not 1st item in mro, do not display
        // and also (OR) if subcon but not 1st item (so restrict to subtrade)
      //} else {
      ?>

      <tr valign=top>
      <td valign=top width=928 class=newscontent style="border-bottom: 1px solid;">

        <table border=0 cellspacing=0 cellpadding=0 width=558>
        <tr valign=top>
        <td valign=top class=newscontent width=498>
        <input id="parent<?=$mytrade['tradepkey'];?>" name="parent<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=$mytrade['tradeparentpkey'];?>">
        <input id="trade<?=$mytrade['tradepkey'];?>" name="trade<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=$mytrade['tradepkey'];?>">
        <input id="code<?=$mytrade['tradepkey'];?>" name="code<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=unescape($mytrade['tradecode']);?>">
        <input id="desc<?=$mytrade['tradepkey'];?>" name="desc<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=unescape($mytrade['tradedescription']);?>">
        <span name="desc2<?=$mytrade['tradepkey'];?>" id="desc2<?=$mytrade['tradepkey'];?>" style="display: none;"><?=unescape($mytrade['tradedescription']);?></span>
        <input id="unit<?=$mytrade['tradepkey'];?>" name="unit<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=$mytrade['tradeunit'];?>">
        <input id="quantity<?=$mytrade['tradepkey'];?>" name="quantity<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=number_format($totalquantity,4);?>">
        <input id="balance<?=$mytrade['tradepkey'];?>" name="balance<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=number_format($totalbalance,4);?>">
        <input id="budgetflag<?=$mytrade['tradepkey'];?>" name="budgetflag<?=$mytrade['tradepkey'];?>" type="hidden" value="<?=$budgetexists;?>">
        <img id="item<?=$mytrade['tradepkey'];?>" src="../gfx/tree/item.png" border=0 width=16 height=16 title="Item" alt="Item" align=left style="display: block;">
        &nbsp;&nbsp;<b><?=$mytrade['tradecode']."</b> - ".unescape($mytrade['tradedescription']);?>&nbsp;&nbsp;
        </td>
        <td valign=top class=newscontent width=60>
        <img src="../gfx/tree/b_select.png" border=0 width=16 height=16 title="Select" alt="Select" style="display: inline; cursor: pointer;" onClick="javascript:trade_tree_select('<?=$mytrade['tradepkey'];?>',<?=$publevel;?>);">&nbsp;<br>
        </td>
        </tr>
        </table>

      </td>
      </tr>

      <?// } ?>

    <?// } ?>

  <? } ?>

  </table>

</td>
</tr>
</table>

</body>
</html>


<script type="text/javascript">

function unFilter() {
  document.filterform.subtrade.value="";
  document.filterform.subsubtrade.value="";
  document.filterform.all.value="yes";
  document.filterform.submit();
}

</script>
