<?
/* index.php: $pubmodule, $pubtask, $pubitemid */
/*
$pubsubjectid = $_REQUEST["subjectid"];
$pubtypeid = $_REQUEST["typeid"];
$pubgroupid = $_REQUEST["groupid"];
*/
$pubprojectid = $_REQUEST["pid"];
$pubtype = $_REQUEST["type"];

$pubpoid = $_REQUEST["poid"];
$pubdoid = $_REQUEST["doid"];

// filter forms..
/*
$filtersessionname=$_POST['sessionname'];
$filteruserpkey=$_POST['userpkey'];
$filterstartdate=$_POST['startdate'];
$filterstopdate=$_POST['stopdate'];
$realstartdate=converttounix($filterstartdate);
$realstopdate=converttounix($filterstopdate)+86399;
*/

$pubpageno1 = $_REQUEST['pageno1'];
if ($pubpageno1=="") $pubpageno1=1;

?>

<script type="text/javascript">

// ................................................................................
// user-interaction handlers..
// ................................................................................

function do_button_add() {
  document.location='index.php?module=do&task=add';
}

function do_button_edit(dopkey) {
  document.location='index.php?module=do&task=edit&itemid='+dopkey;
}

function do_button_view(dopkey) {
  document.location='index.php?module=do&task=view&itemid='+dopkey;
}

function do_button_delete(dopkey) {
  document.location='index.php?module=do&task=delete&itemid='+dopkey;
}

function do_button_additem(dopkey) {
  document.location='index.php?module=do&task=additem&doid='+dopkey;
}

function do_button_edititem(dopkey,itempkey) {
  document.location='index.php?module=do&task=edititem&doid='+dopkey+'&itemid='+itempkey;
}

function do_button_deleteitem(dopkey,itempkey) {
  document.location='index.php?module=do&task=deleteitem&doid='+popkey+'&itemid='+itempkey;
}

function do_button_cancel(dopkey) {
  document.location='index.php?module=do&task=cancel&itemid='+dopkey;
}

function do_button_save() {
  if (validateForm()==true) {
    document.doform.job.value='do_add'; document.doform.submit();
  }
}

function do_button_update() {
  if (validateForm()==true) {
    document.doform.job.value='do_edit'; document.doform.submit();
  }
}

function do_button_updatedos() {
  if (validateForm()==true) {
    if (submitForm()==true) {
      document.doform.job.value='do_add'; document.doform.submit();
    }
  }
}

function do_button_deletedos(itemno) {
  if (validateForm()==true) {
    if (submitForm()==true) {
      if (confirm("Are you sure to Delete the selected DO Item?")) {
        document.doform.job.value='do_remove'; document.doform.submit();
      } else {
        cancelItem(itemno);
      }
    }
  }
}

function do_button_closepo() {
  if (confirm("Are you sure to Close this PO?")) {
    document.doform.job.value='do_closepo'; document.doform.submit();
  }
}

function do_button_purge() {
  document.doform.job.value='do_remove'; document.doform.submit();
}

function do_button_saveitem() {
  if (validateForm()==true) {
    document.doform.job.value='do_additem'; document.doform.submit();
  }
}

function do_button_updateitem() {
  if (validateForm()==true) {
    document.doform.job.value='do_edititem'; document.doform.submit();
  }
}

function do_button_purgeitem() {
  document.doform.job.value='do_removeitem'; document.doform.submit();
}

function do_button_backtolist() {
  document.location='index.php?module=do&task=main';
}

function do_button_back() {
  history.go(-1);
}

</script>

<?

// ................................................................................
// model..
// ................................................................................

include_once('models/doModel.php');
include_once('models/poModel.php');
$mydoModel=new doModel;
$mypoModel=new poModel;

?>

<?

// ................................................................................
// data retrieval functions..
// ................................................................................


// ................................................................................
// data modifications functions..
// ................................................................................


// ................................................................................
// default to list view if none specified..
// ................................................................................
if ($pubtask=="") $pubtask="main";

// ................................................................................
// display page header..
// ................................................................................

$pagetitle="DO";
if (($pubtask=="main")||($pubtask=="submenu")) {
  $subtitle="List";
} else if ($pubtask=="list") {
  $subtitle="List";
} else if ($pubtask=="add") {
  $subtitle="Add DO";
} else if ($pubtask=="edit") {
  $subtitle="Edit DO";
} else if ($pubtask=="view") {
  $subtitle="View DO";
} else if ($pubtask=="delete") {
  $subtitle="Delete DO";
} else if ($pubtask=="additem") {
  $subtitle="Add DO Item";
} else if ($pubtask=="edititem") {
  $subtitle="Edit DO Item";
} else if ($pubtask=="viewitem") {
  $subtitle="View DO Item";
} else if ($pubtask=="deleteitem") {
  $subtitle="Delete DO Item";

} else {
  $subtitle="Error: Invalid Option";
}

