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

$pubmroid = $_REQUEST["mroid"];
$pubpoid = $_REQUEST["poid"];

// filter forms..
$filterpono=$_POST['pono'];
$filterpodate=$_POST['podate'];
$filtervendorname=$_POST['vendorname'];

$filterpono1=$_POST['pono1'];
$filterpodate1=$_POST['podate1'];
$filtervendorname1=$_POST['vendorname1'];
/*
$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;
$pubpageno2 = $_REQUEST['pageno2'];
if ($pubpageno2=="") $pubpageno2=1;
?>

<script type="text/javascript">

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

function po_button_selectmro() {
  document.location='index.php?module=po&task=mro';
}

function po_button_add(projectpkey) {
  document.location='index.php?module=po&task=add&pid='+projectpkey;
}

function po_button_edit(popkey) {
  document.location='index.php?module=po&task=edit&itemid='+popkey;
}

function po_button_view(popkey) {
  document.location='index.php?module=po&task=view&itemid='+popkey;
}

function po_button_delete(popkey) {
  document.location='index.php?module=po&task=delete&itemid='+popkey;
}

function po_button_additem(popkey) {
  document.location='index.php?module=po&task=additem&poid='+popkey;
}

function po_button_edititem(popkey,itempkey) {
  document.location='index.php?module=po&task=edititem&poid='+popkey+'&itemid='+itempkey;
}

function po_button_deleteitem(popkey,itempkey) {
  document.location='index.php?module=po&task=deleteitem&poid='+popkey+'&itemid='+itempkey;
}

function po_button_submit(popkey) {
  if (confirm("Are you sure to submit this PO?")) {
    document.location='index.php?module=po&task=submit&itemid='+popkey;
  }
}

function po_button_cancel(popkey) {
  if (confirm("Are you sure to Cancel this PO?")) {
    document.location='index.php?module=po&task=cancel&itemid='+popkey;
  }
}

function po_button_save() {
  if (validateForm()==true) {
    document.poform.job.value='po_add'; document.poform.submit();
  }
}

function po_button_update() {
  if (validateForm()==true) {
    document.poform.job.value='po_edit'; document.poform.submit();
  }
}

function po_button_purge() {
  document.poform.job.value='po_remove'; document.poform.submit();
}

function po_button_saveitem() {
  if (validateForm()==true) {
    document.poform.job.value='po_additem'; document.poform.submit();
  }
}

function po_button_updateitem() {
  if (validateForm()==true) {
    document.poform.job.value='po_edititem'; document.poform.submit();
  }
}

function po_button_purgeitem() {
  document.poform.job.value='po_removeitem'; document.poform.submit();
}

function po_button_print(popkey) {
  window.open('views/rpt_po.php?itemid='+popkey,'printwindow', 'menubar=0,toolbar=0,scrollbars=1,resizable=0,location=0,width=800,height=600');
}

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

function po_button_backtolist2() {
  document.location='index.php?module=po&task=main&tabid=1';
}

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

</script>

<?

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

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

?>

<?

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


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


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

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

$pagetitle="PO";
if (($pubtask=="main")||($pubtask=="submenu")) {
  $subtitle="PO List";
} else if ($pubtask=="list") {
  $subtitle="PO List";
} else if ($pubtask=="mro") {
  $subtitle="Select MRO for New PO";
} else if ($pubtask=="do") {
  $subtitle="View DOs";
} else if ($pubtask=="add") {
  $subtitle="Add PO";
} else if ($pubtask=="edit") {
  $subtitle="Edit PO";
} else if ($pubtask=="view") {
  $subtitle="View PO";
} else if ($pubtask=="delete") {
  $subtitle="Delete PO";
} else if ($pubtask=="additem") {
  $subtitle="Add PO Item";
} else if ($pubtask=="edititem") {
  $subtitle="Edit PO Item";
} else if ($pubtask=="viewitem") {
  $subtitle="View PO Item";
} else if ($pubtask=="deleteitem") {
  $subtitle="Delete PO Item";
} else if ($pubtask=="submit") {
  $subtitle="Submit PO";
} else if ($pubtask=="cancel") {
  $subtitle="Cancel PO";

} 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, "1", "2", $filterpono, $filterpodate, $filtervendorname);
 
  $po_completed = $mypoModel->getlist_pobyproject($pubprojectpkey, "3", "3", $filterpono1, $filterpodate1, $filtervendorname1);
/*
 $po_completed = $mypoModel->getlist_pobyproject($pubprojectpkey, "3", "3");
 */
 $po_history = $mypoModel->getlist_pobyproject($pubprojectpkey, "9", "9");
  include_once('views/po-list.tpl');

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

  $po_list = $mypoModel->getlist_pobyproject($pubprojectpkey);
  include_once('views/po-list.tpl');

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

  $mro_list = $mymroModel->getlist_mrobyitqstatus($pubprojectpkey,3);
  include_once('views/po-mros.tpl');

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

  $po_data = $mypoModel->getdata_po($pubitemid);
  if ($pubtask=="add") {
    $po_data['po_date']=gettimestamp();
    $mro_data = $mymroModel->getdata_mro($pubmroid);
    $itq_data = $myitqModel->getdata_itqbymro($pubmroid);
  } else {
    $mro_data = $mymroModel->getdata_mro($po_data['po_mropkey']);
    $itq_data = $myitqModel->getdata_itqbymro($po_data['po_mropkey']);
    $item_list = $mypoModel->getlist_poitems($po_data['po_pkey']);
  }
  include_once('views/po-edit.tpl');

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

  include_once('models/supplierModel.php');
  include_once('models/doModel.php');
  $mysupplierModel=new supplierModel;
  $mydoModel=new doModel;

  $po_data = $mypoModel->getdata_po($pubitemid);
  $po_items = $mypoModel->getlist_poitems($pubitemid);
  $supplier_data = $mysupplierModel->getdata_supplier($po_data['po_supplierpkey']);

  include_once('views/po-dos.tpl');

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

  include_once('models/supplierModel.php');
  $mysupplierModel=new supplierModel;

  include_once('models/projectModel.php');
  $myprojectModel=new projectModelPMS;

  $po_data = $mypoModel->getdata_po($pubitemid);
  $supplier_data = $mysupplierModel->getdata_supplier($po_data['po_supplierpkey']);
  $personnel_data = $myprojectModel->getdata_personnel($po_data['po_personnelpkey']);
  $item_list = $mypoModel->getlist_poitems($pubitemid);
  //include_once('views/po-view.tpl');

  $pubtype="3";		// just default :P
  include_once('views/po-approval.tpl');

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

  include_once('models/supplierModel.php');
  $mysupplierModel=new supplierModel;

  include_once('models/projectModel.php');
  $myprojectModel=new projectModelPMS;

  $po_data = $mypoModel->getdata_po($pubitemid);
  $supplier_data = $mysupplierModel->getdata_supplier($po_data['po_supplierpkey']);
  $personnel_data = $myprojectModel->getdata_personnel($po_data['po_personnelpkey']);
  $item_list = $mypoModel->getlist_poitems($pubitemid);
  include_once('views/po-view.tpl');

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

  $po_data = $mypoModel->getdata_po($pubpoid);
  $approval_data = $myitqModel->getlist_itqevaluationbyapproval($po_data['po_itqpkey'], $po_data['po_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 = $mypoModel->getdata_poitem($pubitemid);
  include_once('views/po-edititem.tpl');

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

  $item_data = $mypoModel->getdata_poitem($pubitemid);
  include_once('views/po-viewitem.tpl');

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

  $item_data = $mypoModel->getdata_poitem($pubitemid);
  include_once('views/po-viewitem.tpl');

} else if (($pubtask=="submit")&&(($pubuseraccess['access_po_edit']==1)||($pubuserrole>=2))) {

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

  $opresult = $mypoModel->updatestatus_po($pubitemid,"1");
  $g=goto('index.php?module=po&task=list');
  echo $g;
  exit;

} else if (($pubtask=="cancel")&&(($pubuseraccess['access_po_edit']==1)||($pubuserrole>=2))) {

  // NOT USED now :P

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

  //$opresult = $mypoModel->updatestatus_po($pubitemid,"4");

  $opresult = $mypoModel->cancel_po($pubitemid);
  $g=goto('index.php?module=po&task=list');
  echo $g;
  exit;

} 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>";
  //echo "<button name=\"addpo\" id=\"addpo\" onClick=\"po_button_selectmro();\"><img src='gfx/options/b_add2.png' class='actionbutton'>&nbsp;&nbsp;Add PO</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"po_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=\"addpo\" id=\"addpo\" onClick=\"po_button_selectmro();\"><img src='gfx/options/b_add2.png' class='actionbutton'>&nbsp;&nbsp;Add PO</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"po_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=="mro") {

  echo "<br><hr size=1>";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_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=="do") {

  echo "<br><hr size=1>";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_button_backtolist2();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to PO List</button>&nbsp;";
  echo "<br><hr size=1><br>";

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

  echo "<br><hr size=1>";
  echo "<button name=\"save\" id=\"save\" onClick=\"po_button_save();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save PO</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 PO List</button>&nbsp;";
  echo "<br><hr size=1><br>";

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

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"po_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update PO</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 PO List</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>";
  if (($pubuseraccess['access_po_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"edit\" id=\"edit\" onClick=\"po_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=\"po_button_delete('".$pubitemid."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  }
  echo "<button name=\"print\" id=\"print\" onClick=\"po_button_print('".$pubitemid."');\">&nbsp;<img src='gfx/options/b_print1.png' class='actionbutton'>&nbsp;&nbsp;Print&nbsp;</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"po_button_backtolist();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to PO List&nbsp;</button>&nbsp;";
  echo "</td>";
  echo "<td valign=top width=228 style='text-align: right;'>";
  if (($pubuseraccess['access_po_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_button_cancel('".$pubitemid."');\"><img src='gfx/options/b_stop.png' class='actionbutton'>&nbsp;&nbsp;Cancel PO</button>&nbsp;";
  }
  echo "</td>";
  echo "</tr>";
  echo "</table>";
  echo "<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 PO?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"po_button_purge();\">&nbsp;Yes, Delete PO&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_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=\"po_button_saveitem();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save PO 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 PO Details</button>&nbsp;";
  //echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_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=\"po_button_updateitem();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update PO 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 PO Details</button>&nbsp;";
  //echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_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=\"po_button_edititem('".$pubpoid."','".$pubitemid."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"po_button_deleteitem('".$pubpoid."','".$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 PO Details</button>&nbsp;";
  //echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"po_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 PO ITEM?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"po_button_purgeitem();\">&nbsp;Yes, Delete PO Item&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"po_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');

?>