$headercaption=$pagetitle." ··» ".$subtitle;
include_once('header.php');

include_once('views/general-header.tpl');


// TODO: Later to show list of available variables here if in DEBUG mode!!

// ................................................................................
// display main area..
// ................................................................................

if (($pubtask=="main")||($pubtask=="submenu")) {

  //$po_list = $mypoModel->getlist_pobyproject($pubprojectpkey);
  ////$do_list = $mydoModel->getlist_dobypo($pubitemid);
  //include_once('views/do-pos.tpl');

  //$do_list = $mydoModel->getlist_do($pubprojectpkey);
  $po_list = $mypoModel->getlist_pobyproject($pubprojectpkey, "1", "2", $filterpono, $filterpodate, $filtervendorname);
  include_once('views/do-list.tpl');

} else if ($pubtask=="list") {

  $do_list = $mydoModel->getlist_dobypo($pubpoid);
  include_once('views/do-list.tpl');

} else if (($pubtask=="add")||($pubtask=="edit")) {

  /*
  include_once('models/itqModel.php');
  $myitqModel=new itqModel;

  $supplier_list = $myitqModel->getlist_itqsuppliersbyproject($pubprojectpkey);
  foreach ($supplier_list as $supplier_data) {
    $objsupplierpkeylist .= ",'".$supplier_data['itq_supplierpkey']."'";
  }
  $objsupplierpkeylist = substr($objsupplierpkeylist,1);

  $do_data = $mydoModel->getdata_do($pubitemid);
  $item_list = $mydoModel->getlist_doitems($pubitemid);

  if ($pubtask=="add") {
    $po_data = $mypoModel->getdata_po($pubpoid);
    $do_data['do_popkey']=$po_data['po_pkey'];
    $do_data['do_date']=gettimestamp();
  }
  include_once('views/do-edit.tpl');
  */

  include_once('models/supplierModel.php');
  include_once('models/mroModel.php');
  include_once('models/itqModel.php');
  $mysupplierModel=new supplierModel;
  $mymroModel=new mroModel;
  $myitqModel=new itqModel;

  $po_data = $mypoModel->getdata_po($pubitemid);
  $po_items = $mypoModel->getlist_poitems($pubitemid);

  $supplier_data = $mysupplierModel->getdata_supplier($po_data['po_supplierpkey']);
  $mro_data = $mymroModel->getdata_mro($po_data['po_mropkey']);
  $itq_data = $myitqModel->getdata_itq($po_data['po_itqpkey']);

  include_once('views/do-add.tpl');

} else if ($pubtask=="view") {

  $do_data = $mydoModel->getdata_do($pubitemid);
  $item_list = $mydoModel->getlist_doitems($pubitemid);
  include_once('views/do-view.tpl');

} else if ($pubtask=="delete") {

  $do_data = $mydoModel->getdata_do($pubitemid);
  $item_list = $mydoModel->getlist_doitems($pubitemid);
  include_once('views/do-view.tpl');

} else if (($pubtask=="additem")||($pubtask=="edititem")) {

  include_once('models/itqModel.php');
  $myitqModel=new itqModel;

  include_once('models/mroModel.php');
  $mymroModel=new mroModel;

  $do_data = $mydoModel->getdata_do($pubdoid);
  $approval_data = $myitqModel->getlist_itqevaluationapprovalbysupplier($pubprojectpkey, $do_data['do_supplierpkey']);

  $objtradepkeys = "";
  foreach ($approval_data as $myitem) {
    $mro_data = $mymroModel->getdata_mroitem($myitem['approval_itempkey']);
    $objtradepkeys .= ";".$mro_data['item_subsubtradepkey'];
  }
  $objtradepkeys = substr($objtradepkeys,1);
  $item_data = $mydoModel->getdata_doitem($pubitemid);
  include_once('views/do-edititem.tpl');

} else if ($pubtask=="viewitem") {

  $do_data = $mydoModel->getdata_do($pubpoid);
  $item_data = $mydoModel->getdata_doitem($pubitemid);
  include_once('views/do-viewitem.tpl');

} else if ($pubtask=="deleteitem") {

  $do_data = $mydoModel->getdata_do($pubdoid);
  $item_data = $mydoModel->getdata_doitem($pubitemid);
  include_once('views/do-viewitem.tpl');

} else {
  include_once('views/general-invalidtask.php');
}

// ...............................................................................
// display button options area..
// ...............................................................................

$prevurl = explode('/',$_SESSION['Xpreviousurl']);
$prevfile = $prevurl[count($prevurl)-1];
if ($prevfile=='inputController.php') $backstatus="style='display: none;'";

echo "<br>";

if (($pubtask=="main")||($pubtask=="submenu")) {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_do_edit']==1)||($pubuserrole>=2)) {
    //echo "<button name=\"adddo\" id=\"adddo\" onClick=\"do_button_add();\"><img src='gfx/options/b_add2.png' class='actionbutton'>&nbsp;&nbsp;Add DO</button>&nbsp;";
  }
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"do_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="list") {

  echo "<br><hr size=1>";
  //echo "<button name=\"adddo\" id=\"adddo\" onClick=\"do_button_add('".$pubpoid."');\"><img src='gfx/options/b_add2.png' class='actionbutton'>&nbsp;&nbsp;Add DO</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"do_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="add") {

  echo "<br><hr size=1>";
  //echo "<button name=\"save\" id=\"save\" onClick=\"do_button_save();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save DO</button>&nbsp;";
  if (($pubuseraccess['access_do_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"update\" id=\"update\" onClick=\"do_button_updatedos();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update DO</button>&nbsp;";
  }
  if (($pubuseraccess['access_po_close']==1)||($pubuserrole>=2)) {
    echo "<button name=\"closepo\" id=\"closepo\" onClick=\"do_button_closepo();\"><img src='gfx/options/b_stop.png' class='actionbutton'>&nbsp;&nbsp;Close PO</button>&nbsp;";
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_backtolist();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO List</button>&nbsp;";
  //echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="edit") {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"do_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update DO</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_button_backtolist();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO List</button>&nbsp;";
  //echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_backtolist();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="view") {

  echo "<br><hr size=1>";
  echo "<table border=0 cellspacing=0 cellpadding=0 width=928>";
  echo "<tr valign=top>";
  echo "<td valign=top width=700>";
  echo "<button name=\"edit\" id=\"edit\" onClick=\"do_button_edit('".$pubitemid."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"do_button_delete('".$pubitemid."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"do_button_backtolist();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO List&nbsp;</button>&nbsp;";
  echo "</td>";
  echo "<td valign=top width=228 style='text-align: right;'>";
  if (($pubuseraccess['access_do_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_cancel('".$pubitemid."');\"><img src='gfx/options/b_stop.png' class='actionbutton'>&nbsp;&nbsp;Cancel DO</button>&nbsp;";
  }
  echo "</td>";
  echo "</tr>";
  echo "</table>";
  echo "<hr size=1><br>";

  /*
  echo "<br><hr size=1>";
  echo "<button name=\"edit\" id=\"edit\" onClick=\"do_button_edit('".$pubitemid."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"do_button_delete('".$pubitemid."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"do_button_backtolist();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO List&nbsp;</button>&nbsp;";
  if (($pubuseraccess['access_do_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_cancel('".$pubitemid."');\"><img src='gfx/options/b_stop.png' class='actionbutton'>&nbsp;&nbsp;Cancel DO</button>&nbsp;";
  }
  echo "<br><hr size=1><br>";
  */

} else if ($pubtask=="delete") {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS DO?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"do_button_purge();\">&nbsp;Yes, Delete DO&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";

} else if ($pubtask=="additem") {

  echo "<br><hr size=1>";
  echo "<button name=\"save\" id=\"save\" onClick=\"do_button_saveitem();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save DO Item</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_button_back();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO Details</button>&nbsp;";
  //echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="edititem") {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"do_button_updateitem();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update DO Item</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_button_back();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO Details</button>&nbsp;";
  //echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="viewitem") {

  echo "<br><hr size=1>";
  echo "<button name=\"edit\" id=\"edit\" onClick=\"do_button_edititem('".$pubdoid."','".$pubitemid."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"do_button_deleteitem('".$pubdoid."','".$pubitemid."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_button_back();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to DO Details</button>&nbsp;";
  //echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"do_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="deleteitem") {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS DO ITEM?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"do_button_purgeitem();\">&nbsp;Yes, Delete DO Item&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"do_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";

}

// ................................................................................
// display page footer..
// ................................................................................

include_once('views/general-footer.tpl');

?>
